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