Added new icons
[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 wxWindows 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/file.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 wxSize size(668,510);
79
80 if ((argc > 1) && (!wxStrcmp(argv[1],_T("-L"))))
81 {
82 largecards = TRUE;
83 size = wxSize(1000,750);
84 }
85
86 FortyFrame* frame = new FortyFrame(
87 0,
88 _T("Forty Thieves"),
89 -1, -1, size.x, size.y,largecards
90 );
91
92 // Show the frame
93 frame->Show(TRUE);
94
95 frame->GetCanvas()->ShowPlayerDialog();
96
97 return TRUE;
98 }
99
100 const wxColour& FortyApp::BackgroundColour()
101 {
102 if (!m_backgroundColour)
103 {
104 m_backgroundColour = new wxColour(0, 128, 0);
105 }
106
107 return *m_backgroundColour;
108 }
109
110 const wxBrush& FortyApp::BackgroundBrush()
111 {
112 if (!m_backgroundBrush)
113 {
114 m_backgroundBrush = new wxBrush(BackgroundColour(), wxSOLID);
115 }
116
117 return *m_backgroundBrush;
118 }
119
120 const wxColour& FortyApp::TextColour()
121 {
122 if (!m_textColour)
123 {
124 m_textColour = new wxColour(_T("BLACK"));
125 }
126
127 return *m_textColour;
128 }
129
130 // My frame constructor
131 FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, int x, int y, int w, int h,bool largecards):
132 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
133 {
134 #ifdef __WXMAC__
135 wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
136 #endif
137 // set the icon
138 #ifdef __WXMSW__
139 SetIcon(wxIcon(_T("CardsIcon")));
140 #else
141 #ifdef GTK_TBD
142 SetIcon(wxIcon(Cards_bits, Cards_width, Cards_height));
143 #endif
144 #endif
145
146 // Make a menu bar
147 wxMenu* gameMenu = new wxMenu;
148 gameMenu->Append(NEW_GAME, _T("&New"), _T("Start a new game"));
149 gameMenu->Append(SCORES, _T("&Scores..."), _T("Displays scores"));
150 gameMenu->Append(wxID_EXIT, _T("E&xit"), _T("Exits Forty Thieves"));
151
152 wxMenu* editMenu = new wxMenu;
153 editMenu->Append(UNDO, _T("&Undo"), _T("Undo the last move"));
154 editMenu->Append(REDO, _T("&Redo"), _T("Redo a move that has been undone"));
155
156 wxMenu* optionsMenu = new wxMenu;
157 optionsMenu->Append(RIGHT_BUTTON_UNDO,
158 _T("&Right button undo"),
159 _T("Enables/disables right mouse button undo and redo"),
160 TRUE
161 );
162 optionsMenu->Append(HELPING_HAND,
163 _T("&Helping hand"),
164 _T("Enables/disables hand cursor when a card can be moved"),
165 TRUE
166 );
167 optionsMenu->Append(LARGE_CARDS,
168 _T("&Large cards"),
169 _T("Enables/disables large cards for high resolution displays"),
170 TRUE
171 );
172 optionsMenu->Check(HELPING_HAND, TRUE);
173 optionsMenu->Check(RIGHT_BUTTON_UNDO, TRUE);
174 optionsMenu->Check(LARGE_CARDS, largecards ? TRUE : FALSE);
175
176 wxMenu* helpMenu = new wxMenu;
177 helpMenu->Append(wxID_HELP_CONTENTS, _T("&Help Contents"), _T("Displays information about playing the game"));
178 helpMenu->Append(wxID_ABOUT, _T("&About..."), _T("About Forty Thieves"));
179
180 m_menuBar = new wxMenuBar;
181 m_menuBar->Append(gameMenu, _T("&Game"));
182 m_menuBar->Append(editMenu, _T("&Edit"));
183 m_menuBar->Append(optionsMenu, _T("&Options"));
184 m_menuBar->Append(helpMenu, _T("&Help"));
185
186 SetMenuBar(m_menuBar);
187
188 if (largecards)
189 Card::SetScale(1.3);
190
191 m_canvas = new FortyCanvas(this, 0, 0, 400, 400);
192 wxLayoutConstraints* constr = new wxLayoutConstraints;
193 constr->left.SameAs(this, wxLeft);
194 constr->top.SameAs(this, wxTop);
195 constr->right.SameAs(this, wxRight);
196 constr->height.SameAs(this, wxHeight);
197 m_canvas->SetConstraints(constr);
198
199 CreateStatusBar();
200 }
201
202 FortyFrame::~FortyFrame()
203 {
204 }
205
206 void FortyFrame::OnCloseWindow(wxCloseEvent& event)
207 {
208 if (m_canvas->OnCloseCanvas() )
209 {
210 this->Destroy();
211 }
212 else
213 event.Veto();
214 }
215
216 void
217 FortyFrame::NewGame(wxCommandEvent&)
218 {
219 m_canvas->NewGame();
220 }
221
222 void
223 FortyFrame::Exit(wxCommandEvent&)
224 {
225 Close(TRUE);
226 }
227
228 void
229 FortyFrame::Help(wxCommandEvent& event)
230 {
231 #if wxUSE_HTML
232 if (wxFileExists(wxT("about.htm")))
233 {
234 FortyAboutDialog dialog(this, -1, wxT("Forty Thieves Instructions"));
235 if (dialog.ShowModal() == wxID_OK)
236 {
237 }
238 }
239 else
240 #endif
241 {
242 About(event);
243 }
244 }
245
246 void
247 FortyFrame::About(wxCommandEvent&)
248 {
249 wxMessageBox(
250 _T("Forty Thieves\n\n")
251 _T("A free card game written with the wxWidgets toolkit\n")
252 _T("Author: Chris Breeze (c) 1992-2004\n")
253 _T("email: chris@breezesys.com"),
254 _T("About Forty Thieves"),
255 wxOK|wxICON_INFORMATION, this
256 );
257 }
258
259
260 void
261 FortyFrame::Undo(wxCommandEvent&)
262 {
263 m_canvas->Undo();
264 }
265
266 void
267 FortyFrame::Redo(wxCommandEvent&)
268 {
269 m_canvas->Redo();
270 }
271
272 void
273 FortyFrame::Scores(wxCommandEvent&)
274 {
275 m_canvas->UpdateScores();
276 ScoreDialog scores(this, m_canvas->GetScoreFile());
277 scores.Display();
278 }
279
280 void
281 FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event)
282 {
283 bool checked = m_menuBar->IsChecked(event.GetId());
284 m_canvas->EnableRightButtonUndo(checked);
285 }
286
287 void
288 FortyFrame::ToggleHelpingHand(wxCommandEvent& event)
289 {
290 bool checked = m_menuBar->IsChecked(event.GetId());
291 m_canvas->EnableHelpingHand(checked);
292 }
293
294 void
295 FortyFrame::ToggleCardSize(wxCommandEvent& event)
296 {
297 bool checked = m_menuBar->IsChecked(event.GetId());
298 Card::SetScale(checked ? 1.3 : 1);
299 m_canvas->LayoutGame();
300 m_canvas->Refresh();
301 }
302
303 //----------------------------------------------------------------------------
304 // stAboutDialog
305 //----------------------------------------------------------------------------
306
307 BEGIN_EVENT_TABLE(FortyAboutDialog,wxDialog)
308 END_EVENT_TABLE()
309
310 FortyAboutDialog::FortyAboutDialog( wxWindow *parent, wxWindowID id, const wxString &title,
311 const wxPoint &position, const wxSize& size, long style ) :
312 wxDialog( parent, id, title, position, size, style )
313 {
314 AddControls(this);
315
316 Centre(wxBOTH);
317 }
318
319 bool FortyAboutDialog::AddControls(wxWindow* parent)
320 {
321 #if wxUSE_HTML
322 wxString htmlText;
323 wxString htmlFile(wxT("about.htm"));
324
325 //if (!wxGetApp().GetMemoryTextResource(wxT("about.htm"), htmlText))
326 {
327 // wxSetWorkingDirectory(wxGetApp().GetAppDir());
328 // wxString htmlFile(wxGetApp().GetFullAppPath(wxT("about.htm")));
329
330 if (wxFileExists(htmlFile))
331 {
332 wxFile file;
333 file.Open(htmlFile, wxFile::read);
334 long len = file.Length();
335 wxChar* buf = htmlText.GetWriteBuf(len + 1);
336 file.Read(buf, len);
337 buf[len] = 0;
338 htmlText.UngetWriteBuf();
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 #if 0
349 wxString verString;
350 verString.Printf("%.2f", stVERSION_NUMBER);
351 htmlText.Replace(wxT("$VERSION$"), verString);
352 #endif
353 htmlText.Replace(wxT("$DATE$"), _T(__DATE__));
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
377 wxButton *item2 = new wxButton( parent, wxID_CANCEL, _T("&Close"), wxDefaultPosition, wxDefaultSize, 0 );
378 item2->SetDefault();
379 item2->SetFocus();
380
381 item0->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
382
383 parent->SetAutoLayout( TRUE );
384 parent->SetSizer( item0 );
385 parent->Layout();
386 item0->Fit( parent );
387 item0->SetSizeHints( parent );
388 #endif
389
390 return TRUE;
391 }
392