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 literals
8 locals 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
27 CONSTANT: NSAlphaShiftKeyMask 0x10000
28 CONSTANT: NSShiftKeyMask 0x20000
29 CONSTANT: NSControlKeyMask 0x40000
30 CONSTANT: NSAlternateKeyMask 0x80000
31 CONSTANT: NSCommandKeyMask 0x100000
32 CONSTANT: NSNumericPadKeyMask 0x200000
33 CONSTANT: NSHelpKeyMask 0x400000
34 CONSTANT: NSFunctionKeyMask 0x800000
37 { S+ $ NSShiftKeyMask }
38 { C+ $ NSControlKeyMask }
39 { A+ $ NSCommandKeyMask }
40 { M+ $ NSAlternateKeyMask }
70 : key-code ( event -- string ? )
71 dup -> keyCode key-codes at
72 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
74 : event-modifiers ( event -- modifiers )
75 -> modifierFlags modifiers modifier ;
77 : key-event>gesture ( event -- modifiers keycode action? )
78 [ event-modifiers ] [ key-code ] bi ;
80 : send-key-event ( view gesture -- )
81 swap window [ propagate-key-gesture ] [ drop ] if* ;
83 : interpret-key-event ( view event -- )
84 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
86 : send-key-down-event ( view event -- )
87 [ key-event>gesture <key-down> send-key-event ]
88 [ interpret-key-event ]
91 : send-key-up-event ( view event -- )
92 key-event>gesture <key-up> send-key-event ;
94 : mouse-event>gesture ( event -- modifiers button )
95 [ event-modifiers ] [ button ] bi ;
97 : send-button-down$ ( view event -- )
98 [ nip mouse-event>gesture <button-down> ]
102 [ send-button-down ] [ 2drop ] if* ;
104 : send-button-up$ ( view event -- )
105 [ nip mouse-event>gesture <button-up> ]
109 [ send-button-up ] [ 2drop ] if* ;
111 : send-scroll$ ( view event -- )
112 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
116 [ send-scroll ] [ 2drop ] if* ;
118 : send-action$ ( view event gesture -- )
119 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
121 : add-resize-observer ( observer object -- )
123 "updateFactorGadgetSize:"
124 "NSViewFrameDidChangeNotification" <NSString>
127 : string-or-nil? ( NSString -- ? )
128 [ CF>string NSStringPboardType = ] [ t ] if* ;
130 : valid-service? ( gadget send-type return-type -- ? )
131 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
132 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
134 : NSRect>rect ( NSRect world -- rect )
135 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
136 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
139 : rect>NSRect ( rect world -- NSRect )
140 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
141 [ drop dim>> first2 ]
144 CONSTANT: selector>action H{
145 { "undo:" undo-action }
146 { "redo:" redo-action }
147 { "cut:" cut-action }
148 { "copy:" copy-action }
149 { "paste:" paste-action }
150 { "delete:" delete-action }
151 { "selectAll:" select-all-action }
152 { "newDocument:" new-action }
153 { "openDocument:" open-action }
154 { "saveDocument:" save-action }
155 { "saveDocumentAs:" save-as-action }
156 { "revertDocumentToSaved:" revert-action }
159 : validate-action ( world selector -- ? validated? )
161 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
163 <CLASS: FactorView < NSOpenGLView
164 COCOA-PROTOCOL: NSTextInput
166 METHOD: void prepareOpenGL [
168 self SEL: setWantsBestResolutionOpenGLSurface:
169 -> respondsToSelector: c-bool> [
171 self SEL: setWantsBestResolutionOpenGLSurface: 1
172 void f "objc_msgSend" { id SEL char } f alien-invoke
174 self SEL: backingScaleFactor
175 double f "objc_msgSend" { id SEL } f alien-invoke
178 gl-scale-factor set-global t retina? set-global
179 cached-lines get-global clear-assoc
186 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
189 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
191 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
193 METHOD: void mouseExited: id event [ forget-rollover ] ;
195 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
197 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
199 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
201 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
203 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
205 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
207 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
209 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
211 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
213 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
215 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
217 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
219 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
221 METHOD: char validateUserInterfaceItem: id event
224 event -> action utf8 alien>string validate-action
225 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
229 METHOD: id undo: id event [ self event undo-action send-action$ f ] ;
231 METHOD: id redo: id event [ self event redo-action send-action$ f ] ;
233 METHOD: id cut: id event [ self event cut-action send-action$ f ] ;
235 METHOD: id copy: id event [ self event copy-action send-action$ f ] ;
237 METHOD: id paste: id event [ self event paste-action send-action$ f ] ;
239 METHOD: id delete: id event [ self event delete-action send-action$ f ] ;
241 METHOD: id selectAll: id event [ self event select-all-action send-action$ f ] ;
243 METHOD: id newDocument: id event [ self event new-action send-action$ f ] ;
245 METHOD: id openDocument: id event [ self event open-action send-action$ f ] ;
247 METHOD: id saveDocument: id event [ self event save-action send-action$ f ] ;
249 METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ] ;
251 METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ] ;
253 ! Multi-touch gestures
254 METHOD: void magnifyWithEvent: id event
258 { 1 [ zoom-in-action send-action$ ] }
259 { -1 [ zoom-out-action send-action$ ] }
264 METHOD: void swipeWithEvent: id event
268 { 1 [ left-action send-action$ ] }
269 { -1 [ right-action send-action$ ] }
273 { 1 [ up-action send-action$ ] }
274 { -1 [ down-action send-action$ ] }
282 METHOD: char acceptsFirstResponder [ 1 ] ;
285 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
287 ! We return either self or nil
289 world-focus sendType returnType
290 valid-service? [ self ] [ f ] if
294 METHOD: char writeSelectionToPasteboard: id pboard types: id types
296 NSStringPboardType types CF>string-array member? [
298 world-focus gadget-selection
299 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
304 METHOD: char readSelectionFromPasteboard: id pboard
306 self window :> window
308 pboard pasteboard-string
309 [ window user-input 1 ] [ 0 ] if*
314 METHOD: void insertText: id text
316 self window :> window
318 text CF>string window user-input
322 METHOD: char hasMarkedText [ 0 ] ;
324 METHOD: NSRange markedRange [ 0 0 <NSRange> ] ;
326 METHOD: NSRange selectedRange [ 0 0 <NSRange> ] ;
328 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ] ;
330 METHOD: void unmarkText [ ] ;
332 METHOD: id validAttributesForMarkedText [ NSArray -> array ] ;
334 METHOD: id attributedSubstringFromRange: NSRange range [ f ] ;
336 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
338 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ] ;
340 METHOD: NSInteger conversationIdentifier [ self alien-address ] ;
343 METHOD: void updateFactorGadgetSize: id notification
345 self window :> window
347 self view-dim window dim<< yield
351 METHOD: void doCommandBySelector: SEL selector [ ] ;
353 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
355 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
356 dup dup add-resize-observer
359 METHOD: char isOpaque [ 0 ] ;
368 : sync-refresh-to-screen ( GLView -- )
369 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
370 CGLSetParameter drop ;
372 : <FactorView> ( dim pixel-format -- view )
373 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
375 : save-position ( world window -- )
376 -> frame CGRect-top-left 2array >>window-loc drop ;
378 <CLASS: FactorWindowDelegate < NSObject
380 METHOD: void windowDidMove: id notification
382 notification -> object -> contentView window
383 [ notification -> object save-position ] when*
386 METHOD: void windowDidBecomeKey: id notification
388 notification -> object -> contentView window
389 [ focus-world ] when*
392 METHOD: void windowDidResignKey: id notification
395 notification -> object -> contentView :> view
396 view window :> window
398 view -> isInFullScreenMode 0 =
399 [ window unfocus-world ] when
403 METHOD: char windowShouldClose: id notification [ 1 ] ;
405 METHOD: void windowWillClose: id notification
407 notification -> object -> contentView
408 [ window ungraft ] [ unregister-window ] bi
411 METHOD: void windowDidChangeBackingProperties: id notification
414 notification -> object dup SEL: backingScaleFactor
415 -> respondsToSelector: c-bool> [
416 { double { id SEL } } ?-> backingScaleFactor
418 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
419 [ 1.0 > retina? set-global ] bi
424 : install-window-delegate ( window -- )
425 FactorWindowDelegate install-delegate ;