]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/scoredg.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / demos / forty / scoredg.cpp
CommitLineData
63cafd27
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: scoredg.cpp
3// Purpose: Forty Thieves patience game
4// Author: Chris Breeze
5// Modified by:
6// Created: 21/07/97
63cafd27 7// Copyright: (c) 1993-1998 Chris Breeze
2a21ac15 8// Licence: wxWindows licence
63cafd27
JS
9/////////////////////////////////////////////////////////////////////////////
10
63cafd27
JS
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
63cafd27
JS
22#include "scorefil.h"
23#include "scoredg.h"
24
b6d0f0c7
WS
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
e0b5519a
JS
28
29#if USE_GRID_FOR_SCORE
30#include "wx/grid.h"
31#else
63cafd27
JS
32class ScoreCanvas : public wxScrolledWindow
33{
34public:
2a21ac15
DS
35 ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size);
36 virtual ~ScoreCanvas();
63cafd27 37
2a21ac15 38 void OnDraw(wxDC& dc);
63cafd27
JS
39
40private:
2a21ac15
DS
41 wxFont *m_font;
42 wxString m_text;
63cafd27
JS
43};
44
e0b5519a 45ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size) :
2a21ac15 46 wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
63cafd27 47{
e0b5519a 48 SetBackgroundColour(*wxWHITE);
63cafd27 49#ifdef __WXGTK__
2a21ac15 50 m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
63cafd27 51#else
2a21ac15 52 m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
63cafd27
JS
53#endif
54
e0b5519a 55 wxArrayString players;
2a21ac15
DS
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;
63cafd27
JS
78}
79
80ScoreCanvas::~ScoreCanvas()
81{
82}
83
84void ScoreCanvas::OnDraw(wxDC& dc)
85{
2a21ac15
DS
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 }
63cafd27 125}
e0b5519a 126#endif
63cafd27 127
e3065973
JS
128BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
129 EVT_CLOSE(ScoreDialog::OnCloseWindow)
130END_EVENT_TABLE()
63cafd27 131
e0b5519a 132ScoreDialog::ScoreDialog(wxWindow* parent, ScoreFile* file) :
2a21ac15
DS
133 wxDialog(parent, wxID_ANY, _("Scores"),
134 wxDefaultPosition, wxSize(400, 300)),
135 m_scoreFile(file)
63cafd27 136{
e0b5519a
JS
137 // create grid with players
138 wxArrayString players;
2a21ac15
DS
139 file->GetPlayerList(players);
140
e0b5519a
JS
141 wxSize sz = wxSize(400, 300);
142
143#if USE_GRID_FOR_SCORE
2a21ac15 144 wxGrid* list = new wxGrid(this, wxID_ANY, wxDefaultPosition, sz, 0);
e0b5519a 145 list->CreateGrid(players.Count(), 4);
2a21ac15
DS
146 for (unsigned int i = 0; i < players.Count(); i++)
147 {
148 int wins, games, score;
e0b5519a
JS
149 wxString string_value;
150
2a21ac15
DS
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 }
e0b5519a 157 list->SetCellValue(i,0,players[i]);
9a83f860 158 string_value.Printf( wxT("%u"), wins );
e0b5519a 159 list->SetCellValue(i,1,string_value);
9a83f860 160 string_value.Printf( wxT("%u"), games );
e0b5519a 161 list->SetCellValue(i,2,string_value);
9a83f860 162 string_value.Printf( wxT("%u"), average );
e0b5519a
JS
163 list->SetCellValue(i,3,string_value);
164 }
9a83f860
VZ
165 list->SetColLabelValue(0, wxT("Players"));
166 list->SetColLabelValue(1, wxT("Wins"));
167 list->SetColLabelValue(2, wxT("Games"));
168 list->SetColLabelValue(3, wxT("Score"));
620e83e4 169 list->EnableEditing(false);
e0b5519a
JS
170 list->AutoSizeColumns();
171 list->AutoSizeRows();
172 list->SetRowLabelSize(0);
173 list->EnableDragRowSize(false);
174 list->EnableDragColSize(false);
175 list->EnableDragGridSize(false);
b6d0f0c7
WS
176 list->ClearSelection();
177 list->EnableEditing(false);
178 sz.x = wxDefaultCoord;
e0b5519a 179#else
2a21ac15 180 ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile, wxDefaultPosition, sz);
e0b5519a
JS
181#endif
182
170acdc9 183 list->SetInitialSize(sz);
b6d0f0c7 184
e0b5519a
JS
185 // locate and resize with sizers
186 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
187 topsizer->Add( list, 1, wxALL|wxGROW, 10 );
5d2ac6b8 188 wxButton *button = new wxButton(this, wxID_OK);
b6d0f0c7
WS
189 topsizer->Add( button, 0, wxALIGN_CENTER_HORIZONTAL|wxALL , 10 );
190 button->SetFocus();
e0b5519a
JS
191
192 SetSizer( topsizer );
193
194 GetSizer()->Fit(this);
195 GetSizer()->SetSizeHints(this);
2a21ac15 196
e0b5519a 197 CentreOnParent();
63cafd27
JS
198}
199
63cafd27
JS
200void ScoreDialog::Display()
201{
b6d0f0c7 202 ShowModal();
63cafd27
JS
203}
204
babd36de 205void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
63cafd27 206{
e3065973 207 EndModal(wxID_OK);
63cafd27 208}