//
//  TVCanvasView.m
//  tater_salad
//
//  Created by decaf on 11/17/04.
//  Copyright 2004 __MyCompanyName__. All rights reserved.
//

#import "TVCanvasView.h"

@implementation TVCanvasView

- (id)initWithFrame:(NSRect)frame {
	
	self = [super initWithFrame:frame];
	
	// This is the part where Cocoa calls the canvas for the first time.
	// Here's where you set things up, etc.
	
    if (self) { // Makes we actually got allocated.
        // Initialization code here.
		
		// Set up our dictionary. We're creating three buttons, and placing them conveniently on top of one another.
		// The allocation looks complicated, but it works like this:
		// Allocate a PXIButton -> Set its properties -> Set its echo value -> Add it to the dictionary.
				
		card_buttons = [[NSMutableDictionary alloc] init]; // Allocate a retained dictionary. I'll need to release this in -dealloc.
		[card_buttons setObject:[[[PXIButton alloc] initWithName:@"Button 1" left:7 top:10 width:106 height:26] setEchoval:@"I'm button 1."] forKey:@"Button 1"];
		[card_buttons setObject:[[[PXIButton alloc] initWithName:@"Button 2" left:10 top:40 width:100 height:20] setEchoval:@"I'm button 2."] forKey:@"Button 2"];
		[card_buttons setObject:[[[PXIButton alloc] initWithName:@"Button 3" left:10 top:64 width:100 height:40] setEchoval:@"I'm button 3."] forKey:@"Button 3"];

		// Have to release them so they'll be destroyed when
		// the dictionary goes bye bye.
		[[card_buttons objectForKey:@"Button 1"] release];
		[[card_buttons objectForKey:@"Button 2"] release];
		[[card_buttons objectForKey:@"Button 3"] release];
		
		[[card_buttons objectForKey:@"Button 1"] setPartType:3]; // Default
		[[card_buttons objectForKey:@"Button 2"] setPartType:2]; // Standard
		
		[[card_buttons objectForKey:@"Button 1"] setPartEnabled:false]; // Disable

		[[card_buttons objectForKey:@"Button 1"] setTextAlign:0]; // Left
		[[card_buttons objectForKey:@"Button 2"] setTextAlign:1]; // Center
		[[card_buttons objectForKey:@"Button 3"] setTextAlign:2]; // Right
	}
    return self; // Return myself to whoever created me.
}

- (void)drawRect:(NSRect)rect {
	// This is like REALbasic's paint event. It gets called when it's time to draw the
	// contents of the rectangle. This rectangle happens to be an NSQuickDrawView, so
	// we're going to be using a QD GrafPort to do our drawing.
	
	// Note that, ideally, the drawing code would be encapsulated in the screen objects
	// themselves, and this would be a simple loop to call the paint methods for each
	// object. I decided to do it this way because (1) I wanted to show how you do
	// QuickDraw drawing and (2) I was too lazy to write paint methods.
	
	// NOTE: We've gotta come up with a simpler library for drawing text. QuickDraw's
	// is a complete nightmare.
	
    // Drawing code here.
	
	GrafPtr myPort = [self qdPort]; // Set up a QuickDraw graphics port so I can do stuff.
	
	//char * btitle;
	
	//int l; // String length.
	
	Rect * brect = malloc ( sizeof( Rect ) ); // Allocate a QuickDraw rect. I'll need this
											  // to draw buttons and stuff later on.
	
	NSRect crect; // Allocate an NSRect. I'll need this to work with the NeXT style button rects.
	
	NSEnumerator * e = [card_buttons objectEnumerator]; // Allocate an enumerator so I can 
														// walk through the button dict.
														// Enumerators are the Cocoa equiv-
														// alent to iterators in the STL.
	
	PXIButton * obj; // Create a button to store the current element in.
		
	crect = [super frame];
	
	SetRect ( brect, crect.origin.x, crect.origin.y, crect.origin.x + crect.size.width, crect.origin.y + crect.size.height );

	EraseRect( brect ); // Fill the window with white.
	
	while (obj = [e nextObject]) { // Walk through the dictionary, one item at a time.
				
		[obj drawMe];
		
	}
	
	free( brect ); // Deallocate the QuickDraw rect.
	
}

- (void)mouseDown:(NSEvent*)event
{
	// This is our click event handler. I'm using mouseDown
	// because I'm lazy. This gets called when someone clicks
	// on our rectangle and we want to respond to it.
	
	NSEnumerator * e = [card_buttons objectEnumerator]; // Create an enumerator
														// so we can walk through
														// the button dictionary.

	NSPoint clickPt; // Create an NSPoint to store the click loc in.
	PXIButton * obj; // Create a button to store the current element in.
	
    NSPoint p = [event locationInWindow]; // Create an NSPoint based on the location
										  // of the mouse.
	
    clickPt = [self convertPoint:p fromView:nil]; // Convert it to local coordinates (don't ask.)
	
	while (obj = [e nextObject]) { // Walk through the dictionary one element at a time...
				
		if( NSPointInRect( clickPt, [obj getBoundingBox] ) ) // Check to see if the click is within this element or not.
		{
			NSLog( [obj getEchoval] ); // If it is, echo this element's echoval.
									   // We'd probably want to execute a script right about now.
		}
		
		
	}
	
}

- (void)dealloc
{
	[card_buttons release]; // Release the card_buttons dictionary, so it doesn't leak.
}

@end
