]>
Commit | Line | Data |
---|---|---|
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 "scoredg.h" | |
33 | #ifdef wx_x | |
34 | #include "cards.xbm" | |
35 | #endif | |
36 | ||
37 | class FortyFrame: public wxFrame | |
38 | { | |
39 | public: | |
40 | FortyFrame(wxFrame* frame, char* title, int x, int y, int w, int h); | |
41 | virtual ~FortyFrame(); | |
42 | ||
43 | void OnCloseWindow(wxCloseEvent& event); | |
44 | ||
45 | // Menu callbacks | |
46 | void NewGame(wxCommandEvent& event); | |
47 | void Exit(wxCommandEvent& event); | |
48 | void About(wxCommandEvent& event); | |
49 | void Undo(wxCommandEvent& event); | |
50 | void Redo(wxCommandEvent& event); | |
51 | void Scores(wxCommandEvent& event); | |
52 | void ToggleRightButtonUndo(wxCommandEvent& event); | |
53 | void ToggleHelpingHand(wxCommandEvent& event); | |
54 | ||
55 | DECLARE_EVENT_TABLE() | |
56 | ||
57 | private: | |
58 | enum MenuCommands { NEW_GAME = 10, SCORES, EXIT, | |
59 | UNDO, REDO, | |
60 | RIGHT_BUTTON_UNDO, HELPING_HAND, | |
61 | ABOUT }; | |
62 | ||
63 | wxMenuBar* m_menuBar; | |
64 | FortyCanvas* m_canvas; | |
65 | }; | |
66 | ||
67 | BEGIN_EVENT_TABLE(FortyFrame, wxFrame) | |
68 | EVT_MENU(NEW_GAME, FortyFrame::NewGame) | |
69 | EVT_MENU(EXIT, FortyFrame::Exit) | |
70 | EVT_MENU(ABOUT, FortyFrame::About) | |
71 | EVT_MENU(UNDO, FortyFrame::Undo) | |
72 | EVT_MENU(REDO, FortyFrame::Redo) | |
73 | EVT_MENU(SCORES, FortyFrame::Scores) | |
74 | EVT_MENU(RIGHT_BUTTON_UNDO, FortyFrame::ToggleRightButtonUndo) | |
75 | EVT_MENU(HELPING_HAND, FortyFrame::ToggleHelpingHand) | |
76 | EVT_CLOSE(FortyFrame::OnCloseWindow) | |
77 | END_EVENT_TABLE() | |
78 | ||
79 | // Create a new application object | |
80 | IMPLEMENT_APP (FortyApp) | |
81 | ||
82 | wxColour* FortyApp::m_backgroundColour = 0; | |
83 | wxColour* FortyApp::m_textColour = 0; | |
84 | wxBrush* FortyApp::m_backgroundBrush = 0; | |
85 | ||
86 | bool FortyApp::OnInit() | |
87 | { | |
88 | FortyFrame* frame = new FortyFrame( | |
89 | 0, | |
90 | "Forty Thieves", | |
91 | -1, -1, 668, 510 | |
92 | ); | |
93 | ||
94 | // Show the frame | |
95 | frame->Show(TRUE); | |
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("BLACK"); | |
125 | } | |
126 | ||
127 | return *m_textColour; | |
128 | } | |
129 | ||
130 | // My frame constructor | |
131 | FortyFrame::FortyFrame(wxFrame* frame, char* title, int x, int y, int w, int h): | |
132 | wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) | |
133 | { | |
134 | // set the icon | |
135 | #ifdef __WXMSW__ | |
136 | SetIcon(wxIcon("CardsIcon")); | |
137 | #else | |
138 | #ifdef GTK_TBD | |
139 | SetIcon(wxIcon(Cards_bits, Cards_width, Cards_height)); | |
140 | #endif | |
141 | #endif | |
142 | ||
143 | // Make a menu bar | |
144 | wxMenu* gameMenu = new wxMenu; | |
145 | gameMenu->Append(NEW_GAME, "&New", "Start a new game"); | |
146 | gameMenu->Append(SCORES, "&Scores...", "Displays scores"); | |
147 | gameMenu->Append(EXIT, "E&xit", "Exits Forty Thieves"); | |
148 | ||
149 | wxMenu* editMenu = new wxMenu; | |
150 | editMenu->Append(UNDO, "&Undo", "Undo the last move"); | |
151 | editMenu->Append(REDO, "&Redo", "Redo a move that has been undone"); | |
152 | ||
153 | wxMenu* optionsMenu = new wxMenu; | |
154 | optionsMenu->Append(RIGHT_BUTTON_UNDO, | |
155 | "&Right button undo", | |
156 | "Enables/disables right mouse button undo and redo", | |
157 | TRUE | |
158 | ); | |
159 | optionsMenu->Append(HELPING_HAND, | |
160 | "&Helping hand", | |
161 | "Enables/disables hand cursor when a card can be moved", | |
162 | TRUE | |
163 | ); | |
164 | optionsMenu->Check(HELPING_HAND, TRUE); | |
165 | optionsMenu->Check(RIGHT_BUTTON_UNDO, TRUE); | |
166 | ||
167 | wxMenu* helpMenu = new wxMenu; | |
168 | helpMenu->Append(ABOUT, "&About", "Displays program version information"); | |
169 | ||
170 | m_menuBar = new wxMenuBar; | |
171 | m_menuBar->Append(gameMenu, "&Game"); | |
172 | m_menuBar->Append(editMenu, "&Edit"); | |
173 | m_menuBar->Append(optionsMenu, "&Options"); | |
174 | m_menuBar->Append(helpMenu, "&Help"); | |
175 | ||
176 | SetMenuBar(m_menuBar); | |
177 | ||
178 | m_canvas = new FortyCanvas(this, 0, 0, 400, 400); | |
179 | wxLayoutConstraints* constr = new wxLayoutConstraints; | |
180 | constr->left.SameAs(this, wxLeft); | |
181 | constr->top.SameAs(this, wxTop); | |
182 | constr->right.SameAs(this, wxRight); | |
183 | constr->height.SameAs(this, wxHeight); | |
184 | m_canvas->SetConstraints(constr); | |
185 | ||
186 | CreateStatusBar(); | |
187 | } | |
188 | ||
189 | FortyFrame::~FortyFrame() | |
190 | { | |
191 | } | |
192 | ||
193 | void FortyFrame::OnCloseWindow(wxCloseEvent& event) | |
194 | { | |
195 | if (m_canvas->OnCloseCanvas()) | |
196 | { | |
197 | this->Destroy(); | |
198 | } | |
199 | else | |
200 | event.Veto(); | |
201 | } | |
202 | ||
203 | void | |
204 | FortyFrame::NewGame(wxCommandEvent&) | |
205 | { | |
206 | m_canvas->NewGame(); | |
207 | } | |
208 | ||
209 | void | |
210 | FortyFrame::Exit(wxCommandEvent&) | |
211 | { | |
212 | #ifdef __WXGTK__ | |
213 | // wxGTK doesn't call OnClose() so we do it here | |
214 | // if (OnClose()) | |
215 | #endif | |
216 | Close(TRUE); | |
217 | } | |
218 | ||
219 | void | |
220 | FortyFrame::About(wxCommandEvent&) | |
221 | { | |
222 | wxMessageBox( | |
223 | "Forty Thieves\n\n" | |
224 | "A freeware program using the wxWindows\n" | |
225 | "portable C++ GUI toolkit.\n" | |
226 | "http://web.ukonline.co.uk/julian.smart/wxwin\n" | |
227 | "http://www.freiburg.linux.de/~wxxt\n\n" | |
228 | "Author: Chris Breeze (c) 1992-1998\n" | |
229 | "email: chris.breeze@iname.com", | |
230 | "About Forty Thieves", | |
231 | wxOK, this | |
232 | ); | |
233 | } | |
234 | ||
235 | void | |
236 | FortyFrame::Undo(wxCommandEvent&) | |
237 | { | |
238 | m_canvas->Undo(); | |
239 | } | |
240 | ||
241 | void | |
242 | FortyFrame::Redo(wxCommandEvent&) | |
243 | { | |
244 | m_canvas->Redo(); | |
245 | } | |
246 | ||
247 | void | |
248 | FortyFrame::Scores(wxCommandEvent&) | |
249 | { | |
250 | m_canvas->UpdateScores(); | |
251 | ScoreDialog scores(this, m_canvas->GetScoreFile()); | |
252 | scores.Display(); | |
253 | } | |
254 | ||
255 | void | |
256 | FortyFrame::ToggleRightButtonUndo(wxCommandEvent& event) | |
257 | { | |
258 | bool checked = m_menuBar->IsChecked(event.GetId()); | |
259 | m_canvas->EnableRightButtonUndo(checked); | |
260 | } | |
261 | ||
262 | void | |
263 | FortyFrame::ToggleHelpingHand(wxCommandEvent& event) | |
264 | { | |
265 | bool checked = m_menuBar->IsChecked(event.GetId()); | |
266 | m_canvas->EnableHelpingHand(checked); | |
267 | } |