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