File: AtlasWindow.cpp

package info (click to toggle)
0ad 0.0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,292 kB
  • sloc: cpp: 245,166; ansic: 200,249; python: 13,754; sh: 6,104; perl: 4,620; makefile: 977; xml: 810; java: 533; ruby: 229; erlang: 46; pascal: 30; sql: 21; tcl: 4
file content (317 lines) | stat: -rw-r--r-- 8,517 bytes parent folder | download | duplicates (10)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Copyright (C) 2012 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "AtlasWindow.h"

#include "AtlasObject/AtlasObject.h"
#include "General/AtlasWindowCommand.h"
#include "General/Datafile.h"

#include "wx/artprov.h"
#include "wx/config.h"
#include "wx/file.h"

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

class SaveOnExitDialog : public wxDialog
{
public:
	SaveOnExitDialog(wxWindow* parent, bool allowCancel)
		: wxDialog(parent, wxID_ANY, (wxString) _("Save changes?"))
	{
		wxBitmap bitmap = wxArtProvider::GetIcon(wxART_WARNING, wxART_MESSAGE_BOX);
		wxBoxSizer* topsizer = new wxBoxSizer(wxHORIZONTAL);
		topsizer->Add(new wxStaticBitmap(this, wxID_ANY, bitmap),
						wxSizerFlags().Centre()/*.Border(wxALL, 14)*/);

		topsizer->Add(new wxStaticText(this, wxID_ANY, _("Would you like to save your changes to the current document?")),
						wxSizerFlags().Centre().Border(wxLEFT, 10));

		wxStdDialogButtonSizer* buttons = new wxStdDialogButtonSizer();
		buttons->AddButton(new wxButton(this, wxID_SAVE, _("&Save changes"))); // use _SAVE (instead of _YES) so that nice things happen on Macs
		buttons->AddButton(new wxButton(this, wxID_NO, _("&Discard changes")));
		if (allowCancel)
			buttons->AddButton(new wxButton(this, wxID_CANCEL, _("&Cancel")));
		buttons->Realize();

		wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
		sizer->Add(topsizer, wxSizerFlags().Proportion(1).Centre().Border(wxLEFT|wxRIGHT|wxTOP, 10));
		sizer->Add(buttons, wxSizerFlags().Centre().Border(wxALL, 10));

		SetSizer(sizer);
		sizer->SetSizeHints(this);
	}

	void OnSave(wxCommandEvent& WXUNUSED(event)) { EndModal(wxID_SAVE); }
	void OnNo(wxCommandEvent& WXUNUSED(event)) { EndModal(wxID_NO); }

	DECLARE_EVENT_TABLE();
};

BEGIN_EVENT_TABLE(SaveOnExitDialog, wxDialog)
	EVT_BUTTON(wxID_SAVE, SaveOnExitDialog::OnSave)
	EVT_BUTTON(wxID_NO, SaveOnExitDialog::OnNo)
END_EVENT_TABLE()

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

IMPLEMENT_CLASS(AtlasWindow, wxFrame);

BEGIN_EVENT_TABLE(AtlasWindow, wxFrame)
	EVT_MENU(wxID_NEW, AtlasWindow::OnNew)
//	EVT_MENU(ID_Import, AtlasWindow::OnImport)
//	EVT_MENU(ID_Export, AtlasWindow::OnExport)
	EVT_MENU(wxID_OPEN, AtlasWindow::OnOpen)
	EVT_MENU(wxID_SAVE, AtlasWindow::OnSave)
	EVT_MENU(wxID_SAVEAS, AtlasWindow::OnSaveAs)
	EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, AtlasWindow::OnMRUFile)
	EVT_MENU(wxID_EXIT,  AtlasWindow::OnQuit)

	EVT_MENU(wxID_UNDO, AtlasWindow::OnUndo)
	EVT_MENU(wxID_REDO, AtlasWindow::OnRedo)

	EVT_CLOSE(AtlasWindow::OnClose)
END_EVENT_TABLE()

AtlasWindow::AtlasWindow(wxWindow* parent, const wxString& title, const wxSize& size)
	: wxFrame(parent, wxID_ANY, _T(""), wxDefaultPosition, size),
	m_WindowTitle(title), m_FileHistory(title)
{

	m_MenuBar = new wxMenuBar;
	SetMenuBar(m_MenuBar);

	wxMenu *menuFile = new wxMenu;
	m_MenuBar->Append(menuFile, _("&File"));
	{
		menuFile->Append(wxID_NEW, _("&New\tCtrl+N"));
//		menuFile->Append(ID_Import, _("&Import..."));
//		menuFile->Append(ID_Export, _("&Export..."));
		menuFile->Append(wxID_OPEN, _("&Open...\tCtrl+O"));
		menuFile->Append(wxID_SAVE, _("&Save\tCtrl+S"));
		menuFile->Append(wxID_SAVEAS, _("Save &As..."));
		menuFile->AppendSeparator();//-----------
		menuFile->Append(wxID_EXIT,   _("E&xit"));
		m_FileHistory.UseMenu(menuFile);//-------
		m_FileHistory.AddFilesToMenu();
	}

	m_menuItem_Save = menuFile->FindItem(wxID_SAVE); // remember this item, to let it be greyed out
	wxASSERT(m_menuItem_Save);

	wxMenu *menuEdit = new wxMenu;
	m_MenuBar->Append(menuEdit, _("&Edit"));
	{
		menuEdit->Append(wxID_UNDO, _("&Undo"));
		menuEdit->Append(wxID_REDO, _("&Redo"));
	}

	m_CommandProc.SetEditMenu(menuEdit);
	m_CommandProc.Initialize();

	m_FileHistory.LoadFromSubDir(*wxConfigBase::Get());

	CreateStatusBar();

	SetCurrentFilename();
}

void AtlasWindow::AddCustomMenu(wxMenu* menu, const wxString& title)
{
	m_MenuBar->Append(menu, title);
}

void AtlasWindow::OnQuit(wxCommandEvent& WXUNUSED(event))
{
	Close();
}

void AtlasWindow::OnClose(wxCloseEvent& event)
{
	SaveOnExitDialog dialog(this, event.CanVeto());
	int ret = dialog.ShowModal();

	if (ret == wxID_SAVE)
	{
		if (SaveChanges(false))
			event.Skip(); // save succeeded; exit
		else
			event.Veto(); // save failed; don't exit
	}
	else if (ret == wxID_NO)
	{
		// discard changes
		event.Skip();
	}
	else
	{
		assert(ret == wxID_CANCEL);
		event.Veto();
	}

	if (event.GetSkipped())
		m_FileHistory.SaveToSubDir(*wxConfigBase::Get());
}

void AtlasWindow::OnUndo(wxCommandEvent& WXUNUSED(event))
{
	m_CommandProc.Undo();
}

void AtlasWindow::OnRedo(wxCommandEvent& WXUNUSED(event))
{
	m_CommandProc.Redo();
}


//void AtlasWindow::OnImport(wxCommandEvent& WXUNUSED(event))
//{
//	...
//}
//
//void AtlasWindow::OnExport(wxCommandEvent& WXUNUSED(event))
//{
//	...
//}

void AtlasWindow::OnNew(wxCommandEvent& WXUNUSED(event))
{
	AtObj blank;

	AtlasWindowCommandProc* commandProc = AtlasWindowCommandProc::GetFromParentFrame(this);
	commandProc->Submit(new AtlasCommand_Begin(_("New file"), this));
	ImportData(blank);
	commandProc->Submit(new AtlasCommand_End());

	SetCurrentFilename();
}

void AtlasWindow::OnOpen(wxCommandEvent& WXUNUSED(event))
{
	wxString path, name;
	if (GetCurrentFilename().IsOk())
	{
		path = GetCurrentFilename().GetPath();
		name = GetCurrentFilename().GetFullName();
	}
	else
	{
		path = GetDefaultOpenDirectory();
	}

	wxFileDialog dlg (this, _("Select XML file to open"), path, name, _("XML files (*.xml)|*.xml|All files (*.*)|*.*"), wxFD_OPEN);
	// Set default filter
	dlg.SetFilterIndex(0);

	if (dlg.ShowModal() != wxID_OK)
		return;

	OpenFile(dlg.GetPath());
}

void AtlasWindow::OnSave(wxCommandEvent& WXUNUSED(event))
{
	SaveChanges(false);
}

void AtlasWindow::OnSaveAs(wxCommandEvent& WXUNUSED(event))
{
	SaveChanges(true);
}

void AtlasWindow::OnMRUFile(wxCommandEvent& event)
{
	wxString file (m_FileHistory.GetHistoryFile(event.GetId() - wxID_FILE1));
	if (file.Length())
		OpenFile(file);
}


void AtlasWindow::SetCurrentFilename(wxFileName filename)
{
	m_CurrentFilename = filename;

	if (filename.IsOk())
		SetTitle(m_WindowTitle + _T(" - ") + filename.GetName());
	else
		SetTitle(m_WindowTitle + _T(" - ") + _("Unnamed file"));

	if (m_menuItem_Save)
		m_menuItem_Save->Enable(filename.IsOk());
}


bool AtlasWindow::SaveChanges(bool forceSaveAs)
{
	if (forceSaveAs || !GetCurrentFilename().IsOk())
	{
		wxFileDialog dlg (this, _("Select XML file to save as"),
			GetCurrentFilename().GetPath(), GetCurrentFilename().GetFullName(),
			//_T(""), _T(""),
			_("XML files (*.xml)|*.xml|All files (*.*)|*.*"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
		// Set default filter
		dlg.SetFilterIndex(0);

		if (dlg.ShowModal() != wxID_OK)
			return false;

		wxString filename(dlg.GetPath());

		m_FileHistory.AddFileToHistory(filename);
		SetCurrentFilename(filename);
	}

	if (! GetCurrentFilename().IsOk())
	{
		wxLogError(_T("Invalid 'save as' filename"));
		return false;
	}

	AtObj file (ExportData());
	// TODO: Make sure it succeeded. Back up .xml file in case it didn't.

	std::string xml = AtlasObject::SaveToXML(file);
	wxCHECK(!xml.empty(), false);

	wxFile outfile (GetCurrentFilename().GetFullPath(), wxFile::write);
	outfile.Write(xml.c_str(), xml.length());
	outfile.Close();

	sig_FileSaved();

	return true;
}

bool AtlasWindow::OpenFile(const wxString& filename)
{
	std::string xml;
	wxCHECK(Datafile::SlurpFile(filename, xml), false);
	AtObj file (AtlasObject::LoadFromXML(xml));
	// TODO: Make sure it succeeded.

	AtlasWindowCommandProc* commandProc = AtlasWindowCommandProc::GetFromParentFrame(this);
	commandProc->Submit(new AtlasCommand_Begin(_("Open file"), this));
	ImportData(file);
	commandProc->Submit(new AtlasCommand_End());

	m_FileHistory.AddFileToHistory(filename);
	SetCurrentFilename(filename);

	return true;
}