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