/////////////////////////////////////////////////////////////////////////////
// Name:        schema.cc
// Purpose:     Data Designer Schema
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: schema.cc,v 1.4 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 "config.h"
#include "schema.h"
#include "project.h"
#include "objects/dbobject.h"
#include "objects/dbmodel.h"
#include "objects/dbrelation.h"
#include "objects/dbmodelentity.h"
#include "objects/dbmodelrelation.h"

/*
 *	DataDesignerSchema
 */
BEGIN_EVENT_TABLE(DataDesignerSchema, wxShapeCanvas)
	EVT_PAINT(DataDesignerSchema::OnPaint)
END_EVENT_TABLE()

DataDesignerSchema::DataDesignerSchema(wxWindow *parent, DataDesignerProject *project)
	: wxShapeCanvas(parent), m_scale(1.0), m_project(project)
{
	SetScrollbars(20, 20, 200, 200);
	SetBackgroundColour(*wxWHITE);
	SetDiagram(new wxDiagram());
	GetDiagram()->SetCanvas(this);
}

void DataDesignerSchema::AddObjects()
{
}

void DataDesignerSchema::AddObject(DBObject *object)
{
	wxShape	*shape;
	
	if (object == NULL) {
		wxLogMessage("DataDesignerSchema::AddObject - object==NULL");
		return;
	}
	
	if (object->GetShape() == NULL)
		object->CreateShape();

	if ((shape = object->GetShape())) {
		shape->Show(TRUE);
		AddShape(shape);
	}
}

void DataDesignerSchema::OnDraw(wxDC& dc)
{
}

void DataDesignerSchema::OnPaint(wxPaintEvent& event)
{
//	wxLogMessage("DataDesignerSchema::OnPaint");
	
	wxShapeCanvas::OnPaint(event);
}

void DataDesignerSchema::PrepareDC(wxDC& dc)
{
	wxShapeCanvas::PrepareDC(dc);
	
	dc.SetUserScale(m_scale, m_scale);
}

/*
 *	DataDesignerGlobalSchema
 */
DataDesignerGlobalSchema::DataDesignerGlobalSchema(wxWindow *parent, DataDesignerProject *project)
	: DataDesignerSchema(parent, project)
{
}

void DataDesignerGlobalSchema::AddObjects()
{
	// add entites
	m_project->m_top_entities->AddObjectsToSchema();
		
	// add relations
	m_project->m_top_relations->AddObjectsToSchema();
}

#if 0
void DataDesignerGlobalSchema::AddObject(DBObject *object)
{
	DataDesignerSchema::AddObject(object);
}
#endif

void DataDesignerGlobalSchema::OnDraw(wxDC& dc)
{
#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerGlobalSchema::OnDraw");
#endif
	
	m_project->m_top_entities->OnDraw(dc);
	m_project->m_top_relations->OnDraw(dc);
}

/*
 *	DataDesignerModelSchema
 */
DataDesignerModelSchema::DataDesignerModelSchema(wxWindow *parent, DataDesignerProject *project, DBModel *model)
	: DataDesignerSchema(parent, project), m_model(model)
{
}

void DataDesignerModelSchema::AddObjects()
{
	// add entites
	m_model->m_entities->AddObjectsToSchema();
	m_model->m_relations->AddObjectsToSchema();
	
#if 0
	// add relations where both sides are in this model
	DBRelation	*rel = NULL;
	wxTreeItemId	child;
	long		cookie;
	wxShape		*shape;

	child = m_project->GetFirstChild(m_project->m_top_relations->GetTreeItemId(), cookie);
	while (child.IsOk()) {
		rel = (DBRelation *)(((DataDesignerItemData *)m_project->GetItemData(child))->GetObject());
		if (rel && rel->GetType() == DBRelationType) {
			if (m_model->m_entities->GetObjectByName(rel->m_parent) && m_model->m_entities->GetObjectByName(rel->m_child)) {
				if (rel->GetShape() == NULL)
					rel->CreateShape();
				
				shape = rel->GetShape();
				shape->Show(TRUE);
				AddShape(shape);
			}
		} else {
			wxLogMessage("DataDesignerModelSchema::AddObjects - object is not a relation");
		}
		
		child = m_project->GetNextChild(m_project->m_top_relations->GetTreeItemId(), cookie);
	}
#endif
}

#if 0
void DataDesignerModelSchema::AddObject(DBObject *object)
{
	DataDesignerSchema::AddObject(object);
}
#endif

void DataDesignerModelSchema::OnDraw(wxDC& dc)
{
#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerModelSchema::OnDraw");
#endif

	m_model->m_entities->OnDraw(dc);
	m_model->m_relations->OnDraw(dc);

#if 0
	// add relations where both sides are in this model
	DBRelation	*rel = NULL;
	wxTreeItemId	child;
	long		cookie;
	wxShape		*shape;

	// draw also relations
	child = m_project->GetFirstChild(m_project->m_top_relations->GetTreeItemId(), cookie);
	while (child.IsOk()) {
		rel = (DBRelation *)(((DataDesignerItemData *)m_project->GetItemData(child))->GetObject());
		if (rel && rel->GetType() == DBRelationType) {
			if (m_model->m_entities->GetObjectByName(rel->m_parent) && m_model->m_entities->GetObjectByName(rel->m_child)) {
				if (rel->GetShape() == NULL)
					rel->CreateShape();
				
				shape = rel->GetShape();
				shape->Draw(dc);
			}
		} else {
			wxLogMessage("DataDesignerModelSchema::AddObjects - object is not a relation");
		}
		
		child = m_project->GetNextChild(m_project->m_top_relations->GetTreeItemId(), cookie);
	}
#endif
}


syntax highlighted by Code2HTML, v. 0.9.1