]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/scoredg.cpp
Applied patch [ 919026 ] wxBU_EXACTFIT support in wxBitmapButtonXmlHandler
[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
9// Licence: wxWindows licence
10//---------------------------------------------------------------------------
11// Last modified: 22nd July 1998 - ported to wxWindows 2.0
12/////////////////////////////////////////////////////////////////////////////
13
ab7ce33c 14#if defined(__GNUG__) && !defined(__APPLE__)
63cafd27
JS
15#pragma implementation
16#pragma interface
17#endif
18
19// For compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23#pragma hdrstop
24#endif
25
26#ifndef WX_PRECOMP
27#include "wx/wx.h"
28#endif
29
63cafd27
JS
30#include "scorefil.h"
31#include "scoredg.h"
32
33class ScoreCanvas : public wxScrolledWindow
34{
35public:
36 ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile);
37 virtual ~ScoreCanvas();
38
39 void OnDraw(wxDC& dc);
40
41private:
42 wxFont* m_font;
43 wxString m_text;
44};
45
46
47ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile) :
48 wxScrolledWindow(parent)
49{
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
54ff4a70
RR
56 wxArrayString players;
57 scoreFile->GetPlayerList( players);
63cafd27 58
f37c24e0 59 wxString os;
63cafd27 60
0447ac8e 61 os << wxT("Player\tWins\tGames\tScore\n");
cb43b372 62 for (unsigned int i = 0; i < players.Count(); i++)
63cafd27
JS
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
0447ac8e
JS
72 os << players[i] << wxT('\t')
73 << wins << wxT('\t')
74 << games << wxT('\t')
75 << average << wxT('\n');
63cafd27 76 }
0447ac8e 77 os << wxT('\0');
f37c24e0 78 m_text = os;
63cafd27
JS
79}
80
81ScoreCanvas::~ScoreCanvas()
82{
83}
84
85void ScoreCanvas::OnDraw(wxDC& dc)
86{
16553659 87 dc.SetFont(* m_font);
63cafd27 88
1babe8fd 89 const wxChar* str = m_text;
63cafd27
JS
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;
1babe8fd 97 dc.GetTextExtent(wxT("Testing"), &w, &h);
63cafd27
JS
98 lineSpacing = (int)h;
99 }
100
101 int y = 0;
102 while (*str)
103 {
1babe8fd
JS
104 wxChar text[256];
105 wxChar* dest = text;
63cafd27 106
0447ac8e
JS
107 while (*str && *str >= ' ') *dest++ = *str++;
108 *dest = '\0';
63cafd27
JS
109
110 dc.DrawText(text, tabstops[tab], y);
111
0447ac8e 112 if (*str == '\t')
63cafd27
JS
113 {
114 if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1)
115 {
116 tab++;
117 }
118 }
0447ac8e 119 else if (*str == '\n')
63cafd27
JS
120 {
121 tab = 0;
122 y += lineSpacing;
123 }
124 if (*str) str++;
125 }
126}
127
e3065973
JS
128BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
129 EVT_CLOSE(ScoreDialog::OnCloseWindow)
130END_EVENT_TABLE()
63cafd27
JS
131
132ScoreDialog::ScoreDialog(
133 wxWindow* parent,
134 ScoreFile* file
135 ) :
1babe8fd 136 wxDialog(parent, -1, _("Scores"),
63cafd27
JS
137 wxDefaultPosition, wxSize(310, 200),
138 wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
139 m_scoreFile(file)
140{
141 // enable constraints
142 SetAutoLayout (TRUE);
143
144 ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile);
1babe8fd 145 m_OK = new wxButton(this, wxID_OK, _("OK"));
63cafd27
JS
146
147 wxLayoutConstraints* layout;
148
149 // Constrain the OK button
150 layout = new wxLayoutConstraints;
151 layout->left.SameAs (this, wxLeft, 10);
152 layout->bottom.SameAs (this, wxBottom, 10);
153 layout->height.AsIs();
154 layout->width.AsIs();
155 m_OK->SetConstraints(layout);
156
157 // Constrain the list of players
158 layout = new wxLayoutConstraints;
159 layout->left.SameAs (this, wxLeft, 10);
160 layout->right.SameAs (this, wxRight, 10);
161 layout->top.SameAs (this, wxTop, 10);
162 layout->bottom.SameAs (m_OK, wxTop, 10);
163 list->SetConstraints(layout);
164
165 Layout();
166}
167
168ScoreDialog::~ScoreDialog()
169{
170}
171
172void ScoreDialog::Display()
173{
174 Show(TRUE);
175}
176
babd36de 177void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
63cafd27 178{
e3065973 179 EndModal(wxID_OK);
63cafd27 180}