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 [ move-hand fire-motion yield ] [ drop ] if* ;
18 : button ( event -- n )
19 ! Cocoa -> Factor UI button mapping
62 : key-code ( event -- string ? )
63 dup -> keyCode key-codes at
64 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
66 : event-modifiers ( event -- modifiers )
67 -> modifierFlags modifiers modifier ;
69 : key-event>gesture ( event -- modifiers keycode action? )
70 [ event-modifiers ] [ key-code ] bi ;
72 : send-key-event ( view gesture -- )
73 swap window [ propagate-key-gesture ] [ drop ] if* ;
75 : interpret-key-event ( view event -- )
76 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
78 : send-key-down-event ( view event -- )
79 [ key-event>gesture <key-down> send-key-event ]
80 [ interpret-key-event ]
83 : send-key-up-event ( view event -- )
84 key-event>gesture <key-up> send-key-event ;
86 : mouse-event>gesture ( event -- modifiers button )
87 [ event-modifiers ] [ button ] bi ;
89 : send-button-down$ ( view event -- )
90 [ nip mouse-event>gesture <button-down> ]
94 [ send-button-down ] [ 2drop ] if* ;
96 : send-button-up$ ( view event -- )
97 [ nip mouse-event>gesture <button-up> ]
101 [ send-button-up ] [ 2drop ] if* ;
103 : send-scroll$ ( view event -- )
104 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
108 [ send-scroll ] [ 2drop ] if* ;
110 : send-action$ ( view event gesture -- )
111 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
113 : add-resize-observer ( observer object -- )
115 "updateFactorGadgetSize:"
116 "NSViewFrameDidChangeNotification" <NSString>
119 : string-or-nil? ( NSString -- ? )
120 [ CF>string NSStringPboardType = ] [ t ] if* ;
122 : valid-service? ( gadget send-type return-type -- ? )
123 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
124 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
126 : NSRect>rect ( NSRect world -- rect )
127 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
128 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
131 : rect>NSRect ( rect world -- NSRect )
132 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
133 [ drop dim>> first2 ]
136 CONSTANT: selector>action H{
137 { "undo:" undo-action }
138 { "redo:" redo-action }
139 { "cut:" cut-action }
140 { "copy:" copy-action }
141 { "paste:" paste-action }
142 { "delete:" delete-action }
143 { "selectAll:" select-all-action }
144 { "newDocument:" new-action }
145 { "openDocument:" open-action }
146 { "saveDocument:" save-action }
147 { "saveDocumentAs:" save-as-action }
148 { "revertDocumentToSaved:" revert-action }
151 : validate-action ( world selector -- ? validated? )
153 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
155 CLASS: FactorView < NSOpenGLView
156 COCOA-PROTOCOL: NSTextInput
158 METHOD: void prepareOpenGL [
160 self SEL: setWantsBestResolutionOpenGLSurface:
161 -> respondsToSelector: c-bool> [
163 self SEL: setWantsBestResolutionOpenGLSurface: 1
164 void f "objc_msgSend" { id SEL char } alien-invoke
166 self SEL: backingScaleFactor
167 double f "objc_msgSend" { id SEL } alien-invoke
170 gl-scale-factor set-global t retina? set-global
171 cached-lines get-global clear-assoc
178 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
181 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
183 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
185 METHOD: void mouseExited: id event [ forget-rollover ] ;
187 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
189 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
191 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
193 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
195 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
197 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
199 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
201 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
203 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
205 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
207 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
209 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
211 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
213 METHOD: char validateUserInterfaceItem: id event
216 event -> action utf8 alien>string validate-action
217 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
221 METHOD: id undo: id event [ self event undo-action send-action$ f ] ;
223 METHOD: id redo: id event [ self event redo-action send-action$ f ] ;
225 METHOD: id cut: id event [ self event cut-action send-action$ f ] ;
227 METHOD: id copy: id event [ self event copy-action send-action$ f ] ;
229 METHOD: id paste: id event [ self event paste-action send-action$ f ] ;
231 METHOD: id delete: id event [ self event delete-action send-action$ f ] ;
233 METHOD: id selectAll: id event [ self event select-all-action send-action$ f ] ;
235 METHOD: id newDocument: id event [ self event new-action send-action$ f ] ;
237 METHOD: id openDocument: id event [ self event open-action send-action$ f ] ;
239 METHOD: id saveDocument: id event [ self event save-action send-action$ f ] ;
241 METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ] ;
243 METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ] ;
245 ! Multi-touch gestures
246 METHOD: void magnifyWithEvent: id event
250 { 1 [ zoom-in-action send-action$ ] }
251 { -1 [ zoom-out-action send-action$ ] }
256 METHOD: void swipeWithEvent: id event
260 { 1 [ left-action send-action$ ] }
261 { -1 [ right-action send-action$ ] }
265 { 1 [ up-action send-action$ ] }
266 { -1 [ down-action send-action$ ] }
274 METHOD: char acceptsFirstResponder [ 1 ] ;
277 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
279 ! We return either self or nil
281 world-focus sendType returnType
282 valid-service? [ self ] [ f ] if
286 METHOD: char writeSelectionToPasteboard: id pboard types: id types
288 NSStringPboardType types CF>string-array member? [
290 world-focus gadget-selection
291 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
296 METHOD: char readSelectionFromPasteboard: id pboard
298 self window :> window
300 pboard pasteboard-string
301 [ window user-input 1 ] [ 0 ] if*
306 METHOD: void insertText: id text
308 self window :> window
310 text CF>string window user-input
314 METHOD: char hasMarkedText [ 0 ] ;
316 METHOD: NSRange markedRange [ 0 0 <NSRange> ] ;
318 METHOD: NSRange selectedRange [ 0 0 <NSRange> ] ;
320 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ] ;
322 METHOD: void unmarkText [ ] ;
324 METHOD: id validAttributesForMarkedText [ NSArray -> array ] ;
326 METHOD: id attributedSubstringFromRange: NSRange range [ f ] ;
328 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
330 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ] ;
332 METHOD: NSInteger conversationIdentifier [ self alien-address ] ;
335 METHOD: void updateFactorGadgetSize: id notification
337 self window :> window
339 self view-dim window dim<< yield
343 METHOD: void doCommandBySelector: SEL selector [ ] ;
345 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
347 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
348 dup dup add-resize-observer
351 METHOD: char isOpaque [ 0 ] ;
360 : sync-refresh-to-screen ( GLView -- )
361 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
362 CGLSetParameter drop ;
364 : <FactorView> ( dim pixel-format -- view )
365 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
367 : save-position ( world window -- )
368 -> frame CGRect-top-left 2array >>window-loc drop ;
370 CLASS: FactorWindowDelegate < NSObject
372 METHOD: void windowDidMove: id notification
374 notification -> object -> contentView window
375 [ notification -> object save-position ] when*
378 METHOD: void windowDidBecomeKey: id notification
380 notification -> object -> contentView window
381 [ focus-world ] when*
384 METHOD: void windowDidResignKey: id notification
387 notification -> object -> contentView :> view
388 view window :> window
390 view -> isInFullScreenMode 0 =
391 [ window unfocus-world ] when
395 METHOD: char windowShouldClose: id notification [ 1 ] ;
397 METHOD: void windowWillClose: id notification
399 notification -> object -> contentView
400 [ window ungraft ] [ unregister-window ] bi
403 METHOD: void windowDidChangeBackingProperties: id notification
406 notification -> object dup SEL: backingScaleFactor
407 -> respondsToSelector: c-bool> [
409 SEL: backingScaleFactor
410 double f "objc_msgSend" { id SEL } alien-invoke
412 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
413 [ 1.0 > retina? set-global ] bi
418 : install-window-delegate ( window -- )
419 FactorWindowDelegate install-delegate ;