]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/cocoa.factor
Merge branch 'master' of git@github.com:prunedtree/factor
[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 arrays assocs classes cocoa
4 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 memory
10 namespaces sequences threads ui
11 ui.backend 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     [ first 0 <int> [ swap 0 -> getValues:forAttribute:forVirtualScreen: ] keep *int ]
59     if-empty ;
60
61 TUPLE: pasteboard handle ;
62
63 C: <pasteboard> pasteboard
64
65 M: pasteboard clipboard-contents
66     handle>> pasteboard-string ;
67
68 M: pasteboard set-clipboard-contents
69     handle>> set-pasteboard-string ;
70
71 : init-clipboard ( -- )
72     NSPasteboard -> generalPasteboard <pasteboard>
73     clipboard set-global
74     <clipboard> selection set-global ;
75
76 : world>NSRect ( world -- NSRect )
77     [ 0 0 ] dip dim>> first2 <CGRect> ;
78
79 : auto-position ( window loc -- )
80     #! Note: if this is the initial window, the length of the windows
81     #! vector should be 1, since (open-window) calls auto-position
82     #! after register-window.
83     dup { 0 0 } = [
84         drop
85         windows get length 1 <= [ -> center ] [
86             windows get last second window-loc>>
87             dupd first2 <CGPoint> -> cascadeTopLeftFromPoint:
88             -> setFrameTopLeftPoint:
89         ] if
90     ] [ first2 <CGPoint> -> setFrameTopLeftPoint: ] if ;
91
92 M: cocoa-ui-backend set-title ( string world -- )
93     handle>> window>> swap <NSString> -> setTitle: ;
94
95 : enter-fullscreen ( world -- )
96     handle>> view>>
97     NSScreen -> mainScreen
98     f -> enterFullScreenMode:withOptions:
99     drop ;
100
101 : exit-fullscreen ( world -- )
102     handle>>
103     [ view>> f -> exitFullScreenModeWithOptions: ] 
104     [ [ window>> ] [ view>> ] bi -> makeFirstResponder: drop ] bi ;
105
106 M: cocoa-ui-backend (set-fullscreen) ( world ? -- )
107     [ enter-fullscreen ] [ exit-fullscreen ] if ;
108
109 M: cocoa-ui-backend (fullscreen?) ( world -- ? )
110     handle>> view>> -> isInFullScreenMode zero? not ;
111
112 CONSTANT: window-control>styleMask
113     H{
114         { close-button $ NSClosableWindowMask }
115         { minimize-button $ NSMiniaturizableWindowMask }
116         { maximize-button 0 }
117         { resize-handles $ NSResizableWindowMask }
118         { small-title-bar $[ NSTitledWindowMask NSUtilityWindowMask bitor ] }
119         { normal-title-bar $ NSTitledWindowMask }
120     }
121
122 : world>styleMask ( world -- n )
123     window-controls>> window-control>styleMask symbols>flags ;
124
125 M:: cocoa-ui-backend (open-window) ( world -- )
126     world [ [ dim>> ] dip <FactorView> ]
127     with-world-pixel-format :> view
128     view world [ world>NSRect ] [ world>styleMask ] bi <ViewWindow> :> window
129     view -> release
130     world view register-window
131     window world window-loc>> auto-position
132     world window save-position
133     window install-window-delegate
134     view window <window-handle> world (>>handle)
135     window f -> makeKeyAndOrderFront: ;
136
137 M: cocoa-ui-backend (close-window) ( handle -- )
138     [
139         view>> dup -> isInFullScreenMode zero?
140         [ drop ]
141         [ f -> exitFullScreenModeWithOptions: ] if
142     ] [ window>> -> release ] bi ;
143
144 M: cocoa-ui-backend (grab-input) ( handle -- )
145     0 CGAssociateMouseAndMouseCursorPosition drop
146     CGMainDisplayID CGDisplayHideCursor drop
147     window>> -> frame CGRect>rect rect-center
148     NSScreen -> screens 0 -> objectAtIndex: -> frame CGRect-h
149     [ drop first ] [ swap second - ] 2bi <CGPoint>
150     [ GetCurrentButtonState zero? not ] [ yield ] while
151     CGWarpMouseCursorPosition drop ;
152
153 M: cocoa-ui-backend (ungrab-input) ( handle -- )
154     drop
155     CGMainDisplayID CGDisplayShowCursor drop
156     1 CGAssociateMouseAndMouseCursorPosition drop ;
157
158 M: cocoa-ui-backend close-window ( gadget -- )
159     find-world [
160         handle>> [
161             window>> -> close
162         ] when*
163     ] when* ;
164
165 M: cocoa-ui-backend raise-window* ( world -- )
166     handle>> [
167         window>> dup f -> orderFront: -> makeKeyWindow
168         NSApp 1 -> activateIgnoringOtherApps:
169     ] when* ;
170
171 : pixel-size ( pixel-format -- size )
172     color-bits pixel-format-attribute -3 shift ;
173
174 : offscreen-buffer ( world pixel-format -- alien w h pitch )
175     [ dim>> first2 ] [ pixel-size ] bi*
176     { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ;
177
178 :: gadget-offscreen-context ( world -- context buffer )
179     world [
180         nip :> pf
181         NSOpenGLContext -> alloc pf handle>> f -> initWithFormat:shareContext:
182         dup world pf offscreen-buffer
183         4 npick [ -> setOffScreen:width:height:rowbytes: ] dip
184     ] with-world-pixel-format ;
185
186 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
187     dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
188
189 M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
190     [ context>> -> release ]
191     [ buffer>> free ] bi ;
192
193 GENERIC: (gl-context) ( handle -- context )
194 M: window-handle (gl-context) view>> -> openGLContext ;
195 M: offscreen-handle (gl-context) context>> ;
196
197 M: handle select-gl-context ( handle -- )
198     (gl-context) -> makeCurrentContext ;
199
200 M: handle flush-gl-context ( handle -- )
201     (gl-context) -> flushBuffer ;
202
203 M: cocoa-ui-backend offscreen-pixels ( world -- alien w h )
204     [ handle>> buffer>> ] [ dim>> first2 neg ] bi ;
205
206 M: cocoa-ui-backend beep ( -- )
207     NSBeep ;
208
209 CLASS: {
210     { +superclass+ "NSObject" }
211     { +name+ "FactorApplicationDelegate" }
212 }
213
214 {  "applicationDidUpdate:" "void" { "id" "SEL" "id" }
215     [ 3drop reset-run-loop ]
216 } ;
217
218 : install-app-delegate ( -- )
219     NSApp FactorApplicationDelegate install-delegate ;
220
221 SYMBOL: cocoa-init-hook
222
223 cocoa-init-hook [
224     [ "MiniFactor.nib" load-nib install-app-delegate ]
225 ] initialize
226
227 M: cocoa-ui-backend (with-ui)
228     "UI" assert.app [
229         [
230             init-clipboard
231             cocoa-init-hook get call( -- )
232             start-ui
233             f io-thread-running? set-global
234             init-thread-timer
235             reset-run-loop
236             NSApp -> run
237         ] ui-running
238     ] with-cocoa ;
239
240 cocoa-ui-backend ui-backend set-global
241
242 [ running.app? "ui.tools" "listener" ? ] main-vocab-hook set-global