
/* Tiny.m
   A tiny GUI application that creates a window
   and then displays graphics in it.
   Gorm is not used to create this application.
 */
 
#import <AppKit/AppKit.h>
#include <math.h>

@interface DemoView : NSView
{
}
- (void)drawRect:(NSRect)rect;
@end

@implementation DemoView
#define X(t) (sin(t)+1) * width * 0.5
#define Y(t) (cos(t)+1) * height * 0.5

- (void)drawRect:(NSRect)rect
{
    double f,g;
    double const pi = 2 * acos(0.0);

    int n=24;

    float width = [self bounds].size.width;
    float height = [self bounds].size.height;

    [[NSColor blackColor] set];
    NSRectFill([self bounds]);

    for (f=0; f<2*pi; f+=2*pi/n) {
	for (g=0; g<2*pi; g+=2*pi/n) {
	    [[NSColor colorWithDeviceHue:(g/pi/2) saturation:1.0 brightness:1.0 alpha:1.0] set];
	    NSPoint p1 = NSMakePoint(X(f),Y(f));
	    NSPoint p2 = NSMakePoint(X(g),Y(g));
	    [NSBezierPath strokeLineFromPoint:p1 toPoint:p2];
	}
    }
}

-(void)windowWillClose:(NSNotification *)notification
{
    [NSApp terminate:self];
}
@end

void setup()
{
    NSWindow *myWindow;
    NSView   *myView;
    NSRect    graphicsRect;
    
    graphicsRect = NSMakeRect(100.0, 200.0, 600.0, 400.0);

    myWindow = [ [NSWindow alloc]
               initWithContentRect: graphicsRect
	                 styleMask:NSTitledWindowMask
			          |NSClosableWindowMask
				  |NSMiniaturizableWindowMask
			   backing:NSBackingStoreBuffered
			     defer:NO ];

    [myWindow setTitle:@"Tiny Application Window"];
    
    myView = [[[DemoView alloc] initWithFrame:graphicsRect] autorelease];
    
    [myWindow setContentView:myView];
    
    [myWindow setDelegate:myView];
    [myWindow makeKeyAndOrderFront: nil];
}

int main()
{
    NSMenu *mainMenu;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSApp = [NSApplication sharedApplication];
    mainMenu = [[NSMenu new]autorelease];
    [mainMenu addItemWithTitle: @"Info..."
                                  action: @selector (orderFrontStandardInfoPanel:)
                                  keyEquivalent: @""];
    [mainMenu addItemWithTitle: @"Hide"
                            action: @selector (hide:)
                     keyEquivalent: @"h"];
    [mainMenu addItemWithTitle: @"Quit"
                         action: @selector (terminate:)
	                  keyEquivalent: @"q"];

    [NSApp setMainMenu: mainMenu];
    
    setup();
    
    [NSApp run];
    
    [NSApp release];
    [pool release];
    return(EXIT_SUCCESS);
}
