]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/application/application.factor
use radix literals
[factor.git] / basis / cocoa / application / application.factor
1 ! Copyright (C) 2006, 2010 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.syntax io kernel namespaces
4 core-foundation core-foundation.strings cocoa.messages cocoa
5 cocoa.classes cocoa.runtime sequences init summary
6 kernel.private 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 0xffffffff
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 : running.app? ( -- ? )
44     #! Test if we're running a .app.
45     ".app"
46     NSBundle -> mainBundle -> bundlePath CF>string
47     subseq? ;
48
49 : assert.app ( message -- )
50     running.app? [
51         drop
52     ] [
53         "The " " requires you to run Factor from an application bundle."
54         surround throw
55     ] if ;