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 ( view -- dim )
20 swap NSRect-h >fixnum 2array ;
22 : mouse-location ( view event -- loc )
24 -> locationInWindow f -> convertPoint:fromView:
25 dup NSPoint-x swap NSPoint-y
26 r> -> frame NSRect-h swap - 2array ;
28 : send-mouse-moved ( view event -- )
29 over >r mouse-location r> window move-hand fire-motion ;
31 : button ( event -- n )
32 #! Cocoa -> Factor UI button mapping
33 -> buttonNumber H{ { 0 1 } { 2 2 } { 1 3 } } hash ;
70 : key-code ( event -- string )
71 dup -> keyCode key-codes hash
72 [ ] [ -> charactersIgnoringModifiers CF>string ] ?if ;
74 : event-modifiers ( event -- modifiers )
75 -> modifierFlags modifiers modifier ;
77 : key-event>gesture ( event -- modifiers keycode )
78 dup event-modifiers swap key-code ;
80 : send-key-event ( view event quot -- ? )
81 >r key-event>gesture r> call swap window-focus
84 : send-user-input ( view event -- )
85 -> characters CF>string swap window-focus user-input ;
87 : send-key-down-event ( view event -- )
88 2dup [ <key-down> ] send-key-event
89 [ send-user-input ] [ 2drop ] if ;
91 : send-key-up-event ( view event -- )
92 [ <key-up> ] send-key-event drop ;
94 : mouse-event>gesture ( event -- modifiers button )
95 dup event-modifiers swap button ;
97 : send-button-down$ ( view event -- )
98 [ mouse-event>gesture <button-down> ] 2keep
99 mouse-location rot window send-button-down ;
101 : send-button-up$ ( view event -- )
102 [ mouse-event>gesture <button-up> ] 2keep
103 mouse-location rot window send-button-up ;
105 : send-wheel$ ( view event -- )
107 dup -> deltaX sgn neg over -> deltaY sgn neg 2array -rot
109 r> window send-wheel ;
111 : send-action$ ( view event gesture -- junk )
112 >r drop window r> send-action f ;
114 : add-resize-observer ( observer object -- )
115 >r "updateFactorGadgetSize:"
116 "NSViewFrameDidChangeNotification" <NSString>
119 : string-or-nil? ( NSString -- ? )
120 [ CF>string NSStringPboardType = ] [ t ] if* ;
122 : valid-service? ( gadget send-type return-type -- ? )
123 over string-or-nil? over string-or-nil? and [
124 drop [ gadget-selection? ] [ drop t ] if
129 "NSOpenGLView" "FactorView" {
131 { "acceptsFirstMouse:" "bool" { "id" "SEL" "id" }
135 { "mouseEntered:" "void" { "id" "SEL" "id" }
136 [ [ nip send-mouse-moved ] ui-try ]
139 { "mouseExited:" "void" { "id" "SEL" "id" }
140 [ [ 3drop forget-rollover ] ui-try ]
143 { "mouseMoved:" "void" { "id" "SEL" "id" }
144 [ [ nip send-mouse-moved ] ui-try ]
147 { "mouseDragged:" "void" { "id" "SEL" "id" }
148 [ [ nip send-mouse-moved ] ui-try ]
151 { "rightMouseDragged:" "void" { "id" "SEL" "id" }
152 [ [ nip send-mouse-moved ] ui-try ]
155 { "otherMouseDragged:" "void" { "id" "SEL" "id" }
156 [ [ nip send-mouse-moved ] ui-try ]
159 { "mouseDown:" "void" { "id" "SEL" "id" }
160 [ [ nip send-button-down$ ] ui-try ]
163 { "mouseUp:" "void" { "id" "SEL" "id" }
164 [ [ nip send-button-up$ ] ui-try ]
167 { "rightMouseDown:" "void" { "id" "SEL" "id" }
168 [ [ nip send-button-down$ ] ui-try ]
171 { "rightMouseUp:" "void" { "id" "SEL" "id" }
172 [ [ nip send-button-up$ ] ui-try ]
175 { "otherMouseDown:" "void" { "id" "SEL" "id" }
176 [ [ nip send-button-down$ ] ui-try ]
179 { "otherMouseUp:" "void" { "id" "SEL" "id" }
180 [ [ nip send-button-up$ ] ui-try ]
183 { "scrollWheel:" "void" { "id" "SEL" "id" }
184 [ [ nip send-wheel$ ] ui-try ]
187 { "keyDown:" "void" { "id" "SEL" "id" }
188 [ [ nip send-key-down-event ] ui-try ]
191 { "keyUp:" "void" { "id" "SEL" "id" }
192 [ [ nip send-key-up-event ] ui-try ]
195 { "cut:" "id" { "id" "SEL" "id" }
196 [ [ nip T{ cut-action } send-action$ ] ui-try ]
199 { "copy:" "id" { "id" "SEL" "id" }
200 [ [ nip T{ copy-action } send-action$ ] ui-try ]
203 { "paste:" "id" { "id" "SEL" "id" }
204 [ [ nip T{ paste-action } send-action$ ] ui-try ]
207 { "delete:" "id" { "id" "SEL" "id" }
208 [ [ nip T{ delete-action } send-action$ ] ui-try ]
211 { "selectAll:" "id" { "id" "SEL" "id" }
212 [ [ nip T{ select-all-action } send-action$ ] ui-try ]
216 { "validRequestorForSendType:returnType:" "id" { "id" "SEL" "id" "id" }
218 ! We return either self or nil
219 >r >r over window-focus r> r>
220 valid-service? [ drop ] [ 2drop f ] if
224 { "writeSelectionToPasteboard:types:" "bool" { "id" "SEL" "id" "id" }
226 CF>string-array NSStringPboardType swap member? [
227 >r drop window-focus gadget-selection dup [
228 r> set-pasteboard-string t
238 { "readSelectionFromPasteboard:" "bool" { "id" "SEL" "id" }
240 pasteboard-string dup [
241 >r drop window-focus r> swap user-input t
248 { "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
251 2drop dup view-dim swap window set-gadget-dim
257 { "acceptsFirstResponder" "bool" { "id" "SEL" }
261 { "initWithFrame:pixelFormat:" "id" { "id" "SEL" "NSRect" "id" }
264 SUPER-> initWithFrame:pixelFormat:
265 dup dup add-resize-observer
269 { "dealloc" "void" { "id" "SEL" }
272 dup window close-world
273 dup unregister-window
280 : <FactorView> ( world -- view )
281 FactorView over rect-dim <GLView> [ register-window ] keep ;