Debian Codegraph @

src/highscores_dialog.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2006-2008 by Guy Rutenberg *
3 * [email protected] *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include <wx/button.h>
22 #include <wx/sizer.h>
23
24 #include "highscores_dialog.h"
25
26 #include "icon32.xpm"
27
28 using namespace std;
29 using namespace highscores_dialog;
30
31 const int HIGHSCORELIST_BORDER = 10;
32
33 HighscoresDialog::HighscoresDialog(wxWindow* parent,configuration::Configuration* config, int highlight_rank) :
34 wxDialog(parent, wxID_ANY, wxT("Highscores Table"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
35 {
36 this->highlight_rank = highlight_rank;
37 m_config = config;
38
39 SetIcon(wxIcon(icon32_xpm));
40
41 createControls();
42 loadData();
43 doLayout();
44 connectEventTable();
45 }
46
47 void HighscoresDialog::createControls()
48 {
49 highscoreslist = new wxListCtrl(this,wxID_ANY,wxDefaultPosition,wxSize(450,400),wxLC_REPORT | wxBORDER_SUNKEN );
50
51 wxListItem itemCol;
52
53 itemCol.SetText(wxT("Rank"));
54 itemCol.SetImage(-1);
55 highscoreslist->InsertColumn(0, itemCol);
56
57 itemCol.SetText(wxT("Name"));
58 itemCol.SetImage(-1);
59 highscoreslist->InsertColumn(1, itemCol);
60
61 itemCol.SetText(wxT("Score"));
62 itemCol.SetImage(-1);
63 highscoreslist->InsertColumn(2, itemCol);
64
65 itemCol.SetText(wxT("Date"));
66 itemCol.SetImage(-1);
67 highscoreslist->InsertColumn(3, itemCol);
68
69 highscoreslist->SetColumnWidth(0, 50 );
70 highscoreslist->SetColumnWidth(1, 195 );
71 highscoreslist->SetColumnWidth(2, 60 );
72 highscoreslist->SetColumnWidth(3, 140 );
73 }
74
75 void HighscoresDialog::loadData()
76 {
77 highscoreslist->DeleteAllItems();
78 const configuration::HighscoresList *list = m_config->getHighscores();
79
80 configuration::HighscoresList::const_iterator it;
81 wxString buf;
82
83 int i = 0;
84 for (it = list->begin(); it!=list->end(); it++, i++) {
85 // rank
86 buf.Clear();
87 buf<<(i+1);
88 highscoreslist->InsertItem(i,buf,-1);
89
90 // name
91 buf = wxString::FromUTF8(it->name.c_str());
92 highscoreslist->SetItem(i,1,buf);
93
94 // score
95 buf.Clear();
96 buf<< it->score;
97 highscoreslist->SetItem(i,2,buf);
98
99 // date
100 buf = wxString::FromUTF8(it->date.c_str());
101 highscoreslist->SetItem(i,3,buf);
102 }
103
104 if (highlight_rank) {
105 highscoreslist->SetItemTextColour(highlight_rank-1,*wxRED);
106 }
107 }
108
109 void HighscoresDialog::doLayout()
110 {
111 wxBoxSizer *top_sizer = new wxBoxSizer( wxVERTICAL );
112
113 top_sizer->Add(highscoreslist, 1, wxEXPAND | wxALL, HIGHSCORELIST_BORDER);
114
115 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
116
117 wxSizerFlags flags = wxSizerFlags().Border(wxALL & ~wxLEFT, 10);
118
119 button_sizer->AddStretchSpacer();
120 button_sizer->Add(new wxButton(this,wxID_CLEAR),flags);
121 button_sizer->Add(new wxButton(this,ID_CONFIGURE,wxT("Configure")),flags);
122 button_sizer->Add( new wxButton(this,wxID_CLOSE), flags);
123
124 top_sizer->Add(button_sizer,0,wxEXPAND);
125
126 SetSizer(top_sizer);
127 top_sizer->SetSizeHints(this);
128 }
129
130
131 void HighscoresDialog::connectEventTable()
132 {
133 Connect(wxID_CLOSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(HighscoresDialog::onClose));
134
135 Connect(wxID_CLEAR, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(HighscoresDialog::onClear));
136 Connect(ID_CONFIGURE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(HighscoresDialog::onConfigure));
137 Connect(this->GetId(), wxEVT_SIZE, wxSizeEventHandler(HighscoresDialog::onResize));
138 }
139 void HighscoresDialog::onClose(wxCommandEvent& event)
140 {
141 Close();
142 }
143
144 void HighscoresDialog::onConfigure(wxCommandEvent& event)
145 {
146 HighscoresSettingsDialog *dialog = new HighscoresSettingsDialog(this, m_config);
147
148 if (dialog->ShowModal() == wxID_OK) {
149 loadData();
150 }
151
152 delete dialog;
153 }
154
155 void HighscoresDialog::onClear(wxCommandEvent& event)
156 {
157 int answer = wxMessageBox(
158 wxT("Are you sure you want to clear the Highscores table?\nThis action cannot be reversed."),
159 wxT("Are You Sure?"),
160 wxYES | wxNO,
161 this);
162
163 if (answer == wxYES) {
164 m_config->clearHighscores();
165 loadData();
166 }
167 }
168
169
170 HighscoresSettingsDialog::HighscoresSettingsDialog(wxWindow* parent, configuration::Configuration* config) :
171 wxDialog(parent, wxID_ANY, wxT("Highscores Table Settings"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE)
172 {
173 this->m_config = config;
174 doLayout();
175
176 Connect(wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(HighscoresSettingsDialog::onOk));
177 }
178
179 void HighscoresSettingsDialog::doLayout()
180 {
181 wxBoxSizer* top_sizer = new wxBoxSizer(wxVERTICAL);
182
183 wxBoxSizer* size_sizer = new wxBoxSizer(wxHORIZONTAL);
184
185 size_sizer->Add(new wxStaticText(this, wxID_ANY, wxT("High score table size:")), wxSizerFlags().Border(wxALL,10));
186
187 int size = atoi(m_config->get("highscore-list-size").c_str());
188 spin_ctrl = new wxSpinCtrl(this, wxID_ANY);
189 spin_ctrl->SetRange(10,200);
190 spin_ctrl->SetValue(size);
191
192 size_sizer->Add(spin_ctrl,wxSizerFlags().Border(wxALL & ~wxLEFT,10));
193
194 top_sizer->Add(size_sizer);
195 top_sizer->Add(CreateButtonSizer(wxOK|wxCANCEL), 1, wxBOTTOM, 10);
196
197 SetSizer(top_sizer);
198 top_sizer->SetSizeHints(this);
199 }
200
201 void HighscoresSettingsDialog::onOk(wxCommandEvent& event) {
202 int size = spin_ctrl->GetValue();
203 m_config->setHighscoresSize(size);
204 event.Skip();
205 }
206
207 void HighscoresDialog::onResize(wxSizeEvent &event) {
208
209 const int width = event.GetSize().GetWidth();
210 const int rank_width = highscoreslist->GetColumnWidth(0);
211 const int score_width = highscoreslist->GetColumnWidth(2);
212 const int date_width = highscoreslist->GetColumnWidth(3);
213 const int new_width = width - 2*HIGHSCORELIST_BORDER - rank_width - score_width - date_width;
214
215 highscoreslist->SetColumnWidth(1,new_width);
216 event.Skip();
217 }