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