]>
Commit | Line | Data |
---|---|---|
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 | ||
14 | #if defined(__GNUG__) && !defined(__APPLE__) | |
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 | ||
30 | #include "scorefil.h" | |
31 | #include "scoredg.h" | |
32 | ||
33 | class ScoreCanvas : public wxScrolledWindow | |
34 | { | |
35 | public: | |
36 | ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile); | |
37 | virtual ~ScoreCanvas(); | |
38 | ||
39 | void OnDraw(wxDC& dc); | |
40 | ||
41 | private: | |
42 | wxFont* m_font; | |
43 | wxString m_text; | |
44 | }; | |
45 | ||
46 | ||
47 | ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile) : | |
48 | wxScrolledWindow(parent) | |
49 | { | |
50 | #ifdef __WXGTK__ | |
51 | m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL); | |
52 | #else | |
53 | m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL); | |
54 | #endif | |
55 | ||
56 | wxArrayString players; | |
57 | scoreFile->GetPlayerList( players); | |
58 | ||
59 | wxString os; | |
60 | ||
61 | os << wxT("Player\tWins\tGames\tScore\n"); | |
62 | for (unsigned int i = 0; i < players.Count(); i++) | |
63 | { | |
64 | int wins, games, score; | |
65 | scoreFile->ReadPlayersScore(players[i], wins, games, score); | |
66 | int average = 0; | |
67 | if (games > 0) | |
68 | { | |
69 | average = (2 * score + games) / (2 * games); | |
70 | } | |
71 | ||
72 | os << players[i] << wxT('\t') | |
73 | << wins << wxT('\t') | |
74 | << games << wxT('\t') | |
75 | << average << wxT('\n'); | |
76 | } | |
77 | os << wxT('\0'); | |
78 | m_text = os; | |
79 | } | |
80 | ||
81 | ScoreCanvas::~ScoreCanvas() | |
82 | { | |
83 | } | |
84 | ||
85 | void ScoreCanvas::OnDraw(wxDC& dc) | |
86 | { | |
87 | dc.SetFont(* m_font); | |
88 | ||
89 | const wxChar* str = m_text; | |
90 | unsigned int tab = 0; | |
91 | unsigned int tabstops[] = { 5, 100, 150, 200 }; | |
92 | ||
93 | // get the line spacing for the current font | |
94 | int lineSpacing; | |
95 | { | |
96 | long w, h; | |
97 | dc.GetTextExtent(wxT("Testing"), &w, &h); | |
98 | lineSpacing = (int)h; | |
99 | } | |
100 | ||
101 | int y = 0; | |
102 | while (*str) | |
103 | { | |
104 | wxChar text[256]; | |
105 | wxChar* dest = text; | |
106 | ||
107 | while (*str && *str >= ' ') *dest++ = *str++; | |
108 | *dest = '\0'; | |
109 | ||
110 | dc.DrawText(text, tabstops[tab], y); | |
111 | ||
112 | if (*str == '\t') | |
113 | { | |
114 | if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1) | |
115 | { | |
116 | tab++; | |
117 | } | |
118 | } | |
119 | else if (*str == '\n') | |
120 | { | |
121 | tab = 0; | |
122 | y += lineSpacing; | |
123 | } | |
124 | if (*str) str++; | |
125 | } | |
126 | } | |
127 | ||
128 | BEGIN_EVENT_TABLE(ScoreDialog, wxDialog) | |
129 | EVT_CLOSE(ScoreDialog::OnCloseWindow) | |
130 | END_EVENT_TABLE() | |
131 | ||
132 | ScoreDialog::ScoreDialog( | |
133 | wxWindow* parent, | |
134 | ScoreFile* file | |
135 | ) : | |
136 | wxDialog(parent, -1, _("Scores"), | |
137 | wxDefaultPosition, wxSize(310, 200), | |
138 | wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE), | |
139 | m_scoreFile(file) | |
140 | { | |
141 | // enable constraints | |
142 | SetAutoLayout (TRUE); | |
143 | ||
144 | ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile); | |
145 | m_OK = new wxButton(this, wxID_OK, _("OK")); | |
146 | ||
147 | wxLayoutConstraints* layout; | |
148 | ||
149 | // Constrain the OK button | |
150 | layout = new wxLayoutConstraints; | |
151 | layout->left.SameAs (this, wxLeft, 10); | |
152 | layout->bottom.SameAs (this, wxBottom, 10); | |
153 | layout->height.AsIs(); | |
154 | layout->width.AsIs(); | |
155 | m_OK->SetConstraints(layout); | |
156 | ||
157 | // Constrain the list of players | |
158 | layout = new wxLayoutConstraints; | |
159 | layout->left.SameAs (this, wxLeft, 10); | |
160 | layout->right.SameAs (this, wxRight, 10); | |
161 | layout->top.SameAs (this, wxTop, 10); | |
162 | layout->bottom.SameAs (m_OK, wxTop, 10); | |
163 | list->SetConstraints(layout); | |
164 | ||
165 | Layout(); | |
166 | } | |
167 | ||
168 | ScoreDialog::~ScoreDialog() | |
169 | { | |
170 | } | |
171 | ||
172 | void ScoreDialog::Display() | |
173 | { | |
174 | Show(TRUE); | |
175 | } | |
176 | ||
177 | void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
178 | { | |
179 | EndModal(wxID_OK); | |
180 | } |