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
9 : <NSString> ( str -- alien ) <CFString> -> autorelease ;
11 CONSTANT: NSApplicationDelegateReplySuccess 0
12 CONSTANT: NSApplicationDelegateReplyCancel 1
13 CONSTANT: NSApplicationDelegateReplyFailure 2
15 : with-autorelease-pool ( quot -- )
16 NSAutoreleasePool -> new [ call ] [ -> release ] bi* ; inline
18 : NSApp ( -- app ) NSApplication -> sharedApplication ;
20 CONSTANT: NSAnyEventMask HEX: ffffffff
22 FUNCTION: void NSBeep ( ) ;
24 : with-cocoa ( quot -- )
25 [ NSApp drop call ] with-autorelease-pool ; inline
27 : add-observer ( observer selector name object -- )
29 [ NSNotificationCenter -> defaultCenter ] 2dip
31 ] 2dip -> addObserver:selector:name:object: ;
33 : remove-observer ( observer -- )
34 [ NSNotificationCenter -> defaultCenter ] dip
37 : cocoa-app ( quot -- )
38 [ call NSApp -> run ] with-cocoa ; inline
40 : install-delegate ( receiver delegate -- )
41 -> alloc -> init -> setDelegate: ;
43 TUPLE: objc-error alien reason ;
45 : objc-error ( alien -- * )
46 dup -> reason CF>string \ objc-error boa throw ;
48 M: objc-error summary ( error -- )
49 drop "Objective C exception" ;
51 [ [ objc-error ] 19 set-special-object ] "cocoa.application" add-startup-hook
53 : running.app? ( -- ? )
54 #! Test if we're running a .app.
56 NSBundle -> mainBundle -> bundlePath CF>string
59 : assert.app ( message -- )
63 "The " " requires you to run Factor from an application bundle."