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