]>
Commit | Line | Data |
---|---|---|
63cafd27 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: scoredg.cpp | |
3 | // Purpose: Forty Thieves patience game | |
4 | // Author: Chris Breeze | |
5 | // Modified by: | |
6 | // Created: 21/07/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1993-1998 Chris Breeze | |
9 | // Licence: wxWindows licence | |
10 | //--------------------------------------------------------------------------- | |
11 | // Last modified: 22nd July 1998 - ported to wxWindows 2.0 | |
12 | ///////////////////////////////////////////////////////////////////////////// | |
13 | ||
ab7ce33c | 14 | #if defined(__GNUG__) && !defined(__APPLE__) |
63cafd27 JS |
15 | #pragma implementation |
16 | #pragma interface | |
17 | #endif | |
18 | ||
19 | // For compilers that support precompilation, includes "wx/wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/wx.h" | |
28 | #endif | |
29 | ||
63cafd27 JS |
30 | #include "scorefil.h" |
31 | #include "scoredg.h" | |
32 | ||
e0b5519a JS |
33 | #define USE_GRID_FOR_SCORE 0 |
34 | ||
35 | #if USE_GRID_FOR_SCORE | |
36 | #include "wx/grid.h" | |
37 | #else | |
63cafd27 JS |
38 | class ScoreCanvas : public wxScrolledWindow |
39 | { | |
40 | public: | |
e0b5519a | 41 | ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size); |
63cafd27 JS |
42 | virtual ~ScoreCanvas(); |
43 | ||
44 | void OnDraw(wxDC& dc); | |
45 | ||
46 | private: | |
47 | wxFont* m_font; | |
48 | wxString m_text; | |
49 | }; | |
50 | ||
e0b5519a JS |
51 | ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size) : |
52 | wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER) | |
63cafd27 | 53 | { |
e0b5519a | 54 | SetBackgroundColour(*wxWHITE); |
63cafd27 JS |
55 | #ifdef __WXGTK__ |
56 | m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL); | |
57 | #else | |
58 | m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL); | |
59 | #endif | |
60 | ||
e0b5519a | 61 | wxArrayString players; |
54ff4a70 | 62 | scoreFile->GetPlayerList( players); |
63cafd27 | 63 | |
f37c24e0 | 64 | wxString os; |
63cafd27 | 65 | |
0447ac8e | 66 | os << wxT("Player\tWins\tGames\tScore\n"); |
cb43b372 | 67 | for (unsigned int i = 0; i < players.Count(); i++) |
63cafd27 JS |
68 | { |
69 | int wins, games, score; | |
70 | scoreFile->ReadPlayersScore(players[i], wins, games, score); | |
71 | int average = 0; | |
72 | if (games > 0) | |
73 | { | |
74 | average = (2 * score + games) / (2 * games); | |
75 | } | |
76 | ||
0447ac8e JS |
77 | os << players[i] << wxT('\t') |
78 | << wins << wxT('\t') | |
79 | << games << wxT('\t') | |
80 | << average << wxT('\n'); | |
63cafd27 | 81 | } |
0447ac8e | 82 | os << wxT('\0'); |
f37c24e0 | 83 | m_text = os; |
63cafd27 JS |
84 | } |
85 | ||
86 | ScoreCanvas::~ScoreCanvas() | |
87 | { | |
88 | } | |
89 | ||
90 | void ScoreCanvas::OnDraw(wxDC& dc) | |
91 | { | |
16553659 | 92 | dc.SetFont(* m_font); |
63cafd27 | 93 | |
1babe8fd | 94 | const wxChar* str = m_text; |
63cafd27 JS |
95 | unsigned int tab = 0; |
96 | unsigned int tabstops[] = { 5, 100, 150, 200 }; | |
97 | ||
98 | // get the line spacing for the current font | |
99 | int lineSpacing; | |
100 | { | |
101 | long w, h; | |
1babe8fd | 102 | dc.GetTextExtent(wxT("Testing"), &w, &h); |
63cafd27 JS |
103 | lineSpacing = (int)h; |
104 | } | |
105 | ||
106 | int y = 0; | |
107 | while (*str) | |
108 | { | |
1babe8fd JS |
109 | wxChar text[256]; |
110 | wxChar* dest = text; | |
63cafd27 | 111 | |
0447ac8e JS |
112 | while (*str && *str >= ' ') *dest++ = *str++; |
113 | *dest = '\0'; | |
63cafd27 JS |
114 | |
115 | dc.DrawText(text, tabstops[tab], y); | |
116 | ||
0447ac8e | 117 | if (*str == '\t') |
63cafd27 JS |
118 | { |
119 | if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1) | |
120 | { | |
121 | tab++; | |
122 | } | |
123 | } | |
0447ac8e | 124 | else if (*str == '\n') |
63cafd27 JS |
125 | { |
126 | tab = 0; | |
127 | y += lineSpacing; | |
128 | } | |
129 | if (*str) str++; | |
130 | } | |
131 | } | |
e0b5519a | 132 | #endif |
63cafd27 | 133 | |
e3065973 JS |
134 | BEGIN_EVENT_TABLE(ScoreDialog, wxDialog) |
135 | EVT_CLOSE(ScoreDialog::OnCloseWindow) | |
136 | END_EVENT_TABLE() | |
63cafd27 | 137 | |
e0b5519a JS |
138 | ScoreDialog::ScoreDialog(wxWindow* parent, ScoreFile* file) : |
139 | wxDialog(parent, wxID_ANY, _("Scores"), | |
140 | wxDefaultPosition, wxSize(400, 300), | |
63cafd27 JS |
141 | wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE), |
142 | m_scoreFile(file) | |
143 | { | |
e0b5519a JS |
144 | // create grid with players |
145 | wxArrayString players; | |
146 | file->GetPlayerList(players); | |
147 | ||
148 | wxSize sz = wxSize(400, 300); | |
149 | ||
150 | #if USE_GRID_FOR_SCORE | |
151 | wxGrid* list = new wxGrid(this, wxID_ANY, wxDefaultPosition, sz, 0); | |
152 | list->CreateGrid(players.Count(), 4); | |
153 | for (unsigned int i = 0; i < players.Count(); i++) | |
154 | { | |
155 | int wins, games, score; | |
156 | wxString string_value; | |
157 | ||
158 | file->ReadPlayersScore(players[i], wins, games, score); | |
159 | int average = 0; | |
160 | if (games > 0) | |
161 | { | |
162 | average = (2 * score + games) / (2 * games); | |
163 | } | |
164 | list->SetCellValue(i,0,players[i]); | |
165 | string_value.Printf( _T("%u"), wins ); | |
166 | list->SetCellValue(i,1,string_value); | |
167 | string_value.Printf( _T("%u"), games ); | |
168 | list->SetCellValue(i,2,string_value); | |
169 | string_value.Printf( _T("%u"), average ); | |
170 | list->SetCellValue(i,3,string_value); | |
171 | } | |
172 | list->SetColLabelValue(0, _T("Players")); | |
173 | list->SetColLabelValue(1, _T("Wins")); | |
174 | list->SetColLabelValue(2, _T("Games")); | |
175 | list->SetColLabelValue(3, _T("Score")); | |
176 | list->SetEditable(false); | |
177 | list->AutoSizeColumns(); | |
178 | list->AutoSizeRows(); | |
179 | list->SetRowLabelSize(0); | |
180 | list->EnableDragRowSize(false); | |
181 | list->EnableDragColSize(false); | |
182 | list->EnableDragGridSize(false); | |
183 | #else | |
184 | ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile, wxDefaultPosition, sz); | |
185 | #endif | |
186 | ||
187 | // locate and resize with sizers | |
188 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); | |
189 | topsizer->Add( list, 1, wxALL|wxGROW, 10 ); | |
190 | topsizer->Add( new wxButton(this, wxID_OK, _("OK")), 0, wxALIGN_CENTER_HORIZONTAL|wxALL , 10 ); | |
191 | ||
192 | SetSizer( topsizer ); | |
193 | ||
194 | GetSizer()->Fit(this); | |
195 | GetSizer()->SetSizeHints(this); | |
196 | ||
197 | CentreOnParent(); | |
63cafd27 JS |
198 | } |
199 | ||
200 | ScoreDialog::~ScoreDialog() | |
201 | { | |
202 | } | |
203 | ||
204 | void ScoreDialog::Display() | |
205 | { | |
e0b5519a | 206 | Show(true); |
63cafd27 JS |
207 | } |
208 | ||
babd36de | 209 | void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
63cafd27 | 210 | { |
e3065973 | 211 | EndModal(wxID_OK); |
63cafd27 | 212 | } |