/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2000/02/08
// RCS-ID:      $Id: res_doc.h,v 1.6 2001/03/06 01:25:29 aleksandras Exp $
// Copyright:   (c) Aleksandras Gluchovas
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifndef __RES_DOC_G__
#define __RES_DOC_G__

#include "wx/pmf/pm.h"

#include "res_templ.h"
#include "txtrecset.h"

#include "wx/dialog.h"
#include "wx/listbox.h"
#include "wx/hash.h"

class ResItemDocumentBase;

// container for multiple ResItemDocumentBase children (one for each top-level item)

class ResDocument : public wxPMDocument
{
public:
	ResDocument();
	~ResDocument();

	static ResTemplateManager& GetTemplateManager();

	void GetTopLevelRecordsByType( const wxString& type, FieldListT& toLst );

	// overriden methods of wxPMDocument
	virtual bool CanHaveFile();
	virtual bool SavePMDocument( wxOutputStream& s );
	virtual bool LoadPMDocument( wxInputStream& s );
	virtual void OnActivatePMDocument();
	virtual void OnClosingChildDocument( wxPMDocument* pChild, bool wasSaved );

	// overriden methods of wxDocument
	virtual bool OnCreate(const wxString& path, long flags);

	// other methods

	virtual wxString GetItemClassById( const wxString& itemId );

	virtual void GetSubItemIds( const wxString& parentItem,
								wxStringList& ids,
								wxStringList& classes,
								wxStringList& subclasses );

	virtual void SetItemSubclass( const wxString& itemId, const wxString& subclass );

	Record* GetRootRecord();
	Record* GetCurrentRecord();

	virtual Record* FindTopLevelItem( const wxString& itemId );
	virtual bool ReplaceTopLevelItem( const wxString& itemId, Record* pWithRec );
	virtual wxString SelectItem( const wxString& dlgCaption );

	ResItemDocumentBase* ActivateEditorForItem( const wxString& itemName );
	bool IsActivated( const wxString& itemName );
	bool RemoveItemByName( const wxString& itemName ); 
	Record* FindItemByName( const wxString& itemName ); 

	wxDMClipboard& GetClipboard();

	DECLARE_DYNAMIC_CLASS( ResDocument )

protected:
	virtual void GetSubItemIdsRecursive( Record& rec,
										 wxStringList& ids,
										 wxStringList& classes,
										 wxStringList& subclasses );

	virtual void GetSubItemIdsRecursive( wxDMNode& node,
										 wxStringList& ids,
										 wxStringList& classes,
										 wxStringList& subclasses );

	virtual void SetItemSubclassRecursive( wxDMNode& node, 
											  const wxString& itemId,
											  const wxString& subclass );

	const wxString& DoGetWidgetClass( const wxString& declClass );

	void TranslateFieldRecursively( FieldBase& fld, bool afterLoading );

protected:
	Record* mpRoot;
	Record* mpCurRec;
	wxHashTable mDocsHash; // maps top-level item-names to "wxPMDocument"s
	wxHashTable mRecsHash; // maps top-level "wxPMDocument"s to "Record"s

	static wxDMClipboard* mpClipboard;      // application-wide clipboard object 
	static ResTemplateManager* mpGlobalMgr; // -/- item templates
};

class ResSelectionDialog : public wxDialog
{
public:
	ResSelectionDialog( wxWindow* parent, ResDocument* pDoc );

	void OnOk( wxCommandEvent& event );
	void OnNew( wxCommandEvent& event );
	void OnRemove( wxCommandEvent& event );
	void OnSelected( wxCommandEvent& event );
	void OnDblClick( wxCommandEvent& event );

	enum
	{
		ID_NEW = 110,
		ID_REMOVE,
		ID_LIST_BOX
	};

	DECLARE_EVENT_TABLE()

protected:
	void FillListBox();

protected:
	wxListBox* mpBox;
	wxButton*  mpOkBtn;
	wxButton*  mpNewBtn;
	wxButton*  mpRemoveBtn;
	wxButton*  mpCancelBtn;

	ResDocument* mpDoc;
};

// base for documents which aim editing one item out of many
// contained in ResDocument

class ResItemDocumentBase : public wxPMDocument
{
public:
	virtual void SetRootNode( wxDMNode* pRoot ) = 0;
	virtual wxDMNode* GetRootNode() = 0;

	virtual WidgetResTemplate* GetTemplateForNode( wxDMNode& node ) = 0;

	DECLARE_ABSTRACT_CLASS( ResItemDocumentBase )
};

#endif // __RES_DOC_G__