/* The begining of ObjC support for PLT Scheme. */

/*  Tested on OS 10.5  -  July 2008
   gcc -g -framework Foundation -framework Cocoa -dynamiclib -o objc-support.dylib -fvisibility=default  objc-support.m 
*/

#import <Foundation/Foundation.h>
#import <objc/objc-runtime.h>
#import <Cocoa/Cocoa.h>

static NSAutoreleasePool *default_pool = NULL;

char *objc_description(void *s) {
  return [[(NSObject *)s description] UTF8String];
}

void *string_to_class(char *name) {
  NSString *str = [[NSString alloc] initWithUTF8String: name];
  void *clazz = (void *) NSClassFromString(str);
  [str release];
  return clazz;
}

void *string_to_selector(char *name) {
  NSString *str = [[NSString alloc] initWithUTF8String: name];
  SEL s = (void *)NSSelectorFromString(str);
  [str release];
  return (void *)s;
}

char *selector_chars(void *sel) {
  SEL s = (SEL)sel;
  return (char *)s;
}

void *selector_to_signature(void *x, void *sel, bool clazz) {
  NSObject *o = (NSObject *)x;
  if (clazz) return (void *)[o methodSignatureForSelector: (SEL)sel];
  else return (void *)[[o class] instanceMethodSignatureForSelector: (SEL)sel];
}

char *method_return_type(void *sig) {
  NSMethodSignature *s = (NSMethodSignature *)sig;
  return [s methodReturnType];
}

int method_argument_count(void *sig) {
  NSMethodSignature *s = (NSMethodSignature *)sig;
  return [s numberOfArguments];
}

char *method_argument_type(void *sig, int i) {
  NSMethodSignature *s = (NSMethodSignature *)sig;
  return [s getArgumentTypeAtIndex: i];
}

void *make_nsstring(char *s) {
  return [[NSString alloc] initWithUTF8String: s];
}

bool is_nsstring(void *o) {
  NSObject *ob = (NSObject *)o;
  return [ob isKindOfClass: [NSString class]];
}

void objc_release(void *o) {
  NSObject *obj = (NSObject *)o;
  [obj release];
}

void objc_allocate_autorelease_pool () {
    default_pool = [[NSAutoreleasePool alloc] init];
}


int objc_NSApplicationMain() {
  [NSApplication sharedApplication];

  /* The following ought to load the nib-file MainMenu.nib, but doesn't ... */
  return [NSBundle loadNibNamed:@"MainMenu" owner:NSApp]; 

  /*[NSApp run]; 


    /* ALTERNATIVE APPROACH:

      const char *args[10];
      args[0]= "/Users/jensaxelsoegaard/Work/objc/build/Release/Currency Converter.app/Contents/MacOS/Currency Converter";
      return NSApplicationMain(0, args);
    */
}



