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 core-text io.encodings.utf8 kernel locals
8 math math.order math.rectangles namespaces opengl sequences
9 system-info threads ui.gadgets ui.gadgets.private
10 ui.gadgets.worlds ui.gestures ui.private vocabs vocabs.parser ;
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* ;
150 os-version { 10 7 0 } after=? "retina" "non-retina" ?
151 "ui.backend.cocoa.views." prepend use-vocab
154 CLASS: FactorView < BaseFactorView
157 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ]
160 METHOD: char acceptsFirstMouse: id event [ 0 ]
162 METHOD: void mouseEntered: id event [ self event send-mouse-moved ]
164 METHOD: void mouseExited: id event [ forget-rollover ]
166 METHOD: void mouseMoved: id event [ self event send-mouse-moved ]
168 METHOD: void mouseDragged: id event [ self event send-mouse-moved ]
170 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ]
172 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ]
174 METHOD: void mouseDown: id event [ self event send-button-down$ ]
176 METHOD: void mouseUp: id event [ self event send-button-up$ ]
178 METHOD: void rightMouseDown: id event [ self event send-button-down$ ]
180 METHOD: void rightMouseUp: id event [ self event send-button-up$ ]
182 METHOD: void otherMouseDown: id event [ self event send-button-down$ ]
184 METHOD: void otherMouseUp: id event [ self event send-button-up$ ]
186 METHOD: void scrollWheel: id event [ self event send-scroll$ ]
188 METHOD: void keyDown: id event [ self event send-key-down-event ]
190 METHOD: void keyUp: id event [ self event send-key-up-event ]
192 METHOD: char validateUserInterfaceItem: id event
195 event -> action utf8 alien>string validate-action
196 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
200 METHOD: id undo: id event [ self event undo-action send-action$ f ]
202 METHOD: id redo: id event [ self event redo-action send-action$ f ]
204 METHOD: id cut: id event [ self event cut-action send-action$ f ]
206 METHOD: id copy: id event [ self event copy-action send-action$ f ]
208 METHOD: id paste: id event [ self event paste-action send-action$ f ]
210 METHOD: id delete: id event [ self event delete-action send-action$ f ]
212 METHOD: id selectAll: id event [ self event select-all-action send-action$ f ]
214 METHOD: id newDocument: id event [ self event new-action send-action$ f ]
216 METHOD: id openDocument: id event [ self event open-action send-action$ f ]
218 METHOD: id saveDocument: id event [ self event save-action send-action$ f ]
220 METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ]
222 METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ]
224 ! Multi-touch gestures
225 METHOD: void magnifyWithEvent: id event
229 { 1 [ zoom-in-action send-action$ ] }
230 { -1 [ zoom-out-action send-action$ ] }
235 METHOD: void swipeWithEvent: id event
239 { 1 [ left-action send-action$ ] }
240 { -1 [ right-action send-action$ ] }
244 { 1 [ up-action send-action$ ] }
245 { -1 [ down-action send-action$ ] }
253 METHOD: char acceptsFirstResponder [ 1 ]
256 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
258 ! We return either self or nil
260 world-focus sendType returnType
261 valid-service? [ self ] [ f ] if
265 METHOD: char writeSelectionToPasteboard: id pboard types: id types
267 NSStringPboardType types CF>string-array member? [
269 world-focus gadget-selection
270 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
275 METHOD: char readSelectionFromPasteboard: id pboard
277 self window :> window
279 pboard pasteboard-string
280 [ window user-input 1 ] [ 0 ] if*
285 METHOD: void insertText: id text
287 self window :> window
289 text CF>string window user-input
293 METHOD: char hasMarkedText [ 0 ]
295 METHOD: NSRange markedRange [ 0 0 <NSRange> ]
297 METHOD: NSRange selectedRange [ 0 0 <NSRange> ]
299 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ]
301 METHOD: void unmarkText [ ]
303 METHOD: id validAttributesForMarkedText [ NSArray -> array ]
305 METHOD: id attributedSubstringFromRange: NSRange range [ f ]
307 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ]
309 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ]
311 METHOD: NSInteger conversationIdentifier [ self alien-address ]
314 METHOD: void updateFactorGadgetSize: id notification
316 self window :> window
318 self view-dim window dim<< yield
322 METHOD: void doCommandBySelector: SEL selector [ ]
324 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
326 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
327 dup dup add-resize-observer
330 METHOD: char isOpaque [ 0 ]
339 : sync-refresh-to-screen ( GLView -- )
340 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
341 CGLSetParameter drop ;
343 : <FactorView> ( dim pixel-format -- view )
344 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
346 : save-position ( world window -- )
347 -> frame CGRect-top-left 2array >>window-loc drop ;
349 CLASS: FactorWindowDelegate < NSObject
351 METHOD: void windowDidMove: id notification
353 notification -> object -> contentView window
354 [ notification -> object save-position ] when*
357 METHOD: void windowDidBecomeKey: id notification
359 notification -> object -> contentView window
360 [ focus-world ] when*
363 METHOD: void windowDidResignKey: id notification
366 notification -> object -> contentView :> view
367 view window :> window
369 view -> isInFullScreenMode 0 =
370 [ window unfocus-world ] when
374 METHOD: char windowShouldClose: id notification [ 1 ]
376 METHOD: void windowWillClose: id notification
378 notification -> object -> contentView
379 [ window ungraft ] [ unregister-window ] bi
383 : install-window-delegate ( window -- )
384 FactorWindowDelegate install-delegate ;