1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors math arrays assocs cocoa cocoa.application
4 command-line kernel memory namespaces cocoa.messages
5 cocoa.runtime cocoa.subclassing cocoa.pasteboard cocoa.types
6 cocoa.windows cocoa.classes cocoa.nibs sequences ui ui.private
7 ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds
8 ui.backend.cocoa.views core-foundation core-foundation.run-loop
9 core-graphics.types threads math.rectangles fry libc
10 generalizations alien.c-types cocoa.views
11 combinators io.thread locals ;
15 TUPLE: window-handle < handle view window ;
16 TUPLE: offscreen-handle < handle context buffer ;
18 C: <window-handle> window-handle
19 C: <offscreen-handle> offscreen-handle
21 SINGLETON: cocoa-ui-backend
23 TUPLE: pasteboard handle ;
25 C: <pasteboard> pasteboard
27 M: pasteboard clipboard-contents
28 handle>> pasteboard-string ;
30 M: pasteboard set-clipboard-contents
31 handle>> set-pasteboard-string ;
33 : init-clipboard ( -- )
34 NSPasteboard -> generalPasteboard <pasteboard>
36 <clipboard> selection set-global ;
38 : world>NSRect ( world -- NSRect )
39 [ 0 0 ] dip dim>> first2 <CGRect> ;
41 : auto-position ( window loc -- )
44 windows get [ -> center ] [
45 peek second window-loc>>
46 dupd first2 <CGPoint> -> cascadeTopLeftFromPoint:
47 -> setFrameTopLeftPoint:
49 ] [ first2 <CGPoint> -> setFrameTopLeftPoint: ] if ;
51 M: cocoa-ui-backend set-title ( string world -- )
52 handle>> window>> swap <NSString> -> setTitle: ;
54 : enter-fullscreen ( world -- )
56 NSScreen -> mainScreen
57 f -> enterFullScreenMode:withOptions:
60 : exit-fullscreen ( world -- )
61 handle>> view>> f -> exitFullScreenModeWithOptions: ;
63 M: cocoa-ui-backend set-fullscreen* ( ? world -- )
64 swap [ enter-fullscreen ] [ exit-fullscreen ] if ;
66 M: cocoa-ui-backend fullscreen* ( world -- ? )
67 handle>> view>> -> isInFullScreenMode zero? not ;
69 M:: cocoa-ui-backend (open-window) ( world -- )
70 world dim>> <FactorView> :> view
71 view world world>NSRect <ViewWindow> :> window
73 window world window-loc>> auto-position
74 world view register-window
75 world window save-position
76 window install-window-delegate
77 view window <window-handle> world (>>handle)
78 window f -> makeKeyAndOrderFront: ;
80 M: cocoa-ui-backend (close-window) ( handle -- )
83 M: cocoa-ui-backend close-window ( gadget -- )
86 window>> f -> performClose:
90 M: cocoa-ui-backend raise-window* ( world -- )
92 window>> dup f -> orderFront: -> makeKeyWindow
93 NSApp 1 -> activateIgnoringOtherApps:
96 : pixel-size ( pixel-format -- size )
97 0 <int> [ NSOpenGLPFAColorSize 0 -> getValues:forAttribute:forVirtualScreen: ]
100 : offscreen-buffer ( world pixel-format -- alien w h pitch )
101 [ dim>> first2 ] [ pixel-size ] bi*
102 { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ;
104 : gadget-offscreen-context ( world -- context buffer )
105 NSOpenGLPFAOffScreen 1array <PixelFormat>
106 [ nip NSOpenGLContext -> alloc swap f -> initWithFormat:shareContext: dup ]
107 [ offscreen-buffer ] 2bi
108 4 npick [ -> setOffScreen:width:height:rowbytes: ] dip ;
110 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
111 dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
113 M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
114 [ context>> -> release ]
115 [ buffer>> free ] bi ;
117 GENERIC: (gl-context) ( handle -- context )
118 M: window-handle (gl-context) view>> -> openGLContext ;
119 M: offscreen-handle (gl-context) context>> ;
121 M: handle select-gl-context ( handle -- )
122 (gl-context) -> makeCurrentContext ;
124 M: handle flush-gl-context ( handle -- )
125 (gl-context) -> flushBuffer ;
127 M: cocoa-ui-backend offscreen-pixels ( world -- alien w h )
128 [ handle>> buffer>> ] [ dim>> first2 neg ] bi ;
130 M: cocoa-ui-backend beep ( -- )
134 { +superclass+ "NSObject" }
135 { +name+ "FactorApplicationDelegate" }
138 { "applicationDidUpdate:" "void" { "id" "SEL" "id" }
139 [ 3drop reset-run-loop ]
142 : install-app-delegate ( -- )
143 NSApp FactorApplicationDelegate install-delegate ;
145 SYMBOL: cocoa-init-hook
148 [ "MiniFactor.nib" load-nib install-app-delegate ]
151 M: cocoa-ui-backend (with-ui)
155 cocoa-init-hook get call( -- )
157 f io-thread-running? set-global
164 cocoa-ui-backend ui-backend set-global
166 [ running.app? "ui.tools" "listener" ? ] main-vocab-hook set-global