1 ! Copyright (C) 2006, 2008 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 system ui
7 ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds
8 ui.cocoa.views core-foundation core-foundation.run-loop threads
9 math.geometry.rect fry libc generalizations alien.c-types
10 cocoa.views combinators io.thread ;
14 TUPLE: window-handle < handle view window ;
15 TUPLE: offscreen-handle < handle context buffer ;
17 C: <window-handle> window-handle
18 C: <offscreen-handle> offscreen-handle
20 SINGLETON: cocoa-ui-backend
22 TUPLE: pasteboard handle ;
24 C: <pasteboard> pasteboard
26 M: pasteboard clipboard-contents
27 handle>> pasteboard-string ;
29 M: pasteboard set-clipboard-contents
30 handle>> set-pasteboard-string ;
32 : init-clipboard ( -- )
33 NSPasteboard -> generalPasteboard <pasteboard>
35 <clipboard> selection set-global ;
37 : world>NSRect ( world -- NSRect )
38 [ window-loc>> ] [ dim>> ] bi [ first2 ] bi@ <NSRect> ;
40 : gadget-window ( world -- )
42 2dup swap world>NSRect <ViewWindow>
43 [ [ -> release ] [ install-window-delegate ] bi* ]
44 [ <window-handle> ] 2bi
47 M: cocoa-ui-backend set-title ( string world -- )
48 handle>> window>> swap <NSString> -> setTitle: ;
50 : enter-fullscreen ( world -- )
52 NSScreen -> mainScreen
53 f -> enterFullScreenMode:withOptions:
56 : exit-fullscreen ( world -- )
57 handle>> view>> f -> exitFullScreenModeWithOptions: ;
59 M: cocoa-ui-backend set-fullscreen* ( ? world -- )
60 swap [ enter-fullscreen ] [ exit-fullscreen ] if ;
62 M: cocoa-ui-backend fullscreen* ( world -- ? )
63 handle>> view>> -> isInFullScreenMode zero? not ;
65 : auto-position ( world -- )
66 dup window-loc>> { 0 0 } = [
67 handle>> window>> -> center
72 M: cocoa-ui-backend (open-window) ( world -- )
75 handle>> window>> f -> makeKeyAndOrderFront: ;
77 M: cocoa-ui-backend (close-window) ( handle -- )
80 M: cocoa-ui-backend close-window ( gadget -- )
83 window>> f -> performClose:
87 M: cocoa-ui-backend raise-window* ( world -- )
89 window>> dup f -> orderFront: -> makeKeyWindow
90 NSApp 1 -> activateIgnoringOtherApps:
93 : pixel-size ( pixel-format -- size )
94 0 <int> [ NSOpenGLPFAColorSize 0 -> getValues:forAttribute:forVirtualScreen: ]
97 : offscreen-buffer ( world pixel-format -- alien w h pitch )
98 [ dim>> first2 ] [ pixel-size ] bi*
99 { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ;
101 : gadget-offscreen-context ( world -- context buffer )
102 NSOpenGLPFAOffScreen 1array <PixelFormat>
103 [ nip NSOpenGLContext -> alloc swap f -> initWithFormat:shareContext: dup ]
104 [ offscreen-buffer ] 2bi
105 4 npick [ -> setOffScreen:width:height:rowbytes: ] dip ;
107 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
108 dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
110 M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
111 [ context>> -> release ]
112 [ buffer>> free ] bi ;
114 GENERIC: (gl-context) ( handle -- context )
115 M: window-handle (gl-context) view>> -> openGLContext ;
116 M: offscreen-handle (gl-context) context>> ;
118 M: handle select-gl-context ( handle -- )
119 (gl-context) -> makeCurrentContext ;
121 M: handle flush-gl-context ( handle -- )
122 (gl-context) -> flushBuffer ;
124 M: cocoa-ui-backend offscreen-pixels ( world -- alien w h )
125 [ handle>> buffer>> ] [ dim>> first2 neg ] bi ;
127 M: cocoa-ui-backend beep ( -- )
131 { +superclass+ "NSObject" }
132 { +name+ "FactorApplicationDelegate" }
135 { "applicationDidUpdate:" "void" { "id" "SEL" "id" }
136 [ 3drop reset-run-loop ]
139 : install-app-delegate ( -- )
140 NSApp FactorApplicationDelegate install-delegate ;
142 SYMBOL: cocoa-init-hook
144 cocoa-init-hook global [
145 [ "MiniFactor.nib" load-nib install-app-delegate ] or
148 M: cocoa-ui-backend ui
152 cocoa-init-hook get call
154 f io-thread-running? set-global
161 cocoa-ui-backend ui-backend set-global
163 [ running.app? "ui" "listener" ? ] main-vocab-hook set-global