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

#import "PXIButton.h"

@implementation PXIButton

- (id)initWithName: (NSString*)part_name left:(int)x top:(int)y width:(int)w height:(int)h echoval:(NSString*)ev
{
	self = [super initWithName:part_name left:x top:y width:w height:h];
	//[self setEchoval:ev];
	hilited = false;
	return self;
}


-(PXIButton*) setEchoval: (NSString*)v
{
	echoval = [v copy]; // Copy the incoming string into the echoval parameter.
	return self; // Return a copy of this to whoever asked for it.
}

-(NSString*) getEchoval
{
	return echoval; // Return a reference to the echoval.
}

- (void)setHilited: (bool)b
{
	hilited = b;
}

- (bool)getHilited
{
	return hilited;
}

- (void)drawMe
{
	NSRect crect;
	Rect * brect = malloc ( sizeof ( Rect ) );
	char * btitle;
	int l,tw;
	Point p;
	Pattern gray = { 170, 85, 170, 85, 170, 85, 170, 85 };
	Pattern black = { 255, 255, 255, 255, 255, 255, 255, 255 };
	
	// If this control isn't visible, don't bother with this.
	
	if ( ! visible )
		return;
	
	crect = [self getBoundingBox]; // Get a copy of the bounding box to play with.
	
	// If this control isn't enabled, draw it greyed out.
	if ( ! enabled )
	{
		PenPat ( & gray );
		TextMode ( patCopy );
	}
	else
	{
		PenPat ( & black );
	}
	
	// Now I have to make a P-String. Greeeaat.....
	
	btitle = malloc ( l = ( [[self getPartName] length] + 2 ) ); // Allocate enough space for the PString.
	btitle [ 0 ] = l - 1; // Set the length byte.
	
	tw = TextWidth( [[self getPartName] cString], 0, [[self getPartName] length] );
	
	strcpy ( btitle + 1, [[self getPartName] cString] );
	
	// Set up a QuickDraw rectangle. QD rects are LTRB format, and NSRects are LTWH format. This
	// necessitates a little bit of conversion.
	SetRect ( brect, crect.origin.x, crect.origin.y, crect.origin.x + crect.size.width, crect.origin.y + crect.size.height );
	
	// Set the pen location for drawing the text.
	// When we add support for text styles, make sure to update the hard coded line height.
	
	TextSize( 13 );
	TextFont( applFont );
	
	switch ( textalign )
	{
		case 0:
			// Left
			MoveTo ( brect->left + 3, brect->top + ( round ( crect.size.height / 2 ) ) + ( 13 / 2 ) /* Adjust 1 pixel: */ - 1 );
			break;
		case 1:
			// Center
			MoveTo ( brect->left + ( round( crect.size.width / 2 ) ) - ( round ( ( float ) tw / 2 ) ), brect->top + ( round( crect.size.height / 2 ) ) + ( round ( (float) 13 / 2 ) ) /* Adjust 1 pixel: */ - 1 );
			break;
		case 2:
			MoveTo ( brect->right - ( tw ) - 8, brect->top + ( round ( crect.size.height / 2 ) ) + ( 13 / 2 ) /* Adjust 1 pixel: */ - 1 );
			// Right
			break;
	}
	
	switch ( type )
	{
		case 0:
			DrawString ( btitle );
			break;		
		case 1:
			FrameRect( brect );
			DrawString ( btitle );
			break;
		case 2:
			FrameRoundRect( brect, 10, 10 );
			DrawString ( btitle );
			break;
		case 3:		
			// This one has some pretty heavy borders, so we want to
			// nudge text away from the edges.
			if ( textalign == 0 )
			{
				// adjust left margin.
				GetPen ( & p );
				MoveTo ( p.h + 3, p.v );
			}
			else if ( textalign == 2 )
			{
				// adjust left margin.
				GetPen ( & p );
				MoveTo ( p.h - 3, p.v );			
			}
			
			PenSize( 2, 2 );
			FrameRoundRect( brect, 15, 15 ); // Draw the outer rectangle...
			
			// Reset the pen and create a smaller rect.
			PenSize( 1, 1 );
			SetRect ( brect, crect.origin.x + 3, crect.origin.y + 3 , crect.origin.x + crect.size.width - 3, crect.origin.y + crect.size.height - 3 );
			
			FrameRoundRect( brect, 10, 10 ); // Draw the inner rectangle...
			
			DrawString ( btitle );
			break;
	}
	
	free ( btitle ); // Clean up that memory.
	
	free ( brect );
	
	PenPat ( & black ); // Reset the pen pattern.
						//TextMode ( transparent );
}

-(void) dealloc
{
	[echoval release]; // Release the NSString containing echoval.
	[super dealloc]; // Call PXIPart's deallocator.
}
@end
