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.touchbar
6 cocoa.types cocoa.views combinators core-foundation.strings
7 core-graphics core-graphics.types core-text io.encodings.utf8
8 kernel literals locals math math.rectangles namespaces opengl
9 sequences threads ui.gadgets ui.gadgets.private
10 ui.gadgets.worlds ui.gestures ui.private ui.tools.listener
12 IN: ui.backend.cocoa.views
14 : send-mouse-moved ( view event -- )
15 [ mouse-location ] [ drop window ] 2bi
16 [ move-hand fire-motion yield ] [ drop ] if* ;
19 : button ( event -- n )
20 ! Cocoa -> Factor UI button mapping
28 CONSTANT: NSAlphaShiftKeyMask 0x10000
29 CONSTANT: NSShiftKeyMask 0x20000
30 CONSTANT: NSControlKeyMask 0x40000
31 CONSTANT: NSAlternateKeyMask 0x80000
32 CONSTANT: NSCommandKeyMask 0x100000
33 CONSTANT: NSNumericPadKeyMask 0x200000
34 CONSTANT: NSHelpKeyMask 0x400000
35 CONSTANT: NSFunctionKeyMask 0x800000
38 { S+ $ NSShiftKeyMask }
39 { C+ $ NSControlKeyMask }
40 { A+ $ NSCommandKeyMask }
41 { M+ $ NSAlternateKeyMask }
71 : key-code ( event -- string ? )
72 dup -> keyCode key-codes at
73 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
75 : event-modifiers ( event -- modifiers )
76 -> modifierFlags modifiers modifier ;
78 : key-event>gesture ( event -- modifiers keycode action? )
79 [ event-modifiers ] [ key-code ] bi ;
81 : send-key-event ( view gesture -- )
82 swap window [ propagate-key-gesture ] [ drop ] if* ;
84 : interpret-key-event ( view event -- )
85 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
87 : send-key-down-event ( view event -- )
88 [ key-event>gesture <key-down> send-key-event ]
89 [ interpret-key-event ]
92 : send-key-up-event ( view event -- )
93 key-event>gesture <key-up> send-key-event ;
95 : mouse-event>gesture ( event -- modifiers button )
96 [ event-modifiers ] [ button ] bi ;
98 : send-button-down$ ( view event -- )
99 [ nip mouse-event>gesture <button-down> ]
103 [ send-button-down ] [ 2drop ] if* ;
105 : send-button-up$ ( view event -- )
106 [ nip mouse-event>gesture <button-up> ]
110 [ send-button-up ] [ 2drop ] if* ;
112 : send-scroll$ ( view event -- )
113 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
117 [ send-scroll ] [ 2drop ] if* ;
119 : send-action$ ( view event gesture -- )
120 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
122 : add-resize-observer ( observer object -- )
124 "updateFactorGadgetSize:"
125 "NSViewFrameDidChangeNotification" <NSString>
128 : string-or-nil? ( NSString -- ? )
129 [ CF>string NSStringPboardType = ] [ t ] if* ;
131 : valid-service? ( gadget send-type return-type -- ? )
132 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
133 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
135 : NSRect>rect ( NSRect world -- rect )
136 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
137 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
140 : rect>NSRect ( rect world -- NSRect )
141 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
142 [ drop dim>> first2 ]
145 CONSTANT: selector>action H{
146 { "undo:" undo-action }
147 { "redo:" redo-action }
148 { "cut:" cut-action }
149 { "copy:" copy-action }
150 { "paste:" paste-action }
151 { "delete:" delete-action }
152 { "selectAll:" select-all-action }
153 { "newDocument:" new-action }
154 { "openDocument:" open-action }
155 { "saveDocument:" save-action }
156 { "saveDocumentAs:" save-as-action }
157 { "revertDocumentToSaved:" revert-action }
160 : validate-action ( world selector -- ? validated? )
162 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
164 CLASS: FactorView < NSOpenGLView
165 COCOA-PROTOCOL: NSTextInput
167 METHOD: void prepareOpenGL [
168 self SUPER-> prepareOpenGL
170 self SEL: setWantsBestResolutionOpenGLSurface:
171 -> respondsToSelector: c-bool> [
173 self SEL: setWantsBestResolutionOpenGLSurface: 1
174 void f "objc_msgSend" { id SEL char } f alien-invoke
176 self SEL: backingScaleFactor
177 double f "objc_msgSend" { id SEL } f alien-invoke
180 gl-scale-factor set-global t retina? set-global
181 cached-lines get-global clear-assoc
187 METHOD: void refreshAllAction [
188 [ refresh-all ] \ refresh-all call-listener
191 METHOD: void autoUseAction [
192 [ com-auto-use ] \ com-auto-use call-listener
195 METHOD: Class makeTouchBar [ default-touchbar self make-touchbar ] ;
197 METHOD: Class touchBar: Class touchbar makeItemForIdentifier: Class string [
200 { "refresh-all-action" [
201 self "refresh-all-action" "refresh-all" "refreshAllAction" make-NSTouchBar-button
203 { "auto-use-action" [
204 self "auto-use-action" "auto-use" "autoUseAction" make-NSTouchBar-button
211 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
214 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
216 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
218 METHOD: void mouseExited: id event [ forget-rollover ] ;
220 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
222 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
224 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
226 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
228 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
230 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
232 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
234 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
236 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
238 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
240 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
242 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
244 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
246 METHOD: char validateUserInterfaceItem: id event
249 event -> action utf8 alien>string validate-action
250 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
254 METHOD: id undo: id event [ self event undo-action send-action$ f ] ;
256 METHOD: id redo: id event [ self event redo-action send-action$ f ] ;
258 METHOD: id cut: id event [ self event cut-action send-action$ f ] ;
260 METHOD: id copy: id event [ self event copy-action send-action$ f ] ;
262 METHOD: id paste: id event [ self event paste-action send-action$ f ] ;
264 METHOD: id delete: id event [ self event delete-action send-action$ f ] ;
266 METHOD: id selectAll: id event [ self event select-all-action send-action$ f ] ;
268 METHOD: id newDocument: id event [ self event new-action send-action$ f ] ;
270 METHOD: id openDocument: id event [ self event open-action send-action$ f ] ;
272 METHOD: id saveDocument: id event [ self event save-action send-action$ f ] ;
274 METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ] ;
276 METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ] ;
278 ! Multi-touch gestures
279 METHOD: void magnifyWithEvent: id event
283 { 1 [ zoom-in-action send-action$ ] }
284 { -1 [ zoom-out-action send-action$ ] }
289 METHOD: void swipeWithEvent: id event
293 { 1 [ left-action send-action$ ] }
294 { -1 [ right-action send-action$ ] }
298 { 1 [ up-action send-action$ ] }
299 { -1 [ down-action send-action$ ] }
307 METHOD: char acceptsFirstResponder [ 1 ] ;
310 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
312 ! We return either self or nil
314 world-focus sendType returnType
315 valid-service? [ self ] [ f ] if
319 METHOD: char writeSelectionToPasteboard: id pboard types: id types
321 NSStringPboardType types CF>string-array member? [
323 world-focus gadget-selection
324 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
329 METHOD: char readSelectionFromPasteboard: id pboard
331 self window :> window
333 pboard pasteboard-string
334 [ window user-input 1 ] [ 0 ] if*
339 METHOD: void insertText: id text
341 self window :> window
343 text CF>string window user-input
347 METHOD: char hasMarkedText [ 0 ] ;
349 METHOD: NSRange markedRange [ 0 0 <NSRange> ] ;
351 METHOD: NSRange selectedRange [ 0 0 <NSRange> ] ;
353 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ] ;
355 METHOD: void unmarkText [ ] ;
357 METHOD: id validAttributesForMarkedText [ NSArray -> array ] ;
359 METHOD: id attributedSubstringFromRange: NSRange range [ f ] ;
361 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
363 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ] ;
365 METHOD: NSInteger conversationIdentifier [ self alien-address ] ;
368 METHOD: void updateFactorGadgetSize: id notification
370 self window :> window
372 self view-dim window dim<< yield
376 METHOD: void doCommandBySelector: SEL selector [ ] ;
378 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
380 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
381 dup dup add-resize-observer
384 METHOD: char isOpaque [ 0 ] ;
393 : sync-refresh-to-screen ( GLView -- )
394 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
395 CGLSetParameter drop ;
397 : <FactorView> ( dim pixel-format -- view )
398 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
400 : save-position ( world window -- )
401 -> frame CGRect-top-left 2array >>window-loc drop ;
403 CLASS: FactorWindowDelegate < NSObject
405 METHOD: void windowDidMove: id notification
407 notification -> object -> contentView window
408 [ notification -> object save-position ] when*
411 METHOD: void windowDidBecomeKey: id notification
413 notification -> object -> contentView window
414 [ focus-world ] when*
417 METHOD: void windowDidResignKey: id notification
420 notification -> object -> contentView :> view
421 view window :> window
423 view -> isInFullScreenMode 0 =
424 [ window unfocus-world ] when
428 METHOD: char windowShouldClose: id notification [ 1 ] ;
430 METHOD: void windowWillClose: id notification
432 notification -> object -> contentView
433 [ window ungraft ] [ unregister-window ] bi
436 METHOD: void windowDidChangeBackingProperties: id notification
439 notification -> object dup SEL: backingScaleFactor
440 -> respondsToSelector: c-bool> [
441 { double { id SEL } } ?-> backingScaleFactor
443 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
444 [ 1.0 > retina? set-global ] bi
449 : install-window-delegate ( window -- )
450 FactorWindowDelegate install-delegate ;