// MOTextDocController.m
//
// by Mike Ferris
// Part of MOKit
// Copyright 1993, all rights reserved.

// ABOUT MOKit
// 
// MOKit is a collection of useful and general objects.  Permission is 
// granted by the author to use MOKit in your own programs in any way 
// you see fit.  All other rights to the kit are reserved by the author 
// including the right to sell these objects as part of a LIBRARY or as 
// SOURCE CODE.  In plain English, I wish to retain rights to these 
// objects as objects, but allow the use of the objects as pieces in a 
// fully functional program.  NO WARRANTY is expressed or implied.  The author 
// will under no circumstances be held responsible for ANY consequences to 
// you from the use of these objects.  Since you don't have to pay for 
// them, and full source is provided, I think this is perfectly fair.

#import "MOTextDocController.h"
#import "MOKit/MOPathString.h"

#define CLASS_VERSION			0

#define CLASS_NIB				"MOTextDocController.nib"

@implementation MOTextDocController

+ initialize
// Set the version.  Configure the class.
{
	if (self == [MOTextDocController class])  {
		[self setVersion:CLASS_VERSION];
		
		[self setClassNib:CLASS_NIB];
		[self setControllerName:"Rich Text"];
		[self addDocType:1 name:"Rich Text" extension:"rtf" 
			openSelector:@selector(doRevertRtf:) 
			saveSelector:@selector(doSaveRtf:)];
		[self addDocType:2 name:"Text Doc" extension:"txt" 
			openSelector:@selector(doRevertTxt:) 
			saveSelector:@selector(doSaveTxt:)];
		[self addDocType:3 name:"Raw Man Page" extension:"1" 
			openSelector:@selector(doRevertTxt:) 
			saveSelector:NULL];
		[self addDocType:4 name:"Ascii Text" extension:NULL
			openSelector:@selector(doRevertTxt:) 
			saveSelector:@selector(doSaveTxt:)];
	}
	return self;
}

- nibDidLoad
// Set the text object's delegate.  (We can't set it from inside the 
// scroll view in IB.)
{
	[super nibDidLoad];
	
	[textObj setDelegate:self];
	
	return self;
}

- doSaveRtf:(MODocType *)type
// The saveSelector for rich text.
{
	NXStream *strm = NULL;
	id ret = self;
	
	strm = NXOpenMemory(NULL, 0, NX_WRITEONLY);
	if (strm)  {
		[textObj setMonoFont:NO];
		[textObj writeRichText:strm];
		
		if (NXSaveToFile(strm, [self file]) == -1)  {
			ret = nil;
		}
		
		NXCloseMemory(strm, NX_FREEBUFFER);
	}  else  {
		ret = nil;
	}

	return ret;
}

- doSaveTxt:(MODocType *)type
// The saveSelector for ascii text.
{
	NXStream *strm = NULL;
	id ret = self;
	
	strm = NXOpenMemory(NULL, 0, NX_WRITEONLY);
	if (strm)  {
		[textObj setMonoFont:YES];
		[textObj setFont:[Font userFixedPitchFontOfSize:12.0 
					matrix:NX_FLIPPEDMATRIX]];
		[textObj writeText:strm];
		
		if (NXSaveToFile(strm, [self file]) == -1)  {
			ret = nil;
		}
		
		NXCloseMemory(strm, NX_FREEBUFFER);
	}  else  {
		ret = nil;
	}

	return ret;
}

- doRevertRtf:(MODocType *)type
// The openSelector for rich text.
{
	NXStream *strm = NULL;
	id ret = self;
	
	strm = NXMapFile([self file], NX_READONLY);
	if (strm)  {
		[textObj setMonoFont:NO];
		[textObj readRichText:strm];
		NXCloseMemory(strm, NX_FREEBUFFER);
	}  else  {
		ret = nil;
	}
	
	return ret;
}

- doRevertTxt:(MODocType *)type
// The openSelector for ascii text.
{
	NXStream *strm = NULL;
	id ret = self;
	
	strm = NXMapFile([self file], NX_READONLY);
	if (strm)  {
		[textObj setMonoFont:YES];
		[textObj setFont:[Font userFixedPitchFontOfSize:12.0 
					matrix:NX_FLIPPEDMATRIX]];
		[textObj readText:strm];
		NXCloseMemory(strm, NX_FREEBUFFER);
	}  else  {
		ret = nil;
	}
	
	return ret;
}

- (BOOL)doClear
// Clear the text object.
{
	if (![super doClear])  return NO;
	
	[textObj setText:""];
	[textObj setMonoFont:NO];
	return YES;
}

- (BOOL)doPrint
// Print the text object.
{
	if (![super doClear])  return NO;

	[textObj printPSCode:self];
	return YES;
}

- textObj
// Return the text object.
{
	return textObj;
}

- textDidGetKeys:sender isEmpty:(BOOL)flag
// Mark ourselves as dirty.
{
	[self setDirty:YES];
	return self;
}


@end

