]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/views/views.factor
ui.backend.cocoa.views: fix build error on older OSX versions.
[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.order math.rectangles namespaces opengl sequences
9 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 <<
150 os-version { 10 7 0 } after=? "retina" "non-retina" ?
151 "ui.backend.cocoa.views." prepend use-vocab
152 >>
153
154 CLASS: FactorView < BaseFactorView
155 [
156     ! Rendering
157     METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ]
158
159     ! Events
160     METHOD: char acceptsFirstMouse: id event [ 0 ]
161
162     METHOD: void mouseEntered: id event [ self event send-mouse-moved ]
163
164     METHOD: void mouseExited: id event [ forget-rollover ]
165
166     METHOD: void mouseMoved: id event [ self event send-mouse-moved ]
167
168     METHOD: void mouseDragged: id event [ self event send-mouse-moved ]
169
170     METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ]
171
172     METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ]
173
174     METHOD: void mouseDown: id event [ self event send-button-down$ ]
175
176     METHOD: void mouseUp: id event [ self event send-button-up$ ]
177
178     METHOD: void rightMouseDown: id event [ self event send-button-down$ ]
179
180     METHOD: void rightMouseUp: id event [ self event send-button-up$ ]
181
182     METHOD: void otherMouseDown: id event [ self event send-button-down$ ]
183
184     METHOD: void otherMouseUp: id event [ self event send-button-up$ ]
185
186     METHOD: void scrollWheel: id event [ self event send-scroll$ ]
187
188     METHOD: void keyDown: id event [ self event send-key-down-event ]
189
190     METHOD: void keyUp: id event [ self event send-key-up-event ]
191
192     METHOD: char validateUserInterfaceItem: id event
193     [
194         self window [
195             event -> action utf8 alien>string validate-action
196             [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
197         ] [ 0 ] if*
198     ]
199
200     METHOD: id undo: id event [ self event undo-action send-action$ f ]
201
202     METHOD: id redo: id event [ self event redo-action send-action$ f ]
203
204     METHOD: id cut: id event [ self event cut-action send-action$ f ]
205
206     METHOD: id copy: id event [ self event copy-action send-action$ f ]
207
208     METHOD: id paste: id event [ self event paste-action send-action$ f ]
209
210     METHOD: id delete: id event [ self event delete-action send-action$ f ]
211
212     METHOD: id selectAll: id event [ self event select-all-action send-action$ f ]
213
214     METHOD: id newDocument: id event [ self event new-action send-action$ f ]
215
216     METHOD: id openDocument: id event [ self event open-action send-action$ f ]
217
218     METHOD: id saveDocument: id event [ self event save-action send-action$ f ]
219
220     METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ]
221
222     METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ]
223
224     ! Multi-touch gestures
225     METHOD: void magnifyWithEvent: id event
226     [
227         self event
228         dup -> deltaZ sgn {
229             {  1 [ zoom-in-action send-action$ ] }
230             { -1 [ zoom-out-action send-action$ ] }
231             {  0 [ 2drop ] }
232         } case
233     ]
234
235     METHOD: void swipeWithEvent: id event
236     [
237         self event
238         dup -> deltaX sgn {
239             {  1 [ left-action send-action$ ] }
240             { -1 [ right-action send-action$ ] }
241             {  0
242                 [
243                     dup -> deltaY sgn {
244                         {  1 [ up-action send-action$ ] }
245                         { -1 [ down-action send-action$ ] }
246                         {  0 [ 2drop ] }
247                     } case
248                 ]
249             }
250         } case
251     ]
252
253     METHOD: char acceptsFirstResponder [ 1 ]
254
255     ! Services
256     METHOD: id validRequestorForSendType: id sendType returnType: id returnType
257     [
258         ! We return either self or nil
259         self window [
260             world-focus sendType returnType
261             valid-service? [ self ] [ f ] if
262         ] [ f ] if*
263     ]
264
265     METHOD: char writeSelectionToPasteboard: id pboard types: id types
266     [
267         NSStringPboardType types CF>string-array member? [
268             self window [
269                 world-focus gadget-selection
270                 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
271             ] [ 0 ] if*
272         ] [ 0 ] if
273     ]
274
275     METHOD: char readSelectionFromPasteboard: id pboard
276     [
277         self window :> window
278         window [
279             pboard pasteboard-string
280             [ window user-input 1 ] [ 0 ] if*
281         ] [ 0 ] if
282     ]
283
284     ! Text input
285     METHOD: void insertText: id text
286     [
287         self window :> window
288         window [
289             text CF>string window user-input
290         ] when
291     ]
292
293     METHOD: char hasMarkedText [ 0 ]
294
295     METHOD: NSRange markedRange [ 0 0 <NSRange> ]
296
297     METHOD: NSRange selectedRange [ 0 0 <NSRange> ]
298
299     METHOD: void setMarkedText: id text selectedRange: NSRange range [ ]
300
301     METHOD: void unmarkText [ ]
302
303     METHOD: id validAttributesForMarkedText [ NSArray -> array ]
304
305     METHOD: id attributedSubstringFromRange: NSRange range [ f ]
306
307     METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ]
308
309     METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ]
310
311     METHOD: NSInteger conversationIdentifier [ self alien-address ]
312
313     ! Initialization
314     METHOD: void updateFactorGadgetSize: id notification
315     [
316         self window :> window
317         window [
318             self view-dim window dim<< yield
319         ] when
320     ]
321
322     METHOD: void doCommandBySelector: SEL selector [ ]
323
324     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
325     [
326         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
327         dup dup add-resize-observer
328     ]
329
330     METHOD: char isOpaque [ 0 ]
331
332     METHOD: void dealloc
333     [
334         self remove-observer
335         self SUPER-> dealloc
336     ]
337 ]
338
339 : sync-refresh-to-screen ( GLView -- )
340     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
341     CGLSetParameter drop ;
342
343 : <FactorView> ( dim pixel-format -- view )
344     [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
345
346 : save-position ( world window -- )
347     -> frame CGRect-top-left 2array >>window-loc drop ;
348
349 CLASS: FactorWindowDelegate < NSObject
350 [
351     METHOD: void windowDidMove: id notification
352     [
353         notification -> object -> contentView window
354         [ notification -> object save-position ] when*
355     ]
356
357     METHOD: void windowDidBecomeKey: id notification
358     [
359         notification -> object -> contentView window
360         [ focus-world ] when*
361     ]
362
363     METHOD: void windowDidResignKey: id notification
364     [
365         forget-rollover
366         notification -> object -> contentView :> view
367         view window :> window
368         window [
369             view -> isInFullScreenMode 0 =
370             [ window unfocus-world ] when
371         ] when
372     ]
373
374     METHOD: char windowShouldClose: id notification [ 1 ]
375
376     METHOD: void windowWillClose: id notification
377     [
378         notification -> object -> contentView
379         [ window ungraft ] [ unregister-window ] bi
380     ]
381 ]
382
383 : install-window-delegate ( window -- )
384     FactorWindowDelegate install-delegate ;