//*****************************************************************************
//
//	ToolBar.m.  
//
//		ToolBar control object
//
//			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 <sys/types.h>
#import <sys/dir.h>  /* POSIX applications #include <dirent.h> */

#import "GKdefs.h"
#import "IconView.h"
#import "ToolBar.h"
#import "ToolBarView.h"
#import "ToolIconView.h"
#import "DLRecord.h"			
#import "DLDelegate.h"			
#import "Coordinator.h"
#import <appkit/appkit.h>
#import <dbkit/DBImageView.h>




@implementation ToolBar

//*****************************************************************************
//
//		sent to us after nib objects are unarchived and init'd 
//
//*****************************************************************************

- awakeFromNib
{
NXRect	appIconFrame;
NXRect	lineViewRect;
id appIcon;

	appTile = [NXImage findImageNamed: "NXAppTile"];
	appIcon = [NXApp appIcon];					// get appIcon window
    [appIcon getFrame:&appIconFrame];			// get appIcon frame rect
    [Window getContentRect:&lineViewRect 		// get content view rect appIco
						forFrameRect:&appIconFrame style:[appIcon style]];
				// reduce the size and width/ht by a border of 1
	NX_X(&lineViewRect) = 1;
	NX_Y(&lineViewRect) = 1;
	NX_WIDTH(&lineViewRect) -= 1 ;
	NX_HEIGHT(&lineViewRect) -= 1;
															// create tool view
	theToolView = [[ToolBarView alloc] initFrame:&lineViewRect];	
															// create icon view
	toolIconView = [[ToolIconView alloc] initFrame:&lineViewRect];	
    toolWin = [[Window allocFromZone:[self zone]]			// create tool Win
				   	initContent:&appIconFrame
				   	style:NX_TOKENSTYLE
				   	backing:NX_BUFFERED 
//					buttonMask:NX_CLOSEBUTTONMASK defer:NO];
					buttonMask:0 defer:NO];
	[toolWin setFreeWhenClosed:NO];

	if(![toolWin setFrameUsingName:"toolBar"])			
		[toolWin moveTo:100.0 :100.0];					// initial pos of toolB
	[toolWin setFrameUsingName:"toolbar"];				// remember location
	[toolWin setFrameAutosaveName:"toolbar"];			// remember location
    [[toolWin setContentView:theToolView] free];		// replace cont w/toolV
									// add IB created backWindow's contentView
	[[toolWin contentView] addSubview:[backWindow contentView]];
	toolView = [toolWin setContentView:toolIconView];		
	[toolWin setBackgroundGray:NX_BLACK];
	[[toolWin contentView] getFrame:&lineViewRect];						
	[[toolWin contentView] convertRect:&lineViewRect toView:nil];
	[toolWin setTrackingRect:&lineViewRect					// create mouse 
					inside:YES								// tracking rect
					owner:self
					tag:1500
					left:NO
					right:NO];

	nxAppDel = [NXApp delegate];
	hotListDelegate = [nxAppDel docLDelegate];
	[hotListDelegate setToolBar:toolIconView];
    [toolWin orderFront:self];			
	[toolWin display];

    return self;
}
//*****************************************************************************
//
// 		user pressed stop button in toolbar 
//
//*****************************************************************************

- playUnlink:sender 
{
	[sender setState:0];
	if([[[NXApp mainMenu] findCellWithTag:1] isEnabled])
		[[NXApp delegate] disconnect:self];						
	
	return self;
}
//*****************************************************************************
//
// 		go to next gate doc in hotList 
//
//*****************************************************************************

- nextItem:sender 
{
	return [hotListDelegate nextItem:sender];
}
//*****************************************************************************
//
// 		go to previous gate doc in hotList 
//
//*****************************************************************************

- prevItem:sender 
{
	return [hotListDelegate prevItem:sender];
}
//*****************************************************************************
//
// 		close toolbar 
//
//*****************************************************************************

- close:sender 
{
	if(!NXWriteDefault([NXApp appName], DISPLAYT, "NO"))
		[[NXApp delegate] showAlert:"ddbWriteError"];
	[sender setState:0];
	[toolWin close];

	return self;
}
//*****************************************************************************
//
// 		mouse has entered our tracking rect (window)
//
//*****************************************************************************

- mouseEntered:(NXEvent *)theEvent 
{
	wasHidden = [NXApp isHidden];					// is the app hidden?
	[NXApp perform:@selector(unhide:) with:self 
								afterDelay:100 
								cancelPrevious:YES];				
//	statusVisible = [[hotListDelegate statusWindow] isVisible];						
	[toolWin setContentView:toolView];		
	[toolWin display];	
//	[hotListDelegate loadBrowser];	// NXBrowser s/load cols from its delegate
	if([nxAppDel mailInQueue])
		iWasSet = 8;
	cursorOverToolBar = YES;
	if(iWasSet > 0)
		{
		[[nxAppDel appIconView] setMailFlag:YES];
		[self perform:@selector(mailInQueueIcon) with:self afterDelay:3000 	
														cancelPrevious:YES];
		}
	NXPing();
	
	return self;
}
//*****************************************************************************
//
// 		mouse has exited our tracking rect (window)
//
//*****************************************************************************

- mouseExited:(NXEvent *)theEvent 
{
	[toolWin setContentView:toolIconView];		
	[toolWin display];		

	[[nxAppDel appIconView] setMailFlag:NO];
	if(wasHidden)				// if app was hidden prior to cursor in, hide
		[NXApp perform:@selector(hide:) with:self 
									afterDelay:100 
									cancelPrevious:YES];				
	cursorOverToolBar = NO;

	return self;
}
- (BOOL)acceptsFirstMouse
{
	return YES;
}
//*****************************************************************************
//
// 		clear mail in queue icon from app icon if mail has been sent
//
//*****************************************************************************

- mailInQueueIcon
{
	if([[[NXApp mainMenu] findCellWithTag:1] isEnabled])
		iWasSet = 0;
	if(![nxAppDel mailInQueue])
		{
		iWasSet--;
						// if user mouseExited while mail icon set, 
						// mouseEntered may occur during interim period
						// when mail is not shown in queue, but is
		if(iWasSet > 0 && cursorOverToolBar)
			[[nxAppDel appIconView] setMailFlag:YES];
		else
			[[nxAppDel appIconView] setMailFlag:NO];	
		}
	else
		iWasSet = 8;
	if(iWasSet > 0)
		[self perform:@selector(mailInQueueIcon) with:self 
									afterDelay:4000  cancelPrevious:YES];

	return self;
}
//*****************************************************************************
//
// 		forward anything in Greek to someone who might understand 
//
//*****************************************************************************

- forward:(SEL)aSelector :(marg_list)argFrame
{
    if ( [toolWin respondsTo:aSelector] )
        return [toolWin performv:aSelector :argFrame];

    [self doesNotRecognize:aSelector];					// error abort process

	return self;
}
//*****************************************************************************
//
// 	free gets rid of everything we created
//
//*****************************************************************************

- free
{
	if(toolWin)
		[toolWin free];									
	if(theToolView)
		[theToolView free];									
	if(toolIconView)
		[toolIconView free];									

    return [super free];
}

@end
