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 CONSTANT: NSNotFound 9223372036854775807 inline
192 IMPORT: NSAttributedString
196 :: >codepoint-index ( str utf16-index -- codepoint-index )
197 0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
199 :: >utf16-index ( str codepoint-index -- utf16-index )
200 0 codepoint-index str subseq utf16n encode length 2 / >integer ;
202 :: earlier-caret/mark ( editor -- loc )
203 editor editor-caret :> caret
204 editor editor-mark :> mark
205 caret first mark first = [
206 caret second mark second < [ caret ] [ mark ] if
208 caret first mark first < [ caret ] [ mark ] if
211 :: make-preedit-underlines ( gadget text range -- underlines )
212 f gadget preedit-selection-mode?<<
213 { } clone :> underlines!
214 text -> length :> text-length
215 0 0 <NSRange> :> effective-range
216 text -> string CF>string :> str
217 str utf16n encode :> byte-16n
219 "NSMarkedClauseSegment" <NSString> :> segment-attr
220 [ effective-range [ location>> ] [ length>> ] bi + text-length < ] [
223 effective-range [ location>> ] [ length>> ] bi +
224 effective-range >c-ptr
225 -> attribute:atIndex:effectiveRange: drop
227 range location>> effective-range location>> = [
229 t gadget preedit-selection-mode?<<
232 effective-range [ location>> ] [ length>> ] bi over +
233 [ str swap >codepoint-index ] bi@ swap - :> len
234 cp-loc cp-loc len + dup cp-loc!
235 2array thickness 2array
238 underlines length 1 = [
239 underlines first first 2 2array 1array ! thickness: 2
240 ] [ underlines ] if ;
242 :: update-marked-text ( gadget str selectedRange replacementRange -- )
243 replacementRange location>> NSNotFound = not ! [
244 replacementRange length>> NSNotFound = not and [ ! erase this line
245 gadget editor-caret first
246 dup gadget editor-line
248 replacementRange length>> ! location>>
250 2array gadget set-caret
252 replacementRange length>> 1 + ! [ location>> ] [ length>> ] bi +
254 2array gadget set-mark
256 gadget earlier-caret/mark dup
257 gadget preedit-start<<
258 0 1 2array v+ gadget preedit-end<<
262 gadget remove-preedit-text
265 gadget earlier-caret/mark dup
266 gadget preedit-start<<
267 0 str length 2array v+ gadget preedit-end<<
268 str gadget temp-im-input drop
269 gadget preedit-start>>
270 0 str selectedRange location>> >codepoint-index 2array v+
271 dup gadget preedit-selected-start<<
273 selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
274 [ str swap >codepoint-index ] bi@ -
276 dup gadget preedit-selected-end<<
277 dup gadget set-caret gadget set-mark
278 gadget preedit-start>> gadget preedit-end>> = [
279 gadget remove-preedit-info
284 <CLASS: FactorView < NSOpenGLView
285 COCOA-PROTOCOL: NSTextInputClient
287 METHOD: void prepareOpenGL [
289 self SEL: setWantsBestResolutionOpenGLSurface:
290 -> respondsToSelector: c-bool> [
292 self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
294 self { double { id SEL } } ?-> backingScaleFactor
297 gl-scale-factor set-global t retina? set-global
298 cached-lines get-global clear-assoc
306 METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
307 METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
308 METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
309 METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
310 METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
311 METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
312 METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
313 METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
315 METHOD: id makeTouchBar [
316 touchbar-commands drop [
317 length 8 min <iota> [ number>string ] map
318 ] [ { } ] if* self make-touchbar
321 METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
322 touchbar-commands drop [
323 [ self string CF>string dup string>number ] dip nth
324 second name>> "com-" ?head drop over
325 "touchBarCommand" prepend make-NSTouchBar-button
330 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
333 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
335 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
337 METHOD: void mouseExited: id event [ forget-rollover ] ;
339 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
341 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
343 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
345 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
347 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
349 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
351 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
353 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
355 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
357 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
359 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
361 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
363 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
365 METHOD: char validateUserInterfaceItem: id event
368 event -> action utf8 alien>string validate-action
369 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
373 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
375 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
377 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
379 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
381 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
383 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
385 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
387 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
389 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
391 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
393 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
395 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
397 ! Multi-touch gestures
398 METHOD: void magnifyWithEvent: id event
402 { 1 [ zoom-in-action send-action$ ] }
403 { -1 [ zoom-out-action send-action$ ] }
408 METHOD: void swipeWithEvent: id event
412 { 1 [ left-action send-action$ ] }
413 { -1 [ right-action send-action$ ] }
417 { 1 [ up-action send-action$ ] }
418 { -1 [ down-action send-action$ ] }
426 METHOD: char acceptsFirstResponder [ 1 ] ;
429 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
431 ! We return either self or nil
433 world-focus sendType returnType
434 valid-service? [ self ] [ f ] if
438 METHOD: char writeSelectionToPasteboard: id pboard types: id types
440 NSStringPboardType types CF>string-array member? [
442 world-focus gadget-selection
443 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
448 METHOD: char readSelectionFromPasteboard: id pboard
450 self window :> window
452 pboard pasteboard-string
453 [ window user-input 1 ] [ 0 ] if*
458 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
459 self window :> window
462 text NSString -> class -> isKindOfClass: 0 = not [
465 text -> string CF>string str!
467 window world-focus :> gadget
469 gadget support-input-methods? [
470 replacementRange location>> NSNotFound = [
471 gadget editor-caret first
472 dup gadget editor-line
474 replacementRange location>> >codepoint-index
475 2array gadget set-caret
477 replacementRange [ location>> ] [ length>> ] bi +
479 2array gadget set-mark
483 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
485 str gadget user-input* drop
486 f gadget preedit-selection-mode?<<
488 str window user-input
494 METHOD: char hasMarkedText [
495 self window :> window
497 window world-focus :> gadget
499 gadget preedit? [ 1 ] [ 0 ] if
504 METHOD: NSRange markedRange [
505 self window :> window
507 window world-focus :> gadget
510 gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [
513 gadget preedit-start>> first gadget editor-line :> str
515 [ preedit-start>> second ] ! location
516 [ preedit-end>> second ]
517 bi [ str swap >utf16-index ] bi@ over - ! length
519 ] [ NSNotFound 0 ] if
520 ] [ NSNotFound 0 ] if
521 ] [ NSNotFound 0 ] if
525 METHOD: NSRange selectedRange [
526 self window :> window
528 window world-focus :> gadget
530 gadget support-input-methods? [
531 gadget editor-caret first gadget editor-line :> str
535 [ preedit-selected-start>> second ]
536 [ preedit-start>> second ]
537 bi - >utf16-index ! location
539 [ preedit-selected-end>> second ]
540 [ preedit-selected-start>> second ]
541 bi [ str swap >utf16-index ] bi@ - ! length
543 str gadget editor-caret second >utf16-index 0
551 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
552 replacementRange: NSRange replacementRange [
553 self window :> window
555 window world-focus :> gadget
557 { } clone :> underlines!
559 text NSString -> class -> isKindOfClass: 0 = not [
562 text -> string CF>string str!
563 gadget support-input-methods? [
564 gadget text selectedRange make-preedit-underlines underlines!
567 gadget support-input-methods? [
568 gadget str selectedRange replacementRange update-marked-text
569 underlines gadget preedit-underlines<<
575 METHOD: void unmarkText [
576 self window :> window
578 window world-focus :> gadget
580 gadget support-input-methods? [
583 [ preedit-start>> second ]
584 [ preedit-end>> second ]
585 [ preedit-start>> first ] [ editor-line ]
587 gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
588 gadget user-input* drop
590 f gadget preedit-selection-mode?<<
596 METHOD: id validAttributesForMarkedText [
597 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
600 METHOD: id attributedSubstringForProposedRange: NSRange aRange
601 actualRange: id actualRange [ f ] ;
603 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
605 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
606 actualRange: NSRange actualRange [
607 self window :> window
609 window world-focus :> gadget
611 gadget support-input-methods? [
612 gadget editor-caret first gadget editor-line :> str
613 str aRange location>> >codepoint-index :> start-pos
614 gadget editor-caret first start-pos 2array gadget loc>x
615 gadget caret-loc second gadget caret-dim second +
616 2array ! character pos
617 gadget screen-loc v+ ! + gadget pos
619 window handle>> window>> dup -> frame -> contentRectForFrameRect:
620 CGRect-top-left 2array v+ ! + window pos
621 first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
629 METHOD: void updateFactorGadgetSize: id notification
631 self window :> window
633 self view-dim window dim<< yield
637 METHOD: void doCommandBySelector: SEL selector [ ] ;
639 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
641 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
642 dup dup add-resize-observer
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 ;