]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/views/views.factor
2f8bcbd8720e4203b2140e55187a2155065bf2ec
[factor.git] / basis / ui / backend / cocoa / views / views.factor
1 ! Copyright (C) 2006, 2010 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.strings
4 arrays assocs cocoa cocoa.application cocoa.classes
5 cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.types
6 cocoa.views combinators core-foundation.strings core-graphics
7 core-graphics.types core-text io.encodings.utf8 kernel literals
8 locals math math.rectangles namespaces opengl sequences threads
9 ui.gadgets ui.gadgets.private ui.gadgets.worlds ui.gestures
10 ui.private unicode.case ;
11 IN: ui.backend.cocoa.views
12
13 : send-mouse-moved ( view event -- )
14     [ mouse-location ] [ drop window ] 2bi
15     [ move-hand fire-motion yield ] [ drop ] if* ;
16
17 ! Issue #1453
18 : button ( event -- n )
19     ! Cocoa -> Factor UI button mapping
20     -> buttonNumber {
21         { 0 [ 1 ] }
22         { 1 [ 3 ] }
23         { 2 [ 2 ] }
24         [ ]
25     } case ;
26
27 CONSTANT: NSAlphaShiftKeyMask 0x10000
28 CONSTANT: NSShiftKeyMask      0x20000
29 CONSTANT: NSControlKeyMask    0x40000
30 CONSTANT: NSAlternateKeyMask  0x80000
31 CONSTANT: NSCommandKeyMask    0x100000
32 CONSTANT: NSNumericPadKeyMask 0x200000
33 CONSTANT: NSHelpKeyMask       0x400000
34 CONSTANT: NSFunctionKeyMask   0x800000
35
36 CONSTANT: modifiers {
37         { S+ $ NSShiftKeyMask }
38         { C+ $ NSControlKeyMask }
39         { A+ $ NSCommandKeyMask }
40         { M+ $ NSAlternateKeyMask }
41     }
42
43 CONSTANT: key-codes
44     H{
45         { 71 "CLEAR" }
46         { 36 "RET" }
47         { 76 "ENTER" }
48         { 53 "ESC" }
49         { 48 "TAB" }
50         { 51 "BACKSPACE" }
51         { 115 "HOME" }
52         { 117 "DELETE" }
53         { 119 "END" }
54         { 122 "F1" }
55         { 120 "F2" }
56         { 99 "F3" }
57         { 118 "F4" }
58         { 96 "F5" }
59         { 97 "F6" }
60         { 98 "F7" }
61         { 100 "F8" }
62         { 123 "LEFT" }
63         { 124 "RIGHT" }
64         { 125 "DOWN" }
65         { 126 "UP" }
66         { 116 "PAGE_UP" }
67         { 121 "PAGE_DOWN" }
68     }
69
70 : key-code ( event -- string ? )
71     dup -> keyCode key-codes at
72     [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
73
74 : event-modifiers ( event -- modifiers )
75     -> modifierFlags modifiers modifier ;
76
77 : key-event>gesture ( event -- modifiers keycode action? )
78     [ event-modifiers ] [ key-code ] bi ;
79
80 : send-key-event ( view gesture -- )
81     swap window [ propagate-key-gesture ] [ drop ] if* ;
82
83 : interpret-key-event ( view event -- )
84     NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
85
86 : send-key-down-event ( view event -- )
87     [ key-event>gesture <key-down> send-key-event ]
88     [ interpret-key-event ]
89     2bi ;
90
91 : send-key-up-event ( view event -- )
92     key-event>gesture <key-up> send-key-event ;
93
94 : mouse-event>gesture ( event -- modifiers button )
95     [ event-modifiers ] [ button ] bi ;
96
97 : send-button-down$ ( view event -- )
98     [ nip mouse-event>gesture <button-down> ]
99     [ mouse-location ]
100     [ drop window ]
101     2tri
102     [ send-button-down ] [ 2drop ] if* ;
103
104 : send-button-up$ ( view event -- )
105     [ nip mouse-event>gesture <button-up> ]
106     [ mouse-location ]
107     [ drop window ]
108     2tri
109     [ send-button-up ] [ 2drop ] if* ;
110
111 : send-scroll$ ( view event -- )
112     [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
113     [ mouse-location ]
114     [ drop window ]
115     2tri
116     [ send-scroll ] [ 2drop ] if* ;
117
118 : send-action$ ( view event gesture -- )
119     [ drop window ] dip over [ send-action ] [ 2drop ] if ;
120
121 : add-resize-observer ( observer object -- )
122     [
123         "updateFactorGadgetSize:"
124         "NSViewFrameDidChangeNotification" <NSString>
125     ] dip add-observer ;
126
127 : string-or-nil? ( NSString -- ? )
128     [ CF>string NSStringPboardType = ] [ t ] if* ;
129
130 : valid-service? ( gadget send-type return-type -- ? )
131     2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
132     [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
133
134 : NSRect>rect ( NSRect world -- rect )
135     [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
136     [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
137     2bi <rect> ;
138
139 : rect>NSRect ( rect world -- NSRect )
140     [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
141     [ drop dim>> first2 ]
142     2bi <CGRect> ;
143
144 CONSTANT: selector>action H{
145     { "undo:" undo-action }
146     { "redo:" redo-action }
147     { "cut:" cut-action }
148     { "copy:" copy-action }
149     { "paste:" paste-action }
150     { "delete:" delete-action }
151     { "selectAll:" select-all-action }
152     { "newDocument:" new-action }
153     { "openDocument:" open-action }
154     { "saveDocument:" save-action }
155     { "saveDocumentAs:" save-as-action }
156     { "revertDocumentToSaved:" revert-action }
157 }
158
159 : validate-action ( world selector -- ? validated? )
160     selector>action at
161     [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
162
163 CLASS: FactorView < NSOpenGLView
164     COCOA-PROTOCOL: NSTextInput
165
166     METHOD: void prepareOpenGL [
167         self SUPER-> prepareOpenGL
168
169         self SEL: setWantsBestResolutionOpenGLSurface:
170         -> respondsToSelector: c-bool> [
171
172             self SEL: setWantsBestResolutionOpenGLSurface: 1
173             void f "objc_msgSend" { id SEL char } alien-invoke
174
175             self SEL: backingScaleFactor
176             double f "objc_msgSend" { id SEL } alien-invoke
177
178             dup 1.0 > [
179                 gl-scale-factor set-global t retina? set-global
180                 cached-lines get-global clear-assoc
181             ] [ drop ] if
182
183         ] when
184     ] ;
185
186     ! Rendering
187     METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
188
189     ! Events
190     METHOD: char acceptsFirstMouse: id event [ 0 ] ;
191
192     METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
193
194     METHOD: void mouseExited: id event [ forget-rollover ] ;
195
196     METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
197
198     METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
199
200     METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
201
202     METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
203
204     METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
205
206     METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
207
208     METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
209
210     METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
211
212     METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
213
214     METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
215
216     METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
217
218     METHOD: void keyDown: id event [ self event send-key-down-event ] ;
219
220     METHOD: void keyUp: id event [ self event send-key-up-event ] ;
221
222     METHOD: char validateUserInterfaceItem: id event
223     [
224         self window [
225             event -> action utf8 alien>string validate-action
226             [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
227         ] [ 0 ] if*
228     ] ;
229
230     METHOD: id undo: id event [ self event undo-action send-action$ f ] ;
231
232     METHOD: id redo: id event [ self event redo-action send-action$ f ] ;
233
234     METHOD: id cut: id event [ self event cut-action send-action$ f ] ;
235
236     METHOD: id copy: id event [ self event copy-action send-action$ f ] ;
237
238     METHOD: id paste: id event [ self event paste-action send-action$ f ] ;
239
240     METHOD: id delete: id event [ self event delete-action send-action$ f ] ;
241
242     METHOD: id selectAll: id event [ self event select-all-action send-action$ f ] ;
243
244     METHOD: id newDocument: id event [ self event new-action send-action$ f ] ;
245
246     METHOD: id openDocument: id event [ self event open-action send-action$ f ] ;
247
248     METHOD: id saveDocument: id event [ self event save-action send-action$ f ] ;
249
250     METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ] ;
251
252     METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ] ;
253
254     ! Multi-touch gestures
255     METHOD: void magnifyWithEvent: id event
256     [
257         self event
258         dup -> deltaZ sgn {
259             {  1 [ zoom-in-action send-action$ ] }
260             { -1 [ zoom-out-action send-action$ ] }
261             {  0 [ 2drop ] }
262         } case
263     ] ;
264
265     METHOD: void swipeWithEvent: id event
266     [
267         self event
268         dup -> deltaX sgn {
269             {  1 [ left-action send-action$ ] }
270             { -1 [ right-action send-action$ ] }
271             {  0
272                 [
273                     dup -> deltaY sgn {
274                         {  1 [ up-action send-action$ ] }
275                         { -1 [ down-action send-action$ ] }
276                         {  0 [ 2drop ] }
277                     } case
278                 ]
279             }
280         } case
281     ] ;
282
283     METHOD: char acceptsFirstResponder [ 1 ] ;
284
285     ! Services
286     METHOD: id validRequestorForSendType: id sendType returnType: id returnType
287     [
288         ! We return either self or nil
289         self window [
290             world-focus sendType returnType
291             valid-service? [ self ] [ f ] if
292         ] [ f ] if*
293     ] ;
294
295     METHOD: char writeSelectionToPasteboard: id pboard types: id types
296     [
297         NSStringPboardType types CF>string-array member? [
298             self window [
299                 world-focus gadget-selection
300                 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
301             ] [ 0 ] if*
302         ] [ 0 ] if
303     ] ;
304
305     METHOD: char readSelectionFromPasteboard: id pboard
306     [
307         self window :> window
308         window [
309             pboard pasteboard-string
310             [ window user-input 1 ] [ 0 ] if*
311         ] [ 0 ] if
312     ] ;
313
314     ! Text input
315     METHOD: void insertText: id text
316     [
317         self window :> window
318         window [
319             text CF>string window user-input
320         ] when
321     ] ;
322
323     METHOD: char hasMarkedText [ 0 ] ;
324
325     METHOD: NSRange markedRange [ 0 0 <NSRange> ] ;
326
327     METHOD: NSRange selectedRange [ 0 0 <NSRange> ] ;
328
329     METHOD: void setMarkedText: id text selectedRange: NSRange range [ ] ;
330
331     METHOD: void unmarkText [ ] ;
332
333     METHOD: id validAttributesForMarkedText [ NSArray -> array ] ;
334
335     METHOD: id attributedSubstringFromRange: NSRange range [ f ] ;
336
337     METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
338
339     METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ] ;
340
341     METHOD: NSInteger conversationIdentifier [ self alien-address ] ;
342
343     ! Initialization
344     METHOD: void updateFactorGadgetSize: id notification
345     [
346         self window :> window
347         window [
348             self view-dim window dim<< yield
349         ] when
350     ] ;
351
352     METHOD: void doCommandBySelector: SEL selector [ ] ;
353
354     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
355     [
356         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
357         dup dup add-resize-observer
358     ] ;
359
360     METHOD: char isOpaque [ 0 ] ;
361
362     METHOD: void dealloc
363     [
364         self remove-observer
365         self SUPER-> dealloc
366     ] ;
367 ;
368
369 : sync-refresh-to-screen ( GLView -- )
370     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
371     CGLSetParameter drop ;
372
373 : <FactorView> ( dim pixel-format -- view )
374     [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
375
376 : save-position ( world window -- )
377     -> frame CGRect-top-left 2array >>window-loc drop ;
378
379 CLASS: FactorWindowDelegate < NSObject
380
381     METHOD: void windowDidMove: id notification
382     [
383         notification -> object -> contentView window
384         [ notification -> object save-position ] when*
385     ] ;
386
387     METHOD: void windowDidBecomeKey: id notification
388     [
389         notification -> object -> contentView window
390         [ focus-world ] when*
391     ] ;
392
393     METHOD: void windowDidResignKey: id notification
394     [
395         forget-rollover
396         notification -> object -> contentView :> view
397         view window :> window
398         window [
399             view -> isInFullScreenMode 0 =
400             [ window unfocus-world ] when
401         ] when
402     ] ;
403
404     METHOD: char windowShouldClose: id notification [ 1 ] ;
405
406     METHOD: void windowWillClose: id notification
407     [
408         notification -> object -> contentView
409         [ window ungraft ] [ unregister-window ] bi
410     ] ;
411
412     METHOD: void windowDidChangeBackingProperties: id notification
413     [
414
415         notification -> object dup SEL: backingScaleFactor
416         -> respondsToSelector: c-bool> [
417
418             SEL: backingScaleFactor
419             double f "objc_msgSend" { id SEL } alien-invoke
420
421             [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
422             [ 1.0 > retina? set-global ] bi
423         ] [ drop ] if
424     ] ;
425 ;
426
427 : install-window-delegate ( window -- )
428     FactorWindowDelegate install-delegate ;