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.rectangles memory
10 namespaces sequences specialized-arrays.int 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 ;
17 TUPLE: window-handle < handle view window ;
18 TUPLE: offscreen-handle < handle context buffer ;
20 C: <window-handle> window-handle
21 C: <offscreen-handle> offscreen-handle
23 SINGLETON: cocoa-ui-backend
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 } }
48 M: cocoa-ui-backend (make-pixel-format)
49 nip >NSOpenGLPFA-int-array
50 NSOpenGLPixelFormat -> alloc swap -> initWithAttributes: ;
52 M: cocoa-ui-backend (free-pixel-format)
55 M: cocoa-ui-backend (pixel-format-attribute)
56 [ handle>> ] [ >NSOpenGLPFA ] bi*
58 [ first 0 <int> [ swap 0 -> getValues:forAttribute:forVirtualScreen: ] keep *int ]
61 TUPLE: pasteboard handle ;
63 C: <pasteboard> pasteboard
65 M: pasteboard clipboard-contents
66 handle>> pasteboard-string ;
68 M: pasteboard set-clipboard-contents
69 handle>> set-pasteboard-string ;
71 : init-clipboard ( -- )
72 NSPasteboard -> generalPasteboard <pasteboard>
74 <clipboard> selection set-global ;
76 : world>NSRect ( world -- NSRect )
77 [ 0 0 ] dip dim>> first2 <CGRect> ;
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.
85 windows get length 1 <= [ -> center ] [
86 windows get peek second window-loc>>
87 dupd first2 <CGPoint> -> cascadeTopLeftFromPoint:
88 -> setFrameTopLeftPoint:
90 ] [ first2 <CGPoint> -> setFrameTopLeftPoint: ] if ;
92 M: cocoa-ui-backend set-title ( string world -- )
93 handle>> window>> swap <NSString> -> setTitle: ;
95 : enter-fullscreen ( world -- )
97 NSScreen -> mainScreen
98 f -> enterFullScreenMode:withOptions:
101 : exit-fullscreen ( world -- )
103 [ view>> f -> exitFullScreenModeWithOptions: ]
104 [ [ window>> ] [ view>> ] bi -> makeFirstResponder: drop ] bi ;
106 M: cocoa-ui-backend (set-fullscreen) ( world ? -- )
107 [ enter-fullscreen ] [ exit-fullscreen ] if ;
109 M: cocoa-ui-backend (fullscreen?) ( world -- ? )
110 handle>> view>> -> isInFullScreenMode zero? not ;
112 M:: cocoa-ui-backend (open-window) ( world -- )
113 world [ [ dim>> ] dip <FactorView> ]
114 with-world-pixel-format :> view
115 view world world>NSRect <ViewWindow> :> window
117 world view register-window
118 window world window-loc>> auto-position
119 world window save-position
120 window install-window-delegate
121 view window <window-handle> world (>>handle)
122 window f -> makeKeyAndOrderFront: ;
124 M: cocoa-ui-backend (close-window) ( handle -- )
126 view>> dup -> isInFullScreenMode zero?
128 [ f -> exitFullScreenModeWithOptions: ] if
129 ] [ window>> -> release ] bi ;
131 M: cocoa-ui-backend (grab-input) ( handle -- )
132 0 CGAssociateMouseAndMouseCursorPosition drop
133 CGMainDisplayID CGDisplayHideCursor drop
134 window>> -> frame CGRect>rect rect-center
135 NSScreen -> screens 0 -> objectAtIndex: -> frame CGRect-h
136 [ drop first ] [ swap second - ] 2bi <CGPoint>
137 [ GetCurrentButtonState zero? not ] [ yield ] while
138 CGWarpMouseCursorPosition drop ;
140 M: cocoa-ui-backend (ungrab-input) ( handle -- )
142 CGMainDisplayID CGDisplayShowCursor drop
143 1 CGAssociateMouseAndMouseCursorPosition drop ;
145 M: cocoa-ui-backend close-window ( gadget -- )
148 window>> f -> performClose:
152 M: cocoa-ui-backend raise-window* ( world -- )
154 window>> dup f -> orderFront: -> makeKeyWindow
155 NSApp 1 -> activateIgnoringOtherApps:
158 : pixel-size ( pixel-format -- size )
159 color-bits pixel-format-attribute -3 shift ;
161 : offscreen-buffer ( world pixel-format -- alien w h pitch )
162 [ dim>> first2 ] [ pixel-size ] bi*
163 { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ;
165 :: gadget-offscreen-context ( world -- context buffer )
168 NSOpenGLContext -> alloc pf handle>> f -> initWithFormat:shareContext:
169 dup world pf offscreen-buffer
170 4 npick [ -> setOffScreen:width:height:rowbytes: ] dip
171 ] with-world-pixel-format ;
173 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
174 dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
176 M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
177 [ context>> -> release ]
178 [ buffer>> free ] bi ;
180 GENERIC: (gl-context) ( handle -- context )
181 M: window-handle (gl-context) view>> -> openGLContext ;
182 M: offscreen-handle (gl-context) context>> ;
184 M: handle select-gl-context ( handle -- )
185 (gl-context) -> makeCurrentContext ;
187 M: handle flush-gl-context ( handle -- )
188 (gl-context) -> flushBuffer ;
190 M: cocoa-ui-backend offscreen-pixels ( world -- alien w h )
191 [ handle>> buffer>> ] [ dim>> first2 neg ] bi ;
193 M: cocoa-ui-backend beep ( -- )
197 { +superclass+ "NSObject" }
198 { +name+ "FactorApplicationDelegate" }
201 { "applicationDidUpdate:" "void" { "id" "SEL" "id" }
202 [ 3drop reset-run-loop ]
205 : install-app-delegate ( -- )
206 NSApp FactorApplicationDelegate install-delegate ;
208 SYMBOL: cocoa-init-hook
211 [ "MiniFactor.nib" load-nib install-app-delegate ]
214 M: cocoa-ui-backend (with-ui)
218 cocoa-init-hook get call( -- )
220 f io-thread-running? set-global
227 cocoa-ui-backend ui-backend set-global
229 [ running.app? "ui.tools" "listener" ? ] main-vocab-hook set-global