/*	XGDocument.cpp
 *
 *		The basic document processing class
 */

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

  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>			// must be first to optimize compile on MSVC++
#include <XDocument.h>
#include <XWindow.h>
#include <XForm.h>
#include <XMessages.h>
#include <XApplication.h>
#include <XAppMenu.h>

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGDocument::XGDocument
 *
 *		Create this
 */

XGDocument::XGDocument()
{
	fDirty = false;
	fForm = NULL;
	fList = NULL;
	XGBroadcast::AddBroadcast(this);
}

/*$	XGDocument::~XGDocument
 *
 *		Delete this
 */

XGDocument::~XGDocument()
{
	while (fList) fList->DetachDocument();
	while (fForm) delete fForm;
}


/************************************************************************/
/*																		*/
/*	Dirty Bit															*/
/*																		*/
/************************************************************************/

/*$	XGDocument::IsDocumentDirty
 *
 *		Is this document dirty?
 */

bool XGDocument::IsDocumentDirty()
{
	return fDirty;
}

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

BEGINDISPATCH(XGDocument,XGDispatch)
	DISPATCHTABLE(KXGAppPostEvent,XGDocument::DoPostPeriodic)
	DISPATCHTABLE(KXGGetMenuStatus,XGDocument::DoGetMenuStatus)
	DISPATCHTABLE(KXGDoMenuCommand,XGDocument::DoMenuCommand)
ENDDISPATCH

/*$	XGDocument::DoPostPeriodic
 *
 *		Do the periodic event
 */

long XGDocument::DoPostPeriodic(long, void *)
{
	XGDocEditor *editor;
	XGDocForm *form;

	if (fChanged) {
		fChanged = false;
	
		for (editor = fList; editor != NULL; editor = editor->fNext) {
			editor->DoDocumentChanged();
		}
		for (form = fForm; form != NULL; form = form->fNext) {
			form->DoDocumentChanged();
		}
	}
	return -1;
}

/*$	XGDocument::DoGetMenuStatus
 *
 *		I handle save, save as and revert
 */

long XGDocument::DoGetMenuStatus(long arg, void *parg)
{
	XGSMenuStatus *status = (XGSMenuStatus *)parg;
	
	switch (arg) {
		case KXGFileSave:
			status->enable = IsDocumentDirty();
			return 0;
		case KXGFileSaveAs:
			status->enable = true;
			return 0;
		case KXGFileRevert:
			status->enable = IsDocumentDirty() && IsDocumentNamed();
			return 0;
	}
	return -1;
}

/*$	XGDocument::DoMenuCommand
 *
 *		Handle this menu command
 */

long XGDocument::DoMenuCommand(long arg, void *)
{
	switch (arg) {
		case KXGFileSave:
			DoDocumentSave();
			return 0;
		case KXGFileSaveAs:
			DoDocumentSaveAs();
			return 0;
		case KXGFileRevert:
			DoDocumentRevert();
			return 0;
	}
	return -1;
}

/************************************************************************/
/*																		*/
/*	Message dispatch													*/
/*																		*/
/************************************************************************/

/*$	XGDocument::GetParentDispatch
 *
 *		Get the parent's of this object.
 */

XGDispatch *XGDocument::GetParentDispatch()
{
	return XGAppCore::GetApplication();
}

/************************************************************************/
/*																		*/
/*	Editor Support														*/
/*																		*/
/************************************************************************/

/*$	XGDocument::DocumentChanged
 *
 *		Mark this document as changed. This sets the dirty bit, and
 *	sets an internal bit to send the document changed event later on
 */

void XGDocument::DocumentChanged()
{
	fDirty = true;
	fChanged = true;
}

/*$	XGDocument::DocumentRevert
 *
 *		Mark this document as reverted. This clears the dirty bit, and
 *	sets an internal bit to send the document changed event later on
 */

void XGDocument::DocumentRevert()
{
	XGDocEditor *editor;
	XGDocForm *form;

	fDirty = false;
	fChanged = true;
	
	for (editor = fList; editor != NULL; editor = editor->fNext) {
		editor->DoDocumentRevert();
	}
	for (form = fForm; form != NULL; form = form->fNext) {
		form->DoDocumentRevert();
	}
}

/*$	XGDocument::DocumentRenamed
 *
 *		This should be called when this document is renamed; this
 *	tells all my editors that my name was changed.
 */

void XGDocument::DocumentRenamed()
{
	XGDocEditor *editor;
	XGDocForm *form;
	
	for (editor = fList; editor != NULL; editor = editor->fNext) {
		editor->DoDocumentRenamed();
	}
	for (form = fForm; form != NULL; form = form->fNext) {
		form->DoDocumentRenamed();
	}
}

/*$	XGDocument::DocumentSaved
 *
 *		This shoudl be called when the document is saved. This
 *	clears the dirty bit and sends message
 */

void XGDocument::DocumentSaved()
{
	XGDocEditor *editor;
	XGDocForm *form;

	fDirty = false;
	for (editor = fList; editor != NULL; editor = editor->fNext) {
		editor->DoDocumentSaved();
	}
	for (form = fForm; form != NULL; form = form->fNext) {
		form->DoDocumentSaved();
	}
}

