Removed leaks from Forty demo
[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 BEGIN_EVENT_TABLE(FortyFrame, wxFrame)
36 EVT_MENU(NEW_GAME, FortyFrame::NewGame)
37 EVT_MENU(EXIT, FortyFrame::Exit)
38 EVT_MENU(ABOUT, FortyFrame::About)
39 EVT_MENU(UNDO, FortyFrame::Undo)
40 EVT_MENU(REDO, FortyFrame::Redo)
41 EVT_MENU(SCORES, FortyFrame::Scores)
42 EVT_MENU(RIGHT_BUTTON_UNDO, FortyFrame::ToggleRightButtonUndo)
43 EVT_MENU(HELPING_HAND, FortyFrame::ToggleHelpingHand)
44 EVT_MENU(LARGE_CARDS, FortyFrame::ToggleCardSize)
45 EVT_CLOSE(FortyFrame::OnCloseWindow)
46 END_EVENT_TABLE()
47
48 // Create a new application object
49 IMPLEMENT_APP (FortyApp)
50
51 wxColour* FortyApp::m_backgroundColour = 0;
52 wxColour* FortyApp::m_textColour = 0;
53 wxBrush* FortyApp::m_backgroundBrush = 0;
54
55 FortyApp::FortyApp()
56 {
57 }
58
59 FortyApp::~FortyApp()
60 {
61 delete m_backgroundColour;
62 delete m_textColour;
63 delete m_backgroundBrush;
64 delete Card::m_symbolBmap;
65 delete Card::m_pictureBmap;
66
67 }
68
69 bool FortyApp::OnInit()
70 {
71 bool largecards = FALSE;
72 wxSize size(668,510);
73
74 if ((argc > 1) && (!wxStrcmp(argv[1],"-L")))
75 {
76 largecards = TRUE;
77 size = wxSize(1000,750);
78 }
79
80 FortyFrame* frame = new FortyFrame(
81 0,
82 "Forty Thieves",
83 -1, -1, size.x, size.y,largecards
84 );
85
86 // Show the frame
87 frame->Show(TRUE);
88
89 frame->GetCanvas()->ShowPlayerDialog();
90
91 return TRUE;
92 }
93
94 const wxColour& FortyApp::BackgroundColour()
95 {
96 if (!m_backgroundColour)
97 {
98 m_backgroundColour = new wxColour(0, 128, 0);
99 }
100
101 return *m_backgroundColour;
102 }
103
104 const wxBrush& FortyApp::BackgroundBrush()
105 {
106 if (!m_backgroundBrush)
107 {
108 m_backgroundBrush = new wxBrush(BackgroundColour(), wxSOLID);
109 }
110
111 return *m_backgroundBrush;
112 }
113
114 const wxColour& FortyApp::TextColour()
115 {
116 if (!m_textColour)
117 {
118 m_textColour = new wxColour("BLACK");
119 }
120
121 return *m_textColour;
122 }
123
124 // My frame constructor
125 FortyFrame::FortyFrame(wxFrame* frame, char* title, int x, int y, int w, int h,bool largecards):
126 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
127 {
128 #ifdef __WXMAC__
129 // we need this in order to allow the about menu relocation, since ABOUT is not the default id of the about menu
130 wxApp::s_macAboutMenuItemId = ABOUT ;
131 #endif
132 // set the icon
133 #ifdef __WXMSW__
134 SetIcon(wxIcon("CardsIcon"));
135 #else
136 #ifdef GTK_TBD
137 SetIcon(wxIcon(Cards_bits, Cards_width, Cards_height));
138 #endif
139 #endif
140
141 // Make a menu bar
142 wxMenu* gameMenu = new wxMenu;
143 gameMenu->Append(NEW_GAME, "&New", "Start a new game");
144 gameMenu->Append(SCORES, "&Scores...", "Displays scores");
145 gameMenu->Append(EXIT, "E&xit", "Exits Forty Thieves");
146
147 wxMenu* editMenu = new wxMenu;
148 editMenu->Append(UNDO, "&Undo", "Undo the last move");
149 editMenu->Append(REDO, "&Redo", "Redo a move that has been undone");
150
151 wxMenu* optionsMenu = new wxMenu;
152 optionsMenu->Append(RIGHT_BUTTON_UNDO,
153 "&Right button undo",
154 "Enables/disables right mouse button undo and redo",
155 TRUE
156 );
157 optionsMenu->Append(HELPING_HAND,
158 "&Helping hand",
159 "Enables/disables hand cursor when a card can be moved",
160 TRUE
161 );
162 optionsMenu->Append(LARGE_CARDS,
163 "&Large cards",
164 "Enables/disables large cards for high resolution displays",
165 TRUE
166 );
167 optionsMenu->Check(HELPING_HAND, TRUE);
168 optionsMenu->Check(RIGHT_BUTTON_UNDO, TRUE);
169 optionsMenu->Check(LARGE_CARDS, largecards ? TRUE : FALSE);
170
171 wxMenu* helpMenu = new wxMenu;
172 helpMenu->Append(ABOUT, "&About", "Displays program version information");
173
174 m_menuBar = new wxMenuBar;
175 m_menuBar->Append(gameMenu, "&Game");
176 m_menuBar->Append(editMenu, "&Edit");
177 m_menuBar->Append(optionsMenu, "&Options");
178 m_menuBar->Append(helpMenu, "&Help");
179
180 SetMenuBar(m_menuBar);
181
182 if (largecards)
183 Card::SetScale(1.3);
184
185 m_canvas = new FortyCanvas(this, 0, 0, 400, 400);
186 wxLayoutConstraints* constr = new wxLayoutConstraints;
187 constr->left.SameAs(this, wxLeft);
188 constr->top.SameAs(this, wxTop);
189 constr->right.SameAs(this, wxRight);
190 constr->height.SameAs(this, wxHeight);
191 m_canvas->SetConstraints(constr);
192
193 CreateStatusBar();
194 }
195
196 FortyFrame::~FortyFrame()
197 {
198 }
199
200 void FortyFrame::OnCloseWindow(wxCloseEvent& event)
201 {
202 if (m_canvas->OnCloseCanvas() )
203 {
204 this->Destroy();
205 }
206 else
207 event.Veto();
208 }
209
210 void
211 FortyFrame::NewGame(wxCommandEvent&)
212 {
213 m_canvas->NewGame();
214 }
215
216 void
217 FortyFrame::Exit(wxCommandEvent&)
218 {
219 #ifdef __WXGTK__
220 // wxGTK doesn't call OnClose() so we do it here
221 // if (OnClose())
222 #endif
223 Close(TRUE);
224 }
225
226 void
227 FortyFrame::About(wxCommandEvent&)
228 {
229 wxMessageBox(
230 "Forty Thieves\n\n"
231 "A freeware program using the wxWindows\n"
232 "portable C++ GUI toolkit.\n"
233 "http://www.wxwindows.org\n"
234 "http://www.freiburg.linux.de/~wxxt\n\n"
235 "Author: Chris Breeze (c) 1992-1998\n"
236 "email: chris.breeze@iname.com",
237 "About Forty Thieves",
238 wxOK, this
239 );
240 }
241
242 void
243 FortyFrame::Undo(wxCommandEvent&)
244 {
245 m_canvas->Undo();
246 }
247
248 void
249 FortyFrame::Redo(wxCommandEvent&)
250 {
251 m_canvas->Redo();
252 }
253
254 void
255 FortyFrame::Scores(wxCommandEvent&)
256 {
257 m_canvas->UpdateScores();
258 ScoreDialog scores(this, m_canvas->GetScoreFile());
259 scores.Display();
260 }
261
262 void
263 FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event)
264 {
265 bool checked = m_menuBar->IsChecked(event.GetId());
266 m_canvas->EnableRightButtonUndo(checked);
267 }
268
269 void
270 FortyFrame::ToggleHelpingHand(wxCommandEvent& event)
271 {
272 bool checked = m_menuBar->IsChecked(event.GetId());
273 m_canvas->EnableHelpingHand(checked);
274 }
275
276 void
277 FortyFrame::ToggleCardSize(wxCommandEvent& event)
278 {
279 bool checked = m_menuBar->IsChecked(event.GetId());
280 Card::SetScale(checked ? 1.3 : 1);
281 m_canvas->LayoutGame();
282 m_canvas->Refresh();
283 }
284
285