]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/application/application.factor
Remove ENUM: f and replace uses with CONSTANTs.
[factor.git] / basis / cocoa / application / application.factor
1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.syntax io kernel namespaces core-foundation
4 core-foundation.strings cocoa.messages cocoa cocoa.classes
5 cocoa.runtime sequences init summary kernel.private
6 assocs ;
7 IN: cocoa.application
8
9 : <NSString> ( str -- alien ) <CFString> -> autorelease ;
10
11 CONSTANT: NSApplicationDelegateReplySuccess 0
12 CONSTANT: NSApplicationDelegateReplyCancel  1
13 CONSTANT: NSApplicationDelegateReplyFailure 2
14
15 : with-autorelease-pool ( quot -- )
16     NSAutoreleasePool -> new [ call ] [ -> release ] bi* ; inline
17
18 : NSApp ( -- app ) NSApplication -> sharedApplication ;
19
20 CONSTANT: NSAnyEventMask HEX: ffffffff
21
22 FUNCTION: void NSBeep ( ) ;
23
24 : with-cocoa ( quot -- )
25     [ NSApp drop call ] with-autorelease-pool ; inline
26
27 : add-observer ( observer selector name object -- )
28     [
29         [ NSNotificationCenter -> defaultCenter ] 2dip
30         sel_registerName
31     ] 2dip -> addObserver:selector:name:object: ;
32
33 : remove-observer ( observer -- )
34     [ NSNotificationCenter -> defaultCenter ] dip
35     -> removeObserver: ;
36
37 : cocoa-app ( quot -- )
38     [ call NSApp -> run ] with-cocoa ; inline
39
40 : install-delegate ( receiver delegate -- )
41     -> alloc -> init -> setDelegate: ;
42
43 TUPLE: objc-error alien reason ;
44
45 : objc-error ( alien -- * )
46     dup -> reason CF>string \ objc-error boa throw ;
47
48 M: objc-error summary ( error -- )
49     drop "Objective C exception" ;
50
51 [ [ objc-error ] 19 set-special-object ] "cocoa.application" add-startup-hook
52
53 : running.app? ( -- ? )
54     #! Test if we're running a .app.
55     ".app"
56     NSBundle -> mainBundle -> bundlePath CF>string
57     subseq? ;
58
59 : assert.app ( message -- )
60     running.app? [
61         drop
62     ] [
63         "The " " requires you to run Factor from an application bundle."
64         surround throw
65     ] if ;