1 ! Copyright (C) 2006, 2007 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien io kernel namespaces core-foundation cocoa.messages
4 cocoa cocoa.classes cocoa.runtime sequences threads debugger
5 init inspector kernel.private ;
8 : NSApplicationDelegateReplySuccess 0 ;
9 : NSApplicationDelegateReplyCancel 1 ;
10 : NSApplicationDelegateReplyFailure 2 ;
12 : with-autorelease-pool ( quot -- )
13 NSAutoreleasePool -> new slip -> release ; inline
15 : NSApp ( -- app ) NSApplication -> sharedApplication ;
17 : with-cocoa ( quot -- )
18 [ NSApp drop call ] with-autorelease-pool ;
20 : CFRunLoopDefaultMode "kCFRunLoopDefaultMode" <NSString> ;
22 : next-event ( app -- event )
23 0 f CFRunLoopDefaultMode 1
24 -> nextEventMatchingMask:untilDate:inMode:dequeue: ;
26 : do-event ( app -- ? )
27 dup next-event [ -> sendEvent: t ] [ drop f ] if* ;
29 : do-events ( app -- )
30 dup do-event [ do-events ] [ drop ] if ;
32 : add-observer ( observer selector name object -- )
33 >r >r >r >r NSNotificationCenter -> defaultCenter
34 r> r> sel_registerName
35 r> r> -> addObserver:selector:name:object: ;
37 : remove-observer ( observer -- )
38 >r NSNotificationCenter -> defaultCenter r>
41 : finish-launching ( -- ) NSApp -> finishLaunching ;
43 : install-delegate ( receiver delegate -- )
44 -> alloc -> init -> setDelegate: ;
46 TUPLE: objc-error alien reason ;
48 : objc-error ( alien -- * )
49 dup -> reason CF>string \ objc-error construct-boa throw ;
51 M: objc-error summary ( error -- )
52 drop "Objective C exception" ;
54 [ [ objc-error ] 19 setenv ] "cocoa.application" add-init-hook
56 : running.app? ( -- ? )
57 #! Test if we're running a .app.
59 NSBundle -> mainBundle -> bundlePath CF>string
62 : assert.app ( message -- )
66 "The " swap " requires you to run Factor from an application bundle."