/************************************************************************************************************************/

#import "Controller.h"

/* Declaration for auxiliary function */
static char scanChar(const char* string);


@implementation Controller

/***********************************************************************
* Initialization
***********************************************************************/

- appDidInit:sender
	{
		/* MiscSplitView initialization; not necessary in any way for NTYBrowserData! */
		[splitView setHorizontal:NO];
		[splitView addSubview:browser];
		[splitView addSubview:textView];
		[splitView setDividersAutosaveName:"BrowserDataWindow"];
				
		/* Main window initialization */
		[window setFrameAutosaveName:"BrowserDataWindow"];
		[window makeKeyAndOrderFront:self];
		return self;
	}
	
	


/***********************************************************************
* GUI Actions
***********************************************************************/

- readData:sender
	/* read and display data from a file according to user entries in the GUI */
	{
		/* set configuration data according to user entries in the GUI */
		[browserData setPathSeparator:[pathSeparatorText stringValue][0]];	// do NOT set in NXBrowser directly!
		[browserData setTokenSeparator:scanChar([tokenSeparatorText stringValue])];
		[browserData setToken:(unsigned int)[tokenText intValue]];
		[browserData setUniqueNodes:[uniqueNodesSwitch state]];
		[browserData setIndentChar:scanChar([indentCharacterText stringValue])];
		[browserData setIndentDivisor:(unsigned int)[indentDivisorText intValue]];
		[browserData setSort:[sortSwitch state]];
		
		/* read and display data from a file according to the above settings */
		if([browserData readDataFromFile:[pathText stringValue]]) return self;
		NXBeep();
		return nil;
	}
	
	
- setPath:sender
	/* enter path from Open Panel into text field */
	{
		char *types[1]={0};
		if([[OpenPanel new] runModalForTypes:types]) [pathText setStringValue:[[OpenPanel new] filename]];
		return self;
	}
	
	

- switchMark:sender
	/* select type which is used to mark browser lines */
	{
		[browserData setMarkType:[[sender selectedCell] tag]];
		return self;
	}
	
	
	
- switchMultipleSelection:sender
	/* select if, and what type of, multiple selection is enabled in NXBrowser */
	{
		switch([[sender selectedCell] tag])
		{
			case 0:	[browser setMultipleSelectionEnabled:NO];
					break;
			case 1:	[browser setMultipleSelectionEnabled:YES];
					[browser setBranchSelectionEnabled:NO];
					break;
			case 2:	[browser setMultipleSelectionEnabled:YES];
					[browser setBranchSelectionEnabled:YES];
					break;
		}
		return self;
	}
	
	

- switchSeparatorAtStart:sender
	/* select if in list view the lines should start with a pathSeparator character */
	{
		[browserData setSeparatorAtStart:[sender state]];
		[browserData updateBrowser:self];
		return self;
	}



- selectEnteredPath:sender
	/* select path entered in the Search Text text field */
	{
		id re;
		if(re=[browserData selectPath:[searchText stringValue]]) return re;
		NXBeep();
		return nil;
	}
	
	
	
- markEnteredPath:sender
	/* mark path entered in the Search Text text field */
	{
		if([browserData markPath:[searchText stringValue]]) return [browserData updateBrowser:self];
		NXBeep();
		return nil;
	}
	
	
	
- unmarkEnteredPath:sender
	/* unmark path entered in the Search Text text field */
	{
		if([browserData unmarkPath:[searchText stringValue]]) return [browserData updateBrowser:self];
		NXBeep();
		return nil;
	}
	
	
	
- disableEnteredPath:sender
	/* disable path entered in the Search Text text field */
	{
		if([browserData disablePath:[searchText stringValue]]) return [browserData updateBrowser:self];
		NXBeep();
		return nil;
	}
	
	

- enableEnteredPath:sender
	/* enable path entered in the Search Text text field */
	{
		if([browserData enablePath:[searchText stringValue]]) return [browserData updateBrowser:self];
		NXBeep();
		return nil;
	}
	
	

- addEnteredPath:sender
	/* add path entered in the Search Text text field */
	{
		if([browserData addPath:[searchText stringValue]]) return browserData;
		NXBeep();
		return nil;
	}
	
	

- removeEnteredPath:sender
	/* remove path entered in the Search Text text field */
	{
		if([browserData removePath:[searchText stringValue]]) return browserData;
		NXBeep();
		return nil;
	}
	
	

- findForwards:sender
	/* find string entered in the Search Text text field */
	{
		char *result;
		id re;
		
		if(result=[browserData findPath:[searchText stringValue] startPath:NULL backwards:NO caseSensitive:NO regEx:NO
							   alsoDisabled:NO])
			if(re=[browserData selectPath:result]) return re;
		NXBeep(); 
		return nil;
	}
	
	

- findBackwards:sender
	/* find string entered in the Search Text text field */
	{
		char *result;
		id re;
		
		if(result=[browserData findPath:[searchText stringValue] startPath:NULL backwards:YES caseSensitive:NO regEx:NO
		 					   alsoDisabled:NO])
			if(re=[browserData selectPath:result]) return re;
		NXBeep(); 
		return nil;
	}
	
	

- printPaths:sender
	/* get the selected or the marked paths and print them in the text view */
	{
		char *buffer;
		id pathList;
		int i;
		
		if([[sender selectedCell] tag]) pathList=[browserData getMarkedPaths];	// Print marked paths
		else pathList=[browserData getSelectedPaths];							// Print selected paths
		
		buffer=(char*)malloc(BUFSIZ*[pathList count]);
		buffer[0]=0;	// initialize buffer for strcat
		
		for(i=0;i<[pathList count];i++)	// copy all paths in buffer and separate them by newlines
		{
			strcat (buffer, [pathList stringAt:i]);
			strcat (buffer, "\n");
		}
				
		[text setStringValue:buffer];	// print paths
		[pathList free];
		free(buffer);
		return self;
	}
	
	


	
/***********************************************************************
* Auxiliary functions
***********************************************************************/
	
	static char scanChar(const char* string)
	/* return the first character from string; convert "\t" to the TAB character */
	{
		char buffer;
		
		if(string[0]=='\\' && string[1]=='t') buffer='\t';
		else buffer=string[0];
		return buffer;
	}

	
	
@end


