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