1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*
StoreBitLib.m
*/
#include <Carbon/Carbon.h>
#include <Cocoa/Cocoa.h>
//local function prototypes
static OSStatus LoadFrameworkBundle(CFStringRef framework, CFBundleRef *bundlePtr);
//file static variables
static NSNumber *numberObject=NULL;
/*
Needed to make sure the Cocoa framework has a chance to initialize things in a Carbon app.
awi: Simplified Apple's version by removing conditional for pre 10.2.
*/
typedef BOOL (*NSApplicationLoadFuncPtr)( void );
void InitializeCocoa()
{
CFBundleRef appKitBundleRef;
OSStatus err;
NSApplication *NSApp;
// Load the "AppKit.framework" bundl to locate NSApplicationLoad
err = LoadFrameworkBundle( CFSTR("AppKit.framework"), &appKitBundleRef );
NSApp=[NSApplication sharedApplication];
}
void CocoaStoreBit(Boolean newValue)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if(numberObject != NULL)
[numberObject release];
numberObject=[[NSNumber alloc] initWithBool:newValue];
[pool release];
}
Boolean CocoaGetBit(void)
{
Boolean returnValue;
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if(numberObject == NULL)
returnValue=FALSE;
else
returnValue=[numberObject boolValue];
[pool release];
return(returnValue);
}
void CocoaFreeBit(void)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
if(numberObject!=NULL){
[numberObject release];
numberObject=NULL;
}
[pool release];
}
static OSStatus LoadFrameworkBundle(CFStringRef framework, CFBundleRef *bundlePtr)
{
OSStatus err;
FSRef frameworksFolderRef;
CFURLRef baseURL;
CFURLRef bundleURL;
if ( bundlePtr == nil ) return( -1 );
*bundlePtr = nil;
baseURL = nil;
bundleURL = nil;
err = FSFindFolder(kOnAppropriateDisk, kFrameworksFolderType, true, &frameworksFolderRef);
if (err == noErr) {
baseURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &frameworksFolderRef);
if (baseURL == nil) {
err = coreFoundationUnknownErr;
}
}
if (err == noErr) {
bundleURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorSystemDefault, baseURL, framework, false);
if (bundleURL == nil) {
err = coreFoundationUnknownErr;
}
}
if (err == noErr) {
*bundlePtr = CFBundleCreate(kCFAllocatorSystemDefault, bundleURL);
if (*bundlePtr == nil) {
err = coreFoundationUnknownErr;
}
}
if (err == noErr) {
if ( ! CFBundleLoadExecutable( *bundlePtr ) ) {
err = coreFoundationUnknownErr;
}
}
// Clean up.
if (err != noErr && *bundlePtr != nil) {
CFRelease(*bundlePtr);
*bundlePtr = nil;
}
if (bundleURL != nil) {
CFRelease(bundleURL);
}
if (baseURL != nil) {
CFRelease(baseURL);
}
return err;
}
|