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