Compensate the usage of selecting already selected menu items by explicit 'New game...
[wxWidgets.git] / demos / bombs / bombs.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: bombs.cpp
3 // Purpose: Bombs game
4 // Author: P. Foggia 1996
5 // Modified by: Wlodzimierz Skiba (ABX) since 2003
6 // Created: 1996
7 // RCS-ID: $Id$
8 // Copyright: (c) 1996 P. Foggia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 # pragma implementation
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 # pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 # include "wx/wx.h"
24 #endif //precompiled headers
25
26 #include "wx/stockitem.h"
27
28 #include "bombs.h"
29
30 #include <stdlib.h>
31
32 #ifndef __WXWINCE__
33 # include <time.h>
34 #endif
35
36 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) \
37 || defined(__WXMAC__) || defined(__WXMGL__)
38 # include "bombs.xpm"
39 #endif
40
41 IMPLEMENT_APP(BombsApp)
42
43 #ifdef __WXWINCE__
44 STDAPI_(__int64) CeGetRandomSeed();
45 #endif
46
47 // Called to initialize the program
48 bool BombsApp::OnInit()
49 {
50 #ifdef __WXWINCE__
51 srand((unsigned) CeGetRandomSeed());
52 #else
53 srand((unsigned) time(NULL));
54 #endif
55
56 m_frame = new BombsFrame(&m_game);
57
58 m_frame->NewGame(bombsID_EASY, false);
59
60 return true;
61 }
62
63 BEGIN_EVENT_TABLE(BombsFrame, wxFrame)
64 EVT_MENU(wxID_NEW, BombsFrame::OnNewGame)
65 EVT_MENU(bombsID_EASY, BombsFrame::OnEasyGame)
66 EVT_MENU(bombsID_MEDIUM, BombsFrame::OnMediumGame)
67 EVT_MENU(bombsID_HARD, BombsFrame::OnHardGame)
68 EVT_MENU(bombsID_EASYCORNER, BombsFrame::OnEasyCorner)
69 EVT_MENU(wxID_EXIT, BombsFrame::OnExit)
70 EVT_MENU(wxID_ABOUT, BombsFrame::OnAbout)
71 END_EVENT_TABLE()
72
73 BombsFrame::BombsFrame(BombsGame *game)
74 : wxFrame(NULL, wxID_ANY, wxT("wxBombs"), wxDefaultPosition,
75 wxSize(300, 300), wxDEFAULT_DIALOG_STYLE|wxMINIMIZE_BOX)
76 {
77 m_game = game;
78 m_easyCorner = false;
79 m_lastLevel = bombsID_EASY;
80
81 SetIcon(wxICON(bombs));
82
83 #if wxUSE_STATUSBAR
84 CreateStatusBar();
85 #endif
86
87 // Create a menu bar for the frame
88 wxMenuBar *menuBar = new wxMenuBar;
89 wxMenu *menuFile = new wxMenu;
90 wxMenu *menuLevel = new wxMenu;
91 menuLevel->AppendRadioItem(bombsID_EASY, wxT("&Easy (10x10)\tCtrl-1"));
92 menuLevel->AppendRadioItem(bombsID_MEDIUM, wxT("&Medium (15x15)\tCtrl-2"));
93 menuLevel->AppendRadioItem(bombsID_HARD, wxT("&Hard (25x20)\tCtrl-3"));
94
95 menuFile->Append(wxID_NEW, wxT("&New game\tCtrl-N"));
96 menuFile->Append(bombsID_LEVEL, wxT("&Level"),menuLevel, wxT("Starts a new game"));
97 menuFile->AppendCheckItem(bombsID_EASYCORNER, wxT("&Easy corner"));
98
99 menuFile->AppendSeparator();
100 menuFile->Append(wxID_EXIT, wxGetStockLabel(wxID_EXIT), wxT("Quits the application"));
101
102 menuBar->Append(menuFile, wxT("&File"));
103
104
105 wxMenu *menuHelp = new wxMenu;
106 menuHelp->Append(wxID_ABOUT, wxT("&About"),
107 wxT("Displays the program information") );
108
109 menuBar->Append(menuHelp, wxT("&Help"));
110
111 SetMenuBar(menuBar);
112
113 // Create child subwindows.
114 m_canvas = new BombsCanvas(this, m_game);
115
116 // Ensure the subwindows get resized o.k.
117 // OnSize(width, height);
118
119 // Centre frame on the screen.
120 Centre(wxBOTH);
121
122 // Show the frame.
123 Show();
124 }
125
126 void BombsFrame::OnExit(wxCommandEvent& WXUNUSED(event))
127 {
128 Close();
129 }
130
131 void BombsFrame::NewGame(int level, bool query)
132 {
133 if(query)
134 {
135 int ok = wxMessageBox(
136 wxT("Start new game regardless previous board?"),
137 wxT("Confirm"),
138 wxYES_NO | wxICON_QUESTION,
139 this
140 );
141 if(ok!=wxYES)return;
142 }
143
144 int numHorzCells = 20, numVertCells = 20;
145 m_lastLevel = level;
146
147 switch(level)
148 {
149 case bombsID_EASY:
150 numHorzCells = numVertCells = 10;
151 break;
152
153 case bombsID_MEDIUM:
154 numHorzCells = numVertCells = 15;
155 break;
156
157 case bombsID_HARD:
158 numHorzCells = 25; numVertCells = 20;
159 break;
160
161 default :
162 wxFAIL_MSG(wxT("Invalid level"));
163 break;
164 }
165
166 m_game->Init(numHorzCells, numVertCells, m_easyCorner);
167
168 GetMenuBar()->Check(level, true);
169
170 m_canvas->UpdateGridSize();
171 SetClientSize(m_canvas->GetGridSizeInPixels());
172 }
173
174 void BombsFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
175 {
176 wxMessageBox(
177 wxT("wxBombs (c) 1996 by P. Foggia\n<foggia@amalfi.dis.unina.it>"),
178 wxT("About wxBombs") );
179 }
180
181 void BombsFrame::OnNewGame(wxCommandEvent& WXUNUSED(event))
182 {
183 NewGame(m_lastLevel, true);
184 }
185
186 void BombsFrame::OnEasyGame(wxCommandEvent& WXUNUSED(event))
187 {
188 NewGame(bombsID_EASY, true);
189 }
190
191 void BombsFrame::OnMediumGame(wxCommandEvent& WXUNUSED(event))
192 {
193 NewGame(bombsID_MEDIUM, true);
194 }
195
196 void BombsFrame::OnHardGame(wxCommandEvent& WXUNUSED(event))
197 {
198 NewGame(bombsID_HARD, true);
199 }
200
201 void BombsFrame::OnEasyCorner(wxCommandEvent& WXUNUSED(event))
202 {
203 wxString msg;
204 if(m_easyCorner)
205 msg = wxT("enable");
206 else
207 msg = wxT("disable");
208
209 msg = wxT("Do you really want to ") + msg + wxT(" having\ntop left corner always empty for easier start?");
210
211 int ok = wxMessageBox(
212 msg,
213 wxT("Confirm"),
214 wxYES_NO | wxICON_QUESTION,
215 this
216 );
217
218 if(ok!=wxYES)return;
219
220 m_easyCorner = !m_easyCorner;
221
222 NewGame(m_lastLevel, true);
223 }
224
225 BEGIN_EVENT_TABLE(BombsCanvas, wxPanel)
226 EVT_PAINT(BombsCanvas::OnPaint)
227 EVT_MOUSE_EVENTS(BombsCanvas::OnMouseEvent)
228 EVT_CHAR(BombsCanvas::OnChar)
229 END_EVENT_TABLE()
230
231 BombsCanvas::BombsCanvas(wxFrame *parent, BombsGame *game)
232 : wxPanel(parent, wxID_ANY)
233 {
234 m_game = game;
235 int sx, sy;
236 wxClientDC dc(this);
237 wxFont font= BOMBS_FONT;
238 dc.SetFont(font);
239
240 long chw, chh;
241 wxString buf = wxT("M");
242
243 dc.GetTextExtent(buf, &chw, &chh);
244 dc.SetFont(wxNullFont);
245
246 dc.SetMapMode(wxMM_METRIC);
247
248 int xcm = dc.LogicalToDeviceX(10);
249 int ycm = dc.LogicalToDeviceY(10);
250 // To have a square cell, there must be :
251 // sx*ycm == sy*xcm
252 if (chw*ycm < chh*xcm)
253 {
254 sy = chh;
255 sx = chh*xcm/ycm;
256 }
257 else
258 {
259 sx = chw;
260 sy = chw*ycm/xcm;
261 }
262
263 m_cellWidth = (sx+3+X_UNIT)/X_UNIT;
264 m_cellHeight = (sy+3+Y_UNIT)/Y_UNIT;
265 dc.SetMapMode(wxMM_TEXT);
266 m_bmp = NULL;
267 }
268
269 BombsCanvas::~BombsCanvas()
270 {
271 if (m_bmp)
272 {
273 delete m_bmp;
274 m_bmp = NULL;
275 }
276 }
277
278 // Called when canvas needs to be repainted.
279 void BombsCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
280 {
281 wxPaintDC dc(this);
282
283 const int numHorzCells = m_game->GetWidth();
284 const int numVertCells = m_game->GetHeight();
285 // Insert your drawing code here.
286 if (!m_bmp)
287 {
288 wxSize size = dc.GetSize();
289 m_bmp = new wxBitmap(size.GetWidth(), size.GetHeight());
290 if (m_bmp)
291 {
292 wxMemoryDC memDC;
293 memDC.SelectObject(*m_bmp);
294 DrawField(&memDC, 0, 0, numHorzCells-1, numVertCells-1);
295 memDC.SelectObject(wxNullBitmap);
296 }
297 }
298
299 if (m_bmp)
300 {
301 wxMemoryDC memDC;
302 memDC.SelectObject(*m_bmp);
303 wxSize size = dc.GetSize();
304 dc.Blit(0, 0, size.GetWidth(), size.GetHeight(),
305 &memDC, 0, 0, wxCOPY);
306 memDC.SelectObject(wxNullBitmap);
307 }
308 else
309 {
310 DrawField(&dc, 0, 0, numHorzCells-1, numVertCells-1);
311 }
312 }
313
314 void BombsCanvas::UpdateGridSize()
315 {
316
317 if (m_bmp)
318 {
319 delete m_bmp;
320 m_bmp = NULL;
321 }
322 SetSize(GetGridSizeInPixels());
323 Refresh();
324 }
325
326 wxSize BombsCanvas::GetGridSizeInPixels() const
327 {
328 return wxSize(m_cellWidth*X_UNIT*m_game->GetWidth(),
329 m_cellHeight*Y_UNIT*m_game->GetHeight());
330 }
331