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