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