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 // ==========================================================================
17 #pragma implementation "life.h"
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/statline.h"
37 // --------------------------------------------------------------------------
39 // --------------------------------------------------------------------------
41 #if defined(__WXGTK__) || defined(__WXMOTIF__)
43 #include "mondrian.xpm"
45 // bitmap buttons for the toolbar
46 #include "bitmaps/reset.xpm"
47 #include "bitmaps/play.xpm"
48 #include "bitmaps/stop.xpm"
49 #include "bitmaps/zoomin.xpm"
50 #include "bitmaps/zoomout.xpm"
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"
60 // --------------------------------------------------------------------------
62 // --------------------------------------------------------------------------
64 // IDs for the controls and the menu commands
70 // menu items and toolbar buttons
82 // speed selection slider
95 // --------------------------------------------------------------------------
96 // event tables and other macros for wxWindows
97 // --------------------------------------------------------------------------
100 BEGIN_EVENT_TABLE(LifeFrame
, wxFrame
)
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
)
105 EVT_MENU (ID_START
, LifeFrame::OnMenu
)
106 EVT_MENU (ID_STEP
, LifeFrame::OnMenu
)
107 EVT_MENU (ID_STOP
, LifeFrame::OnMenu
)
108 EVT_MENU (ID_TOPSPEED
, LifeFrame::OnMenu
)
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
)
118 EVT_COMMAND_SCROLL (ID_SLIDER
, LifeFrame::OnSlider
)
119 EVT_TIMER (ID_TIMER
, LifeFrame::OnTimer
)
120 EVT_CLOSE ( LifeFrame::OnClose
)
123 BEGIN_EVENT_TABLE(LifeNavigator
, wxMiniFrame
)
124 EVT_CLOSE ( LifeNavigator::OnClose
)
127 BEGIN_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
)
136 // Create a new application object
137 IMPLEMENT_APP(LifeApp
)
140 // ==========================================================================
142 // ==========================================================================
145 #define ADD_TOOL(id, bmp, tooltip, help) \
146 toolBar->AddTool(id, bmp, wxNullBitmap, FALSE, -1, -1, (wxObject *)0, tooltip, help)
149 // --------------------------------------------------------------------------
151 // --------------------------------------------------------------------------
153 // 'Main program' equivalent: the program execution "starts" here
154 bool LifeApp::OnInit()
156 // create the main application window
157 LifeFrame
*frame
= new LifeFrame();
159 // show it and tell the application that it's our main window
163 // enter the main message loop and run the app
167 // --------------------------------------------------------------------------
169 // --------------------------------------------------------------------------
172 LifeFrame::LifeFrame() : wxFrame((wxFrame
*)0, -1, _("Life!"), wxPoint(200, 200))
175 SetIcon(wxICON(mondrian
));
178 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
179 wxMenu
*menuView
= new wxMenu("", wxMENU_TEAROFF
);
180 wxMenu
*menuGame
= new wxMenu("", wxMENU_TEAROFF
);
182 menuFile
->Append(ID_RESET
, _("Reset"), _("Start a new game"));
183 menuFile
->Append(ID_SAMPLES
, _("Sample game..."), _("Select a sample configuration"));
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"));
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"));
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();
207 menuGame
->Append(ID_TOPSPEED
, _("Top speed!"), _("Go as fast as possible"));
209 wxMenuBar
*menuBar
= new wxMenuBar();
210 menuBar
->Append(menuFile
, _("&File"));
211 menuBar
->Append(menuView
, _("&View"));
212 menuBar
->Append(menuGame
, _("&Game"));
216 wxBitmap tbBitmaps
[5];
218 tbBitmaps
[0] = wxBITMAP(reset
);
219 tbBitmaps
[1] = wxBITMAP(play
);
220 tbBitmaps
[2] = wxBITMAP(stop
);
221 tbBitmaps
[3] = wxBITMAP(zoomin
);
222 tbBitmaps
[4] = wxBITMAP(zoomout
);
224 wxToolBar
*toolBar
= CreateToolBar();
225 toolBar
->SetMargins(5, 5);
226 toolBar
->SetToolBitmapSize(wxSize(16, 16));
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"));
230 toolBar
->AddSeparator();
231 ADD_TOOL(ID_ZOOMIN
, tbBitmaps
[3], _("Zoom in"), _("Zoom in"));
232 ADD_TOOL(ID_ZOOMOUT
, tbBitmaps
[4], _("Zoom out"), _("Zoom out"));
234 toolBar
->EnableTool(ID_STOP
, FALSE
); // must be after Realize() !
238 SetStatusText(_("Welcome to Life!"));
242 m_timer
= new wxTimer(this, ID_TIMER
);
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.
253 wxPanel
*panel1
= new wxPanel(this, -1);
254 wxPanel
*panel2
= new wxPanel(this, -1);
257 m_canvas
= new LifeCanvas(panel1
, m_life
);
260 m_text
= new wxStaticText(panel2
, -1,
264 wxALIGN_CENTER
| wxST_NO_AUTORESIZE
);
266 wxSlider
*slider
= new wxSlider(panel2
, ID_SLIDER
,
270 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
275 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
276 wxBoxSizer
*sizer2
= new wxBoxSizer(wxVERTICAL
);
277 wxBoxSizer
*sizer3
= new wxBoxSizer(wxVERTICAL
);
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
);
286 sizer2
->Add( m_text
, 0, wxGROW
| wxTOP
, 4 );
287 sizer2
->Add( slider
, 0, wxCENTRE
| wxALL
, 4 );
289 panel2
->SetSizer( sizer2
);
290 panel2
->SetAutoLayout( TRUE
);
291 sizer2
->Fit( panel2
);
293 sizer3
->Add( panel1
, 1, wxGROW
);
294 sizer3
->Add( panel2
, 0, wxGROW
);
296 SetAutoLayout( TRUE
);
299 // set minimum frame size
300 sizer3
->SetSizeHints( this );
303 m_navigator
= new LifeNavigator(this);
306 LifeFrame::~LifeFrame()
311 void LifeFrame::UpdateInfoText()
315 msg
.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
317 m_topspeed
? 0 : m_interval
,
318 m_life
->GetNumCells());
319 m_text
->SetLabel(msg
);
322 // Enable or disable tools and menu entries according to the current
323 // state. See also wxEVT_UPDATE_UI events for a slightly different
325 void LifeFrame::UpdateUI()
328 GetToolBar()->EnableTool(ID_START
, !m_running
);
329 GetToolBar()->EnableTool(ID_STOP
, m_running
);
330 GetMenuBar()->GetMenu(2)->Enable(ID_START
, !m_running
);
331 GetMenuBar()->GetMenu(2)->Enable(ID_STEP
, !m_running
);
332 GetMenuBar()->GetMenu(2)->Enable(ID_STOP
, m_running
);
335 int cellsize
= m_canvas
->GetCellSize();
336 GetToolBar()->EnableTool(ID_ZOOMIN
, cellsize
< 32);
337 GetToolBar()->EnableTool(ID_ZOOMOUT
, cellsize
> 1);
338 GetMenuBar()->GetMenu(1)->Enable(ID_ZOOMIN
, cellsize
< 32);
339 GetMenuBar()->GetMenu(1)->Enable(ID_ZOOMOUT
, cellsize
> 1);
343 void LifeFrame::OnMenu(wxCommandEvent
& event
)
345 switch (event
.GetId())
347 case ID_START
: OnStart(); break;
348 case ID_STEP
: OnStep(); break;
349 case ID_STOP
: OnStop(); break;
352 bool checked
= GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV
);
353 m_navigator
->Show(checked
);
361 while (m_running
&& m_topspeed
)
370 // stop if it was running
373 m_canvas
->Recenter(0, 0);
380 LifeAboutDialog
dialog(this);
386 // TRUE is to force the frame to close
393 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
395 // Stop if it was running; this is absolutely needed because
396 // the frame won't be actually destroyed until there are no
397 // more pending events, and this in turn won't ever happen
398 // if the timer is running faster than the window can redraw.
403 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
405 // stop if it was running
409 LifeSamplesDialog
dialog(this);
411 if (dialog
.ShowModal() == wxID_OK
)
413 const LifeShape shape
= dialog
.GetShape();
417 m_life
->SetShape(shape
);
420 m_canvas
->Recenter(0, 0);
426 void LifeFrame::OnZoom(wxCommandEvent
& event
)
428 int cellsize
= m_canvas
->GetCellSize();
430 if ((event
.GetId() == ID_ZOOMIN
) && cellsize
< 32)
432 m_canvas
->SetCellSize(cellsize
* 2);
435 else if ((event
.GetId() == ID_ZOOMOUT
) && cellsize
> 1)
437 m_canvas
->SetCellSize(cellsize
/ 2);
442 void LifeFrame::OnNavigate(wxCommandEvent
& event
)
446 switch (event
.GetId())
448 case ID_NORTH
: c
= m_life
->FindNorth(); break;
449 case ID_SOUTH
: c
= m_life
->FindSouth(); break;
450 case ID_WEST
: c
= m_life
->FindWest(); break;
451 case ID_EAST
: c
= m_life
->FindEast(); break;
452 case ID_CENTER
: c
= m_life
->FindCenter(); break;
453 case ID_ORIGIN
: c
.i
= c
.j
= 0; break;
456 m_canvas
->Recenter(c
.i
, c
.j
);
459 void LifeFrame::OnSlider(wxScrollEvent
& event
)
461 m_interval
= event
.GetPosition() * 100;
472 void LifeFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
477 void LifeFrame::OnStart()
481 m_timer
->Start(m_interval
);
487 void LifeFrame::OnStop()
498 void LifeFrame::OnStep()
500 if (m_life
->NextTic())
505 m_canvas
->DrawChanged();
510 // --------------------------------------------------------------------------
511 // LifeNavigator miniframe
512 // --------------------------------------------------------------------------
514 LifeNavigator::LifeNavigator(wxWindow
*parent
)
515 : wxMiniFrame(parent
, -1,
519 wxCAPTION
| wxSIMPLE_BORDER
)
521 wxPanel
*panel
= new wxPanel(this, -1);
522 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
523 wxBoxSizer
*sizer2
= new wxBoxSizer(wxHORIZONTAL
);
525 // create bitmaps and masks for the buttons
527 bmpn
= wxBITMAP(north
),
528 bmpw
= wxBITMAP(west
),
529 bmpc
= wxBITMAP(center
),
530 bmpe
= wxBITMAP(east
),
531 bmps
= wxBITMAP(south
);
534 bmpn
.SetMask(new wxMask(bmpn
, *wxLIGHT_GREY
));
535 bmpw
.SetMask(new wxMask(bmpw
, *wxLIGHT_GREY
));
536 bmpc
.SetMask(new wxMask(bmpc
, *wxLIGHT_GREY
));
537 bmpe
.SetMask(new wxMask(bmpe
, *wxLIGHT_GREY
));
538 bmps
.SetMask(new wxMask(bmps
, *wxLIGHT_GREY
));
541 // create the buttons and attach tooltips to them
543 *bn
= new wxBitmapButton(panel
, ID_NORTH
, bmpn
),
544 *bw
= new wxBitmapButton(panel
, ID_WEST
, bmpw
),
545 *bc
= new wxBitmapButton(panel
, ID_CENTER
, bmpc
),
546 *be
= new wxBitmapButton(panel
, ID_EAST
, bmpe
),
547 *bs
= new wxBitmapButton(panel
, ID_SOUTH
, bmps
);
550 bn
->SetToolTip(_("Find northernmost cell"));
551 bw
->SetToolTip(_("Find westernmost cell"));
552 bc
->SetToolTip(_("Find center of mass"));
553 be
->SetToolTip(_("Find easternmost cell"));
554 bs
->SetToolTip(_("Find southernmost cell"));
557 // add buttons to sizers
558 sizer2
->Add( bw
, 0, wxCENTRE
| wxWEST
, 4 );
559 sizer2
->Add( bc
, 0, wxCENTRE
);
560 sizer2
->Add( be
, 0, wxCENTRE
| wxEAST
, 4 );
561 sizer1
->Add( bn
, 0, wxCENTRE
| wxNORTH
, 4 );
562 sizer1
->Add( sizer2
);
563 sizer1
->Add( bs
, 0, wxCENTRE
| wxSOUTH
, 4 );
565 // set the miniframe size
566 panel
->SetSizer(sizer1
);
567 panel
->SetAutoLayout(TRUE
);
569 sizer1
->SetSizeHints(this);
571 // move it to a sensible position
572 wxRect parentRect
= parent
->GetRect();
573 wxSize childSize
= GetSize();
574 int x
= parentRect
.GetX() +
575 parentRect
.GetWidth();
576 int y
= parentRect
.GetY() +
577 (parentRect
.GetHeight() - childSize
.GetHeight()) / 4;
584 void LifeNavigator::OnClose(wxCloseEvent
& event
)
593 // --------------------------------------------------------------------------
595 // --------------------------------------------------------------------------
597 // canvas constructor
598 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
599 : wxWindow(parent
, -1, wxPoint(0, 0), wxSize(100, 100),
603 m_interactive
= interactive
;
605 m_status
= MOUSE_NOACTION
;
612 SetCursor(*wxCROSS_CURSOR
);
614 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
615 SetBackgroundColour(*wxWHITE
);
618 LifeCanvas::~LifeCanvas()
623 // recenter at the given position
624 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
626 m_viewportX
= i
- m_viewportW
/ 2;
627 m_viewportY
= j
- m_viewportH
/ 2;
633 // set the cell size and refresh display
634 void LifeCanvas::SetCellSize(int cellsize
)
636 m_cellsize
= cellsize
;
638 // find current center
639 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
640 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
642 // get current canvas size and adjust viewport accordingly
644 GetClientSize(&w
, &h
);
645 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
646 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
649 m_viewportX
= cx
- m_viewportW
/ 2;
650 m_viewportY
= cy
- m_viewportH
/ 2;
655 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
656 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
657 m_thumbX
= m_viewportW
;
658 m_thumbY
= m_viewportH
;
665 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
669 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
670 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
677 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
679 wxCoord x
= CellToX(i
);
680 wxCoord y
= CellToY(j
);
682 // if cellsize is 1 or 2, there will be no grid
689 dc
.DrawRectangle(x
, y
, 2, 2);
692 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
696 // draw all changed cells
697 void LifeCanvas::DrawChanged()
705 m_life
->BeginFind(m_viewportX
,
707 m_viewportX
+ m_viewportW
,
708 m_viewportY
+ m_viewportH
,
715 dc
.SetPen(*wxBLACK_PEN
);
719 dc
.SetPen(*wxTRANSPARENT_PEN
);
720 dc
.SetBrush(*wxBLACK_BRUSH
);
722 dc
.SetLogicalFunction(wxINVERT
);
726 done
= m_life
->FindMore(&cells
, &ncells
);
728 for (size_t m
= 0; m
< ncells
; m
++)
729 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
735 void LifeCanvas::OnPaint(wxPaintEvent
& event
)
738 wxRect rect
= GetUpdateRegion().GetBox();
740 wxInt32 i0
, j0
, i1
, j1
;
746 h
= rect
.GetHeight();
750 i1
= XToCell(x
+ w
- 1);
751 j1
= YToCell(y
+ h
- 1);
757 m_life
->BeginFind(i0
, j0
, i1
, j1
, FALSE
);
758 done
= m_life
->FindMore(&cells
, &ncells
);
760 // erase all damaged cells and draw the grid
762 dc
.SetBrush(*wxWHITE_BRUSH
);
767 dc
.SetPen(*wxWHITE_PEN
);
768 dc
.DrawRectangle(x
, y
, w
, h
);
774 w
= CellToX(i1
+ 1) - x
+ 1;
775 h
= CellToY(j1
+ 1) - y
+ 1;
777 dc
.SetPen(*wxLIGHT_GREY_PEN
);
778 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
779 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
780 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
781 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
784 // draw all alive cells
785 dc
.SetPen(*wxBLACK_PEN
);
786 dc
.SetBrush(*wxBLACK_BRUSH
);
790 for (size_t m
= 0; m
< ncells
; m
++)
791 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
793 done
= m_life
->FindMore(&cells
, &ncells
);
797 for (size_t m
= 0; m
< ncells
; m
++)
798 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
803 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
808 // which cell are we pointing at?
809 wxInt32 i
= XToCell( event
.GetX() );
810 wxInt32 j
= YToCell( event
.GetY() );
812 // set statusbar text
814 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
815 ((LifeFrame
*) wxGetApp().GetTopWindow())->SetStatusText(msg
, 1);
818 if (!event
.LeftIsDown())
820 m_status
= MOUSE_NOACTION
;
824 // button just pressed?
825 if (m_status
== MOUSE_NOACTION
)
827 // yes, update status and toggle this cell
828 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
832 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
833 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
835 else if ((m_mi
!= i
) || (m_mj
!= j
))
837 bool alive
= (m_status
== MOUSE_DRAWING
);
839 // prepare DC and pen + brush to optimize drawing
841 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
842 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
845 // draw a line of cells using Bresenham's algorithm
846 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
849 si
= (di
< 0)? -1 : 1;
852 sj
= (dj
< 0)? -1 : 1;
864 m_life
->SetCell(ii
, jj
, alive
);
865 DrawCell(ii
, jj
, dc
);
882 m_life
->SetCell(ii
, jj
, alive
);
883 DrawCell(ii
, jj
, dc
);
895 m_life
->SetCell(ii
, jj
, alive
);
896 DrawCell(ii
, jj
, dc
);
903 ((LifeFrame
*) wxGetApp().GetTopWindow())->UpdateInfoText();
906 void LifeCanvas::OnSize(wxSizeEvent
& event
)
909 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
910 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
913 wxCoord w
= event
.GetSize().GetX();
914 wxCoord h
= event
.GetSize().GetY();
915 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
916 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
919 m_viewportX
= cx
- m_viewportW
/ 2;
920 m_viewportY
= cy
- m_viewportH
/ 2;
925 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
926 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
927 m_thumbX
= m_viewportW
;
928 m_thumbY
= m_viewportH
;
931 // allow default processing
935 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
937 WXTYPE type
= event
.GetEventType();
938 int pos
= event
.GetPosition();
939 int orient
= event
.GetOrientation();
941 // calculate scroll increment
945 case wxEVT_SCROLLWIN_TOP
:
947 if (orient
== wxHORIZONTAL
)
948 scrollinc
= -m_viewportW
;
950 scrollinc
= -m_viewportH
;
953 case wxEVT_SCROLLWIN_BOTTOM
:
955 if (orient
== wxHORIZONTAL
)
956 scrollinc
= m_viewportW
;
958 scrollinc
= m_viewportH
;
961 case wxEVT_SCROLLWIN_LINEUP
: scrollinc
= -1; break;
962 case wxEVT_SCROLLWIN_LINEDOWN
: scrollinc
= +1; break;
963 case wxEVT_SCROLLWIN_PAGEUP
: scrollinc
= -10; break;
964 case wxEVT_SCROLLWIN_PAGEDOWN
: scrollinc
= +10; break;
965 case wxEVT_SCROLLWIN_THUMBTRACK
:
967 if (orient
== wxHORIZONTAL
)
969 scrollinc
= pos
- m_thumbX
;
974 scrollinc
= pos
- m_thumbY
;
979 case wxEVT_SCROLLWIN_THUMBRELEASE
:
981 m_thumbX
= m_viewportW
;
982 m_thumbY
= m_viewportH
;
986 #if defined(__WXGTK__) || defined(__WXMOTIF__) // what about Motif?
987 // wxGTK updates the thumb automatically (wxMSW doesn't); reset it back
988 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
990 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
991 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
995 if (scrollinc
== 0) return;
997 // scroll the window and adjust the viewport
998 if (orient
== wxHORIZONTAL
)
1000 m_viewportX
+= scrollinc
;
1001 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
1005 m_viewportY
+= scrollinc
;
1006 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
1010 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
1012 // do nothing. I just don't want the background to be erased, you know.