/* 
   buttons.m

   This file is part of the mySTEP Library and is provided
   under the terms of the GNU Library General Public License.
*/ 

#import <AppKit/AppKit.h>


@interface RectView : NSView
@end

@implementation RectView

- (void) drawRect:(NSRect)rect						
{
NSTimeInterval elapsed, start;
NSRect r = {{10,10},{40,20}};
NSRect c = {{10,25},{10,10}};

#if 0
	start = [[NSDate date] timeIntervalSinceReferenceDate];

	for (i = 0; i < 10000; i++)
		NSDrawButton (r, r);		

	elapsed = [[NSDate date] timeIntervalSinceReferenceDate] - start;
	printf ("elapsed time %f\n", elapsed);
#endif

	NSDrawButton (r, r);
	NSMinX(r) += 50;		
	NSDrawGrayBezel (r, r);
	NSMinX(r) += 50;		
	NSDrawWhiteBezel (r, r);
	NSMinX(r) += 50;		
	NSDrawGroove (r, r);
	NSMinX(r) += 50;		
	GSDrawPhotoFrame (r, r);
	[[NSColor redColor] set];
	NSMinX(r) += 50;		
	NSFrameRectWithWidth (r, 3.0);
}

@end

@interface FlippedRectView : NSView
@end

@implementation FlippedRectView

- (void) drawRect:(NSRect)rect						
{
NSRect r = {{10,10},{40,20}};
NSRect c = {{10,25},{10,10}};

	NSDrawButton (r, r);
	NSMinX(r) += 50;		
	NSDrawGrayBezel (r, r);
	NSMinX(r) += 50;		
	NSDrawWhiteBezel (r, r);
	NSMinX(r) += 50;		
	NSDrawGroove (r, r);
	NSMinX(r) += 50;		
	GSDrawPhotoFrame (r, r);
	[[NSColor blackColor] set];
	NSMinX(r) += 50;		
	NSFrameRectWithWidth (r, 3.0);
}

- (BOOL) isFlipped								{ return YES; }

@end

@interface RFView : NSView
@end

@implementation RFView

- (void) drawRect:(NSRect)rect						
{
	fprintf (stderr,
	"RFView:  (%1.2f, %1.2f), (%1.2f, %1.2f)\n",
				rect.origin.x, rect.origin.y, 
				rect.size.width, rect.size.height);
	[[NSColor redColor] set];
	NSRectFill(rect);					
}

- (BOOL) isFlipped								{ return YES; }

@end

@interface GFView : NSView
@end

@implementation GFView

- (void) drawRect:(NSRect)rect						
{
	fprintf (stderr,
	"GFView: drawRect (%1.2f, %1.2f), (%1.2f, %1.2f)\n",
				rect.origin.x, rect.origin.y, 
				rect.size.width, rect.size.height);
	[[NSColor greenColor] set];
	NSRectFill(rect);					
}

- (BOOL) isFlipped								{ return YES; }

@end

@interface BNView : NSView
@end

@implementation BNView

- (void)drawRect:(NSRect)rect						
{
	fprintf (stderr,
	"BNView: drawRect (%1.2f, %1.2f), (%1.2f, %1.2f)\n",
				rect.origin.x, rect.origin.y, 
				rect.size.width, rect.size.height);
	[[NSColor blueColor] set];
	NSRectFill(rect);					
}

@end

@interface RNView : NSView
@end

@implementation RNView

- (void) drawRect:(NSRect)rect						
{
	fprintf (stderr,
	"RNView: drawRect (%1.2f, %1.2f), (%1.2f, %1.2f)\n",
				rect.origin.x, rect.origin.y, 
				rect.size.width, rect.size.height);
	[[NSColor redColor] set];
	NSRectFill(rect);					
}

@end

@interface Controller : NSObject
@end

@implementation Controller

- (id) init
{
	[[NSApplication sharedApplication] setDelegate: self];	
}

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSPoint point;
NSWindow *win;
NSView *v;
NSScrollView *t;
NSView *a, *b, *c;
NSRect rect;
NSView* anotherView2;

    NSLog(@"Starting the application\n");

	win = [[NSWindow alloc] initWithContentRect:(NSRect){{100, 100},{500, 500}}
							styleMask:GSAllWindowMask
							backing:NSBackingStoreBuffered	
							defer:NO];

    v = [win contentView];

    c = [[RFView alloc] initWithFrame: (NSRect){{10, 10}, {100, 100}}];
    b = [[GFView alloc] initWithFrame: (NSRect){{0, 0}, {90, 90}}];
    [c addSubview:b];
    a = [[BNView alloc] initWithFrame: (NSRect){{0, 80}, {80, 80}}];
    [b addSubview:a];
    [v addSubview:c];

/*
	t = [[NSTextView alloc] initWithFrame: (NSRect){{200, 100}, {180, 180}}];
	[t setString:@"Starting the application\nI hope this works\nbut if not"];
	[v addSubview:t];
*/


												// flipped scrollview
	t = [[NSScrollView alloc] initWithFrame: (NSRect){{150, 100}, {200, 200}}];
	[t setHasVerticalScroller:YES];
    b = [[GFView alloc] initWithFrame: (NSRect){{0, 0}, {180, 1500}}];
    a = [[BNView alloc] initWithFrame: (NSRect){{10, 80}, {80, 80}}];
    [b addSubview:a];
	[t setDocumentView:b];
	[[t contentView] setBoundsOrigin:(NSPoint){0,50}];
	[v addSubview:t];

	point = [b convertPoint:NSZeroPoint toView:nil];
	fprintf(stderr, "green 1 -- 0,0: (%1.2f, %1.2f)\n", point.x, point.y);
	point = [b convertPoint:(NSPoint){0,80} toView:nil];
	fprintf(stderr, "green 2 -- 0,80: (%1.2f, %1.2f)\n", point.x, point.y);
	rect = [b visibleRect];
	fprintf(stderr, "green visible: (%1.2f, %1.2f) (%1.2f, %1.2f)\n", 
			rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);


												// unflipped scrollview
	t = [[NSScrollView alloc] initWithFrame: (NSRect){{10, 200}, {100, 200}}];
	[t setHasVerticalScroller:YES];
    b = [[RNView alloc] initWithFrame: (NSRect){{0, 0}, {80, 1000}}];
    a = [[BNView alloc] initWithFrame: (NSRect){{10, 80}, {40, 40}}];
    [b addSubview:a];
	[t setDocumentView:b];
	[[t contentView] setBoundsOrigin:(NSPoint){0,40}];
	[v addSubview:t];

	point = [b convertPoint:NSZeroPoint toView:nil];
	fprintf(stderr, "red 1 -- 0,0: (%1.2f, %1.2f)\n", point.x, point.y);
	point = [b convertPoint:(NSPoint){0,80} toView:nil];
	fprintf(stderr, "red 2 -- 0,80: (%1.2f, %1.2f)\n", point.x, point.y);
	rect = [b visibleRect];
	fprintf(stderr, "red visible: (%1.2f, %1.2f) (%1.2f, %1.2f)\n", 
			rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);


    c = [[RectView alloc] initWithFrame:(NSRect){{150,400},{350, 100}}];
	[v addSubview:c];
    c = [[FlippedRectView alloc] initWithFrame:(NSRect){{150,300},{350, 100}}];
	[v addSubview:c];


	[v display];
    [win orderFront:nil];

/*
	point = [c convertPoint:NSZeroPoint toView:nil];
	fprintf(stderr, "red 1 -- 0,0: (%1.2f, %1.2f)\n", point.x, point.y);
	point = [c convertPoint:(NSPoint){0,5} toView:nil];
	fprintf(stderr, "red 2 -- 0,5: (%1.2f, %1.2f)\n", point.x, point.y);

	point = [b convertPoint:NSZeroPoint toView:nil];
	fprintf(stderr, "green 1 -- 0,0: (%1.2f, %1.2f)\n", point.x, point.y);
	point = [b convertPoint:(NSPoint){0,80} toView:nil];
	fprintf(stderr, "green 2 -- 0,80: (%1.2f, %1.2f)\n", point.x, point.y);
	point = [a convertPoint:(NSPoint){0,72} toView:nil];
	fprintf(stderr, "blue 1 -- 0,72: (%1.2f, %1.2f)\n", point.x, point.y);
*/

}

@end

int
main(int argc, char **argv, char** env)
{
    return NSApplicationMain(argc, argv);
}
