2010-03-13 11:39:22 +01:00
# include "main.h"
# include <stdexcept>
# include <wx/wx.h>
# include <wx/sizer.h>
# include <wx/image.h>
# include <wx/bitmap.h>
# include <wx/icon.h>
2011-05-04 01:34:25 +02:00
# include "platform_common.h"
# include "config.h"
# include "game_constants.h"
# include "util.h"
# include <wx/stdpaths.h>
2011-05-23 21:23:00 +02:00
# include <platform_util.h>
2010-03-13 11:39:22 +01:00
using namespace std ;
2011-05-04 01:34:25 +02:00
using namespace Shared : : PlatformCommon ;
using namespace Glest : : Game ;
using namespace Shared : : Util ;
namespace Glest { namespace Game {
string getGameReadWritePath ( string lookupKey ) {
string path = " " ;
if ( path = = " " & & getenv ( " GLESTHOME " ) ! = NULL ) {
path = getenv ( " GLESTHOME " ) ;
if ( path ! = " " & & EndsWith ( path , " / " ) = = false & & EndsWith ( path , " \\ " ) = = false ) {
path + = " / " ;
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for read/write files [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
}
return path ;
}
} }
2010-03-13 11:39:22 +01:00
2011-05-04 01:34:25 +02:00
namespace Configurator {
2010-03-13 11:39:22 +01:00
// ===============================================
// class MainWindow
// ===============================================
const int MainWindow : : margin = 10 ;
const int MainWindow : : panelMargin = 20 ;
const int MainWindow : : gridMarginHorizontal = 30 ;
2010-07-18 07:22:32 +02:00
const string MainWindow : : versionString = " v1.3.5 " ;
2010-03-13 11:39:22 +01:00
const string MainWindow : : winHeader = " Mega-Glest config " + versionString + " - Built: " + __DATE__ ;
2011-05-04 01:34:25 +02:00
MainWindow : : MainWindow ( string appPath ) :
2010-03-13 11:39:22 +01:00
wxFrame (
NULL , - 1 , Configuration : : ToUnicode ( winHeader ) ,
wxDefaultPosition , wxSize ( 800 , 600 ) ) {
SetExtraStyle ( wxFRAME_EX_CONTEXTHELP ) ;
2011-05-04 01:34:25 +02:00
this - > appPath = appPath ;
Properties : : setApplicationPath ( executable_path ( appPath ) ) ;
Config & config = Config : : getInstance ( ) ;
string iniFilePath = extractDirectoryPathFromFile ( config . getFileName ( false ) ) ;
configuration . load ( iniFilePath + " configuration.xml " ) ;
2010-03-13 11:39:22 +01:00
//Create(NULL, -1, "", wxDefaultPosition, wxDefaultSize, wxCAPTION | wxSYSTEM_MENU);
SetTitle ( Configuration : : ToUnicode ( ( " Configurator - " + configuration . getTitle ( ) + " - Editing " + configuration . getFileName ( ) ) . c_str ( ) ) ) ;
2011-05-04 01:34:25 +02:00
if ( configuration . getIcon ( ) ) {
string iconPath = configuration . getIconPath ( ) ;
printf ( " In [%s::%s] icon = [%s] \n " , __FILE__ , __FUNCTION__ , iconPath . c_str ( ) ) ;
2010-03-13 11:39:22 +01:00
wxInitAllImageHandlers ( ) ;
wxIcon icon ;
2011-05-04 01:34:25 +02:00
icon . LoadFile ( Configuration : : ToUnicode ( iconPath . c_str ( ) ) , wxBITMAP_TYPE_ICO ) ;
2010-03-13 11:39:22 +01:00
SetIcon ( icon ) ;
}
notebook = new wxNotebook ( this , - 1 ) ;
wxSizer * mainSizer = new wxBoxSizer ( wxVERTICAL ) ;
wxSizer * topSizer = new wxBoxSizer ( wxHORIZONTAL ) ;
topSizer - > Add ( notebook , 0 , wxALL , 0 ) ;
mainSizer - > Add ( topSizer , 0 , wxALL , margin ) ;
2010-10-21 09:20:17 +02:00
for ( unsigned int i = 0 ; i < configuration . getFieldGroupCount ( ) ; + + i ) {
2010-03-13 11:39:22 +01:00
//create page
FieldGroup * fg = configuration . getFieldGroup ( i ) ;
wxPanel * panel = new wxPanel ( notebook , - 1 ) ;
notebook - > AddPage ( panel , Configuration : : ToUnicode ( fg - > getName ( ) . c_str ( ) ) ) ;
//sizers
wxSizer * gridSizer = new wxFlexGridSizer ( 2 , margin , gridMarginHorizontal ) ;
wxSizer * panelSizer = new wxBoxSizer ( wxVERTICAL ) ;
panelSizer - > Add ( gridSizer , 0 , wxALL , panelMargin ) ;
panel - > SetSizer ( panelSizer ) ;
2010-10-21 09:20:17 +02:00
for ( unsigned int j = 0 ; j < fg - > getFieldCount ( ) ; + + j ) {
2010-03-13 11:39:22 +01:00
Field * f = fg - > getField ( j ) ;
FieldText * staticText = new FieldText ( panel , this , f ) ;
staticText - > SetAutoLayout ( true ) ;
gridSizer - > Add ( staticText ) ;
f - > createControl ( panel , gridSizer ) ;
idMap . insert ( IdPair ( staticText - > GetId ( ) , staticText ) ) ;
}
}
//buttons
wxSizer * bottomSizer = new wxBoxSizer ( wxHORIZONTAL ) ;
buttonOk = new wxButton ( this , biOk , wxT ( " OK " ) ) ;
buttonApply = new wxButton ( this , biApply , wxT ( " Apply " ) ) ;
buttonCancel = new wxButton ( this , biCancel , wxT ( " Cancel " ) ) ;
buttonDefault = new wxButton ( this , biDefault , wxT ( " Default " ) ) ;
bottomSizer - > Add ( buttonOk , 0 , wxALL , margin ) ;
bottomSizer - > Add ( buttonApply , 0 , wxRIGHT | wxDOWN | wxUP , margin ) ;
bottomSizer - > Add ( buttonCancel , 0 , wxRIGHT | wxDOWN | wxUP , margin ) ;
bottomSizer - > Add ( buttonDefault , 0 , wxRIGHT | wxDOWN | wxUP , margin ) ;
infoText = new wxTextCtrl ( this , - 1 , Configuration : : ToUnicode ( " Info text. " ) , wxDefaultPosition , wxDefaultSize , wxTE_MULTILINE | wxTE_READONLY ) ;
infoText - > SetSize ( infoText - > GetSize ( ) . x , 2 * infoText - > GetSize ( ) . y / 3 ) ;
infoText - > SetBackgroundColour ( buttonOk - > GetBackgroundColour ( ) ) ;
mainSizer - > Add ( infoText , 1 , wxGROW | wxALL | wxALIGN_CENTER , margin ) ;
mainSizer - > Add ( bottomSizer , 0 , wxALIGN_CENTER ) ;
SetBackgroundColour ( buttonOk - > GetBackgroundColour ( ) ) ;
SetSizerAndFit ( mainSizer ) ;
Refresh ( ) ;
}
void MainWindow : : init ( ) {
}
void MainWindow : : onButtonOk ( wxCommandEvent & event ) {
configuration . save ( ) ;
Close ( ) ;
}
void MainWindow : : onButtonApply ( wxCommandEvent & event ) {
configuration . save ( ) ;
}
void MainWindow : : onButtonCancel ( wxCommandEvent & event ) {
Close ( ) ;
}
void MainWindow : : onButtonDefault ( wxCommandEvent & event ) {
2010-10-21 09:20:17 +02:00
for ( unsigned int i = 0 ; i < configuration . getFieldGroupCount ( ) ; + + i ) {
2010-03-13 11:39:22 +01:00
FieldGroup * fg = configuration . getFieldGroup ( i ) ;
2010-10-21 09:20:17 +02:00
for ( unsigned int j = 0 ; j < fg - > getFieldCount ( ) ; + + j ) {
2010-03-13 11:39:22 +01:00
Field * f = fg - > getField ( j ) ;
f - > setValue ( f - > getDefaultValue ( ) ) ;
f - > updateControl ( ) ;
}
}
}
void MainWindow : : onClose ( wxCloseEvent & event ) {
Destroy ( ) ;
}
void MainWindow : : onMouseDown ( wxMouseEvent & event ) {
setInfoText ( " " ) ;
}
void MainWindow : : setInfoText ( const string & str ) {
infoText - > SetValue ( Configuration : : ToUnicode ( str . c_str ( ) ) ) ;
}
BEGIN_EVENT_TABLE ( MainWindow , wxFrame )
EVT_BUTTON ( biOk , MainWindow : : onButtonOk )
EVT_BUTTON ( biApply , MainWindow : : onButtonApply )
EVT_BUTTON ( biCancel , MainWindow : : onButtonCancel )
EVT_BUTTON ( biDefault , MainWindow : : onButtonDefault )
EVT_CLOSE ( MainWindow : : onClose )
EVT_LEFT_DOWN ( MainWindow : : onMouseDown )
END_EVENT_TABLE ( )
// ===============================================
// class FieldText
// ===============================================
FieldText : : FieldText ( wxWindow * parent , MainWindow * mainWindow , const Field * field ) :
wxStaticText ( parent , - 1 , Configuration : : ToUnicode ( field - > getName ( ) . c_str ( ) ) )
{
this - > mainWindow = mainWindow ;
this - > field = field ;
}
void FieldText : : onHelp ( wxHelpEvent & event ) {
string str = field - > getInfo ( ) + " . " ;
if ( ! field - > getDescription ( ) . empty ( ) ) {
str + = " \n " + field - > getDescription ( ) + " . " ;
}
mainWindow - > setInfoText ( str ) ;
}
BEGIN_EVENT_TABLE ( FieldText , wxStaticText )
EVT_HELP ( - 1 , FieldText : : onHelp )
END_EVENT_TABLE ( )
// ===============================================
// class App
// ===============================================
bool App : : OnInit ( ) {
2011-05-04 01:34:25 +02:00
try {
SystemFlags : : VERBOSE_MODE_ENABLED = true ;
string appPath = " " ;
wxString exe_path = wxStandardPaths : : Get ( ) . GetExecutablePath ( ) ;
2011-05-23 21:50:17 +02:00
# ifdef WIN32
2011-05-23 21:23:00 +02:00
const wxWX2MBbuf tmp_buf = wxConvCurrent - > cWX2MB ( wxFNCONV ( exe_path ) ) ;
appPath = tmp_buf ;
2011-05-23 21:50:17 +02:00
2011-05-23 21:23:00 +02:00
std : : auto_ptr < wchar_t > wstr ( Ansi2WideString ( appPath . c_str ( ) ) ) ;
appPath = utf8_encode ( wstr . get ( ) ) ;
2011-05-23 21:50:17 +02:00
# else
appPath = wxFNCONV ( exe_path ) ;
# endif
2011-05-23 21:23:00 +02:00
//#if defined(__MINGW32__)
// const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(exe_path));
// appPath = tmp_buf;
//#else
// appPath = wxFNCONV(exe_path);
//#endif
2011-05-04 01:34:25 +02:00
mainWindow = new MainWindow ( appPath ) ;
2010-03-13 11:39:22 +01:00
mainWindow - > Show ( ) ;
}
catch ( const exception & e ) {
wxMessageDialog ( NULL , Configuration : : ToUnicode ( e . what ( ) ) , Configuration : : ToUnicode ( " Exception " ) , wxOK | wxICON_ERROR ) . ShowModal ( ) ;
return 0 ;
}
return true ;
}
int App : : MainLoop ( ) {
try {
return wxApp : : MainLoop ( ) ;
}
catch ( const exception & e ) {
wxMessageDialog ( NULL , Configuration : : ToUnicode ( e . what ( ) ) , Configuration : : ToUnicode ( " Exception " ) , wxOK | wxICON_ERROR ) . ShowModal ( ) ;
return 0 ;
}
}
int App : : OnExit ( ) {
2011-05-24 03:44:48 +02:00
SystemFlags : : Close ( ) ;
SystemFlags : : SHUTDOWN_PROGRAM_MODE = true ;
2010-03-13 11:39:22 +01:00
return 0 ;
}
} //end namespace
IMPLEMENT_APP ( Configurator : : App )