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 cocoa.application cocoa.classes
5 cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.types
6 cocoa.views combinators core-foundation.strings core-graphics
7 core-graphics.types io.encodings.utf8 kernel locals math
8 math.rectangles namespaces opengl sequences threads ui.gadgets
9 ui.gadgets.private ui.gadgets.worlds ui.gestures ui.private ;
10 IN: ui.backend.cocoa.views
12 : send-mouse-moved ( view event -- )
13 [ mouse-location ] [ drop window ] 2bi
14 dup [ move-hand fire-motion yield ] [ 2drop ] if ;
16 : button ( event -- n )
17 #! Cocoa -> Factor UI button mapping
18 -> buttonNumber H{ { 0 1 } { 2 2 } { 1 3 } } at ;
55 : key-code ( event -- string ? )
56 dup -> keyCode key-codes at
57 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
59 : event-modifiers ( event -- modifiers )
60 -> modifierFlags modifiers modifier ;
62 : key-event>gesture ( event -- modifiers keycode action? )
63 [ event-modifiers ] [ key-code ] bi ;
65 : send-key-event ( view gesture -- )
66 swap window dup [ propagate-key-gesture ] [ 2drop ] if ;
68 : interpret-key-event ( view event -- )
69 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
71 : send-key-down-event ( view event -- )
72 [ key-event>gesture <key-down> send-key-event ]
73 [ interpret-key-event ]
76 : send-key-up-event ( view event -- )
77 key-event>gesture <key-up> send-key-event ;
79 : mouse-event>gesture ( event -- modifiers button )
80 [ event-modifiers ] [ button ] bi ;
82 : send-button-down$ ( view event -- )
83 [ nip mouse-event>gesture <button-down> ]
87 dup [ send-button-down ] [ 3drop ] if ;
89 : send-button-up$ ( view event -- )
90 [ nip mouse-event>gesture <button-up> ]
94 dup [ send-button-up ] [ 3drop ] if ;
96 : send-scroll$ ( view event -- )
97 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
101 dup [ send-scroll ] [ 3drop ] if ;
103 : send-action$ ( view event gesture -- )
104 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
106 : add-resize-observer ( observer object -- )
108 "updateFactorGadgetSize:"
109 "NSViewFrameDidChangeNotification" <NSString>
112 : string-or-nil? ( NSString -- ? )
113 [ CF>string NSStringPboardType = ] [ t ] if* ;
115 : valid-service? ( gadget send-type return-type -- ? )
116 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
117 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
119 : NSRect>rect ( NSRect world -- rect )
120 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
121 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
124 : rect>NSRect ( rect world -- NSRect )
125 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
126 [ drop dim>> first2 ]
129 CONSTANT: selector>action H{
130 { "undo:" undo-action }
131 { "redo:" redo-action }
132 { "cut:" cut-action }
133 { "copy:" copy-action }
134 { "paste:" paste-action }
135 { "delete:" delete-action }
136 { "selectAll:" select-all-action }
137 { "newDocument:" new-action }
138 { "openDocument:" open-action }
139 { "saveDocument:" save-action }
140 { "saveDocumentAs:" save-as-action }
141 { "revertDocumentToSaved:" revert-action }
144 : validate-action ( world selector -- ? validated? )
146 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
148 CLASS: FactorView < NSOpenGLView NSTextInput
151 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ]
154 METHOD: char acceptsFirstMouse: id event [ 0 ]
156 METHOD: void mouseEntered: id event [ self event send-mouse-moved ]
158 METHOD: void mouseExited: id event [ forget-rollover ]
160 METHOD: void mouseMoved: id event [ self event send-mouse-moved ]
162 METHOD: void mouseDragged: id event [ self event send-mouse-moved ]
164 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ]
166 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ]
168 METHOD: void mouseDown: id event [ self event send-button-down$ ]
170 METHOD: void mouseUp: id event [ self event send-button-up$ ]
172 METHOD: void rightMouseDown: id event [ self event send-button-down$ ]
174 METHOD: void rightMouseUp: id event [ self event send-button-up$ ]
176 METHOD: void otherMouseDown: id event [ self event send-button-down$ ]
178 METHOD: void otherMouseUp: id event [ self event send-button-up$ ]
180 METHOD: void scrollWheel: id event [ self event send-scroll$ ]
182 METHOD: void keyDown: id event [ self event send-key-down-event ]
184 METHOD: void keyUp: id event [ self event send-key-up-event ]
186 METHOD: char validateUserInterfaceItem: id event
189 event -> action utf8 alien>string validate-action
190 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
194 METHOD: id undo: id event [ self event undo-action send-action$ f ]
196 METHOD: id redo: id event [ self event redo-action send-action$ f ]
198 METHOD: id cut: id event [ self event cut-action send-action$ f ]
200 METHOD: id copy: id event [ self event copy-action send-action$ f ]
202 METHOD: id paste: id event [ self event paste-action send-action$ f ]
204 METHOD: id delete: id event [ self event delete-action send-action$ f ]
206 METHOD: id selectAll: id event [ self event select-all-action send-action$ f ]
208 METHOD: id newDocument: id event [ self event new-action send-action$ f ]
210 METHOD: id openDocument: id event [ self event open-action send-action$ f ]
212 METHOD: id saveDocument: id event [ self event save-action send-action$ f ]
214 METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ]
216 METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ]
218 ! Multi-touch gestures
219 METHOD: void magnifyWithEvent: id event
223 { 1 [ zoom-in-action send-action$ ] }
224 { -1 [ zoom-out-action send-action$ ] }
229 METHOD: void swipeWithEvent: id event
233 { 1 [ left-action send-action$ ] }
234 { -1 [ right-action send-action$ ] }
238 { 1 [ up-action send-action$ ] }
239 { -1 [ down-action send-action$ ] }
247 METHOD: char acceptsFirstResponder [ 1 ]
250 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
252 ! We return either self or nil
254 world-focus sendType returnType
255 valid-service? [ self ] [ f ] if
259 METHOD: char writeSelectionToPasteboard: id pboard types: id types
261 NSStringPboardType types CF>string-array member? [
263 world-focus gadget-selection
264 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
269 METHOD: char readSelectionFromPasteboard: id pboard
271 self window :> window
273 pboard pasteboard-string
274 [ window user-input 1 ] [ 0 ] if*
279 METHOD: void insertText: id text
281 self window :> window
283 text CF>string window user-input
287 METHOD: char hasMarkedText [ 0 ]
289 METHOD: NSRange markedRange [ 0 0 <NSRange> ]
291 METHOD: NSRange selectedRange [ 0 0 <NSRange> ]
293 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ]
295 METHOD: void unmarkText [ ]
297 METHOD: id validAttributesForMarkedText [ NSArray -> array ]
299 METHOD: id attributedSubstringFromRange: NSRange range [ f ]
301 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ]
303 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ]
305 METHOD: NSInteger conversationIdentifier [ self alien-address ]
307 METHOD: void prepareOpenGL [
308 self 1 -> setWantsBestResolutionOpenGLSurface:
309 self -> backingScaleFactor gl-scale-factor set-global
313 METHOD: void updateFactorGadgetSize: id notification
315 self window :> window
317 self view-dim window dim<< yield
321 METHOD: void doCommandBySelector: SEL selector [ ]
323 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
325 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
326 dup dup add-resize-observer
329 METHOD: char isOpaque [ 0 ]
338 : sync-refresh-to-screen ( GLView -- )
339 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
340 CGLSetParameter drop ;
342 : <FactorView> ( dim pixel-format -- view )
343 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
345 : save-position ( world window -- )
346 -> frame CGRect-top-left 2array >>window-loc drop ;
348 CLASS: FactorWindowDelegate < NSObject
350 METHOD: void windowDidMove: id notification
352 notification -> object -> contentView window
353 [ notification -> object save-position ] when*
356 METHOD: void windowDidBecomeKey: id notification
358 notification -> object -> contentView window
359 [ focus-world ] when*
362 METHOD: void windowDidResignKey: id notification
365 notification -> object -> contentView :> view
366 view window :> window
368 view -> isInFullScreenMode 0 =
369 [ window unfocus-world ] when
373 METHOD: char windowShouldClose: id notification [ 1 ]
375 METHOD: void windowWillClose: id notification
377 notification -> object -> contentView
378 [ window ungraft ] [ unregister-window ] bi
382 : install-window-delegate ( window -- )
383 FactorWindowDelegate install-delegate ;