1 ! Copyright (C) 2006, 2010 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.strings
4 arrays assocs cocoa kernel math cocoa.messages cocoa.subclassing
5 cocoa.classes cocoa.views cocoa.application cocoa.pasteboard
6 cocoa.runtime cocoa.types cocoa.windows sequences
7 io.encodings.utf8 locals ui ui.private ui.gadgets
8 ui.gadgets.private ui.gadgets.worlds ui.gestures
9 core-foundation.strings core-graphics core-graphics.types
10 threads combinators math.rectangles ;
11 IN: ui.backend.cocoa.views
13 : send-mouse-moved ( view event -- )
14 [ mouse-location ] [ drop window ] 2bi
15 dup [ move-hand fire-motion yield ] [ 2drop ] if ;
17 : button ( event -- n )
18 #! Cocoa -> Factor UI button mapping
19 -> buttonNumber H{ { 0 1 } { 2 2 } { 1 3 } } at ;
56 : key-code ( event -- string ? )
57 dup -> keyCode key-codes at
58 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
60 : event-modifiers ( event -- modifiers )
61 -> modifierFlags modifiers modifier ;
63 : key-event>gesture ( event -- modifiers keycode action? )
64 [ event-modifiers ] [ key-code ] bi ;
66 : send-key-event ( view gesture -- )
67 swap window dup [ propagate-key-gesture ] [ 2drop ] if ;
69 : interpret-key-event ( view event -- )
70 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
72 : send-key-down-event ( view event -- )
73 [ key-event>gesture <key-down> send-key-event ]
74 [ interpret-key-event ]
77 : send-key-up-event ( view event -- )
78 key-event>gesture <key-up> send-key-event ;
80 : mouse-event>gesture ( event -- modifiers button )
81 [ event-modifiers ] [ button ] bi ;
83 : send-button-down$ ( view event -- )
84 [ nip mouse-event>gesture <button-down> ]
88 dup [ send-button-down ] [ 3drop ] if ;
90 : send-button-up$ ( view event -- )
91 [ nip mouse-event>gesture <button-up> ]
95 dup [ send-button-up ] [ 3drop ] if ;
97 : send-scroll$ ( view event -- )
98 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
102 dup [ send-scroll ] [ 3drop ] if ;
104 : send-action$ ( view event gesture -- )
105 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
107 : add-resize-observer ( observer object -- )
109 "updateFactorGadgetSize:"
110 "NSViewFrameDidChangeNotification" <NSString>
113 : string-or-nil? ( NSString -- ? )
114 [ CF>string NSStringPboardType = ] [ t ] if* ;
116 : valid-service? ( gadget send-type return-type -- ? )
117 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
118 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
120 : NSRect>rect ( NSRect world -- rect )
121 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
122 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
125 : rect>NSRect ( rect world -- NSRect )
126 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
127 [ drop dim>> first2 ]
130 CONSTANT: selector>action H{
131 { "undo:" undo-action }
132 { "redo:" redo-action }
133 { "cut:" cut-action }
134 { "copy:" copy-action }
135 { "paste:" paste-action }
136 { "delete:" delete-action }
137 { "selectAll:" select-all-action }
138 { "newDocument:" new-action }
139 { "openDocument:" open-action }
140 { "saveDocument:" save-action }
141 { "saveDocumentAs:" save-as-action }
142 { "revertDocumentToSaved:" revert-action }
145 : validate-action ( world selector -- ? validated? )
147 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
149 CLASS: FactorView < NSOpenGLView NSTextInput
152 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ]
155 METHOD: char acceptsFirstMouse: id event [ 0 ]
157 METHOD: void mouseEntered: id event [ self event send-mouse-moved ]
159 METHOD: void mouseExited: id event [ forget-rollover ]
161 METHOD: void mouseMoved: id event [ self event send-mouse-moved ]
163 METHOD: void mouseDragged: id event [ self event send-mouse-moved ]
165 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ]
167 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ]
169 METHOD: void mouseDown: id event [ self event send-button-down$ ]
171 METHOD: void mouseUp: id event [ self event send-button-up$ ]
173 METHOD: void rightMouseDown: id event [ self event send-button-down$ ]
175 METHOD: void rightMouseUp: id event [ self event send-button-up$ ]
177 METHOD: void otherMouseDown: id event [ self event send-button-down$ ]
179 METHOD: void otherMouseUp: id event [ self event send-button-up$ ]
181 METHOD: void scrollWheel: id event [ self event send-scroll$ ]
183 METHOD: void keyDown: id event [ self event send-key-down-event ]
185 METHOD: void keyUp: id event [ self event send-key-up-event ]
187 METHOD: char validateUserInterfaceItem: id event
190 event -> action utf8 alien>string validate-action
191 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
195 METHOD: id undo: id event [ self event undo-action send-action$ f ]
197 METHOD: id redo: id event [ self event redo-action send-action$ f ]
199 METHOD: id cut: id event [ self event cut-action send-action$ f ]
201 METHOD: id copy: id event [ self event copy-action send-action$ f ]
203 METHOD: id paste: id event [ self event paste-action send-action$ f ]
205 METHOD: id delete: id event [ self event delete-action send-action$ f ]
207 METHOD: id selectAll: id event [ self event select-all-action send-action$ f ]
209 METHOD: id newDocument: id event [ self event new-action send-action$ f ]
211 METHOD: id openDocument: id event [ self event open-action send-action$ f ]
213 METHOD: id saveDocument: id event [ self event save-action send-action$ f ]
215 METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ]
217 METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ]
219 ! Multi-touch gestures
220 METHOD: void magnifyWithEvent: id event
224 { 1 [ zoom-in-action send-action$ ] }
225 { -1 [ zoom-out-action send-action$ ] }
230 METHOD: void swipeWithEvent: id event
234 { 1 [ left-action send-action$ ] }
235 { -1 [ right-action send-action$ ] }
239 { 1 [ up-action send-action$ ] }
240 { -1 [ down-action send-action$ ] }
248 METHOD: char acceptsFirstResponder [ 1 ]
251 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
253 ! We return either self or nil
255 world-focus sendType returnType
256 valid-service? [ self ] [ f ] if
260 METHOD: char writeSelectionToPasteboard: id pboard types: id types
262 NSStringPboardType types CF>string-array member? [
264 world-focus gadget-selection
265 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
270 METHOD: char readSelectionFromPasteboard: id pboard
272 self window :> window
274 pboard pasteboard-string
275 [ window user-input 1 ] [ 0 ] if*
280 METHOD: void insertText: id text
282 self window :> window
284 text CF>string window user-input
288 METHOD: char hasMarkedText [ 0 ]
290 METHOD: NSRange markedRange [ 0 0 <NSRange> ]
292 METHOD: NSRange selectedRange [ 0 0 <NSRange> ]
294 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ]
296 METHOD: void unmarkText [ ]
298 METHOD: id validAttributesForMarkedText [ NSArray -> array ]
300 METHOD: id attributedSubstringFromRange: NSRange range [ f ]
302 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ]
304 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ]
306 METHOD: NSInteger conversationIdentifier [ self alien-address ]
309 METHOD: void updateFactorGadgetSize: id notification
311 self window :> window
313 self view-dim window dim<< yield
317 METHOD: void doCommandBySelector: SEL selector [ ]
319 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
321 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
322 dup dup add-resize-observer
325 METHOD: char isOpaque [ 0 ]
334 : sync-refresh-to-screen ( GLView -- )
335 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
336 CGLSetParameter drop ;
338 : <FactorView> ( dim pixel-format -- view )
339 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
341 : save-position ( world window -- )
342 -> frame CGRect-top-left 2array >>window-loc drop ;
344 CLASS: FactorWindowDelegate < NSObject
346 METHOD: void windowDidMove: id notification
348 notification -> object -> contentView window
349 [ notification -> object save-position ] when*
352 METHOD: void windowDidBecomeKey: id notification
354 notification -> object -> contentView window
355 [ focus-world ] when*
358 METHOD: void windowDidResignKey: id notification
361 notification -> object -> contentView :> view
362 view window :> window
364 view -> isInFullScreenMode 0 =
365 [ window unfocus-world ] when
369 METHOD: char windowShouldClose: id notification [ 1 ]
371 METHOD: void windowWillClose: id notification
373 notification -> object -> contentView
374 [ window ungraft ] [ unregister-window ] bi
378 : install-window-delegate ( window -- )
379 FactorWindowDelegate install-delegate ;