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