/////////////////////////////////////////////////////////////////////////////
// Name:        datadesigner.cc
// Purpose:     Main Program for DataDesigner
// Author:      Daniel Horak
// Modified by:
// RCS-ID:      $Id: datadesigner.cc,v 1.3 2003/12/21 22:04:33 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

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

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

#include <wx/toolbar.h>
#include <wx/splitter.h>
#include <wx/treectrl.h>
#include <wx/ogl/ogl.h>
#include "config.h"
#include "datadesigner.h"
#include "project.h"
#include "doc.h"
#include "view.h"
#include "objects/dbobject.h"
#include "servers/dbserver.h"

#ifdef __WXMSW__
#define USE_XPM_BITMAPS 0
#else
#define USE_XPM_BITMAPS 1
#endif

#if USE_XPM_BITMAPS
#include "bitmaps/new.xpm"
#include "bitmaps/open.xpm"
#include "bitmaps/save.xpm"
#include "bitmaps/print.xpm"
#include "bitmaps/help.xpm"
#endif // USE_XPM_BITMAPS

DataDesignerFrame	*frame	 = (DataDesignerFrame *) NULL;

IMPLEMENT_APP(DataDesignerApp)

DataDesignerApp::DataDesignerApp()
{
	m_docmanager = (wxDocManager *) NULL;
}

// `Main program' equivalent: the program execution "starts" here
bool DataDesignerApp::OnInit()
{
	m_docmanager = new wxDocManager;

	(void) new wxDocTemplate((wxDocManager *) m_docmanager, _T("DataDesigner Project"), _T("*.ddp"), _T(""), _T("ddp"),
		_T("DataDesigner Project"), _T("DataDesigner View"), CLASSINFO(DataDesignerDocument), CLASSINFO(DataDesignerView));
	     
#ifdef ENABLE_NLS
	// enable locale
	wxLocale *locale = new wxLocale(wxLANGUAGE_DEFAULT);
	locale->AddCatalog(PACKAGE);
#endif

	wxOGLInitialize();

	// create main application frame
	frame = new DataDesignerFrame(m_docmanager, (wxFrame *)NULL, "Data Designer", wxDefaultPosition,
		wxSize(600, 400), wxDEFAULT_FRAME_STYLE);

	wxMenu *menuFile = new wxMenu;
	menuFile->Append(wxID_NEW, _("&New project\tCtrl-N"), _("New project"));
	menuFile->Append(wxID_OPEN, _("&Open project...\tCtrl-O"), _("Open project"));
	menuFile->AppendSeparator();
	menuFile->Append(wxID_EXIT, _("&Quit\tCtrl-Q"), _("Quit this program"));
	
	m_docmanager->FileHistoryUseMenu(menuFile);

	wxMenu *menuHelp = new wxMenu;
	menuHelp->Append(DataDesigner_About, _("&About...\tCtrl-A"), _("Show about dialog"));

	wxMenuBar *menuBar = new wxMenuBar();

	menuBar->Append(menuFile, _("&File"));
	menuBar->Append(menuHelp, _("&Help"));

	frame->SetMenuBar(menuBar);

	frame->CenterOnScreen();
	frame->Show(TRUE);
	
	SetTopWindow(frame);

#ifdef ENABLE_DEBUG
	// setup logging
        wxString logpath = wxGetHomeDir() + wxFILE_SEP_PATH + "datadesigner.log";
        FILE *fp = fopen(logpath.c_str(), "a");
        if (fp == NULL) {
		return FALSE;
        }

        wxLog *logger = new wxLogStderr(fp);
        wxLog::SetActiveTarget(logger);
	wxLog::SetActiveTarget(new wxLogWindow(frame, "log"));
#else
        wxLog *logger = new wxLogStderr();
        wxLog::SetActiveTarget(logger);
#endif
	wxLog::SetTimestamp("[%y/%m/%d %H:%M:%S] ");
#ifdef ENABLE_DEBUG
	wxLogMessage("startup");
#endif
	
	InitServers();

	return TRUE;
}

int DataDesignerApp::OnExit()
{
	delete m_docmanager;
	DestroyServers();
	wxOGLCleanUp();
#ifdef ENABLE_DEBUG
	wxLogMessage("finish");
#endif
	return 0;
}

wxMDIChildFrame *DataDesignerApp::CreateChildFrame(wxDocument *doc, wxView *view)
{
	wxDocMDIChildFrame	*subframe = new wxDocMDIChildFrame(doc, view, GetMainFrame(), -1,
		_T("Child Frame"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE);
		
	wxMenu *menuFile = new wxMenu;
	menuFile->Append(wxID_NEW, _("&New project\tCtrl-N"), _("New project"));
	menuFile->Append(wxID_OPEN, _("&Open project...\tCtrl-O"), _("Open project"));
	menuFile->Append(wxID_CLOSE, _("&Close\tCtrl-C"), _("Close project"));
	menuFile->Append(wxID_SAVE, _("&Save...\tCtrl-S"), _("Save project"));
	menuFile->Append(wxID_SAVEAS, _("Save &As..."), _("Save project as"));
	menuFile->AppendSeparator();
	menuFile->Append(DataDesigner_ExportDDL, _("&Export DDL..."), _("Export DDL commands for project"));
	menuFile->AppendSeparator();
	menuFile->Append(wxID_PRINT, _("&Print...\tCtrl-P"), _("Print schema"));
	menuFile->Append(wxID_PRINT_SETUP, _("Print setup..."), _("Print setup"));
	menuFile->Append(wxID_PREVIEW, _("Preview..."), _("Preview"));
	menuFile->AppendSeparator();
	menuFile->Append(wxID_EXIT, _("&Quit\tCtrl-Q"), _("Quit this program"));
	
	wxMenu *menuEdit = new wxMenu;
	menuEdit->Append(wxID_UNDO, _("&Undo"), _("Undo last step"));
	menuEdit->Append(wxID_REDO, _("&Redo"), _("Redo last step"));
	doc->GetCommandProcessor()->SetEditMenu(menuEdit);

	wxMenu *menuObject = new wxMenu;
	menuObject->Append(DataDesigner_Entity, _("&Entity..."), _("Entity"));
	menuObject->Append(DataDesigner_Domain, _("&Domain..."), _("Domain"));
	menuObject->Append(DataDesigner_View, _("&View..."), _("View"));
	menuObject->Append(DataDesigner_Procedure, _("&Procedure..."), _("Procedure"));
	menuObject->Append(DataDesigner_Relation, _("&Relation..."), _("Relation"));
	menuObject->Append(DataDesigner_Sequence, _("&Sequence..."), _("Sequence"));

	wxMenu *menuView = new wxMenu;
	menuView->Append(DataDesigner_ZoomIn, _("Zoom in"), _("Zoom in"));
	menuView->Append(DataDesigner_ZoomOut, _("Zoom out"), _("Zoom out"));
	menuView->Append(DataDesigner_Zoom100, _("Original"), _("Original size"));

	wxMenu *menuHelp = new wxMenu;
	menuHelp->Append(DataDesigner_About, _("&About...\tCtrl-A"), _("Show about dialog"));

	wxMenuBar *menuBar = new wxMenuBar();

	menuBar->Append(menuFile, _("&File"));
	menuBar->Append(menuEdit, _("&Edit"));
	menuBar->Append(menuObject, _("&Objects"));
	menuBar->Append(menuView, _("&View"));
	menuBar->Append(menuHelp, _("&Help"));

	subframe->SetMenuBar(menuBar);
	
	return subframe;
}


// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

IMPLEMENT_CLASS(DataDesignerFrame, wxDocMDIParentFrame)
BEGIN_EVENT_TABLE(DataDesignerFrame, wxDocMDIParentFrame)
	EVT_MENU(DataDesigner_About, 		DataDesignerFrame::OnAbout)
END_EVENT_TABLE()

// frame constructor
DataDesignerFrame::DataDesignerFrame(wxDocManager *mgr, wxFrame *frame, const wxString& title,
	const wxPoint& pos, const wxSize& size, long type)
       : wxDocMDIParentFrame(mgr, frame, -1, title, pos, size, type, _T("DataDesignerFrame"))
{
}

// event handlers

void DataDesignerFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
	wxString msg;
	msg.Printf( _T("DataDesigner " VERSION
			"\n\n(c) 2002-2003 Daniel Horak")
	);

	wxMessageBox(msg, _("About..."), wxOK | wxICON_INFORMATION, this);
}

DataDesignerSplitter *DataDesignerFrame::CreateSplitter(wxView *view, wxMDIChildFrame *parent)
{
	int	width, height;
	
	parent->GetClientSize(&width, &height);
	
	DataDesignerSplitter	*splitter = new DataDesignerSplitter(view, parent, wxDefaultPosition, wxSize(width, height), 0);
	
	return splitter;
}

DataDesignerFrame *GetMainFrame()
{
	return frame;
}


syntax highlighted by Code2HTML, v. 0.9.1