]> git.saurik.com Git - wxWidgets.git/blob - demos/forty/scoredg.cpp
removed GSocket_[Un]Streamed
[wxWidgets.git] / demos / 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
12 #if defined(__GNUG__) && !defined(__APPLE__)
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "scorefil.h"
29 #include "scoredg.h"
30
31 #define USE_GRID_FOR_SCORE 0
32
33 #if USE_GRID_FOR_SCORE
34 #include "wx/grid.h"
35 #else
36 class ScoreCanvas : public wxScrolledWindow
37 {
38 public:
39 ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size);
40 virtual ~ScoreCanvas();
41
42 void OnDraw(wxDC& dc);
43
44 private:
45 wxFont *m_font;
46 wxString m_text;
47 };
48
49 ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size) :
50 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
51 {
52 SetBackgroundColour(*wxWHITE);
53 #ifdef __WXGTK__
54 m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
55 #else
56 m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
57 #endif
58
59 wxArrayString players;
60 scoreFile->GetPlayerList( players);
61
62 wxString os;
63
64 os << wxT("Player\tWins\tGames\tScore\n");
65 for (unsigned int i = 0; i < players.Count(); i++)
66 {
67 int wins, games, score;
68 scoreFile->ReadPlayersScore(players[i], wins, games, score);
69 int average = 0;
70 if (games > 0)
71 {
72 average = (2 * score + games) / (2 * games);
73 }
74
75 os << players[i] << wxT('\t')
76 << wins << wxT('\t')
77 << games << wxT('\t')
78 << average << wxT('\n');
79 }
80 os << wxT('\0');
81 m_text = os;
82 }
83
84 ScoreCanvas::~ScoreCanvas()
85 {
86 }
87
88 void ScoreCanvas::OnDraw(wxDC& dc)
89 {
90 dc.SetFont(* m_font);
91
92 const wxChar* str = m_text;
93 unsigned int tab = 0;
94 unsigned int tabstops[] = { 5, 100, 150, 200 };
95
96 // get the line spacing for the current font
97 int lineSpacing;
98 {
99 long w, h;
100 dc.GetTextExtent(wxT("Testing"), &w, &h);
101 lineSpacing = (int)h;
102 }
103
104 int y = 0;
105 while (*str)
106 {
107 wxChar text[256];
108 wxChar* dest = text;
109
110 while (*str && *str >= ' ') *dest++ = *str++;
111 *dest = '\0';
112
113 dc.DrawText(text, tabstops[tab], y);
114
115 if (*str == '\t')
116 {
117 if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1)
118 {
119 tab++;
120 }
121 }
122 else if (*str == '\n')
123 {
124 tab = 0;
125 y += lineSpacing;
126 }
127 if (*str) str++;
128 }
129 }
130 #endif
131
132 BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
133 EVT_CLOSE(ScoreDialog::OnCloseWindow)
134 END_EVENT_TABLE()
135
136 ScoreDialog::ScoreDialog(wxWindow* parent, ScoreFile* file) :
137 wxDialog(parent, wxID_ANY, _("Scores"),
138 wxDefaultPosition, wxSize(400, 300)),
139 m_scoreFile(file)
140 {
141 // create grid with players
142 wxArrayString players;
143 file->GetPlayerList(players);
144
145 wxSize sz = wxSize(400, 300);
146
147 #if USE_GRID_FOR_SCORE
148 wxGrid* list = new wxGrid(this, wxID_ANY, wxDefaultPosition, sz, 0);
149 list->CreateGrid(players.Count(), 4);
150 for (unsigned int i = 0; i < players.Count(); i++)
151 {
152 int wins, games, score;
153 wxString string_value;
154
155 file->ReadPlayersScore(players[i], wins, games, score);
156 int average = 0;
157 if (games > 0)
158 {
159 average = (2 * score + games) / (2 * games);
160 }
161 list->SetCellValue(i,0,players[i]);
162 string_value.Printf( _T("%u"), wins );
163 list->SetCellValue(i,1,string_value);
164 string_value.Printf( _T("%u"), games );
165 list->SetCellValue(i,2,string_value);
166 string_value.Printf( _T("%u"), average );
167 list->SetCellValue(i,3,string_value);
168 }
169 list->SetColLabelValue(0, _T("Players"));
170 list->SetColLabelValue(1, _T("Wins"));
171 list->SetColLabelValue(2, _T("Games"));
172 list->SetColLabelValue(3, _T("Score"));
173 list->SetEditable(false);
174 list->AutoSizeColumns();
175 list->AutoSizeRows();
176 list->SetRowLabelSize(0);
177 list->EnableDragRowSize(false);
178 list->EnableDragColSize(false);
179 list->EnableDragGridSize(false);
180 #else
181 ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile, wxDefaultPosition, sz);
182 #endif
183
184 // locate and resize with sizers
185 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
186 topsizer->Add( list, 1, wxALL|wxGROW, 10 );
187 topsizer->Add( new wxButton(this, wxID_OK, _("OK")), 0, wxALIGN_CENTER_HORIZONTAL|wxALL , 10 );
188
189 SetSizer( topsizer );
190
191 GetSizer()->Fit(this);
192 GetSizer()->SetSizeHints(this);
193
194 CentreOnParent();
195 }
196
197 ScoreDialog::~ScoreDialog()
198 {
199 }
200
201 void ScoreDialog::Display()
202 {
203 Show(true);
204 }
205
206 void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
207 {
208 EndModal(wxID_OK);
209 }