Home | History | Annotate | Download | only in mac
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 #include <crt_externs.h>
     10 #import <Cocoa/Cocoa.h>
     11 #include "SkApplication.h"
     12 #include "SkGraphics.h"
     13 #include "SkNSView.h"
     14 
     15 @interface MainView : SkNSView {
     16 }
     17 - (id)initWithFrame: (NSRect)frame ;
     18 - (void)dealloc;
     19 - (void)begin;
     20 @end
     21 
     22 @implementation MainView : SkNSView
     23 
     24 - (id)initWithFrame: (NSRect)frame {
     25     self = [super initWithFrame:frame];
     26     return self;
     27 }
     28 
     29 - (void)dealloc {
     30     delete self.fWind;
     31     [super dealloc];
     32 }
     33 
     34 - (void)begin {
     35     self.fWind = create_sk_window(self, *_NSGetArgc(), *_NSGetArgv());
     36     [self setUpWindow];
     37 }
     38 @end
     39 
     40 @interface AppDelegate : NSObject<NSApplicationDelegate, NSWindowDelegate> {
     41 }
     42 - (id)init;
     43 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
     44 @end
     45 
     46 #
     47 @implementation AppDelegate : NSObject
     48 - (id)init {
     49     self = [super init];
     50     return self;
     51 }
     52 
     53 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
     54     return TRUE;
     55 }
     56 @end
     57 
     58 int main(int argc, char *argv[]) {
     59     SkGraphics::Init();
     60     signal(SIGPIPE, SIG_IGN);
     61     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     62 
     63     NSApplication* app = [NSApplication sharedApplication];
     64 
     65     NSUInteger windowStyle = (NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask);
     66 
     67     NSRect windowRect = NSMakeRect(100, 100, 1000, 1000);
     68     NSWindow* window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:windowStyle backing:NSBackingStoreBuffered defer:NO];
     69 
     70     NSRect rect = [NSWindow contentRectForFrameRect:windowRect styleMask:windowStyle];
     71     MainView* customView = [[MainView alloc] initWithFrame:rect];
     72     [customView setTranslatesAutoresizingMaskIntoConstraints:NO];
     73     NSView* contentView = window.contentView;
     74     [contentView addSubview:customView];
     75     NSDictionary *views = NSDictionaryOfVariableBindings(customView);
     76 
     77     [contentView addConstraints:
     78      [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|"
     79                                              options:0
     80                                              metrics:nil
     81                                                views:views]];
     82 
     83     [contentView addConstraints:
     84      [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|"
     85                                              options:0
     86                                              metrics:nil
     87                                                views:views]];
     88 
     89     [customView begin];
     90     [customView release];
     91 
     92     [window makeKeyAndOrderFront:NSApp];
     93 
     94     AppDelegate * appDelegate = [[[AppDelegate alloc] init] autorelease];
     95 
     96     app.delegate = appDelegate;
     97 
     98     NSMenu* menu=[[NSMenu alloc] initWithTitle:@"AMainMenu"];
     99     NSMenuItem* item;
    100     NSMenu* subMenu;
    101 
    102     //Create the application menu.
    103     item=[[NSMenuItem alloc] initWithTitle:@"Apple" action:NULL keyEquivalent:@""];
    104     [menu addItem:item];
    105     subMenu=[[NSMenu alloc] initWithTitle:@"Apple"];
    106     [menu setSubmenu:subMenu forItem:item];
    107     [item release];
    108     item=[[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
    109     [subMenu addItem:item];
    110     [item release];
    111     [subMenu release];
    112 
    113     //Add the menu to the app.
    114     [app setMenu:menu];
    115 
    116     [app setActivationPolicy:NSApplicationActivationPolicyRegular];
    117 
    118     [app run];
    119 
    120     [menu release];
    121     [appDelegate release];
    122     [window release];
    123     [pool release];
    124 
    125     return EXIT_SUCCESS;
    126 }
    127