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
)
107 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 (wxID_ZOOM_IN
, LifeFrame::OnZoom
)
120 EVT_MENU (wxID_ZOOM_OUT
,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 (wxID_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, wxDefaultCoord, wxDefaultCoord, (wxObject *)NULL, 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() :
189 wxFrame( (wxFrame
*) NULL
, wxID_ANY
, _("Life!"), wxDefaultPosition
),
193 SetIcon(wxICON(mondrian
));
196 wxMenu
*menuFile
= new wxMenu(wxMENU_TEAROFF
);
197 wxMenu
*menuView
= new wxMenu(wxMENU_TEAROFF
);
198 wxMenu
*menuGame
= new wxMenu(wxMENU_TEAROFF
);
199 wxMenu
*menuHelp
= new wxMenu(wxMENU_TEAROFF
);
201 menuFile
->Append(wxID_NEW
, wxGetStockLabel(wxID_NEW
), _("Start a new game"));
203 menuFile
->Append(wxID_OPEN
, wxGetStockLabel(wxID_OPEN
), _("Open an existing Life pattern"));
205 menuFile
->Append(ID_SAMPLES
, _("&Sample game..."), _("Select a sample configuration"));
206 #if ! (defined(__SMARTPHONE__) || defined(__POCKETPC__))
207 menuFile
->AppendSeparator();
208 menuFile
->Append(wxID_EXIT
, wxGetStockLabel(wxID_EXIT
, true, _T("Alt-X")), _("Quit this program"));
210 menuView
->Append(ID_SHOWNAV
, _("Navigation &toolbox"), _("Show or hide toolbox"), wxITEM_CHECK
);
211 menuView
->Check(ID_SHOWNAV
, true);
212 menuView
->AppendSeparator();
215 menuView
->Append(ID_ORIGIN
, _("&Absolute origin"), _("Go to (0, 0)"));
216 menuView
->Append(ID_CENTER
, _("&Center of mass"), _("Find center of mass"));
217 menuView
->Append(ID_NORTH
, _("&North"), _("Find northernmost cell"));
218 menuView
->Append(ID_SOUTH
, _("&South"), _("Find southernmost cell"));
219 menuView
->Append(ID_EAST
, _("&East"), _("Find easternmost cell"));
220 menuView
->Append(ID_WEST
, _("&West"), _("Find westernmost cell"));
221 menuView
->AppendSeparator();
222 menuView
->Append(wxID_ZOOM_IN
, wxGetStockLabel(wxID_ZOOM_IN
, true, _T("Ctrl-I")), _("Zoom in"));
223 menuView
->Append(wxID_ZOOM_OUT
, wxGetStockLabel(wxID_ZOOM_OUT
, true, _T("Ctrl-O")), _("Zoom out"));
224 menuView
->Append(ID_INFO
, _("&Description\tCtrl-D"), _("View pattern description"));
226 menuGame
->Append(ID_START
, _("&Start\tCtrl-S"), _("Start"));
227 menuGame
->Append(ID_STEP
, _("&Next\tCtrl-N"), _("Single step"));
228 menuGame
->Append(wxID_STOP
, wxGetStockLabel(wxID_STOP
, true, _T("Ctrl-T")), _("Stop"));
229 menuGame
->Enable(wxID_STOP
, false);
230 menuGame
->AppendSeparator();
231 menuGame
->Append(ID_TOPSPEED
, _("T&op speed!"), _("Go as fast as possible"));
233 menuHelp
->Append(wxID_ABOUT
, _("&About\tCtrl-A"), _("Show about dialog"));
235 wxMenuBar
*menuBar
= new wxMenuBar();
236 menuBar
->Append(menuFile
, _("&File"));
237 menuBar
->Append(menuView
, _("&View"));
238 menuBar
->Append(menuGame
, _("&Game"));
239 menuBar
->Append(menuHelp
, _("&Help"));
243 wxBitmap tbBitmaps
[7];
245 tbBitmaps
[0] = wxBITMAP(reset
);
246 tbBitmaps
[1] = wxBITMAP(open
);
247 tbBitmaps
[2] = wxBITMAP(zoomin
);
248 tbBitmaps
[3] = wxBITMAP(zoomout
);
249 tbBitmaps
[4] = wxBITMAP(info
);
250 tbBitmaps
[5] = wxBITMAP(play
);
251 tbBitmaps
[6] = wxBITMAP(stop
);
253 wxToolBar
*toolBar
= CreateToolBar();
254 toolBar
->SetMargins(5, 5);
255 toolBar
->SetToolBitmapSize(wxSize(16, 16));
257 ADD_TOOL(wxID_NEW
, tbBitmaps
[0], wxGetStockLabel(wxID_NEW
, false), _("Start a new game"));
260 ADD_TOOL(wxID_OPEN
, tbBitmaps
[1], wxGetStockLabel(wxID_OPEN
, false), _("Open an existing Life pattern"));
261 #endif // wxUSE_FILEDLG
263 toolBar
->AddSeparator();
264 ADD_TOOL(wxID_ZOOM_IN
, tbBitmaps
[2], wxGetStockLabel(wxID_ZOOM_IN
, false), _("Zoom in"));
265 ADD_TOOL(wxID_ZOOM_OUT
, tbBitmaps
[3], wxGetStockLabel(wxID_ZOOM_OUT
, false), _("Zoom out"));
266 ADD_TOOL(ID_INFO
, tbBitmaps
[4], _("Description"), _("Show description"));
267 toolBar
->AddSeparator();
268 #endif // __POCKETPC__
269 ADD_TOOL(ID_START
, tbBitmaps
[5], _("Start"), _("Start"));
270 ADD_TOOL(wxID_STOP
, tbBitmaps
[6], wxGetStockLabel(wxID_STOP
, false), _("Stop"));
273 toolBar
->EnableTool(wxID_STOP
, false); // must be after Realize() !
278 SetStatusText(_("Welcome to Life!"));
279 #endif // wxUSE_STATUSBAR
283 m_timer
= new wxTimer(this, ID_TIMER
);
289 // We use two different panels to reduce flicker in wxGTK, because
290 // some widgets (like wxStaticText) don't have their own X11 window,
291 // and thus updating the text would result in a refresh of the canvas
292 // if they belong to the same parent.
294 wxPanel
*panel1
= new wxPanel(this, wxID_ANY
);
295 wxPanel
*panel2
= new wxPanel(this, wxID_ANY
);
298 m_canvas
= new LifeCanvas(panel1
, m_life
);
301 m_text
= new wxStaticText(panel2
, wxID_ANY
,
305 wxALIGN_CENTER
| wxST_NO_AUTORESIZE
);
307 wxSlider
*slider
= new wxSlider(panel2
, ID_SLIDER
,
310 wxSize(200, wxDefaultCoord
),
311 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
316 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
317 wxBoxSizer
*sizer2
= new wxBoxSizer(wxVERTICAL
);
318 wxBoxSizer
*sizer3
= new wxBoxSizer(wxVERTICAL
);
321 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
322 #endif // wxUSE_STATLINE
323 sizer1
->Add( m_canvas
, 1, wxGROW
| wxALL
, 2 );
325 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
326 #endif // wxUSE_STATLINE
327 panel1
->SetSizer( sizer1
);
328 sizer1
->Fit( panel1
);
330 sizer2
->Add( m_text
, 0, wxGROW
| wxTOP
, 4 );
331 sizer2
->Add( slider
, 0, wxCENTRE
| wxALL
, 4 );
333 panel2
->SetSizer( sizer2
);
334 sizer2
->Fit( panel2
);
336 sizer3
->Add( panel1
, 1, wxGROW
);
337 sizer3
->Add( panel2
, 0, wxGROW
);
343 // set minimum frame size
344 sizer3
->SetSizeHints( this );
346 // navigator frame - not appropriate for small devices
347 m_navigator
= new LifeNavigator(this);
352 LifeFrame::~LifeFrame()
357 void LifeFrame::UpdateInfoText()
361 msg
.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
363 m_topspeed
? 0 : m_interval
,
364 m_life
->GetNumCells());
365 m_text
->SetLabel(msg
);
368 // Enable or disable tools and menu entries according to the current
369 // state. See also wxEVT_UPDATE_UI events for a slightly different
371 void LifeFrame::UpdateUI()
374 GetToolBar()->EnableTool(ID_START
, !m_running
);
375 GetToolBar()->EnableTool(wxID_STOP
, m_running
);
376 GetMenuBar()->Enable(ID_START
, !m_running
);
377 GetMenuBar()->Enable(ID_STEP
, !m_running
);
378 GetMenuBar()->Enable(wxID_STOP
, m_running
);
379 GetMenuBar()->Enable(ID_TOPSPEED
, !m_topspeed
);
382 int cellsize
= m_canvas
->GetCellSize();
383 GetToolBar()->EnableTool(wxID_ZOOM_IN
, cellsize
< 32);
384 GetToolBar()->EnableTool(wxID_ZOOM_OUT
, cellsize
> 1);
385 GetMenuBar()->Enable(wxID_ZOOM_IN
, cellsize
< 32);
386 GetMenuBar()->Enable(wxID_ZOOM_OUT
, cellsize
> 1);
389 // Event handlers -----------------------------------------------------------
391 // OnMenu handles all events which don't have their own event handler
392 void LifeFrame::OnMenu(wxCommandEvent
& event
)
394 switch (event
.GetId())
398 // stop if it was running
401 m_canvas
->Recenter(0, 0);
408 LifeAboutDialog
dialog(this);
414 // true is to force the frame to close
420 bool checked
= GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV
);
422 m_navigator
->Show(checked
);
427 wxString desc
= m_life
->GetDescription();
430 desc
= _("Not available");
432 // should we make the description editable here?
433 wxMessageBox(desc
, _("Description"), wxOK
| wxICON_INFORMATION
);
437 case ID_START
: OnStart(); break;
438 case ID_STEP
: OnStep(); break;
439 case wxID_STOP
: OnStop(); break;
445 while (m_running
&& m_topspeed
)
456 void LifeFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
458 wxFileDialog
filedlg(this,
459 _("Choose a file to open"),
462 _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"),
463 wxOPEN
| wxFILE_MUST_EXIST
);
465 if (filedlg
.ShowModal() == wxID_OK
)
467 wxFileInputStream
stream(filedlg
.GetPath());
468 LifeReader
reader(stream
);
470 // the reader handles errors itself, no need to do anything here
473 // stop if running and put the pattern
476 m_life
->SetPattern(reader
.GetPattern());
479 m_canvas
->Recenter(0, 0);
487 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
489 // stop if it was running
493 LifeSamplesDialog
dialog(this);
495 if (dialog
.ShowModal() == wxID_OK
)
497 const LifePattern pattern
= dialog
.GetPattern();
501 m_life
->SetPattern(pattern
);
504 m_canvas
->Recenter(0, 0);
510 void LifeFrame::OnZoom(wxCommandEvent
& event
)
512 int cellsize
= m_canvas
->GetCellSize();
514 if ((event
.GetId() == wxID_ZOOM_IN
) && cellsize
< 32)
516 m_canvas
->SetCellSize(cellsize
* 2);
519 else if ((event
.GetId() == wxID_ZOOM_OUT
) && cellsize
> 1)
521 m_canvas
->SetCellSize(cellsize
/ 2);
526 void LifeFrame::OnNavigate(wxCommandEvent
& event
)
530 switch (event
.GetId())
532 case ID_NORTH
: c
= m_life
->FindNorth(); break;
533 case ID_SOUTH
: c
= m_life
->FindSouth(); break;
534 case ID_WEST
: c
= m_life
->FindWest(); break;
535 case ID_EAST
: c
= m_life
->FindEast(); break;
536 case ID_CENTER
: c
= m_life
->FindCenter(); break;
540 case ID_ORIGIN
: c
.i
= c
.j
= 0; break;
543 m_canvas
->Recenter(c
.i
, c
.j
);
546 void LifeFrame::OnSlider(wxScrollEvent
& event
)
548 m_interval
= event
.GetPosition() * 100;
559 void LifeFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
564 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
566 // Stop if it was running; this is absolutely needed because
567 // the frame won't be actually destroyed until there are no
568 // more pending events, and this in turn won't ever happen
569 // if the timer is running faster than the window can redraw.
574 void LifeFrame::OnStart()
578 m_timer
->Start(m_interval
);
584 void LifeFrame::OnStop()
595 void LifeFrame::OnStep()
597 if (m_life
->NextTic())
602 m_canvas
->DrawChanged();
607 // --------------------------------------------------------------------------
608 // LifeNavigator miniframe
609 // --------------------------------------------------------------------------
611 LifeNavigator::LifeNavigator(wxWindow
*parent
)
612 : wxMiniFrame(parent
, wxID_ANY
,
616 wxCAPTION
| wxSIMPLE_BORDER
)
618 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
619 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
620 wxBoxSizer
*sizer2
= new wxBoxSizer(wxHORIZONTAL
);
622 // create bitmaps and masks for the buttons
624 bmpn
= wxBITMAP(north
),
625 bmpw
= wxBITMAP(west
),
626 bmpc
= wxBITMAP(center
),
627 bmpe
= wxBITMAP(east
),
628 bmps
= wxBITMAP(south
);
630 #if !defined(__WXGTK__) && !defined(__WXMOTIF__) && !defined(__WXMAC__)
631 bmpn
.SetMask(new wxMask(bmpn
, *wxLIGHT_GREY
));
632 bmpw
.SetMask(new wxMask(bmpw
, *wxLIGHT_GREY
));
633 bmpc
.SetMask(new wxMask(bmpc
, *wxLIGHT_GREY
));
634 bmpe
.SetMask(new wxMask(bmpe
, *wxLIGHT_GREY
));
635 bmps
.SetMask(new wxMask(bmps
, *wxLIGHT_GREY
));
638 // create the buttons and attach tooltips to them
640 *bn
= new wxBitmapButton(panel
, ID_NORTH
, bmpn
),
641 *bw
= new wxBitmapButton(panel
, ID_WEST
, bmpw
),
642 *bc
= new wxBitmapButton(panel
, ID_CENTER
, bmpc
),
643 *be
= new wxBitmapButton(panel
, ID_EAST
, bmpe
),
644 *bs
= new wxBitmapButton(panel
, ID_SOUTH
, bmps
);
647 bn
->SetToolTip(_("Find northernmost cell"));
648 bw
->SetToolTip(_("Find westernmost cell"));
649 bc
->SetToolTip(_("Find center of mass"));
650 be
->SetToolTip(_("Find easternmost cell"));
651 bs
->SetToolTip(_("Find southernmost cell"));
654 // add buttons to sizers
655 sizer2
->Add( bw
, 0, wxCENTRE
| wxWEST
, 4 );
656 sizer2
->Add( bc
, 0, wxCENTRE
);
657 sizer2
->Add( be
, 0, wxCENTRE
| wxEAST
, 4 );
658 sizer1
->Add( bn
, 0, wxCENTRE
| wxNORTH
, 4 );
659 sizer1
->Add( sizer2
);
660 sizer1
->Add( bs
, 0, wxCENTRE
| wxSOUTH
, 4 );
662 // set the panel and miniframe size
663 panel
->SetSizer(sizer1
);
666 SetClientSize(panel
->GetSize());
667 wxSize sz
= GetSize();
668 SetSizeHints(sz
.x
, sz
.y
, sz
.x
, sz
.y
);
670 // move it to a sensible position
671 wxRect parentRect
= parent
->GetRect();
672 wxSize childSize
= GetSize();
673 int x
= parentRect
.GetX() +
674 parentRect
.GetWidth();
675 int y
= parentRect
.GetY() +
676 (parentRect
.GetHeight() - childSize
.GetHeight()) / 4;
683 void LifeNavigator::OnClose(wxCloseEvent
& event
)
693 // --------------------------------------------------------------------------
695 // --------------------------------------------------------------------------
697 // canvas constructor
698 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
699 : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxSize(100, 100),
700 wxFULL_REPAINT_ON_RESIZE
701 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
709 m_interactive
= interactive
;
711 m_status
= MOUSE_NOACTION
;
718 SetCursor(*wxCROSS_CURSOR
);
720 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
721 SetBackgroundColour(*wxWHITE
);
724 LifeCanvas::~LifeCanvas()
729 // recenter at the given position
730 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
732 m_viewportX
= i
- m_viewportW
/ 2;
733 m_viewportY
= j
- m_viewportH
/ 2;
739 // set the cell size and refresh display
740 void LifeCanvas::SetCellSize(int cellsize
)
742 m_cellsize
= cellsize
;
744 // find current center
745 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
746 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
748 // get current canvas size and adjust viewport accordingly
750 GetClientSize(&w
, &h
);
751 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
752 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
755 m_viewportX
= cx
- m_viewportW
/ 2;
756 m_viewportY
= cy
- m_viewportH
/ 2;
761 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
762 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
763 m_thumbX
= m_viewportW
;
764 m_thumbY
= m_viewportH
;
771 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
775 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
776 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
783 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
785 wxCoord x
= CellToX(i
);
786 wxCoord y
= CellToY(j
);
788 // if cellsize is 1 or 2, there will be no grid
795 dc
.DrawRectangle(x
, y
, 2, 2);
798 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
802 // draw all changed cells
803 void LifeCanvas::DrawChanged()
811 m_life
->BeginFind(m_viewportX
,
813 m_viewportX
+ m_viewportW
,
814 m_viewportY
+ m_viewportH
,
821 dc
.SetPen(*wxBLACK_PEN
);
825 dc
.SetPen(*wxTRANSPARENT_PEN
);
826 dc
.SetBrush(*wxBLACK_BRUSH
);
828 dc
.SetLogicalFunction(wxINVERT
);
832 done
= m_life
->FindMore(&cells
, &ncells
);
834 for (size_t m
= 0; m
< ncells
; m
++)
835 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
841 void LifeCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
844 wxRect rect
= GetUpdateRegion().GetBox();
846 wxInt32 i0
, j0
, i1
, j1
;
852 h
= rect
.GetHeight();
856 i1
= XToCell(x
+ w
- 1);
857 j1
= YToCell(y
+ h
- 1);
862 m_life
->BeginFind(i0
, j0
, i1
, j1
, false);
863 bool done
= m_life
->FindMore(&cells
, &ncells
);
865 // erase all damaged cells and draw the grid
867 dc
.SetBrush(*wxWHITE_BRUSH
);
872 dc
.SetPen(*wxWHITE_PEN
);
873 dc
.DrawRectangle(x
, y
, w
, h
);
879 w
= CellToX(i1
+ 1) - x
+ 1;
880 h
= CellToY(j1
+ 1) - y
+ 1;
882 dc
.SetPen(*wxLIGHT_GREY_PEN
);
883 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
884 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
885 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
886 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
889 // draw all alive cells
890 dc
.SetPen(*wxBLACK_PEN
);
891 dc
.SetBrush(*wxBLACK_BRUSH
);
895 for (size_t m
= 0; m
< ncells
; m
++)
896 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
898 done
= m_life
->FindMore(&cells
, &ncells
);
902 for (size_t m
= 0; m
< ncells
; m
++)
903 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
908 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
913 // which cell are we pointing at?
914 wxInt32 i
= XToCell( event
.GetX() );
915 wxInt32 j
= YToCell( event
.GetY() );
918 // set statusbar text
920 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
921 ((LifeFrame
*) wxGetApp().GetTopWindow())->SetStatusText(msg
, 1);
922 #endif // wxUSE_STATUSBAR
924 // NOTE that wxMouseEvent::LeftDown() and wxMouseEvent::LeftIsDown()
925 // have different semantics. The first one is used to signal that the
926 // button was just pressed (i.e., in "button down" events); the second
927 // one just describes the current status of the button, independently
928 // of the mouse event type. LeftIsDown is typically used in "mouse
929 // move" events, to test if the button is _still_ pressed.
931 // is the button down?
932 if (!event
.LeftIsDown())
934 m_status
= MOUSE_NOACTION
;
938 // was it pressed just now?
939 if (event
.LeftDown())
941 // yes: start a new action and toggle this cell
942 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
946 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
947 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
949 else if ((m_mi
!= i
) || (m_mj
!= j
))
951 // no: continue ongoing action
952 bool alive
= (m_status
== MOUSE_DRAWING
);
954 // prepare DC and pen + brush to optimize drawing
956 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
957 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
960 // draw a line of cells using Bresenham's algorithm
961 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
964 si
= (di
< 0)? -1 : 1;
967 sj
= (dj
< 0)? -1 : 1;
979 m_life
->SetCell(ii
, jj
, alive
);
980 DrawCell(ii
, jj
, dc
);
997 m_life
->SetCell(ii
, jj
, alive
);
998 DrawCell(ii
, jj
, dc
);
1010 m_life
->SetCell(ii
, jj
, alive
);
1011 DrawCell(ii
, jj
, dc
);
1018 ((LifeFrame
*) wxGetApp().GetTopWindow())->UpdateInfoText();
1021 void LifeCanvas::OnSize(wxSizeEvent
& event
)
1024 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
1025 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
1028 wxCoord w
= event
.GetSize().GetX();
1029 wxCoord h
= event
.GetSize().GetY();
1030 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
1031 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
1034 m_viewportX
= cx
- m_viewportW
/ 2;
1035 m_viewportY
= cy
- m_viewportH
/ 2;
1040 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1041 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1042 m_thumbX
= m_viewportW
;
1043 m_thumbY
= m_viewportH
;
1046 // allow default processing
1050 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
1052 WXTYPE type
= (WXTYPE
)event
.GetEventType();
1053 int pos
= event
.GetPosition();
1054 int orient
= event
.GetOrientation();
1056 // calculate scroll increment
1058 if (type
== wxEVT_SCROLLWIN_TOP
)
1060 if (orient
== wxHORIZONTAL
)
1061 scrollinc
= -m_viewportW
;
1063 scrollinc
= -m_viewportH
;
1066 if (type
== wxEVT_SCROLLWIN_BOTTOM
)
1068 if (orient
== wxHORIZONTAL
)
1069 scrollinc
= m_viewportW
;
1071 scrollinc
= m_viewportH
;
1074 if (type
== wxEVT_SCROLLWIN_LINEUP
)
1079 if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
1084 if (type
== wxEVT_SCROLLWIN_PAGEUP
)
1089 if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
1094 if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
1096 if (orient
== wxHORIZONTAL
)
1098 scrollinc
= pos
- m_thumbX
;
1103 scrollinc
= pos
- m_thumbY
;
1108 if (type
== wxEVT_SCROLLWIN_THUMBRELEASE
)
1110 m_thumbX
= m_viewportW
;
1111 m_thumbY
= m_viewportH
;
1114 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1115 // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't);
1116 // so reset it back as we always want it to be in the same position.
1117 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
1119 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1120 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1124 if (scrollinc
== 0) return;
1126 // scroll the window and adjust the viewport
1127 if (orient
== wxHORIZONTAL
)
1129 m_viewportX
+= scrollinc
;
1130 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
1134 m_viewportY
+= scrollinc
;
1135 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
1139 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
1141 // do nothing. I just don't want the background to be erased, you know.