#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>

#include <cairo.h>

#include "cairo-openstep.h"

#include <sys/types.h>

@interface AppDelegate : NSObject
{
  NSImage *nativeImage, *simulatedImage;
}
- (id) init;
- (void) applicationWillFinishLaunching: (NSNotification *) not;
- (void) applicationDidFinishLaunching: (NSNotification *) not;
@end

int main (int argc, const char *argv[])
{
  [NSApplication sharedApplication];
  [NSApp setDelegate: [[AppDelegate alloc] init]];

  return NSApplicationMain (argc, argv);
}


#define WIDTH 400
#define HEIGHT 400
#define TITLE @"Rectangles"

void draw(cairo_surface_t *surface)
{
  cairo_t *context = cairo_create(surface);
  cairo_pattern_t *pattern;
  //double dashes[4] = { 1.0, 2.0, 3.0, 4.0 };
  //cairo_set_dash(context, dashes, 4, 5);
  cairo_translate(context, WIDTH/2, HEIGHT/2);
  cairo_rotate(context, 0.25);
  cairo_translate(context, -WIDTH/2, -HEIGHT/2);
  cairo_rectangle(context, WIDTH/4, HEIGHT/4, WIDTH/2, HEIGHT/2);
  cairo_set_source_rgba(context, 1, 0.5, 0.5, 0.5);
  cairo_fill_preserve(context);
  cairo_set_source_rgba(context, 0, 1, 0, 1);
  cairo_stroke(context);
  cairo_set_operator(context, CAIRO_OPERATOR_SOURCE);
  //cairo_set_tolerance(context, 3);
  //cairo_rectangle(context, WIDTH*3/8, HEIGHT*3/8, WIDTH/2, HEIGHT/2);
  cairo_arc(context, WIDTH*5/8, HEIGHT*5/8, WIDTH/4, 0, 2*3.14159265);
  cairo_close_path(context);
  //cairo_set_source_rgba(context, 0, 0.5, 0.5, 0.5);
  pattern = cairo_pattern_create_linear(WIDTH*4/8, HEIGHT*4/8, WIDTH*6/8, HEIGHT*6/8);
  cairo_pattern_add_color_stop_rgba(pattern, 0, 0, 0.5, 0.5, 0.5);
  cairo_pattern_add_color_stop_rgba(pattern, 1, 1, 0.5, 0, 1);
  cairo_set_source(context, pattern);
  cairo_fill_preserve(context);
  cairo_pattern_destroy(pattern);
  //cairo_set_source_rgba(context, 0, 0, 1, 1);
  pattern = cairo_pattern_create_linear(WIDTH*4/8, HEIGHT*4/8, WIDTH*6/8, HEIGHT*6/8);
  cairo_pattern_add_color_stop_rgba(pattern, 0, 0.5, 0.5, 0, 0.5);
  cairo_pattern_add_color_stop_rgba(pattern, 1, 0, 0.5, 1, 1);
  cairo_set_source(context, pattern);
  cairo_stroke(context);
  cairo_pattern_destroy(pattern);
  cairo_destroy(context);
}


@implementation AppDelegate
- (void) dealloc
{
  [super dealloc];
}

- (id) init
{
  return self;
}

- (void) applicationWillFinishLaunching: (NSNotification *) not
{
  NSMenu *menu;
  menu = AUTORELEASE([NSMenu new]);
  [menu addItemWithTitle: @"Quit"
        action: @selector (terminate:)
        keyEquivalent: @"q"];
  [NSApp setMainMenu: menu];

  // set images
  nativeImage = [[NSImage alloc] initWithSize: NSMakeSize(WIDTH,HEIGHT)];
  NSBitmapImageRep *rep;

  cairo_surface_t *nativeSurface = cairo_openstep_surface_create(nativeImage);
  [nativeImage lockFocus];
  draw(nativeSurface);
  cairo_surface_finish(nativeSurface);

  rep = [[NSBitmapImageRep alloc]
	  initWithFocusedViewRect: NSMakeRect (0,0,WIDTH,HEIGHT)];
  [nativeImage unlockFocus];

  [nativeImage addRepresentation: rep];

  cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
							WIDTH, HEIGHT);
  draw(surface);
  simulatedImage = [[NSImage alloc] initWithSize: NSMakeSize(WIDTH,HEIGHT)];
  [simulatedImage addRepresentation:
		    [[NSBitmapImageRep alloc] initWithCairoSurface: surface]];
  cairo_surface_finish(surface);
}

- (void) applicationDidFinishLaunching: (NSNotification *) not
{
  NSWindow *window;
  NSImageView *nativeView, *simulatedView;
  NSView *combinedView;
  NSRect rect = NSMakeRect(0,0,WIDTH*2,HEIGHT);
  unsigned int styleMask = NSTitledWindowMask | NSMiniaturizableWindowMask;

  nativeView = [[NSImageView alloc] initWithFrame: NSMakeRect(0,0,WIDTH,HEIGHT)];
  [nativeView setImage: nativeImage];

  simulatedView = [[NSImageView alloc] initWithFrame: NSMakeRect(WIDTH,0,WIDTH,HEIGHT)];
  [simulatedView setImage: simulatedImage];

  combinedView = [[NSView alloc] initWithFrame: rect];
  [combinedView addSubview: nativeView];
  [combinedView addSubview: simulatedView];

  window = [[NSWindow alloc] initWithContentRect: rect
			     styleMask: styleMask
			     backing: NSBackingStoreBuffered
			     defer: NO];
  [window setTitle: TITLE];
  [window setContentView: combinedView];

  [window makeKeyAndOrderFront: nil];
}
@end
