]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/application/application.factor
3216427a8c3caa7f3c1594900be3bed8cffb2fb9
[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.c-types alien.syntax cocoa cocoa.classes
4 cocoa.runtime core-foundation.strings kernel sequences ;
5 IN: cocoa.application
6
7 : <NSString> ( str -- alien ) <CFString> -> autorelease ;
8
9 CONSTANT: NSApplicationDelegateReplySuccess 0
10 CONSTANT: NSApplicationDelegateReplyCancel  1
11 CONSTANT: NSApplicationDelegateReplyFailure 2
12
13 : with-autorelease-pool ( quot -- )
14     NSAutoreleasePool -> new [ call ] [ -> release ] bi* ; inline
15
16 : NSApp ( -- app ) NSApplication -> sharedApplication ;
17
18 CONSTANT: NSAnyEventMask 0xffffffff
19
20 FUNCTION: void NSBeep ( )
21
22 : with-cocoa ( quot -- )
23     [ NSApp drop call ] with-autorelease-pool ; inline
24
25 : add-observer ( observer selector name object -- )
26     [
27         [ NSNotificationCenter -> defaultCenter ] 2dip
28         sel_registerName
29     ] 2dip -> addObserver:selector:name:object: ;
30
31 : remove-observer ( observer -- )
32     [ NSNotificationCenter -> defaultCenter ] dip
33     -> removeObserver: ;
34
35 : cocoa-app ( quot -- )
36     [ call NSApp -> run ] with-cocoa ; inline
37
38 : install-delegate ( receiver delegate -- )
39     -> alloc -> init -> setDelegate: ;
40
41 : running.app? ( -- ? )
42     ! Test if we're running a .app.
43     NSBundle -> mainBundle -> bundlePath CF>string
44     ".app" subseq-index? ;
45
46 : assert.app ( message -- )
47     running.app? [
48         drop
49     ] [
50         "The " " requires you to run Factor from an application bundle."
51         surround throw
52     ] if ;