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