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