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