
/*
	Copyright 1993  Jeremy Slade.

	You are free to use all or any parts of the Locus project
	however you wish, just give credit where credit is due.
	The author (Jeremy Slade) shall not be held responsible
	for any damages that result out of use or misuse of any
	part of this project.

*/

/*
	Project: Locus
	
	Class: FolderViewer
	
	Description: See FolderViewer.h
		
	Original Author: Jeremy Slade
	
	Revision History:
		Created
			V.101	JGS Wed Feb  3 23:17:39 GMT-0700 1993

*/

#import "FolderViewer.h"

#import "Folder.h"
#import "Globals.h"
#import "GroupBrowser.h"
#import "WidePopupController.h"


@implementation FolderViewer


// -------------------------------------------------------------------------
//   Creating, Initializing methods
// -------------------------------------------------------------------------


+ initialize
{
	[self setVersion:FolderViewer_VERSION];
	return ( self );
}



- initContent:(const NXRect *)rect
	style:(int)aStyle
	backing:(int)bufferingType
	buttonMask:(int)mask
	defer:(BOOL)flag;
{
	[super initContent:rect
		style:aStyle
		backing:bufferingType
		buttonMask:mask
		defer:flag];

	[[[self setHideOnDeactivate:hideDeactive]
		setExcludedFromWindowsMenu:NO]
		setMiniwindowIcon:"FolderIcon"];
	[contentView setAutoresizeSubviews:YES];

	groupMenu = [[PopUpList allocFromZone:[self zone]] init];
	widePopupController = [WidePopupController allocFromZone:[self zone]];
	
	return ( self );
}



- awakeFromNib
/*
	Perform final initializations
*/
{
	NXAttachPopUpList ( groupMenuCover, groupMenu );
	[widePopupController initForButton:groupMenuCover andPopup:groupMenu];
	
	[browser setDelegate:delegate];
	[[[browser setTarget:delegate]
		setAction:@selector(selectItem:)]
		setDoubleAction:@selector(launchSelectedItems:)];
		
	return ( self );
}



- free
{
	[groupMenu free];
	[widePopupController free];
	[NXApp removeWindowsItem:self];
	return ( [super free] );
}



// -------------------------------------------------------------------------
//   Subviews
// -------------------------------------------------------------------------


- groupMenu
{
	return ( groupMenu );
}


- groupMenuCover
{
	return ( groupMenuCover );
}



- browser
{
	return ( browser );
}



// -------------------------------------------------------------------------
//   Settting the title
// -------------------------------------------------------------------------


- setTitleAsFilename:(const char *)aString
/*
	Sets the title of the window, and automatically adds it to the Windows menu.
*/
{
	[super setTitleAsFilename:aString];
	[NXApp addWindowsItem:self title:aString filename:YES];
	return ( self );
}



// -------------------------------------------------------------------------
//   Misc
// -------------------------------------------------------------------------


- updateInspector:sender
/*
	Sent by the Inspector when it needs to be updated
*/
{
	// Pass this on to our delegate -- the Folder
	[delegate updateInspector:sender];
	return ( self );
}



- becomeKeyWindow
{
	return ( [super becomeKeyWindow] );
}


- becomeMainWindow
/*	
	Tell our delegate (the folder) to become the key folder
*/
{
	[delegate becomeKeyFolder];
	return ( [super becomeMainWindow] );
}



- resignKeyWindow
{
	return ( [super resignKeyWindow] );
}


- resignMainWindow
/*	
	Tell our delegate (the folder) it is no longer the key folder
*/
{
	[delegate resignKeyFolder];
	return ( [super resignMainWindow] );
}



- miniaturize:sender
/*
	Sent when the user clicks on the miniaturize button.  Causes the window to resign keywindow status.
*/
{
	[self resignKeyWindow];	
	[super miniaturize:sender];	
	return ( self );
}



- keyDown:(NXEvent *)event
/*
	This message is sent when a key is pressed and this viewer is the key window.  This method passes it first to the folder to handle it, then to the browser, and finally up the responder chain.
*/
{
	int mask = (NX_CONTROLMASK | NX_ALTERNATEMASK
		| NX_COMMANDMASK );

	// Let the viewer's delegate (the actual Folder) respond to it first off
	if ( [delegate keyDown:event] )
		return ( self );
			
	// Next send it to the browser
	if ( !(event->flags & mask ) && event->data.key.charSet == NX_ASCIISET ) {
		[browser selectKey:event->data.key.charCode time:event->time];
		return ( self );
	}
	
	// If nobody else has responded to it, just pass it on...
	return ( [super keyDown:event] );
}



- (BOOL)commandKey:(NXEvent *)event
{
	// Pass the event to the GroupMenu
	return ( [groupMenu commandKey:event] ? YES : [super commandKey:event] );
}


@end
