
///////////////////////////////////////////////////////////
//                                                       //
//                         SAGA                          //
//                                                       //
//      System for Automated Geoscientific Analyses      //
//                                                       //
//                    User Interface                     //
//                                                       //
//                    Program: SAGA                      //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//                    SAGA_Frame.cpp                     //
//                                                       //
//          Copyright (C) 2005 by Olaf Conrad            //
//                                                       //
//-------------------------------------------------------//
//                                                       //
// This file is part of 'SAGA - System for Automated     //
// Geoscientific Analyses'. SAGA is free software; you   //
// can redistribute it and/or modify it under the terms  //
// of the GNU General Public License as published by the //
// Free Software Foundation; version 2 of the License.   //
//                                                       //
// SAGA is distributed in the hope that it will be       //
// useful, but WITHOUT ANY WARRANTY; without even the    //
// implied warranty of MERCHANTABILITY or FITNESS FOR A  //
// PARTICULAR PURPOSE. See the GNU General Public        //
// License for more details.                             //
//                                                       //
// You should have received a copy of the GNU General    //
// Public License along with this program; if not,       //
// write to the Free Software Foundation, Inc.,          //
// 59 Temple Place - Suite 330, Boston, MA 02111-1307,   //
// USA.                                                  //
//                                                       //
//-------------------------------------------------------//
//                                                       //
//    contact:    Olaf Conrad                            //
//                Institute of Geography                 //
//                University of Goettingen               //
//                Goldschmidtstr. 5                      //
//                37077 Goettingen                       //
//                Germany                                //
//                                                       //
//    e-mail:     oconrad@saga-gis.org                   //
//                                                       //
///////////////////////////////////////////////////////////

//---------------------------------------------------------


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#include <wx/wx.h>
#include <wx/frame.h>
#include <wx/statusbr.h>
#include <wx/icon.h>
#include <wx/gauge.h>
#include <wx/choicdlg.h>
#include <wx/toolbar.h>
#include <wx/aui/aui.h>

#include <saga_api/saga_api.h>

#include "callback.h"

#include "res_commands.h"
#include "res_controls.h"
#include "res_dialogs.h"
#include "res_images.h"

#include "helper.h"

#include "saga.h"
#include "saga_frame.h"
#include "saga_frame_droptarget.h"

#include "info.h"
#include "wksp.h"
#include "active.h"

#include "wksp_module_manager.h"
#include "wksp_module.h"
#include "wksp_module_menu.h"

#include "wksp_data_manager.h"
#include "wksp_data_menu_files.h"

#include "wksp_map_manager.h"

#include "view_map.h"
#include "view_map_3d.h"
#include "view_table.h"
#include "view_table_diagram.h"
#include "view_histogram.h"
#include "view_scatterplot.h"
#include "view_layout.h"

#include "dlg_about.h"

// stliersch begin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "PGSQL/pgdatabase.h"
#include "PGSQL/menu_db.h"
// stliersch end   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
class CSAGA_Frame_StatusBar : public wxStatusBar
{
public:
	CSAGA_Frame_StatusBar(wxWindow *parent, wxWindowID id, long style = wxST_SIZEGRIP, const wxString& name = "statusBar")
		: wxStatusBar(parent, id, style, name)
	{
		m_pProgressBar	= new wxGauge(this, ID_WND_PROGRESSBAR, 100);
	}

	void		On_Size		(wxSizeEvent     &event)
	{
		wxRect	r;

		if( m_pProgressBar && GetFieldRect(STATUSBAR_PROGRESS, r) )
		{
			m_pProgressBar->SetSize(r);
		}

		event.Skip();
	}

	wxGauge		*m_pProgressBar;

	DECLARE_EVENT_TABLE()
};

//---------------------------------------------------------
BEGIN_EVENT_TABLE(CSAGA_Frame_StatusBar, wxStatusBar)
	EVT_SIZE			(CSAGA_Frame_StatusBar::On_Size)
END_EVENT_TABLE()

//---------------------------------------------------------
wxStatusBar * CSAGA_Frame::OnCreateStatusBar(int number, long style, wxWindowID id, const wxString& name)
{
	CSAGA_Frame_StatusBar	*sb	= new CSAGA_Frame_StatusBar(this, id, style, name);

	sb->SetFieldsCount(number);

	return( sb );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
CSAGA_Frame	*g_pSAGA_Frame	= NULL;


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
BEGIN_EVENT_TABLE(CSAGA_Frame, wxMDIParentFrame)
	EVT_CLOSE			(CSAGA_Frame::On_Close)
	EVT_SIZE			(CSAGA_Frame::On_Size)

	EVT_MENU			(ID_CMD_FRAME_QUIT			, CSAGA_Frame::On_Quit)
	EVT_MENU			(ID_CMD_FRAME_HELP			, CSAGA_Frame::On_Help)
	EVT_MENU			(ID_CMD_FRAME_ABOUT			, CSAGA_Frame::On_About)

	EVT_MENU			(ID_CMD_FRAME_CASCADE		, CSAGA_Frame::On_Frame_Cascade)
	EVT_UPDATE_UI		(ID_CMD_FRAME_CASCADE		, CSAGA_Frame::On_Frame_Cascade_UI)
	EVT_MENU			(ID_CMD_FRAME_TILE_HORZ		, CSAGA_Frame::On_Frame_hTile)
	EVT_UPDATE_UI		(ID_CMD_FRAME_TILE_HORZ		, CSAGA_Frame::On_Frame_hTile_UI)
	EVT_MENU			(ID_CMD_FRAME_TILE_VERT		, CSAGA_Frame::On_Frame_vTile)
	EVT_UPDATE_UI		(ID_CMD_FRAME_TILE_VERT		, CSAGA_Frame::On_Frame_vTile_UI)
	EVT_MENU			(ID_CMD_FRAME_ARRANGEICONS	, CSAGA_Frame::On_Frame_ArrangeIcons)
	EVT_UPDATE_UI		(ID_CMD_FRAME_ARRANGEICONS	, CSAGA_Frame::On_Frame_ArrangeIcons_UI)
	EVT_MENU			(ID_CMD_FRAME_NEXT			, CSAGA_Frame::On_Frame_Next)
	EVT_UPDATE_UI		(ID_CMD_FRAME_NEXT			, CSAGA_Frame::On_Frame_Next_UI)
	EVT_MENU			(ID_CMD_FRAME_PREVIOUS		, CSAGA_Frame::On_Frame_Previous)
	EVT_UPDATE_UI		(ID_CMD_FRAME_PREVIOUS		, CSAGA_Frame::On_Frame_Previous_UI)
	EVT_MENU			(ID_CMD_FRAME_CLOSE			, CSAGA_Frame::On_Frame_Close)
	EVT_UPDATE_UI		(ID_CMD_FRAME_CLOSE			, CSAGA_Frame::On_Frame_Close_UI)
	EVT_MENU			(ID_CMD_FRAME_CLOSE_ALL		, CSAGA_Frame::On_Frame_Close_All)
	EVT_UPDATE_UI		(ID_CMD_FRAME_CLOSE_ALL		, CSAGA_Frame::On_Frame_Close_All_UI)

	EVT_MENU			(ID_CMD_FRAME_WKSP_SHOW		, CSAGA_Frame::On_WKSP_Show)
	EVT_UPDATE_UI		(ID_CMD_FRAME_WKSP_SHOW		, CSAGA_Frame::On_WKSP_Show_UI)
	EVT_MENU			(ID_CMD_FRAME_ACTIVE_SHOW	, CSAGA_Frame::On_Active_Show)
	EVT_UPDATE_UI		(ID_CMD_FRAME_ACTIVE_SHOW	, CSAGA_Frame::On_Active_Show_UI)
	EVT_MENU			(ID_CMD_FRAME_INFO_SHOW		, CSAGA_Frame::On_INFO_Show)
	EVT_UPDATE_UI		(ID_CMD_FRAME_INFO_SHOW		, CSAGA_Frame::On_INFO_Show_UI)

	EVT_MENU_RANGE		(ID_CMD_WKSP_FIRST			, ID_CMD_WKSP_LAST		, CSAGA_Frame::On_Command_Workspace)
	EVT_UPDATE_UI_RANGE	(ID_CMD_WKSP_FIRST			, ID_CMD_WKSP_LAST		, CSAGA_Frame::On_Command_Workspace_UI)
	EVT_MENU_RANGE		(ID_CMD_MODULE_FIRST		, ID_CMD_MODULE_LAST	, CSAGA_Frame::On_Command_Module)
	EVT_UPDATE_UI_RANGE	(ID_CMD_MODULE_FIRST		, ID_CMD_MODULE_LAST	, CSAGA_Frame::On_Command_Module_UI)

	EVT_MENU_RANGE		(ID_CMD_CHILD_FIRST			, ID_CMD_CHILD_LAST		, CSAGA_Frame::On_Command_Child)
	EVT_UPDATE_UI_RANGE	(ID_CMD_MAP_FIRST			, ID_CMD_MAP_LAST		, CSAGA_Frame::On_Command_Child_UI)
	EVT_UPDATE_UI_RANGE	(ID_CMD_HISTOGRAM_FIRST		, ID_CMD_HISTOGRAM_LAST	, CSAGA_Frame::On_Command_Child_UI)

// stliersch begin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Event handler routing for Menu 'Database'
	EVT_MENU			(ID_CMD_MENU_DB_CONN			, CSAGA_Frame::On_ConnectDB)
	EVT_MENU			(ID_CMD_MENU_DB_DECONN			, CSAGA_Frame::On_DeConnectDB)
	EVT_MENU			(ID_CMD_MENU_DB_IMPORT_TABLE	, CSAGA_Frame::On_DB_ImportTable)
	EVT_MENU			(ID_CMD_MENU_DB_EXPORT_TABLE	, CSAGA_Frame::On_DB_ExportTable)
	EVT_MENU			(ID_CMD_MENU_DB_CREATE_TABLE	, CSAGA_Frame::On_DB_CreateTable)
	EVT_MENU			(ID_CMD_MENU_DB_DELETE_TABLE	, CSAGA_Frame::On_DB_DeleteTable)
	EVT_MENU			(ID_CMD_MENU_DB_MODIFY_TABLE	, CSAGA_Frame::On_DB_ModifyTable)
	EVT_MENU			(ID_CMD_MENU_DB_IMPORTSIMPLEFEATURE, CSAGA_Frame::On_DB_ImportSimpleFeature)
	EVT_MENU			(ID_CMD_MENU_DB_IMPORTASSHAPE	, CSAGA_Frame::On_DB_ImportAsShape)
	EVT_MENU			(ID_CMD_MENU_DB_EXPORTASSHAPE	, CSAGA_Frame::On_DB_ExportAsShape)
	EVT_MENU			(ID_CMD_MENU_DB_IMPORTASGML		, CSAGA_Frame::On_DB_ImportAsGML)
	EVT_MENU			(ID_CMD_MENU_DB_EXPORTASGML		, CSAGA_Frame::On_DB_ExportAsGML)
	EVT_MENU			(ID_CMD_MENU_DB_EXPORTASSIMPLEFEATURE	, CSAGA_Frame::On_DB_ExportAsSimpleFeature)
// stliersch end   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
END_EVENT_TABLE()


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
CSAGA_Frame::CSAGA_Frame(void)
	: wxMDIParentFrame(NULL, ID_WND_MAIN, LNG("SAGA"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE|wxHSCROLL|wxVSCROLL|wxFRAME_NO_WINDOW_MENU)
{
#ifdef _DEBUG
	SetTitle("SAGA [Debug]");
#endif

	//-----------------------------------------------------
	g_pSAGA_Frame		= this;

	m_pINFO				= NULL;
	m_pActive			= NULL;
	m_pWKSP				= NULL;

// stliersch begin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	pDB_Menu			= NULL;
// stliersch end   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	SG_Set_UI_Callback	(Get_Callback());

	SetIcon				(IMG_Get_Icon(ID_IMG_SAGA_ICON_32));

	SetDropTarget		(new CSAGA_Frame_DropTarget);

	//-----------------------------------------------------
	int		STATUSBAR_Sizes[STATUSBAR_COUNT]	= {	-1, -1, 90, 90, 90, -1	};

	CreateStatusBar		(STATUSBAR_COUNT);
	SetStatusWidths		(STATUSBAR_COUNT, STATUSBAR_Sizes);
	SetStatusBarPane	(STATUSBAR_DEFAULT);
	StatusBar_Set_Text	(LNG("[VAL] ready"));

	m_pProgressBar		= ((CSAGA_Frame_StatusBar *)GetStatusBar())->m_pProgressBar;

	//-----------------------------------------------------
	m_pLayout			= new wxFrameManager(this);

	m_pLayout->GetArtProvider()->SetColor	(wxAUI_ART_ACTIVE_CAPTION_COLOUR,
		wxSystemSettings::GetColour(wxSYS_COLOUR_ACTIVECAPTION)
	);

	m_pLayout->GetArtProvider()->SetColor	(wxAUI_ART_INACTIVE_CAPTION_COLOUR,
		wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE)
	);

	m_pLayout->GetArtProvider()->SetMetric	(wxAUI_ART_GRADIENT_TYPE	, wxAUI_GRADIENT_NONE);
	m_pLayout->GetArtProvider()->SetMetric	(wxAUI_ART_CAPTION_SIZE		, 14);

	m_pLayout->SetFlags(m_pLayout->GetFlags() ^ wxAUI_MGR_TRANSPARENT_DRAG);
//	m_pLayout->SetFlags(m_pLayout->GetFlags() ^ wxAUI_MGR_ALLOW_ACTIVE_PANE);

	//-----------------------------------------------------
	_Bar_Add(m_pINFO   = new CINFO  (this), 0);
	_Bar_Add(m_pActive = new CACTIVE(this), 1);
	_Bar_Add(m_pWKSP   = new CWKSP  (this), 2);

	m_pINFO		->Add_Pages();
	m_pActive	->Add_Pages();
	m_pWKSP		->Add_Pages();

	//-----------------------------------------------------
	SetMenuBar(MB_Create(NULL));

	//-----------------------------------------------------
	m_pTB_Main			= 						  _Create_ToolBar();
	m_pTB_Map			= CVIEW_Map				::_Create_ToolBar();
	m_pTB_Map_3D		= CVIEW_Map_3D			::_Create_ToolBar();
	m_pTB_Layout		= CVIEW_Layout			::_Create_ToolBar();
	m_pTB_Table			= CVIEW_Table			::_Create_ToolBar();
	m_pTB_Diagram		= CVIEW_Table_Diagram	::_Create_ToolBar();
	m_pTB_Histogram		= CVIEW_Histogram		::_Create_ToolBar();
	m_pTB_ScatterPlot	= CVIEW_ScatterPlot		::_Create_ToolBar();

	//-----------------------------------------------------
	m_pLayout->GetPane(GetClientWindow()).Show().Center();

	wxString	s;

	if( CONFIG_Read("/FL", "MANAGER", s) )
	{
		m_pLayout->LoadPerspective(s);
	}

	_Bar_Show(m_pTB_Main, true);

	m_pLayout->Update();

	Show(true);

	//-----------------------------------------------------
	int		x, y, dx, dy;
	long	l;

	x	= CONFIG_Read("/FL", "X" , l) ? l : -1;
	y	= CONFIG_Read("/FL", "Y" , l) ? l : -1;
	dx	= CONFIG_Read("/FL", "DX", l) ? l : 800;
	dy	= CONFIG_Read("/FL", "DY", l) ? l : 600;

	SetSize(x, y, dx, dy);

	if( !(CONFIG_Read("/FL", "STATE", l) && l == 0) )
	{
		Maximize();
	}

	Update();

	//-----------------------------------------------------
	if( g_pData->Initialise() )
	{
		Refresh(false);
	}
}

//---------------------------------------------------------
CSAGA_Frame::~CSAGA_Frame(void)
{
	//-----------------------------------------------------
	if( IsIconized() )
	{
		Iconize(false);
	}

	if( IsMaximized() )
	{
		CONFIG_Write("/FL", "STATE" , (long)1);
	}
	else
	{
		CONFIG_Write("/FL", "STATE", (long)0);
		CONFIG_Write("/FL", "X"    , (long)GetPosition().x);
		CONFIG_Write("/FL", "Y"    , (long)GetPosition().y);
		CONFIG_Write("/FL", "DX"   , (long)GetSize().x);
		CONFIG_Write("/FL", "DY"   , (long)GetSize().y);
	}

	//-----------------------------------------------------
	CONFIG_Write("/FL", "MANAGER", m_pLayout->SavePerspective().c_str());

	m_pLayout->UnInit();

	delete(m_pLayout);

	//-----------------------------------------------------
	SG_Set_UI_Callback(NULL);

	g_pSAGA_Frame	= NULL;
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSAGA_Frame::On_Close(wxCloseEvent &event)
{
	if( event.CanVeto() )
	{
		if( !g_pModule && DLG_Message_Confirm(ID_DLG_CLOSE) && g_pData->Close(true) )
		{
			g_pModules->Finalise();

			Destroy();
		}
		else
		{
			if( g_pModule )
			{
				DLG_Message_Show(LNG("Please stop module execution before exiting SAGA."), LNG("Exit SAGA"));
			}

			event.Veto();
		}
	}
	else
	{
		g_pModules->Finalise();

		g_pData->Close(true);

		event.Skip();
	}
}

//---------------------------------------------------------
void CSAGA_Frame::On_Size(wxSizeEvent &event)
{
	event.Skip();
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSAGA_Frame::On_Quit(wxCommandEvent &WXUNUSED(event))
{
	Close(false);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Help(wxCommandEvent &WXUNUSED(event))
{
	DLG_Message_Show(
		LNG("Currently no help files exist for SAGA.\nIt depends on your support, if you will find any help at this place in future!\nThanks a lot..."),
		LNG("SAGA Help")
	);
}

//---------------------------------------------------------
void CSAGA_Frame::On_About(wxCommandEvent &WXUNUSED(event))
{
	CDLG_About	dlg;

	dlg.ShowModal();
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_Cascade(wxCommandEvent &WXUNUSED(event))
{
	Cascade();
}

void CSAGA_Frame::On_Frame_Cascade_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_hTile(wxCommandEvent &WXUNUSED(event))
{
	Tile(wxHORIZONTAL);
}

void CSAGA_Frame::On_Frame_hTile_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_vTile(wxCommandEvent &WXUNUSED(event))
{
	Tile(wxVERTICAL);
}

void CSAGA_Frame::On_Frame_vTile_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_ArrangeIcons(wxCommandEvent &WXUNUSED(event))
{
	ArrangeIcons();
}

void CSAGA_Frame::On_Frame_ArrangeIcons_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_Next(wxCommandEvent &WXUNUSED(event))
{
	ActivateNext();
}

void CSAGA_Frame::On_Frame_Next_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_Previous(wxCommandEvent &WXUNUSED(event))
{
	ActivatePrevious();
}

void CSAGA_Frame::On_Frame_Previous_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_Close(wxCommandEvent &WXUNUSED(event))
{
	if( GetActiveChild() != NULL )
	{
		GetActiveChild()->Close();
	}
}

void CSAGA_Frame::On_Frame_Close_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Frame_Close_All(wxCommandEvent &WXUNUSED(event))
{
	while( GetActiveChild() != NULL )
	{
		delete(GetActiveChild());
	}
}

void CSAGA_Frame::On_Frame_Close_All_UI(wxUpdateUIEvent &event)
{
	event.Enable(GetActiveChild() != NULL);
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSAGA_Frame::On_WKSP_Show(wxCommandEvent &WXUNUSED(event))
{
	_Bar_Toggle(m_pWKSP);
}

void CSAGA_Frame::On_WKSP_Show_UI(wxUpdateUIEvent &event)
{
	event.Check(m_pWKSP->IsShown());
}

//---------------------------------------------------------
void CSAGA_Frame::On_Active_Show(wxCommandEvent &WXUNUSED(event))
{
	_Bar_Toggle(m_pActive);
}

void CSAGA_Frame::On_Active_Show_UI(wxUpdateUIEvent &event)
{
	event.Check(m_pActive->IsShown());
}

//---------------------------------------------------------
void CSAGA_Frame::On_INFO_Show(wxCommandEvent &WXUNUSED(event))
{
	_Bar_Toggle(m_pINFO);
}

void CSAGA_Frame::On_INFO_Show_UI(wxUpdateUIEvent &event)
{
	event.Check(m_pINFO->IsShown());
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSAGA_Frame::On_Command_Workspace(wxCommandEvent &event)
{
	m_pWKSP->On_Command(event);
}

void CSAGA_Frame::On_Command_Workspace_UI(wxUpdateUIEvent &event)
{
	m_pWKSP->On_Command_UI(event);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Command_Module(wxCommandEvent &event)
{
	m_pWKSP->On_Command_Module(event);
}

void CSAGA_Frame::On_Command_Module_UI(wxUpdateUIEvent &event)
{
	m_pWKSP->On_Command_UI_Module(event);
}

//---------------------------------------------------------
void CSAGA_Frame::On_Command_Child(wxCommandEvent &event)
{
	wxMDIChildFrame	*pChild;

	if( (pChild = GetActiveChild()) != NULL )
	{
		pChild->AddPendingEvent(event);
	}
}

void CSAGA_Frame::On_Command_Child_UI(wxUpdateUIEvent &event)
{
	CVIEW_Base	*pChild;

	if( (pChild = wxDynamicCast(GetActiveChild(), CVIEW_Base)) != NULL )
	{
		pChild->On_Command_UI(event);
	}
}

// stliersch begin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Event handler functions menu 'Database'

void CSAGA_Frame::On_ConnectDB(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_ConnectDB();
}

void CSAGA_Frame::On_DeConnectDB(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DeConnectDB();
}

void CSAGA_Frame::On_DB_ImportTable(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ImportTable();
}

void CSAGA_Frame::On_DB_ExportTable(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ExportTable();
}

void CSAGA_Frame::On_DB_CreateTable(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_CreateTable();
}

void CSAGA_Frame::On_DB_DeleteTable(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_DeleteTable();
}

void CSAGA_Frame::On_DB_ModifyTable(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ModifyTable();
}

void CSAGA_Frame::On_DB_ImportSimpleFeature(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ImportSimpleFeature();
}

void CSAGA_Frame::On_DB_ImportAsShape(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ImportAsShape();
}

void CSAGA_Frame::On_DB_ExportAsShape(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ExportAsShape();
}

void CSAGA_Frame::On_DB_ImportAsGML(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ImportAsGML();
}

void CSAGA_Frame::On_DB_ExportAsGML(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ExportAsGML();
}

void CSAGA_Frame::On_DB_ExportAsSimpleFeature(wxCommandEvent &WXUNUSED(event))
{
	pDB_Menu->On_DB_ExportAsSimpleFeature();
}
// stliersch end   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
bool CSAGA_Frame::Process_Get_Okay(bool bBlink)
{
	static bool	s_bBlink	= false;

	if( bBlink )
	{
		ProgressBar_Set_Position((s_bBlink = !s_bBlink) == true ? 100 : 0);
	}

	return( g_pSAGA->Process_Get_Okay() );
}

//---------------------------------------------------------
bool CSAGA_Frame::Process_Set_Okay(bool bOkay)
{
	StatusBar_Set_Text(LNG("[VAL] ready"));

	ProgressBar_Set_Position(0);

	g_pSAGA->Process_Set_Okay(bOkay);

	return( bOkay );
}

//---------------------------------------------------------
bool CSAGA_Frame::ProgressBar_Set_Position(int Position)
{
	if( Position < 0 )
	{
		Position	= 0;
	}
	else if( Position > 100 )
	{
		Position	= 100;
	}

	m_pProgressBar->SetValue(Position);

	return( Process_Get_Okay(false) );
}

//---------------------------------------------------------
bool CSAGA_Frame::ProgressBar_Set_Position(double Position, double Range)
{
	return( ProgressBar_Set_Position(Range != 0.0 ? (int)(100.0 * Position / Range) : 0) );
}

//---------------------------------------------------------
void CSAGA_Frame::StatusBar_Set_Text(const char *Text, int iPane)
{
	if( iPane < 0 || iPane >= STATUSBAR_PROGRESS )
	{
		iPane	= STATUSBAR_DEFAULT;
	}

	SetStatusText(Text, iPane);
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSAGA_Frame::On_Child_Activates(CVIEW_Base *pChild, bool bActivates)
{
	switch( pChild->Get_View_ID() )
	{
	case ID_VIEW_TABLE:
		_Bar_Show(m_pTB_Table		, bActivates);
		break;

	case ID_VIEW_TABLE_DIAGRAM:
		_Bar_Show(m_pTB_Diagram		, bActivates);
		break;

	case ID_VIEW_MAP:
		_Bar_Show(m_pTB_Map			, bActivates);
		break;

	case ID_VIEW_MAP_3D:
		_Bar_Show(m_pTB_Map_3D		, bActivates);
		break;

	case ID_VIEW_HISTOGRAM:
		_Bar_Show(m_pTB_Histogram	, bActivates);
		break;

	case ID_VIEW_SCATTERPLOT:
		_Bar_Show(m_pTB_ScatterPlot	, bActivates);
		break;

	case ID_VIEW_LAYOUT:
		_Bar_Show(m_pTB_Layout		, bActivates);
		break;
	}
}


///////////////////////////////////////////////////////////
//														 //
//						MenuBar							 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
wxMenuBar * CSAGA_Frame::MB_Create(CVIEW_Base *pChild)
{
	wxMenu		*pMenu;
	wxMenuBar	*pMenuBar	= new wxMenuBar;

	//-----------------------------------------------------
	// 1. File...

	pMenu		= new wxMenu;

	g_pData->Get_FileMenus()->Add(pMenu);

	pMenuBar->Append(pMenu, LNG("[MNU] File"));


	//-----------------------------------------------------
	// 2. Modules...

	pMenu		= new wxMenu;

	g_pModules->Get_Modules_Menu()->Add(pMenu);

	pMenuBar->Append(pMenu, LNG("[MNU] Modules"));


	//-----------------------------------------------------
	// 3. Window...

	pMenu		= new wxMenu;

	CMD_Menu_Add_Item(pMenu,  true, ID_CMD_FRAME_WKSP_SHOW);
	CMD_Menu_Add_Item(pMenu,  true, ID_CMD_FRAME_ACTIVE_SHOW);
	CMD_Menu_Add_Item(pMenu,  true, ID_CMD_FRAME_INFO_SHOW);

#ifdef __WXMSW__
	pMenu->AppendSeparator();
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_CASCADE);
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_TILE_HORZ);
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_TILE_VERT);
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_ARRANGEICONS);
#endif	// #ifdef __WXMSW__

	pMenu->AppendSeparator();
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_NEXT);
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_PREVIOUS);
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_CLOSE);
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_CLOSE_ALL);

	pMenuBar->Append(pMenu, LNG("[MNU] Window"));

	//-----------------------------------------------------

// stliersch begin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// 4. Database

	pMenu		= new wxMenu;
	pDB_Menu	= new Cmenu_db(pMenu);
	pMenuBar->Append(pMenu, wxT("Database"));
// stliersch end   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	//-----------------------------------------------------
	// 5. Help...

	pMenu		= new wxMenu;

	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_HELP);
	CMD_Menu_Add_Item(pMenu, false, ID_CMD_FRAME_ABOUT);

	pMenuBar->Append(pMenu, LNG("[MNU] ?"));


	//-----------------------------------------------------
	return( pMenuBar );
}

//---------------------------------------------------------
void CSAGA_Frame::MB_Remove(wxMenu *pMenu_File, wxMenu *pMenu_Modules)
{
	g_pData		->Get_FileMenus()		->Del(pMenu_File);
	g_pModules	->Get_Modules_Menu()	->Del(pMenu_Modules);
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
void CSAGA_Frame::_Bar_Add(wxWindow *pWindow, int Position)
{
	m_pLayout->AddPane(pWindow, wxPaneInfo()
		.Name		(pWindow->GetName())
		.Caption	(pWindow->GetName())
		.Layer		(0)
		.Row		(0)
		.Position	(0)
		.MinSize	(100, 100)
		.BestSize	(200, 100)
	);

	switch( Position )
	{
	default:
	case 0:	m_pLayout->GetPane(pWindow).Bottom().FloatingSize(400, 200);	break;
	case 1:	m_pLayout->GetPane(pWindow).Right ().FloatingSize(200, 400);	break;
	case 2:	m_pLayout->GetPane(pWindow).Left  ().FloatingSize(200, 400);	break;
	case 3:	m_pLayout->GetPane(pWindow).Top   ().FloatingSize(400, 200);	break;
	}
}

//---------------------------------------------------------
void CSAGA_Frame::_Bar_Toggle(wxWindow *pWindow)
{
	if( pWindow->IsShown() )
	{
		m_pLayout->GetPane(pWindow).Hide();

		if( m_pLayout->GetPane(pWindow).IsToolbar() )
			pWindow->Hide();
	}
	else
	{
		if( m_pLayout->GetPane(pWindow).IsToolbar() )
			pWindow->Show();

		m_pLayout->GetPane(pWindow).Show();
	}

	m_pLayout->Update();
}

//---------------------------------------------------------
void CSAGA_Frame::_Bar_Show(wxWindow *pWindow, bool bShow)
{
	if( pWindow && pWindow->IsShown() != bShow )
	{
		_Bar_Toggle(pWindow);
	}
}


///////////////////////////////////////////////////////////
//														 //
//						ToolBar							 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
#define TOOLBAR_SIZE_IMG		16

//---------------------------------------------------------
wxToolBarBase * CSAGA_Frame::TB_Create(int ID)
{
	wxToolBar	*pToolBar	= new wxToolBar(this, ID, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxTB_FLAT|wxTB_NODIVIDER);

	pToolBar->SetToolBitmapSize(wxSize(TOOLBAR_SIZE_IMG, TOOLBAR_SIZE_IMG));

	return( pToolBar );
}

//---------------------------------------------------------
void CSAGA_Frame::TB_Add(wxToolBarBase *pToolBar, const char *Name)
{
	pToolBar->Realize();
	pToolBar->Hide();

	m_pLayout->AddPane(pToolBar, wxPaneInfo()
	//	.Name			(pToolBar->GetName())
		.Name			(Name)
		.Caption		(Name)
		.ToolbarPane	()
		.Top			()
		.LeftDockable	(false)
		.RightDockable	(false)
		.Hide			()
	);
}

//---------------------------------------------------------
void CSAGA_Frame::TB_Add_Item(wxToolBarBase *pToolBar, bool bCheck, int Cmd_ID)
{
	if( bCheck )
		((wxToolBar *)pToolBar)->AddTool(Cmd_ID, CMD_Get_Name(Cmd_ID), IMG_Get_Bitmap(CMD_Get_ImageID(Cmd_ID), TOOLBAR_SIZE_IMG), CMD_Get_Help(Cmd_ID), wxITEM_CHECK);
	else
		((wxToolBar *)pToolBar)->AddTool(Cmd_ID, CMD_Get_Name(Cmd_ID), IMG_Get_Bitmap(CMD_Get_ImageID(Cmd_ID), TOOLBAR_SIZE_IMG), CMD_Get_Help(Cmd_ID));
}

//---------------------------------------------------------
void CSAGA_Frame::TB_Add_Separator(wxToolBarBase *pToolBar)
{
	((wxToolBar *)pToolBar)->AddSeparator();
}

//---------------------------------------------------------
wxToolBarBase * CSAGA_Frame::_Create_ToolBar(void)
{
	wxToolBarBase	*pToolBar	= TB_Create(ID_TB_MAIN);

	CMD_ToolBar_Add_Item(pToolBar, false, ID_CMD_WKSP_OPEN);
	CMD_ToolBar_Add_Separator(pToolBar);
	CMD_ToolBar_Add_Item(pToolBar, true , ID_CMD_FRAME_WKSP_SHOW);
	CMD_ToolBar_Add_Item(pToolBar, true , ID_CMD_FRAME_ACTIVE_SHOW);
	CMD_ToolBar_Add_Item(pToolBar, true , ID_CMD_FRAME_INFO_SHOW);
	CMD_ToolBar_Add_Separator(pToolBar);
	CMD_ToolBar_Add_Item(pToolBar, false, ID_CMD_FRAME_HELP);

	TB_Add(pToolBar, LNG("[CAP] Standard"));

	return( pToolBar );
}


///////////////////////////////////////////////////////////
//														 //
//														 //
//														 //
///////////////////////////////////////////////////////////

//---------------------------------------------------------
