]> gitweb.factorcode.org Git - factor.git/blob - core/ui/cocoa/core-foundation.factor
08ca9bbdba7f0aff105e978598c13281191a91ca
[factor.git] / core / ui / cocoa / core-foundation.factor
1 ! Copyright (C) 2006 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: cocoa
4 USING: alien arrays errors hashtables kernel math memory
5 namespaces sequences ;
6
7 TYPEDEF: int CFIndex
8
9 FUNCTION: void* CFArrayCreateMutable ( void* allocator, CFIndex capacity, void* callbacks ) ;
10
11 FUNCTION: void* CFArrayGetValueAtIndex ( void* array, CFIndex idx ) ;
12
13 FUNCTION: void CFArraySetValueAtIndex ( void* array, CFIndex index, void* value ) ;
14
15 FUNCTION: CFIndex CFArrayGetCount ( void* array ) ;
16
17 : kCFURLPOSIXPathStyle 0 ;
18
19 FUNCTION: void* CFURLCreateWithFileSystemPath ( void* allocator, void* filePath, int pathStyle, bool isDirectory ) ;
20
21 FUNCTION: void* CFURLCreateWithString ( void* allocator, void* string, void* base ) ;
22
23 FUNCTION: void* CFURLCopyFileSystemPath ( void* url, int pathStyle ) ;
24
25 FUNCTION: void* CFStringCreateWithCharacters ( void* allocator, ushort* cStr, CFIndex numChars ) ;
26
27 FUNCTION: CFIndex CFStringGetLength ( void* theString ) ;
28
29 FUNCTION: void CFStringGetCharacters ( void* theString, CFIndex start, CFIndex length, void* buffer ) ;
30
31 FUNCTION: CFIndex CFStringGetLength ( void* string ) ;
32
33 FUNCTION: void* CFBundleCreate ( void* allocator, void* bundleURL ) ;
34
35 FUNCTION: bool CFBundleLoadExecutable ( void* bundle ) ;
36
37 FUNCTION: void CFRelease ( void* cf ) ;
38
39 : CF>array ( alien -- array )
40     dup CFArrayGetCount [ CFArrayGetValueAtIndex ] map-with ;
41
42 : <CFArray> ( seq -- array )
43     [ f swap length f CFArrayCreateMutable ] keep
44     [ length ] keep
45     [ >r dupd r> CFArraySetValueAtIndex ] 2each ;
46
47 : <CFString> ( string -- cf )
48     f swap dup length CFStringCreateWithCharacters ;
49
50 : CF>string ( string -- string )
51     dup CFStringGetLength 1+ "ushort" <c-array> [
52         >r 0 over CFStringGetLength r> CFStringGetCharacters
53     ] keep alien>u16-string ;
54
55 : CF>string-array ( alien -- seq )
56     CF>array [ CF>string ] map ;
57
58 : <CFFileSystemURL> ( string dir? -- cf )
59     >r <CFString> f over kCFURLPOSIXPathStyle
60     r> CFURLCreateWithFileSystemPath swap CFRelease ;
61
62 : <CFURL> ( string -- cf )
63     <CFString>
64     [ f swap f CFURLCreateWithString ] keep
65     CFRelease ;
66
67 : <CFBundle> ( string -- cf )
68     t <CFFileSystemURL> [
69         f swap CFBundleCreate
70     ] keep CFRelease ;
71
72 : load-framework ( name -- )
73     dup <CFBundle> [
74         CFBundleLoadExecutable drop
75     ] [
76         "Cannot load bundled named " swap append throw
77     ] ?if ;