1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: The game of Life, created by J. H. Conway
4 // Author: Guillermo Rodriguez Garcia, <guille@iies.es>
8 // Copyright: (c) 2000, Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ==========================================================================
13 // headers, declarations, constants
14 // ==========================================================================
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/statline.h"
28 #include "wx/wfstream.h"
29 #include "wx/filedlg.h"
30 #include "wx/stockitem.h"
37 // --------------------------------------------------------------------------
39 // --------------------------------------------------------------------------
41 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
43 #include "mondrian.xpm"
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"
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"
62 // --------------------------------------------------------------------------
64 // --------------------------------------------------------------------------
66 // IDs for the controls and the menu commands. Exluding those already defined
67 // by wxWidgets, such as wxID_NEW.
71 ID_TIMER
= wxID_HIGHEST
,
91 // speed selection slider
95 // --------------------------------------------------------------------------
96 // event tables and other macros for wxWidgets
97 // --------------------------------------------------------------------------
100 BEGIN_EVENT_TABLE(LifeFrame
, wxFrame
)
101 EVT_MENU (wxID_NEW
, LifeFrame::OnMenu
)
103 EVT_MENU (wxID_OPEN
, LifeFrame::OnOpen
)
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
)
127 BEGIN_EVENT_TABLE(LifeNavigator
, wxMiniFrame
)
128 EVT_CLOSE ( LifeNavigator::OnClose
)
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
)
143 // Create a new application object
144 IMPLEMENT_APP(LifeApp
)
147 // ==========================================================================
149 // ==========================================================================
152 #define ADD_TOOL(id, bmp, tooltip, help) \
153 toolBar->AddTool(id, bmp, wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, (wxObject *)NULL, tooltip, help)
156 // --------------------------------------------------------------------------
158 // --------------------------------------------------------------------------
160 // 'Main program' equivalent: the program execution "starts" here
161 bool LifeApp::OnInit()
163 // create the main application window
164 LifeFrame
*frame
= new LifeFrame();
166 // show it and tell the application that it's our main window
172 frame
->UpdateInfoText();
175 // enter the main message loop and run the app
179 // --------------------------------------------------------------------------
181 // --------------------------------------------------------------------------
184 LifeFrame::LifeFrame() :
185 wxFrame( (wxFrame
*) NULL
, wxID_ANY
, _("Life!"), wxDefaultPosition
),
189 SetIcon(wxICON(mondrian
));
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
);
197 menuFile
->Append(wxID_NEW
, wxGetStockLabel(wxID_NEW
), _("Start a new game"));
199 menuFile
->Append(wxID_OPEN
, wxGetStockLabel(wxID_OPEN
), _("Open an existing Life pattern"));
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"));
206 menuView
->Append(ID_SHOWNAV
, _("Navigation &toolbox"), _("Show or hide toolbox"), wxITEM_CHECK
);
207 menuView
->Check(ID_SHOWNAV
, true);
208 menuView
->AppendSeparator();
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"));
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"));
229 menuHelp
->Append(wxID_ABOUT
, _("&About\tCtrl-A"), _("Show about dialog"));
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"));
239 wxBitmap tbBitmaps
[7];
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
);
249 wxToolBar
*toolBar
= CreateToolBar();
250 toolBar
->SetMargins(5, 5);
251 toolBar
->SetToolBitmapSize(wxSize(16, 16));
253 ADD_TOOL(wxID_NEW
, tbBitmaps
[0], wxGetStockLabel(wxID_NEW
, false), _("Start a new game"));
256 ADD_TOOL(wxID_OPEN
, tbBitmaps
[1], wxGetStockLabel(wxID_OPEN
, false), _("Open an existing Life pattern"));
257 #endif // wxUSE_FILEDLG
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"));
269 toolBar
->EnableTool(wxID_STOP
, false); // must be after Realize() !
274 SetStatusText(_("Welcome to Life!"));
275 #endif // wxUSE_STATUSBAR
279 m_timer
= new wxTimer(this, ID_TIMER
);
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.
290 wxPanel
*panel1
= new wxPanel(this, wxID_ANY
);
291 wxPanel
*panel2
= new wxPanel(this, wxID_ANY
);
294 m_canvas
= new LifeCanvas(panel1
, m_life
);
297 m_text
= new wxStaticText(panel2
, wxID_ANY
,
301 wxALIGN_CENTER
| wxST_NO_AUTORESIZE
);
303 wxSlider
*slider
= new wxSlider(panel2
, ID_SLIDER
,
306 wxSize(200, wxDefaultCoord
),
307 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
312 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
313 wxBoxSizer
*sizer2
= new wxBoxSizer(wxVERTICAL
);
314 wxBoxSizer
*sizer3
= new wxBoxSizer(wxVERTICAL
);
317 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
318 #endif // wxUSE_STATLINE
319 sizer1
->Add( m_canvas
, 1, wxGROW
| wxALL
, 2 );
321 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
322 #endif // wxUSE_STATLINE
323 panel1
->SetSizer( sizer1
);
324 sizer1
->Fit( panel1
);
326 sizer2
->Add( m_text
, 0, wxGROW
| wxTOP
, 4 );
327 sizer2
->Add( slider
, 0, wxCENTRE
| wxALL
, 4 );
329 panel2
->SetSizer( sizer2
);
330 sizer2
->Fit( panel2
);
332 sizer3
->Add( panel1
, 1, wxGROW
);
333 sizer3
->Add( panel2
, 0, wxGROW
);
339 // set minimum frame size
340 sizer3
->SetSizeHints( this );
342 // navigator frame - not appropriate for small devices
343 m_navigator
= new LifeNavigator(this);
348 LifeFrame::~LifeFrame()
353 void LifeFrame::UpdateInfoText()
357 msg
.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
359 m_topspeed
? 0 : m_interval
,
360 m_life
->GetNumCells());
361 m_text
->SetLabel(msg
);
364 // Enable or disable tools and menu entries according to the current
365 // state. See also wxEVT_UPDATE_UI events for a slightly different
367 void LifeFrame::UpdateUI()
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
);
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);
385 // Event handlers -----------------------------------------------------------
387 // OnMenu handles all events which don't have their own event handler
388 void LifeFrame::OnMenu(wxCommandEvent
& event
)
390 switch (event
.GetId())
394 // stop if it was running
397 m_canvas
->Recenter(0, 0);
404 LifeAboutDialog
dialog(this);
410 // true is to force the frame to close
416 bool checked
= GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV
);
418 m_navigator
->Show(checked
);
423 wxString desc
= m_life
->GetDescription();
426 desc
= _("Not available");
428 // should we make the description editable here?
429 wxMessageBox(desc
, _("Description"), wxOK
| wxICON_INFORMATION
);
433 case ID_START
: OnStart(); break;
434 case ID_STEP
: OnStep(); break;
435 case wxID_STOP
: OnStop(); break;
441 while (m_running
&& m_topspeed
)
452 void LifeFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
454 wxFileDialog
filedlg(this,
455 _("Choose a file to open"),
458 _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"),
459 wxOPEN
| wxFILE_MUST_EXIST
);
461 if (filedlg
.ShowModal() == wxID_OK
)
463 wxFileInputStream
stream(filedlg
.GetPath());
464 LifeReader
reader(stream
);
466 // the reader handles errors itself, no need to do anything here
469 // stop if running and put the pattern
472 m_life
->SetPattern(reader
.GetPattern());
475 m_canvas
->Recenter(0, 0);
483 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
485 // stop if it was running
489 LifeSamplesDialog
dialog(this);
491 if (dialog
.ShowModal() == wxID_OK
)
493 const LifePattern pattern
= dialog
.GetPattern();
497 m_life
->SetPattern(pattern
);
500 m_canvas
->Recenter(0, 0);
506 void LifeFrame::OnZoom(wxCommandEvent
& event
)
508 int cellsize
= m_canvas
->GetCellSize();
510 if ((event
.GetId() == wxID_ZOOM_IN
) && cellsize
< 32)
512 m_canvas
->SetCellSize(cellsize
* 2);
515 else if ((event
.GetId() == wxID_ZOOM_OUT
) && cellsize
> 1)
517 m_canvas
->SetCellSize(cellsize
/ 2);
522 void LifeFrame::OnNavigate(wxCommandEvent
& event
)
526 switch (event
.GetId())
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;
536 case ID_ORIGIN
: c
.i
= c
.j
= 0; break;
539 m_canvas
->Recenter(c
.i
, c
.j
);
542 void LifeFrame::OnSlider(wxScrollEvent
& event
)
544 m_interval
= event
.GetPosition() * 100;
555 void LifeFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
560 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
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.
570 void LifeFrame::OnStart()
574 m_timer
->Start(m_interval
);
580 void LifeFrame::OnStop()
591 void LifeFrame::OnStep()
593 if (m_life
->NextTic())
598 m_canvas
->DrawChanged();
603 // --------------------------------------------------------------------------
604 // LifeNavigator miniframe
605 // --------------------------------------------------------------------------
607 LifeNavigator::LifeNavigator(wxWindow
*parent
)
608 : wxMiniFrame(parent
, wxID_ANY
,
612 wxCAPTION
| wxSIMPLE_BORDER
)
614 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
615 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
616 wxBoxSizer
*sizer2
= new wxBoxSizer(wxHORIZONTAL
);
618 // create bitmaps and masks for the buttons
620 bmpn
= wxBITMAP(north
),
621 bmpw
= wxBITMAP(west
),
622 bmpc
= wxBITMAP(center
),
623 bmpe
= wxBITMAP(east
),
624 bmps
= wxBITMAP(south
);
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
));
634 // create the buttons and attach tooltips to them
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
);
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"));
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 );
658 // set the panel and miniframe size
659 panel
->SetSizer(sizer1
);
662 SetClientSize(panel
->GetSize());
663 wxSize sz
= GetSize();
664 SetSizeHints(sz
.x
, sz
.y
, sz
.x
, sz
.y
);
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;
679 void LifeNavigator::OnClose(wxCloseEvent
& event
)
689 // --------------------------------------------------------------------------
691 // --------------------------------------------------------------------------
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__)
705 m_interactive
= interactive
;
707 m_status
= MOUSE_NOACTION
;
714 SetCursor(*wxCROSS_CURSOR
);
716 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
717 SetBackgroundColour(*wxWHITE
);
720 LifeCanvas::~LifeCanvas()
725 // recenter at the given position
726 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
728 m_viewportX
= i
- m_viewportW
/ 2;
729 m_viewportY
= j
- m_viewportH
/ 2;
735 // set the cell size and refresh display
736 void LifeCanvas::SetCellSize(int cellsize
)
738 m_cellsize
= cellsize
;
740 // find current center
741 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
742 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
744 // get current canvas size and adjust viewport accordingly
746 GetClientSize(&w
, &h
);
747 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
748 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
751 m_viewportX
= cx
- m_viewportW
/ 2;
752 m_viewportY
= cy
- m_viewportH
/ 2;
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
;
767 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
771 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
772 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
777 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
779 wxCoord x
= CellToX(i
);
780 wxCoord y
= CellToY(j
);
782 // if cellsize is 1 or 2, there will be no grid
789 dc
.DrawRectangle(x
, y
, 2, 2);
792 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
796 // draw all changed cells
797 void LifeCanvas::DrawChanged()
805 m_life
->BeginFind(m_viewportX
,
807 m_viewportX
+ m_viewportW
,
808 m_viewportY
+ m_viewportH
,
813 dc
.SetPen(*wxBLACK_PEN
);
817 dc
.SetPen(*wxTRANSPARENT_PEN
);
818 dc
.SetBrush(*wxBLACK_BRUSH
);
820 dc
.SetLogicalFunction(wxINVERT
);
824 done
= m_life
->FindMore(&cells
, &ncells
);
826 for (size_t m
= 0; m
< ncells
; m
++)
827 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
832 void LifeCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
835 wxRect rect
= GetUpdateRegion().GetBox();
837 wxInt32 i0
, j0
, i1
, j1
;
843 h
= rect
.GetHeight();
847 i1
= XToCell(x
+ w
- 1);
848 j1
= YToCell(y
+ h
- 1);
853 m_life
->BeginFind(i0
, j0
, i1
, j1
, false);
854 bool done
= m_life
->FindMore(&cells
, &ncells
);
856 // erase all damaged cells and draw the grid
857 dc
.SetBrush(*wxWHITE_BRUSH
);
862 dc
.SetPen(*wxWHITE_PEN
);
863 dc
.DrawRectangle(x
, y
, w
, h
);
869 w
= CellToX(i1
+ 1) - x
+ 1;
870 h
= CellToY(j1
+ 1) - y
+ 1;
872 dc
.SetPen(*wxLIGHT_GREY_PEN
);
873 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
874 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
875 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
876 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
879 // draw all alive cells
880 dc
.SetPen(*wxBLACK_PEN
);
881 dc
.SetBrush(*wxBLACK_BRUSH
);
885 for (size_t m
= 0; m
< ncells
; m
++)
886 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
888 done
= m_life
->FindMore(&cells
, &ncells
);
892 for (size_t m
= 0; m
< ncells
; m
++)
893 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
896 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
901 // which cell are we pointing at?
902 wxInt32 i
= XToCell( event
.GetX() );
903 wxInt32 j
= YToCell( event
.GetY() );
906 // set statusbar text
908 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
909 ((LifeFrame
*) wxGetApp().GetTopWindow())->SetStatusText(msg
, 1);
910 #endif // wxUSE_STATUSBAR
912 // NOTE that wxMouseEvent::LeftDown() and wxMouseEvent::LeftIsDown()
913 // have different semantics. The first one is used to signal that the
914 // button was just pressed (i.e., in "button down" events); the second
915 // one just describes the current status of the button, independently
916 // of the mouse event type. LeftIsDown is typically used in "mouse
917 // move" events, to test if the button is _still_ pressed.
919 // is the button down?
920 if (!event
.LeftIsDown())
922 m_status
= MOUSE_NOACTION
;
926 // was it pressed just now?
927 if (event
.LeftDown())
929 // yes: start a new action and toggle this cell
930 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
934 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
935 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
937 else if ((m_mi
!= i
) || (m_mj
!= j
))
939 // no: continue ongoing action
940 bool alive
= (m_status
== MOUSE_DRAWING
);
942 // prepare DC and pen + brush to optimize drawing
944 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
945 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
947 // draw a line of cells using Bresenham's algorithm
948 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
951 si
= (di
< 0)? -1 : 1;
954 sj
= (dj
< 0)? -1 : 1;
966 m_life
->SetCell(ii
, jj
, alive
);
967 DrawCell(ii
, jj
, dc
);
984 m_life
->SetCell(ii
, jj
, alive
);
985 DrawCell(ii
, jj
, dc
);
997 m_life
->SetCell(ii
, jj
, alive
);
998 DrawCell(ii
, jj
, dc
);
1003 ((LifeFrame
*) wxGetApp().GetTopWindow())->UpdateInfoText();
1006 void LifeCanvas::OnSize(wxSizeEvent
& event
)
1009 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
1010 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
1013 wxCoord w
= event
.GetSize().GetX();
1014 wxCoord h
= event
.GetSize().GetY();
1015 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
1016 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
1019 m_viewportX
= cx
- m_viewportW
/ 2;
1020 m_viewportY
= cy
- m_viewportH
/ 2;
1025 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1026 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1027 m_thumbX
= m_viewportW
;
1028 m_thumbY
= m_viewportH
;
1031 // allow default processing
1035 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
1037 WXTYPE type
= (WXTYPE
)event
.GetEventType();
1038 int pos
= event
.GetPosition();
1039 int orient
= event
.GetOrientation();
1041 // calculate scroll increment
1043 if (type
== wxEVT_SCROLLWIN_TOP
)
1045 if (orient
== wxHORIZONTAL
)
1046 scrollinc
= -m_viewportW
;
1048 scrollinc
= -m_viewportH
;
1051 if (type
== wxEVT_SCROLLWIN_BOTTOM
)
1053 if (orient
== wxHORIZONTAL
)
1054 scrollinc
= m_viewportW
;
1056 scrollinc
= m_viewportH
;
1059 if (type
== wxEVT_SCROLLWIN_LINEUP
)
1064 if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
1069 if (type
== wxEVT_SCROLLWIN_PAGEUP
)
1074 if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
1079 if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
1081 if (orient
== wxHORIZONTAL
)
1083 scrollinc
= pos
- m_thumbX
;
1088 scrollinc
= pos
- m_thumbY
;
1093 if (type
== wxEVT_SCROLLWIN_THUMBRELEASE
)
1095 m_thumbX
= m_viewportW
;
1096 m_thumbY
= m_viewportH
;
1099 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1100 // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't);
1101 // so reset it back as we always want it to be in the same position.
1102 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
1104 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1105 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1109 if (scrollinc
== 0) return;
1111 // scroll the window and adjust the viewport
1112 if (orient
== wxHORIZONTAL
)
1114 m_viewportX
+= scrollinc
;
1115 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
1119 m_viewportY
+= scrollinc
;
1120 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
1124 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
1126 // do nothing. I just don't want the background to be erased, you know.