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.rectangles namespaces opengl sequences threads
9 ui.gadgets ui.gadgets.private ui.gadgets.worlds ui.gestures
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
60 : key-code ( event -- string ? )
61 dup -> keyCode key-codes at
62 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
64 : event-modifiers ( event -- modifiers )
65 -> modifierFlags modifiers modifier ;
67 : key-event>gesture ( event -- modifiers keycode action? )
68 [ event-modifiers ] [ key-code ] bi ;
70 : send-key-event ( view gesture -- )
71 swap window dup [ propagate-key-gesture ] [ 2drop ] if ;
73 : interpret-key-event ( view event -- )
74 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
76 : send-key-down-event ( view event -- )
77 [ key-event>gesture <key-down> send-key-event ]
78 [ interpret-key-event ]
81 : send-key-up-event ( view event -- )
82 key-event>gesture <key-up> send-key-event ;
84 : mouse-event>gesture ( event -- modifiers button )
85 [ event-modifiers ] [ button ] bi ;
87 : send-button-down$ ( view event -- )
88 [ nip mouse-event>gesture <button-down> ]
92 dup [ send-button-down ] [ 3drop ] if ;
94 : send-button-up$ ( view event -- )
95 [ nip mouse-event>gesture <button-up> ]
99 dup [ send-button-up ] [ 3drop ] if ;
101 : send-scroll$ ( view event -- )
102 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
106 dup [ send-scroll ] [ 3drop ] if ;
108 : send-action$ ( view event gesture -- )
109 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
111 : add-resize-observer ( observer object -- )
113 "updateFactorGadgetSize:"
114 "NSViewFrameDidChangeNotification" <NSString>
117 : string-or-nil? ( NSString -- ? )
118 [ CF>string NSStringPboardType = ] [ t ] if* ;
120 : valid-service? ( gadget send-type return-type -- ? )
121 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
122 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
124 : NSRect>rect ( NSRect world -- rect )
125 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
126 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
129 : rect>NSRect ( rect world -- NSRect )
130 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
131 [ drop dim>> first2 ]
134 CONSTANT: selector>action H{
135 { "undo:" undo-action }
136 { "redo:" redo-action }
137 { "cut:" cut-action }
138 { "copy:" copy-action }
139 { "paste:" paste-action }
140 { "delete:" delete-action }
141 { "selectAll:" select-all-action }
142 { "newDocument:" new-action }
143 { "openDocument:" open-action }
144 { "saveDocument:" save-action }
145 { "saveDocumentAs:" save-as-action }
146 { "revertDocumentToSaved:" revert-action }
149 : validate-action ( world selector -- ? validated? )
151 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
153 CLASS: FactorView < NSOpenGLView NSTextInput
156 METHOD: void prepareOpenGL [
158 self SEL: setWantsBestResolutionOpenGLSurface:
159 -> respondsToSelector: c-bool> [
161 self SEL: setWantsBestResolutionOpenGLSurface: 1
162 void f "objc_msgSend" { id SEL char } alien-invoke
164 self SEL: backingScaleFactor
165 double f "objc_msgSend" { id SEL } alien-invoke
168 gl-scale-factor set-global t retina? set-global
169 cached-lines get-global clear-assoc
176 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ]
179 METHOD: char acceptsFirstMouse: id event [ 0 ]
181 METHOD: void mouseEntered: id event [ self event send-mouse-moved ]
183 METHOD: void mouseExited: id event [ forget-rollover ]
185 METHOD: void mouseMoved: id event [ self event send-mouse-moved ]
187 METHOD: void mouseDragged: id event [ self event send-mouse-moved ]
189 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ]
191 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ]
193 METHOD: void mouseDown: id event [ self event send-button-down$ ]
195 METHOD: void mouseUp: id event [ self event send-button-up$ ]
197 METHOD: void rightMouseDown: id event [ self event send-button-down$ ]
199 METHOD: void rightMouseUp: id event [ self event send-button-up$ ]
201 METHOD: void otherMouseDown: id event [ self event send-button-down$ ]
203 METHOD: void otherMouseUp: id event [ self event send-button-up$ ]
205 METHOD: void scrollWheel: id event [ self event send-scroll$ ]
207 METHOD: void keyDown: id event [ self event send-key-down-event ]
209 METHOD: void keyUp: id event [ self event send-key-up-event ]
211 METHOD: char validateUserInterfaceItem: id event
214 event -> action utf8 alien>string validate-action
215 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
219 METHOD: id undo: id event [ self event undo-action send-action$ f ]
221 METHOD: id redo: id event [ self event redo-action send-action$ f ]
223 METHOD: id cut: id event [ self event cut-action send-action$ f ]
225 METHOD: id copy: id event [ self event copy-action send-action$ f ]
227 METHOD: id paste: id event [ self event paste-action send-action$ f ]
229 METHOD: id delete: id event [ self event delete-action send-action$ f ]
231 METHOD: id selectAll: id event [ self event select-all-action send-action$ f ]
233 METHOD: id newDocument: id event [ self event new-action send-action$ f ]
235 METHOD: id openDocument: id event [ self event open-action send-action$ f ]
237 METHOD: id saveDocument: id event [ self event save-action send-action$ f ]
239 METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ]
241 METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ]
243 ! Multi-touch gestures
244 METHOD: void magnifyWithEvent: id event
248 { 1 [ zoom-in-action send-action$ ] }
249 { -1 [ zoom-out-action send-action$ ] }
254 METHOD: void swipeWithEvent: id event
258 { 1 [ left-action send-action$ ] }
259 { -1 [ right-action send-action$ ] }
263 { 1 [ up-action send-action$ ] }
264 { -1 [ down-action send-action$ ] }
272 METHOD: char acceptsFirstResponder [ 1 ]
275 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
277 ! We return either self or nil
279 world-focus sendType returnType
280 valid-service? [ self ] [ f ] if
284 METHOD: char writeSelectionToPasteboard: id pboard types: id types
286 NSStringPboardType types CF>string-array member? [
288 world-focus gadget-selection
289 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
294 METHOD: char readSelectionFromPasteboard: id pboard
296 self window :> window
298 pboard pasteboard-string
299 [ window user-input 1 ] [ 0 ] if*
304 METHOD: void insertText: id text
306 self window :> window
308 text CF>string window user-input
312 METHOD: char hasMarkedText [ 0 ]
314 METHOD: NSRange markedRange [ 0 0 <NSRange> ]
316 METHOD: NSRange selectedRange [ 0 0 <NSRange> ]
318 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ]
320 METHOD: void unmarkText [ ]
322 METHOD: id validAttributesForMarkedText [ NSArray -> array ]
324 METHOD: id attributedSubstringFromRange: NSRange range [ f ]
326 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ]
328 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ]
330 METHOD: NSInteger conversationIdentifier [ self alien-address ]
333 METHOD: void updateFactorGadgetSize: id notification
335 self window :> window
337 self view-dim window dim<< yield
341 METHOD: void doCommandBySelector: SEL selector [ ]
343 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
345 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
346 dup dup add-resize-observer
349 METHOD: char isOpaque [ 0 ]
358 : sync-refresh-to-screen ( GLView -- )
359 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
360 CGLSetParameter drop ;
362 : <FactorView> ( dim pixel-format -- view )
363 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
365 : save-position ( world window -- )
366 -> frame CGRect-top-left 2array >>window-loc drop ;
368 CLASS: FactorWindowDelegate < NSObject
370 METHOD: void windowDidMove: id notification
372 notification -> object -> contentView window
373 [ notification -> object save-position ] when*
376 METHOD: void windowDidBecomeKey: id notification
378 notification -> object -> contentView window
379 [ focus-world ] when*
382 METHOD: void windowDidResignKey: id notification
385 notification -> object -> contentView :> view
386 view window :> window
388 view -> isInFullScreenMode 0 =
389 [ window unfocus-world ] when
393 METHOD: char windowShouldClose: id notification [ 1 ]
395 METHOD: void windowWillClose: id notification
397 notification -> object -> contentView
398 [ window ungraft ] [ unregister-window ] bi
401 METHOD: void windowDidChangeBackingProperties: id notification
404 notification -> object dup SEL: backingScaleFactor
405 -> respondsToSelector: c-bool> [
407 SEL: backingScaleFactor
408 double f "objc_msgSend" { id SEL } alien-invoke
410 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
411 [ 1.0 > retina? set-global ] bi
416 : install-window-delegate ( window -- )
417 FactorWindowDelegate install-delegate ;