1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien arrays assocs cocoa kernel math cocoa.messages
4 cocoa.subclassing cocoa.classes cocoa.views cocoa.application
5 cocoa.pasteboard cocoa.types cocoa.windows sequences ui
6 ui.gadgets ui.gadgets.worlds ui.gestures core-foundation
10 : send-mouse-moved ( view event -- )
11 over >r mouse-location r> window 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 event quot -- ? )
63 >r key-event>gesture r> call swap window-focus
66 : send-user-input ( view string -- )
67 CF>string swap window-focus user-input ;
69 : interpret-key-event ( view event -- )
70 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
72 : send-key-down-event ( view event -- )
73 2dup [ <key-down> ] send-key-event
74 [ interpret-key-event ] [ 2drop ] if ;
76 : send-key-up-event ( view event -- )
77 [ <key-up> ] send-key-event drop ;
79 : mouse-event>gesture ( event -- modifiers button )
80 dup event-modifiers swap button ;
82 : send-button-down$ ( view event -- )
83 [ mouse-event>gesture <button-down> ] 2keep
84 mouse-location rot window send-button-down ;
86 : send-button-up$ ( view event -- )
87 [ mouse-event>gesture <button-up> ] 2keep
88 mouse-location rot window send-button-up ;
90 : send-wheel$ ( view event -- )
92 dup -> deltaX sgn neg over -> deltaY sgn neg 2array -rot
94 r> window send-wheel ;
96 : send-action$ ( view event gesture -- junk )
97 >r drop window r> send-action f ;
99 : add-resize-observer ( observer object -- )
100 >r "updateFactorGadgetSize:"
101 "NSViewFrameDidChangeNotification" <NSString>
104 : string-or-nil? ( NSString -- ? )
105 [ CF>string NSStringPboardType = ] [ t ] if* ;
107 : valid-service? ( gadget send-type return-type -- ? )
108 over string-or-nil? over string-or-nil? and [
109 drop [ gadget-selection? ] [ drop t ] if
114 : NSRect>rect ( NSRect world -- rect )
115 >r dup NSRect-x over NSRect-y r>
116 rect-dim second swap - 2array
117 over NSRect-w rot NSRect-h 2array
120 : rect>NSRect ( rect world -- NSRect )
121 over rect-loc first2 rot rect-dim second swap -
122 rot rect-dim first2 <NSRect> ;
125 { +superclass+ "NSOpenGLView" }
126 { +name+ "FactorView" }
127 { +protocols+ { "NSTextInput" } }
132 { "drawRect:" "void" { "id" "SEL" "id" "NSRect" }
133 [ 3drop window relayout-1 ]
137 { "acceptsFirstMouse:" "bool" { "id" "SEL" "id" }
141 { "mouseEntered:" "void" { "id" "SEL" "id" }
142 [ [ nip send-mouse-moved ] ui-try ]
145 { "mouseExited:" "void" { "id" "SEL" "id" }
146 [ [ 3drop forget-rollover ] ui-try ]
149 { "mouseMoved:" "void" { "id" "SEL" "id" }
150 [ [ nip send-mouse-moved ] ui-try ]
153 { "mouseDragged:" "void" { "id" "SEL" "id" }
154 [ [ nip send-mouse-moved ] ui-try ]
157 { "rightMouseDragged:" "void" { "id" "SEL" "id" }
158 [ [ nip send-mouse-moved ] ui-try ]
161 { "otherMouseDragged:" "void" { "id" "SEL" "id" }
162 [ [ nip send-mouse-moved ] ui-try ]
165 { "mouseDown:" "void" { "id" "SEL" "id" }
166 [ [ nip send-button-down$ ] ui-try ]
169 { "mouseUp:" "void" { "id" "SEL" "id" }
170 [ [ nip send-button-up$ ] ui-try ]
173 { "rightMouseDown:" "void" { "id" "SEL" "id" }
174 [ [ nip send-button-down$ ] ui-try ]
177 { "rightMouseUp:" "void" { "id" "SEL" "id" }
178 [ [ nip send-button-up$ ] ui-try ]
181 { "otherMouseDown:" "void" { "id" "SEL" "id" }
182 [ [ nip send-button-down$ ] ui-try ]
185 { "otherMouseUp:" "void" { "id" "SEL" "id" }
186 [ [ nip send-button-up$ ] ui-try ]
189 { "scrollWheel:" "void" { "id" "SEL" "id" }
190 [ [ nip send-wheel$ ] ui-try ]
193 { "keyDown:" "void" { "id" "SEL" "id" }
194 [ [ nip send-key-down-event ] ui-try ]
197 { "keyUp:" "void" { "id" "SEL" "id" }
198 [ [ nip send-key-up-event ] ui-try ]
201 { "cut:" "id" { "id" "SEL" "id" }
202 [ [ nip T{ cut-action } send-action$ ] ui-try ]
205 { "copy:" "id" { "id" "SEL" "id" }
206 [ [ nip T{ copy-action } send-action$ ] ui-try ]
209 { "paste:" "id" { "id" "SEL" "id" }
210 [ [ nip T{ paste-action } send-action$ ] ui-try ]
213 { "delete:" "id" { "id" "SEL" "id" }
214 [ [ nip T{ delete-action } send-action$ ] ui-try ]
217 { "selectAll:" "id" { "id" "SEL" "id" }
218 [ [ nip T{ select-all-action } send-action$ ] ui-try ]
221 ! Multi-touch gestures: this is undocumented.
222 ! http://cocoadex.com/2008/02/nsevent-modifications-swipe-ro.html
223 { "magnifyWithEvent:" "void" { "id" "SEL" "id" }
227 { 1 [ T{ zoom-in-action } send-action$ ] }
228 { -1 [ T{ zoom-out-action } send-action$ ] }
234 { "swipeWithEvent:" "void" { "id" "SEL" "id" }
238 { 1 [ T{ left-action } send-action$ ] }
239 { -1 [ T{ right-action } send-action$ ] }
243 { 1 [ T{ up-action } send-action$ ] }
244 { -1 [ T{ down-action } send-action$ ] }
253 ! "rotateWithEvent:" "void" { "id" "SEL" "id" }}
255 { "acceptsFirstResponder" "bool" { "id" "SEL" }
260 { "validRequestorForSendType:returnType:" "id" { "id" "SEL" "id" "id" }
262 ! We return either self or nil
263 >r >r over window-focus r> r>
264 valid-service? [ drop ] [ 2drop f ] if
268 { "writeSelectionToPasteboard:types:" "bool" { "id" "SEL" "id" "id" }
270 CF>string-array NSStringPboardType swap member? [
271 >r drop window-focus gadget-selection dup [
272 r> set-pasteboard-string t
282 { "readSelectionFromPasteboard:" "bool" { "id" "SEL" "id" }
284 pasteboard-string dup [
285 >r drop window-focus r> swap user-input t
293 { "insertText:" "void" { "id" "SEL" "id" }
294 [ [ nip send-user-input ] ui-try ]
297 { "hasMarkedText" "bool" { "id" "SEL" }
301 { "markedRange" "NSRange" { "id" "SEL" }
302 [ 2drop 0 0 <NSRange> ]
305 { "selectedRange" "NSRange" { "id" "SEL" }
306 [ 2drop 0 0 <NSRange> ]
309 { "setMarkedText:selectedRange:" "void" { "id" "SEL" "id" "NSRange" }
313 { "unmarkText" "void" { "id" "SEL" }
317 { "validAttributesForMarkedText" "id" { "id" "SEL" }
318 [ 2drop NSArray -> array ]
321 { "attributedSubstringFromRange:" "id" { "id" "SEL" "NSRange" }
325 { "characterIndexForPoint:" "uint" { "id" "SEL" "NSPoint" }
329 { "firstRectForCharacterRange:" "NSRect" { "id" "SEL" "NSRange" }
330 [ 3drop 0 0 0 0 <NSRect> ]
333 { "conversationIdentifier" "long" { "id" "SEL" }
334 [ drop alien-address ]
338 { "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
341 2drop dup view-dim swap window set-gadget-dim yield
346 { "initWithFrame:pixelFormat:" "id" { "id" "SEL" "NSRect" "id" }
349 SUPER-> initWithFrame:pixelFormat:
350 dup dup add-resize-observer
354 { "dealloc" "void" { "id" "SEL" }
357 dup unregister-window
363 : <FactorView> ( world -- view )
364 FactorView over rect-dim <GLView> [ register-window ] keep ;
367 { +superclass+ "NSObject" }
368 { +name+ "FactorWindowDelegate" }
371 { "windowDidMove:" "void" { "id" "SEL" "id" }
374 dup window-content-rect NSRect-x-y 2array
375 swap -> contentView window set-world-loc
379 { "windowDidBecomeKey:" "void" { "id" "SEL" "id" }
381 2nip -> object -> contentView window focus-world
385 { "windowDidResignKey:" "void" { "id" "SEL" "id" }
388 2nip -> object -> contentView window unfocus-world
392 { "windowShouldClose:" "bool" { "id" "SEL" "id" }
398 { "windowWillClose:" "void" { "id" "SEL" "id" }
400 2nip -> object -> contentView window ungraft
404 : install-window-delegate ( window -- )
405 FactorWindowDelegate install-delegate ;