File: main.cpp

package info (click to toggle)
glest 3.2.2-2
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 2,800 kB
  • ctags: 6,581
  • sloc: cpp: 32,575; sh: 8,341; makefile: 63
file content (197 lines) | stat: -rw-r--r-- 5,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#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>

using namespace std;

namespace Configurator{

// ===============================================
// 	class MainWindow
// ===============================================


const int MainWindow::margin= 10;
const int MainWindow::panelMargin= 20;
const int MainWindow::gridMarginHorizontal= 30;

MainWindow::MainWindow(){
	
	SetExtraStyle(wxFRAME_EX_CONTEXTHELP);
	
	configuration.load("configuration.xml");

	Create(NULL, -1, "", wxDefaultPosition,  wxDefaultSize, wxCAPTION | wxSYSTEM_MENU);

	SetTitle(("Configurator - " + configuration.getTitle() + " - Editing " + configuration.getFileName()).c_str());

	if(configuration.getIcon()){
		wxIcon icon;
		icon.LoadFile(configuration.getIconPath().c_str(), wxBITMAP_TYPE_ICO);
		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);

	for(int i=0; i<configuration.getFieldGroupCount(); ++i){

		//create page
		FieldGroup *fg= configuration.getFieldGroup(i);
		wxPanel *panel= new wxPanel(notebook, -1);
		notebook->AddPage(panel, 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);
			
		for(int j=0; j<fg->getFieldCount(); ++j){
			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, "OK");
	buttonApply= new wxButton(this, biApply, "Apply");
	buttonCancel= new wxButton(this, biCancel, "Cancel");
	buttonDefault= new wxButton(this, biDefault, "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, "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::onButtonOk(wxCommandEvent &event){
	configuration.save();
	Close();
}

void MainWindow::onButtonApply(wxCommandEvent &event){
	configuration.save();
}

void MainWindow::onButtonCancel(wxCommandEvent &event){
	Close();
}

void MainWindow::onButtonDefault(wxCommandEvent &event){
	for(int i=0; i<configuration.getFieldGroupCount(); ++i){
		FieldGroup *fg= configuration.getFieldGroup(i);
		for(int j=0; j<fg->getFieldCount(); ++j){
			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(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, 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(){
	try{
		mainWindow= new MainWindow();
		mainWindow->Show();
	}
	catch(const exception &e){
		wxMessageDialog(NULL, e.what(), "Exception", wxOK | wxICON_ERROR).ShowModal();
		return 0;
	}
	return true;
}

int App::MainLoop(){
	try{
		return wxApp::MainLoop();
	}
	catch(const exception &e){
		wxMessageDialog(NULL, e.what(), "Exception", wxOK | wxICON_ERROR).ShowModal();
		return 0;
	}
}

int App::OnExit(){
	return 0;
}

}//end namespace

IMPLEMENT_APP(Configurator::App)