/*

File AppDelegate.m

The application delegate is responsible for updating menus and handling basic menu commands.

*/

#import <appkit/appkit.h>

#import "AppDelegate.h"
#import "WinDelegate.h"
#import "Crossword.h"
#import "Inspector.h"
#import "filing.h"


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


#define	PLAINMENU		0
#define	PUZZLEMENU		1
#define NUMBERMENU		2
#define CONTINUEMENU	3

static BOOL				isNumbered = NO;
static const char *		numberMenuTitle [] = {

		"Number",
		"Unnumber"
	};

static char *	types [] = {

		"xword",
		"XWORD",	NULL
	};

static const char *	pasteTypes [] = {

		NULL,
		NULL
	};

static void		initMenu	(id);


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


@implementation AppDelegate


- appDidInit: sender
{
	initMenu([NXApp  mainMenu]);
	[NXApp  setAutoupdate: YES];

	return self;
}


- (BOOL) appAcceptsAnotherFile: sender
{
	return YES;
}


- (BOOL) app: sender  openFile: (const char *) file  type: (const char *) type
{
	[NXApp  loadNibSection: "Puzzle.nib"  owner: inspector];
	[NXGetNamedObject("Puzzle", inspector)  readPuzzle: file];
	
	return YES;
}


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


- new: sender
{
	[NXApp  loadNibSection: "Puzzle.nib"  owner: inspector];
	[NXGetNamedObject("Puzzle", inspector)  new];

	return self;
}


- open: sender
{
	const char	* filename;
	
	if ((filename = fileForOpen(types)) != NULL)
	{
		[NXApp  loadNibSection: "Puzzle.nib"  owner: inspector];
		[NXGetNamedObject("Puzzle", inspector)  readPuzzle: filename];
	}
	
	return self;
}


- copyTime: sender
{
	id			pasteboard;
	const char	* value;
	
	pasteboard = [Pasteboard  new];
	value = [[inspector  getTimeCell]  stringValue];
	pasteTypes[0] = NXAsciiPboardType;
	
	while (*value == ' ') value++;
	
	[pasteboard
			declareTypes: pasteTypes
			num: 1
			owner: nil];
	
	[pasteboard
			writeType: NXAsciiPboardType
			data: value
			length: strlen(value)];
	
	return self;
}


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


- (BOOL) menuItemUpdate: menuCell
{
	id		puzzle;
	BOOL	status, update;
	
	switch ([menuCell  tag])
	{
		case PUZZLEMENU:
			if ((status = ([self  mainCrossword] != nil)) != [menuCell  isEnabled])
			{
				[menuCell  setEnabled: status];
				return YES;
			}
			
			else return NO;
			break;
			
		case NUMBERMENU:
			update = NO;
			
			if (((puzzle = [self  mainCrossword]) != nil) != [menuCell  isEnabled])
			{
				[menuCell  setEnabled: puzzle != nil];
				update = YES;
			}
			
			if ([puzzle  getNumbered] != isNumbered)
			{
				[menuCell  setTitleNoCopy:
								numberMenuTitle[isNumbered = !isNumbered]];
				update = YES;
			}
			
			return update;
			break;
			
		case CONTINUEMENU:
			if ((status = [[self  mainPuzzle]  canContinue])
										!= [menuCell  isEnabled])
			{
				[menuCell  setEnabled: status];
				return YES;
			}
			
			else return NO;
			break;			
			
		default:
			return NO;
			break;
	}
}


- mainCrossword		{	return [[NXApp  mainWindow]  firstResponder];	}
- mainPuzzle		{	return [[NXApp  mainWindow]  delegate];			}


@end


/* 行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行行  */


/*

Here is code for updating the menu items.  Some menus need a current scene.  Others need a first responder.  The code is taken from NeXTanswers appkit #636.

*/

static void initMenu (id menu)
{
	int	i;
	id	matrix, cell;

	matrix = [menu  itemList];
	i = [matrix  cellCount];
	
	while (i--)
	{
		cell = [matrix  cellAt: i:0];
		if ([cell  tag] != PLAINMENU)
				[cell  setUpdateAction: @selector(menuItemUpdate:)  forMenu: menu];
		
		else if ([cell hasSubmenu])
				initMenu([cell  target]);
	}
}