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"
34 #include "wx/stockitem.h"
41 // --------------------------------------------------------------------------
43 // --------------------------------------------------------------------------
45 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
47 #include "mondrian.xpm"
49 // bitmap buttons for the toolbar
50 #include "bitmaps/reset.xpm"
51 #include "bitmaps/open.xpm"
52 #include "bitmaps/play.xpm"
53 #include "bitmaps/stop.xpm"
54 #include "bitmaps/zoomin.xpm"
55 #include "bitmaps/zoomout.xpm"
56 #include "bitmaps/info.xpm"
59 #include "bitmaps/north.xpm"
60 #include "bitmaps/south.xpm"
61 #include "bitmaps/east.xpm"
62 #include "bitmaps/west.xpm"
63 #include "bitmaps/center.xpm"
66 // --------------------------------------------------------------------------
68 // --------------------------------------------------------------------------
70 // IDs for the controls and the menu commands. Exluding those already defined
71 // by wxWidgets, such as wxID_NEW.
75 ID_TIMER
= wxID_HIGHEST
,
95 // speed selection slider
99 // --------------------------------------------------------------------------
100 // event tables and other macros for wxWidgets
101 // --------------------------------------------------------------------------
104 BEGIN_EVENT_TABLE(LifeFrame
, wxFrame
)
105 EVT_MENU (wxID_NEW
, LifeFrame::OnMenu
)
106 EVT_MENU (wxID_OPEN
, LifeFrame::OnOpen
)
107 EVT_MENU (ID_SAMPLES
, LifeFrame::OnSamples
)
108 EVT_MENU (wxID_ABOUT
, LifeFrame::OnMenu
)
109 EVT_MENU (wxID_EXIT
, LifeFrame::OnMenu
)
110 EVT_MENU (ID_SHOWNAV
, LifeFrame::OnMenu
)
111 EVT_MENU (ID_ORIGIN
, LifeFrame::OnNavigate
)
112 EVT_BUTTON (ID_CENTER
, LifeFrame::OnNavigate
)
113 EVT_BUTTON (ID_NORTH
, LifeFrame::OnNavigate
)
114 EVT_BUTTON (ID_SOUTH
, LifeFrame::OnNavigate
)
115 EVT_BUTTON (ID_EAST
, LifeFrame::OnNavigate
)
116 EVT_BUTTON (ID_WEST
, LifeFrame::OnNavigate
)
117 EVT_MENU (wxID_ZOOM_IN
, LifeFrame::OnZoom
)
118 EVT_MENU (wxID_ZOOM_OUT
,LifeFrame::OnZoom
)
119 EVT_MENU (ID_INFO
, LifeFrame::OnMenu
)
120 EVT_MENU (ID_START
, LifeFrame::OnMenu
)
121 EVT_MENU (ID_STEP
, LifeFrame::OnMenu
)
122 EVT_MENU (wxID_STOP
, LifeFrame::OnMenu
)
123 EVT_MENU (ID_TOPSPEED
, LifeFrame::OnMenu
)
124 EVT_COMMAND_SCROLL (ID_SLIDER
, LifeFrame::OnSlider
)
125 EVT_TIMER (ID_TIMER
, LifeFrame::OnTimer
)
126 EVT_CLOSE ( LifeFrame::OnClose
)
129 BEGIN_EVENT_TABLE(LifeNavigator
, wxMiniFrame
)
130 EVT_CLOSE ( LifeNavigator::OnClose
)
133 BEGIN_EVENT_TABLE(LifeCanvas
, wxWindow
)
134 EVT_PAINT ( LifeCanvas::OnPaint
)
135 EVT_SCROLLWIN ( LifeCanvas::OnScroll
)
136 EVT_SIZE ( LifeCanvas::OnSize
)
137 EVT_MOTION ( LifeCanvas::OnMouse
)
138 EVT_LEFT_DOWN ( LifeCanvas::OnMouse
)
139 EVT_LEFT_UP ( LifeCanvas::OnMouse
)
140 EVT_LEFT_DCLICK ( LifeCanvas::OnMouse
)
141 EVT_ERASE_BACKGROUND( LifeCanvas::OnEraseBackground
)
145 // Create a new application object
146 IMPLEMENT_APP(LifeApp
)
149 // ==========================================================================
151 // ==========================================================================
154 #define ADD_TOOL(id, bmp, tooltip, help) \
155 toolBar->AddTool(id, bmp, wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, (wxObject *)NULL, tooltip, help)
158 // --------------------------------------------------------------------------
160 // --------------------------------------------------------------------------
162 // 'Main program' equivalent: the program execution "starts" here
163 bool LifeApp::OnInit()
165 // create the main application window
166 LifeFrame
*frame
= new LifeFrame();
168 // show it and tell the application that it's our main window
174 frame
->UpdateInfoText();
177 // enter the main message loop and run the app
181 // --------------------------------------------------------------------------
183 // --------------------------------------------------------------------------
186 LifeFrame::LifeFrame() :
187 wxFrame( (wxFrame
*) NULL
, wxID_ANY
, _("Life!"), wxDefaultPosition
),
191 SetIcon(wxICON(mondrian
));
194 wxMenu
*menuFile
= new wxMenu(wxMENU_TEAROFF
);
195 wxMenu
*menuView
= new wxMenu(wxMENU_TEAROFF
);
196 wxMenu
*menuGame
= new wxMenu(wxMENU_TEAROFF
);
197 wxMenu
*menuHelp
= new wxMenu(wxMENU_TEAROFF
);
199 menuFile
->Append(wxID_NEW
, wxGetStockLabel(wxID_NEW
), _("Start a new game"));
200 menuFile
->Append(wxID_OPEN
, wxGetStockLabel(wxID_OPEN
), _("Open an existing Life pattern"));
201 menuFile
->Append(ID_SAMPLES
, _("&Sample game..."), _("Select a sample configuration"));
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"));
254 ADD_TOOL(wxID_OPEN
, tbBitmaps
[1], wxGetStockLabel(wxID_OPEN
, false), _("Open an existing Life pattern"));
256 // PocketPC only works with standard identifiers, and there's restricted space
258 toolBar
->AddSeparator();
259 ADD_TOOL(wxID_ZOOM_IN
, tbBitmaps
[2], wxGetStockLabel(wxID_ZOOM_IN
, false), _("Zoom in"));
260 ADD_TOOL(wxID_ZOOM_OUT
, tbBitmaps
[3], wxGetStockLabel(wxID_ZOOM_OUT
, false), _("Zoom out"));
261 ADD_TOOL(ID_INFO
, tbBitmaps
[4], _("Description"), _("Show description"));
262 toolBar
->AddSeparator();
263 ADD_TOOL(ID_START
, tbBitmaps
[5], _("Start"), _("Start"));
264 ADD_TOOL(wxID_STOP
, tbBitmaps
[6], wxGetStockLabel(wxID_STOP
, false), _("Stop"));
268 toolBar
->EnableTool(wxID_STOP
, false); // must be after Realize() !
273 SetStatusText(_("Welcome to Life!"));
274 #endif // wxUSE_STATUSBAR
278 m_timer
= new wxTimer(this, ID_TIMER
);
284 // We use two different panels to reduce flicker in wxGTK, because
285 // some widgets (like wxStaticText) don't have their own X11 window,
286 // and thus updating the text would result in a refresh of the canvas
287 // if they belong to the same parent.
289 wxPanel
*panel1
= new wxPanel(this, wxID_ANY
);
290 wxPanel
*panel2
= new wxPanel(this, wxID_ANY
);
293 m_canvas
= new LifeCanvas(panel1
, m_life
);
296 m_text
= new wxStaticText(panel2
, wxID_ANY
,
300 wxALIGN_CENTER
| wxST_NO_AUTORESIZE
);
302 wxSlider
*slider
= new wxSlider(panel2
, ID_SLIDER
,
305 wxSize(200, wxDefaultCoord
),
306 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
311 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
312 wxBoxSizer
*sizer2
= new wxBoxSizer(wxVERTICAL
);
313 wxBoxSizer
*sizer3
= new wxBoxSizer(wxVERTICAL
);
316 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
317 #endif // wxUSE_STATLINE
318 sizer1
->Add( m_canvas
, 1, wxGROW
| wxALL
, 2 );
320 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
321 #endif // wxUSE_STATLINE
322 panel1
->SetSizer( sizer1
);
323 sizer1
->Fit( panel1
);
325 sizer2
->Add( m_text
, 0, wxGROW
| wxTOP
, 4 );
326 sizer2
->Add( slider
, 0, wxCENTRE
| wxALL
, 4 );
328 panel2
->SetSizer( sizer2
);
329 sizer2
->Fit( panel2
);
331 sizer3
->Add( panel1
, 1, wxGROW
);
332 sizer3
->Add( panel2
, 0, wxGROW
);
338 // set minimum frame size
339 sizer3
->SetSizeHints( this );
341 // navigator frame - not appropriate for small devices
342 m_navigator
= new LifeNavigator(this);
347 LifeFrame::~LifeFrame()
352 void LifeFrame::UpdateInfoText()
356 msg
.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
358 m_topspeed
? 0 : m_interval
,
359 m_life
->GetNumCells());
360 m_text
->SetLabel(msg
);
363 // Enable or disable tools and menu entries according to the current
364 // state. See also wxEVT_UPDATE_UI events for a slightly different
366 void LifeFrame::UpdateUI()
369 GetToolBar()->EnableTool(ID_START
, !m_running
);
370 GetToolBar()->EnableTool(wxID_STOP
, m_running
);
371 GetMenuBar()->Enable(ID_START
, !m_running
);
372 GetMenuBar()->Enable(ID_STEP
, !m_running
);
373 GetMenuBar()->Enable(wxID_STOP
, m_running
);
374 GetMenuBar()->Enable(ID_TOPSPEED
, !m_topspeed
);
377 int cellsize
= m_canvas
->GetCellSize();
378 GetToolBar()->EnableTool(wxID_ZOOM_IN
, cellsize
< 32);
379 GetToolBar()->EnableTool(wxID_ZOOM_OUT
, cellsize
> 1);
380 GetMenuBar()->Enable(wxID_ZOOM_IN
, cellsize
< 32);
381 GetMenuBar()->Enable(wxID_ZOOM_OUT
, cellsize
> 1);
384 // Event handlers -----------------------------------------------------------
386 // OnMenu handles all events which don't have their own event handler
387 void LifeFrame::OnMenu(wxCommandEvent
& event
)
389 switch (event
.GetId())
393 // stop if it was running
396 m_canvas
->Recenter(0, 0);
403 LifeAboutDialog
dialog(this);
409 // true is to force the frame to close
415 bool checked
= GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV
);
417 m_navigator
->Show(checked
);
422 wxString desc
= m_life
->GetDescription();
425 desc
= _("Not available");
427 // should we make the description editable here?
428 wxMessageBox(desc
, _("Description"), wxOK
| wxICON_INFORMATION
);
432 case ID_START
: OnStart(); break;
433 case ID_STEP
: OnStep(); break;
434 case wxID_STOP
: OnStop(); break;
440 while (m_running
&& m_topspeed
)
450 void LifeFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
452 wxFileDialog
filedlg(this,
453 _("Choose a file to open"),
456 _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"),
457 wxOPEN
| wxFILE_MUST_EXIST
);
459 if (filedlg
.ShowModal() == wxID_OK
)
461 wxFileInputStream
stream(filedlg
.GetPath());
462 LifeReader
reader(stream
);
464 // the reader handles errors itself, no need to do anything here
467 // stop if running and put the pattern
470 m_life
->SetPattern(reader
.GetPattern());
473 m_canvas
->Recenter(0, 0);
480 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
482 // stop if it was running
486 LifeSamplesDialog
dialog(this);
488 if (dialog
.ShowModal() == wxID_OK
)
490 const LifePattern pattern
= dialog
.GetPattern();
494 m_life
->SetPattern(pattern
);
497 m_canvas
->Recenter(0, 0);
503 void LifeFrame::OnZoom(wxCommandEvent
& event
)
505 int cellsize
= m_canvas
->GetCellSize();
507 if ((event
.GetId() == wxID_ZOOM_IN
) && cellsize
< 32)
509 m_canvas
->SetCellSize(cellsize
* 2);
512 else if ((event
.GetId() == wxID_ZOOM_OUT
) && cellsize
> 1)
514 m_canvas
->SetCellSize(cellsize
/ 2);
519 void LifeFrame::OnNavigate(wxCommandEvent
& event
)
523 switch (event
.GetId())
525 case ID_NORTH
: c
= m_life
->FindNorth(); break;
526 case ID_SOUTH
: c
= m_life
->FindSouth(); break;
527 case ID_WEST
: c
= m_life
->FindWest(); break;
528 case ID_EAST
: c
= m_life
->FindEast(); break;
529 case ID_CENTER
: c
= m_life
->FindCenter(); break;
533 case ID_ORIGIN
: c
.i
= c
.j
= 0; break;
536 m_canvas
->Recenter(c
.i
, c
.j
);
539 void LifeFrame::OnSlider(wxScrollEvent
& event
)
541 m_interval
= event
.GetPosition() * 100;
552 void LifeFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
557 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
559 // Stop if it was running; this is absolutely needed because
560 // the frame won't be actually destroyed until there are no
561 // more pending events, and this in turn won't ever happen
562 // if the timer is running faster than the window can redraw.
567 void LifeFrame::OnStart()
571 m_timer
->Start(m_interval
);
577 void LifeFrame::OnStop()
588 void LifeFrame::OnStep()
590 if (m_life
->NextTic())
595 m_canvas
->DrawChanged();
600 // --------------------------------------------------------------------------
601 // LifeNavigator miniframe
602 // --------------------------------------------------------------------------
604 LifeNavigator::LifeNavigator(wxWindow
*parent
)
605 : wxMiniFrame(parent
, wxID_ANY
,
609 wxCAPTION
| wxSIMPLE_BORDER
)
611 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
612 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
613 wxBoxSizer
*sizer2
= new wxBoxSizer(wxHORIZONTAL
);
615 // create bitmaps and masks for the buttons
617 bmpn
= wxBITMAP(north
),
618 bmpw
= wxBITMAP(west
),
619 bmpc
= wxBITMAP(center
),
620 bmpe
= wxBITMAP(east
),
621 bmps
= wxBITMAP(south
);
623 #if !defined(__WXGTK__) && !defined(__WXMOTIF__) && !defined(__WXMAC__)
624 bmpn
.SetMask(new wxMask(bmpn
, *wxLIGHT_GREY
));
625 bmpw
.SetMask(new wxMask(bmpw
, *wxLIGHT_GREY
));
626 bmpc
.SetMask(new wxMask(bmpc
, *wxLIGHT_GREY
));
627 bmpe
.SetMask(new wxMask(bmpe
, *wxLIGHT_GREY
));
628 bmps
.SetMask(new wxMask(bmps
, *wxLIGHT_GREY
));
631 // create the buttons and attach tooltips to them
633 *bn
= new wxBitmapButton(panel
, ID_NORTH
, bmpn
),
634 *bw
= new wxBitmapButton(panel
, ID_WEST
, bmpw
),
635 *bc
= new wxBitmapButton(panel
, ID_CENTER
, bmpc
),
636 *be
= new wxBitmapButton(panel
, ID_EAST
, bmpe
),
637 *bs
= new wxBitmapButton(panel
, ID_SOUTH
, bmps
);
640 bn
->SetToolTip(_("Find northernmost cell"));
641 bw
->SetToolTip(_("Find westernmost cell"));
642 bc
->SetToolTip(_("Find center of mass"));
643 be
->SetToolTip(_("Find easternmost cell"));
644 bs
->SetToolTip(_("Find southernmost cell"));
647 // add buttons to sizers
648 sizer2
->Add( bw
, 0, wxCENTRE
| wxWEST
, 4 );
649 sizer2
->Add( bc
, 0, wxCENTRE
);
650 sizer2
->Add( be
, 0, wxCENTRE
| wxEAST
, 4 );
651 sizer1
->Add( bn
, 0, wxCENTRE
| wxNORTH
, 4 );
652 sizer1
->Add( sizer2
);
653 sizer1
->Add( bs
, 0, wxCENTRE
| wxSOUTH
, 4 );
655 // set the panel and miniframe size
656 panel
->SetSizer(sizer1
);
659 SetClientSize(panel
->GetSize());
660 wxSize sz
= GetSize();
661 SetSizeHints(sz
.x
, sz
.y
, sz
.x
, sz
.y
);
663 // move it to a sensible position
664 wxRect parentRect
= parent
->GetRect();
665 wxSize childSize
= GetSize();
666 int x
= parentRect
.GetX() +
667 parentRect
.GetWidth();
668 int y
= parentRect
.GetY() +
669 (parentRect
.GetHeight() - childSize
.GetHeight()) / 4;
676 void LifeNavigator::OnClose(wxCloseEvent
& event
)
686 // --------------------------------------------------------------------------
688 // --------------------------------------------------------------------------
690 // canvas constructor
691 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
692 : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxSize(100, 100),
693 wxSUNKEN_BORDER
|wxFULL_REPAINT_ON_RESIZE
)
696 m_interactive
= interactive
;
698 m_status
= MOUSE_NOACTION
;
705 SetCursor(*wxCROSS_CURSOR
);
707 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
708 SetBackgroundColour(*wxWHITE
);
711 LifeCanvas::~LifeCanvas()
716 // recenter at the given position
717 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
719 m_viewportX
= i
- m_viewportW
/ 2;
720 m_viewportY
= j
- m_viewportH
/ 2;
726 // set the cell size and refresh display
727 void LifeCanvas::SetCellSize(int cellsize
)
729 m_cellsize
= cellsize
;
731 // find current center
732 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
733 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
735 // get current canvas size and adjust viewport accordingly
737 GetClientSize(&w
, &h
);
738 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
739 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
742 m_viewportX
= cx
- m_viewportW
/ 2;
743 m_viewportY
= cy
- m_viewportH
/ 2;
748 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
749 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
750 m_thumbX
= m_viewportW
;
751 m_thumbY
= m_viewportH
;
758 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
762 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
763 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
770 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
772 wxCoord x
= CellToX(i
);
773 wxCoord y
= CellToY(j
);
775 // if cellsize is 1 or 2, there will be no grid
782 dc
.DrawRectangle(x
, y
, 2, 2);
785 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
789 // draw all changed cells
790 void LifeCanvas::DrawChanged()
798 m_life
->BeginFind(m_viewportX
,
800 m_viewportX
+ m_viewportW
,
801 m_viewportY
+ m_viewportH
,
808 dc
.SetPen(*wxBLACK_PEN
);
812 dc
.SetPen(*wxTRANSPARENT_PEN
);
813 dc
.SetBrush(*wxBLACK_BRUSH
);
815 dc
.SetLogicalFunction(wxINVERT
);
819 done
= m_life
->FindMore(&cells
, &ncells
);
821 for (size_t m
= 0; m
< ncells
; m
++)
822 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
828 void LifeCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
831 wxRect rect
= GetUpdateRegion().GetBox();
833 wxInt32 i0
, j0
, i1
, j1
;
839 h
= rect
.GetHeight();
843 i1
= XToCell(x
+ w
- 1);
844 j1
= YToCell(y
+ h
- 1);
849 m_life
->BeginFind(i0
, j0
, i1
, j1
, false);
850 bool done
= m_life
->FindMore(&cells
, &ncells
);
852 // erase all damaged cells and draw the grid
854 dc
.SetBrush(*wxWHITE_BRUSH
);
859 dc
.SetPen(*wxWHITE_PEN
);
860 dc
.DrawRectangle(x
, y
, w
, h
);
866 w
= CellToX(i1
+ 1) - x
+ 1;
867 h
= CellToY(j1
+ 1) - y
+ 1;
869 dc
.SetPen(*wxLIGHT_GREY_PEN
);
870 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
871 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
872 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
873 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
876 // draw all alive cells
877 dc
.SetPen(*wxBLACK_PEN
);
878 dc
.SetBrush(*wxBLACK_BRUSH
);
882 for (size_t m
= 0; m
< ncells
; m
++)
883 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
885 done
= m_life
->FindMore(&cells
, &ncells
);
889 for (size_t m
= 0; m
< ncells
; m
++)
890 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
895 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
900 // which cell are we pointing at?
901 wxInt32 i
= XToCell( event
.GetX() );
902 wxInt32 j
= YToCell( event
.GetY() );
905 // set statusbar text
907 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
908 ((LifeFrame
*) wxGetApp().GetTopWindow())->SetStatusText(msg
, 1);
909 #endif // wxUSE_STATUSBAR
911 // NOTE that wxMouseEvent::LeftDown() and wxMouseEvent::LeftIsDown()
912 // have different semantics. The first one is used to signal that the
913 // button was just pressed (i.e., in "button down" events); the second
914 // one just describes the current status of the button, independently
915 // of the mouse event type. LeftIsDown is typically used in "mouse
916 // move" events, to test if the button is _still_ pressed.
918 // is the button down?
919 if (!event
.LeftIsDown())
921 m_status
= MOUSE_NOACTION
;
925 // was it pressed just now?
926 if (event
.LeftDown())
928 // yes: start a new action and toggle this cell
929 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
933 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
934 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
936 else if ((m_mi
!= i
) || (m_mj
!= j
))
938 // no: continue ongoing action
939 bool alive
= (m_status
== MOUSE_DRAWING
);
941 // prepare DC and pen + brush to optimize drawing
943 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
944 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
);
1005 ((LifeFrame
*) wxGetApp().GetTopWindow())->UpdateInfoText();
1008 void LifeCanvas::OnSize(wxSizeEvent
& event
)
1011 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
1012 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
1015 wxCoord w
= event
.GetSize().GetX();
1016 wxCoord h
= event
.GetSize().GetY();
1017 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
1018 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
1021 m_viewportX
= cx
- m_viewportW
/ 2;
1022 m_viewportY
= cy
- m_viewportH
/ 2;
1027 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1028 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1029 m_thumbX
= m_viewportW
;
1030 m_thumbY
= m_viewportH
;
1033 // allow default processing
1037 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
1039 WXTYPE type
= (WXTYPE
)event
.GetEventType();
1040 int pos
= event
.GetPosition();
1041 int orient
= event
.GetOrientation();
1043 // calculate scroll increment
1045 if (type
== wxEVT_SCROLLWIN_TOP
)
1047 if (orient
== wxHORIZONTAL
)
1048 scrollinc
= -m_viewportW
;
1050 scrollinc
= -m_viewportH
;
1053 if (type
== wxEVT_SCROLLWIN_BOTTOM
)
1055 if (orient
== wxHORIZONTAL
)
1056 scrollinc
= m_viewportW
;
1058 scrollinc
= m_viewportH
;
1061 if (type
== wxEVT_SCROLLWIN_LINEUP
)
1066 if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
1071 if (type
== wxEVT_SCROLLWIN_PAGEUP
)
1076 if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
1081 if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
1083 if (orient
== wxHORIZONTAL
)
1085 scrollinc
= pos
- m_thumbX
;
1090 scrollinc
= pos
- m_thumbY
;
1095 if (type
== wxEVT_SCROLLWIN_THUMBRELEASE
)
1097 m_thumbX
= m_viewportW
;
1098 m_thumbY
= m_viewportH
;
1101 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1102 // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't);
1103 // so reset it back as we always want it to be in the same position.
1104 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
1106 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1107 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1111 if (scrollinc
== 0) return;
1113 // scroll the window and adjust the viewport
1114 if (orient
== wxHORIZONTAL
)
1116 m_viewportX
+= scrollinc
;
1117 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
1121 m_viewportY
+= scrollinc
;
1122 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
1126 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
1128 // do nothing. I just don't want the background to be erased, you know.