diff --git a/examples/cocoa-webkit.factor b/examples/cocoa-webkit.factor new file mode 100644 index 0000000000..8585126f8f --- /dev/null +++ b/examples/cocoa-webkit.factor @@ -0,0 +1,111 @@ +IN: webkit-test +USING: alien compiler io kernel math objc parser sequences ; + +! Very rough Cocoa bridge demo. + +! Problems: +! - does not release many objects; need to support autorelease +! - you cannot return to the REPL after, because we don't +! support Cocoa events yet. + +! Core Foundation utilities -- will be moved elsewhere +: kCFURLPOSIXPathStyle 0 ; + +: kCFStringEncodingMacRoman 0 ; + +FUNCTION: void* CFURLCreateWithFileSystemPath ( void* allocator, void* filePath, int pathStyle, bool isDirectory ) ; compiled + +FUNCTION: void* CFURLCreateWithString ( void* allocator, void* string, void* base ) ; compiled + +FUNCTION: void* CFStringCreateWithCString ( void* allocator, char* cStr, int encoding ) ; compiled + +FUNCTION: void* CFBundleCreate ( void* allocator, void* bundleURL ) ; compiled + +FUNCTION: void* CFBundleGetFunctionPointerForName ( void* bundle, void* functionName ) ; compiled + +FUNCTION: bool CFBundleLoadExecutable ( void* bundle ) ; compiled + +FUNCTION: void CFRelease ( void* cf ) ; compiled + +: ( 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 ; + +! Cocoa, WebKit classes and messages + +! We do this at parse time so that the following code can see +! the new words +: init-cocoa + "/System/Library/Frameworks/WebKit.framework" + CFBundleLoadExecutable drop + { + "NSObject" "NSWindow" + "NSURLRequest" "NSApplication" "%NSURL" + "WebView" "WebFrame" + } [ dup define-objc-class "objc-" swap append use+ ] each ; + parsing + +init-cocoa + +! This will move elsewhere really soon... +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 ; + +: NSBorderlessWindowMask 0 ; inline +: NSTitledWindowMask 1 ; inline +: NSClosableWindowMask 2 ; inline +: NSMiniaturizableWindowMask 4 ; inline +: NSResizableWindowMask 8 ; inline + +: NSBackingStoreRetained 0 ; inline +: NSBackingStoreNonretained 1 ; inline +: NSBackingStoreBuffered 2 ; inline + +: ( string -- id ) + NSURLRequest swap [requestWithURL:] ; + +! The ugliest colon definition ever +: webkit-test + NSWindow [alloc] + 10 10 600 600 + NSTitledWindowMask NSClosableWindowMask NSMiniaturizableWindowMask NSResizableWindowMask bitor bitor bitor + NSBackingStoreBuffered 1 [initWithContentRect:styleMask:backing:defer:] + dup "Hello world" [setTitle:] + dup + + WebView [alloc] 10 10 600 600 f f [initWithFrame:frameName:groupName:] + + dup [mainFrame] "http://factorcode.org" [loadRequest:] + + [setContentView:] + + dup f [makeKeyAndOrderFront:] + NSApplication [sharedApplication] [run] ; + +\ webkit-test compile + +webkit-test