]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/tools/tools.factor
Merge branch 'master' into startup
[factor.git] / basis / ui / backend / cocoa / tools / tools.factor
1 ! Copyright (C) 2006, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.syntax cocoa cocoa.nibs cocoa.application
4 cocoa.classes cocoa.dialogs cocoa.pasteboard cocoa.runtime
5 cocoa.subclassing core-foundation core-foundation.strings
6 help.topics kernel memory namespaces parser system ui
7 ui.tools.browser ui.tools.listener ui.backend.cocoa eval
8 locals vocabs.refresh ;
9 FROM: alien.c-types => int void ;
10 IN: ui.backend.cocoa.tools
11
12 : finder-run-files ( alien -- )
13     CF>string-array listener-run-files
14     NSApp NSApplicationDelegateReplySuccess
15     -> replyToOpenOrPrint: ;
16
17 : menu-run-files ( -- )
18     open-panel [ listener-run-files ] when* ;
19
20 : menu-save-image ( -- )
21     image save-panel [ save-image ] when* ;
22
23 ! Handle Open events from the Finder
24 CLASS: {
25     { +superclass+ "FactorApplicationDelegate" }
26     { +name+ "FactorWorkspaceApplicationDelegate" }
27 }
28
29 { "application:openFiles:" void { id SEL id id }
30     [ [ 3drop ] dip finder-run-files ]
31 }
32
33 { "applicationShouldHandleReopen:hasVisibleWindows:" int { id SEL id int }
34     [ [ 3drop ] dip 0 = [ show-listener ] when 1 ]
35 }
36
37 { "factorListener:" id { id SEL id }
38     [ 3drop show-listener f ]
39 }
40
41 { "factorBrowser:" id { id SEL id }
42     [ 3drop show-browser f ]
43 }
44
45 { "newFactorListener:" id { id SEL id }
46     [ 3drop listener-window f ]
47 }
48
49 { "newFactorBrowser:" id { id SEL id }
50     [ 3drop browser-window f ]
51 }
52
53 { "runFactorFile:" id { id SEL id }
54     [ 3drop menu-run-files f ]
55 }
56
57 { "saveFactorImage:" id { id SEL id }
58     [ 3drop save f ]
59 }
60
61 { "saveFactorImageAs:" id { id SEL id }
62     [ 3drop menu-save-image f ]
63 }
64
65 { "refreshAll:" id { id SEL id }
66     [ 3drop [ refresh-all ] \ refresh-all call-listener f ]
67 } ;
68
69 : install-app-delegate ( -- )
70     NSApp FactorWorkspaceApplicationDelegate install-delegate ;
71
72 ! Service support; evaluate Factor code from other apps
73 :: do-service ( pboard error quot -- )
74     pboard error ?pasteboard-string
75     dup [ quot call( string -- result/f ) ] when
76     [ pboard set-pasteboard-string ] when* ;
77
78 CLASS: {
79     { +superclass+ "NSObject" }
80     { +name+ "FactorServiceProvider" }
81 } {
82     "evalInListener:userData:error:"
83     void
84     { id SEL id id id }
85     [ nip [ eval-listener f ] do-service 2drop ]
86 } {
87     "evalToString:userData:error:"
88     void
89     { id SEL id id id }
90     [ nip [ eval>string ] do-service 2drop ]
91 } ;
92
93 : register-services ( -- )
94     NSApp
95     FactorServiceProvider -> alloc -> init
96     -> setServicesProvider: ;
97
98 FUNCTION: void NSUpdateDynamicServices ;
99
100 [
101     install-app-delegate
102     "Factor.nib" load-nib
103     register-services
104 ] cocoa-startup-hook set-global