]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/cocoa/cocoa.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / ui / cocoa / cocoa.factor
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
9 libc generalizations ;
10 IN: ui.cocoa
11
12 TUPLE: handle ;
13 TUPLE: window-handle < handle view window ;
14 TUPLE: offscreen-handle < handle context buffer ;
15
16 C: <window-handle> window-handle
17 C: <offscreen-handle> offscreen-handle
18
19 M: offscreen-handle window>> f ;
20 M: offscreen-handle view>>   f ;
21
22 SINGLETON: cocoa-ui-backend
23
24 M: cocoa-ui-backend do-events ( -- )
25     [ NSApp '[ _ do-event ] loop ui-wait ] with-autorelease-pool ;
26
27 TUPLE: pasteboard handle ;
28
29 C: <pasteboard> pasteboard
30
31 M: pasteboard clipboard-contents
32     handle>> pasteboard-string ;
33
34 M: pasteboard set-clipboard-contents
35     handle>> set-pasteboard-string ;
36
37 : init-clipboard ( -- )
38     NSPasteboard -> generalPasteboard <pasteboard>
39     clipboard set-global
40     <clipboard> selection set-global ;
41
42 : world>NSRect ( world -- NSRect )
43     [ window-loc>> ] [ dim>> ] bi [ first2 ] bi@ <NSRect> ;
44
45 : gadget-window ( world -- )
46     dup <FactorView>
47     2dup swap world>NSRect <ViewWindow>
48     [ [ -> release ] [ install-window-delegate ] bi* ]
49     [ <window-handle> ] 2bi
50     >>handle drop ;
51
52 M: cocoa-ui-backend set-title ( string world -- )
53     handle>> window>> swap <NSString> -> setTitle: ;
54
55 : enter-fullscreen ( world -- )
56     handle>> view>>
57     NSScreen -> mainScreen
58     f -> enterFullScreenMode:withOptions:
59     drop ;
60
61 : exit-fullscreen ( world -- )
62     handle>> view>> f -> exitFullScreenModeWithOptions: ;
63
64 M: cocoa-ui-backend set-fullscreen* ( ? world -- )
65     swap [ enter-fullscreen ] [ exit-fullscreen ] if ;
66
67 M: cocoa-ui-backend fullscreen* ( world -- ? )
68     handle>> view>> -> isInFullScreenMode zero? not ;
69
70 : auto-position ( world -- )
71     dup window-loc>> { 0 0 } = [
72         handle>> window>> -> center
73     ] [
74         drop
75     ] if ;
76
77 M: cocoa-ui-backend (open-window) ( world -- )
78     dup gadget-window
79     dup auto-position
80     handle>> window>> f -> makeKeyAndOrderFront: ;
81
82 M: cocoa-ui-backend (close-window) ( handle -- )
83     window>> -> release ;
84
85 M: cocoa-ui-backend close-window ( gadget -- )
86     find-world [
87         handle>> [
88             window>> f -> performClose:
89         ] when*
90     ] when* ;
91
92 M: cocoa-ui-backend raise-window* ( world -- )
93     handle>> [
94         window>> dup f -> orderFront: -> makeKeyWindow
95         NSApp 1 -> activateIgnoringOtherApps:
96     ] when* ;
97
98 : pixel-size ( pixel-format -- size )
99     0 <int> [ NSOpenGLPFAColorSize 0 -> getValues:forAttribute:forVirtualScreen: ]
100     keep *int -3 shift ;
101
102 : offscreen-buffer ( world pixel-format -- alien w h pitch )
103     [ dim>> first2 ] [ pixel-size ] bi*
104     { [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } cleave ;
105
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 ;
111
112 M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
113     dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
114
115 M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
116     [ context>> -> release ]
117     [ buffer>> free ] bi ;
118
119 GENERIC: gl-context ( handle -- context )
120 M: window-handle gl-context view>> -> openGLContext ;
121 M: offscreen-handle gl-context context>> ;
122
123 M: handle select-gl-context ( handle -- )
124     gl-context -> makeCurrentContext ;
125
126 M: handle flush-gl-context ( handle -- )
127     gl-context -> flushBuffer ;
128
129 M: cocoa-ui-backend beep ( -- )
130     NSBeep ;
131
132 CLASS: {
133     { +superclass+ "NSObject" }
134     { +name+ "FactorApplicationDelegate" }
135 }
136
137 { "applicationDidFinishLaunching:" "void" { "id" "SEL" "id" }
138     [ 3drop event-loop ]
139 } ;
140
141 : install-app-delegate ( -- )
142     NSApp FactorApplicationDelegate install-delegate ;
143
144 SYMBOL: cocoa-init-hook
145
146 cocoa-init-hook global [ [ install-app-delegate ] or ] change-at
147
148 M: cocoa-ui-backend ui
149     "UI" assert.app [
150         [
151             init-clipboard
152             cocoa-init-hook get call
153             start-ui
154             NSApp -> run
155         ] ui-running
156     ] with-cocoa ;
157
158 cocoa-ui-backend ui-backend set-global
159
160 [ running.app? "ui" "listener" ? ] main-vocab-hook set-global