diff --git a/library/cocoa/application-utils.factor b/library/cocoa/application-utils.factor new file mode 100644 index 0000000000..cca37f254d --- /dev/null +++ b/library/cocoa/application-utils.factor @@ -0,0 +1,26 @@ +! Copyright (C) 2006 Slava Pestov +! See http://factorcode.org/license.txt for BSD license. +IN: cocoa +USING: errors kernel objc-NSApplication objc-NSAutoreleasePool +objc-NSObject threads ; + +: with-autorelease-pool ( quot -- ) + NSAutoreleasePool [new] [ + [ call ] keep + ] [ + drop [release] + ] cleanup ; inline + +: [autorelease] ; + +: next-event ( app -- ) + 0 f "NSDefaultRunLoopMode" 1 + [nextEventMatchingMask:untilDate:inMode:dequeue:] ; + +: do-events ( app -- ) + dup dup next-event [ [sendEvent:] do-events ] [ drop ] if* ; + +: event-loop ( -- ) + [ + NSApplication [sharedApplication] do-events + ] with-autorelease-pool 10 sleep event-loop ; diff --git a/library/cocoa/cocoa-types.factor b/library/cocoa/cocoa-types.factor new file mode 100644 index 0000000000..a106ce6329 --- /dev/null +++ b/library/cocoa/cocoa-types.factor @@ -0,0 +1,44 @@ +! Copyright (C) 2006 Slava Pestov +! See http://factorcode.org/license.txt for BSD license. +IN: cocoa +USING: alien kernel ; + +BEGIN-STRUCT: NSRect + FIELD: float x + FIELD: float y + FIELD: float w + FIELD: float h +END-STRUCT + +TYPEDEF: NSRect _NSRect + +: + "NSRect" + [ set-NSRect-h ] keep + [ set-NSRect-w ] keep + [ set-NSRect-y ] keep + [ set-NSRect-x ] keep ; + +BEGIN-STRUCT: NSPoint + FIELD: float x + FIELD: float y +END-STRUCT + +TYPEDEF: NSPoint _NSPoint + +: + "NSPoint" + [ set-NSPoint-y ] keep + [ set-NSPoint-x ] keep ; + +BEGIN-STRUCT: NSSize + FIELD: float w + FIELD: float h +END-STRUCT + +TYPEDEF: NSSize _NSSize + +: + "NSSize" + [ set-NSSize-h ] keep + [ set-NSSize-w ] keep ; diff --git a/library/cocoa/core-foundation.factor b/library/cocoa/core-foundation.factor new file mode 100644 index 0000000000..875924c443 --- /dev/null +++ b/library/cocoa/core-foundation.factor @@ -0,0 +1,48 @@ +! Copyright (C) 2006 Slava Pestov +! See http://factorcode.org/license.txt for BSD license. +IN: cocoa +USING: hashtables kernel namespaces ; + +! Core Foundation utilities -- will be moved elsewhere +: kCFURLPOSIXPathStyle 0 ; + +: kCFStringEncodingMacRoman 0 ; + +FUNCTION: void* CFURLCreateWithFileSystemPath ( void* allocator, void* filePath, int pathStyle, bool isDirectory ) ; + +FUNCTION: void* CFURLCreateWithString ( void* allocator, void* string, void* base ) ; + +FUNCTION: void* CFStringCreateWithCString ( void* allocator, char* cStr, int encoding ) ; + +FUNCTION: void* CFBundleCreate ( void* allocator, void* bundleURL ) ; + +FUNCTION: void* CFBundleGetFunctionPointerForName ( void* bundle, void* functionName ) ; + +FUNCTION: bool CFBundleLoadExecutable ( void* bundle ) ; + +FUNCTION: void CFRelease ( void* cf ) ; + +: ( string -- cf ) + f swap kCFStringEncodingMacRoman CFStringCreateWithCString ; + +: ( string dir? -- cf ) + >r f over kCFURLPOSIXPathStyle + r> CFURLCreateWithFileSystemPath swap CFRelease ; + +: ( string -- cf ) + + [ f swap f CFURLCreateWithString ] keep + CFRelease ; + +: ( string -- cf ) + t f over CFBundleCreate swap CFRelease ; + +! A rough framework loader. +SYMBOL: frameworks + +: load-framework ( name -- ) + frameworks get [ + dup CFBundleLoadExecutable drop + ] cache drop ; + +H{ } clone frameworks set-global diff --git a/library/cocoa/init-cocoa.factor b/library/cocoa/init-cocoa.factor new file mode 100644 index 0000000000..1db6d1e903 --- /dev/null +++ b/library/cocoa/init-cocoa.factor @@ -0,0 +1,38 @@ +! Copyright (C) 2006 Slava Pestov +! See http://factorcode.org/license.txt for BSD license. +USING: cocoa compiler io kernel objc sequences words ; + +"Compiling Objective C bridge..." print + +{ "cocoa" "objc" } compile-vocabs + +"Loading Cocoa frameworks..." print +{ + "/System/Library/Frameworks/WebKit.framework" + "/System/Library/Frameworks/QTKit.framework" + "/System/Library/Frameworks/Quartz.framework/Frameworks/PDFKit.framework" +} [ + dup print flush load-framework +] each + +"Importing Cocoa classes..." print +{ + "NSApplication" + "NSAutoreleasePool" + "NSDate" + "NSEvent" + "NSInvocation" + "NSMethodSignature" + "NSObject" + "NSOpenGLView" + "NSSpeechSynthesizer" + "NSURLRequest" + "NSWindow" + "PDFView" + "QTMovie" + "QTMovieView" + "WebFrame" + "WebView" +} [ + dup print flush define-objc-class +] each diff --git a/library/cocoa/load.factor b/library/cocoa/load.factor new file mode 100644 index 0000000000..dd41ed1b7b --- /dev/null +++ b/library/cocoa/load.factor @@ -0,0 +1,16 @@ +USING: compiler io parser sequences words ; + +{ + "/library/cocoa/objc-runtime.factor" + "/library/cocoa/objc-utils.factor" + "/library/cocoa/core-foundation.factor" + "/library/cocoa/cocoa-types.factor" + "/library/cocoa/init-cocoa.factor" + "/library/cocoa/application-utils.factor" + "/library/cocoa/window-utils.factor" +} [ + run-resource +] each + +"Compiling Cocoa bindings..." print +vocabs [ "objc-" head? ] subset compile-vocabs diff --git a/library/cocoa/window-utils.factor b/library/cocoa/window-utils.factor new file mode 100644 index 0000000000..2351c636f7 --- /dev/null +++ b/library/cocoa/window-utils.factor @@ -0,0 +1,25 @@ +! Copyright (C) 2006 Slava Pestov +! See http://factorcode.org/license.txt for BSD license. +IN: cocoa +USING: kernel math objc-NSObject objc-NSWindow ; + +: NSBorderlessWindowMask 0 ; inline +: NSTitledWindowMask 1 ; inline +: NSClosableWindowMask 2 ; inline +: NSMiniaturizableWindowMask 4 ; inline +: NSResizableWindowMask 8 ; inline + +: NSBackingStoreRetained 0 ; inline +: NSBackingStoreNonretained 1 ; inline +: NSBackingStoreBuffered 2 ; inline + +: standard-window-type + NSTitledWindowMask + NSClosableWindowMask bitor + NSMiniaturizableWindowMask bitor + NSResizableWindowMask bitor ; inline + +: ( title rect type -- window ) + NSWindow [alloc] -rot NSBackingStoreBuffered 1 + [initWithContentRect:styleMask:backing:defer:] + [ swap [setTitle:] ] keep ;