//************************************************************************
//
//	IconView.m.  	
//
//		Displays an image in the app Icon
//
//		by	Felipe A. Rodriguez
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//************************************************************************


#import "IconView.h"


@implementation IconView

//*****************************************************************************
//
// 		init the appIconView
//
//*****************************************************************************

- initFrame:(const NXRect *)bRect 
{
    [super initFrame:bRect];
	[super setAutodisplay:YES];
	[super notifyAncestorWhenFrameChanged:YES];
	appTile = [NXImage findImageNamed: "NXAppTile"];
	[self getFrame:&aRect];										// setup a 
	timeCell = [[TextFieldCell alloc] initTextCell:"    "];		// transparent
	[timeCell setBackgroundTransparent:YES];					// textcell
	[timeCell setTextColor:NX_COLORGREEN];						// for display
	timeFont = [Font newFont:"Ohlfs" size:14];					// of time in
	[timeCell setFont:timeFont];								// appIcon
	[timeCell setAlignment:NX_CENTERED];	// center text horiz
	NXOffsetRect(&aRect, 0, -22);			// magic number center text vert
	mailIcon = [NXImage findImageNamed: "mailq"];
	Image = [NXImage findImageNamed: "GateKeeper"];
	
    return self;
}
//************************************************************************
//
// 			app Icon handling Methods
//
//************************************************************************

- (NXPoint) _centerPoint:anImage
{
NXSize	appTileSize;
NXSize	imageSize;
NXPoint	centerPoint;

		//  You must center the composited image within the contentView of the
		//  appIcon window.
	[appTile getSize: &appTileSize];
	[anImage getSize: &imageSize];
		// center image and add offset to compensate for location of subview
	if( imageSize.width < appTileSize.width ) 
		centerPoint.x += ((appTileSize.width - imageSize.width ) / 2.0) - 1.0;
	
	if( imageSize.height < appTileSize.height ) 
		centerPoint.y += ((appTileSize.height - imageSize.height) / 2.0) - 1.0;
		
	return centerPoint;
}
//************************************************************************
//
// 	NXAppTile is composited first at 0,0 of the icon window's content view
//	(this is required in order to maintain the NeXT icon look).  'anImage'
//	is then composited at center (centering is also a requirement).
//
//************************************************************************

- _display:anImage
{
NXPoint	contentViewOrigin = {-1.0, -1.0 };
NXPoint	centerPoint = [self _centerPoint: anImage];
	
	[appTile composite:NX_SOVER toPoint:&contentViewOrigin];
	[anImage composite:NX_SOVER toPoint:&centerPoint];
	[timeCell drawSelf:&aRect inView:self];	// draw 1st
	NXOffsetRect(&aRect, 1, 0);				// offset trick to make font BOLD
	[timeCell drawSelf:&aRect inView:self];	// draw 2nd w/off
	NXOffsetRect(&aRect, -1, 0);					// return offset to orginal
	if(mailFlag)
		[mailIcon composite:NX_SOVER toPoint:&centerPoint];

	return self;
}

- drawSelf:(const NXRect *)rects :(int)rectCount 
{
	[self _display:Image]; 

	return self;
}
//*****************************************************************************
//
// 		we will display the NXImage we are passed  
//
//*****************************************************************************

- setImage:theImage 
{
	oldImage = Image; 
	Image = theImage; 
	[self display];

	return oldImage; 
}
//*****************************************************************************
//
// 		set whether we will display mail icon in corner of app icon  
//
//*****************************************************************************

- setMailFlag:(BOOL)flag 
{
	if(mailFlag != flag)
		{
		mailFlag = flag;
		[self display];
		} 

	return self; 
}
//*****************************************************************************
//
// 		return the text Cell within the app Icon Tile 
//
//*****************************************************************************

- getTextCell 
{
	return timeCell; 
}
//************************************************************************
//
// 	free simply gets rid of everything we created
// 	This is how nice objects clean up.
//
//************************************************************************

- free
{
	[timeCell free];

    return [super free];
}

@end
