1 ! Copyright (C) 2006, 2007 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 ;
9 : send-mouse-moved ( view event -- )
10 over >r mouse-location r> window move-hand fire-motion ;
12 : button ( event -- n )
13 #! Cocoa -> Factor UI button mapping
14 -> buttonNumber H{ { 0 1 } { 2 2 } { 1 3 } } at ;
51 : key-code ( event -- string ? )
52 dup -> keyCode key-codes at
53 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
55 : event-modifiers ( event -- modifiers )
56 -> modifierFlags modifiers modifier ;
58 : key-event>gesture ( event -- modifiers keycode action? )
59 dup event-modifiers swap key-code ;
61 : send-key-event ( view event quot -- ? )
62 >r key-event>gesture r> call swap window-focus
65 : send-user-input ( view string -- )
66 CF>string swap window-focus user-input ;
68 : interpret-key-event ( view event -- )
69 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
71 : send-key-down-event ( view event -- )
72 2dup [ <key-down> ] send-key-event
73 [ interpret-key-event ] [ 2drop ] if ;
75 : send-key-up-event ( view event -- )
76 [ <key-up> ] send-key-event drop ;
78 : mouse-event>gesture ( event -- modifiers button )
79 dup event-modifiers swap button ;
81 : send-button-down$ ( view event -- )
82 [ mouse-event>gesture <button-down> ] 2keep
83 mouse-location rot window send-button-down ;
85 : send-button-up$ ( view event -- )
86 [ mouse-event>gesture <button-up> ] 2keep
87 mouse-location rot window send-button-up ;
89 : send-wheel$ ( view event -- )
91 dup -> deltaX sgn neg over -> deltaY sgn neg 2array -rot
93 r> window send-wheel ;
95 : send-action$ ( view event gesture -- junk )
96 >r drop window r> send-action f ;
98 : add-resize-observer ( observer object -- )
99 >r "updateFactorGadgetSize:"
100 "NSViewFrameDidChangeNotification" <NSString>
103 : string-or-nil? ( NSString -- ? )
104 [ CF>string NSStringPboardType = ] [ t ] if* ;
106 : valid-service? ( gadget send-type return-type -- ? )
107 over string-or-nil? over string-or-nil? and [
108 drop [ gadget-selection? ] [ drop t ] if
113 : NSRect>rect ( NSRect world -- rect )
114 >r dup NSRect-x over NSRect-y r>
115 rect-dim second swap - 2array
116 over NSRect-w rot NSRect-h 2array
119 : rect>NSRect ( rect world -- NSRect )
120 over rect-loc first2 rot rect-dim second swap -
121 rot rect-dim first2 <NSRect> ;
124 { +superclass+ "NSOpenGLView" }
125 { +name+ "FactorView" }
126 { +protocols+ { "NSTextInput" } }
129 { "acceptsFirstMouse:" "bool" { "id" "SEL" "id" }
133 { "mouseEntered:" "void" { "id" "SEL" "id" }
134 [ [ nip send-mouse-moved ] ui-try ]
137 { "mouseExited:" "void" { "id" "SEL" "id" }
138 [ [ 3drop forget-rollover ] ui-try ]
141 { "mouseMoved:" "void" { "id" "SEL" "id" }
142 [ [ nip send-mouse-moved ] ui-try ]
145 { "mouseDragged:" "void" { "id" "SEL" "id" }
146 [ [ nip send-mouse-moved ] ui-try ]
149 { "rightMouseDragged:" "void" { "id" "SEL" "id" }
150 [ [ nip send-mouse-moved ] ui-try ]
153 { "otherMouseDragged:" "void" { "id" "SEL" "id" }
154 [ [ nip send-mouse-moved ] ui-try ]
157 { "mouseDown:" "void" { "id" "SEL" "id" }
158 [ [ nip send-button-down$ ] ui-try ]
161 { "mouseUp:" "void" { "id" "SEL" "id" }
162 [ [ nip send-button-up$ ] ui-try ]
165 { "rightMouseDown:" "void" { "id" "SEL" "id" }
166 [ [ nip send-button-down$ ] ui-try ]
169 { "rightMouseUp:" "void" { "id" "SEL" "id" }
170 [ [ nip send-button-up$ ] ui-try ]
173 { "otherMouseDown:" "void" { "id" "SEL" "id" }
174 [ [ nip send-button-down$ ] ui-try ]
177 { "otherMouseUp:" "void" { "id" "SEL" "id" }
178 [ [ nip send-button-up$ ] ui-try ]
181 { "scrollWheel:" "void" { "id" "SEL" "id" }
182 [ [ nip send-wheel$ ] ui-try ]
185 { "keyDown:" "void" { "id" "SEL" "id" }
186 [ [ nip send-key-down-event ] ui-try ]
189 { "keyUp:" "void" { "id" "SEL" "id" }
190 [ [ nip send-key-up-event ] ui-try ]
193 { "cut:" "id" { "id" "SEL" "id" }
194 [ [ nip T{ cut-action } send-action$ ] ui-try ]
197 { "copy:" "id" { "id" "SEL" "id" }
198 [ [ nip T{ copy-action } send-action$ ] ui-try ]
201 { "paste:" "id" { "id" "SEL" "id" }
202 [ [ nip T{ paste-action } send-action$ ] ui-try ]
205 { "delete:" "id" { "id" "SEL" "id" }
206 [ [ nip T{ delete-action } send-action$ ] ui-try ]
209 { "selectAll:" "id" { "id" "SEL" "id" }
210 [ [ nip T{ select-all-action } send-action$ ] ui-try ]
213 { "acceptsFirstResponder" "bool" { "id" "SEL" }
218 { "validRequestorForSendType:returnType:" "id" { "id" "SEL" "id" "id" }
220 ! We return either self or nil
221 >r >r over window-focus r> r>
222 valid-service? [ drop ] [ 2drop f ] if
226 { "writeSelectionToPasteboard:types:" "bool" { "id" "SEL" "id" "id" }
228 CF>string-array NSStringPboardType swap member? [
229 >r drop window-focus gadget-selection dup [
230 r> set-pasteboard-string t
240 { "readSelectionFromPasteboard:" "bool" { "id" "SEL" "id" }
242 pasteboard-string dup [
243 >r drop window-focus r> swap user-input t
251 { "insertText:" "void" { "id" "SEL" "id" }
252 [ [ nip send-user-input ] ui-try ]
255 { "hasMarkedText" "bool" { "id" "SEL" }
259 { "markedRange" "NSRange" { "id" "SEL" }
260 [ 2drop 0 0 <NSRange> ]
263 { "selectedRange" "NSRange" { "id" "SEL" }
264 [ 2drop 0 0 <NSRange> ]
267 { "setMarkedText:selectedRange:" "void" { "id" "SEL" "id" "NSRange" }
271 { "unmarkText" "void" { "id" "SEL" }
275 { "validAttributesForMarkedText" "id" { "id" "SEL" }
276 [ 2drop NSArray -> array ]
279 { "attributedSubstringFromRange:" "id" { "id" "SEL" "NSRange" }
283 { "characterIndexForPoint:" "uint" { "id" "SEL" "NSPoint" }
287 { "firstRectForCharacterRange:" "NSRect" { "id" "SEL" "NSRange" }
288 [ 3drop 0 0 0 0 <NSRect> ]
291 { "conversationIdentifier" "long" { "id" "SEL" }
292 [ drop alien-address ]
296 { "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
299 2drop dup view-dim swap window set-gadget-dim
305 { "initWithFrame:pixelFormat:" "id" { "id" "SEL" "NSRect" "id" }
308 SUPER-> initWithFrame:pixelFormat:
309 dup dup add-resize-observer
313 { "dealloc" "void" { "id" "SEL" }
316 dup window stop-world
317 dup unregister-window
323 : <FactorView> ( world -- view )
324 FactorView over rect-dim <GLView> [ register-window ] keep ;
327 { +superclass+ "NSObject" }
328 { +name+ "FactorWindowDelegate" }
331 { "windowDidMove:" "void" { "id" "SEL" "id" }
334 dup window-content-rect NSRect-x-y 2array
335 swap -> contentView window set-world-loc
339 { "windowDidBecomeKey:" "void" { "id" "SEL" "id" }
341 2nip -> object -> contentView window focus-world
345 { "windowDidResignKey:" "void" { "id" "SEL" "id" }
348 2nip -> object -> contentView window unfocus-world
352 : install-window-delegate ( window -- )
353 FactorWindowDelegate install-delegate ;