/*	XDocument.h
 *
 *		This is the root document class.
 *
 *		The way the document works is to insert itself in the XGDispatch
 *	list between the XGAppCore class and the XGWindow which displays
 *	the document's contents.
 *
 *		A document then can watch incomming messages and help regulate
 *	how those messages are handled
 */

/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

#ifndef PandaWave_XDOCUMENT
#define PandaWave_XDOCUMENT

#include <XConfig.h>
#include <XEvent.h>
#include <XDynArray.h>

/************************************************************************/
/*																		*/
/*	Randomness															*/
/*																		*/
/************************************************************************/

class XGWindow;

/************************************************************************/
/*																		*/
/*	Structures															*/
/*																		*/
/************************************************************************/

/*	_DocRecord
 *
 *		Document record which stores what's in my window list
 */

struct _DocRecord {
	bool			fSubDoc;				// true if window views or edits part
	XGWindow		*fWindow;				// the window
};

/************************************************************************/
/*																		*/
/*	Classes																*/
/*																		*/
/************************************************************************/

/*	@C"XGDocument"
 *	@D"XGDispatch" @D"XGSend"
 *	@H"XDocument.h"
 *	@I
 *
 *		This is the basic document class. This is the root object used
 *	for defining a document, a database which mediates between the contents
 *	of a file and the editing done in a window.
 */

class XGDocument : public XGDispatch, public XGSend {
	public:
								XGDocument();
		virtual					~XGDocument();
		
		/*
		 *	Window management
		 */
		
		void					AttachWindow(XGWindow *w, bool sdf);
		void					DetachWindow(XGWindow *w);
		long					GetNumAttachWindows(void);
		XGWindow				*GetAttachedWindow(long i);
		bool					LastEditWindow(XGWindow *);
		
		/*
		 *	Document message wrappers
		 */
		
		// @G"Document message wrappers"
		
		void					DocumentRenamed();
		void					DocumentSaved();
		
		/*	@F"XGDocument::DocumentChanged" @I
		 *
		 *		This method is called by the document when it's contents
		 *	are changed. This marks the document as changed, and at the
		 *	end of the current event loop, dispatches a message that
		 *	this document changed to all registered dispatch listeners.
		 */
		
		void					DocumentChanged()
									{
										fChanged = true;
									}
				
		/*
		 *	Document save/restore hooks
		 */
		
		// @G"Document save/restore hooks"
		
		/*	@F"XGDocument::IsDocumentNamed" @I
		 *
		 *		Returns 'true' if this document has a file name.
		 */
		
		virtual bool			IsDocumentNamed() = 0;

		/*	@F"XGDocument::IsDocumentDirty" @I
		 *
		 *		Returns 'true' if this document is dirty and needs to be
		 *	saved to file.
		 */
		
		virtual bool			IsDocumentDirty() = 0;

		/*	@F"XGDocument::GetDocumentName" @I
		 *
		 *		Returns the name of this document.
		 */
		
		virtual void			GetDocumentName(char *) = 0;

		/*	@F"XGDocument::DoDocumentSave" @I
		 *
		 *		Attempts to save this document. Returns 'true' if the
		 *	file was successfully saved.
		 */
		
		virtual bool			DoDocumentSave() = 0;

		/*	@F"XGDocument::DoDocumentSaveAs" @I
		 *
		 *		Attempts to save this document to a new file. Returns
		 *	'true' if successful.
		 */
		
		virtual bool			DoDocumentSaveAs() = 0;

		/*	@F"XGDocument::DoDocumentRevert" @I
		 *
		 *		Reverts the contents of this file from the values
		 *	written to disk.
		 */
		
		virtual void			DoDocumentRevert() = 0;

		DECLAREDISPATCH
		long					DoGetMenuStatus(long,void *);
		long					DoMenuCommand(long,void *);	
		long					DoPostPeriodic(long, void *);	

	private:
		bool					fChanged;
		void					DispatchChanged();

		XGSDynArray<_DocRecord>	fWindowList;
};


#endif /* PandaWave_XDOCUMENT */
