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, wxEmptyString, bmp, wxNullBitmap, wxITEM_NORMAL, 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();
171 frame
->UpdateInfoText();
174 // enter the main message loop and run the app
178 // --------------------------------------------------------------------------
180 // --------------------------------------------------------------------------
183 LifeFrame::LifeFrame() :
184 wxFrame( (wxFrame
*) NULL
, wxID_ANY
, _("Life!"), wxDefaultPosition
),
188 SetIcon(wxICON(mondrian
));
191 wxMenu
*menuFile
= new wxMenu(wxMENU_TEAROFF
);
192 wxMenu
*menuView
= new wxMenu(wxMENU_TEAROFF
);
193 wxMenu
*menuGame
= new wxMenu(wxMENU_TEAROFF
);
194 wxMenu
*menuHelp
= new wxMenu(wxMENU_TEAROFF
);
196 menuFile
->Append(wxID_NEW
, wxEmptyString
, _("Start a new game"));
198 menuFile
->Append(wxID_OPEN
, wxEmptyString
, _("Open an existing Life pattern"));
200 menuFile
->Append(ID_SAMPLES
, _("&Sample game..."), _("Select a sample configuration"));
201 #if ! (defined(__SMARTPHONE__) || defined(__POCKETPC__))
202 menuFile
->AppendSeparator();
203 menuFile
->Append(wxID_EXIT
);
205 menuView
->Append(ID_SHOWNAV
, _("Navigation &toolbox"), _("Show or hide toolbox"), wxITEM_CHECK
);
206 menuView
->Check(ID_SHOWNAV
, true);
207 menuView
->AppendSeparator();
210 menuView
->Append(ID_ORIGIN
, _("&Absolute origin"), _("Go to (0, 0)"));
211 menuView
->Append(ID_CENTER
, _("&Center of mass"), _("Find center of mass"));
212 menuView
->Append(ID_NORTH
, _("&North"), _("Find northernmost cell"));
213 menuView
->Append(ID_SOUTH
, _("&South"), _("Find southernmost cell"));
214 menuView
->Append(ID_EAST
, _("&East"), _("Find easternmost cell"));
215 menuView
->Append(ID_WEST
, _("&West"), _("Find westernmost cell"));
216 menuView
->AppendSeparator();
217 menuView
->Append(wxID_ZOOM_IN
, wxEmptyString
, _("Zoom in"));
218 menuView
->Append(wxID_ZOOM_OUT
, wxEmptyString
, _("Zoom out"));
219 menuView
->Append(ID_INFO
, _("&Description\tCtrl-D"), _("View pattern description"));
221 menuGame
->Append(ID_START
, _("&Start\tCtrl-S"), _("Start"));
222 menuGame
->Append(ID_STEP
, _("&Next\tCtrl-N"), _("Single step"));
223 menuGame
->Append(wxID_STOP
, wxEmptyString
, _("Stop"));
224 menuGame
->Enable(wxID_STOP
, false);
225 menuGame
->AppendSeparator();
226 menuGame
->Append(ID_TOPSPEED
, _("T&op speed!"), _("Go as fast as possible"));
228 menuHelp
->Append(wxID_ABOUT
, _("&About\tCtrl-A"), _("Show about dialog"));
230 wxMenuBar
*menuBar
= new wxMenuBar();
231 menuBar
->Append(menuFile
, _("&File"));
232 menuBar
->Append(menuView
, _("&View"));
233 menuBar
->Append(menuGame
, _("&Game"));
234 menuBar
->Append(menuHelp
, _("&Help"));
238 wxBitmap tbBitmaps
[7];
240 tbBitmaps
[0] = wxBITMAP(reset
);
241 tbBitmaps
[1] = wxBITMAP(open
);
242 tbBitmaps
[2] = wxBITMAP(zoomin
);
243 tbBitmaps
[3] = wxBITMAP(zoomout
);
244 tbBitmaps
[4] = wxBITMAP(info
);
245 tbBitmaps
[5] = wxBITMAP(play
);
246 tbBitmaps
[6] = wxBITMAP(stop
);
248 wxToolBar
*toolBar
= CreateToolBar();
249 toolBar
->SetMargins(5, 5);
250 toolBar
->SetToolBitmapSize(wxSize(16, 16));
252 ADD_TOOL(wxID_NEW
, tbBitmaps
[0], wxGetStockLabel(wxID_NEW
, wxSTOCK_NOFLAGS
), _("Start a new game"));
255 ADD_TOOL(wxID_OPEN
, tbBitmaps
[1], wxGetStockLabel(wxID_OPEN
, wxSTOCK_NOFLAGS
), _("Open an existing Life pattern"));
256 #endif // wxUSE_FILEDLG
258 toolBar
->AddSeparator();
259 ADD_TOOL(wxID_ZOOM_IN
, tbBitmaps
[2], wxGetStockLabel(wxID_ZOOM_IN
, wxSTOCK_NOFLAGS
), _("Zoom in"));
260 ADD_TOOL(wxID_ZOOM_OUT
, tbBitmaps
[3], wxGetStockLabel(wxID_ZOOM_OUT
, wxSTOCK_NOFLAGS
), _("Zoom out"));
261 ADD_TOOL(ID_INFO
, tbBitmaps
[4], _("Description"), _("Show description"));
262 toolBar
->AddSeparator();
263 #endif // __POCKETPC__
264 ADD_TOOL(ID_START
, tbBitmaps
[5], _("Start"), _("Start"));
265 ADD_TOOL(wxID_STOP
, tbBitmaps
[6], _("Stop"), _("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: %lu (T: %lu ms), Population: %lu "),
358 m_topspeed
? 0 : m_interval
,
359 static_cast<unsigned long>(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
)
451 void LifeFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
453 wxFileDialog
filedlg(this,
454 _("Choose a file to open"),
457 _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"),
458 wxFD_OPEN
| wxFD_FILE_MUST_EXIST
);
460 if (filedlg
.ShowModal() == wxID_OK
)
462 wxFileInputStream
stream(filedlg
.GetPath());
463 LifeReader
reader(stream
);
465 // the reader handles errors itself, no need to do anything here
468 // stop if running and put the pattern
471 m_life
->SetPattern(reader
.GetPattern());
474 m_canvas
->Recenter(0, 0);
482 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
484 // stop if it was running
488 LifeSamplesDialog
dialog(this);
490 if (dialog
.ShowModal() == wxID_OK
)
492 const LifePattern pattern
= dialog
.GetPattern();
496 m_life
->SetPattern(pattern
);
499 m_canvas
->Recenter(0, 0);
505 void LifeFrame::OnZoom(wxCommandEvent
& event
)
507 int cellsize
= m_canvas
->GetCellSize();
509 if ((event
.GetId() == wxID_ZOOM_IN
) && cellsize
< 32)
511 m_canvas
->SetCellSize(cellsize
* 2);
514 else if ((event
.GetId() == wxID_ZOOM_OUT
) && cellsize
> 1)
516 m_canvas
->SetCellSize(cellsize
/ 2);
521 void LifeFrame::OnNavigate(wxCommandEvent
& event
)
525 switch (event
.GetId())
527 case ID_NORTH
: c
= m_life
->FindNorth(); break;
528 case ID_SOUTH
: c
= m_life
->FindSouth(); break;
529 case ID_WEST
: c
= m_life
->FindWest(); break;
530 case ID_EAST
: c
= m_life
->FindEast(); break;
531 case ID_CENTER
: c
= m_life
->FindCenter(); break;
535 case ID_ORIGIN
: c
.i
= c
.j
= 0; break;
538 m_canvas
->Recenter(c
.i
, c
.j
);
541 void LifeFrame::OnSlider(wxScrollEvent
& event
)
543 m_interval
= event
.GetPosition() * 100;
554 void LifeFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
559 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
561 // Stop if it was running; this is absolutely needed because
562 // the frame won't be actually destroyed until there are no
563 // more pending events, and this in turn won't ever happen
564 // if the timer is running faster than the window can redraw.
569 void LifeFrame::OnStart()
573 m_timer
->Start(m_interval
);
579 void LifeFrame::OnStop()
590 void LifeFrame::OnStep()
592 if (m_life
->NextTic())
597 m_canvas
->DrawChanged();
602 // --------------------------------------------------------------------------
603 // LifeNavigator miniframe
604 // --------------------------------------------------------------------------
606 LifeNavigator::LifeNavigator(wxWindow
*parent
)
607 : wxMiniFrame(parent
, wxID_ANY
,
611 wxCAPTION
| wxSIMPLE_BORDER
)
613 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
614 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
615 wxBoxSizer
*sizer2
= new wxBoxSizer(wxHORIZONTAL
);
617 // create bitmaps and masks for the buttons
619 bmpn
= wxBITMAP(north
),
620 bmpw
= wxBITMAP(west
),
621 bmpc
= wxBITMAP(center
),
622 bmpe
= wxBITMAP(east
),
623 bmps
= wxBITMAP(south
);
625 #if !defined(__WXGTK__) && !defined(__WXMOTIF__) && !defined(__WXMAC__)
626 bmpn
.SetMask(new wxMask(bmpn
, *wxLIGHT_GREY
));
627 bmpw
.SetMask(new wxMask(bmpw
, *wxLIGHT_GREY
));
628 bmpc
.SetMask(new wxMask(bmpc
, *wxLIGHT_GREY
));
629 bmpe
.SetMask(new wxMask(bmpe
, *wxLIGHT_GREY
));
630 bmps
.SetMask(new wxMask(bmps
, *wxLIGHT_GREY
));
633 // create the buttons and attach tooltips to them
635 *bn
= new wxBitmapButton(panel
, ID_NORTH
, bmpn
),
636 *bw
= new wxBitmapButton(panel
, ID_WEST
, bmpw
),
637 *bc
= new wxBitmapButton(panel
, ID_CENTER
, bmpc
),
638 *be
= new wxBitmapButton(panel
, ID_EAST
, bmpe
),
639 *bs
= new wxBitmapButton(panel
, ID_SOUTH
, bmps
);
642 bn
->SetToolTip(_("Find northernmost cell"));
643 bw
->SetToolTip(_("Find westernmost cell"));
644 bc
->SetToolTip(_("Find center of mass"));
645 be
->SetToolTip(_("Find easternmost cell"));
646 bs
->SetToolTip(_("Find southernmost cell"));
649 // add buttons to sizers
650 sizer2
->Add( bw
, 0, wxCENTRE
| wxWEST
, 4 );
651 sizer2
->Add( bc
, 0, wxCENTRE
);
652 sizer2
->Add( be
, 0, wxCENTRE
| wxEAST
, 4 );
653 sizer1
->Add( bn
, 0, wxCENTRE
| wxNORTH
, 4 );
654 sizer1
->Add( sizer2
);
655 sizer1
->Add( bs
, 0, wxCENTRE
| wxSOUTH
, 4 );
657 // set the panel and miniframe size
658 panel
->SetSizer(sizer1
);
661 SetClientSize(panel
->GetSize());
662 wxSize sz
= GetSize();
663 SetSizeHints(sz
.x
, sz
.y
, sz
.x
, sz
.y
);
665 // move it to a sensible position
666 wxRect parentRect
= parent
->GetRect();
667 wxSize childSize
= GetSize();
668 int x
= parentRect
.GetX() +
669 parentRect
.GetWidth();
670 int y
= parentRect
.GetY() +
671 (parentRect
.GetHeight() - childSize
.GetHeight()) / 4;
678 void LifeNavigator::OnClose(wxCloseEvent
& event
)
688 // --------------------------------------------------------------------------
690 // --------------------------------------------------------------------------
692 // canvas constructor
693 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
694 : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxSize(100, 100),
695 wxFULL_REPAINT_ON_RESIZE
| wxHSCROLL
| wxVSCROLL
696 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
704 m_interactive
= interactive
;
706 m_status
= MOUSE_NOACTION
;
713 SetCursor(*wxCROSS_CURSOR
);
715 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
716 SetBackgroundColour(*wxWHITE
);
719 LifeCanvas::~LifeCanvas()
724 // recenter at the given position
725 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
727 m_viewportX
= i
- m_viewportW
/ 2;
728 m_viewportY
= j
- m_viewportH
/ 2;
734 // set the cell size and refresh display
735 void LifeCanvas::SetCellSize(int cellsize
)
737 m_cellsize
= cellsize
;
739 // find current center
740 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
741 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
743 // get current canvas size and adjust viewport accordingly
745 GetClientSize(&w
, &h
);
746 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
747 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
750 m_viewportX
= cx
- m_viewportW
/ 2;
751 m_viewportY
= cy
- m_viewportH
/ 2;
756 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
757 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
758 m_thumbX
= m_viewportW
;
759 m_thumbY
= m_viewportH
;
766 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
770 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
771 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
776 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
778 wxCoord x
= CellToX(i
);
779 wxCoord y
= CellToY(j
);
781 // if cellsize is 1 or 2, there will be no grid
788 dc
.DrawRectangle(x
, y
, 2, 2);
791 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
795 // draw all changed cells
796 void LifeCanvas::DrawChanged()
804 m_life
->BeginFind(m_viewportX
,
806 m_viewportX
+ m_viewportW
,
807 m_viewportY
+ m_viewportH
,
812 dc
.SetPen(*wxBLACK_PEN
);
816 dc
.SetPen(*wxTRANSPARENT_PEN
);
817 dc
.SetBrush(*wxBLACK_BRUSH
);
819 dc
.SetLogicalFunction(wxINVERT
);
823 done
= m_life
->FindMore(&cells
, &ncells
);
825 for (size_t m
= 0; m
< ncells
; m
++)
826 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
831 void LifeCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
834 wxRect rect
= GetUpdateRegion().GetBox();
836 wxInt32 i0
, j0
, i1
, j1
;
842 h
= rect
.GetHeight();
846 i1
= XToCell(x
+ w
- 1);
847 j1
= YToCell(y
+ h
- 1);
852 m_life
->BeginFind(i0
, j0
, i1
, j1
, false);
853 bool done
= m_life
->FindMore(&cells
, &ncells
);
855 // erase all damaged cells and draw the grid
856 dc
.SetBrush(*wxWHITE_BRUSH
);
861 dc
.SetPen(*wxWHITE_PEN
);
862 dc
.DrawRectangle(x
, y
, w
, h
);
868 w
= CellToX(i1
+ 1) - x
+ 1;
869 h
= CellToY(j1
+ 1) - y
+ 1;
871 dc
.SetPen(*wxLIGHT_GREY_PEN
);
872 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
873 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
874 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
875 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
878 // draw all alive cells
879 dc
.SetPen(*wxBLACK_PEN
);
880 dc
.SetBrush(*wxBLACK_BRUSH
);
884 for (size_t m
= 0; m
< ncells
; m
++)
885 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
887 done
= m_life
->FindMore(&cells
, &ncells
);
891 for (size_t m
= 0; m
< ncells
; m
++)
892 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
);
946 // draw a line of cells using Bresenham's algorithm
947 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
950 si
= (di
< 0)? -1 : 1;
953 sj
= (dj
< 0)? -1 : 1;
965 m_life
->SetCell(ii
, jj
, alive
);
966 DrawCell(ii
, jj
, dc
);
983 m_life
->SetCell(ii
, jj
, alive
);
984 DrawCell(ii
, jj
, dc
);
996 m_life
->SetCell(ii
, jj
, alive
);
997 DrawCell(ii
, jj
, dc
);
1002 ((LifeFrame
*) wxGetApp().GetTopWindow())->UpdateInfoText();
1005 void LifeCanvas::OnSize(wxSizeEvent
& event
)
1008 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
1009 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
1012 wxCoord w
= event
.GetSize().GetX();
1013 wxCoord h
= event
.GetSize().GetY();
1014 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
1015 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
1018 m_viewportX
= cx
- m_viewportW
/ 2;
1019 m_viewportY
= cy
- m_viewportH
/ 2;
1024 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1025 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1026 m_thumbX
= m_viewportW
;
1027 m_thumbY
= m_viewportH
;
1030 // allow default processing
1034 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
1036 WXTYPE type
= (WXTYPE
)event
.GetEventType();
1037 int pos
= event
.GetPosition();
1038 int orient
= event
.GetOrientation();
1040 // calculate scroll increment
1042 if (type
== wxEVT_SCROLLWIN_TOP
)
1044 if (orient
== wxHORIZONTAL
)
1045 scrollinc
= -m_viewportW
;
1047 scrollinc
= -m_viewportH
;
1050 if (type
== wxEVT_SCROLLWIN_BOTTOM
)
1052 if (orient
== wxHORIZONTAL
)
1053 scrollinc
= m_viewportW
;
1055 scrollinc
= m_viewportH
;
1058 if (type
== wxEVT_SCROLLWIN_LINEUP
)
1063 if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
1068 if (type
== wxEVT_SCROLLWIN_PAGEUP
)
1073 if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
1078 if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
1080 if (orient
== wxHORIZONTAL
)
1082 scrollinc
= pos
- m_thumbX
;
1087 scrollinc
= pos
- m_thumbY
;
1092 if (type
== wxEVT_SCROLLWIN_THUMBRELEASE
)
1094 m_thumbX
= m_viewportW
;
1095 m_thumbY
= m_viewportH
;
1098 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1099 // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't);
1100 // so reset it back as we always want it to be in the same position.
1101 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
1103 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1104 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1108 if (scrollinc
== 0) return;
1110 // scroll the window and adjust the viewport
1111 if (orient
== wxHORIZONTAL
)
1113 m_viewportX
+= scrollinc
;
1114 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
1118 m_viewportY
+= scrollinc
;
1119 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
1123 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
1125 // do nothing. I just don't want the background to be erased, you know.