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.application sequences system
7 ui ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds
8 ui.cocoa.views core-foundation threads math.geometry.rect fry
13 TUPLE: window-handle < handle view window ;
14 TUPLE: offscreen-handle < handle context buffer ;
16 C: <window-handle> window-handle
17 C: <offscreen-handle> offscreen-handle
19 M: offscreen-handle window>> f ;
20 M: offscreen-handle view>> f ;
22 SINGLETON: cocoa-ui-backend
24 M: cocoa-ui-backend do-events ( -- )
25 [ NSApp '[ _ do-event ] loop ui-wait ] with-autorelease-pool ;
27 TUPLE: pasteboard handle ;
29 C: <pasteboard> pasteboard
31 M: pasteboard clipboard-contents
32 handle>> pasteboard-string ;
34 M: pasteboard set-clipboard-contents
35 handle>> set-pasteboard-string ;
37 : init-clipboard ( -- )
38 NSPasteboard -> generalPasteboard <pasteboard>
40 <clipboard> selection set-global ;
42 : world>NSRect ( world -- NSRect )
43 [ window-loc>> ] [ dim>> ] bi [ first2 ] bi@ <NSRect> ;
45 : gadget-window ( world -- )
47 2dup swap world>NSRect <ViewWindow>
48 [ [ -> release ] [ install-window-delegate ] bi* ]
49 [ <window-handle> ] 2bi
52 M: cocoa-ui-backend set-title ( string world -- )
53 handle>> window>> swap <NSString> -> setTitle: ;
55 : enter-fullscreen ( world -- )
57 NSScreen -> mainScreen
58 f -> enterFullScreenMode:withOptions:
61 : exit-fullscreen ( world -- )
62 handle>> view>> f -> exitFullScreenModeWithOptions: ;
64 M: cocoa-ui-backend set-fullscreen* ( ? world -- )
65 swap [ enter-fullscreen ] [ exit-fullscreen ] if ;
67 M: cocoa-ui-backend fullscreen* ( world -- ? )
68 handle>> view>> -> isInFullScreenMode zero? not ;
70 : auto-position ( world -- )
71 dup window-loc>> { 0 0 } = [
72 handle>> window>> -> center
77 M: cocoa-ui-backend (open-window) ( world -- )
80 handle>> window>> f -> makeKeyAndOrderFront: ;
82 M: cocoa-ui-backend (close-window) ( handle -- )
85 M: cocoa-ui-backend close-window ( gadget -- )
88 window>> f -> performClose:
92 M: cocoa-ui-backend raise-window* ( world -- )
94 window>> dup f -> orderFront: -> makeKeyWindow
95 NSApp 1 -> activateIgnoringOtherApps:
98 : pixel-size ( pixel-format -- size )
99 0 <int> [ NSOpenGLPFAColorSize 0 -> getValues:forAttribute:forVirtualScreen: ]
102 : offscreen-buffer ( world pixel-format -- alien w h pitch )
103 [ dim>> first2 ] [ pixel-size ] bi*
104 { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } cleave ;
106 : gadget-offscreen-context ( world -- context buffer )
107 { NSOpenGLPFAOffscreen } <PixelFormat>
108 [ NSOpenGLContext -> alloc swap f -> initWithFormat:shareContext: ]
109 [ offscreen-buffer ] bi
110 4 npick [ setOffScreen:width:height:rowbytes: ] dip ;
112 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
113 dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
115 M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
116 [ context>> -> release ]
117 [ buffer>> free ] bi ;
119 GENERIC: gl-context ( handle -- context )
120 M: window-handle gl-context view>> -> openGLContext ;
121 M: offscreen-handle gl-context context>> ;
123 M: handle select-gl-context ( handle -- )
124 gl-context -> makeCurrentContext ;
126 M: handle flush-gl-context ( handle -- )
127 gl-context -> flushBuffer ;
129 M: cocoa-ui-backend beep ( -- )
133 { +superclass+ "NSObject" }
134 { +name+ "FactorApplicationDelegate" }
137 { "applicationDidFinishLaunching:" "void" { "id" "SEL" "id" }
141 : install-app-delegate ( -- )
142 NSApp FactorApplicationDelegate install-delegate ;
144 SYMBOL: cocoa-init-hook
146 cocoa-init-hook global [ [ install-app-delegate ] or ] change-at
148 M: cocoa-ui-backend ui
152 cocoa-init-hook get call
158 cocoa-ui-backend ui-backend set-global
160 [ running.app? "ui" "listener" ? ] main-vocab-hook set-global