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"
32 #include "wx/wfstream.h"
33 #include "wx/filedlg.h"
40 // --------------------------------------------------------------------------
42 // --------------------------------------------------------------------------
44 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
46 #include "mondrian.xpm"
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"
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"
65 // --------------------------------------------------------------------------
67 // --------------------------------------------------------------------------
69 // IDs for the controls and the menu commands. Exluding those already defined
70 // by wxWindows, such as wxID_NEW.
74 ID_TIMER
= wxID_HIGHEST
,
97 // speed selection slider
101 // --------------------------------------------------------------------------
102 // event tables and other macros for wxWindows
103 // --------------------------------------------------------------------------
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
)
131 BEGIN_EVENT_TABLE(LifeNavigator
, wxMiniFrame
)
132 EVT_CLOSE ( LifeNavigator::OnClose
)
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
)
147 // Create a new application object
148 IMPLEMENT_APP(LifeApp
)
151 // ==========================================================================
153 // ==========================================================================
156 #define ADD_TOOL(id, bmp, tooltip, help) \
157 toolBar->AddTool(id, bmp, wxNullBitmap, false, -1, -1, (wxObject *)0, tooltip, help)
160 // --------------------------------------------------------------------------
162 // --------------------------------------------------------------------------
164 // 'Main program' equivalent: the program execution "starts" here
165 bool LifeApp::OnInit()
167 // create the main application window
168 LifeFrame
*frame
= new LifeFrame();
170 // show it and tell the application that it's our main window
176 frame
->UpdateInfoText();
179 // enter the main message loop and run the app
183 // --------------------------------------------------------------------------
185 // --------------------------------------------------------------------------
188 LifeFrame::LifeFrame() : wxFrame( (wxFrame
*) NULL
, wxID_ANY
,
189 _("Life!"), wxPoint(200, 200) )
192 SetIcon(wxICON(mondrian
));
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
);
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"));
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"));
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"));
227 menuHelp
->Append(wxID_ABOUT
, _("&About\tCtrl-A"), _("Show about dialog"));
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"));
237 wxBitmap tbBitmaps
[7];
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
);
247 wxToolBar
*toolBar
= CreateToolBar();
248 toolBar
->SetMargins(5, 5);
249 toolBar
->SetToolBitmapSize(wxSize(16, 16));
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"));
262 toolBar
->EnableTool(ID_STOP
, false); // must be after Realize() !
266 SetStatusText(_("Welcome to Life!"));
270 m_timer
= new wxTimer(this, ID_TIMER
);
276 // We use two different panels to reduce flicker in wxGTK, because
277 // some widgets (like wxStaticText) don't have their own X11 window,
278 // and thus updating the text would result in a refresh of the canvas
279 // if they belong to the same parent.
281 wxPanel
*panel1
= new wxPanel(this, wxID_ANY
);
282 wxPanel
*panel2
= new wxPanel(this, wxID_ANY
);
285 m_canvas
= new LifeCanvas(panel1
, m_life
);
288 m_text
= new wxStaticText(panel2
, wxID_ANY
,
292 wxALIGN_CENTER
| wxST_NO_AUTORESIZE
);
294 wxSlider
*slider
= new wxSlider(panel2
, ID_SLIDER
,
298 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
303 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
304 wxBoxSizer
*sizer2
= new wxBoxSizer(wxVERTICAL
);
305 wxBoxSizer
*sizer3
= new wxBoxSizer(wxVERTICAL
);
307 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
308 sizer1
->Add( m_canvas
, 1, wxGROW
| wxALL
, 2 );
309 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
310 panel1
->SetSizer( sizer1
);
311 sizer1
->Fit( panel1
);
313 sizer2
->Add( m_text
, 0, wxGROW
| wxTOP
, 4 );
314 sizer2
->Add( slider
, 0, wxCENTRE
| wxALL
, 4 );
316 panel2
->SetSizer( sizer2
);
317 sizer2
->Fit( panel2
);
319 sizer3
->Add( panel1
, 1, wxGROW
);
320 sizer3
->Add( panel2
, 0, wxGROW
);
324 // set minimum frame size
325 sizer3
->SetSizeHints( this );
328 m_navigator
= new LifeNavigator(this);
331 LifeFrame::~LifeFrame()
336 void LifeFrame::UpdateInfoText()
340 msg
.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
342 m_topspeed
? 0 : m_interval
,
343 m_life
->GetNumCells());
344 m_text
->SetLabel(msg
);
347 // Enable or disable tools and menu entries according to the current
348 // state. See also wxEVT_UPDATE_UI events for a slightly different
350 void LifeFrame::UpdateUI()
353 GetToolBar()->EnableTool(ID_START
, !m_running
);
354 GetToolBar()->EnableTool(ID_STOP
, m_running
);
355 GetMenuBar()->Enable(ID_START
, !m_running
);
356 GetMenuBar()->Enable(ID_STEP
, !m_running
);
357 GetMenuBar()->Enable(ID_STOP
, m_running
);
358 GetMenuBar()->Enable(ID_TOPSPEED
, !m_topspeed
);
361 int cellsize
= m_canvas
->GetCellSize();
362 GetToolBar()->EnableTool(ID_ZOOMIN
, cellsize
< 32);
363 GetToolBar()->EnableTool(ID_ZOOMOUT
, cellsize
> 1);
364 GetMenuBar()->Enable(ID_ZOOMIN
, cellsize
< 32);
365 GetMenuBar()->Enable(ID_ZOOMOUT
, cellsize
> 1);
368 // Event handlers -----------------------------------------------------------
370 // OnMenu handles all events which don't have their own event handler
371 void LifeFrame::OnMenu(wxCommandEvent
& event
)
373 switch (event
.GetId())
377 // stop if it was running
380 m_canvas
->Recenter(0, 0);
387 LifeAboutDialog
dialog(this);
393 // true is to force the frame to close
399 bool checked
= GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV
);
400 m_navigator
->Show(checked
);
405 wxString desc
= m_life
->GetDescription();
407 if ( desc
.IsEmpty() )
408 desc
= _("Not available");
410 // should we make the description editable here?
411 wxMessageBox(desc
, _("Description"), wxOK
| wxICON_INFORMATION
);
415 case ID_START
: OnStart(); break;
416 case ID_STEP
: OnStep(); break;
417 case ID_STOP
: OnStop(); break;
423 while (m_running
&& m_topspeed
)
433 void LifeFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
435 wxFileDialog
filedlg(this,
436 _("Choose a file to open"),
439 _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"),
440 wxOPEN
| wxFILE_MUST_EXIST
);
442 if (filedlg
.ShowModal() == wxID_OK
)
444 wxFileInputStream
stream(filedlg
.GetPath());
445 LifeReader
reader(stream
);
447 // the reader handles errors itself, no need to do anything here
450 // stop if running and put the pattern
453 m_life
->SetPattern(reader
.GetPattern());
456 m_canvas
->Recenter(0, 0);
463 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
465 // stop if it was running
469 LifeSamplesDialog
dialog(this);
471 if (dialog
.ShowModal() == wxID_OK
)
473 const LifePattern pattern
= dialog
.GetPattern();
477 m_life
->SetPattern(pattern
);
480 m_canvas
->Recenter(0, 0);
486 void LifeFrame::OnZoom(wxCommandEvent
& event
)
488 int cellsize
= m_canvas
->GetCellSize();
490 if ((event
.GetId() == ID_ZOOMIN
) && cellsize
< 32)
492 m_canvas
->SetCellSize(cellsize
* 2);
495 else if ((event
.GetId() == ID_ZOOMOUT
) && cellsize
> 1)
497 m_canvas
->SetCellSize(cellsize
/ 2);
502 void LifeFrame::OnNavigate(wxCommandEvent
& event
)
506 switch (event
.GetId())
508 case ID_NORTH
: c
= m_life
->FindNorth(); break;
509 case ID_SOUTH
: c
= m_life
->FindSouth(); break;
510 case ID_WEST
: c
= m_life
->FindWest(); break;
511 case ID_EAST
: c
= m_life
->FindEast(); break;
512 case ID_CENTER
: c
= m_life
->FindCenter(); break;
516 case ID_ORIGIN
: c
.i
= c
.j
= 0; break;
519 m_canvas
->Recenter(c
.i
, c
.j
);
522 void LifeFrame::OnSlider(wxScrollEvent
& event
)
524 m_interval
= event
.GetPosition() * 100;
535 void LifeFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
540 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
542 // Stop if it was running; this is absolutely needed because
543 // the frame won't be actually destroyed until there are no
544 // more pending events, and this in turn won't ever happen
545 // if the timer is running faster than the window can redraw.
550 void LifeFrame::OnStart()
554 m_timer
->Start(m_interval
);
560 void LifeFrame::OnStop()
571 void LifeFrame::OnStep()
573 if (m_life
->NextTic())
578 m_canvas
->DrawChanged();
583 // --------------------------------------------------------------------------
584 // LifeNavigator miniframe
585 // --------------------------------------------------------------------------
587 LifeNavigator::LifeNavigator(wxWindow
*parent
)
588 : wxMiniFrame(parent
, wxID_ANY
,
592 wxCAPTION
| wxSIMPLE_BORDER
)
594 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
595 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
596 wxBoxSizer
*sizer2
= new wxBoxSizer(wxHORIZONTAL
);
598 // create bitmaps and masks for the buttons
600 bmpn
= wxBITMAP(north
),
601 bmpw
= wxBITMAP(west
),
602 bmpc
= wxBITMAP(center
),
603 bmpe
= wxBITMAP(east
),
604 bmps
= wxBITMAP(south
);
606 #if !defined(__WXGTK__) && !defined(__WXMOTIF__) && !defined(__WXMAC__)
607 bmpn
.SetMask(new wxMask(bmpn
, *wxLIGHT_GREY
));
608 bmpw
.SetMask(new wxMask(bmpw
, *wxLIGHT_GREY
));
609 bmpc
.SetMask(new wxMask(bmpc
, *wxLIGHT_GREY
));
610 bmpe
.SetMask(new wxMask(bmpe
, *wxLIGHT_GREY
));
611 bmps
.SetMask(new wxMask(bmps
, *wxLIGHT_GREY
));
614 // create the buttons and attach tooltips to them
616 *bn
= new wxBitmapButton(panel
, ID_NORTH
, bmpn
),
617 *bw
= new wxBitmapButton(panel
, ID_WEST
, bmpw
),
618 *bc
= new wxBitmapButton(panel
, ID_CENTER
, bmpc
),
619 *be
= new wxBitmapButton(panel
, ID_EAST
, bmpe
),
620 *bs
= new wxBitmapButton(panel
, ID_SOUTH
, bmps
);
623 bn
->SetToolTip(_("Find northernmost cell"));
624 bw
->SetToolTip(_("Find westernmost cell"));
625 bc
->SetToolTip(_("Find center of mass"));
626 be
->SetToolTip(_("Find easternmost cell"));
627 bs
->SetToolTip(_("Find southernmost cell"));
630 // add buttons to sizers
631 sizer2
->Add( bw
, 0, wxCENTRE
| wxWEST
, 4 );
632 sizer2
->Add( bc
, 0, wxCENTRE
);
633 sizer2
->Add( be
, 0, wxCENTRE
| wxEAST
, 4 );
634 sizer1
->Add( bn
, 0, wxCENTRE
| wxNORTH
, 4 );
635 sizer1
->Add( sizer2
);
636 sizer1
->Add( bs
, 0, wxCENTRE
| wxSOUTH
, 4 );
638 // set the panel and miniframe size
639 panel
->SetSizer(sizer1
);
642 SetClientSize(panel
->GetSize());
643 wxSize sz
= GetSize();
644 SetSizeHints(sz
.x
, sz
.y
, sz
.x
, sz
.y
);
646 // move it to a sensible position
647 wxRect parentRect
= parent
->GetRect();
648 wxSize childSize
= GetSize();
649 int x
= parentRect
.GetX() +
650 parentRect
.GetWidth();
651 int y
= parentRect
.GetY() +
652 (parentRect
.GetHeight() - childSize
.GetHeight()) / 4;
659 void LifeNavigator::OnClose(wxCloseEvent
& event
)
669 // --------------------------------------------------------------------------
671 // --------------------------------------------------------------------------
673 // canvas constructor
674 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
675 : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxSize(100, 100),
679 m_interactive
= interactive
;
681 m_status
= MOUSE_NOACTION
;
688 SetCursor(*wxCROSS_CURSOR
);
690 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
691 SetBackgroundColour(*wxWHITE
);
694 LifeCanvas::~LifeCanvas()
699 // recenter at the given position
700 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
702 m_viewportX
= i
- m_viewportW
/ 2;
703 m_viewportY
= j
- m_viewportH
/ 2;
709 // set the cell size and refresh display
710 void LifeCanvas::SetCellSize(int cellsize
)
712 m_cellsize
= cellsize
;
714 // find current center
715 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
716 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
718 // get current canvas size and adjust viewport accordingly
720 GetClientSize(&w
, &h
);
721 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
722 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
725 m_viewportX
= cx
- m_viewportW
/ 2;
726 m_viewportY
= cy
- m_viewportH
/ 2;
731 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
732 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
733 m_thumbX
= m_viewportW
;
734 m_thumbY
= m_viewportH
;
741 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
745 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
746 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
753 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
755 wxCoord x
= CellToX(i
);
756 wxCoord y
= CellToY(j
);
758 // if cellsize is 1 or 2, there will be no grid
765 dc
.DrawRectangle(x
, y
, 2, 2);
768 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
772 // draw all changed cells
773 void LifeCanvas::DrawChanged()
781 m_life
->BeginFind(m_viewportX
,
783 m_viewportX
+ m_viewportW
,
784 m_viewportY
+ m_viewportH
,
791 dc
.SetPen(*wxBLACK_PEN
);
795 dc
.SetPen(*wxTRANSPARENT_PEN
);
796 dc
.SetBrush(*wxBLACK_BRUSH
);
798 dc
.SetLogicalFunction(wxINVERT
);
802 done
= m_life
->FindMore(&cells
, &ncells
);
804 for (size_t m
= 0; m
< ncells
; m
++)
805 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
811 void LifeCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
814 wxRect rect
= GetUpdateRegion().GetBox();
816 wxInt32 i0
, j0
, i1
, j1
;
822 h
= rect
.GetHeight();
826 i1
= XToCell(x
+ w
- 1);
827 j1
= YToCell(y
+ h
- 1);
832 m_life
->BeginFind(i0
, j0
, i1
, j1
, false);
833 bool done
= m_life
->FindMore(&cells
, &ncells
);
835 // erase all damaged cells and draw the grid
837 dc
.SetBrush(*wxWHITE_BRUSH
);
842 dc
.SetPen(*wxWHITE_PEN
);
843 dc
.DrawRectangle(x
, y
, w
, h
);
849 w
= CellToX(i1
+ 1) - x
+ 1;
850 h
= CellToY(j1
+ 1) - y
+ 1;
852 dc
.SetPen(*wxLIGHT_GREY_PEN
);
853 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
854 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
855 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
856 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
859 // draw all alive cells
860 dc
.SetPen(*wxBLACK_PEN
);
861 dc
.SetBrush(*wxBLACK_BRUSH
);
865 for (size_t m
= 0; m
< ncells
; m
++)
866 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
868 done
= m_life
->FindMore(&cells
, &ncells
);
872 for (size_t m
= 0; m
< ncells
; m
++)
873 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
878 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
883 // which cell are we pointing at?
884 wxInt32 i
= XToCell( event
.GetX() );
885 wxInt32 j
= YToCell( event
.GetY() );
887 // set statusbar text
889 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
890 ((LifeFrame
*) wxGetApp().GetTopWindow())->SetStatusText(msg
, 1);
892 // NOTE that wxMouseEvent::LeftDown() and wxMouseEvent::LeftIsDown()
893 // have different semantics. The first one is used to signal that the
894 // button was just pressed (i.e., in "button down" events); the second
895 // one just describes the current status of the button, independently
896 // of the mouse event type. LeftIsDown is typically used in "mouse
897 // move" events, to test if the button is _still_ pressed.
899 // is the button down?
900 if (!event
.LeftIsDown())
902 m_status
= MOUSE_NOACTION
;
906 // was it pressed just now?
907 if (event
.LeftDown())
909 // yes: start a new action and toggle this cell
910 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
914 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
915 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
917 else if ((m_mi
!= i
) || (m_mj
!= j
))
919 // no: continue ongoing action
920 bool alive
= (m_status
== MOUSE_DRAWING
);
922 // prepare DC and pen + brush to optimize drawing
924 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
925 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
928 // draw a line of cells using Bresenham's algorithm
929 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
932 si
= (di
< 0)? -1 : 1;
935 sj
= (dj
< 0)? -1 : 1;
947 m_life
->SetCell(ii
, jj
, alive
);
948 DrawCell(ii
, jj
, dc
);
965 m_life
->SetCell(ii
, jj
, alive
);
966 DrawCell(ii
, jj
, dc
);
978 m_life
->SetCell(ii
, jj
, alive
);
979 DrawCell(ii
, jj
, dc
);
986 ((LifeFrame
*) wxGetApp().GetTopWindow())->UpdateInfoText();
989 void LifeCanvas::OnSize(wxSizeEvent
& event
)
992 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
993 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
996 wxCoord w
= event
.GetSize().GetX();
997 wxCoord h
= event
.GetSize().GetY();
998 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
999 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
1002 m_viewportX
= cx
- m_viewportW
/ 2;
1003 m_viewportY
= cy
- m_viewportH
/ 2;
1008 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1009 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1010 m_thumbX
= m_viewportW
;
1011 m_thumbY
= m_viewportH
;
1014 // allow default processing
1018 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
1020 WXTYPE type
= event
.GetEventType();
1021 int pos
= event
.GetPosition();
1022 int orient
= event
.GetOrientation();
1024 // calculate scroll increment
1026 if (type
== wxEVT_SCROLLWIN_TOP
)
1028 if (orient
== wxHORIZONTAL
)
1029 scrollinc
= -m_viewportW
;
1031 scrollinc
= -m_viewportH
;
1034 if (type
== wxEVT_SCROLLWIN_BOTTOM
)
1036 if (orient
== wxHORIZONTAL
)
1037 scrollinc
= m_viewportW
;
1039 scrollinc
= m_viewportH
;
1042 if (type
== wxEVT_SCROLLWIN_LINEUP
)
1047 if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
1052 if (type
== wxEVT_SCROLLWIN_PAGEUP
)
1057 if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
1062 if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
1064 if (orient
== wxHORIZONTAL
)
1066 scrollinc
= pos
- m_thumbX
;
1071 scrollinc
= pos
- m_thumbY
;
1076 if (type
== wxEVT_SCROLLWIN_THUMBRELEASE
)
1078 m_thumbX
= m_viewportW
;
1079 m_thumbY
= m_viewportH
;
1082 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1083 // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't);
1084 // so reset it back as we always want it to be in the same position.
1085 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
1087 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1088 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1092 if (scrollinc
== 0) return;
1094 // scroll the window and adjust the viewport
1095 if (orient
== wxHORIZONTAL
)
1097 m_viewportX
+= scrollinc
;
1098 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
1102 m_viewportY
+= scrollinc
;
1103 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
1107 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
1109 // do nothing. I just don't want the background to be erased, you know.