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