ftp.nice.ch/peanuts/GeneralData/Documents/books/AlexNeXTSTEPSource.tar.gz#/NSProgramming/Chapter10_Help/Words/TextController.m

This is TextController.m in view mode; [Download] [Up]

#import <appkit/appkit.h>
#import "Document.h"
#import "TextController.h"

# define FILE_EXTENSION "word"

// this class is responsible for managing
// the savepanel, openpanel and files opened
// from the Workspace
@implementation TextController

// automatically sent after the .nib file
// has finished loading
- awakeFromNib
{
	// create the savepanel and openpanel
	savePanel = [SavePanel new];
	openPanel = [OpenPanel new];
	// by default, an openpanel only allows
	// one file to be opened
	[openPanel allowMultipleFiles:YES];
	return self;
}

// create a new document in response
// to the New menu option
- newDocument:sender
{
	id document = [[Document alloc] init];
	[document showDocument];
	return self;
}

- (BOOL)appAcceptsAnotherFile:sender
{
	// Double-clicking on a file in the WSM
	// causes the following actions:
	// * NeXTSTEP sends an openFile:ok
	// 	message to NXApp
	// * openFile:ok sends a appAcceptsAnotherFile:
	//	message to check if it's OK to open
	//	a file; by returning YES, the app
	//	allows files to be opened from the WSM
	// * openFile:ok then sends a
	//	app:openFile:type message to actually
	//	open the file; the app:openFile:type
	//	should return YES if successful and
	//	NO if not
	// the printf statements illustrate the orders
	// these messages are being sent
	printf("in appAcceptsAnotherFile:\n");
	return YES;
}

// actually open the file here
- (int)app:sender openFile:(const char*)filename
	type:(const char *)filetype
{
	printf("In app:openFile:type:\n");
	return ([[Document alloc]
		initDocumentFromFile:filename]);
}

// open the document in response to
// the Open menu option
- showOpenPanel:sender
{
	const char *file, *directory;
	const char *const *filenames;
	static const char
		*const wordTypes[] = {FILE_EXTENSION, NULL};
	char fullPathName[81];

	// display only files with "word" extension
	if ([openPanel runModalForTypes:wordTypes])
		{
		// get list of filename(s) selected
		// the filenames method returns a
		// a pointer to all the strings
		filenames = [openPanel filenames];
		do
			{
			// get directory first
			directory = [openPanel directory];
			// get filename
			file = *(filenames++);
			// construct entire pathname
			strcpy(fullPathName, directory);
			// append directory to filename
			strcat(fullPathName, "/");
			strcat(fullPathName, file);
			[[Document alloc]
				initDocumentFromFile:fullPathName];
			}
		while (*(filenames) != NULL);
	    	}
	return self;
}

// allow the document to be saved under
// a different name
- saveAs:sender
{
	const char *wordType = FILE_EXTENSION;
	const char *fullPathName;
	id document = [[NXApp mainWindow] delegate];
	const char *filename = "";

	[savePanel setRequiredFileType:wordType];
	if ([savePanel runModal])
		{
		fullPathName = [savePanel filename];
		[document saveDocumentToFile:fullPathName];
		}
	return self;
}

// save the document in response to
// the Save menu option
- showSavePanel:sender
{
	const char *wordType = FILE_EXTENSION;
	const char *fullPathName;
	id document = [[NXApp mainWindow] delegate];
	const char *filename = [document filename];

	// if document has not been saved
	// then present savepanel to get filename
	if (!filename)
		{
		[savePanel setRequiredFileType:wordType];
		if ([savePanel runModal])
			{
			fullPathName = [savePanel filename];
			[document saveDocumentToFile:fullPathName];
			}
		}
	// else just save the file
	else
		[document saveDocumentToFile:filename];
	return self;
}

// return the id of the savepanel
- savePanel
{
	return savePanel;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.