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