view MyApp.m @ 6:ea0dcbf6a0bc

Converted app to objective C
author Atul Varma <avarma@mozilla.com>
date Sun, 11 Apr 2010 18:33:24 -0700
parents 620147798b16
children
line wrap: on
line source

#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSURLRequest.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSColor.h>
#import <WebKit/WebView.h>
#import <WebKit/WebFrame.h>
#import <WebKit/WebFrameView.h>
#import <WebKit/DOMEvents.h>
#import <JavaScriptCore/JavaScriptCore.h>

JSValueRef boop(JSContextRef ctx,
                JSObjectRef function,
                JSObjectRef thisObject,
                size_t argumentCount,
                const JSValueRef arguments[],
                JSValueRef* exception)
{
  if (argumentCount >= 1 &&
      JSValueIsNumber(ctx, arguments[0])) {
    printf("BOOP IS CALLED WITH %f\n",
           JSValueToNumber(ctx, arguments[0], NULL));
  }
  return JSValueMakeUndefined(ctx);
}

@interface MyApp : NSObject {
  NSWindow *window;
  WebView *view;
}
- (id)init;
@end

@implementation MyApp
- (void)defineJSGlobals {
  JSGlobalContextRef ctx = [[view mainFrame] globalContext];

  JSObjectRef global = JSContextGetGlobalObject(ctx);
  JSStringRef boopStr = JSStringCreateWithUTF8CString("boop");
  JSObjectRef func = JSObjectMakeFunctionWithCallback(ctx, boopStr, boop);
  JSObjectSetProperty(ctx, global, boopStr, func, NULL,
                      kJSPropertyAttributeNone);
}

- (id)init {
  if (self = [super init]) {
    NSRect rect = {{20, 20},
                   {100, 100}};

    window = [[NSWindow alloc] initWithContentRect: rect
                               styleMask: NSBorderlessWindowMask
                               backing: NSBackingStoreBuffered
                               defer: YES];

    view = [[WebView alloc] initWithFrame: rect
                            frameName: nil
                            groupName: nil];

    NSURL *url = [NSURL URLWithString: @"http://127.0.0.1:8000"];
    NSURLRequest *req = [
      NSURLRequest requestWithURL: url
      cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
      timeoutInterval: 5
    ];

    [view setDrawsBackground: NO];
    [[[view mainFrame] frameView] setAllowsScrolling: NO];
    [[view mainFrame] loadRequest: req];

    [self defineJSGlobals];

    [window setOpaque: NO];
    [window setBackgroundColor: [NSColor colorWithCalibratedRed: 0.0
                                         green: 0.0
                                         blue: 0.0
                                         alpha: 0.0]];
    [window setContentView: view];
    [window setLevel: NSScreenSaverWindowLevel];
    [window makeKeyAndOrderFront: self];
  }
  return self;
}
@end

int main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  [NSApplication sharedApplication];

  MyApp *app = [[MyApp alloc] init];

  [NSApp run];

  return 0;
}