]> git.saurik.com Git - wxWidgets.git/blame - demos/life/life.cpp
Various compiler changes
[wxWidgets.git] / demos / life / life.cpp
CommitLineData
5a1dca12
GRG
1/////////////////////////////////////////////////////////////////////////////
2// Name: life.cpp
29b07a38 3// Purpose: The game of Life, created by J. H. Conway
5a1dca12
GRG
4// Author: Guillermo Rodriguez Garcia, <guille@iies.es>
5// Modified by:
6// Created: Jan/2000
7// RCS-ID: $Id$
8// Copyright: (c) 2000, Guillermo Rodriguez Garcia
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ==========================================================================
e0a40292 13// headers, declarations, constants
5a1dca12
GRG
14// ==========================================================================
15
5a1dca12 16#ifdef __GNUG__
2480be69 17 #pragma implementation "life.h"
5a1dca12
GRG
18#endif
19
2fa7c206
JS
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
29b07a38 24 #pragma hdrstop
2fa7c206
JS
25#endif
26
27#ifndef WX_PRECOMP
29b07a38 28 #include "wx/wx.h"
2fa7c206
JS
29#endif
30
5a1dca12 31#include "wx/statline.h"
2480be69
GRG
32
33#include "life.h"
34#include "game.h"
35#include "dialogs.h"
5a1dca12
GRG
36
37// --------------------------------------------------------------------------
38// resources
39// --------------------------------------------------------------------------
40
41#if defined(__WXGTK__) || defined(__WXMOTIF__)
29b07a38 42 // application icon
5a1dca12
GRG
43 #include "mondrian.xpm"
44
45 // bitmap buttons for the toolbar
46 #include "bitmaps/reset.xpm"
47 #include "bitmaps/play.xpm"
48 #include "bitmaps/stop.xpm"
e0a40292
GRG
49 #include "bitmaps/zoomin.xpm"
50 #include "bitmaps/zoomout.xpm"
29b07a38
GRG
51
52 // navigator
53 #include "bitmaps/north.xpm"
54 #include "bitmaps/south.xpm"
55 #include "bitmaps/east.xpm"
56 #include "bitmaps/west.xpm"
57 #include "bitmaps/center.xpm"
5a1dca12
GRG
58#endif
59
5a1dca12
GRG
60// --------------------------------------------------------------------------
61// constants
62// --------------------------------------------------------------------------
63
64// IDs for the controls and the menu commands
65enum
66{
29b07a38
GRG
67 // timer
68 ID_TIMER = 1001,
69
5a1dca12 70 // menu items and toolbar buttons
29b07a38 71 ID_RESET,
087e4f4a
GRG
72 ID_SAMPLES,
73 ID_ABOUT,
74 ID_EXIT,
5a1dca12 75 ID_START,
087e4f4a 76 ID_STEP,
5a1dca12 77 ID_STOP,
e0a40292
GRG
78 ID_ZOOMIN,
79 ID_ZOOMOUT,
80 ID_TOPSPEED,
5a1dca12 81
087e4f4a 82 // speed selection slider
29b07a38
GRG
83 ID_SLIDER,
84
85 // navigation
86 ID_SHOWNAV,
87 ID_ORIGIN,
88 ID_CENTER,
89 ID_NORTH,
90 ID_SOUTH,
91 ID_EAST,
92 ID_WEST,
5a1dca12
GRG
93};
94
95// --------------------------------------------------------------------------
96// event tables and other macros for wxWindows
97// --------------------------------------------------------------------------
98
99// Event tables
5a1dca12 100BEGIN_EVENT_TABLE(LifeFrame, wxFrame)
e0a40292
GRG
101 EVT_MENU (ID_SAMPLES, LifeFrame::OnSamples)
102 EVT_MENU (ID_RESET, LifeFrame::OnMenu)
103 EVT_MENU (ID_ABOUT, LifeFrame::OnMenu)
104 EVT_MENU (ID_EXIT, LifeFrame::OnMenu)
e0a40292
GRG
105 EVT_MENU (ID_START, LifeFrame::OnMenu)
106 EVT_MENU (ID_STEP, LifeFrame::OnMenu)
107 EVT_MENU (ID_STOP, LifeFrame::OnMenu)
e0a40292 108 EVT_MENU (ID_TOPSPEED, LifeFrame::OnMenu)
29b07a38
GRG
109 EVT_MENU (ID_SHOWNAV, LifeFrame::OnMenu)
110 EVT_MENU (ID_ORIGIN, LifeFrame::OnNavigate)
111 EVT_BUTTON (ID_CENTER, LifeFrame::OnNavigate)
112 EVT_BUTTON (ID_NORTH, LifeFrame::OnNavigate)
113 EVT_BUTTON (ID_SOUTH, LifeFrame::OnNavigate)
114 EVT_BUTTON (ID_EAST, LifeFrame::OnNavigate)
115 EVT_BUTTON (ID_WEST, LifeFrame::OnNavigate)
116 EVT_MENU (ID_ZOOMIN, LifeFrame::OnZoom)
117 EVT_MENU (ID_ZOOMOUT, LifeFrame::OnZoom)
e0a40292 118 EVT_COMMAND_SCROLL (ID_SLIDER, LifeFrame::OnSlider)
29b07a38 119 EVT_TIMER (ID_TIMER, LifeFrame::OnTimer)
e0a40292 120 EVT_CLOSE ( LifeFrame::OnClose)
ecbdd409 121END_EVENT_TABLE()
5a1dca12 122
29b07a38
GRG
123BEGIN_EVENT_TABLE(LifeNavigator, wxMiniFrame)
124 EVT_CLOSE ( LifeNavigator::OnClose)
125END_EVENT_TABLE()
126
e0a40292
GRG
127BEGIN_EVENT_TABLE(LifeCanvas, wxWindow)
128 EVT_PAINT ( LifeCanvas::OnPaint)
129 EVT_SCROLLWIN ( LifeCanvas::OnScroll)
130 EVT_SIZE ( LifeCanvas::OnSize)
131 EVT_MOUSE_EVENTS ( LifeCanvas::OnMouse)
132 EVT_ERASE_BACKGROUND( LifeCanvas::OnEraseBackground)
ecbdd409 133END_EVENT_TABLE()
5a1dca12 134
5a1dca12
GRG
135
136// Create a new application object
137IMPLEMENT_APP(LifeApp)
138
e0a40292 139
5a1dca12
GRG
140// ==========================================================================
141// implementation
142// ==========================================================================
143
2480be69 144// some shortcuts
e0a40292
GRG
145#define ADD_TOOL(id, bmp, tooltip, help) \
146 toolBar->AddTool(id, bmp, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, tooltip, help)
2480be69 147
2480be69 148
5a1dca12
GRG
149// --------------------------------------------------------------------------
150// LifeApp
151// --------------------------------------------------------------------------
152
e0a40292 153// 'Main program' equivalent: the program execution "starts" here
5a1dca12
GRG
154bool LifeApp::OnInit()
155{
156 // create the main application window
157 LifeFrame *frame = new LifeFrame();
158
159 // show it and tell the application that it's our main window
160 frame->Show(TRUE);
161 SetTopWindow(frame);
162
163 // enter the main message loop and run the app
164 return TRUE;
165}
166
167// --------------------------------------------------------------------------
168// LifeFrame
169// --------------------------------------------------------------------------
170
171// frame constructor
29b07a38 172LifeFrame::LifeFrame() : wxFrame((wxFrame *)0, -1, _("Life!"), wxPoint(200, 200))
5a1dca12
GRG
173{
174 // frame icon
175 SetIcon(wxICON(mondrian));
176
177 // menu bar
178 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
29b07a38 179 wxMenu *menuView = new wxMenu("", wxMENU_TEAROFF);
087e4f4a 180 wxMenu *menuGame = new wxMenu("", wxMENU_TEAROFF);
5a1dca12 181
e0a40292 182 menuFile->Append(ID_RESET, _("Reset"), _("Start a new game"));
087e4f4a 183 menuFile->Append(ID_SAMPLES, _("Sample game..."), _("Select a sample configuration"));
5a1dca12
GRG
184 menuFile->AppendSeparator();
185 menuFile->Append(ID_ABOUT, _("&About...\tCtrl-A"), _("Show about dialog"));
186 menuFile->AppendSeparator();
187 menuFile->Append(ID_EXIT, _("E&xit\tAlt-X"), _("Quit this program"));
087e4f4a 188
29b07a38
GRG
189 menuView->Append(ID_SHOWNAV, _("Navigation toolbox"), _("Show or hide toolbox"), TRUE);
190 menuView->Check (ID_SHOWNAV, TRUE);
191 menuView->AppendSeparator();
192 menuView->Append(ID_ORIGIN, _("Absolute origin"), _("Go to (0, 0)"));
193 menuView->Append(ID_CENTER, _("Center of mass"), _("Find center of mass"));
194 menuView->Append(ID_NORTH, _("North"), _("Find northernmost cell"));
195 menuView->Append(ID_SOUTH, _("South"), _("Find southernmost cell"));
196 menuView->Append(ID_EAST, _("East"), _("Find easternmost cell"));
197 menuView->Append(ID_WEST, _("West"), _("Find westernmost cell"));
198 menuView->AppendSeparator();
199 menuView->Append(ID_ZOOMIN, _("Zoom &in\tCtrl-I"));
200 menuView->Append(ID_ZOOMOUT, _("Zoom &out\tCtrl-O"));
201
087e4f4a
GRG
202 menuGame->Append(ID_START, _("&Start\tCtrl-S"), _("Start"));
203 menuGame->Append(ID_STEP, _("&Next\tCtrl-N"), _("Single step"));
204 menuGame->Append(ID_STOP, _("S&top\tCtrl-T"), _("Stop"));
205 menuGame->Enable(ID_STOP, FALSE);
206 menuGame->AppendSeparator();
e0a40292 207 menuGame->Append(ID_TOPSPEED, _("Top speed!"), _("Go as fast as possible"));
087e4f4a 208
5a1dca12
GRG
209 wxMenuBar *menuBar = new wxMenuBar();
210 menuBar->Append(menuFile, _("&File"));
29b07a38 211 menuBar->Append(menuView, _("&View"));
087e4f4a 212 menuBar->Append(menuGame, _("&Game"));
5a1dca12
GRG
213 SetMenuBar(menuBar);
214
215 // tool bar
e0a40292 216 wxBitmap tbBitmaps[5];
471ed537 217
5a1dca12
GRG
218 tbBitmaps[0] = wxBITMAP(reset);
219 tbBitmaps[1] = wxBITMAP(play);
220 tbBitmaps[2] = wxBITMAP(stop);
e0a40292
GRG
221 tbBitmaps[3] = wxBITMAP(zoomin);
222 tbBitmaps[4] = wxBITMAP(zoomout);
5a1dca12
GRG
223
224 wxToolBar *toolBar = CreateToolBar();
225 toolBar->SetMargins(5, 5);
226 toolBar->SetToolBitmapSize(wxSize(16, 16));
33e39147
GRG
227 ADD_TOOL(ID_RESET, tbBitmaps[0], _("Reset"), _("Start a new game"));
228 ADD_TOOL(ID_START, tbBitmaps[1], _("Start"), _("Start"));
229 ADD_TOOL(ID_STOP, tbBitmaps[2], _("Stop"), _("Stop"));
e0a40292 230 toolBar->AddSeparator();
33e39147
GRG
231 ADD_TOOL(ID_ZOOMIN, tbBitmaps[3], _("Zoom in"), _("Zoom in"));
232 ADD_TOOL(ID_ZOOMOUT, tbBitmaps[4], _("Zoom out"), _("Zoom out"));
5a1dca12 233 toolBar->Realize();
e0a40292 234 toolBar->EnableTool(ID_STOP, FALSE); // must be after Realize() !
5a1dca12
GRG
235
236 // status bar
237 CreateStatusBar(2);
238 SetStatusText(_("Welcome to Life!"));
239
29b07a38
GRG
240 // game and timer
241 m_life = new Life();
242 m_timer = new wxTimer(this, ID_TIMER);
243 m_running = FALSE;
244 m_topspeed = FALSE;
245 m_interval = 500;
246 m_tics = 0;
5a1dca12 247
29b07a38
GRG
248 // We use two different panels to reduce flicker in wxGTK, because
249 // some widgets (like wxStaticText) don't have their own X11 window,
250 // and thus updating the text would result in a refresh of the canvas
251 // if they belong to the same parent.
252
253 wxPanel *panel1 = new wxPanel(this, -1);
254 wxPanel *panel2 = new wxPanel(this, -1);
255
256 // canvas
257 m_canvas = new LifeCanvas(panel1, m_life);
258
259 // info panel
260 m_text = new wxStaticText(panel2, -1,
261 wxEmptyString,
262 wxDefaultPosition,
263 wxDefaultSize,
264 wxALIGN_CENTER | wxST_NO_AUTORESIZE);
265
266 wxSlider *slider = new wxSlider(panel2, ID_SLIDER,
2480be69
GRG
267 5, 1, 10,
268 wxDefaultPosition,
269 wxSize(200, -1),
270 wxSL_HORIZONTAL | wxSL_AUTOTICKS);
5a1dca12 271
29b07a38
GRG
272 UpdateInfoText();
273
5a1dca12 274 // component layout
29b07a38
GRG
275 wxBoxSizer *sizer1 = new wxBoxSizer(wxVERTICAL);
276 wxBoxSizer *sizer2 = new wxBoxSizer(wxVERTICAL);
277 wxBoxSizer *sizer3 = new wxBoxSizer(wxVERTICAL);
278
279 sizer1->Add( new wxStaticLine(panel1, -1), 0, wxGROW );
280 sizer1->Add( m_canvas, 1, wxGROW | wxALL, 2 );
281 sizer1->Add( new wxStaticLine(panel1, -1), 0, wxGROW );
282 panel1->SetSizer( sizer1 );
283 panel1->SetAutoLayout( TRUE );
284 sizer1->Fit( panel1 );
285
286 sizer2->Add( m_text, 0, wxGROW | wxTOP, 4 );
287 sizer2->Add( slider, 0, wxCENTRE | wxALL, 4 );
288 panel2->SetSizer( sizer2 );
289 panel2->SetAutoLayout( TRUE );
290 sizer2->Fit( panel2 );
291
292 sizer3->Add( panel1, 1, wxGROW );
293 sizer3->Add( panel2, 0, wxGROW );
294 SetSizer( sizer3 );
295 SetAutoLayout( TRUE );
296 sizer3->Fit( this );
297
298 // set minimum frame size
299 sizer3->SetSizeHints( this );
300
301 // navigator frame
302 m_navigator = new LifeNavigator(this);
5a1dca12
GRG
303}
304
305LifeFrame::~LifeFrame()
306{
307 delete m_timer;
5a1dca12
GRG
308}
309
310void LifeFrame::UpdateInfoText()
311{
312 wxString msg;
313
29b07a38 314 msg.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
e0a40292
GRG
315 m_tics,
316 m_topspeed? 0 : m_interval,
317 m_life->GetNumCells());
5a1dca12
GRG
318 m_text->SetLabel(msg);
319}
320
e0a40292
GRG
321// Enable or disable tools and menu entries according to the current
322// state. See also wxEVT_UPDATE_UI events for a slightly different
323// way to do this.
324void LifeFrame::UpdateUI()
325{
a9cf4097 326 // start / stop
e0a40292
GRG
327 GetToolBar()->EnableTool(ID_START, !m_running);
328 GetToolBar()->EnableTool(ID_STOP, m_running);
29b07a38
GRG
329 GetMenuBar()->GetMenu(2)->Enable(ID_START, !m_running);
330 GetMenuBar()->GetMenu(2)->Enable(ID_STEP, !m_running);
331 GetMenuBar()->GetMenu(2)->Enable(ID_STOP, m_running);
a9cf4097
GRG
332
333 // zooming
334 int cellsize = m_canvas->GetCellSize();
335 GetToolBar()->EnableTool(ID_ZOOMIN, cellsize < 32);
336 GetToolBar()->EnableTool(ID_ZOOMOUT, cellsize > 1);
337 GetMenuBar()->GetMenu(1)->Enable(ID_ZOOMIN, cellsize < 32);
338 GetMenuBar()->GetMenu(1)->Enable(ID_ZOOMOUT, cellsize > 1);
e0a40292
GRG
339}
340
5a1dca12
GRG
341// event handlers
342void LifeFrame::OnMenu(wxCommandEvent& event)
343{
344 switch (event.GetId())
345 {
346 case ID_START : OnStart(); break;
29b07a38 347 case ID_STEP : OnStep(); break;
5a1dca12 348 case ID_STOP : OnStop(); break;
29b07a38 349 case ID_SHOWNAV :
087e4f4a 350 {
29b07a38
GRG
351 bool checked = GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV);
352 m_navigator->Show(checked);
e0a40292
GRG
353 break;
354 }
355 case ID_TOPSPEED:
356 {
357 m_running = TRUE;
358 m_topspeed = TRUE;
359 UpdateUI();
360 while (m_running && m_topspeed)
361 {
29b07a38 362 OnStep();
e0a40292
GRG
363 wxYield();
364 }
365 break;
366 }
367 case ID_RESET:
368 {
369 // stop if it was running
5a1dca12
GRG
370 OnStop();
371 m_life->Clear();
e0a40292 372 m_canvas->Recenter(0, 0);
5a1dca12
GRG
373 m_tics = 0;
374 UpdateInfoText();
375 break;
376 }
e0a40292 377 case ID_ABOUT:
5a1dca12 378 {
e0a40292
GRG
379 LifeAboutDialog dialog(this);
380 dialog.ShowModal();
5a1dca12
GRG
381 break;
382 }
383 case ID_EXIT :
384 {
385 // TRUE is to force the frame to close
386 Close(TRUE);
387 break;
388 }
389 }
390}
391
e0a40292 392void LifeFrame::OnClose(wxCloseEvent& WXUNUSED(event))
5a1dca12 393{
e0a40292
GRG
394 // Stop if it was running; this is absolutely needed because
395 // the frame won't be actually destroyed until there are no
396 // more pending events, and this in turn won't ever happen
397 // if the timer is running faster than the window can redraw.
5a1dca12 398 OnStop();
e0a40292 399 Destroy();
5a1dca12
GRG
400}
401
087e4f4a
GRG
402void LifeFrame::OnSamples(wxCommandEvent& WXUNUSED(event))
403{
404 // stop if it was running
405 OnStop();
406
471ed537 407 // dialog box
087e4f4a
GRG
408 LifeSamplesDialog dialog(this);
409
087e4f4a
GRG
410 if (dialog.ShowModal() == wxID_OK)
411 {
e0a40292 412 const LifeShape shape = dialog.GetShape();
087e4f4a
GRG
413
414 // put the shape
e0a40292
GRG
415 m_life->Clear();
416 m_life->SetShape(shape);
087e4f4a 417
e0a40292
GRG
418 // recenter canvas
419 m_canvas->Recenter(0, 0);
087e4f4a
GRG
420 m_tics = 0;
421 UpdateInfoText();
422 }
423}
424
29b07a38
GRG
425void LifeFrame::OnZoom(wxCommandEvent& event)
426{
427 int cellsize = m_canvas->GetCellSize();
428
429 if ((event.GetId() == ID_ZOOMIN) && cellsize < 32)
430 {
431 m_canvas->SetCellSize(cellsize * 2);
432 UpdateUI();
433 }
434 else if ((event.GetId() == ID_ZOOMOUT) && cellsize > 1)
435 {
436 m_canvas->SetCellSize(cellsize / 2);
437 UpdateUI();
438 }
439}
440
441void LifeFrame::OnNavigate(wxCommandEvent& event)
442{
443 Cell c;
444
445 switch (event.GetId())
446 {
447 case ID_NORTH: c = m_life->FindNorth(); break;
448 case ID_SOUTH: c = m_life->FindSouth(); break;
449 case ID_WEST: c = m_life->FindWest(); break;
450 case ID_EAST: c = m_life->FindEast(); break;
451 case ID_CENTER: c = m_life->FindCenter(); break;
452 case ID_ORIGIN: c.i = c.j = 0; break;
453 }
454
455 m_canvas->Recenter(c.i, c.j);
456}
457
458void LifeFrame::OnSlider(wxScrollEvent& event)
459{
460 m_interval = event.GetPosition() * 100;
461
462 if (m_running)
463 {
464 OnStop();
465 OnStart();
466 }
467
468 UpdateInfoText();
469}
470
471void LifeFrame::OnTimer(wxTimerEvent& WXUNUSED(event))
472{
473 OnStep();
474}
475
5a1dca12
GRG
476void LifeFrame::OnStart()
477{
087e4f4a
GRG
478 if (!m_running)
479 {
087e4f4a
GRG
480 m_timer->Start(m_interval);
481 m_running = TRUE;
e0a40292 482 UpdateUI();
087e4f4a 483 }
5a1dca12
GRG
484}
485
486void LifeFrame::OnStop()
487{
087e4f4a
GRG
488 if (m_running)
489 {
087e4f4a
GRG
490 m_timer->Stop();
491 m_running = FALSE;
e0a40292
GRG
492 m_topspeed = FALSE;
493 UpdateUI();
087e4f4a 494 }
5a1dca12
GRG
495}
496
29b07a38 497void LifeFrame::OnStep()
5a1dca12 498{
a36f0f83
GRG
499 if (m_life->NextTic())
500 m_tics++;
501 else
502 OnStop();
5a1dca12 503
e0a40292 504 m_canvas->DrawChanged();
a36f0f83 505 UpdateInfoText();
5a1dca12
GRG
506}
507
a36f0f83 508
5a1dca12 509// --------------------------------------------------------------------------
29b07a38 510// LifeNavigator miniframe
5a1dca12
GRG
511// --------------------------------------------------------------------------
512
29b07a38
GRG
513LifeNavigator::LifeNavigator(wxWindow *parent)
514 : wxMiniFrame(parent, -1,
515 _("Navigation"),
516 wxDefaultPosition,
517 wxDefaultSize,
518 wxCAPTION | wxSIMPLE_BORDER)
5a1dca12 519{
29b07a38
GRG
520 wxPanel *panel = new wxPanel(this, -1);
521 wxBoxSizer *sizer1 = new wxBoxSizer(wxVERTICAL);
522 wxBoxSizer *sizer2 = new wxBoxSizer(wxHORIZONTAL);
523
524 // create bitmaps and masks for the buttons
525 wxBitmap
526 bmpn = wxBITMAP(north),
527 bmpw = wxBITMAP(west),
528 bmpc = wxBITMAP(center),
529 bmpe = wxBITMAP(east),
530 bmps = wxBITMAP(south);
531
532#ifdef __WXMSW__
533 bmpn.SetMask(new wxMask(bmpn, *wxLIGHT_GREY));
534 bmpw.SetMask(new wxMask(bmpw, *wxLIGHT_GREY));
535 bmpc.SetMask(new wxMask(bmpc, *wxLIGHT_GREY));
536 bmpe.SetMask(new wxMask(bmpe, *wxLIGHT_GREY));
537 bmps.SetMask(new wxMask(bmps, *wxLIGHT_GREY));
538#endif
539
540 // create the buttons and attach tooltips to them
541 wxBitmapButton
542 *bn = new wxBitmapButton(panel, ID_NORTH, bmpn),
543 *bw = new wxBitmapButton(panel, ID_WEST , bmpw),
544 *bc = new wxBitmapButton(panel, ID_CENTER, bmpc),
545 *be = new wxBitmapButton(panel, ID_EAST , bmpe),
546 *bs = new wxBitmapButton(panel, ID_SOUTH, bmps);
547
548 bn->SetToolTip(_("Find northernmost cell"));
549 bw->SetToolTip(_("Find westernmost cell"));
550 bc->SetToolTip(_("Find center of mass"));
551 be->SetToolTip(_("Find easternmost cell"));
552 bs->SetToolTip(_("Find southernmost cell"));
553
554 // add buttons to sizers
555 sizer2->Add( bw, 0, wxCENTRE | wxWEST, 4 );
556 sizer2->Add( bc, 0, wxCENTRE);
557 sizer2->Add( be, 0, wxCENTRE | wxEAST, 4 );
558 sizer1->Add( bn, 0, wxCENTRE | wxNORTH, 4 );
559 sizer1->Add( sizer2 );
560 sizer1->Add( bs, 0, wxCENTRE | wxSOUTH, 4 );
561
562 // set the miniframe size
563 panel->SetSizer(sizer1);
564 panel->SetAutoLayout(TRUE);
565 sizer1->Fit(this);
566 sizer1->SetSizeHints(this);
567
568 // move it to a sensible position
569 wxRect parentRect = parent->GetRect();
570 wxSize childSize = GetSize();
571 int x = parentRect.GetX() +
572 parentRect.GetWidth();
573 int y = parentRect.GetY() +
574 (parentRect.GetHeight() - childSize.GetHeight()) / 4;
575 Move(x, y);
576
577 // done
578 Show(TRUE);
579}
580
581void LifeNavigator::OnClose(wxCloseEvent& event)
582{
583 if (event.CanVeto())
584 event.Veto();
585 else
586 Destroy();
587}
588
5a1dca12
GRG
589
590// --------------------------------------------------------------------------
087e4f4a 591// LifeCanvas
5a1dca12
GRG
592// --------------------------------------------------------------------------
593
594// canvas constructor
087e4f4a 595LifeCanvas::LifeCanvas(wxWindow *parent, Life *life, bool interactive)
e0a40292
GRG
596 : wxWindow(parent, -1, wxPoint(0, 0), wxSize(100, 100),
597 wxSUNKEN_BORDER)
5a1dca12 598{
087e4f4a
GRG
599 m_life = life;
600 m_interactive = interactive;
601 m_cellsize = 8;
e0a40292
GRG
602 m_status = MOUSE_NOACTION;
603 m_viewportX = 0;
604 m_viewportY = 0;
605 m_viewportH = 0;
606 m_viewportW = 0;
607
608 if (m_interactive)
609 SetCursor(*wxCROSS_CURSOR);
610
611 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
612 SetBackgroundColour(*wxWHITE);
5a1dca12
GRG
613}
614
615LifeCanvas::~LifeCanvas()
616{
e0a40292 617 delete m_life;
5a1dca12
GRG
618}
619
e0a40292
GRG
620// recenter at the given position
621void LifeCanvas::Recenter(wxInt32 i, wxInt32 j)
5a1dca12 622{
e0a40292
GRG
623 m_viewportX = i - m_viewportW / 2;
624 m_viewportY = j - m_viewportH / 2;
5a1dca12 625
087e4f4a 626 // redraw everything
e0a40292 627 Refresh(FALSE);
5a1dca12
GRG
628}
629
e0a40292
GRG
630// set the cell size and refresh display
631void LifeCanvas::SetCellSize(int cellsize)
5a1dca12 632{
e0a40292
GRG
633 m_cellsize = cellsize;
634
635 // find current center
636 wxInt32 cx = m_viewportX + m_viewportW / 2;
637 wxInt32 cy = m_viewportY + m_viewportH / 2;
638
639 // get current canvas size and adjust viewport accordingly
2fa7c206 640 int w, h;
e0a40292
GRG
641 GetClientSize(&w, &h);
642 m_viewportW = (w + m_cellsize - 1) / m_cellsize;
643 m_viewportH = (h + m_cellsize - 1) / m_cellsize;
644
645 // recenter
646 m_viewportX = cx - m_viewportW / 2;
647 m_viewportY = cy - m_viewportH / 2;
648
649 // adjust scrollbars
650 if (m_interactive)
651 {
652 SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW);
653 SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH);
654 m_thumbX = m_viewportW;
655 m_thumbY = m_viewportH;
656 }
657
658 Refresh(FALSE);
659}
2480be69 660
e0a40292
GRG
661// draw a cell
662void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, bool alive)
663{
664 wxClientDC dc(this);
5a1dca12 665
e0a40292
GRG
666 dc.SetPen(alive? *wxBLACK_PEN : *wxWHITE_PEN);
667 dc.SetBrush(alive? *wxBLACK_BRUSH : *wxWHITE_BRUSH);
5a1dca12 668
e0a40292
GRG
669 dc.BeginDrawing();
670 DrawCell(i, j, dc);
5a1dca12 671 dc.EndDrawing();
5a1dca12
GRG
672}
673
e0a40292 674void LifeCanvas::DrawCell(wxInt32 i, wxInt32 j, wxDC &dc)
5a1dca12 675{
e0a40292
GRG
676 wxCoord x = CellToX(i);
677 wxCoord y = CellToY(j);
5a1dca12 678
e0a40292
GRG
679 // if cellsize is 1 or 2, there will be no grid
680 switch (m_cellsize)
681 {
682 case 1:
683 dc.DrawPoint(x, y);
684 break;
685 case 2:
686 dc.DrawRectangle(x, y, 2, 2);
687 break;
688 default:
689 dc.DrawRectangle(x + 1, y + 1, m_cellsize - 1, m_cellsize - 1);
690 }
5a1dca12
GRG
691}
692
e0a40292
GRG
693// draw all changed cells
694void LifeCanvas::DrawChanged()
5a1dca12 695{
e0a40292
GRG
696 wxClientDC dc(this);
697
698 size_t ncells;
699 Cell *cells;
700 bool done = FALSE;
701
702 m_life->BeginFind(m_viewportX,
703 m_viewportY,
704 m_viewportX + m_viewportW,
705 m_viewportY + m_viewportH,
706 TRUE);
707
708 dc.BeginDrawing();
e0a40292
GRG
709
710 if (m_cellsize == 1)
5a1dca12
GRG
711 {
712 dc.SetPen(*wxBLACK_PEN);
5a1dca12
GRG
713 }
714 else
715 {
e0a40292
GRG
716 dc.SetPen(*wxTRANSPARENT_PEN);
717 dc.SetBrush(*wxBLACK_BRUSH);
718 }
030d06e1 719 dc.SetLogicalFunction(wxINVERT);
e0a40292
GRG
720
721 while (!done)
722 {
723 done = m_life->FindMore(&cells, &ncells);
724
725 for (size_t m = 0; m < ncells; m++)
726 DrawCell(cells[m].i, cells[m].j, dc);
5a1dca12 727 }
e0a40292 728 dc.EndDrawing();
5a1dca12 729}
ecbdd409 730
5a1dca12
GRG
731// event handlers
732void LifeCanvas::OnPaint(wxPaintEvent& event)
733{
734 wxPaintDC dc(this);
e0a40292
GRG
735 wxRect rect = GetUpdateRegion().GetBox();
736 wxCoord x, y, w, h;
737 wxInt32 i0, j0, i1, j1;
738
739 // find damaged area
740 x = rect.GetX();
741 y = rect.GetY();
742 w = rect.GetWidth();
743 h = rect.GetHeight();
744
745 i0 = XToCell(x);
746 j0 = YToCell(y);
747 i1 = XToCell(x + w - 1);
748 j1 = YToCell(y + h - 1);
749
750 size_t ncells;
751 Cell *cells;
752 bool done = FALSE;
5a1dca12 753
e0a40292
GRG
754 m_life->BeginFind(i0, j0, i1, j1, FALSE);
755 done = m_life->FindMore(&cells, &ncells);
5a1dca12 756
e0a40292 757 // erase all damaged cells and draw the grid
5a1dca12 758 dc.BeginDrawing();
e0a40292 759 dc.SetBrush(*wxWHITE_BRUSH);
5a1dca12 760
e0a40292 761 if (m_cellsize <= 2)
5a1dca12 762 {
e0a40292
GRG
763 // no grid
764 dc.SetPen(*wxWHITE_PEN);
765 dc.DrawRectangle(x, y, w, h);
5a1dca12 766 }
e0a40292
GRG
767 else
768 {
769 x = CellToX(i0);
770 y = CellToY(j0);
771 w = CellToX(i1 + 1) - x + 1;
772 h = CellToY(j1 + 1) - y + 1;
773
774 dc.SetPen(*wxLIGHT_GREY_PEN);
775 for (wxInt32 yy = y; yy <= (y + h - m_cellsize); yy += m_cellsize)
776 dc.DrawRectangle(x, yy, w, m_cellsize + 1);
777 for (wxInt32 xx = x; xx <= (x + w - m_cellsize); xx += m_cellsize)
778 dc.DrawLine(xx, y, xx, y + h);
779 }
780
781 // draw all alive cells
782 dc.SetPen(*wxBLACK_PEN);
783 dc.SetBrush(*wxBLACK_BRUSH);
784
785 while (!done)
786 {
787 for (size_t m = 0; m < ncells; m++)
788 DrawCell(cells[m].i, cells[m].j, dc);
789
790 done = m_life->FindMore(&cells, &ncells);
791 }
792
793 // last set
794 for (size_t m = 0; m < ncells; m++)
795 DrawCell(cells[m].i, cells[m].j, dc);
5a1dca12 796
5a1dca12
GRG
797 dc.EndDrawing();
798}
799
800void LifeCanvas::OnMouse(wxMouseEvent& event)
801{
087e4f4a
GRG
802 if (!m_interactive)
803 return;
804
5a1dca12 805 // which cell are we pointing at?
e0a40292
GRG
806 wxInt32 i = XToCell( event.GetX() );
807 wxInt32 j = YToCell( event.GetY() );
808
809 // set statusbar text
810 wxString msg;
811 msg.Printf(_("Cell: (%d, %d)"), i, j);
29b07a38 812 ((LifeFrame *) wxGetApp().GetTopWindow())->SetStatusText(msg, 1);
5a1dca12
GRG
813
814 // button pressed?
815 if (!event.LeftIsDown())
816 {
817 m_status = MOUSE_NOACTION;
7989fb37 818 return;
5a1dca12 819 }
5a1dca12 820
7989fb37
GRG
821 // button just pressed?
822 if (m_status == MOUSE_NOACTION)
823 {
824 // yes, update status and toggle this cell
825 m_status = (m_life->IsAlive(i, j)? MOUSE_ERASING : MOUSE_DRAWING);
826
827 m_mi = i;
828 m_mj = j;
829 m_life->SetCell(i, j, m_status == MOUSE_DRAWING);
830 DrawCell(i, j, m_status == MOUSE_DRAWING);
831 }
832 else if ((m_mi != i) || (m_mj != j))
833 {
5e9ff6ad
GRG
834 bool alive = (m_status == MOUSE_DRAWING);
835
836 // prepare DC and pen + brush to optimize drawing
837 wxClientDC dc(this);
838 dc.SetPen(alive? *wxBLACK_PEN : *wxWHITE_PEN);
839 dc.SetBrush(alive? *wxBLACK_BRUSH : *wxWHITE_BRUSH);
840 dc.BeginDrawing();
841
7989fb37
GRG
842 // draw a line of cells using Bresenham's algorithm
843 wxInt32 d, ii, jj, di, ai, si, dj, aj, sj;
844 di = i - m_mi;
845 ai = abs(di) << 1;
846 si = (di < 0)? -1 : 1;
847 dj = j - m_mj;
848 aj = abs(dj) << 1;
849 sj = (dj < 0)? -1 : 1;
850
851 ii = m_mi;
852 jj = m_mj;
853
854 if (ai > aj)
855 {
856 // iterate over i
857 d = aj - (ai >> 1);
858
859 while (ii != i)
860 {
5e9ff6ad
GRG
861 m_life->SetCell(ii, jj, alive);
862 DrawCell(ii, jj, dc);
7989fb37
GRG
863 if (d >= 0)
864 {
865 jj += sj;
866 d -= ai;
867 }
868 ii += si;
869 d += aj;
870 }
871 }
872 else
5a1dca12 873 {
7989fb37
GRG
874 // iterate over j
875 d = ai - (aj >> 1);
876
877 while (jj != j)
878 {
5e9ff6ad
GRG
879 m_life->SetCell(ii, jj, alive);
880 DrawCell(ii, jj, dc);
7989fb37
GRG
881 if (d >= 0)
882 {
883 ii += si;
884 d -= aj;
885 }
886 jj += sj;
887 d += ai;
888 }
5a1dca12 889 }
7989fb37
GRG
890
891 // last cell
5e9ff6ad
GRG
892 m_life->SetCell(ii, jj, alive);
893 DrawCell(ii, jj, dc);
7989fb37
GRG
894 m_mi = ii;
895 m_mj = jj;
5e9ff6ad
GRG
896
897 dc.EndDrawing();
5a1dca12 898 }
7989fb37 899
29b07a38 900 ((LifeFrame *) wxGetApp().GetTopWindow())->UpdateInfoText();
5a1dca12
GRG
901}
902
903void LifeCanvas::OnSize(wxSizeEvent& event)
904{
e0a40292
GRG
905 // find center
906 wxInt32 cx = m_viewportX + m_viewportW / 2;
907 wxInt32 cy = m_viewportY + m_viewportH / 2;
908
909 // get new size
5a1dca12
GRG
910 wxCoord w = event.GetSize().GetX();
911 wxCoord h = event.GetSize().GetY();
e0a40292
GRG
912 m_viewportW = (w + m_cellsize - 1) / m_cellsize;
913 m_viewportH = (h + m_cellsize - 1) / m_cellsize;
914
915 // recenter
916 m_viewportX = cx - m_viewportW / 2;
917 m_viewportY = cy - m_viewportH / 2;
918
919 // scrollbars
920 if (m_interactive)
921 {
922 SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW);
923 SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH);
924 m_thumbX = m_viewportW;
925 m_thumbY = m_viewportH;
926 }
5a1dca12
GRG
927
928 // allow default processing
929 event.Skip();
930}
e0a40292
GRG
931
932void LifeCanvas::OnScroll(wxScrollWinEvent& event)
933{
934 WXTYPE type = event.GetEventType();
935 int pos = event.GetPosition();
936 int orient = event.GetOrientation();
e0a40292
GRG
937
938 // calculate scroll increment
33e39147 939 int scrollinc = 0;
e0a40292
GRG
940 switch (type)
941 {
942 case wxEVT_SCROLLWIN_TOP:
943 {
944 if (orient == wxHORIZONTAL)
945 scrollinc = -m_viewportW;
946 else
947 scrollinc = -m_viewportH;
948 break;
949 }
950 case wxEVT_SCROLLWIN_BOTTOM:
951 {
952 if (orient == wxHORIZONTAL)
953 scrollinc = m_viewportW;
954 else
955 scrollinc = m_viewportH;
956 break;
957 }
958 case wxEVT_SCROLLWIN_LINEUP: scrollinc = -1; break;
959 case wxEVT_SCROLLWIN_LINEDOWN: scrollinc = +1; break;
960 case wxEVT_SCROLLWIN_PAGEUP: scrollinc = -10; break;
961 case wxEVT_SCROLLWIN_PAGEDOWN: scrollinc = +10; break;
962 case wxEVT_SCROLLWIN_THUMBTRACK:
963 {
33e39147 964 if (orient == wxHORIZONTAL)
e0a40292 965 {
33e39147
GRG
966 scrollinc = pos - m_thumbX;
967 m_thumbX = pos;
e0a40292
GRG
968 }
969 else
970 {
33e39147
GRG
971 scrollinc = pos - m_thumbY;
972 m_thumbY = pos;
e0a40292
GRG
973 }
974 break;
975 }
33e39147
GRG
976 case wxEVT_SCROLLWIN_THUMBRELEASE:
977 {
978 m_thumbX = m_viewportW;
979 m_thumbY = m_viewportH;
980 }
e0a40292
GRG
981 }
982
c17e4955 983#if defined(__WXGTK__) || defined(__WXMOTIF__) // what about Motif?
e0a40292 984 // wxGTK updates the thumb automatically (wxMSW doesn't); reset it back
33e39147 985 if (type != wxEVT_SCROLLWIN_THUMBTRACK)
e0a40292
GRG
986 {
987 SetScrollbar(wxHORIZONTAL, m_viewportW, m_viewportW, 3 * m_viewportW);
988 SetScrollbar(wxVERTICAL, m_viewportH, m_viewportH, 3 * m_viewportH);
989 }
990#endif
991
992 if (scrollinc == 0) return;
993
994 // scroll the window and adjust the viewport
995 if (orient == wxHORIZONTAL)
996 {
997 m_viewportX += scrollinc;
998 ScrollWindow( -m_cellsize * scrollinc, 0, (const wxRect *) NULL);
999 }
1000 else
1001 {
1002 m_viewportY += scrollinc;
1003 ScrollWindow( 0, -m_cellsize * scrollinc, (const wxRect *) NULL);
1004 }
1005}
1006
1007void LifeCanvas::OnEraseBackground(wxEraseEvent& WXUNUSED(event))
1008{
1009 // do nothing. I just don't want the background to be erased, you know.
1010}
29b07a38
GRG
1011
1012