]> git.saurik.com Git - wxWidgets.git/blob - samples/forty/scoredg.cpp
Added the forgotten file txtstrm.tex
[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 BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
141 EVT_CLOSE(ScoreDialog::OnCloseWindow)
142 END_EVENT_TABLE()
143
144 ScoreDialog::ScoreDialog(
145 wxWindow* parent,
146 ScoreFile* file
147 ) :
148 wxDialog(parent, -1, "Scores",
149 wxDefaultPosition, wxSize(310, 200),
150 wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
151 m_scoreFile(file)
152 {
153 // enable constraints
154 SetAutoLayout (TRUE);
155
156 ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile);
157 m_OK = new wxButton(this, wxID_OK, "OK");
158
159 wxLayoutConstraints* layout;
160
161 // Constrain the OK button
162 layout = new wxLayoutConstraints;
163 layout->left.SameAs (this, wxLeft, 10);
164 layout->bottom.SameAs (this, wxBottom, 10);
165 layout->height.AsIs();
166 layout->width.AsIs();
167 m_OK->SetConstraints(layout);
168
169 // Constrain the list of players
170 layout = new wxLayoutConstraints;
171 layout->left.SameAs (this, wxLeft, 10);
172 layout->right.SameAs (this, wxRight, 10);
173 layout->top.SameAs (this, wxTop, 10);
174 layout->bottom.SameAs (m_OK, wxTop, 10);
175 list->SetConstraints(layout);
176
177 Layout();
178 }
179
180 ScoreDialog::~ScoreDialog()
181 {
182 }
183
184 void ScoreDialog::Display()
185 {
186 Show(TRUE);
187 }
188
189 void ScoreDialog::OnCloseWindow(wxCloseEvent& event)
190 {
191 EndModal(wxID_OK);
192 }