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