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