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
366 event -> action utf8 alien>string validate-action
367 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
371 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
373 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
375 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
377 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
379 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
381 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
383 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
385 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
387 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
389 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
391 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
393 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
395 ! Multi-touch gestures
396 METHOD: void magnifyWithEvent: id event
400 { 1 [ zoom-in-action send-action$ ] }
401 { -1 [ zoom-out-action send-action$ ] }
406 METHOD: void swipeWithEvent: id event
410 { 1 [ left-action send-action$ ] }
411 { -1 [ right-action send-action$ ] }
415 { 1 [ up-action send-action$ ] }
416 { -1 [ down-action send-action$ ] }
424 METHOD: char acceptsFirstResponder [ 1 ] ;
427 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
429 ! We return either self or nil
431 world-focus sendType returnType
432 valid-service? [ self ] [ f ] if
436 METHOD: char writeSelectionToPasteboard: id pboard types: id types
438 NSStringPboardType types CF>string-array member? [
440 world-focus gadget-selection
441 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
446 METHOD: char readSelectionFromPasteboard: id pboard
448 self window :> window
450 pboard pasteboard-string
451 [ window user-input 1 ] [ 0 ] if*
456 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
457 self window :> window
460 text NSString -> class -> isKindOfClass: 0 = not [
463 text -> string CF>string str!
465 window world-focus :> gadget
467 gadget support-input-methods? [
468 replacementRange location>> NSNotFound = [
469 gadget editor-caret first
470 dup gadget editor-line
472 replacementRange location>> >codepoint-index
473 2array gadget set-caret
475 replacementRange [ location>> ] [ length>> ] bi +
477 2array gadget set-mark
481 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
483 str gadget user-input* drop
484 f gadget preedit-selection-mode?<<
486 str window user-input
492 METHOD: char hasMarkedText [
493 self window :> window
495 window world-focus :> gadget
497 gadget preedit? [ 1 ] [ 0 ] if
502 METHOD: NSRange markedRange [
503 self window :> window
505 window world-focus :> gadget
508 gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [
511 gadget preedit-start>> first gadget editor-line :> str
513 [ preedit-start>> second ] ! location
514 [ preedit-end>> second ]
515 bi [ str swap >utf16-index ] bi@ over - ! length
517 ] [ NSNotFound 0 ] if
518 ] [ NSNotFound 0 ] if
519 ] [ NSNotFound 0 ] if
523 METHOD: NSRange selectedRange [
524 self window :> window
526 window world-focus :> gadget
528 gadget support-input-methods? [
529 gadget editor-caret first gadget editor-line :> str
533 [ preedit-selected-start>> second ]
534 [ preedit-start>> second ]
535 bi - >utf16-index ! location
537 [ preedit-selected-end>> second ]
538 [ preedit-selected-start>> second ]
539 bi [ str swap >utf16-index ] bi@ - ! length
541 str gadget editor-caret second >utf16-index 0
549 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
550 replacementRange: NSRange replacementRange [
551 self window :> window
553 window world-focus :> gadget
555 { } clone :> underlines!
557 text NSString -> class -> isKindOfClass: 0 = not [
560 text -> string CF>string str!
561 gadget support-input-methods? [
562 gadget text selectedRange make-preedit-underlines underlines!
565 gadget support-input-methods? [
566 gadget str selectedRange replacementRange update-marked-text
567 underlines gadget preedit-underlines<<
573 METHOD: void unmarkText [
574 self window :> window
576 window world-focus :> gadget
578 gadget support-input-methods? [
581 [ preedit-start>> second ]
582 [ preedit-end>> second ]
583 [ preedit-start>> first ] [ editor-line ]
585 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
586 gadget user-input* drop
588 f gadget preedit-selection-mode?<<
594 METHOD: id validAttributesForMarkedText [
595 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
598 METHOD: id attributedSubstringForProposedRange: NSRange aRange
599 actualRange: id actualRange [ f ] ;
601 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
603 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
604 actualRange: NSRange actualRange [
605 self window :> window
607 window world-focus :> gadget
609 gadget support-input-methods? [
610 gadget editor-caret first gadget editor-line :> str
611 str aRange location>> >codepoint-index :> start-pos
612 gadget editor-caret first start-pos 2array gadget loc>x
613 gadget caret-loc second gadget caret-dim second +
614 2array ! character pos
615 gadget screen-loc v+ ! + gadget pos
617 window handle>> window>> dup -> frame -> contentRectForFrameRect:
618 CGRect-top-left 2array v+ ! + window pos
619 first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
627 METHOD: void updateFactorGadgetSize: id notification
629 self window :> window
631 self view-dim window dim<< yield
635 METHOD: void doCommandBySelector: SEL selector [ ] ;
637 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
639 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
640 dup dup add-resize-observer
643 METHOD: char isOpaque [ 0 ] ;
652 : sync-refresh-to-screen ( GLView -- )
653 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
654 CGLSetParameter drop ;
656 : <FactorView> ( dim pixel-format -- view )
657 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
659 : save-position ( world window -- )
660 -> frame CGRect-top-left 2array >>window-loc drop ;
662 <CLASS: FactorWindowDelegate < NSObject
664 METHOD: void windowDidMove: id notification
666 notification -> object -> contentView window
667 [ notification -> object save-position ] when*
670 METHOD: void windowDidBecomeKey: id notification
672 notification -> object -> contentView window
673 [ focus-world ] when*
676 METHOD: void windowDidResignKey: id notification
679 notification -> object -> contentView :> view
680 view window :> window
682 view -> isInFullScreenMode 0 =
683 [ window unfocus-world ] when
687 METHOD: char windowShouldClose: id notification [ 1 ] ;
689 METHOD: void windowWillClose: id notification
691 notification -> object -> contentView
692 [ window ungraft ] [ unregister-window ] bi
695 METHOD: void windowDidChangeBackingProperties: id notification
698 notification -> object dup SEL: backingScaleFactor
699 -> respondsToSelector: c-bool> [
700 { double { id SEL } } ?-> backingScaleFactor
702 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
703 [ 1.0 > retina? set-global ] bi
708 : install-window-delegate ( window -- )
709 FactorWindowDelegate install-delegate ;