/////////////////////////////////////////////////////////////////////////////
// Name:        view.cc
// Purpose:     View classes
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: view.cc,v 1.2 2003/06/06 17:46:05 horakdan Exp $
// Copyright:   (c) Daniel Horak
// Licence:     GPL
/////////////////////////////////////////////////////////////////////////////

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

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#if !wxUSE_DOC_VIEW_ARCHITECTURE
#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
#endif

#include "datadesigner.h"
#include "project.h"
#include "doc.h"
#include "view.h"
#include "schema.h"
#include "servers/dbserver.h"

/*
 * DataDesignerSplitter
 */
BEGIN_EVENT_TABLE(DataDesignerSplitter, wxSplitterWindow)
END_EVENT_TABLE()

// Define a constructor for my canvas
DataDesignerSplitter::DataDesignerSplitter(wxView *v, wxMDIChildFrame *frame, const wxPoint& pos, const wxSize& size, long style):
	wxSplitterWindow(frame, -1, pos, size, style)
{
	m_view = v;
	
	m_project = new DataDesignerProject(this);
	m_panel = new wxPanel(this);
	
	SplitVertically(m_project, m_panel, 200);
	
	SetAutoLayout(TRUE);
}

// Define the repainting behaviour
void DataDesignerSplitter::OnDraw(wxDC& dc)
{
#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerSplitter::OnDraw");
#endif
	
	if (m_view)
		m_view->OnDraw(& dc);
}

/*
 * DataDesignerView
 */
IMPLEMENT_DYNAMIC_CLASS(DataDesignerView, wxView)

BEGIN_EVENT_TABLE(DataDesignerView, wxView)
    EVT_MENU(DataDesigner_Entity,	DataDesignerView::OnObject)
    EVT_MENU(DataDesigner_Domain,	DataDesignerView::OnObject)
    EVT_MENU(DataDesigner_View,		DataDesignerView::OnObject)
    EVT_MENU(DataDesigner_Procedure,	DataDesignerView::OnObject)
    EVT_MENU(DataDesigner_Relation,	DataDesignerView::OnObject)
    EVT_MENU(DataDesigner_Sequence,	DataDesignerView::OnObject)
    EVT_MENU(DataDesigner_ZoomIn,	DataDesignerView::OnZoom)
    EVT_MENU(DataDesigner_ZoomOut,	DataDesignerView::OnZoom)
    EVT_MENU(DataDesigner_Zoom100,	DataDesignerView::OnZoom)
    EVT_MENU(DataDesigner_ExportDDL,	DataDesignerView::OnExportDDL)
END_EVENT_TABLE()

DataDesignerView::DataDesignerView()
	: m_frame(NULL), m_splitter(NULL), m_zoom(100)
{
}

DataDesignerView::~DataDesignerView()
{
}

// What to do when a view is created. Creates actual
// windows for displaying the view.
bool DataDesignerView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
{
#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerView::OnCreate");
#endif

	m_frame = wxGetApp().CreateChildFrame(doc, this);
	m_frame->SetTitle(_T("DataDesignerView"));

	m_splitter = GetMainFrame()->CreateSplitter(this, m_frame);
#ifdef __X__
    // X seems to require a forced resize
    int x, y;
    m_frame->GetSize(&x, &y);
    m_frame->SetSize(-1, -1, x, y);
#endif
	m_frame->Show(TRUE);
	Activate(TRUE);

	return TRUE;
}

void DataDesignerView::OnDraw(wxDC *dc)
{
	DataDesignerSchema	*schema;
	
#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerView::OnDraw");
#endif

	if ((schema = m_splitter->GetProject()->GetSchema()) == NULL)
		return;
	
	schema->OnDraw(*dc);
}

void DataDesignerView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
{
	if (m_splitter)
		m_splitter->Refresh();
}

// Clean up windows used for displaying the view.
bool DataDesignerView::OnClose(bool deleteWindow)
{
	if (!GetDocument()->Close())
		return FALSE;

	wxString s(wxTheApp->GetAppName());
	if (m_frame)
		m_frame->SetTitle(s);

	SetFrame((wxFrame*)NULL);

	Activate(FALSE);

	if (deleteWindow)
	{
		delete m_frame;
		return TRUE;
	}
	return TRUE;
}

void DataDesignerView::OnObject(wxCommandEvent& event)
{
	DataDesignerContainer	*top = NULL;
	DataDesignerProject	*project;

#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerView::OnObject");
#endif
	
	project = m_splitter->GetProject();
	
	switch (event.GetId()) {
		case DataDesigner_Domain:
			top = project->m_top_domains;
			break;
		case DataDesigner_Entity:
			top = project->m_top_entities;
			break;
		case DataDesigner_View:
			top = project->m_top_views;
			break;
		case DataDesigner_Procedure:
			top = project->m_top_procedures;
			break;
		case DataDesigner_Sequence:
			top = project->m_top_sequences;
			break;
		case DataDesigner_Relation:
			top = project->m_top_relations;
			break;
		default:
			wxLogMessage("Unknow object type %d", event.GetId());
			break;
	}
	
	if (top)
		project->NewObject(top);
}

void DataDesignerView::OnZoom(wxCommandEvent& event)
{
	DataDesignerSchema	*schema;

#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerView::OnZoom");
#endif
	
	if ((schema = m_splitter->GetProject()->GetSchema()) == NULL)
	{
		wxLogMessage("we have no schema to zoom");
		return;
	}
	
	if (event.GetId() == DataDesigner_ZoomIn)
		m_zoom += 25;
	else if (event.GetId() == DataDesigner_ZoomOut)
		m_zoom -= 25;
	else if (event.GetId() == DataDesigner_Zoom100)
		m_zoom = 100;
	
#ifdef ENABLE_DEBUG
	wxLogMessage("m_zoom = %d", m_zoom);
#endif
	schema->SetScale((double)(m_zoom / 100.0));
	schema->Refresh();
}

void DataDesignerView::OnExportDDL(wxCommandEvent& event)
{
#ifdef ENABLE_DEBUG
	wxLogMessage("DataDesignerView::OnExportDDL");
#endif
	
	DataDesignerProject	*project = m_splitter->GetProject();
	
	project->GetServer()->ExportDDL(this);
}


syntax highlighted by Code2HTML, v. 0.9.1