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