Enhanced stock labels usage. Source cleaning.
[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 #include "wx/stockitem.h"
41
42 BEGIN_EVENT_TABLE(FortyFrame, wxFrame)
43 EVT_MENU(wxID_NEW, FortyFrame::NewGame)
44 EVT_MENU(wxID_EXIT, FortyFrame::Exit)
45 EVT_MENU(wxID_ABOUT, FortyFrame::About)
46 EVT_MENU(wxID_HELP_CONTENTS, FortyFrame::Help)
47 EVT_MENU(wxID_UNDO, FortyFrame::Undo)
48 EVT_MENU(wxID_REDO, FortyFrame::Redo)
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)
53 EVT_CLOSE(FortyFrame::OnCloseWindow)
54 END_EVENT_TABLE()
55
56 // Create a new application object
57 IMPLEMENT_APP (FortyApp)
58
59 wxColour* FortyApp::m_backgroundColour = 0;
60 wxColour* FortyApp::m_textColour = 0;
61 wxBrush* FortyApp::m_backgroundBrush = 0;
62
63 FortyApp::~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
73 bool FortyApp::OnInit()
74 {
75 bool largecards = false;
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 }
83
84 wxSize size(668,510);
85
86 if ((argc > 1) && (!wxStrcmp(argv[1],_T("-L"))))
87 {
88 largecards = true;
89 size = wxSize(1000,750);
90 }
91
92 FortyFrame* frame = new FortyFrame(
93 0,
94 _T("Forty Thieves"),
95 wxDefaultPosition,
96 size,
97 largecards
98 );
99
100 // Show the frame
101 frame->Show(true);
102
103 frame->GetCanvas()->ShowPlayerDialog();
104
105 return true;
106 }
107
108 const wxColour& FortyApp::BackgroundColour()
109 {
110 if (!m_backgroundColour)
111 {
112 m_backgroundColour = new wxColour(0, 128, 0);
113 }
114
115 return *m_backgroundColour;
116 }
117
118 const wxBrush& FortyApp::BackgroundBrush()
119 {
120 if (!m_backgroundBrush)
121 {
122 m_backgroundBrush = new wxBrush(BackgroundColour(), wxSOLID);
123 }
124
125 return *m_backgroundBrush;
126 }
127
128 const wxColour& FortyApp::TextColour()
129 {
130 if (!m_textColour)
131 {
132 m_textColour = new wxColour(_T("BLACK"));
133 }
134
135 return *m_textColour;
136 }
137
138 // My frame constructor
139 FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size, bool largecards):
140 wxFrame(frame, wxID_ANY, title, pos, size)
141 {
142 #ifdef __WXMAC__
143 wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
144 #endif
145 // set the icon
146 #ifdef __WXMSW__
147 SetIcon(wxIcon(_T("CardsIcon")));
148 #else
149 #ifdef GTK_TBD
150 SetIcon(wxIcon(Cards_bits, Cards_width, Cards_height));
151 #endif
152 #endif
153
154 // Make a menu bar
155 wxMenu* gameMenu = new wxMenu;
156 gameMenu->Append(wxID_NEW, wxGetStockLabel(wxID_NEW), _T("Start a new game"));
157 gameMenu->Append(SCORES, _T("&Scores..."), _T("Displays scores"));
158 gameMenu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), _T("Exits Forty Thieves"));
159
160 wxMenu* editMenu = new wxMenu;
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"));
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
206 #if wxUSE_STATUSBAR
207 CreateStatusBar();
208 #endif // wxUSE_STATUSBAR
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 if (wxFileExists(wxGetApp().GetHelpFile()))
238 {
239 FortyAboutDialog dialog(this, wxID_ANY, wxT("Forty Thieves Instructions"));
240 if (dialog.ShowModal() == wxID_OK)
241 {
242 }
243 }
244 else
245 #endif
246 {
247 About(event);
248 }
249 }
250
251 void
252 FortyFrame::About(wxCommandEvent&)
253 {
254 wxMessageBox(
255 _T("Forty Thieves\n\n")
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"),
259 _T("About Forty Thieves"),
260 wxOK|wxICON_INFORMATION, this
261 );
262 }
263
264
265 void
266 FortyFrame::Undo(wxCommandEvent&)
267 {
268 m_canvas->Undo();
269 }
270
271 void
272 FortyFrame::Redo(wxCommandEvent&)
273 {
274 m_canvas->Redo();
275 }
276
277 void
278 FortyFrame::Scores(wxCommandEvent&)
279 {
280 m_canvas->UpdateScores();
281 ScoreDialog scores(this, m_canvas->GetScoreFile());
282 scores.Display();
283 }
284
285 void
286 FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event)
287 {
288 bool checked = m_menuBar->IsChecked(event.GetId());
289 m_canvas->EnableRightButtonUndo(checked);
290 }
291
292 void
293 FortyFrame::ToggleHelpingHand(wxCommandEvent& event)
294 {
295 bool checked = m_menuBar->IsChecked(event.GetId());
296 m_canvas->EnableHelpingHand(checked);
297 }
298
299 void
300 FortyFrame::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
308 //----------------------------------------------------------------------------
309 // stAboutDialog
310 //----------------------------------------------------------------------------
311
312 BEGIN_EVENT_TABLE(FortyAboutDialog,wxDialog)
313 EVT_BUTTON(wxID_CLOSE, wxDialog::OnOK)
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().GetHelpFile();
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.empty())
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_CLOSE );
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