/*	NibList - A program to list the contents of nib files.
    Copyright (C) 1991  Oceania Healthcare Systems

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program, in a file called COPYING; if not, write to
	the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*  This file (and some of NibFile.m) is a perfect example of
**  cut-and-paste programming.  It is a stripped down version of
**  the file of the same name in /NextDeveloper/Examples/TextLab.
**  Where would we be without those guys?

**  Controller.m --- Main controller object for the TextLab program.
**  Authors: Bruce Blumberg and Ali Ozer, NeXT Developer Support Group
**	8-14-90 Converted to 2.0 by Randy Nelson, NeXT Developer Training
**	Changed internal strings to external using NXStringTable
**	Removed outlet initializers
**	Removed 0.9 reference to Font Manager
**	9-7-90 Changed new method to use init, externally stored info panel
**
**	You may freely copy, distribute and reuse the code in this example.
**	NeXT disclaims any warranty of any kind, expressed or implied, as to
**	its fitness for any particular use.

**  3-25-91 Gutted and mangled for NibList application by Russell Selph
**    at Oceania Healthcare Systems.
**  Removed all save methods (but left hooks for later.)
**  Made window creation zone conscious.
**  Added gnu software license panel
**  Changed treatment of openPanel, other details.
**   
*/

// Controller object is the central object in NibList.  It manages the
// windows, open/save panels, and menu commands. 

#import "Controller.h"
#import "NibFile.h"
#import <appkit/Application.h>
#import <objc/typedstream.h>
#import <objc/NXStringTable.h>
#import <appkit/FontManager.h>
#import <zone.h>
#import <mach.h>

@implementation Controller

- appDidInit:sender
{  
  // get instances of support objects
  openReq = [OpenPanel new];
  saveReq = [SavePanel new];
  return self;

}

- infoPanel:sender
{
    if (infoPanel == nil) {
	[NXApp loadNibSection:"Info.nib" owner:self];
    }
    [infoPanel orderFront:sender];
    return self;
}

- gnuPanel:sender
{
    if (gnuPanel == nil) {
	[NXApp loadNibSection:"Info.nib" owner:self];
    }
    [gnuPanel orderFront:sender];
    return self;
}

- showError: (const char *)errorMessage
{
  NXRunAlertPanel(NULL, errorMessage,
  		  [stringSet valueForStringKey:"OK"], NULL, NULL);
  return self;
}


// appAcceptsAnotherFile is an application delegate method which 
// returns whether it is OK for the application to try to open more files
// with the appOpenFile:type: method. NibList can indeed open multiple
// windows, so we return YES.

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


// appOpenFile:type: is called to open the specified file. It is normally
// called by the Application object in response to open requests from the
// Workspace. Here we also route the open requests from the OpenPanel
// to this method (see openRequest:).

-(int) appOpenFile:(char *)fileName type:(char *)fileType
{
	if( !strcmp(fileType, [stringSet valueForStringKey:"extension"]))
	   return [self openFile:fileName];
	else
		return NO;
}

// openRequest: opens a new file. It puts up a open panel, and, if the user
// doesn't cancel, it reads the specified archive file. If the selected file
// is not a proper archive file, then openRequest: will complain.

- openRequest:sender
{
    const char *fileName;
    const char *const types[2] = {[stringSet valueForStringKey:"extension"],
    				  NULL};

    if ([openReq runModalForTypes:types] && (fileName = [openReq filename])) {
	[self openFile:fileName];
    }
    else
	[self showError:[stringSet valueForStringKey:"errorOnOpenRequest"]];
    return self;
}

-(int) openFile:(const char *)fileName
{
   id newFile;
   
    /* allocate a new zone for the NibFile, allocate the new NibFile in
	     that zone, and init: it with the filename */
    if(!(newFile = [[NibFile allocFromZone:NXCreateZone(vm_page_size, vm_page_size, YES)]
						initFile:fileName])){
		[self showError:[stringSet valueForStringKey:"errorOnOpen"]];
		return NO;
    }
    else {
		[[newFile window] setDelegate:self];
		return YES;
    } 
}

/******  The following three save methods are only left as hooks for later use. *****/
// saveRequest: saves the current window under its default name (found in
// the title bar). Note that if the title bar is empty or the default title
// is "Untitled" then saveRequest: will put up a save panel, giving the user
// a chance to specify a real title.

- saveRequest:sender
{
    return self;
}

// saveInRequest: gives the user a chance to save the current window
// under a new name. 

- saveInRequest:sender
{
    return self;
}


// saveWindow writes a window out the archive file whose name is specified
// by the second argument. The title of the current window is also set 
// accordingly. 

- saveWindow:(id)win inPath:(const char *)name
{	
    return self;
}

// Printing is rather simple; just send printPSCode: to the text view
// you wish to print. The print panel will automatically pop up and unless
// the user cancels the printout the text view will be printed.

- printRequest:sender
{
    id curText = [[[NXApp mainWindow] contentView] docView];

    if (curText == nil) [self showError:[stringSet
    				    valueForStringKey:"errorOnPrintRequest"]];
    else {
		[[[NXApp printInfo] setHorizCentered:NO] setVertCentered:NO];
		[curText printPSCode:self];
    }
    return self;
}

// closeRequest closes the current window by simulating a click on the
// closebutton. A check should probably be added to give the user the 
// option of saving the window before closing

- closeRequest:sender
{
   [[NXApp mainWindow] performClose:sender];
   return self;
}

- (BOOL)isSimulating
{
	return NO;
}

@end
