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 classes 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.order math.parser
9 math.rectangles namespaces opengl sequences splitting threads
10 ui.commands ui.gadgets ui.gadgets.private ui.gadgets.worlds
11 ui.gestures ui.private words sorting math.vectors
12 ui.baseline-alignment ui.gadgets.line-support
13 ui.gadgets.editors ui.backend.cocoa.input-methods
14 ui.backend.cocoa.input-methods.editors io.encodings.utf16n
15 io.encodings.string classes.struct ;
16 IN: ui.backend.cocoa.views
18 : send-mouse-moved ( view event -- )
19 [ mouse-location ] [ drop window ] 2bi
20 [ move-hand fire-motion yield ] [ drop ] if* ;
23 : button ( event -- n )
24 ! Cocoa -> Factor UI button mapping
32 CONSTANT: NSAlphaShiftKeyMask 0x10000
33 CONSTANT: NSShiftKeyMask 0x20000
34 CONSTANT: NSControlKeyMask 0x40000
35 CONSTANT: NSAlternateKeyMask 0x80000
36 CONSTANT: NSCommandKeyMask 0x100000
37 CONSTANT: NSNumericPadKeyMask 0x200000
38 CONSTANT: NSHelpKeyMask 0x400000
39 CONSTANT: NSFunctionKeyMask 0x800000
42 { S+ $ NSShiftKeyMask }
43 { C+ $ NSControlKeyMask }
44 { A+ $ NSCommandKeyMask }
45 { M+ $ NSAlternateKeyMask }
75 : key-code ( event -- string ? )
76 dup -> keyCode key-codes at
77 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
79 : event-modifiers ( event -- modifiers )
80 -> modifierFlags modifiers modifier ;
82 : key-event>gesture ( event -- modifiers keycode action? )
83 [ event-modifiers ] [ key-code ] bi ;
85 : send-key-event ( view gesture -- )
86 swap window [ propagate-key-gesture ] [ drop ] if* ;
88 : interpret-key-event ( view event -- )
89 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
91 : send-key-down-event ( view event -- )
92 [ key-event>gesture <key-down> send-key-event ]
93 [ interpret-key-event ]
96 : send-key-up-event ( view event -- )
97 key-event>gesture <key-up> send-key-event ;
99 : mouse-event>gesture ( event -- modifiers button )
100 [ event-modifiers ] [ button ] bi ;
102 : send-button-down$ ( view event -- )
103 [ nip mouse-event>gesture <button-down> ]
107 [ send-button-down ] [ 2drop ] if* ;
109 : send-button-up$ ( view event -- )
110 [ nip mouse-event>gesture <button-up> ]
114 [ send-button-up ] [ 2drop ] if* ;
116 : send-scroll$ ( view event -- )
117 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
121 [ send-scroll ] [ 2drop ] if* ;
123 : send-action$ ( view event gesture -- )
124 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
126 : add-resize-observer ( observer object -- )
128 "updateFactorGadgetSize:"
129 "NSViewFrameDidChangeNotification" <NSString>
132 : string-or-nil? ( NSString -- ? )
133 [ CF>string NSStringPboardType = ] [ t ] if* ;
135 : valid-service? ( gadget send-type return-type -- ? )
136 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
137 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
139 : NSRect>rect ( NSRect world -- rect )
140 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
141 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
144 : rect>NSRect ( rect world -- NSRect )
145 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
146 [ drop dim>> first2 ]
149 CONSTANT: selector>action H{
150 { "undo:" undo-action }
151 { "redo:" redo-action }
152 { "cut:" cut-action }
153 { "copy:" copy-action }
154 { "paste:" paste-action }
155 { "delete:" delete-action }
156 { "selectAll:" select-all-action }
157 { "newDocument:" new-action }
158 { "openDocument:" open-action }
159 { "saveDocument:" save-action }
160 { "saveDocumentAs:" save-as-action }
161 { "revertDocumentToSaved:" revert-action }
164 : validate-action ( world selector -- ? validated? )
166 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
168 : touchbar-commands ( -- commands/f gadget )
171 class-of "commands" word-prop
172 "touchbar" of dup [ commands>> ] when
176 TUPLE: send-touchbar-command target command ;
178 M: send-touchbar-command send-queued-gesture
179 [ target>> ] [ command>> ] bi invoke-command ;
181 : touchbar-invoke-command ( n -- )
182 [ touchbar-commands ] dip over [
184 send-touchbar-command queue-gesture notify-ui-thread
188 CONSTANT: NSNotFound 9223372036854775807 inline
190 IMPORT: NSAttributedString
194 :: >codepoint-index ( str utf16-index -- codepoint-index )
195 0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
197 :: >utf16-index ( str codepoint-index -- utf16-index )
198 0 codepoint-index str subseq utf16n encode length 2 / >integer ;
200 :: earlier-caret/mark ( editor -- loc )
201 editor editor-caret :> caret
202 editor editor-mark :> mark
203 caret first mark first = [
204 caret second mark second < [ caret ] [ mark ] if
206 caret first mark first < [ caret ] [ mark ] if
209 :: make-preedit-underlines ( gadget text range -- underlines )
210 f gadget preedit-selection-mode?<<
211 { } clone :> underlines!
212 text -> length :> text-length
213 0 0 <NSRange> :> effective-range
214 text -> string CF>string :> str
215 str utf16n encode :> byte-16n
217 "NSMarkedClauseSegment" <NSString> :> segment-attr
218 [ effective-range [ location>> ] [ length>> ] bi + text-length < ] [
221 effective-range [ location>> ] [ length>> ] bi +
222 effective-range >c-ptr
223 -> attribute:atIndex:effectiveRange: drop
225 range location>> effective-range location>> = [
227 t gadget preedit-selection-mode?<<
230 effective-range [ location>> ] [ length>> ] bi over +
231 [ str swap >codepoint-index ] bi@ swap - :> len
232 cp-loc cp-loc len + dup cp-loc!
233 2array thickness 2array
236 underlines length 1 = [
237 underlines first first 2 2array 1array ! thickness: 2
238 ] [ underlines ] if ;
240 :: update-marked-text ( gadget str range -- )
242 gadget remove-preedit-text
244 gadget earlier-caret/mark dup
245 gadget preedit-start<<
246 0 str length 2array v+ gadget preedit-end<<
247 str gadget temp-im-input drop
248 gadget preedit-start>>
249 0 str range location>> >codepoint-index
251 dup gadget preedit-selected-start<<
253 range location>> dup range length>> + [ 2 * ] bi@
254 str utf16n encode subseq utf16n decode length
256 dup gadget preedit-selected-end<<
257 dup gadget set-caret gadget set-mark
258 gadget preedit-start>> gadget preedit-end>> = [
259 gadget remove-preedit-info
264 <CLASS: FactorView < NSOpenGLView
265 COCOA-PROTOCOL: NSTextInputClient
267 METHOD: void prepareOpenGL [
269 self SEL: setWantsBestResolutionOpenGLSurface:
270 -> respondsToSelector: c-bool> [
272 self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
274 self { double { id SEL } } ?-> backingScaleFactor
277 gl-scale-factor set-global t retina? set-global
278 cached-lines get-global clear-assoc
286 METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
287 METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
288 METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
289 METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
290 METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
291 METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
292 METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
293 METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
295 METHOD: id makeTouchBar [
296 touchbar-commands drop [
297 length 8 min <iota> [ number>string ] map
298 ] [ { } ] if* self make-touchbar
301 METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
302 touchbar-commands drop [
303 [ self string CF>string dup string>number ] dip nth
304 second name>> "com-" ?head drop over
305 "touchBarCommand" prepend make-NSTouchBar-button
310 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
313 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
315 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
317 METHOD: void mouseExited: id event [ forget-rollover ] ;
319 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
321 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
323 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
325 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
327 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
329 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
331 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
333 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
335 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
337 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
339 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
341 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
343 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
345 METHOD: char validateUserInterfaceItem: id event
348 event -> action utf8 alien>string validate-action
349 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
353 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
355 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
357 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
359 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
361 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
363 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
365 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
367 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
369 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
371 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
373 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
375 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
377 ! Multi-touch gestures
378 METHOD: void magnifyWithEvent: id event
382 { 1 [ zoom-in-action send-action$ ] }
383 { -1 [ zoom-out-action send-action$ ] }
388 METHOD: void swipeWithEvent: id event
392 { 1 [ left-action send-action$ ] }
393 { -1 [ right-action send-action$ ] }
397 { 1 [ up-action send-action$ ] }
398 { -1 [ down-action send-action$ ] }
406 METHOD: char acceptsFirstResponder [ 1 ] ;
409 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
411 ! We return either self or nil
413 world-focus sendType returnType
414 valid-service? [ self ] [ f ] if
418 METHOD: char writeSelectionToPasteboard: id pboard types: id types
420 NSStringPboardType types CF>string-array member? [
422 world-focus gadget-selection
423 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
428 METHOD: char readSelectionFromPasteboard: id pboard
430 self window :> window
432 pboard pasteboard-string
433 [ window user-input 1 ] [ 0 ] if*
438 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
439 self window :> window
442 text NSString -> class -> isKindOfClass: 0 = not [
445 text -> string CF>string str!
447 window world-focus :> gadget
448 gadget support-input-methods? [
450 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
452 str gadget user-input* drop
453 f gadget preedit-selection-mode?<<
455 str window user-input
460 METHOD: char hasMarkedText [
461 self window :> window
463 window world-focus :> gadget
464 gadget preedit? [ 1 ] [ 0 ] if
468 METHOD: NSRange markedRange [
469 self window :> window
471 window world-focus :> gadget
473 gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [
476 gadget preedit-start>> first gadget editor-line :> str
477 str gadget preedit-start>> second >utf16-index dup ! location
478 str gadget preedit-end>> second >utf16-index swap - ! length
480 ] [ NSNotFound 0 ] if
481 ] [ NSNotFound 0 ] if
485 METHOD: NSRange selectedRange [
486 self window :> window
488 window world-focus :> gadget
489 gadget support-input-methods? [
491 gadget preedit-start>> first gadget editor-line :> str
494 [ preedit-selected-start>> second ]
495 [ preedit-start>> second ] bi - >utf16-index ! location
496 str gadget preedit-selected-end>> second >utf16-index
497 str gadget preedit-selected-start>> second >utf16-index - ! length
498 ] [ gadget earlier-caret/mark second 0 ] if
504 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
505 replacementRange: NSRange replacementRange [
506 self window :> window
507 { } clone :> underlines!
509 window world-focus :> gadget
511 text NSString -> class -> isKindOfClass: 0 = not [
514 text -> string CF>string str!
515 gadget support-input-methods? [
516 gadget text selectedRange make-preedit-underlines underlines!
519 gadget support-input-methods? [
520 gadget str selectedRange update-marked-text
521 underlines gadget preedit-underlines<<
526 METHOD: void unmarkText [
527 self window :> window
529 window world-focus :> gadget
530 gadget support-input-methods? [
533 [ preedit-start>> second ]
534 [ preedit-end>> second ]
535 [ preedit-start>> first ] [ editor-line ]
537 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
538 gadget user-input* drop
540 f gadget preedit-selection-mode?<<
545 METHOD: id validAttributesForMarkedText [
546 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
549 METHOD: id attributedSubstringForProposedRange: NSRange range
550 actualRange: id actualRange [ f ] ;
552 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
554 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
555 actualRange: id actualRange [
558 actualRange NSRange memory>struct range!
561 self window :> window
563 window world-focus preedit? [
564 window world-focus :> gadget
565 gadget editor-caret first gadget editor-line :> str
566 str range location>> >codepoint-index :> start-pos
568 gadget editor-caret first start-pos 2array gadget loc>x dup :> xl
569 gadget caret-loc second gadget caret-dim second +
570 [ >fixnum ] bi@ 2array v+ { 1 -1 } v*
571 window handle>> window>> dup -> frame -> contentRectForFrameRect:
572 CGRect-top-left 2array v+
573 first2 0 gadget line-height >fixnum
580 METHOD: void updateFactorGadgetSize: id notification
582 self window :> window
584 self view-dim window dim<< yield
588 METHOD: void doCommandBySelector: SEL selector [ ] ;
590 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
592 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
593 dup dup add-resize-observer
596 METHOD: char isOpaque [ 0 ] ;
605 : sync-refresh-to-screen ( GLView -- )
606 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
607 CGLSetParameter drop ;
609 : <FactorView> ( dim pixel-format -- view )
610 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
612 : save-position ( world window -- )
613 -> frame CGRect-top-left 2array >>window-loc drop ;
615 <CLASS: FactorWindowDelegate < NSObject
617 METHOD: void windowDidMove: id notification
619 notification -> object -> contentView window
620 [ notification -> object save-position ] when*
623 METHOD: void windowDidBecomeKey: id notification
625 notification -> object -> contentView window
626 [ focus-world ] when*
629 METHOD: void windowDidResignKey: id notification
632 notification -> object -> contentView :> view
633 view window :> window
635 view -> isInFullScreenMode 0 =
636 [ window unfocus-world ] when
640 METHOD: char windowShouldClose: id notification [ 1 ] ;
642 METHOD: void windowWillClose: id notification
644 notification -> object -> contentView
645 [ window ungraft ] [ unregister-window ] bi
648 METHOD: void windowDidChangeBackingProperties: id notification
651 notification -> object dup SEL: backingScaleFactor
652 -> respondsToSelector: c-bool> [
653 { double { id SEL } } ?-> backingScaleFactor
655 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
656 [ 1.0 > retina? set-global ] bi
661 : install-window-delegate ( window -- )
662 FactorWindowDelegate install-delegate ;