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