]> gitweb.factorcode.org Git - factor.git/blob - extra/ui/cocoa/cocoa.factor
Added set-fullscreen? and fullscreen? hooks along with their cocoa implementations.
[factor.git] / extra / ui / cocoa / cocoa.factor
1 ! Copyright (C) 2006, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: math arrays cocoa cocoa.application command-line
4 kernel memory namespaces cocoa.messages cocoa.runtime
5 cocoa.subclassing cocoa.pasteboard cocoa.types cocoa.windows
6 cocoa.classes cocoa.application sequences system ui ui.backend
7 ui.clipboards ui.gadgets ui.gadgets.worlds ui.cocoa.views
8 core-foundation threads ;
9 IN: ui.cocoa
10
11 TUPLE: cocoa-ui-backend ;
12
13 SYMBOL: stop-after-last-window?
14
15 : event-loop? ( -- ? )
16     stop-after-last-window? get-global
17     [ windows get-global empty? not ] [ t ] if ;
18
19 : event-loop ( -- )
20     event-loop? [
21         [
22             [ NSApp do-events ui-step 10 sleep ] ui-try
23         ] with-autorelease-pool event-loop
24     ] when ;
25
26 TUPLE: pasteboard handle ;
27
28 C: <pasteboard> pasteboard
29
30 M: pasteboard clipboard-contents
31     pasteboard-handle pasteboard-string ;
32
33 M: pasteboard set-clipboard-contents
34     pasteboard-handle set-pasteboard-string ;
35
36 : init-clipboard ( -- )
37     NSPasteboard -> generalPasteboard <pasteboard>
38     clipboard set-global
39     <clipboard> selection set-global ;
40
41 : world>NSRect ( world -- NSRect )
42     dup world-loc first2 rot rect-dim first2 <NSRect> ;
43
44 : gadget-window ( world -- )
45     [
46         dup <FactorView>
47         dup rot world>NSRect <ViewWindow>
48         dup install-window-delegate
49         over -> release
50         2array
51     ] keep set-world-handle ;
52
53 M: cocoa-ui-backend set-title ( string world -- )
54     world-handle second swap <NSString> -> setTitle: ;
55
56 : enter-fullscreen ( world -- )
57     world-handle first NSScreen -> mainScreen f -> enterFullScreenMode:withOptions: drop ;
58
59 : exit-fullscreen ( world -- )
60     world-handle first f -> exitFullScreenModeWithOptions: ;
61
62 M: cocoa-ui-backend set-fullscreen? ( ? world -- )
63     swap [ enter-fullscreen ] [ exit-fullscreen ] if ;
64
65 M: cocoa-ui-backend fullscreen? ( world -- ? )
66     world-handle first -> isInFullScreenMode zero? not ;
67
68 : auto-position ( world -- )
69     dup world-loc { 0 0 } = [
70         world-handle second -> center
71     ] [
72         drop
73     ] if ;
74
75 M: cocoa-ui-backend (open-window) ( world -- )
76     dup gadget-window
77     dup auto-position
78     world-handle second f -> makeKeyAndOrderFront: ;
79
80 M: cocoa-ui-backend (close-window) ( handle -- )
81     first unregister-window ;
82
83 M: cocoa-ui-backend close-window ( gadget -- )
84     find-world [
85         world-handle second f -> performClose:
86     ] when* ;
87
88 M: cocoa-ui-backend raise-window ( world -- )
89     world-handle [
90         second dup f -> orderFront: -> makeKeyWindow
91         NSApp 1 -> activateIgnoringOtherApps:
92     ] when* ;
93
94 M: cocoa-ui-backend select-gl-context ( handle -- )
95     first -> openGLContext -> makeCurrentContext ;
96
97 M: cocoa-ui-backend flush-gl-context ( handle -- )
98     first -> openGLContext -> flushBuffer ;
99
100 SYMBOL: cocoa-init-hook
101
102 M: cocoa-ui-backend ui
103     "UI" assert.app [
104         [
105             init-clipboard
106             cocoa-init-hook get [ call ] when*
107             start-ui
108             finish-launching
109             event-loop
110         ] ui-running
111     ] with-cocoa ;
112
113 T{ cocoa-ui-backend } ui-backend set-global
114
115 [ running.app? "ui" "listener" ? ] main-vocab-hook set-global