/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2000/02/08
// RCS-ID:      $Id: pm_docmgr.h,v 1.7 2001/03/14 13:48:11 aleksandras Exp $
// Copyright:   (c) Aleksandras Gluchovas
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifndef __PMDOC_MGR_G__
#define __PMDOC_MGR_G__

#include "wx/pmf/pm_doc.h"

// Listener interface for receiving document-management related
// notifications

class wxPMDocumentListener
{
public:
	virtual void OnDocumentOpened( wxPMDocument& doc, bool created );
	virtual void OnDocumentClosed( wxPMDocument& doc, bool saved );

	// methods which allow listeners storing/loading their extra data
	// data which they have associated with a particular document
	// RETURN: FALSE if operation should fail

	virtual bool OnClosingDocument( wxPMDocument& doc, wxOutputStream* s, wxPMSerializer* ser );
	virtual bool OnOpeningDocument( wxPMDocument& doc, wxInputStream* s, wxPMSerializer* ser );
};

// we need our own doc-manager derivative to handle funcional enhancements
// which is not present or differ from that in wxDocManager
// TBD:: ideas...

class wxPMDocManager : public wxDocManager
{
public:
	wxPMDocManager( long flags = wxDEFAULT_DOCMAN_FLAGS, bool initialize = TRUE );
	~wxPMDocManager();

	virtual wxPMMergableMenuChain* GetMergableMenuChain();

	// API-level commands

	virtual void NewProject( wxPMFileInfo& location, wxDocTemplate& templ );
	virtual void NewWorkplace( wxPMFileInfo& location, wxDocTemplate& templ );

	virtual wxPMProject* OpenProject( const wxPMFileInfo& location );
	virtual wxPMWorkplace* OpenWorkplace( const wxPMFileInfo& location );

	// opens document from file, or activates it if already opened

	virtual wxPMDocument* OpenDocument( const wxPMFileInfo& finfo, 
										wxPMDocument* pParentDoc,
										bool activate = TRUE );

	virtual void ActivateDocument( wxPMDocument* pDoc );
	virtual void ActivatePMView( wxPMView* pView );

	// pDoc can be workplace or project or any other derivative of wxPMDocument

	virtual bool SaveDocument( wxPMDocument* pDoc ); // 
	// -/-
	virtual bool CloseDocument( wxPMDocument* pDoc );
	virtual bool CloseWorkplace();

	virtual void AddListener( wxPMDocumentListener* listener );
	virtual void RemoveListener( wxPMDocumentListener* listener );

	virtual wxPMDocument* CreatePMDocument( const wxPMFileInfo& finfo, long flags = 0,
											wxDocTemplate* pSingleTemplate = NULL,
											wxPMDocument* pParentDoc = NULL,
											bool activate = TRUE
										  );

	// overriden methods of wxDocManager

    virtual wxDocument* CreateDocument(const wxString& path, long flags = 0);
	virtual wxDocTemplate* FindTemplateForPath(const wxString& path);
	virtual wxDocTemplate* FindTemplateForDocClass( wxClassInfo* pDocClass );

	// some helpers

	virtual wxDocTemplate* FindTemplateForDocType( const wxString& docTypeName );
	virtual wxDocTemplate* FindTemplateForDocument( wxPMDocument* pDoc );

	/*** command-event handlers ***/

	void OnNew( wxCommandEvent& event );
	void OnOpenFile( wxCommandEvent& event );
	void OnOpenURL( wxCommandEvent& event );
	void OnOpenWorkplace( wxCommandEvent& event );
	void OnSaveWorkplace( wxCommandEvent& event );
	void OnCloseWorkplace( wxCommandEvent& event );
	void OnOpenProject( wxCommandEvent& event );
	void OnCloseFile( wxCommandEvent& event );
	void OnCloseAll( wxCommandEvent& event );
	void OnSave( wxCommandEvent& event );
	void OnSaveAs( wxCommandEvent& event );
	void OnSaveAsURL( wxCommandEvent& event );
	void OnSaveAll( wxCommandEvent& event );

	// IDs which can be used by other services, for
	// translation of command-events to the wxPMDocManager object

	enum COMMAND_IDS
	{
		ID_OPEN_WORKPLACE = 50,
		ID_SAVE_WORKPLACE,
//		ID_CLOSE_WORKPLACE,
//		ID_OPEN_PROJECT,
		ID_OPEN_URL,
		ID_CLOSE_ALL,
		ID_SAVE_ALL,
		ID_SAVE_AS_URL,
		ID_CLOSE_WORKPLACE,
	};

	DECLARE_EVENT_TABLE()

protected:

	// TBD:: comments...

	virtual void StartAssociatedServices( wxPMDocument& doc );
	virtual void StopAssociatedServices( wxPMDocument& doc );

	virtual wxClassInfo* GetDocClass( wxDocTemplate* pTempl );
	virtual wxPMProject* FindProjectForOtherFiles();

	virtual bool DoCreateWorkplace( wxPMProject* pFirstPrj );
	virtual wxPMProject* CreateProjectByClass( wxClassInfo* pPrjClass );
	virtual bool DoCloseDocument( wxPMDocument* pDoc, bool wasSaved );
	virtual bool DoSaveDocumentRecursively( wxPMDocument* pDoc, 
										    PMClassInfoListT& docsToSkip,
											bool closeFlag );

protected:

	PMDocumentListenerListT m_Listeners;

};

#endif // __PMDOC_MGR_G__