/////////////////////////////////////////////////////////////////////////////
// Name:        container.cc
// Purpose:     Data Designer Project
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: container.cc,v 1.3 2003/12/28 18:51:43 horakdan Exp $
// Copyright:   (c) Daniel Horak
// Licence:     GPL
/////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWindows headers
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <wx/treectrl.h>
#include "config.h"
#include "container.h"
#include "xml.h"
#include "objects/dbobject.h"
#include "schema.h"

DataDesignerContainer::DataDesignerContainer(DataDesignerProject *project, const wxTreeItemId& parent, const wxString& name)
	: m_name(name), m_treeitemid(parent), m_enabled(TRUE), m_project(project), m_list(NULL), m_lastid(0)
{
}

DataDesignerContainer::~DataDesignerContainer()
{
	m_project->Delete(m_treeitemid);
}

void DataDesignerContainer::LoadXmlNode(wxXmlNode *node)
{
	wxXmlNode	*child;
	DBObject	*object;
	wxString	number;
	
	if (node->GetName() == m_name) {
		number = node->GetPropVal("lastid", "0");
		m_lastid = wxAtoi(number);
	
		child = node->GetChildren();
		while (child) {
			object = CreateObject();
			if (object) {
				object->AppendItem();
				object->LoadXmlNode(child);
			}
			child = child->GetNext();
		}
	}
}

DBObject *DataDesignerContainer::CreateObject()
{
	return NULL;
}

wxXmlNode *DataDesignerContainer::GetXmlNode()
{
	wxXmlNode	*node;
	wxTreeItemId	child;
	long		cookie;
	wxString	number;
	
	node = new wxXmlNode(wxXML_ELEMENT_NODE, m_name);
	number.Printf("%d", m_lastid);
	node->AddProperty("lastid", number);
	
	child = m_project->GetFirstChild(m_treeitemid, cookie);
	while (child.IsOk()) {
		node->AddChild((((DataDesignerItemData *)m_project->GetItemData(child))->GetObject())->GetXmlNode());

		child = m_project->GetNextChild(m_treeitemid, cookie);
	}
	
	return node;
}

wxString **DataDesignerContainer::ListNames()
{
	int		count;
	wxString	**strings;
	wxTreeItemId	child;
	long		cookie;
	int		idx;
	
	count = m_project->GetChildrenCount(m_treeitemid, FALSE);
	strings = new wxString * [count+1];
	
	idx = 0;
	child = m_project->GetFirstChild(m_treeitemid, cookie);
	while (child.IsOk()) {
		strings[idx++] = &(((DataDesignerItemData *)m_project->GetItemData(child))->GetObject())->GetName();
		child = m_project->GetNextChild(m_treeitemid, cookie);
	}
	strings[idx] = NULL;
	
	return strings;
}

DBObject *DataDesignerContainer::GetObjectByName(const wxString& name)
{
	DBObject	*object = NULL;
	wxTreeItemId	child;
	long		cookie;

#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerContainer::GetObjectByName - " + name);
#endif

	child = m_project->GetFirstChild(m_treeitemid, cookie);
	while (child.IsOk()) {
		object = ((DataDesignerItemData *)m_project->GetItemData(child))->GetObject();
		if (object->GetName() == name)
			break;
		
		child = m_project->GetNextChild(m_treeitemid, cookie);
		object = NULL;
	}
	
	return object;
}

void DataDesignerContainer::AddObjectsToList()
{
	DBObject	*object = NULL;
	wxTreeItemId	child;
	long		cookie;
	long		item;

	if (m_list) {
		item = 0;
		child = m_project->GetFirstChild(m_treeitemid, cookie);
		while (child.IsOk()) {
			object = ((DataDesignerItemData *)m_project->GetItemData(child))->GetObject();
			m_list->AddObject(item++, object);
			child = m_project->GetNextChild(m_treeitemid, cookie);
		}
	}
}

void DataDesignerContainer::AddObjectsToListAndShow()
{
	if (m_list) {
		AddObjectsToList();
		
		DataDesignerSplitter *splitter = m_project->GetSplitter();
		wxWindow *win2 = splitter->GetWindow2();
		if (splitter->ReplaceWindow(win2, m_list)) {
			m_list->Show(TRUE);
			
			// destroy "closed" window
			if (win2)
				win2->Destroy();
		}
	}
}

void DataDesignerContainer::AddObjectsToSchema()
{
	DBObject	*object = NULL;
	DataDesignerSchema	*schema;
	wxTreeItemId	child;
	long		cookie;

	schema = m_project->GetSchema();
	if (! schema) {
		wxLogMessage("DataDesignerContainer::AddObjectsToSchema - no schema");
		return;
	}
	
	child = m_project->GetFirstChild(m_treeitemid, cookie);
	while (child.IsOk()) {
		object = ((DataDesignerItemData *)m_project->GetItemData(child))->GetObject();
		if (object) {
			schema->AddObject(object);
		}
		
		child = m_project->GetNextChild(m_treeitemid, cookie);
	}
}

int DataDesignerContainer::GetChildrenCount()
{
	int	res = 0;

	if (m_project != NULL) {
		res = m_project->GetChildrenCount(m_treeitemid, FALSE);
	}

	return res;
}

void DataDesignerContainer::OnDraw(wxDC& dc)
{
	DBObject	*object = NULL;
	DataDesignerSchema	*schema;
	wxTreeItemId	child;
	long		cookie;
	wxShape		*shape;

	schema = m_project->GetSchema();
	if (! schema)
		return;
	
	child = m_project->GetFirstChild(m_treeitemid, cookie);
	while (child.IsOk()) {
		object = ((DataDesignerItemData *)m_project->GetItemData(child))->GetObject();
		if (object) {
			if (! object->GetShape())
				object->CreateShape();
				
			if ((shape = object->GetShape()))
				shape->Draw(dc);
		}
		
		child = m_project->GetNextChild(m_treeitemid, cookie);
	}
}


syntax highlighted by Code2HTML, v. 0.9.1