]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/views/views.factor
ui.backend.cocoa.views: remove listener touchbar support (temporarily).
[factor.git] / basis / ui / backend / cocoa / views / views.factor
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 cocoa cocoa.application cocoa.classes
5 cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.types
6 cocoa.views combinators core-foundation.strings core-graphics
7 core-graphics.types core-text io.encodings.utf8 kernel literals
8 locals math math.rectangles namespaces opengl sequences threads
9 ui.gadgets ui.gadgets.private ui.gadgets.worlds ui.gestures
10 ui.private ;
11 IN: ui.backend.cocoa.views
12
13 : send-mouse-moved ( view event -- )
14     [ mouse-location ] [ drop window ] 2bi
15     [ move-hand fire-motion yield ] [ drop ] if* ;
16
17 ! Issue #1453
18 : button ( event -- n )
19     ! Cocoa -> Factor UI button mapping
20     -> buttonNumber {
21         { 0 [ 1 ] }
22         { 1 [ 3 ] }
23         { 2 [ 2 ] }
24         [ ]
25     } case ;
26
27 CONSTANT: NSAlphaShiftKeyMask 0x10000
28 CONSTANT: NSShiftKeyMask      0x20000
29 CONSTANT: NSControlKeyMask    0x40000
30 CONSTANT: NSAlternateKeyMask  0x80000
31 CONSTANT: NSCommandKeyMask    0x100000
32 CONSTANT: NSNumericPadKeyMask 0x200000
33 CONSTANT: NSHelpKeyMask       0x400000
34 CONSTANT: NSFunctionKeyMask   0x800000
35
36 CONSTANT: modifiers {
37         { S+ $ NSShiftKeyMask }
38         { C+ $ NSControlKeyMask }
39         { A+ $ NSCommandKeyMask }
40         { M+ $ NSAlternateKeyMask }
41     }
42
43 CONSTANT: key-codes
44     H{
45         { 71 "CLEAR" }
46         { 36 "RET" }
47         { 76 "ENTER" }
48         { 53 "ESC" }
49         { 48 "TAB" }
50         { 51 "BACKSPACE" }
51         { 115 "HOME" }
52         { 117 "DELETE" }
53         { 119 "END" }
54         { 122 "F1" }
55         { 120 "F2" }
56         { 99 "F3" }
57         { 118 "F4" }
58         { 96 "F5" }
59         { 97 "F6" }
60         { 98 "F7" }
61         { 100 "F8" }
62         { 123 "LEFT" }
63         { 124 "RIGHT" }
64         { 125 "DOWN" }
65         { 126 "UP" }
66         { 116 "PAGE_UP" }
67         { 121 "PAGE_DOWN" }
68     }
69
70 : key-code ( event -- string ? )
71     dup -> keyCode key-codes at
72     [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
73
74 : event-modifiers ( event -- modifiers )
75     -> modifierFlags modifiers modifier ;
76
77 : key-event>gesture ( event -- modifiers keycode action? )
78     [ event-modifiers ] [ key-code ] bi ;
79
80 : send-key-event ( view gesture -- )
81     swap window [ propagate-key-gesture ] [ drop ] if* ;
82
83 : interpret-key-event ( view event -- )
84     NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
85
86 : send-key-down-event ( view event -- )
87     [ key-event>gesture <key-down> send-key-event ]
88     [ interpret-key-event ]
89     2bi ;
90
91 : send-key-up-event ( view event -- )
92     key-event>gesture <key-up> send-key-event ;
93
94 : mouse-event>gesture ( event -- modifiers button )
95     [ event-modifiers ] [ button ] bi ;
96
97 : send-button-down$ ( view event -- )
98     [ nip mouse-event>gesture <button-down> ]
99     [ mouse-location ]
100     [ drop window ]
101     2tri
102     [ send-button-down ] [ 2drop ] if* ;
103
104 : send-button-up$ ( view event -- )
105     [ nip mouse-event>gesture <button-up> ]
106     [ mouse-location ]
107     [ drop window ]
108     2tri
109     [ send-button-up ] [ 2drop ] if* ;
110
111 : send-scroll$ ( view event -- )
112     [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
113     [ mouse-location ]
114     [ drop window ]
115     2tri
116     [ send-scroll ] [ 2drop ] if* ;
117
118 : send-action$ ( view event gesture -- )
119     [ drop window ] dip over [ send-action ] [ 2drop ] if ;
120
121 : add-resize-observer ( observer object -- )
122     [
123         "updateFactorGadgetSize:"
124         "NSViewFrameDidChangeNotification" <NSString>
125     ] dip add-observer ;
126
127 : string-or-nil? ( NSString -- ? )
128     [ CF>string NSStringPboardType = ] [ t ] if* ;
129
130 : valid-service? ( gadget send-type return-type -- ? )
131     2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
132     [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
133
134 : NSRect>rect ( NSRect world -- rect )
135     [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
136     [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
137     2bi <rect> ;
138
139 : rect>NSRect ( rect world -- NSRect )
140     [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
141     [ drop dim>> first2 ]
142     2bi <CGRect> ;
143
144 CONSTANT: selector>action H{
145     { "undo:" undo-action }
146     { "redo:" redo-action }
147     { "cut:" cut-action }
148     { "copy:" copy-action }
149     { "paste:" paste-action }
150     { "delete:" delete-action }
151     { "selectAll:" select-all-action }
152     { "newDocument:" new-action }
153     { "openDocument:" open-action }
154     { "saveDocument:" save-action }
155     { "saveDocumentAs:" save-as-action }
156     { "revertDocumentToSaved:" revert-action }
157 }
158
159 : validate-action ( world selector -- ? validated? )
160     selector>action at
161     [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
162
163 <CLASS: FactorView < NSOpenGLView
164     COCOA-PROTOCOL: NSTextInput
165
166     METHOD: void prepareOpenGL [
167
168         self SEL: setWantsBestResolutionOpenGLSurface:
169         -> respondsToSelector: c-bool> [
170
171             self SEL: setWantsBestResolutionOpenGLSurface: 1
172             void f "objc_msgSend" { id SEL char } f alien-invoke
173
174             self SEL: backingScaleFactor
175             double f "objc_msgSend" { id SEL } f alien-invoke
176
177             dup 1.0 > [
178                 gl-scale-factor set-global t retina? set-global
179                 cached-lines get-global clear-assoc
180             ] [ drop ] if
181
182         ] when
183     ] ;
184
185     ! Rendering
186     METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
187
188     ! Events
189     METHOD: char acceptsFirstMouse: id event [ 0 ] ;
190
191     METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
192
193     METHOD: void mouseExited: id event [ forget-rollover ] ;
194
195     METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
196
197     METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
198
199     METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
200
201     METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
202
203     METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
204
205     METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
206
207     METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
208
209     METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
210
211     METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
212
213     METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
214
215     METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
216
217     METHOD: void keyDown: id event [ self event send-key-down-event ] ;
218
219     METHOD: void keyUp: id event [ self event send-key-up-event ] ;
220
221     METHOD: char validateUserInterfaceItem: id event
222     [
223         self window [
224             event -> action utf8 alien>string validate-action
225             [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
226         ] [ 0 ] if*
227     ] ;
228
229     METHOD: id undo: id event [ self event undo-action send-action$ f ] ;
230
231     METHOD: id redo: id event [ self event redo-action send-action$ f ] ;
232
233     METHOD: id cut: id event [ self event cut-action send-action$ f ] ;
234
235     METHOD: id copy: id event [ self event copy-action send-action$ f ] ;
236
237     METHOD: id paste: id event [ self event paste-action send-action$ f ] ;
238
239     METHOD: id delete: id event [ self event delete-action send-action$ f ] ;
240
241     METHOD: id selectAll: id event [ self event select-all-action send-action$ f ] ;
242
243     METHOD: id newDocument: id event [ self event new-action send-action$ f ] ;
244
245     METHOD: id openDocument: id event [ self event open-action send-action$ f ] ;
246
247     METHOD: id saveDocument: id event [ self event save-action send-action$ f ] ;
248
249     METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ] ;
250
251     METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ] ;
252
253     ! Multi-touch gestures
254     METHOD: void magnifyWithEvent: id event
255     [
256         self event
257         dup -> deltaZ sgn {
258             {  1 [ zoom-in-action send-action$ ] }
259             { -1 [ zoom-out-action send-action$ ] }
260             {  0 [ 2drop ] }
261         } case
262     ] ;
263
264     METHOD: void swipeWithEvent: id event
265     [
266         self event
267         dup -> deltaX sgn {
268             {  1 [ left-action send-action$ ] }
269             { -1 [ right-action send-action$ ] }
270             {  0
271                 [
272                     dup -> deltaY sgn {
273                         {  1 [ up-action send-action$ ] }
274                         { -1 [ down-action send-action$ ] }
275                         {  0 [ 2drop ] }
276                     } case
277                 ]
278             }
279         } case
280     ] ;
281
282     METHOD: char acceptsFirstResponder [ 1 ] ;
283
284     ! Services
285     METHOD: id validRequestorForSendType: id sendType returnType: id returnType
286     [
287         ! We return either self or nil
288         self window [
289             world-focus sendType returnType
290             valid-service? [ self ] [ f ] if
291         ] [ f ] if*
292     ] ;
293
294     METHOD: char writeSelectionToPasteboard: id pboard types: id types
295     [
296         NSStringPboardType types CF>string-array member? [
297             self window [
298                 world-focus gadget-selection
299                 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
300             ] [ 0 ] if*
301         ] [ 0 ] if
302     ] ;
303
304     METHOD: char readSelectionFromPasteboard: id pboard
305     [
306         self window :> window
307         window [
308             pboard pasteboard-string
309             [ window user-input 1 ] [ 0 ] if*
310         ] [ 0 ] if
311     ] ;
312
313     ! Text input
314     METHOD: void insertText: id text
315     [
316         self window :> window
317         window [
318             text CF>string window user-input
319         ] when
320     ] ;
321
322     METHOD: char hasMarkedText [ 0 ] ;
323
324     METHOD: NSRange markedRange [ 0 0 <NSRange> ] ;
325
326     METHOD: NSRange selectedRange [ 0 0 <NSRange> ] ;
327
328     METHOD: void setMarkedText: id text selectedRange: NSRange range [ ] ;
329
330     METHOD: void unmarkText [ ] ;
331
332     METHOD: id validAttributesForMarkedText [ NSArray -> array ] ;
333
334     METHOD: id attributedSubstringFromRange: NSRange range [ f ] ;
335
336     METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
337
338     METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ] ;
339
340     METHOD: NSInteger conversationIdentifier [ self alien-address ] ;
341
342     ! Initialization
343     METHOD: void updateFactorGadgetSize: id notification
344     [
345         self window :> window
346         window [
347             self view-dim window dim<< yield
348         ] when
349     ] ;
350
351     METHOD: void doCommandBySelector: SEL selector [ ] ;
352
353     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
354     [
355         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
356         dup dup add-resize-observer
357     ] ;
358
359     METHOD: char isOpaque [ 0 ] ;
360
361     METHOD: void dealloc
362     [
363         self remove-observer
364         self SUPER-> dealloc
365     ] ;
366 ;CLASS>
367
368 : sync-refresh-to-screen ( GLView -- )
369     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
370     CGLSetParameter drop ;
371
372 : <FactorView> ( dim pixel-format -- view )
373     [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
374
375 : save-position ( world window -- )
376     -> frame CGRect-top-left 2array >>window-loc drop ;
377
378 <CLASS: FactorWindowDelegate < NSObject
379
380     METHOD: void windowDidMove: id notification
381     [
382         notification -> object -> contentView window
383         [ notification -> object save-position ] when*
384     ] ;
385
386     METHOD: void windowDidBecomeKey: id notification
387     [
388         notification -> object -> contentView window
389         [ focus-world ] when*
390     ] ;
391
392     METHOD: void windowDidResignKey: id notification
393     [
394         forget-rollover
395         notification -> object -> contentView :> view
396         view window :> window
397         window [
398             view -> isInFullScreenMode 0 =
399             [ window unfocus-world ] when
400         ] when
401     ] ;
402
403     METHOD: char windowShouldClose: id notification [ 1 ] ;
404
405     METHOD: void windowWillClose: id notification
406     [
407         notification -> object -> contentView
408         [ window ungraft ] [ unregister-window ] bi
409     ] ;
410
411     METHOD: void windowDidChangeBackingProperties: id notification
412     [
413
414         notification -> object dup SEL: backingScaleFactor
415         -> respondsToSelector: c-bool> [
416             { double { id SEL } } ?-> backingScaleFactor
417
418             [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
419             [ 1.0 > retina? set-global ] bi
420         ] [ drop ] if
421     ] ;
422 ;CLASS>
423
424 : install-window-delegate ( window -- )
425     FactorWindowDelegate install-delegate ;