/*	TTagFileDocument.cpp
 *
 *		TagFile document; this handles the document
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>
#include <XDataUtil.h>
#include <stdio.h>
#include <string.h>
#include "TTagFileDocument.h"
#include "TTagFileForm.h"
//#include "TCatalogForm.h"
//#include "TInspectorForm.h"
//#include "TResForm.h"

/************************************************************************/
/*																		*/
/*	Class Declaration													*/
/*																		*/
/************************************************************************/

/*	TTagFileDocument::TTagFileDocument
 *
 *		Create the project document object
 */

TTagFileDocument::TTagFileDocument()
{
	fFile = NULL;
	the_tag_file = 0;
}

/*	TTagFileDocument::~TTagFileDocument
 *
 *		Delete me
 */

TTagFileDocument::~TTagFileDocument()
{
	if (fFile) delete fFile;
}

/************************************************************************/
/*																		*/
/*	Events																*/
/*																		*/
/************************************************************************/

/*	TTagFileDocument::IsDocumentNamed
 *
 *		Is the document named?
 */

bool TTagFileDocument::IsDocumentNamed()
{
	if (fFile) return true;
	return false;
}

/*	TTagFileDocument::GetDocumentName
 *
 *		Get the name of the document. If not saved, return "untitled"
 */

void TTagFileDocument::GetDocumentName(char *name)
{
	if (fFile) fFile->GetFileName(name);
	else strcpy(name,"untitled");
}

/*	TTagFileDocument::DoDocumentSave
 *
 *		Save the document. If a file is not open, call save as
 */

bool TTagFileDocument::DoDocumentSave()
{
	if (fFile == NULL) return DoDocumentSaveAs();
	
	//fRes.WriteData(*fFile);
	DocumentSaved();
	//ExportAll();					// and export the three data files
	return true;
}

/*	TTagFileDocument::DoDocumentSaveAs
 *
 *		Save the document as another file
 */

bool TTagFileDocument::DoDocumentSaveAs()
{
	XGFile *oldfile;
	char oldname[256];
	
	GetDocumentName(oldname);
	oldfile = fFile;

	try {
#if OPT_MACOS
		fFile = XGFile::SaveDialog(128,'VNGN','TAGS',oldname);
#endif

#if OPT_WINOS
		fFile = XGFile::SaveDialog("Myth 3 Tag File|*.TAG","TAG",oldname);
#endif

#if OPT_XWIN
		fFile = XGFile::SaveDialog("tag",oldname);
#endif
		if (fFile == NULL) {
			fFile = oldfile;
			return false;
		}
		DocumentRenamed();
		
		return DoDocumentSave();
	}
	catch (...) {
		fFile = oldfile;
		throw;
	}
	return true;
}

/*	TTagFileDocument::DoDocumentRevert
 *
 *		Load the project. Close all of the open subwindows as well.
 */

void TTagFileDocument::DoDocumentRevert()
{
	XGDocEditor *editor, *nexteditor;
	
	if (fFile == NULL) return;

	/*
	 *	Revert data
	 */
		
	//fRes.ReadData(*fFile);
	//ReloadCatalog();
	
	/*
	 *	And close the secondary windows
	 */
	
	for (editor = GetFirstEditor(); editor != NULL; editor = nexteditor) {
		nexteditor = GetNextEditor(editor);
	/*	TResForm *f;
		
		f = dynamic_cast<TResForm *>(editor);
		if (f) delete f; */
		delete editor;
	}
	
	DocumentRevert();
}

/*	TTagFileDocument::DoOpenDocument
 *
 *		Open the document. This opens the document given it's name
 */

bool TTagFileDocument::DoOpenDocument(XGFileSpecifier *file)
{
	fFile = new XGFile;
	//fFile->Open(file,true);
	
	/* load and scan the tag file. */
	the_tag_file = scan_tag_file(*file);
	
	return true;
}

/*	TTagFileDocument::DoOpenDocument
 *
 *		Open the document. This opens the document using a dialog box.
 *	returns false if the user canceled the operation
 */

bool TTagFileDocument::DoOpenDocument()
{
#if OPT_MACOS
	fFile = XGFile::OpenDialog(128,true);
#endif

#if OPT_WINOS
	fFile = XGFile::OpenDialog("Myth 3 Tag File|*.TAG","TAG",true);
#endif

#if OPT_XWIN
	fFile = XGFile::OpenDialog("tag",true);
#endif

	if (fFile == NULL) return false;
	fFile->Close();
	
	the_tag_file = scan_tag_file(fFile->GetFileSpecifier());
	
	return true;
}

/*	TTagFileDocument::DoNewDocument
 *
 *		New document.
 */

void TTagFileDocument::DoNewDocument()
{
	fFile = NULL;
	the_tag_file = 0;
}

