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
20 : send-mouse-moved ( view event -- )
21 [ mouse-location ] [ drop window ] 2bi
22 [ move-hand fire-motion yield ] [ drop ] if* ;
25 : button ( event -- n )
26 ! Cocoa -> Factor UI button mapping
34 CONSTANT: NSAlphaShiftKeyMask 0x10000
35 CONSTANT: NSShiftKeyMask 0x20000
36 CONSTANT: NSControlKeyMask 0x40000
37 CONSTANT: NSAlternateKeyMask 0x80000
38 CONSTANT: NSCommandKeyMask 0x100000
39 CONSTANT: NSNumericPadKeyMask 0x200000
40 CONSTANT: NSHelpKeyMask 0x400000
41 CONSTANT: NSFunctionKeyMask 0x800000
44 { S+ $ NSShiftKeyMask }
45 { C+ $ NSControlKeyMask }
46 { A+ $ NSCommandKeyMask }
47 { M+ $ NSAlternateKeyMask }
77 : key-code ( event -- string ? )
78 dup -> keyCode key-codes at
79 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
81 : event-modifiers ( event -- modifiers )
82 -> modifierFlags modifiers modifier ;
84 : key-event>gesture ( event -- modifiers keycode action? )
85 [ event-modifiers ] [ key-code ] bi ;
87 : send-key-event ( view gesture -- )
88 swap window [ propagate-key-gesture ] [ drop ] if* ;
90 : interpret-key-event ( view event -- )
91 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
93 : send-key-down-event ( view event -- )
94 [ key-event>gesture <key-down> send-key-event ]
95 [ interpret-key-event ]
98 : send-key-up-event ( view event -- )
99 key-event>gesture <key-up> send-key-event ;
101 : mouse-event>gesture ( event -- modifiers button )
102 [ event-modifiers ] [ button ] bi ;
104 : send-button-down$ ( view event -- )
105 [ nip mouse-event>gesture <button-down> ]
109 [ send-button-down ] [ 2drop ] if* ;
111 : send-button-up$ ( view event -- )
112 [ nip mouse-event>gesture <button-up> ]
116 [ send-button-up ] [ 2drop ] if* ;
118 : send-scroll$ ( view event -- )
119 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
123 [ send-scroll ] [ 2drop ] if* ;
125 : send-action$ ( view event gesture -- )
126 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
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 : touchbar-commands ( -- commands/f gadget )
167 class-of "commands" word-prop
168 "touchbar" of dup [ commands>> ] when
172 TUPLE: send-touchbar-command target command ;
174 M: send-touchbar-command send-queued-gesture
175 [ target>> ] [ command>> ] bi invoke-command ;
177 : touchbar-invoke-command ( n -- )
178 [ touchbar-commands ] dip over [
180 send-touchbar-command queue-gesture notify-ui-thread
184 IMPORT: NSAttributedString
188 :: >codepoint-index ( str utf16-index -- codepoint-index )
189 0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
191 :: >utf16-index ( str codepoint-index -- utf16-index )
192 0 codepoint-index str subseq utf16n encode length 2 /i ;
194 :: earlier-caret/mark ( editor -- loc )
195 editor editor-caret :> caret
196 editor editor-mark :> mark
197 caret first mark first = [
198 caret second mark second < [ caret ] [ mark ] if
200 caret first mark first < [ caret ] [ mark ] if
203 :: make-preedit-underlines ( gadget text range -- underlines )
204 f gadget preedit-selection-mode?<<
205 { } clone :> underlines!
206 text -> length :> text-length
207 0 0 <NSRange> :> effective-range
208 text -> string CF>string :> str
209 str utf16n encode :> byte-16n
211 "NSMarkedClauseSegment" <NSString> :> segment-attr
212 [ effective-range [ location>> ] [ length>> ] bi + text-length < ] [
215 effective-range [ location>> ] [ length>> ] bi +
216 effective-range >c-ptr
217 -> attribute:atIndex:effectiveRange: drop
219 range location>> effective-range location>> = [
221 t gadget preedit-selection-mode?<<
224 effective-range [ location>> ] [ length>> ] bi over +
225 [ str swap >codepoint-index ] bi@ swap - :> len
226 cp-loc cp-loc len + dup cp-loc!
227 2array thickness 2array
230 underlines length 1 = [
231 underlines first first 2 2array 1array ! thickness: 2
232 ] [ underlines ] if ;
234 :: update-marked-text ( gadget str selectedRange replacementRange -- )
235 replacementRange location>> NSNotFound = [
236 gadget editor-caret first
237 dup gadget editor-line
239 replacementRange location>>
241 2array gadget set-caret
243 replacementRange [ location>> ] [ length>> ] bi +
245 2array gadget set-mark
247 gadget earlier-caret/mark dup
248 gadget preedit-start<<
249 0 1 2array v+ gadget preedit-end<<
253 gadget remove-preedit-text
256 gadget earlier-caret/mark dup
257 gadget preedit-start<<
258 0 str length 2array v+ gadget preedit-end<<
259 str gadget temp-im-input drop
260 gadget preedit-start>>
261 0 str selectedRange location>> >codepoint-index 2array v+
262 dup gadget preedit-selected-start<<
264 selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
265 [ str swap >codepoint-index ] bi@ -
267 dup gadget preedit-selected-end<<
268 dup gadget set-caret gadget set-mark
269 gadget preedit-start>> gadget preedit-end>> = [
270 gadget remove-preedit-info
275 <CLASS: FactorView < NSOpenGLView
276 COCOA-PROTOCOL: NSTextInputClient
278 METHOD: void prepareOpenGL [
280 self SEL: setWantsBestResolutionOpenGLSurface:
281 -> respondsToSelector: c-bool> [
283 self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
285 self { double { id SEL } } ?-> backingScaleFactor
288 gl-scale-factor set-global t retina? set-global
289 cached-lines get-global clear-assoc
296 METHOD: void reshape [
297 self window :> window
299 self view-dim window dim<<
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 [
335 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
337 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
339 METHOD: void mouseExited: id event [ forget-rollover ] ;
341 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
343 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
345 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
347 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
349 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
351 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
353 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
355 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
357 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
359 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
361 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
363 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
365 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
367 METHOD: char validateUserInterfaceItem: id event
369 self window :> window
371 window world-focus :> gadget
373 gadget preedit? not [
374 window event -> action utf8 alien>string validate-action
375 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
381 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
383 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
385 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
387 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
389 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
391 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
393 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
395 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
397 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
399 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
401 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
403 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
405 ! Multi-touch gestures
406 METHOD: void magnifyWithEvent: id event
410 { 1 [ zoom-in-action send-action$ ] }
411 { -1 [ zoom-out-action send-action$ ] }
416 METHOD: void swipeWithEvent: id event
420 { 1 [ left-action send-action$ ] }
421 { -1 [ right-action send-action$ ] }
425 { 1 [ up-action send-action$ ] }
426 { -1 [ down-action send-action$ ] }
434 METHOD: char acceptsFirstResponder [ 1 ] ;
437 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
439 ! We return either self or nil
441 world-focus sendType returnType
442 valid-service? [ self ] [ f ] if
446 METHOD: char writeSelectionToPasteboard: id pboard types: id types
448 NSStringPboardType types CF>string-array member? [
450 world-focus gadget-selection
451 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
456 METHOD: char readSelectionFromPasteboard: id pboard
458 self window :> window
460 pboard pasteboard-string
461 [ window user-input 1 ] [ 0 ] if*
466 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
467 self window :> window
470 text NSString -> class -> isKindOfClass: 0 = not [
473 text -> string CF>string str!
475 window world-focus :> gadget
477 gadget support-input-methods? [
478 replacementRange location>> NSNotFound = [
479 gadget editor-caret first
480 dup gadget editor-line
482 replacementRange location>> >codepoint-index
483 2array gadget set-caret
485 replacementRange [ location>> ] [ length>> ] bi +
487 2array gadget set-mark
491 gadget remove-preedit-text
492 gadget remove-preedit-info
493 str gadget user-input* drop
494 f gadget preedit-selection-mode?<<
496 str window user-input
499 str window user-input
505 METHOD: char hasMarkedText [
506 self window :> window
508 window world-focus :> gadget
510 gadget preedit? 1 0 ?
515 METHOD: NSRange markedRange [
516 self window :> window
518 window world-focus :> gadget
521 gadget preedit-start>> second
522 gadget preedit-end>> second < [
523 gadget preedit-start>> first gadget editor-line :> str
524 gadget preedit-start>> second ! location
525 gadget preedit-end>> second
526 [ str swap >utf16-index ] bi@ over - ! length
527 ] [ NSNotFound 0 ] if
528 ] [ NSNotFound 0 ] if
529 ] [ NSNotFound 0 ] if
530 ] [ NSNotFound 0 ] if
534 METHOD: NSRange selectedRange [
535 self window :> window
537 window world-focus :> gadget
539 gadget support-input-methods? [
540 gadget editor-caret first gadget editor-line :> str
543 gadget preedit-selected-start>> second
544 gadget preedit-start>> second
545 - >utf16-index ! location
546 gadget preedit-selected-end>> second
547 gadget preedit-selected-start>> second
548 [ str swap >utf16-index ] bi@ - ! length
550 str gadget editor-caret second >utf16-index 0
558 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
559 replacementRange: NSRange replacementRange [
560 self window :> window
562 window world-focus :> gadget
564 { } clone :> underlines!
566 text NSString -> class -> isKindOfClass: 0 = not [
569 text -> string CF>string str!
570 gadget support-input-methods? [
571 gadget text selectedRange make-preedit-underlines underlines!
574 gadget support-input-methods? [
575 gadget str selectedRange replacementRange update-marked-text
576 underlines gadget preedit-underlines<<
582 METHOD: void unmarkText [
583 self window :> window
585 window world-focus :> gadget
587 gadget support-input-methods? [
590 [ preedit-start>> second ]
591 [ preedit-end>> second ]
592 [ preedit-start>> first ]
595 gadget remove-preedit-text
596 gadget remove-preedit-info
597 gadget user-input* drop
599 f gadget preedit-selection-mode?<<
605 METHOD: id validAttributesForMarkedText [
606 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
609 METHOD: id attributedSubstringForProposedRange: NSRange aRange
610 actualRange: id actualRange [ f ] ;
612 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
614 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
615 actualRange: NSRange actualRange [
616 self window :> window
618 window world-focus :> gadget
620 gadget support-input-methods? [
621 gadget editor-caret first gadget editor-line :> str
622 str aRange location>> >codepoint-index :> start-pos
623 gadget editor-caret first start-pos 2array gadget loc>x
624 gadget caret-loc second gadget caret-dim second +
625 2array ! character pos
626 gadget screen-loc v+ ! + gadget pos
628 window handle>> window>> dup -> frame -> contentRectForFrameRect:
629 CGRect-top-left 2array v+ ! + window pos
630 first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
637 METHOD: void doCommandBySelector: SEL selector [ ] ;
640 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
642 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
645 METHOD: char isOpaque [ 0 ] ;
654 : sync-refresh-to-screen ( GLView -- )
655 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
656 CGLSetParameter drop ;
658 : <FactorView> ( dim pixel-format -- view )
659 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
661 : save-position ( world window -- )
662 -> frame CGRect-top-left 2array >>window-loc drop ;
664 <CLASS: FactorWindowDelegate < NSObject
666 METHOD: void windowDidMove: id notification
668 notification -> object -> contentView window
669 [ notification -> object save-position ] when*
672 METHOD: void windowDidBecomeKey: id notification
674 notification -> object -> contentView window
675 [ focus-world ] when*
678 METHOD: void windowDidResignKey: id notification
681 notification -> object -> contentView :> view
682 view window :> window
684 view -> isInFullScreenMode 0 =
685 [ window unfocus-world ] when
689 METHOD: char windowShouldClose: id notification [ 1 ] ;
691 METHOD: void windowWillClose: id notification
693 notification -> object -> contentView
694 [ window ungraft ] [ unregister-window ] bi
697 METHOD: void windowDidChangeBackingProperties: id notification
700 notification -> object dup SEL: backingScaleFactor
701 -> respondsToSelector: c-bool> [
702 { double { id SEL } } ?-> backingScaleFactor
704 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
705 [ 1.0 > retina? set-global ] bi
710 : install-window-delegate ( window -- )
711 FactorWindowDelegate install-delegate ;