1 ! Copyright (C) 2006 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
7 USING: arrays gadgets hashtables kernel math namespaces objc
10 : <GLView> ( class dim -- view )
11 >r -> alloc 0 0 r> first2 <NSRect>
12 NSOpenGLView -> defaultPixelFormat
13 -> initWithFrame:pixelFormat:
14 dup 1 -> setPostsBoundsChangedNotifications:
15 dup 1 -> setPostsFrameChangedNotifications: ;
17 : view-dim -> bounds dup NSRect-w swap NSRect-h 2array ;
19 : mouse-location ( view event -- loc )
21 -> locationInWindow f -> convertPoint:fromView:
22 dup NSPoint-x swap NSPoint-y
23 r> -> frame NSRect-h swap - 2array ;
25 : send-mouse-moved ( view event -- )
26 over >r mouse-location r> window move-hand fire-motion ;
28 : button ( event -- n )
29 #! Cocoa -> Factor UI button mapping
30 -> buttonNumber H{ { 0 1 } { 2 2 } { 1 3 } } hash ;
67 : key-code ( event -- string )
68 dup -> keyCode key-codes hash
69 [ ] [ -> charactersIgnoringModifiers CF>string ] ?if ;
71 : event-modifiers ( event -- modifiers )
72 -> modifierFlags modifiers modifier ;
74 : key-event>gesture ( event -- modifiers keycode )
75 dup event-modifiers swap key-code ;
77 : send-key-event ( view event quot -- ? )
78 >r key-event>gesture r> call swap window-focus
79 handle-gesture ; inline
81 : send-user-input ( view event -- )
82 -> characters CF>string swap window-focus user-input ;
84 : send-key-down-event ( view event -- )
85 2dup [ <key-down> ] send-key-event
86 [ send-user-input ] [ 2drop ] if ;
88 : send-key-up-event ( view event -- )
89 [ <key-up> ] send-key-event drop ;
91 : mouse-event>gesture ( event -- modifiers button )
92 dup event-modifiers swap button ;
94 : send-button-down$ ( view event -- )
95 [ mouse-event>gesture <button-down> ] 2keep
96 mouse-location rot window send-button-down ;
98 : send-button-up$ ( view event -- )
99 [ mouse-event>gesture <button-up> ] 2keep
100 mouse-location rot window send-button-up ;
102 : send-wheel$ ( view event -- )
104 dup -> deltaX sgn neg over -> deltaY sgn neg 2array -rot
106 r> window send-wheel ;
108 : send-action$ ( view event gesture -- junk )
109 >r drop window r> send-action f ;
111 : add-resize-observer ( observer object -- )
112 >r "updateFactorGadgetSize:"
113 "NSViewFrameDidChangeNotification" <NSString>
116 : string-or-nil? ( NSString -- ? )
117 [ CF>string NSStringPboardType = ] [ t ] if* ;
119 : valid-service? ( gadget send-type return-type -- ? )
120 over string-or-nil? over string-or-nil? and [
121 drop [ gadget-selection? ] [ drop t ] if
126 "NSOpenGLView" "FactorView" {
128 { "acceptsFirstMouse:" "bool" { "id" "SEL" "id" }
132 { "mouseEntered:" "void" { "id" "SEL" "id" }
133 [ [ nip send-mouse-moved ] ui-try ]
136 { "mouseExited:" "void" { "id" "SEL" "id" }
137 [ [ 3drop forget-rollover ] ui-try ]
140 { "mouseMoved:" "void" { "id" "SEL" "id" }
141 [ [ nip send-mouse-moved ] ui-try ]
144 { "mouseDragged:" "void" { "id" "SEL" "id" }
145 [ [ nip send-mouse-moved ] ui-try ]
148 { "rightMouseDragged:" "void" { "id" "SEL" "id" }
149 [ [ nip send-mouse-moved ] ui-try ]
152 { "otherMouseDragged:" "void" { "id" "SEL" "id" }
153 [ [ nip send-mouse-moved ] ui-try ]
156 { "mouseDown:" "void" { "id" "SEL" "id" }
157 [ [ nip send-button-down$ ] ui-try ]
160 { "mouseUp:" "void" { "id" "SEL" "id" }
161 [ [ nip send-button-up$ ] ui-try ]
164 { "rightMouseDown:" "void" { "id" "SEL" "id" }
165 [ [ nip send-button-down$ ] ui-try ]
168 { "rightMouseUp:" "void" { "id" "SEL" "id" }
169 [ [ nip send-button-up$ ] ui-try ]
172 { "otherMouseDown:" "void" { "id" "SEL" "id" }
173 [ [ nip send-button-down$ ] ui-try ]
176 { "otherMouseUp:" "void" { "id" "SEL" "id" }
177 [ [ nip send-button-up$ ] ui-try ]
180 { "scrollWheel:" "void" { "id" "SEL" "id" }
181 [ [ nip send-wheel$ ] ui-try ]
184 { "keyDown:" "void" { "id" "SEL" "id" }
185 [ [ nip send-key-down-event ] ui-try ]
188 { "keyUp:" "void" { "id" "SEL" "id" }
189 [ [ nip send-key-up-event ] ui-try ]
192 { "cut:" "id" { "id" "SEL" "id" }
193 [ [ nip T{ cut-action } send-action$ ] ui-try ]
196 { "copy:" "id" { "id" "SEL" "id" }
197 [ [ nip T{ copy-action } send-action$ ] ui-try ]
200 { "paste:" "id" { "id" "SEL" "id" }
201 [ [ nip T{ paste-action } send-action$ ] ui-try ]
204 { "delete:" "id" { "id" "SEL" "id" }
205 [ [ nip T{ delete-action } send-action$ ] ui-try ]
208 { "selectAll:" "id" { "id" "SEL" "id" }
209 [ [ nip T{ select-all-action } send-action$ ] ui-try ]
213 { "validRequestorForSendType:returnType:" "id" { "id" "SEL" "id" "id" }
215 ! We return either self or nil
216 >r >r over window-focus r> r>
217 valid-service? [ drop ] [ 2drop f ] if
221 { "writeSelectionToPasteboard:types:" "bool" { "id" "SEL" "id" "id" }
223 CF>string-array NSStringPboardType swap member? [
224 >r drop window-focus gadget-selection dup [
225 r> set-pasteboard-string t
235 { "readSelectionFromPasteboard:" "bool" { "id" "SEL" "id" }
237 pasteboard-string dup [
238 >r drop window-focus r> swap user-input t
245 { "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
248 2drop dup view-dim swap window set-gadget-dim
254 { "acceptsFirstResponder" "bool" { "id" "SEL" }
258 { "initWithFrame:pixelFormat:" "id" { "id" "SEL" "NSRect" "id" }
261 SUPER-> initWithFrame:pixelFormat:
262 dup dup add-resize-observer
266 { "dealloc" "void" { "id" "SEL" }
269 dup window close-world
270 dup unregister-window
275 } { } define-objc-class
277 : <FactorView> ( world -- view )
278 FactorView over rect-dim <GLView> [ register-window ] keep ;