More likely to find the about.htm instructions now
[wxWidgets.git] / demos / forty / forty.cpp
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 //---------------------------------------------------------------------------
11 // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
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"
32 #include "card.h"
33 #include "scoredg.h"
34
35 #if wxUSE_HTML
36 #include "wx/textfile.h"
37 #include "wx/html/htmlwin.h"
38 #endif
39
40 BEGIN_EVENT_TABLE(FortyFrame, wxFrame)
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)
51 EVT_CLOSE(FortyFrame::OnCloseWindow)
52 END_EVENT_TABLE()
53
54 // Create a new application object
55 IMPLEMENT_APP (FortyApp)
56
57 wxColour* FortyApp::m_backgroundColour = 0;
58 wxColour* FortyApp::m_textColour = 0;
59 wxBrush* FortyApp::m_backgroundBrush = 0;
60
61 FortyApp::FortyApp()
62 {
63 }
64
65 FortyApp::~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
75 bool FortyApp::OnInit()
76 {
77 bool largecards = false;
78 m_dir = wxGetCwd();
79
80 wxSize size(668,510);
81
82 if ((argc > 1) && (!wxStrcmp(argv[1],_T("-L"))))
83 {
84 largecards = true;
85 size = wxSize(1000,750);
86 }
87
88 FortyFrame* frame = new FortyFrame(
89 0,
90 _T("Forty Thieves"),
91 wxDefaultPosition,
92 size,
93 largecards
94 );
95
96 // Show the frame
97 frame->Show(true);
98
99 frame->GetCanvas()->ShowPlayerDialog();
100
101 return true;
102 }
103
104 const wxColour& FortyApp::BackgroundColour()
105 {
106 if (!m_backgroundColour)
107 {
108 m_backgroundColour = new wxColour(0, 128, 0);
109 }
110
111 return *m_backgroundColour;
112 }
113
114 const wxBrush& FortyApp::BackgroundBrush()
115 {
116 if (!m_backgroundBrush)
117 {
118 m_backgroundBrush = new wxBrush(BackgroundColour(), wxSOLID);
119 }
120
121 return *m_backgroundBrush;
122 }
123
124 const wxColour& FortyApp::TextColour()
125 {
126 if (!m_textColour)
127 {
128 m_textColour = new wxColour(_T("BLACK"));
129 }
130
131 return *m_textColour;
132 }
133
134 // My frame constructor
135 FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size, bool largecards):
136 wxFrame(frame, wxID_ANY, title, pos, size)
137 {
138 #ifdef __WXMAC__
139 wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
140 #endif
141 // set the icon
142 #ifdef __WXMSW__
143 SetIcon(wxIcon(_T("CardsIcon")));
144 #else
145 #ifdef GTK_TBD
146 SetIcon(wxIcon(Cards_bits, Cards_width, Cards_height));
147 #endif
148 #endif
149
150 // Make a menu bar
151 wxMenu* gameMenu = new wxMenu;
152 gameMenu->Append(NEW_GAME, _T("&New"), _T("Start a new game"));
153 gameMenu->Append(SCORES, _T("&Scores..."), _T("Displays scores"));
154 gameMenu->Append(wxID_EXIT, _T("E&xit"), _T("Exits Forty Thieves"));
155
156 wxMenu* editMenu = new wxMenu;
157 editMenu->Append(UNDO, _T("&Undo"), _T("Undo the last move"));
158 editMenu->Append(REDO, _T("&Redo"), _T("Redo a move that has been undone"));
159
160 wxMenu* optionsMenu = new wxMenu;
161 optionsMenu->Append(RIGHT_BUTTON_UNDO,
162 _T("&Right button undo"),
163 _T("Enables/disables right mouse button undo and redo"),
164 true
165 );
166 optionsMenu->Append(HELPING_HAND,
167 _T("&Helping hand"),
168 _T("Enables/disables hand cursor when a card can be moved"),
169 true
170 );
171 optionsMenu->Append(LARGE_CARDS,
172 _T("&Large cards"),
173 _T("Enables/disables large cards for high resolution displays"),
174 true
175 );
176 optionsMenu->Check(HELPING_HAND, true);
177 optionsMenu->Check(RIGHT_BUTTON_UNDO, true);
178 optionsMenu->Check(LARGE_CARDS, largecards ? true : false);
179
180 wxMenu* helpMenu = new wxMenu;
181 helpMenu->Append(wxID_HELP_CONTENTS, _T("&Help Contents"), _T("Displays information about playing the game"));
182 helpMenu->Append(wxID_ABOUT, _T("&About..."), _T("About Forty Thieves"));
183
184 m_menuBar = new wxMenuBar;
185 m_menuBar->Append(gameMenu, _T("&Game"));
186 m_menuBar->Append(editMenu, _T("&Edit"));
187 m_menuBar->Append(optionsMenu, _T("&Options"));
188 m_menuBar->Append(helpMenu, _T("&Help"));
189
190 SetMenuBar(m_menuBar);
191
192 if (largecards)
193 Card::SetScale(1.3);
194
195 m_canvas = new FortyCanvas(this, wxDefaultPosition, size);
196
197 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
198 topsizer->Add( m_canvas, 1, wxEXPAND | wxALL, 0);
199 SetSizer( topsizer );
200 topsizer->SetSizeHints( this );
201
202 #if wxUSE_STATUSBAR
203 CreateStatusBar();
204 #endif // wxUSE_STATUSBAR
205 }
206
207 FortyFrame::~FortyFrame()
208 {
209 }
210
211 void FortyFrame::OnCloseWindow(wxCloseEvent& event)
212 {
213 if (m_canvas->OnCloseCanvas() )
214 {
215 this->Destroy();
216 }
217 else
218 event.Veto();
219 }
220
221 void
222 FortyFrame::NewGame(wxCommandEvent&)
223 {
224 m_canvas->NewGame();
225 }
226
227 void
228 FortyFrame::Exit(wxCommandEvent&)
229 {
230 Close(true);
231 }
232
233 void
234 FortyFrame::Help(wxCommandEvent& event)
235 {
236 #if wxUSE_HTML
237 wxString htmlFile = wxGetApp().GetDir() + wxFILE_SEP_PATH + wxT("about.htm");
238 if (wxFileExists(htmlFile))
239 {
240 FortyAboutDialog dialog(this, wxID_ANY, wxT("Forty Thieves Instructions"));
241 if (dialog.ShowModal() == wxID_OK)
242 {
243 }
244 }
245 else
246 #endif
247 {
248 About(event);
249 }
250 }
251
252 void
253 FortyFrame::About(wxCommandEvent&)
254 {
255 wxMessageBox(
256 _T("Forty Thieves\n\n")
257 _T("A free card game written with the wxWidgets toolkit\n")
258 _T("Author: Chris Breeze (c) 1992-2004\n")
259 _T("email: chris@breezesys.com"),
260 _T("About Forty Thieves"),
261 wxOK|wxICON_INFORMATION, this
262 );
263 }
264
265
266 void
267 FortyFrame::Undo(wxCommandEvent&)
268 {
269 m_canvas->Undo();
270 }
271
272 void
273 FortyFrame::Redo(wxCommandEvent&)
274 {
275 m_canvas->Redo();
276 }
277
278 void
279 FortyFrame::Scores(wxCommandEvent&)
280 {
281 m_canvas->UpdateScores();
282 ScoreDialog scores(this, m_canvas->GetScoreFile());
283 scores.Display();
284 }
285
286 void
287 FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event)
288 {
289 bool checked = m_menuBar->IsChecked(event.GetId());
290 m_canvas->EnableRightButtonUndo(checked);
291 }
292
293 void
294 FortyFrame::ToggleHelpingHand(wxCommandEvent& event)
295 {
296 bool checked = m_menuBar->IsChecked(event.GetId());
297 m_canvas->EnableHelpingHand(checked);
298 }
299
300 void
301 FortyFrame::ToggleCardSize(wxCommandEvent& event)
302 {
303 bool checked = m_menuBar->IsChecked(event.GetId());
304 Card::SetScale(checked ? 1.3 : 1);
305 m_canvas->LayoutGame();
306 m_canvas->Refresh();
307 }
308
309 //----------------------------------------------------------------------------
310 // stAboutDialog
311 //----------------------------------------------------------------------------
312
313 BEGIN_EVENT_TABLE(FortyAboutDialog,wxDialog)
314 END_EVENT_TABLE()
315
316 FortyAboutDialog::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
325 bool FortyAboutDialog::AddControls(wxWindow* parent)
326 {
327 #if wxUSE_HTML
328 wxString htmlText;
329 wxString htmlFile = wxGetApp().GetDir() + wxFILE_SEP_PATH + wxT("about.htm");
330
331 {
332 wxTextFile file(htmlFile);
333 if (file.Exists())
334 {
335 file.Open();
336 for ( htmlText = file.GetFirstLine();
337 !file.Eof();
338 htmlText << file.GetNextLine() << _T("\n") ) ;
339 }
340 }
341
342 if (htmlText.IsEmpty())
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
348 htmlText.Replace(wxT("$DATE$"), _T(__DATE__));
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);
363
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
372 wxButton *item2 = new wxButton( parent, wxID_CANCEL, _T("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
373 item2->SetDefault();
374 item2->SetFocus();
375
376 item0->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
377
378 parent->SetSizer( item0 );
379 parent->Layout();
380 item0->Fit( parent );
381 item0->SetSizeHints( parent );
382 #endif
383
384 return true;
385 }
386