
/*
	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: Preferences
	
	Description: Preferences.h
	
	Original Author: Jeremy Slade
	
	Revision History:
		Created
			V.101	JGS Mon Feb  8 22:49:15 GMT-0700 1993

*/

#import "Preferences.h"

#import "Activator.h"
#import "AppIconView.h"
#import "AtomList.h"
#import "FolderController.h"
#import "FolderViewer.h"
#import "SwapView.h"
#import "Globals.h"
#import "MainController.h"

#import <defaults/defaults.h>
#import <objc/List.h>
#import <objc/NXStringTable.h>
#import <stdlib.h>
#import <sys/param.h>


// Toggles
#define TOGGLE_AUTOLAUNCH_HIDE	0
#define TOGGLE_HIDEONDEACTIVATE	1
#define TOGGLE_VERIFY_ACTIONS		2
#define TOGGLE_AUTOSAVE				3
#define TOGGLE_ACTIVATOR_ADD		4
#define TOGGLE_APPICON_ADD			5


char *prefsTitles[] = {
	"General Options", "Default Viewer", "Startup Folders", NULL
};


@implementation Preferences


// -------------------------------------------------------------------------
//   Creating, initializing
// -------------------------------------------------------------------------


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



- init
{
	[super init];
	
	// Load the nib section into the created zone
	[NXApp loadNibSection:"Preferences.nib"
		owner:self
		withNames:NO];
	[self setGeneralLevel:self];

	return ( self );
}



- awakeFromNib
/*
	Perfrom final initializations
*/
{
	[self setGeneralLevel:self];
	return ( self );
}



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



// -------------------------------------------------------------------------
//   Setting the prefs level
// -------------------------------------------------------------------------


- setGeneralLevel:sender
{
	prefsLevel = PREFS_GENERAL_LEVEL;
	[self displayPrefs];
	return ( self );
}



- setViewerLevel:sender
{
	prefsLevel = PREFS_VIEWER_LEVEL;
	[self displayPrefs];
	return ( self );
}



- setFoldersLevel:sender
{
	prefsLevel = PREFS_FOLDERS_LEVEL;
	[self displayPrefs];
	return ( self );
}



- swapPaneFor:sender
/*
	The SwapView's delegate method that determines which view to use for the current setting
*/
{
	switch ( prefsLevel ) {
		case PREFS_GENERAL_LEVEL: return generalPrefsPanel;
		case PREFS_VIEWER_LEVEL: return viewerPrefsPanel;
		case PREFS_FOLDERS_LEVEL: return folderPrefsPanel;
	}
	
	return ( nil ); // unrecognized prefs level
}



// -------------------------------------------------------------------------
//   Displaying
// -------------------------------------------------------------------------


- panel
{
	return ( panel );
}



- displayPrefs
/*
	Update the display to show current settings
*/
{
	[swapView swap];
	
	switch ( prefsLevel ) {
		case PREFS_GENERAL_LEVEL:
			[self displayActivator];
			[self displayToggles];
			break;
			
		case PREFS_VIEWER_LEVEL:
			[self displayViewerFrame];
			break;
			
		case PREFS_FOLDERS_LEVEL:
			[self displayStartupFolders];
			break;
			
		default:	break;
	}
	
	[modeCover setTitle:prefsTitles[prefsLevel]];
	[panel display];
	return ( self );
}



- displayActivator
{
	id activator = [mainController activator];

	activatorMask = [activator barMask];
	[topSwitch setState:[activator isBarOn:ACT_TOP]];
	[leftSwitch setState:[activator isBarOn:ACT_LEFT]];
	[rightSwitch setState:[activator isBarOn:ACT_RIGHT]];
	[bottomSwitch setState:[activator isBarOn:ACT_BOTTOM]];

	[floatSwitch setState:(floatActivator = [activator isFloating])];
	
	[interiorWell setColor:(activatorFillColor = [activator color])];
	[borderWell setColor:(activatorBorderColor = [activator borderColor])];

	[mainController hideActivator:self];
	[mainController showActivator:self];
	
	return ( self );
}



- displayToggles
{
	[[toggles cellAt:TOGGLE_AUTOLAUNCH_HIDE :0] setState:autolaunchHide];
	[[toggles cellAt:TOGGLE_HIDEONDEACTIVATE :0] setState:hideDeactive];
	[[toggles cellAt:TOGGLE_VERIFY_ACTIONS :0] setState:verifyActions];
	[[toggles cellAt:TOGGLE_AUTOSAVE :0] setState:autoSave];
	[[toggles cellAt:TOGGLE_ACTIVATOR_ADD :0] setState:activatorAdd];
	[[toggles cellAt:TOGGLE_APPICON_ADD :0] setState:appIconAdd];
	
	return ( self );
}



- displayViewerFrame
{
	[xField setFloatValue:NX_X(&defaultViewerFrame)];
	[yField setFloatValue:NX_Y(&defaultViewerFrame)];
	[wField setFloatValue:NX_WIDTH(&defaultViewerFrame)];
	[hField setFloatValue:NX_HEIGHT(&defaultViewerFrame)];
	
	return ( self );
}



- displayStartupFolders
{
	[browser loadColumnZero];
	return ( self );
}



// -------------------------------------------------------------------------
//   Toggles
// -------------------------------------------------------------------------


- setAutolaunchHide:sender
{
	autolaunchHide = [[toggles cellAt:TOGGLE_AUTOLAUNCH_HIDE :0] state];	return ( self );
}



- setHideDeactive:sender
{
	hideDeactive = [[toggles cellAt:TOGGLE_HIDEONDEACTIVATE :0] state];
	return ( self );
}



- setVerifyActions:sender
{
	verifyActions = [[toggles cellAt:TOGGLE_VERIFY_ACTIONS :0] state];
	return ( self );
}



- setAutoSave:sender
{
	autoSave = [[toggles cellAt:TOGGLE_AUTOSAVE :0] state];
	return ( self );
}



- setActivatorAdd:sender
{
	activatorAdd = [[toggles cellAt:TOGGLE_ACTIVATOR_ADD :0] state];
	[activator setDraggingEnabled:activatorAdd];
	return ( self );
}



- setAppIconAdd:sender
{
	appIconAdd = [[toggles cellAt:TOGGLE_APPICON_ADD :0] state];
	[appIconView setDraggingEnabled:appIconAdd];
	return ( self );
}



// -------------------------------------------------------------------------
//   Activator Bar
// -------------------------------------------------------------------------


- setActivatorTop:sender
{
	[[mainController activator] setBar:ACT_TOP on:[topSwitch state]];
	[self displayActivator];
	return ( self );
}



- setActivatorLeft:sender
{
	[[mainController activator] setBar:ACT_LEFT on:[leftSwitch state]];
	[self displayActivator];
	return ( self );
}



- setActivatorRight:sender
{
	[[mainController activator] setBar:ACT_RIGHT on:[rightSwitch state]];
	[self displayActivator];
	return ( self );
}



- setActivatorBottom:sender
{
	[[mainController activator] setBar:ACT_BOTTOM on:[bottomSwitch state]];
	[self displayActivator];
	return ( self );
}



- setActivatorFloat:sender
{
	[[mainController activator] setFloating:[floatSwitch state]];
	[self displayActivator];
	return ( self );
}



- setActivatorFillColor:sender
{
	[[mainController activator] setColor:[interiorWell color]];
	[self displayActivator];
	return ( self );
}



- setActivatorBorderColor:sender
{
	[[mainController activator] setBorderColor:[borderWell color]];
	[self displayActivator];
	return ( self );
}



// -------------------------------------------------------------------------
//   Default Viewer Frame
// -------------------------------------------------------------------------


- setViewerFrameToCurrent:sender
{
	id currentViewer = [[folderController keyFolder] viewer];
	if ( !currentViewer ) NXBeep();
		else  [currentViewer getFrame:&defaultViewerFrame];
	[self displayViewerFrame];
	return ( self );
}



- setViewerX:sender
{
	defaultViewerFrame.origin.x = [xField intValue];
	[self displayViewerFrame];
	return ( self );
}



- setViewerY:sender
{
	defaultViewerFrame.origin.y = [yField intValue];
	[self displayViewerFrame];
	return ( self );
}



- setViewerW:sender
{
	defaultViewerFrame.size.width = [wField intValue];
	[self displayViewerFrame];
	return ( self );
}



- setViewerH:sender
{
	defaultViewerFrame.size.height = [hField intValue];
	[self displayViewerFrame];
	return ( self );
}



// -------------------------------------------------------------------------
//   Startup Folders
// -------------------------------------------------------------------------


- (int)browser:sender fillMatrix:matrix inColumn:(int)col
/*
	Load the browser with the folders in startupFolders.
	StartupFolders is an AtomList containing the names of the folders that will be opened at startup
*/
{
	id cell;
	int i, count;
	
	count = [startupFolders count];
	for ( i=0; i<count; i++ ) {
		[matrix addRow];
		cell = [matrix cellAt:i :0];
		[cell initTextCell:[startupFolders atomAt:i]];
		[[cell setLoaded:YES] setLeaf:YES];
	}
		
	return ( count );
}



- addFolders:sender
/*
	Add a new folder(s) to the startupFolders using an OpenPanel
*/
{
	id openPanel = [OpenPanel new];
	static char directory[MAXPATHLEN+1] = "";
	const char *const *files;
	const char *const types[2] = { FOLDER_EXT, NULL };
	int i;
	char path[MAXPATHLEN+1];
	
	if ( !strlen ( directory ) ) 
		sprintf ( directory, "%s/%s", NXHomeDirectory(), LIBRARY_DIR );
	
	[[openPanel setTitle:"Add Startup Folder"]
		setPrompt:"File:"];
	[openPanel allowMultipleFiles:YES];
	
	if ( [openPanel runModalForDirectory:directory file:NULL types:types] ) {
		strcpy ( directory, [openPanel directory] );
		files = [openPanel filenames];
		
		// Register selected items
		i = 0;
		while ( files[i] ) {
			sprintf ( path, "%s/%s", directory, files[i] );
			[startupFolders addAtomIfAbsent:
				NXUniqueString([folderController relativePathForFolder:path])];
			i++;
		}	
	}
	
	[self displayStartupFolders];
	
	return ( self );
}



- removeFolders:sender
/*
	Remove the selected cells in the browser from the startupFolders
*/
{
	int i, count;
	int j, cnt;
	id cellList;
		
	cellList = [[browser matrixInColumn:0] cellList];
	count = [cellList count];
	for ( i=0; i<count; i++ )
		if ( [[cellList objectAt:i] state] ) {
			cnt = [startupFolders count];
			for ( j=0; j<cnt; j++ ) {
				if ( !strcmp ( [startupFolders atomAt:j],
					[[cellList objectAt:i] stringValue] ) ) {
						[startupFolders removeAtomAt:j];
						break; // break out of inner for loop
				}
			}
		}
			
	[self displayStartupFolders];
	
	return ( self );
}



// -------------------------------------------------------------------------
//   Saving settings
// -------------------------------------------------------------------------


- setPrefs:sender
{
	[mainController writePrefs:self];
	return ( self );
}



- restorePrefs:sender
{
	[mainController readPrefs:self];	
	[self displayPrefs];	
	return ( self );
}



@end
