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 ;
10 : send-mouse-moved ( view event -- )
11 [ mouse-location ] [ drop window ] 2bi move-hand fire-motion ;
13 : button ( event -- n )
14 #! Cocoa -> Factor UI button mapping
15 -> buttonNumber H{ { 0 1 } { 2 2 } { 1 3 } } at ;
52 : key-code ( event -- string ? )
53 dup -> keyCode key-codes at
54 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
56 : event-modifiers ( event -- modifiers )
57 -> modifierFlags modifiers modifier ;
59 : key-event>gesture ( event -- modifiers keycode action? )
60 dup event-modifiers swap key-code ;
62 : send-key-event ( view gesture -- )
63 swap window propagate-key-gesture ;
65 : interpret-key-event ( view event -- )
66 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
68 : send-key-down-event ( view event -- )
69 [ key-event>gesture <key-down> send-key-event ]
70 [ interpret-key-event ]
73 : send-key-up-event ( view event -- )
74 key-event>gesture <key-up> send-key-event ;
76 : mouse-event>gesture ( event -- modifiers button )
77 dup event-modifiers swap button ;
79 : send-button-down$ ( view event -- )
80 [ nip mouse-event>gesture <button-down> ]
83 2tri send-button-down ;
85 : send-button-up$ ( view event -- )
86 [ nip mouse-event>gesture <button-up> ]
91 : send-wheel$ ( view event -- )
92 [ nip [ -> deltaX ] [ -> deltaY ] bi [ sgn neg ] bi@ 2array ]
97 : send-action$ ( view event gesture -- junk )
98 [ drop window ] dip send-action f ;
100 : add-resize-observer ( observer object -- )
102 "updateFactorGadgetSize:"
103 "NSViewFrameDidChangeNotification" <NSString>
106 : string-or-nil? ( NSString -- ? )
107 [ CF>string NSStringPboardType = ] [ t ] if* ;
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 ;
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 ]
118 : rect>NSRect ( rect world -- NSRect )
119 [ [ rect-loc first2 ] [ dim>> second ] bi* swap - ]
120 [ drop rect-dim first2 ]
124 { +superclass+ "NSOpenGLView" }
125 { +name+ "FactorView" }
126 { +protocols+ { "NSTextInput" } }
130 { "drawRect:" "void" { "id" "SEL" "NSRect" }
131 [ 2drop window relayout-1 ]
135 { "acceptsFirstMouse:" "char" { "id" "SEL" "id" }
139 { "mouseEntered:" "void" { "id" "SEL" "id" }
140 [ nip send-mouse-moved ]
143 { "mouseExited:" "void" { "id" "SEL" "id" }
144 [ 3drop forget-rollover ]
147 { "mouseMoved:" "void" { "id" "SEL" "id" }
148 [ nip send-mouse-moved ]
151 { "mouseDragged:" "void" { "id" "SEL" "id" }
152 [ nip send-mouse-moved ]
155 { "rightMouseDragged:" "void" { "id" "SEL" "id" }
156 [ nip send-mouse-moved ]
159 { "otherMouseDragged:" "void" { "id" "SEL" "id" }
160 [ nip send-mouse-moved ]
163 { "mouseDown:" "void" { "id" "SEL" "id" }
164 [ nip send-button-down$ ]
167 { "mouseUp:" "void" { "id" "SEL" "id" }
168 [ nip send-button-up$ ]
171 { "rightMouseDown:" "void" { "id" "SEL" "id" }
172 [ nip send-button-down$ ]
175 { "rightMouseUp:" "void" { "id" "SEL" "id" }
176 [ nip send-button-up$ ]
179 { "otherMouseDown:" "void" { "id" "SEL" "id" }
180 [ nip send-button-down$ ]
183 { "otherMouseUp:" "void" { "id" "SEL" "id" }
184 [ nip send-button-up$ ]
187 { "scrollWheel:" "void" { "id" "SEL" "id" }
191 { "keyDown:" "void" { "id" "SEL" "id" }
192 [ nip send-key-down-event ]
195 { "keyUp:" "void" { "id" "SEL" "id" }
196 [ nip send-key-up-event ]
199 { "cut:" "id" { "id" "SEL" "id" }
200 [ nip T{ cut-action } send-action$ ]
203 { "copy:" "id" { "id" "SEL" "id" }
204 [ nip T{ copy-action } send-action$ ]
207 { "paste:" "id" { "id" "SEL" "id" }
208 [ nip T{ paste-action } send-action$ ]
211 { "delete:" "id" { "id" "SEL" "id" }
212 [ nip T{ delete-action } send-action$ ]
215 { "selectAll:" "id" { "id" "SEL" "id" }
216 [ nip T{ select-all-action } send-action$ ]
219 ! Multi-touch gestures: this is undocumented.
220 ! http://cocoadex.com/2008/02/nsevent-modifications-swipe-ro.html
221 { "magnifyWithEvent:" "void" { "id" "SEL" "id" }
225 { 1 [ T{ zoom-in-action } send-action$ ] }
226 { -1 [ T{ zoom-out-action } send-action$ ] }
232 { "swipeWithEvent:" "void" { "id" "SEL" "id" }
236 { 1 [ T{ left-action } send-action$ ] }
237 { -1 [ T{ right-action } send-action$ ] }
241 { 1 [ T{ up-action } send-action$ ] }
242 { -1 [ T{ down-action } send-action$ ] }
251 ! "rotateWithEvent:" "void" { "id" "SEL" "id" }}
253 { "acceptsFirstResponder" "char" { "id" "SEL" }
258 { "validRequestorForSendType:returnType:" "id" { "id" "SEL" "id" "id" }
260 ! We return either self or nil
261 [ over window-focus ] 2dip
262 valid-service? [ drop ] [ 2drop f ] if
266 { "writeSelectionToPasteboard:types:" "char" { "id" "SEL" "id" "id" }
268 CF>string-array NSStringPboardType swap member? [
269 [ drop window-focus gadget-selection ] dip over
270 [ set-pasteboard-string 1 ] [ 2drop 0 ] if
275 { "readSelectionFromPasteboard:" "char" { "id" "SEL" "id" }
277 pasteboard-string dup [
278 [ drop window ] dip swap user-input 1
284 { "insertText:" "void" { "id" "SEL" "id" }
285 [ nip CF>string swap window user-input ]
288 { "hasMarkedText" "char" { "id" "SEL" }
292 { "markedRange" "NSRange" { "id" "SEL" }
293 [ 2drop 0 0 <NSRange> ]
296 { "selectedRange" "NSRange" { "id" "SEL" }
297 [ 2drop 0 0 <NSRange> ]
300 { "setMarkedText:selectedRange:" "void" { "id" "SEL" "id" "NSRange" }
304 { "unmarkText" "void" { "id" "SEL" }
308 { "validAttributesForMarkedText" "id" { "id" "SEL" }
309 [ 2drop NSArray -> array ]
312 { "attributedSubstringFromRange:" "id" { "id" "SEL" "NSRange" }
316 { "characterIndexForPoint:" "NSUInteger" { "id" "SEL" "NSPoint" }
320 { "firstRectForCharacterRange:" "NSRect" { "id" "SEL" "NSRange" }
321 [ 3drop 0 0 0 0 <NSRect> ]
324 { "conversationIdentifier" "NSInteger" { "id" "SEL" }
325 [ drop alien-address ]
329 { "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
330 [ 2drop dup view-dim swap window (>>dim) yield ]
333 { "doCommandBySelector:" "void" { "id" "SEL" "SEL" }
337 { "initWithFrame:pixelFormat:" "id" { "id" "SEL" "NSRect" "id" }
340 SUPER-> initWithFrame:pixelFormat:
341 dup dup add-resize-observer
345 { "dealloc" "void" { "id" "SEL" }
348 [ unregister-window ]
355 : sync-refresh-to-screen ( GLView -- )
356 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 <int>
357 CGLSetParameter drop ;
359 : <FactorView> ( world -- view )
360 FactorView over rect-dim <GLView>
361 [ sync-refresh-to-screen ] keep
362 [ register-window ] keep ;
365 { +superclass+ "NSObject" }
366 { +name+ "FactorWindowDelegate" }
369 { "windowDidMove:" "void" { "id" "SEL" "id" }
372 dup window-content-rect NSRect-x-y 2array
373 swap -> contentView window (>>window-loc)
377 { "windowDidBecomeKey:" "void" { "id" "SEL" "id" }
379 2nip -> object -> contentView window focus-world
383 { "windowDidResignKey:" "void" { "id" "SEL" "id" }
386 2nip -> object -> contentView window unfocus-world
390 { "windowShouldClose:" "char" { "id" "SEL" "id" }
396 { "windowWillClose:" "void" { "id" "SEL" "id" }
398 2nip -> object -> contentView window ungraft
402 : install-window-delegate ( window -- )
403 FactorWindowDelegate install-delegate ;