/*
 * Filename: ns_main.m
 * Project:  Pascal's NSX Kit
 *
 * Author:   Pascal Forget
 * Date:     January 1996
 *
 * Copyright (C) 1996 Free Software Foundation
 */
#include "ns_main.h"
#include <gnustep/PXKDrawingEngine.h>
#include <AppKit/NSWindow.h>

void
drawLines(NSWindow *win)
{
    NSPoint a, b;
    NSRect aLine;
    NSRect rect;

    a.x = 10;
    a.y = 5;
    b.x = 100;
    b.y = 50;
    
    [(NSView *)[win contentView] lockFocus];

    aLine.origin.x = a.x;
    aLine.origin.y = a.y;
    aLine.size.width = 90;
    aLine.size.height = 35;
    
    rect.origin.x = 20.0;
    rect.origin.y = 10.0;
    rect.size.width = 300.0;
    rect.size.height = 150.0;    
    
    aLine = clipLineToRect(aLine, rect);
    
    a.x = aLine.origin.x;
    a.y = aLine.origin.y;
    b.x = aLine.origin.x + aLine.size.width;
    b.y = aLine.origin.y + aLine.size.height;
    
    fprintf(stderr, "clipped line: %3.0f %3.0f %3.0f %3.0f\n",
	    a.x, a.y, b.x, b.y);
    
    pxkDrawLine(a,b,[NSColor blackColor]); 

    [(NSView *)[win contentView] unlockFocus];
    [win display];
}

void
testLineIntersection(void)
{
    NSRect rectA, rectB;
    NSPoint intersect;
    
    rectA.origin.x = 100.0;
    rectA.origin.y = 100.0;
    rectA.size.width = 100.0;
    rectA.size.height = 0.0;

    rectB.origin.x = 150.0;
    rectB.origin.y = 100.0;
    rectB.size.width = 0.0;
    rectB.size.height = 100.0;
    
    intersect = intersectionPoint(rectA, rectB);
    
    fprintf(stdout, "intersection: %3.0f %3.0f\n", 
	    intersect.x,
	    intersect.y);
}

void
drawRect(NSWindow *win)
{    
    NSRect rect;
    
    fprintf(stderr, "Drawing rect...\n");    

    rect.origin.x = 20.0;
    rect.origin.y = 10.0;
    rect.size.width = 300.0;
    rect.size.height = 150.0;
    
    [(NSView *)[win contentView] lockFocus];

    NSFrameRect(rect);

    [(NSView *)[win contentView] unlockFocus];
    [win display];
    
    printf("Done.\n");
    sleep(1);
}


void
drawButtons(NSWindow *win)
{
    NSPoint a, b;
    NSRect aLine;
    NSRect rect;

    a.x = 60;
    a.y = 200;
    b.x = 100;
    b.y = 220;
    
    [(NSView *)[win contentView] lockFocus];

    aLine.origin.x = a.x;
    aLine.origin.y = a.y;
    aLine.size.width = 90;
    aLine.size.height = 35;
    
    rect.origin.x = 0.0;
    rect.origin.y = 0.0;
    rect.size.width = 900.0;
    rect.size.height = 950.0;    

    NSDrawButton(aLine, rect); /* here the button fits entirely in clipRect */
    
    aLine.origin.x = 200.0;
    rect.origin.x = 220.0; /* here we'll have to clip */
    NSDrawButton(aLine, rect);
    
    [(NSView *)[win contentView] unlockFocus];
    [win display];
}

void
drawBox(NSWindow *win)
{
    NSRect aLine;
    NSRect rect;

    [(NSView *)[win contentView] lockFocus];

    aLine.origin.x = 60;
    aLine.origin.y = 40;
    aLine.size.width = 100;
    aLine.size.height = 60;
    
    rect.origin.x = 0.0;
    rect.origin.y = 0.0;
    rect.size.width = 900.0;
    rect.size.height = 950.0;    

    NSDrawGroove(aLine, rect);
        
    [(NSView *)[win contentView] unlockFocus];
    [win display];
}


void
drawBezel(NSWindow *win)
{
    NSRect aLine;
    NSRect rect;

    [(NSView *)[win contentView] lockFocus];

    aLine.origin.x = 200;
    aLine.origin.y = 40;
    aLine.size.width = 60;
    aLine.size.height = 60;
    
    rect.origin.x = 100.0;
    rect.origin.y = 0.0;
    rect.size.width = 400.0;
    rect.size.height = 400.0;    

    pxkFillRect(aLine, [NSColor whiteColor]);
    
    NSDrawGrayBezel(aLine, rect);
        
    [(NSView *)[win contentView] unlockFocus];
    [win display];
}


int
main(int argc, char **argv)
{
    NSRect rect;
    int i;
    int windowId;
    NSWindow *win;
    
    initializeDrawingEngine();

    /* Create a new window */
#if 0    
    rect.origin.x = 0;
    rect.origin.y = 0;
    rect.size.width = 400;
    rect.size.height = 300;

    win = [[NSWindow alloc] initWithContentRect:rect
			    styleMask:0
			    backing:NSBackingStoreBuffered
			    defer:NO];
    
    /* Display the window */

    [win display];

    windowId = [win windowNumber];
#endif

#if 0
    [win close];

    sleep(2);

    [win display];

    sleep(2);

    /* Resize the window */

    printf("Heightening window\n");

    for (i=0; i<4; i++ ) {
	rect.size.height += 50;
	//setWindowFrameSize(windowId, rect.size);
	//displayWindow(windowId);

	[win setFrame:rect display:YES];

	sleep(1);
    }
    printf("done.\n");    

    sleep(1);
    printf("Widening window\n");
    
    for (i=0; i<4; i++ ) {
	rect.size.width += 50;
	setWindowFrameSize(windowId, rect.size);
	displayWindow(windowId);
	sleep(1);
    }
    printf("done.\n");
    
    sleep(1);
#endif

#if 0    
    /* Move and resize at the same time */

    printf("Moving and resizing window\n");
    
    for (i=0; i<200; i+=50) {
	rect.origin.x = 100 + i;
	rect.origin.y = 300 + i;
	rect.size.width = 400 + i;
	rect.size.height = 200 + i;

	[win setFrame:rect display:YES];
	
	//setWindowFrame(windowId, rect);
	//displayWindow(windowId);

	sleep(1);
    }

    printf("done.\n");    
#endif
    
    /* Draw lines in the content view */

    drawLines(win);

    /* Test the intersection of two 2D lines */

    testLineIntersection();
    
    drawRect(win);

    drawButtons(win);

    sleep(1);

    drawBox(win);
    
    drawBezel(win);
    
    sleep(8);
    return 0;
}
