~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/////////////////////////////////////////////////////////////////////////////
// Name:        scoredg.cpp
// Purpose:     Forty Thieves patience game
// Author:      Chris Breeze
// Modified by:
// Created:     21/07/97
// RCS-ID:      $Id: scoredg.cpp 70099 2011-12-23 17:38:30Z PC $
// Copyright:   (c) 1993-1998 Chris Breeze
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

#include "scorefil.h"
#include "scoredg.h"

// adjust USE_GRID_FOR_SCORE with O or 1 to your preferences
// by default it takes wxGrid component for score display if available in target port
#define USE_GRID_FOR_SCORE     wxUSE_GRID

#if USE_GRID_FOR_SCORE
#include "wx/grid.h"
#else
class ScoreCanvas : public wxScrolledWindow
{
public:
    ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size);
    virtual ~ScoreCanvas();

    void OnDraw(wxDC& dc);

private:
    wxFont     *m_font;
    wxString    m_text;
};

ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size) :
    wxScrolledWindow(parent, wxID_ANY, pos, size, wxSUNKEN_BORDER)
{
    SetBackgroundColour(*wxWHITE);
#ifdef __WXGTK__
    m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
#else
    m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
#endif

    wxArrayString players;
    scoreFile->GetPlayerList( players);

    wxString os;

    os << wxT("Player\tWins\tGames\tScore\n");
    for (unsigned int i = 0; i < players.Count(); i++)
    {
        int wins, games, score;
        scoreFile->ReadPlayersScore(players[i], wins, games, score);
        int average = 0;
        if (games > 0)
        {
            average = (2 * score + games) / (2 * games);
        }

        os << players[i] << wxT('\t')
           << wins  << wxT('\t')
           << games << wxT('\t')
           << average << wxT('\n');
    }
    os << wxT('\0');
    m_text = os;
}

ScoreCanvas::~ScoreCanvas()
{
}

void ScoreCanvas::OnDraw(wxDC& dc)
{
    dc.SetFont(* m_font);

    const wxChar* str = m_text;
    unsigned int tab = 0;
    unsigned int tabstops[] = { 5, 100, 150, 200 };

    // get the line spacing for the current font
    int lineSpacing;
    {
        long w, h;
        dc.GetTextExtent(wxT("Testing"), &w, &h);
        lineSpacing = (int)h;
    }

    int y = 0;
    while (*str)
    {
        wxChar text[256];
        wxChar* dest = text;

        while (*str && *str >= ' ') *dest++ = *str++;
        *dest = '\0';

        dc.DrawText(text, tabstops[tab], y);

        if (*str == '\t')
        {
            if (tab < sizeof(tabstops) / sizeof(tabstops[0]) - 1)
            {
                tab++;
            }
        }
        else if (*str == '\n')
        {
            tab = 0;
            y += lineSpacing;
        }
        if (*str) str++;
    }
}
#endif

BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
    EVT_CLOSE(ScoreDialog::OnCloseWindow)
END_EVENT_TABLE()

ScoreDialog::ScoreDialog(wxWindow* parent, ScoreFile* file) :
    wxDialog(parent, wxID_ANY, _("Scores"),
            wxDefaultPosition, wxSize(400, 300)),
    m_scoreFile(file)
{
    // create grid with players
    wxArrayString players;
    file->GetPlayerList(players);

    wxSize sz = wxSize(400, 300);

#if USE_GRID_FOR_SCORE
    wxGrid* list = new wxGrid(this, wxID_ANY, wxDefaultPosition, sz, 0);
    list->CreateGrid(players.Count(), 4);
    for (unsigned int i = 0; i < players.Count(); i++)
    {
        int wins, games, score;
        wxString string_value;

        file->ReadPlayersScore(players[i], wins, games, score);
        int average = 0;
        if (games > 0)
        {
            average = (2 * score + games) / (2 * games);
        }
        list->SetCellValue(i,0,players[i]);
        string_value.Printf( wxT("%u"), wins );
        list->SetCellValue(i,1,string_value);
        string_value.Printf( wxT("%u"), games );
        list->SetCellValue(i,2,string_value);
        string_value.Printf( wxT("%u"), average );
        list->SetCellValue(i,3,string_value);
    }
    list->SetColLabelValue(0, wxT("Players"));
    list->SetColLabelValue(1, wxT("Wins"));
    list->SetColLabelValue(2, wxT("Games"));
    list->SetColLabelValue(3, wxT("Score"));
    list->EnableEditing(false);
    list->AutoSizeColumns();
    list->AutoSizeRows();
    list->SetRowLabelSize(0);
    list->EnableDragRowSize(false);
    list->EnableDragColSize(false);
    list->EnableDragGridSize(false);
    list->ClearSelection();
    list->EnableEditing(false);
    sz.x = wxDefaultCoord;
#else
    ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile, wxDefaultPosition, sz);
#endif

    list->SetInitialSize(sz);

    // locate and resize with sizers
    wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
    topsizer->Add( list, 1, wxALL|wxGROW, 10 );
    wxButton *button = new wxButton(this, wxID_OK);
    topsizer->Add( button, 0, wxALIGN_CENTER_HORIZONTAL|wxALL , 10 );
    button->SetFocus();

    SetSizer( topsizer );

    GetSizer()->Fit(this);
    GetSizer()->SetSizeHints(this);

    CentreOnParent();
}

void ScoreDialog::Display()
{
    ShowModal();
}

void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
{
    EndModal(wxID_OK);
}