]> gitweb.factorcode.org Git - factor.git/blob - library/ui/cocoa/ui.factor
e7a455a517755e49e9c79a2c57e33f8e626a68d3
[factor.git] / library / ui / cocoa / ui.factor
1 ! Copyright (C) 2006 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: objc-classes
4 DEFER: FactorApplicationDelegate
5
6 IN: cocoa
7 USING: arrays gadgets gadgets-listener gadgets-workspace
8 hashtables kernel memory namespaces objc sequences errors freetype ;
9
10 : finder-run-files ( alien -- )
11     #! We filter out the image name since that might be there on
12     #! first launch.
13     CF>string-array [ image = not ] subset listener-run-files
14     NSApp NSApplicationDelegateReplySuccess
15     -> replyToOpenOrPrint: ;
16
17 ! Handle Open events from the Finder
18 "NSObject" "FactorApplicationDelegate" {
19     { "application:openFiles:" "void" { "id" "SEL" "id" "id" }
20         [ >r 3drop r> finder-run-files ]
21     }
22 } define-objc-class
23
24 : install-app-delegate ( -- )
25     NSApp FactorApplicationDelegate install-delegate ;
26
27 : init-cocoa ( -- )
28     reset-callbacks
29     install-app-delegate
30     register-services
31     init-clipboard
32     default-main-menu ;
33
34 : rect>NSRect
35     dup world-loc first2 rot rect-dim first2 <NSRect> ;
36
37 : gadget-window ( world -- )
38     [
39         dup <FactorView>
40         dup rot rect>NSRect <ViewWindow>
41         dup install-window-delegate
42         over -> release
43         2array
44     ] keep set-world-handle ;
45
46 IN: gadgets
47
48 : set-title ( string world -- )
49     world-handle second swap <NSString> -> setTitle: ;
50
51 : open-window* ( world -- )
52     dup gadget-window
53     dup start-world
54     world-handle second f -> makeKeyAndOrderFront: ;
55
56 : raise-window ( world -- )
57     world-handle second dup f -> orderFront: -> makeKeyWindow ;
58
59 : select-gl-context ( handle -- )
60     first -> openGLContext -> makeCurrentContext ;
61
62 : flush-gl-context ( handle -- )
63     first -> openGLContext -> flushBuffer ;
64
65 : running.app? ( -- ? )
66     #! Test if we're running Factor.app.
67     "Factor.app"
68     NSBundle -> mainBundle -> bundlePath CF>string
69     subseq? ;
70
71 IN: shells
72
73 : ui
74     running.app? [
75         "The Factor UI requires you to run the supplied Factor.app." throw
76     ] unless
77     [
78         [
79             init-timers
80             init-cocoa
81             restore-windows? [
82                 restore-windows
83             ] [
84                 init-ui
85                 workspace-window
86                 drop
87             ] if
88             finish-launching
89             event-loop
90         ] with-cocoa
91     ] with-freetype ;
92
93 IN: command-line
94
95 : default-shell running.app? "ui" "tty" ? ;