]> git.saurik.com Git - wxWidgets.git/blob - samples/forty/scoredg.cpp
Removed /install/gtk/configure from cvs
[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 wxArrayString players;
62 scoreFile->GetPlayerList( players);
63
64 ostrstream os;
65
66 os << "Player\tWins\tGames\tScore\n";
67 for (int i = 0; i < players.Count(); i++)
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
77 os << players[i] << '\t'
78 << wins << '\t'
79 << games << '\t'
80 << average << '\n';
81 }
82 os << '\0';
83 char* str = os.str();
84 m_text = str;
85 delete str;
86 }
87
88 ScoreCanvas::~ScoreCanvas()
89 {
90 }
91
92 void ScoreCanvas::OnDraw(wxDC& dc)
93 {
94 dc.SetFont(m_font);
95
96 const char* str = m_text;
97 unsigned int tab = 0;
98 unsigned int tabstops[] = { 5, 100, 150, 200 };
99
100 // get the line spacing for the current font
101 int lineSpacing;
102 {
103 long w, h;
104 dc.GetTextExtent("Testing", &w, &h);
105 lineSpacing = (int)h;
106 }
107
108 int y = 0;
109 while (*str)
110 {
111 char text[256];
112 char* dest = text;
113
114 while (*str && *str >= ' ') *dest++ = *str++;
115 *dest = '\0';
116
117 dc.DrawText(text, tabstops[tab], y);
118
119 if (*str == '\t')
120 {
121 if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1)
122 {
123 tab++;
124 }
125 }
126 else if (*str == '\n')
127 {
128 tab = 0;
129 y += lineSpacing;
130 }
131 if (*str) str++;
132 }
133 }
134
135
136 ScoreDialog::ScoreDialog(
137 wxWindow* parent,
138 ScoreFile* file
139 ) :
140 wxDialog(parent, -1, "Scores",
141 wxDefaultPosition, wxSize(310, 200),
142 wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
143 m_scoreFile(file)
144 {
145 // enable constraints
146 SetAutoLayout (TRUE);
147
148 ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile);
149 m_OK = new wxButton(this, wxID_OK, "OK");
150
151 wxLayoutConstraints* layout;
152
153 // Constrain the OK button
154 layout = new wxLayoutConstraints;
155 layout->left.SameAs (this, wxLeft, 10);
156 layout->bottom.SameAs (this, wxBottom, 10);
157 layout->height.AsIs();
158 layout->width.AsIs();
159 m_OK->SetConstraints(layout);
160
161 // Constrain the list of players
162 layout = new wxLayoutConstraints;
163 layout->left.SameAs (this, wxLeft, 10);
164 layout->right.SameAs (this, wxRight, 10);
165 layout->top.SameAs (this, wxTop, 10);
166 layout->bottom.SameAs (m_OK, wxTop, 10);
167 list->SetConstraints(layout);
168
169 Layout();
170 }
171
172 ScoreDialog::~ScoreDialog()
173 {
174 }
175
176 void ScoreDialog::Display()
177 {
178 Show(TRUE);
179 }
180
181 bool ScoreDialog::OnClose()
182 {
183 // hide the dialog
184 // NB don't return TRUE otherwise delete is called
185 Show(FALSE);
186 return FALSE;
187 }