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 selectedRange replacementRange -- )
241 replacementRange location>> NSNotFound = not ! [
242 replacementRange length>> NSNotFound = not and [ ! erase this line
243 gadget editor-caret first
244 dup gadget editor-line
246 replacementRange length>> ! location>>
248 2array gadget set-caret
250 replacementRange length>> 1 + ! [ location>> ] [ length>> ] bi +
252 2array gadget set-mark
254 gadget earlier-caret/mark dup
255 gadget preedit-start<<
256 0 1 2array v+ gadget preedit-end<<
260 gadget remove-preedit-text
263 gadget earlier-caret/mark dup
264 gadget preedit-start<<
265 0 str length 2array v+ gadget preedit-end<<
266 str gadget temp-im-input drop
267 gadget preedit-start>>
268 0 str selectedRange location>> >codepoint-index 2array v+
269 dup gadget preedit-selected-start<<
271 selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
272 [ str swap >codepoint-index ] bi@ -
274 dup gadget preedit-selected-end<<
275 dup gadget set-caret gadget set-mark
276 gadget preedit-start>> gadget preedit-end>> = [
277 gadget remove-preedit-info
282 <CLASS: FactorView < NSOpenGLView
283 COCOA-PROTOCOL: NSTextInputClient
285 METHOD: void prepareOpenGL [
287 self SEL: setWantsBestResolutionOpenGLSurface:
288 -> respondsToSelector: c-bool> [
290 self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
292 self { double { id SEL } } ?-> backingScaleFactor
295 gl-scale-factor set-global t retina? set-global
296 cached-lines get-global clear-assoc
304 METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
305 METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
306 METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
307 METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
308 METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
309 METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
310 METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
311 METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
313 METHOD: id makeTouchBar [
314 touchbar-commands drop [
315 length 8 min <iota> [ number>string ] map
316 ] [ { } ] if* self make-touchbar
319 METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
320 touchbar-commands drop [
321 [ self string CF>string dup string>number ] dip nth
322 second name>> "com-" ?head drop over
323 "touchBarCommand" prepend make-NSTouchBar-button
328 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
331 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
333 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
335 METHOD: void mouseExited: id event [ forget-rollover ] ;
337 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
339 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
341 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
343 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
345 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
347 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
349 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
351 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
353 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
355 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
357 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
359 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
361 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
363 METHOD: char validateUserInterfaceItem: id event
365 self window :> window
367 window world-focus :> gadget
369 gadget preedit? not [
370 window event -> action utf8 alien>string validate-action
371 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
377 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
379 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
381 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
383 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
385 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
387 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
389 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
391 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
393 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
395 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
397 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
399 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
401 ! Multi-touch gestures
402 METHOD: void magnifyWithEvent: id event
406 { 1 [ zoom-in-action send-action$ ] }
407 { -1 [ zoom-out-action send-action$ ] }
412 METHOD: void swipeWithEvent: id event
416 { 1 [ left-action send-action$ ] }
417 { -1 [ right-action send-action$ ] }
421 { 1 [ up-action send-action$ ] }
422 { -1 [ down-action send-action$ ] }
430 METHOD: char acceptsFirstResponder [ 1 ] ;
433 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
435 ! We return either self or nil
437 world-focus sendType returnType
438 valid-service? [ self ] [ f ] if
442 METHOD: char writeSelectionToPasteboard: id pboard types: id types
444 NSStringPboardType types CF>string-array member? [
446 world-focus gadget-selection
447 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
452 METHOD: char readSelectionFromPasteboard: id pboard
454 self window :> window
456 pboard pasteboard-string
457 [ window user-input 1 ] [ 0 ] if*
462 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
463 self window :> window
466 text NSString -> class -> isKindOfClass: 0 = not [
469 text -> string CF>string str!
471 window world-focus :> gadget
473 gadget support-input-methods? [
474 replacementRange location>> NSNotFound = [
475 gadget editor-caret first
476 dup gadget editor-line
478 replacementRange location>> >codepoint-index
479 2array gadget set-caret
481 replacementRange [ location>> ] [ length>> ] bi +
483 2array gadget set-mark
487 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
488 str gadget user-input* drop
489 f gadget preedit-selection-mode?<<
491 str window user-input
494 str window user-input
500 METHOD: char hasMarkedText [
501 self window :> window
503 window world-focus :> gadget
505 gadget preedit? [ 1 ] [ 0 ] if
510 METHOD: NSRange markedRange [
511 self window :> window
513 window world-focus :> gadget
516 gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [
519 gadget preedit-start>> first gadget editor-line :> str
521 [ preedit-start>> second ] ! location
522 [ preedit-end>> second ]
523 bi [ str swap >utf16-index ] bi@ over - ! length
525 ] [ NSNotFound 0 ] if
526 ] [ NSNotFound 0 ] if
527 ] [ NSNotFound 0 ] if
531 METHOD: NSRange selectedRange [
532 self window :> window
534 window world-focus :> gadget
536 gadget support-input-methods? [
537 gadget editor-caret first gadget editor-line :> str
541 [ preedit-selected-start>> second ]
542 [ preedit-start>> second ]
543 bi - >utf16-index ! location
545 [ preedit-selected-end>> second ]
546 [ preedit-selected-start>> second ]
547 bi [ str swap >utf16-index ] bi@ - ! length
549 str gadget editor-caret second >utf16-index 0
557 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
558 replacementRange: NSRange replacementRange [
559 self window :> window
561 window world-focus :> gadget
563 { } clone :> underlines!
565 text NSString -> class -> isKindOfClass: 0 = not [
568 text -> string CF>string str!
569 gadget support-input-methods? [
570 gadget text selectedRange make-preedit-underlines underlines!
573 gadget support-input-methods? [
574 gadget str selectedRange replacementRange update-marked-text
575 underlines gadget preedit-underlines<<
581 METHOD: void unmarkText [
582 self window :> window
584 window world-focus :> gadget
586 gadget support-input-methods? [
589 [ preedit-start>> second ]
590 [ preedit-end>> second ]
591 [ preedit-start>> first ] [ editor-line ]
593 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
594 gadget user-input* drop
596 f gadget preedit-selection-mode?<<
602 METHOD: id validAttributesForMarkedText [
603 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
606 METHOD: id attributedSubstringForProposedRange: NSRange aRange
607 actualRange: id actualRange [ f ] ;
609 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
611 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
612 actualRange: NSRange actualRange [
613 self window :> window
615 window world-focus :> gadget
617 gadget support-input-methods? [
618 gadget editor-caret first gadget editor-line :> str
619 str aRange location>> >codepoint-index :> start-pos
620 gadget editor-caret first start-pos 2array gadget loc>x
621 gadget caret-loc second gadget caret-dim second +
622 2array ! character pos
623 gadget screen-loc v+ ! + gadget pos
625 window handle>> window>> dup -> frame -> contentRectForFrameRect:
626 CGRect-top-left 2array v+ ! + window pos
627 first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
634 METHOD: void doCommandBySelector: SEL selector [ ] ;
637 METHOD: void updateFactorGadgetSize: id notification
639 self window :> window
641 self view-dim window dim<< yield
645 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
647 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
648 dup dup add-resize-observer
651 METHOD: char isOpaque [ 0 ] ;
660 : sync-refresh-to-screen ( GLView -- )
661 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
662 CGLSetParameter drop ;
664 : <FactorView> ( dim pixel-format -- view )
665 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
667 : save-position ( world window -- )
668 -> frame CGRect-top-left 2array >>window-loc drop ;
670 <CLASS: FactorWindowDelegate < NSObject
672 METHOD: void windowDidMove: id notification
674 notification -> object -> contentView window
675 [ notification -> object save-position ] when*
678 METHOD: void windowDidBecomeKey: id notification
680 notification -> object -> contentView window
681 [ focus-world ] when*
684 METHOD: void windowDidResignKey: id notification
687 notification -> object -> contentView :> view
688 view window :> window
690 view -> isInFullScreenMode 0 =
691 [ window unfocus-world ] when
695 METHOD: char windowShouldClose: id notification [ 1 ] ;
697 METHOD: void windowWillClose: id notification
699 notification -> object -> contentView
700 [ window ungraft ] [ unregister-window ] bi
703 METHOD: void windowDidChangeBackingProperties: id notification
706 notification -> object dup SEL: backingScaleFactor
707 -> respondsToSelector: c-bool> [
708 { double { id SEL } } ?-> backingScaleFactor
710 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
711 [ 1.0 > retina? set-global ] bi
716 : install-window-delegate ( window -- )
717 FactorWindowDelegate install-delegate ;