/*

File WinDelegate.m

The window delegate file categorizes the puzzle methods that relate to the crossword window.  These methods save a puzzle to a file, as well as handling details of resizing and inspecting.

*/

#import <appkit/appkit.h>
#import <strings.h>

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


/* ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐ  */


@implementation Puzzle (WinDelegate)


- (BOOL) save: sender
{
	if ([window  isDocEdited])
	if (filename[0] != '\0')
			
			return [self  writePuzzle: filename];	else
			return [self  saveAs: self];
	
	return YES;
}


- (BOOL) saveAs: sender
{
	const char	* file;
	
	if ((file = fileForSave("xword")) != NULL)
	if ([self  writePuzzle: file])
	{
		strcpy(filename, file);
		[self  updateTitle];
		return YES;
	}
	
	return NO;
}


- (BOOL) saveTo: sender
{
	const char 	* file;
	
	if ((file = fileForSave("xword")) != NULL)
	
		return [self  writePuzzle: file];		else
    	return NO;
}


- (BOOL) writePuzzle: (const char *) file
{
	NXTypedStream	* stream;
	
	if ((stream = NXOpenTypedStreamForFile(file, NX_WRITEONLY)) != NULL)
	{
		NXWriteRootObject(stream, crossword);
		NXCloseTypedStream(stream);
		[window  setDocEdited: NO];
		return YES;
	}
	
	else return NO;
}


/* ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐ  */


/*

Opening a puzzle from a file is a two step process.  First the puzzle is unarchived.  Then the window is adjusted to reflect its new contents.

*/


- readPuzzle: (const char *) file;
{
	NXTypedStream		* stream;
	
	strcpy(filename, file);
	stream = NXOpenTypedStreamForFile(filename, NX_READONLY);
	
	[self  changePuzzle: NXReadObject(stream)];
	[self  new];
	
	NXCloseTypedStream(stream);
	return self;
}


- new
{
	[window  makeFirstResponder: crossword];
	[window  center];
	[window  makeKeyAndOrderFront: self];
	
	return self;
}


/* ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐ  */


- number: sender
{
	if ([crossword  getNumbered])
			[crossword  unnumber];		else
			[crossword  number];
	
	return self;
}


- use: sender
{
	[dictionary  use: sender];
	return self;
}


- updateTitle
{
	char	* file;
	char	buffer [MAXLENGTH];
	int		width, height;
	
	[crossword  getNumRows: &height  numCols: &width];

	if (filename[0] != '\0')
	{
		file = rindex(filename, '/');
		file[0] = '\0';
		sprintf(buffer, "%s Ð %d x %d Ð %s", file + 1, height, width, filename);
		file[0] = '/';
	}
	
	else sprintf(buffer, "New Puzzle Ð %d x %d", height, width);
	[window  setTitle: buffer];
	
	return self;
}


- changePuzzle: (id) newPuzzle
{
	NXRect		frame, old, new;
	float		dw, dh;
	
	[newPuzzle  getBounds: &new];
	[crossword  getBounds: &old];
	[[window  contentView]  getBounds: &frame];
	
	dw = new.size.width - old.size.width;
	dh = new.size.height - old.size.height;
	
	[window  sizeWindow: frame.size.width + dw: frame.size.height + dh];
	[[window  contentView]  replaceSubview: crossword  with: newPuzzle];
	[crossword  free];
	crossword = newPuzzle;
	
	[self  updateTitle];
	[crossword  setInspector: self];
	
	return self;
}


- update: sender
{
	[window  setDocEdited: YES];
	return self;
}


/* ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐ  */


/*

Here are the standard window delegate methods.  Resizing should be restricted to integral multiples of the squarewidth.

*/

- windowWillResize: (id) theWindow  toSize: (NXSize *) size
{
	int		spaceWidth;
	NXRect	frame;
	
	spaceWidth = [crossword  getSquareWidth] + INTERCELLWIDTH;
	[theWindow  getFrame: &frame];
	size->width -= ((int) (size->width - frame.size.width)) % spaceWidth;
	size->height -= ((int) (size->height - frame.size.height)) % spaceWidth;
	
	return self;
}


- windowDidResize: sender
{
	[self  updateTitle];
	return self;
}


- windowDidClose: sender
{
	[self  free];
	return self;
}


- windowDidBecomeMain: sender
{
	id		manager;
	
	[manager = [FontManager  new]  setEnabled: YES];
	[manager  setSelFont: [crossword  font]  isMultiple: NO];
	
	return self;
}


- windowDidResignMain: sender
{
	if ([NXApp  mainWindow] == nil) [[FontManager  new]  setEnabled: NO];
	return self;
}


@end