]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/cocoa.factor
Updating code to use with-out-parameters
[factor.git] / basis / ui / backend / cocoa / cocoa.factor
1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data arrays assocs classes
4 cocoa cocoa.application cocoa.classes cocoa.messages cocoa.nibs
5 cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.types
6 cocoa.views cocoa.windows combinators command-line
7 core-foundation core-foundation.run-loop core-graphics
8 core-graphics.types destructors fry generalizations io.thread
9 kernel libc literals locals math math.bitwise math.rectangles
10 memory namespaces sequences threads ui colors ui.backend
11 ui.backend.cocoa.views ui.clipboards ui.gadgets
12 ui.gadgets.worlds ui.pixel-formats ui.pixel-formats.private
13 ui.private words.symbol ;
14 IN: ui.backend.cocoa
15
16 TUPLE: handle ;
17 TUPLE: window-handle < handle view window ;
18 TUPLE: offscreen-handle < handle context buffer ;
19
20 C: <window-handle> window-handle
21 C: <offscreen-handle> offscreen-handle
22
23 SINGLETON: cocoa-ui-backend
24
25 PIXEL-FORMAT-ATTRIBUTE-TABLE: NSOpenGLPFA { } H{
26     { double-buffered { $ NSOpenGLPFADoubleBuffer } }
27     { stereo { $ NSOpenGLPFAStereo } }
28     { offscreen { $ NSOpenGLPFAOffScreen } }
29     { fullscreen { $ NSOpenGLPFAFullScreen } }
30     { windowed { $ NSOpenGLPFAWindow } }
31     { accelerated { $ NSOpenGLPFAAccelerated } }
32     { software-rendered { $ NSOpenGLPFARendererID $ kCGLRendererGenericFloatID } }
33     { backing-store { $ NSOpenGLPFABackingStore } }
34     { multisampled { $ NSOpenGLPFAMultisample } }
35     { supersampled { $ NSOpenGLPFASupersample } }
36     { sample-alpha { $ NSOpenGLPFASampleAlpha } }
37     { color-float { $ NSOpenGLPFAColorFloat } }
38     { color-bits { $ NSOpenGLPFAColorSize } }
39     { alpha-bits { $ NSOpenGLPFAAlphaSize } }
40     { accum-bits { $ NSOpenGLPFAAccumSize } }
41     { depth-bits { $ NSOpenGLPFADepthSize } }
42     { stencil-bits { $ NSOpenGLPFAStencilSize } }
43     { aux-buffers { $ NSOpenGLPFAAuxBuffers } }
44     { sample-buffers { $ NSOpenGLPFASampleBuffers } }
45     { samples { $ NSOpenGLPFASamples } }
46 }
47
48 M: cocoa-ui-backend (make-pixel-format)
49     nip >NSOpenGLPFA-int-array
50     NSOpenGLPixelFormat -> alloc swap -> initWithAttributes: ;
51
52 M: cocoa-ui-backend (free-pixel-format)
53     handle>> -> release ;
54
55 M: cocoa-ui-backend (pixel-format-attribute)
56     [ handle>> ] [ >NSOpenGLPFA ] bi*
57     [ drop f ]
58     [
59         first
60         { int } [ swap 0 -> getValues:forAttribute:forVirtualScreen: ] [ ]
61         with-out-parameters
62     ] if-empty ;
63
64 TUPLE: pasteboard handle ;
65
66 C: <pasteboard> pasteboard
67
68 M: pasteboard clipboard-contents
69     handle>> pasteboard-string ;
70
71 M: pasteboard set-clipboard-contents
72     handle>> set-pasteboard-string ;
73
74 : init-clipboard ( -- )
75     NSPasteboard -> generalPasteboard <pasteboard>
76     clipboard set-global
77     <clipboard> selection set-global ;
78
79 : world>NSRect ( world -- NSRect )
80     [ 0 0 ] dip dim>> first2 <CGRect> ;
81
82 : auto-position ( window loc -- )
83     #! Note: if this is the initial window, the length of the windows
84     #! vector should be 1, since (open-window) calls auto-position
85     #! after register-window.
86     dup { 0 0 } = [
87         drop
88         windows get length 1 <= [ -> center ] [
89             windows get last second window-loc>>
90             dupd first2 <CGPoint> -> cascadeTopLeftFromPoint:
91             -> setFrameTopLeftPoint:
92         ] if
93     ] [ first2 <CGPoint> -> setFrameTopLeftPoint: ] if ;
94
95 M: cocoa-ui-backend set-title ( string world -- )
96     handle>> window>> swap <NSString> -> setTitle: ;
97
98 : enter-fullscreen ( world -- )
99     handle>> view>>
100     NSScreen -> mainScreen
101     f -> enterFullScreenMode:withOptions:
102     drop ;
103
104 : exit-fullscreen ( world -- )
105     handle>>
106     [ view>> f -> exitFullScreenModeWithOptions: ] 
107     [ [ window>> ] [ view>> ] bi -> makeFirstResponder: drop ] bi ;
108
109 M: cocoa-ui-backend (set-fullscreen) ( world ? -- )
110     [ enter-fullscreen ] [ exit-fullscreen ] if ;
111
112 M: cocoa-ui-backend (fullscreen?) ( world -- ? )
113     handle>> view>> -> isInFullScreenMode zero? not ;
114
115 CONSTANT: window-control>styleMask
116     H{
117         { close-button $ NSClosableWindowMask }
118         { minimize-button $ NSMiniaturizableWindowMask }
119         { maximize-button 0 }
120         { resize-handles $ NSResizableWindowMask }
121         { small-title-bar $[ NSTitledWindowMask NSUtilityWindowMask bitor ] }
122         { normal-title-bar $ NSTitledWindowMask }
123         { textured-background $ NSTexturedBackgroundWindowMask }
124     }
125
126 : world>styleMask ( world -- n )
127     window-controls>> window-control>styleMask symbols>flags ;
128
129 : make-context-transparent ( view -- )
130     -> openGLContext
131     0 <int> NSOpenGLCPSurfaceOpacity -> setValues:forParameter: ;
132
133 M:: cocoa-ui-backend (open-window) ( world -- )
134     world [ [ dim>> ] dip <FactorView> ]
135     with-world-pixel-format :> view
136     world window-controls>> textured-background swap member-eq?
137     [ view make-context-transparent ] when
138     view world [ world>NSRect ] [ world>styleMask ] bi <ViewWindow> :> window
139     view -> release
140     world view register-window
141     window world window-loc>> auto-position
142     world window save-position
143     window install-window-delegate
144     view window <window-handle> world handle<<
145     window f -> makeKeyAndOrderFront: ;
146
147 M: cocoa-ui-backend (close-window) ( handle -- )
148     [
149         view>> dup -> isInFullScreenMode zero?
150         [ drop ]
151         [ f -> exitFullScreenModeWithOptions: ] if
152     ] [ window>> -> release ] bi ;
153
154 M: cocoa-ui-backend (grab-input) ( handle -- )
155     0 CGAssociateMouseAndMouseCursorPosition drop
156     CGMainDisplayID CGDisplayHideCursor drop
157     window>> -> frame CGRect>rect rect-center
158     NSScreen -> screens 0 -> objectAtIndex: -> frame CGRect-h
159     [ drop first ] [ swap second - ] 2bi <CGPoint>
160     [ GetCurrentButtonState zero? not ] [ yield ] while
161     CGWarpMouseCursorPosition drop ;
162
163 M: cocoa-ui-backend (ungrab-input) ( handle -- )
164     drop
165     CGMainDisplayID CGDisplayShowCursor drop
166     1 CGAssociateMouseAndMouseCursorPosition drop ;
167
168 M: cocoa-ui-backend close-window ( gadget -- )
169     find-world [
170         handle>> [
171             window>> -> close
172         ] when*
173     ] when* ;
174
175 M: cocoa-ui-backend raise-window* ( world -- )
176     handle>> [
177         window>> dup f -> orderFront: -> makeKeyWindow
178         NSApp 1 -> activateIgnoringOtherApps:
179     ] when* ;
180
181 : pixel-size ( pixel-format -- size )
182     color-bits pixel-format-attribute -3 shift ;
183
184 : offscreen-buffer ( world pixel-format -- alien w h pitch )
185     [ dim>> first2 ] [ pixel-size ] bi*
186     { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ;
187
188 :: gadget-offscreen-context ( world -- context buffer )
189     world [
190         nip :> pf
191         NSOpenGLContext -> alloc pf handle>> f -> initWithFormat:shareContext:
192         dup world pf offscreen-buffer
193         4 npick [ -> setOffScreen:width:height:rowbytes: ] dip
194     ] with-world-pixel-format ;
195
196 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
197     dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
198
199 M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
200     [ context>> -> release ]
201     [ buffer>> free ] bi ;
202
203 GENERIC: (gl-context) ( handle -- context )
204 M: window-handle (gl-context) view>> -> openGLContext ;
205 M: offscreen-handle (gl-context) context>> ;
206
207 M: handle select-gl-context ( handle -- )
208     (gl-context) -> makeCurrentContext ;
209
210 M: handle flush-gl-context ( handle -- )
211     (gl-context) -> flushBuffer ;
212
213 M: cocoa-ui-backend offscreen-pixels ( world -- alien w h )
214     [ handle>> buffer>> ] [ dim>> first2 neg ] bi ;
215
216 M: cocoa-ui-backend beep ( -- )
217     NSBeep ;
218
219 M: cocoa-ui-backend system-alert
220     invalidate-run-loop-timers
221     NSAlert -> alloc -> init -> autorelease [
222         {
223             [ swap <NSString> -> setInformativeText: ]
224             [ swap <NSString> -> setMessageText: ]
225             [ "OK" <NSString> -> addButtonWithTitle: drop ]
226             [ -> runModal drop ]
227         } cleave
228     ] [ 2drop ] if*
229     init-thread-timer ;
230
231 CLASS: {
232     { +superclass+ "NSObject" }
233     { +name+ "FactorApplicationDelegate" }
234 }
235
236 { "applicationDidUpdate:" void { id SEL id }
237     [ 3drop reset-run-loop ]
238 } ;
239
240 : install-app-delegate ( -- )
241     NSApp FactorApplicationDelegate install-delegate ;
242
243 SYMBOL: cocoa-startup-hook
244
245 cocoa-startup-hook [
246     [ "MiniFactor.nib" load-nib install-app-delegate ]
247 ] initialize
248
249 M: cocoa-ui-backend (with-ui)
250     "UI" assert.app [
251         [
252             init-clipboard
253             cocoa-startup-hook get call( -- )
254             start-ui
255             f io-thread-running? set-global
256             init-thread-timer
257             reset-run-loop
258             NSApp -> run
259         ] ui-running
260     ] with-cocoa ;
261
262 cocoa-ui-backend ui-backend set-global
263
264 [ running.app? "ui.tools" "listener" ? ] main-vocab-hook set-global