]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/forty.cpp
Ensure the correct cleanup order for GDI+-related modules in wxMSW.
[wxWidgets.git] / demos / forty / forty.cpp
CommitLineData
63cafd27
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: forty.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
010216e3 9// Licence: wxWindows licence
63cafd27 10//---------------------------------------------------------------------------
be5a51fb 11// Last modified: 22nd July 1998 - ported to wxWidgets 2.0
63cafd27
JS
12/////////////////////////////////////////////////////////////////////////////
13
63cafd27
JS
14// For compilers that support precompilation, includes "wx/wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18#pragma hdrstop
19#endif
20
21#ifndef WX_PRECOMP
22#include "wx/wx.h"
23#endif
24
25#include "canvas.h"
26#include "forty.h"
fc799548 27#include "card.h"
63cafd27 28#include "scoredg.h"
68ca12fe 29#include "forty.xpm"
63cafd27 30
5cdeff75 31#if wxUSE_HTML
886c2532 32#include "wx/textfile.h"
5cdeff75
JS
33#include "wx/html/htmlwin.h"
34#endif
35
5d2ac6b8
WS
36#include "wx/stockitem.h"
37
63cafd27 38BEGIN_EVENT_TABLE(FortyFrame, wxFrame)
5d2ac6b8 39 EVT_MENU(wxID_NEW, FortyFrame::NewGame)
010216e3
WS
40 EVT_MENU(wxID_EXIT, FortyFrame::Exit)
41 EVT_MENU(wxID_ABOUT, FortyFrame::About)
42 EVT_MENU(wxID_HELP_CONTENTS, FortyFrame::Help)
5d2ac6b8
WS
43 EVT_MENU(wxID_UNDO, FortyFrame::Undo)
44 EVT_MENU(wxID_REDO, FortyFrame::Redo)
010216e3
WS
45 EVT_MENU(SCORES, FortyFrame::Scores)
46 EVT_MENU(RIGHT_BUTTON_UNDO, FortyFrame::ToggleRightButtonUndo)
47 EVT_MENU(HELPING_HAND, FortyFrame::ToggleHelpingHand)
48 EVT_MENU(LARGE_CARDS, FortyFrame::ToggleCardSize)
e3065973 49 EVT_CLOSE(FortyFrame::OnCloseWindow)
63cafd27
JS
50END_EVENT_TABLE()
51
52// Create a new application object
010216e3 53IMPLEMENT_APP (FortyApp)
63cafd27
JS
54
55wxColour* FortyApp::m_backgroundColour = 0;
56wxColour* FortyApp::m_textColour = 0;
57wxBrush* FortyApp::m_backgroundBrush = 0;
58
15bee36f
JS
59FortyApp::~FortyApp()
60{
61 delete m_backgroundColour;
62 delete m_textColour;
63 delete m_backgroundBrush;
64 delete Card::m_symbolBmap;
65 delete Card::m_pictureBmap;
66
67}
68
63cafd27
JS
69bool FortyApp::OnInit()
70{
010216e3 71 bool largecards = false;
236d7be3
JS
72#ifndef __WXWINCE__
73 m_helpFile = wxGetCwd() + wxFILE_SEP_PATH + wxT("about.htm");
74 if (!wxFileExists(m_helpFile))
75#endif
76 {
77 m_helpFile = wxPathOnly(argv[0]) + wxFILE_SEP_PATH + wxT("about.htm");
78 }
e0b5519a 79
010216e3 80 wxSize size(668,510);
fc799548 81
9a83f860 82 if ((argc > 1) && (!wxStrcmp(argv[1],wxT("-L"))))
010216e3
WS
83 {
84 largecards = true;
85 size = wxSize(1000,750);
86 }
fc799548 87
010216e3
WS
88 FortyFrame* frame = new FortyFrame(
89 0,
9a83f860 90 wxT("Forty Thieves"),
010216e3
WS
91 wxDefaultPosition,
92 size,
93 largecards
94 );
63cafd27 95
010216e3
WS
96 // Show the frame
97 frame->Show(true);
63cafd27 98
010216e3 99 frame->GetCanvas()->ShowPlayerDialog();
868741e9 100
010216e3 101 return true;
63cafd27
JS
102}
103
79490c3d 104const wxColour& FortyApp::BackgroundColour()
63cafd27 105{
010216e3
WS
106 if (!m_backgroundColour)
107 {
108 m_backgroundColour = new wxColour(0, 128, 0);
109 }
79490c3d 110
010216e3 111 return *m_backgroundColour;
63cafd27
JS
112}
113
79490c3d 114const wxBrush& FortyApp::BackgroundBrush()
63cafd27 115{
010216e3
WS
116 if (!m_backgroundBrush)
117 {
118 m_backgroundBrush = new wxBrush(BackgroundColour(), wxSOLID);
119 }
79490c3d 120
010216e3 121 return *m_backgroundBrush;
63cafd27
JS
122}
123
79490c3d 124const wxColour& FortyApp::TextColour()
63cafd27 125{
010216e3
WS
126 if (!m_textColour)
127 {
19bc1514 128 m_textColour = new wxColour(*wxBLACK);
010216e3 129 }
79490c3d 130
010216e3 131 return *m_textColour;
63cafd27
JS
132}
133
134// My frame constructor
e0b5519a 135FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size, bool largecards):
010216e3 136 wxFrame(frame, wxID_ANY, title, pos, size)
63cafd27 137{
8fbb465f 138#ifdef __WXMAC__
010216e3 139 wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
8fbb465f 140#endif
010216e3 141 // set the icon
63cafd27 142#ifdef __WXMSW__
9a83f860 143 SetIcon(wxIcon(wxT("CardsIcon")));
63cafd27 144#else
68ca12fe 145 SetIcon(wxIcon(forty_xpm));
63cafd27
JS
146#endif
147
010216e3
WS
148 // Make a menu bar
149 wxMenu* gameMenu = new wxMenu;
9a83f860
VZ
150 gameMenu->Append(wxID_NEW, wxGetStockLabel(wxID_NEW), wxT("Start a new game"));
151 gameMenu->Append(SCORES, wxT("&Scores..."), wxT("Displays scores"));
152 gameMenu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Exits Forty Thieves"));
010216e3
WS
153
154 wxMenu* editMenu = new wxMenu;
9a83f860
VZ
155 editMenu->Append(wxID_UNDO, wxGetStockLabel(wxID_UNDO), wxT("Undo the last move"));
156 editMenu->Append(wxID_REDO, wxGetStockLabel(wxID_REDO), wxT("Redo a move that has been undone"));
010216e3
WS
157
158 wxMenu* optionsMenu = new wxMenu;
159 optionsMenu->Append(RIGHT_BUTTON_UNDO,
9a83f860
VZ
160 wxT("&Right button undo"),
161 wxT("Enables/disables right mouse button undo and redo"),
010216e3
WS
162 true
163 );
164 optionsMenu->Append(HELPING_HAND,
9a83f860
VZ
165 wxT("&Helping hand"),
166 wxT("Enables/disables hand cursor when a card can be moved"),
010216e3
WS
167 true
168 );
169 optionsMenu->Append(LARGE_CARDS,
9a83f860
VZ
170 wxT("&Large cards"),
171 wxT("Enables/disables large cards for high resolution displays"),
010216e3
WS
172 true
173 );
174 optionsMenu->Check(HELPING_HAND, true);
175 optionsMenu->Check(RIGHT_BUTTON_UNDO, true);
176 optionsMenu->Check(LARGE_CARDS, largecards ? true : false);
177
178 wxMenu* helpMenu = new wxMenu;
9a83f860 179 helpMenu->Append(wxID_HELP_CONTENTS, wxT("&Help Contents"), wxT("Displays information about playing the game"));
2d143b66 180 helpMenu->Append(wxID_ABOUT, wxT("&About"), wxT("About Forty Thieves"));
010216e3
WS
181
182 m_menuBar = new wxMenuBar;
9a83f860
VZ
183 m_menuBar->Append(gameMenu, wxT("&Game"));
184 m_menuBar->Append(editMenu, wxT("&Edit"));
185 m_menuBar->Append(optionsMenu, wxT("&Options"));
186 m_menuBar->Append(helpMenu, wxT("&Help"));
010216e3
WS
187
188 SetMenuBar(m_menuBar);
189
190 if (largecards)
191 Card::SetScale(1.3);
192
193 m_canvas = new FortyCanvas(this, wxDefaultPosition, size);
194
195 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
196 topsizer->Add( m_canvas, 1, wxEXPAND | wxALL, 0);
197 SetSizer( topsizer );
010216e3 198
67a99992 199#if wxUSE_STATUSBAR
010216e3 200 CreateStatusBar();
67a99992 201#endif // wxUSE_STATUSBAR
dd9f8b6b
JS
202
203 topsizer->SetSizeHints( this );
63cafd27
JS
204}
205
e3065973 206void FortyFrame::OnCloseWindow(wxCloseEvent& event)
63cafd27 207{
cba2db0c 208 if (m_canvas->OnCloseCanvas() )
e3065973
JS
209 {
210 this->Destroy();
211 }
212 else
213 event.Veto();
63cafd27
JS
214}
215
216void
217FortyFrame::NewGame(wxCommandEvent&)
218{
010216e3 219 m_canvas->NewGame();
63cafd27
JS
220}
221
222void
223FortyFrame::Exit(wxCommandEvent&)
224{
010216e3 225 Close(true);
63cafd27
JS
226}
227
228void
0aff9d91 229FortyFrame::Help(wxCommandEvent& event)
63cafd27 230{
5cdeff75 231#if wxUSE_HTML
236d7be3 232 if (wxFileExists(wxGetApp().GetHelpFile()))
5cdeff75 233 {
e0b5519a 234 FortyAboutDialog dialog(this, wxID_ANY, wxT("Forty Thieves Instructions"));
5cdeff75
JS
235 if (dialog.ShowModal() == wxID_OK)
236 {
237 }
238 }
239 else
240#endif
241 {
0aff9d91
JS
242 About(event);
243 }
244}
245
246void
247FortyFrame::About(wxCommandEvent&)
248{
5cdeff75 249 wxMessageBox(
9a83f860
VZ
250 wxT("Forty Thieves\n\n")
251 wxT("A free card game written with the wxWidgets toolkit\n")
252 wxT("Author: Chris Breeze (c) 1992-2004\n")
253 wxT("email: chris@breezesys.com"),
254 wxT("About Forty Thieves"),
0aff9d91 255 wxOK|wxICON_INFORMATION, this
5cdeff75 256 );
63cafd27
JS
257}
258
0aff9d91 259
63cafd27
JS
260void
261FortyFrame::Undo(wxCommandEvent&)
262{
010216e3 263 m_canvas->Undo();
63cafd27
JS
264}
265
266void
267FortyFrame::Redo(wxCommandEvent&)
268{
010216e3 269 m_canvas->Redo();
63cafd27
JS
270}
271
272void
273FortyFrame::Scores(wxCommandEvent&)
274{
010216e3
WS
275 m_canvas->UpdateScores();
276 ScoreDialog scores(this, m_canvas->GetScoreFile());
277 scores.Display();
63cafd27
JS
278}
279
280void
281FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event)
282{
010216e3
WS
283 bool checked = m_menuBar->IsChecked(event.GetId());
284 m_canvas->EnableRightButtonUndo(checked);
63cafd27
JS
285}
286
287void
288FortyFrame::ToggleHelpingHand(wxCommandEvent& event)
289{
010216e3
WS
290 bool checked = m_menuBar->IsChecked(event.GetId());
291 m_canvas->EnableHelpingHand(checked);
63cafd27 292}
fc799548
JS
293
294void
295FortyFrame::ToggleCardSize(wxCommandEvent& event)
296{
297 bool checked = m_menuBar->IsChecked(event.GetId());
298 Card::SetScale(checked ? 1.3 : 1);
299 m_canvas->LayoutGame();
300 m_canvas->Refresh();
301}
302
5cdeff75
JS
303//----------------------------------------------------------------------------
304// stAboutDialog
305//----------------------------------------------------------------------------
306
5cdeff75
JS
307FortyAboutDialog::FortyAboutDialog( wxWindow *parent, wxWindowID id, const wxString &title,
308 const wxPoint &position, const wxSize& size, long style ) :
309 wxDialog( parent, id, title, position, size, style )
310{
311 AddControls(this);
312
313 Centre(wxBOTH);
314}
315
316bool FortyAboutDialog::AddControls(wxWindow* parent)
317{
318#if wxUSE_HTML
319 wxString htmlText;
236d7be3 320 wxString htmlFile = wxGetApp().GetHelpFile();
5cdeff75 321
5cdeff75 322 {
886c2532
WS
323 wxTextFile file(htmlFile);
324 if (file.Exists())
5cdeff75 325 {
886c2532 326 file.Open();
254a2129
WS
327 for ( htmlText = file.GetFirstLine();
328 !file.Eof();
9a83f860 329 htmlText << file.GetNextLine() << wxT("\n") ) ;
5cdeff75
JS
330 }
331 }
332
5d2ac6b8 333 if (htmlText.empty())
5cdeff75
JS
334 {
335 htmlText.Printf(wxT("<html><head><title>Warning</title></head><body><P>Sorry, could not find resource for About dialog<P></body></html>"));
336 }
337
338 // Customize the HTML
9a83f860 339 htmlText.Replace(wxT("$DATE$"), wxT(__DATE__));
5cdeff75
JS
340
341 wxSize htmlSize(400, 290);
342
343 // Note: in later versions of wxWin this will be fixed so wxRAISED_BORDER
344 // does the right thing. Meanwhile, this is a workaround.
345#ifdef __WXMSW__
346 long borderStyle = wxDOUBLE_BORDER;
347#else
348 long borderStyle = wxRAISED_BORDER;
349#endif
350
351 wxHtmlWindow* html = new wxHtmlWindow(this, ID_ABOUT_HTML_WINDOW, wxDefaultPosition, htmlSize, borderStyle);
352 html -> SetBorders(10);
353 html -> SetPage(htmlText);
254a2129 354
5cdeff75
JS
355 //// Start of sizer-based control creation
356
357 wxSizer *item0 = new wxBoxSizer( wxVERTICAL );
358
359 wxWindow *item1 = parent->FindWindow( ID_ABOUT_HTML_WINDOW );
360 wxASSERT( item1 );
361 item0->Add( item1, 0, wxALIGN_CENTRE|wxALL, 5 );
362
5d2ac6b8 363 wxButton *item2 = new wxButton( parent, wxID_CLOSE );
5cdeff75
JS
364 item2->SetDefault();
365 item2->SetFocus();
8394f0a0 366 SetAffirmativeId(wxID_CLOSE);
5cdeff75
JS
367
368 item0->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
369
5cdeff75
JS
370 parent->SetSizer( item0 );
371 parent->Layout();
372 item0->Fit( parent );
373 item0->SetSizeHints( parent );
374#endif
375
e0b5519a 376 return true;
5cdeff75 377}