]>
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 | ||
63cafd27 JS |
14 | // For compilers that support precompilation, includes "wx/wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/wx.h" | |
23 | #endif | |
24 | ||
25 | #include "canvas.h" | |
26 | #include "forty.h" | |
fc799548 | 27 | #include "card.h" |
63cafd27 | 28 | #include "scoredg.h" |
63cafd27 | 29 | |
5cdeff75 | 30 | #if wxUSE_HTML |
886c2532 | 31 | #include "wx/textfile.h" |
5cdeff75 JS |
32 | #include "wx/html/htmlwin.h" |
33 | #endif | |
34 | ||
5d2ac6b8 WS |
35 | #include "wx/stockitem.h" |
36 | ||
63cafd27 | 37 | BEGIN_EVENT_TABLE(FortyFrame, wxFrame) |
5d2ac6b8 | 38 | EVT_MENU(wxID_NEW, FortyFrame::NewGame) |
010216e3 WS |
39 | EVT_MENU(wxID_EXIT, FortyFrame::Exit) |
40 | EVT_MENU(wxID_ABOUT, FortyFrame::About) | |
41 | EVT_MENU(wxID_HELP_CONTENTS, FortyFrame::Help) | |
5d2ac6b8 WS |
42 | EVT_MENU(wxID_UNDO, FortyFrame::Undo) |
43 | EVT_MENU(wxID_REDO, FortyFrame::Redo) | |
010216e3 WS |
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) | |
e3065973 | 48 | EVT_CLOSE(FortyFrame::OnCloseWindow) |
63cafd27 JS |
49 | END_EVENT_TABLE() |
50 | ||
51 | // Create a new application object | |
010216e3 | 52 | IMPLEMENT_APP (FortyApp) |
63cafd27 JS |
53 | |
54 | wxColour* FortyApp::m_backgroundColour = 0; | |
55 | wxColour* FortyApp::m_textColour = 0; | |
56 | wxBrush* FortyApp::m_backgroundBrush = 0; | |
57 | ||
15bee36f JS |
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 | ||
63cafd27 JS |
68 | bool FortyApp::OnInit() |
69 | { | |
010216e3 | 70 | bool largecards = false; |
236d7be3 JS |
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 | } | |
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; | |
5d2ac6b8 | 151 | gameMenu->Append(wxID_NEW, wxGetStockLabel(wxID_NEW), _T("Start a new game")); |
010216e3 | 152 | gameMenu->Append(SCORES, _T("&Scores..."), _T("Displays scores")); |
5d2ac6b8 | 153 | gameMenu->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), _T("Exits Forty Thieves")); |
010216e3 WS |
154 | |
155 | wxMenu* editMenu = new wxMenu; | |
5d2ac6b8 WS |
156 | editMenu->Append(wxID_UNDO, wxGetStockLabel(wxID_UNDO), _T("Undo the last move")); |
157 | editMenu->Append(wxID_REDO, wxGetStockLabel(wxID_REDO), _T("Redo a move that has been undone")); | |
010216e3 WS |
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 | ||
e3065973 | 206 | void FortyFrame::OnCloseWindow(wxCloseEvent& event) |
63cafd27 | 207 | { |
cba2db0c | 208 | if (m_canvas->OnCloseCanvas() ) |
e3065973 JS |
209 | { |
210 | this->Destroy(); | |
211 | } | |
212 | else | |
213 | event.Veto(); | |
63cafd27 JS |
214 | } |
215 | ||
216 | void | |
217 | FortyFrame::NewGame(wxCommandEvent&) | |
218 | { | |
010216e3 | 219 | m_canvas->NewGame(); |
63cafd27 JS |
220 | } |
221 | ||
222 | void | |
223 | FortyFrame::Exit(wxCommandEvent&) | |
224 | { | |
010216e3 | 225 | Close(true); |
63cafd27 JS |
226 | } |
227 | ||
228 | void | |
0aff9d91 | 229 | FortyFrame::Help(wxCommandEvent& event) |
63cafd27 | 230 | { |
5cdeff75 | 231 | #if wxUSE_HTML |
236d7be3 | 232 | if (wxFileExists(wxGetApp().GetHelpFile())) |
5cdeff75 | 233 | { |
e0b5519a | 234 | FortyAboutDialog dialog(this, wxID_ANY, wxT("Forty Thieves Instructions")); |
5cdeff75 JS |
235 | if (dialog.ShowModal() == wxID_OK) |
236 | { | |
237 | } | |
238 | } | |
239 | else | |
240 | #endif | |
241 | { | |
0aff9d91 JS |
242 | About(event); |
243 | } | |
244 | } | |
245 | ||
246 | void | |
247 | FortyFrame::About(wxCommandEvent&) | |
248 | { | |
5cdeff75 | 249 | wxMessageBox( |
f37c24e0 | 250 | _T("Forty Thieves\n\n") |
0aff9d91 JS |
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"), | |
f37c24e0 | 254 | _T("About Forty Thieves"), |
0aff9d91 | 255 | wxOK|wxICON_INFORMATION, this |
5cdeff75 | 256 | ); |
63cafd27 JS |
257 | } |
258 | ||
0aff9d91 | 259 | |
63cafd27 JS |
260 | void |
261 | FortyFrame::Undo(wxCommandEvent&) | |
262 | { | |
010216e3 | 263 | m_canvas->Undo(); |
63cafd27 JS |
264 | } |
265 | ||
266 | void | |
267 | FortyFrame::Redo(wxCommandEvent&) | |
268 | { | |
010216e3 | 269 | m_canvas->Redo(); |
63cafd27 JS |
270 | } |
271 | ||
272 | void | |
273 | FortyFrame::Scores(wxCommandEvent&) | |
274 | { | |
010216e3 WS |
275 | m_canvas->UpdateScores(); |
276 | ScoreDialog scores(this, m_canvas->GetScoreFile()); | |
277 | scores.Display(); | |
63cafd27 JS |
278 | } |
279 | ||
280 | void | |
281 | FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event) | |
282 | { | |
010216e3 WS |
283 | bool checked = m_menuBar->IsChecked(event.GetId()); |
284 | m_canvas->EnableRightButtonUndo(checked); | |
63cafd27 JS |
285 | } |
286 | ||
287 | void | |
288 | FortyFrame::ToggleHelpingHand(wxCommandEvent& event) | |
289 | { | |
010216e3 WS |
290 | bool checked = m_menuBar->IsChecked(event.GetId()); |
291 | m_canvas->EnableHelpingHand(checked); | |
63cafd27 | 292 | } |
fc799548 JS |
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 | ||
5cdeff75 JS |
303 | //---------------------------------------------------------------------------- |
304 | // stAboutDialog | |
305 | //---------------------------------------------------------------------------- | |
306 | ||
307 | BEGIN_EVENT_TABLE(FortyAboutDialog,wxDialog) | |
5d2ac6b8 | 308 | EVT_BUTTON(wxID_CLOSE, wxDialog::OnOK) |
5cdeff75 JS |
309 | END_EVENT_TABLE() |
310 | ||
311 | FortyAboutDialog::FortyAboutDialog( wxWindow *parent, wxWindowID id, const wxString &title, | |
312 | const wxPoint &position, const wxSize& size, long style ) : | |
313 | wxDialog( parent, id, title, position, size, style ) | |
314 | { | |
315 | AddControls(this); | |
316 | ||
317 | Centre(wxBOTH); | |
318 | } | |
319 | ||
320 | bool FortyAboutDialog::AddControls(wxWindow* parent) | |
321 | { | |
322 | #if wxUSE_HTML | |
323 | wxString htmlText; | |
236d7be3 | 324 | wxString htmlFile = wxGetApp().GetHelpFile(); |
5cdeff75 | 325 | |
5cdeff75 | 326 | { |
886c2532 WS |
327 | wxTextFile file(htmlFile); |
328 | if (file.Exists()) | |
5cdeff75 | 329 | { |
886c2532 | 330 | file.Open(); |
254a2129 WS |
331 | for ( htmlText = file.GetFirstLine(); |
332 | !file.Eof(); | |
886c2532 | 333 | htmlText << file.GetNextLine() << _T("\n") ) ; |
5cdeff75 JS |
334 | } |
335 | } | |
336 | ||
5d2ac6b8 | 337 | if (htmlText.empty()) |
5cdeff75 JS |
338 | { |
339 | htmlText.Printf(wxT("<html><head><title>Warning</title></head><body><P>Sorry, could not find resource for About dialog<P></body></html>")); | |
340 | } | |
341 | ||
342 | // Customize the HTML | |
f37c24e0 | 343 | htmlText.Replace(wxT("$DATE$"), _T(__DATE__)); |
5cdeff75 JS |
344 | |
345 | wxSize htmlSize(400, 290); | |
346 | ||
347 | // Note: in later versions of wxWin this will be fixed so wxRAISED_BORDER | |
348 | // does the right thing. Meanwhile, this is a workaround. | |
349 | #ifdef __WXMSW__ | |
350 | long borderStyle = wxDOUBLE_BORDER; | |
351 | #else | |
352 | long borderStyle = wxRAISED_BORDER; | |
353 | #endif | |
354 | ||
355 | wxHtmlWindow* html = new wxHtmlWindow(this, ID_ABOUT_HTML_WINDOW, wxDefaultPosition, htmlSize, borderStyle); | |
356 | html -> SetBorders(10); | |
357 | html -> SetPage(htmlText); | |
254a2129 | 358 | |
5cdeff75 JS |
359 | //// Start of sizer-based control creation |
360 | ||
361 | wxSizer *item0 = new wxBoxSizer( wxVERTICAL ); | |
362 | ||
363 | wxWindow *item1 = parent->FindWindow( ID_ABOUT_HTML_WINDOW ); | |
364 | wxASSERT( item1 ); | |
365 | item0->Add( item1, 0, wxALIGN_CENTRE|wxALL, 5 ); | |
366 | ||
5d2ac6b8 | 367 | wxButton *item2 = new wxButton( parent, wxID_CLOSE ); |
5cdeff75 JS |
368 | item2->SetDefault(); |
369 | item2->SetFocus(); | |
370 | ||
371 | item0->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 ); | |
372 | ||
5cdeff75 JS |
373 | parent->SetSizer( item0 ); |
374 | parent->Layout(); | |
375 | item0->Fit( parent ); | |
376 | item0->SetSizeHints( parent ); | |
377 | #endif | |
378 | ||
e0b5519a | 379 | return true; |
5cdeff75 | 380 | } |
fc799548 | 381 |