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 : add-resize-observer ( observer object -- )
130 "updateFactorGadgetSize:"
131 "NSViewFrameDidChangeNotification" <NSString>
134 : string-or-nil? ( NSString -- ? )
135 [ CF>string NSStringPboardType = ] [ t ] if* ;
137 : valid-service? ( gadget send-type return-type -- ? )
138 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
139 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
141 : NSRect>rect ( NSRect world -- rect )
142 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
143 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
146 : rect>NSRect ( rect world -- NSRect )
147 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
148 [ drop dim>> first2 ]
151 CONSTANT: selector>action H{
152 { "undo:" undo-action }
153 { "redo:" redo-action }
154 { "cut:" cut-action }
155 { "copy:" copy-action }
156 { "paste:" paste-action }
157 { "delete:" delete-action }
158 { "selectAll:" select-all-action }
159 { "newDocument:" new-action }
160 { "openDocument:" open-action }
161 { "saveDocument:" save-action }
162 { "saveDocumentAs:" save-as-action }
163 { "revertDocumentToSaved:" revert-action }
166 : validate-action ( world selector -- ? validated? )
168 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
170 : touchbar-commands ( -- commands/f gadget )
173 class-of "commands" word-prop
174 "touchbar" of dup [ commands>> ] when
178 TUPLE: send-touchbar-command target command ;
180 M: send-touchbar-command send-queued-gesture
181 [ target>> ] [ command>> ] bi invoke-command ;
183 : touchbar-invoke-command ( n -- )
184 [ touchbar-commands ] dip over [
186 send-touchbar-command queue-gesture notify-ui-thread
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 /i ;
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 = [
242 gadget editor-caret first
243 dup gadget editor-line
245 replacementRange location>>
247 2array gadget set-caret
249 replacementRange [ location>> ] [ length>> ] bi +
251 2array gadget set-mark
253 gadget earlier-caret/mark dup
254 gadget preedit-start<<
255 0 1 2array v+ gadget preedit-end<<
259 gadget remove-preedit-text
262 gadget earlier-caret/mark dup
263 gadget preedit-start<<
264 0 str length 2array v+ gadget preedit-end<<
265 str gadget temp-im-input drop
266 gadget preedit-start>>
267 0 str selectedRange location>> >codepoint-index 2array v+
268 dup gadget preedit-selected-start<<
270 selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
271 [ str swap >codepoint-index ] bi@ -
273 dup gadget preedit-selected-end<<
274 dup gadget set-caret gadget set-mark
275 gadget preedit-start>> gadget preedit-end>> = [
276 gadget remove-preedit-info
281 <CLASS: FactorView < NSOpenGLView
282 COCOA-PROTOCOL: NSTextInputClient
284 METHOD: void prepareOpenGL [
286 self SEL: setWantsBestResolutionOpenGLSurface:
287 -> respondsToSelector: c-bool> [
289 self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
291 self { double { id SEL } } ?-> backingScaleFactor
294 gl-scale-factor set-global t retina? set-global
295 cached-lines get-global clear-assoc
303 METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
304 METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
305 METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
306 METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
307 METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
308 METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
309 METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
310 METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
312 METHOD: id makeTouchBar [
313 touchbar-commands drop [
314 length 8 min <iota> [ number>string ] map
315 ] [ { } ] if* self make-touchbar
318 METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
319 touchbar-commands drop [
320 [ self string CF>string dup string>number ] dip nth
321 second name>> "com-" ?head drop over
322 "touchBarCommand" prepend make-NSTouchBar-button
327 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
330 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
332 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
334 METHOD: void mouseExited: id event [ forget-rollover ] ;
336 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
338 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
340 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
342 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
344 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
346 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
348 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
350 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
352 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
354 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
356 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
358 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
360 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
362 METHOD: char validateUserInterfaceItem: id event
364 self window :> window
366 window world-focus :> gadget
368 gadget preedit? not [
369 window event -> action utf8 alien>string validate-action
370 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
376 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
378 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
380 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
382 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
384 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
386 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
388 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
390 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
392 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
394 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
396 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
398 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
400 ! Multi-touch gestures
401 METHOD: void magnifyWithEvent: id event
405 { 1 [ zoom-in-action send-action$ ] }
406 { -1 [ zoom-out-action send-action$ ] }
411 METHOD: void swipeWithEvent: id event
415 { 1 [ left-action send-action$ ] }
416 { -1 [ right-action send-action$ ] }
420 { 1 [ up-action send-action$ ] }
421 { -1 [ down-action send-action$ ] }
429 METHOD: char acceptsFirstResponder [ 1 ] ;
432 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
434 ! We return either self or nil
436 world-focus sendType returnType
437 valid-service? [ self ] [ f ] if
441 METHOD: char writeSelectionToPasteboard: id pboard types: id types
443 NSStringPboardType types CF>string-array member? [
445 world-focus gadget-selection
446 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
451 METHOD: char readSelectionFromPasteboard: id pboard
453 self window :> window
455 pboard pasteboard-string
456 [ window user-input 1 ] [ 0 ] if*
461 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
462 self window :> window
465 text NSString -> class -> isKindOfClass: 0 = not [
468 text -> string CF>string str!
470 window world-focus :> gadget
472 gadget support-input-methods? [
473 replacementRange location>> NSNotFound = [
474 gadget editor-caret first
475 dup gadget editor-line
477 replacementRange location>> >codepoint-index
478 2array gadget set-caret
480 replacementRange [ location>> ] [ length>> ] bi +
482 2array gadget set-mark
486 gadget remove-preedit-text
487 gadget remove-preedit-info
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 ?
510 METHOD: NSRange markedRange [
511 self window :> window
513 window world-focus :> gadget
516 gadget preedit-start>> second
517 gadget preedit-end>> second < [
518 gadget preedit-start>> first gadget editor-line :> str
519 gadget preedit-start>> second ! location
520 gadget preedit-end>> second
521 [ str swap >utf16-index ] bi@ over - ! length
522 ] [ NSNotFound 0 ] if
523 ] [ NSNotFound 0 ] if
524 ] [ NSNotFound 0 ] if
525 ] [ NSNotFound 0 ] if
529 METHOD: NSRange selectedRange [
530 self window :> window
532 window world-focus :> gadget
534 gadget support-input-methods? [
535 gadget editor-caret first gadget editor-line :> str
538 gadget preedit-selected-start>> second
539 gadget preedit-start>> second
540 - >utf16-index ! location
541 gadget preedit-selected-end>> second
542 gadget preedit-selected-start>> second
543 [ str swap >utf16-index ] bi@ - ! length
545 str gadget editor-caret second >utf16-index 0
553 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
554 replacementRange: NSRange replacementRange [
555 self window :> window
557 window world-focus :> gadget
559 { } clone :> underlines!
561 text NSString -> class -> isKindOfClass: 0 = not [
564 text -> string CF>string str!
565 gadget support-input-methods? [
566 gadget text selectedRange make-preedit-underlines underlines!
569 gadget support-input-methods? [
570 gadget str selectedRange replacementRange update-marked-text
571 underlines gadget preedit-underlines<<
577 METHOD: void unmarkText [
578 self window :> window
580 window world-focus :> gadget
582 gadget support-input-methods? [
585 [ preedit-start>> second ]
586 [ preedit-end>> second ]
587 [ preedit-start>> first ]
590 gadget remove-preedit-text
591 gadget remove-preedit-info
592 gadget user-input* drop
594 f gadget preedit-selection-mode?<<
600 METHOD: id validAttributesForMarkedText [
601 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
604 METHOD: id attributedSubstringForProposedRange: NSRange aRange
605 actualRange: id actualRange [ f ] ;
607 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
609 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
610 actualRange: NSRange actualRange [
611 self window :> window
613 window world-focus :> gadget
615 gadget support-input-methods? [
616 gadget editor-caret first gadget editor-line :> str
617 str aRange location>> >codepoint-index :> start-pos
618 gadget editor-caret first start-pos 2array gadget loc>x
619 gadget caret-loc second gadget caret-dim second +
620 2array ! character pos
621 gadget screen-loc v+ ! + gadget pos
623 window handle>> window>> dup -> frame -> contentRectForFrameRect:
624 CGRect-top-left 2array v+ ! + window pos
625 first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
632 METHOD: void doCommandBySelector: SEL selector [ ] ;
635 METHOD: void updateFactorGadgetSize: id notification
637 self window :> window
639 self view-dim window dim<< yield
643 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
645 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
646 dup dup add-resize-observer
649 METHOD: char isOpaque [ 0 ] ;
658 : sync-refresh-to-screen ( GLView -- )
659 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
660 CGLSetParameter drop ;
662 : <FactorView> ( dim pixel-format -- view )
663 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
665 : save-position ( world window -- )
666 -> frame CGRect-top-left 2array >>window-loc drop ;
668 <CLASS: FactorWindowDelegate < NSObject
670 METHOD: void windowDidMove: id notification
672 notification -> object -> contentView window
673 [ notification -> object save-position ] when*
676 METHOD: void windowDidBecomeKey: id notification
678 notification -> object -> contentView window
679 [ focus-world ] when*
682 METHOD: void windowDidResignKey: id notification
685 notification -> object -> contentView :> view
686 view window :> window
688 view -> isInFullScreenMode 0 =
689 [ window unfocus-world ] when
693 METHOD: char windowShouldClose: id notification [ 1 ] ;
695 METHOD: void windowWillClose: id notification
697 notification -> object -> contentView
698 [ window ungraft ] [ unregister-window ] bi
701 METHOD: void windowDidChangeBackingProperties: id notification
704 notification -> object dup SEL: backingScaleFactor
705 -> respondsToSelector: c-bool> [
706 { double { id SEL } } ?-> backingScaleFactor
708 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
709 [ 1.0 > retina? set-global ] bi
714 : install-window-delegate ( window -- )
715 FactorWindowDelegate install-delegate ;