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