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