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