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 wxWidgets, such as wxID_NEW.
74 ID_TIMER
= wxID_HIGHEST
,
97 // speed selection slider
101 // --------------------------------------------------------------------------
102 // event tables and other macros for wxWidgets
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, 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() : 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() !
267 SetStatusText(_("Welcome to Life!"));
268 #endif // wxUSE_STATUSBAR
272 m_timer
= new wxTimer(this, ID_TIMER
);
278 // We use two different panels to reduce flicker in wxGTK, because
279 // some widgets (like wxStaticText) don't have their own X11 window,
280 // and thus updating the text would result in a refresh of the canvas
281 // if they belong to the same parent.
283 wxPanel
*panel1
= new wxPanel(this, wxID_ANY
);
284 wxPanel
*panel2
= new wxPanel(this, wxID_ANY
);
287 m_canvas
= new LifeCanvas(panel1
, m_life
);
290 m_text
= new wxStaticText(panel2
, wxID_ANY
,
294 wxALIGN_CENTER
| wxST_NO_AUTORESIZE
);
296 wxSlider
*slider
= new wxSlider(panel2
, ID_SLIDER
,
299 wxSize(200, wxDefaultCoord
),
300 wxSL_HORIZONTAL
| wxSL_AUTOTICKS
);
305 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
306 wxBoxSizer
*sizer2
= new wxBoxSizer(wxVERTICAL
);
307 wxBoxSizer
*sizer3
= new wxBoxSizer(wxVERTICAL
);
310 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
311 #endif // wxUSE_STATLINE
312 sizer1
->Add( m_canvas
, 1, wxGROW
| wxALL
, 2 );
314 sizer1
->Add( new wxStaticLine(panel1
, wxID_ANY
), 0, wxGROW
);
315 #endif // wxUSE_STATLINE
316 panel1
->SetSizer( sizer1
);
317 sizer1
->Fit( panel1
);
319 sizer2
->Add( m_text
, 0, wxGROW
| wxTOP
, 4 );
320 sizer2
->Add( slider
, 0, wxCENTRE
| wxALL
, 4 );
322 panel2
->SetSizer( sizer2
);
323 sizer2
->Fit( panel2
);
325 sizer3
->Add( panel1
, 1, wxGROW
);
326 sizer3
->Add( panel2
, 0, wxGROW
);
330 // set minimum frame size
331 sizer3
->SetSizeHints( this );
334 m_navigator
= new LifeNavigator(this);
337 LifeFrame::~LifeFrame()
342 void LifeFrame::UpdateInfoText()
346 msg
.Printf(_(" Generation: %u (T: %u ms), Population: %u "),
348 m_topspeed
? 0 : m_interval
,
349 m_life
->GetNumCells());
350 m_text
->SetLabel(msg
);
353 // Enable or disable tools and menu entries according to the current
354 // state. See also wxEVT_UPDATE_UI events for a slightly different
356 void LifeFrame::UpdateUI()
359 GetToolBar()->EnableTool(ID_START
, !m_running
);
360 GetToolBar()->EnableTool(ID_STOP
, m_running
);
361 GetMenuBar()->Enable(ID_START
, !m_running
);
362 GetMenuBar()->Enable(ID_STEP
, !m_running
);
363 GetMenuBar()->Enable(ID_STOP
, m_running
);
364 GetMenuBar()->Enable(ID_TOPSPEED
, !m_topspeed
);
367 int cellsize
= m_canvas
->GetCellSize();
368 GetToolBar()->EnableTool(ID_ZOOMIN
, cellsize
< 32);
369 GetToolBar()->EnableTool(ID_ZOOMOUT
, cellsize
> 1);
370 GetMenuBar()->Enable(ID_ZOOMIN
, cellsize
< 32);
371 GetMenuBar()->Enable(ID_ZOOMOUT
, cellsize
> 1);
374 // Event handlers -----------------------------------------------------------
376 // OnMenu handles all events which don't have their own event handler
377 void LifeFrame::OnMenu(wxCommandEvent
& event
)
379 switch (event
.GetId())
383 // stop if it was running
386 m_canvas
->Recenter(0, 0);
393 LifeAboutDialog
dialog(this);
399 // true is to force the frame to close
405 bool checked
= GetMenuBar()->GetMenu(1)->IsChecked(ID_SHOWNAV
);
406 m_navigator
->Show(checked
);
411 wxString desc
= m_life
->GetDescription();
413 if ( desc
.IsEmpty() )
414 desc
= _("Not available");
416 // should we make the description editable here?
417 wxMessageBox(desc
, _("Description"), wxOK
| wxICON_INFORMATION
);
421 case ID_START
: OnStart(); break;
422 case ID_STEP
: OnStep(); break;
423 case ID_STOP
: OnStop(); break;
429 while (m_running
&& m_topspeed
)
439 void LifeFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
441 wxFileDialog
filedlg(this,
442 _("Choose a file to open"),
445 _("Life patterns (*.lif)|*.lif|All files (*.*)|*.*"),
446 wxOPEN
| wxFILE_MUST_EXIST
);
448 if (filedlg
.ShowModal() == wxID_OK
)
450 wxFileInputStream
stream(filedlg
.GetPath());
451 LifeReader
reader(stream
);
453 // the reader handles errors itself, no need to do anything here
456 // stop if running and put the pattern
459 m_life
->SetPattern(reader
.GetPattern());
462 m_canvas
->Recenter(0, 0);
469 void LifeFrame::OnSamples(wxCommandEvent
& WXUNUSED(event
))
471 // stop if it was running
475 LifeSamplesDialog
dialog(this);
477 if (dialog
.ShowModal() == wxID_OK
)
479 const LifePattern pattern
= dialog
.GetPattern();
483 m_life
->SetPattern(pattern
);
486 m_canvas
->Recenter(0, 0);
492 void LifeFrame::OnZoom(wxCommandEvent
& event
)
494 int cellsize
= m_canvas
->GetCellSize();
496 if ((event
.GetId() == ID_ZOOMIN
) && cellsize
< 32)
498 m_canvas
->SetCellSize(cellsize
* 2);
501 else if ((event
.GetId() == ID_ZOOMOUT
) && cellsize
> 1)
503 m_canvas
->SetCellSize(cellsize
/ 2);
508 void LifeFrame::OnNavigate(wxCommandEvent
& event
)
512 switch (event
.GetId())
514 case ID_NORTH
: c
= m_life
->FindNorth(); break;
515 case ID_SOUTH
: c
= m_life
->FindSouth(); break;
516 case ID_WEST
: c
= m_life
->FindWest(); break;
517 case ID_EAST
: c
= m_life
->FindEast(); break;
518 case ID_CENTER
: c
= m_life
->FindCenter(); break;
522 case ID_ORIGIN
: c
.i
= c
.j
= 0; break;
525 m_canvas
->Recenter(c
.i
, c
.j
);
528 void LifeFrame::OnSlider(wxScrollEvent
& event
)
530 m_interval
= event
.GetPosition() * 100;
541 void LifeFrame::OnTimer(wxTimerEvent
& WXUNUSED(event
))
546 void LifeFrame::OnClose(wxCloseEvent
& WXUNUSED(event
))
548 // Stop if it was running; this is absolutely needed because
549 // the frame won't be actually destroyed until there are no
550 // more pending events, and this in turn won't ever happen
551 // if the timer is running faster than the window can redraw.
556 void LifeFrame::OnStart()
560 m_timer
->Start(m_interval
);
566 void LifeFrame::OnStop()
577 void LifeFrame::OnStep()
579 if (m_life
->NextTic())
584 m_canvas
->DrawChanged();
589 // --------------------------------------------------------------------------
590 // LifeNavigator miniframe
591 // --------------------------------------------------------------------------
593 LifeNavigator::LifeNavigator(wxWindow
*parent
)
594 : wxMiniFrame(parent
, wxID_ANY
,
598 wxCAPTION
| wxSIMPLE_BORDER
)
600 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
601 wxBoxSizer
*sizer1
= new wxBoxSizer(wxVERTICAL
);
602 wxBoxSizer
*sizer2
= new wxBoxSizer(wxHORIZONTAL
);
604 // create bitmaps and masks for the buttons
606 bmpn
= wxBITMAP(north
),
607 bmpw
= wxBITMAP(west
),
608 bmpc
= wxBITMAP(center
),
609 bmpe
= wxBITMAP(east
),
610 bmps
= wxBITMAP(south
);
612 #if !defined(__WXGTK__) && !defined(__WXMOTIF__) && !defined(__WXMAC__)
613 bmpn
.SetMask(new wxMask(bmpn
, *wxLIGHT_GREY
));
614 bmpw
.SetMask(new wxMask(bmpw
, *wxLIGHT_GREY
));
615 bmpc
.SetMask(new wxMask(bmpc
, *wxLIGHT_GREY
));
616 bmpe
.SetMask(new wxMask(bmpe
, *wxLIGHT_GREY
));
617 bmps
.SetMask(new wxMask(bmps
, *wxLIGHT_GREY
));
620 // create the buttons and attach tooltips to them
622 *bn
= new wxBitmapButton(panel
, ID_NORTH
, bmpn
),
623 *bw
= new wxBitmapButton(panel
, ID_WEST
, bmpw
),
624 *bc
= new wxBitmapButton(panel
, ID_CENTER
, bmpc
),
625 *be
= new wxBitmapButton(panel
, ID_EAST
, bmpe
),
626 *bs
= new wxBitmapButton(panel
, ID_SOUTH
, bmps
);
629 bn
->SetToolTip(_("Find northernmost cell"));
630 bw
->SetToolTip(_("Find westernmost cell"));
631 bc
->SetToolTip(_("Find center of mass"));
632 be
->SetToolTip(_("Find easternmost cell"));
633 bs
->SetToolTip(_("Find southernmost cell"));
636 // add buttons to sizers
637 sizer2
->Add( bw
, 0, wxCENTRE
| wxWEST
, 4 );
638 sizer2
->Add( bc
, 0, wxCENTRE
);
639 sizer2
->Add( be
, 0, wxCENTRE
| wxEAST
, 4 );
640 sizer1
->Add( bn
, 0, wxCENTRE
| wxNORTH
, 4 );
641 sizer1
->Add( sizer2
);
642 sizer1
->Add( bs
, 0, wxCENTRE
| wxSOUTH
, 4 );
644 // set the panel and miniframe size
645 panel
->SetSizer(sizer1
);
648 SetClientSize(panel
->GetSize());
649 wxSize sz
= GetSize();
650 SetSizeHints(sz
.x
, sz
.y
, sz
.x
, sz
.y
);
652 // move it to a sensible position
653 wxRect parentRect
= parent
->GetRect();
654 wxSize childSize
= GetSize();
655 int x
= parentRect
.GetX() +
656 parentRect
.GetWidth();
657 int y
= parentRect
.GetY() +
658 (parentRect
.GetHeight() - childSize
.GetHeight()) / 4;
665 void LifeNavigator::OnClose(wxCloseEvent
& event
)
675 // --------------------------------------------------------------------------
677 // --------------------------------------------------------------------------
679 // canvas constructor
680 LifeCanvas::LifeCanvas(wxWindow
*parent
, Life
*life
, bool interactive
)
681 : wxWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxSize(100, 100),
682 wxSUNKEN_BORDER
|wxFULL_REPAINT_ON_RESIZE
)
685 m_interactive
= interactive
;
687 m_status
= MOUSE_NOACTION
;
694 SetCursor(*wxCROSS_CURSOR
);
696 // reduce flicker if wxEVT_ERASE_BACKGROUND is not available
697 SetBackgroundColour(*wxWHITE
);
700 LifeCanvas::~LifeCanvas()
705 // recenter at the given position
706 void LifeCanvas::Recenter(wxInt32 i
, wxInt32 j
)
708 m_viewportX
= i
- m_viewportW
/ 2;
709 m_viewportY
= j
- m_viewportH
/ 2;
715 // set the cell size and refresh display
716 void LifeCanvas::SetCellSize(int cellsize
)
718 m_cellsize
= cellsize
;
720 // find current center
721 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
722 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
724 // get current canvas size and adjust viewport accordingly
726 GetClientSize(&w
, &h
);
727 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
728 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
731 m_viewportX
= cx
- m_viewportW
/ 2;
732 m_viewportY
= cy
- m_viewportH
/ 2;
737 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
738 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
739 m_thumbX
= m_viewportW
;
740 m_thumbY
= m_viewportH
;
747 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, bool alive
)
751 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
752 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
759 void LifeCanvas::DrawCell(wxInt32 i
, wxInt32 j
, wxDC
&dc
)
761 wxCoord x
= CellToX(i
);
762 wxCoord y
= CellToY(j
);
764 // if cellsize is 1 or 2, there will be no grid
771 dc
.DrawRectangle(x
, y
, 2, 2);
774 dc
.DrawRectangle(x
+ 1, y
+ 1, m_cellsize
- 1, m_cellsize
- 1);
778 // draw all changed cells
779 void LifeCanvas::DrawChanged()
787 m_life
->BeginFind(m_viewportX
,
789 m_viewportX
+ m_viewportW
,
790 m_viewportY
+ m_viewportH
,
797 dc
.SetPen(*wxBLACK_PEN
);
801 dc
.SetPen(*wxTRANSPARENT_PEN
);
802 dc
.SetBrush(*wxBLACK_BRUSH
);
804 dc
.SetLogicalFunction(wxINVERT
);
808 done
= m_life
->FindMore(&cells
, &ncells
);
810 for (size_t m
= 0; m
< ncells
; m
++)
811 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
817 void LifeCanvas::OnPaint(wxPaintEvent
& WXUNUSED(event
))
820 wxRect rect
= GetUpdateRegion().GetBox();
822 wxInt32 i0
, j0
, i1
, j1
;
828 h
= rect
.GetHeight();
832 i1
= XToCell(x
+ w
- 1);
833 j1
= YToCell(y
+ h
- 1);
838 m_life
->BeginFind(i0
, j0
, i1
, j1
, false);
839 bool done
= m_life
->FindMore(&cells
, &ncells
);
841 // erase all damaged cells and draw the grid
843 dc
.SetBrush(*wxWHITE_BRUSH
);
848 dc
.SetPen(*wxWHITE_PEN
);
849 dc
.DrawRectangle(x
, y
, w
, h
);
855 w
= CellToX(i1
+ 1) - x
+ 1;
856 h
= CellToY(j1
+ 1) - y
+ 1;
858 dc
.SetPen(*wxLIGHT_GREY_PEN
);
859 for (wxInt32 yy
= y
; yy
<= (y
+ h
- m_cellsize
); yy
+= m_cellsize
)
860 dc
.DrawRectangle(x
, yy
, w
, m_cellsize
+ 1);
861 for (wxInt32 xx
= x
; xx
<= (x
+ w
- m_cellsize
); xx
+= m_cellsize
)
862 dc
.DrawLine(xx
, y
, xx
, y
+ h
);
865 // draw all alive cells
866 dc
.SetPen(*wxBLACK_PEN
);
867 dc
.SetBrush(*wxBLACK_BRUSH
);
871 for (size_t m
= 0; m
< ncells
; m
++)
872 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
874 done
= m_life
->FindMore(&cells
, &ncells
);
878 for (size_t m
= 0; m
< ncells
; m
++)
879 DrawCell(cells
[m
].i
, cells
[m
].j
, dc
);
884 void LifeCanvas::OnMouse(wxMouseEvent
& event
)
889 // which cell are we pointing at?
890 wxInt32 i
= XToCell( event
.GetX() );
891 wxInt32 j
= YToCell( event
.GetY() );
894 // set statusbar text
896 msg
.Printf(_("Cell: (%d, %d)"), i
, j
);
897 ((LifeFrame
*) wxGetApp().GetTopWindow())->SetStatusText(msg
, 1);
898 #endif // wxUSE_STATUSBAR
900 // NOTE that wxMouseEvent::LeftDown() and wxMouseEvent::LeftIsDown()
901 // have different semantics. The first one is used to signal that the
902 // button was just pressed (i.e., in "button down" events); the second
903 // one just describes the current status of the button, independently
904 // of the mouse event type. LeftIsDown is typically used in "mouse
905 // move" events, to test if the button is _still_ pressed.
907 // is the button down?
908 if (!event
.LeftIsDown())
910 m_status
= MOUSE_NOACTION
;
914 // was it pressed just now?
915 if (event
.LeftDown())
917 // yes: start a new action and toggle this cell
918 m_status
= (m_life
->IsAlive(i
, j
)? MOUSE_ERASING
: MOUSE_DRAWING
);
922 m_life
->SetCell(i
, j
, m_status
== MOUSE_DRAWING
);
923 DrawCell(i
, j
, m_status
== MOUSE_DRAWING
);
925 else if ((m_mi
!= i
) || (m_mj
!= j
))
927 // no: continue ongoing action
928 bool alive
= (m_status
== MOUSE_DRAWING
);
930 // prepare DC and pen + brush to optimize drawing
932 dc
.SetPen(alive
? *wxBLACK_PEN
: *wxWHITE_PEN
);
933 dc
.SetBrush(alive
? *wxBLACK_BRUSH
: *wxWHITE_BRUSH
);
936 // draw a line of cells using Bresenham's algorithm
937 wxInt32 d
, ii
, jj
, di
, ai
, si
, dj
, aj
, sj
;
940 si
= (di
< 0)? -1 : 1;
943 sj
= (dj
< 0)? -1 : 1;
955 m_life
->SetCell(ii
, jj
, alive
);
956 DrawCell(ii
, jj
, dc
);
973 m_life
->SetCell(ii
, jj
, alive
);
974 DrawCell(ii
, jj
, dc
);
986 m_life
->SetCell(ii
, jj
, alive
);
987 DrawCell(ii
, jj
, dc
);
994 ((LifeFrame
*) wxGetApp().GetTopWindow())->UpdateInfoText();
997 void LifeCanvas::OnSize(wxSizeEvent
& event
)
1000 wxInt32 cx
= m_viewportX
+ m_viewportW
/ 2;
1001 wxInt32 cy
= m_viewportY
+ m_viewportH
/ 2;
1004 wxCoord w
= event
.GetSize().GetX();
1005 wxCoord h
= event
.GetSize().GetY();
1006 m_viewportW
= (w
+ m_cellsize
- 1) / m_cellsize
;
1007 m_viewportH
= (h
+ m_cellsize
- 1) / m_cellsize
;
1010 m_viewportX
= cx
- m_viewportW
/ 2;
1011 m_viewportY
= cy
- m_viewportH
/ 2;
1016 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1017 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1018 m_thumbX
= m_viewportW
;
1019 m_thumbY
= m_viewportH
;
1022 // allow default processing
1026 void LifeCanvas::OnScroll(wxScrollWinEvent
& event
)
1028 WXTYPE type
= event
.GetEventType();
1029 int pos
= event
.GetPosition();
1030 int orient
= event
.GetOrientation();
1032 // calculate scroll increment
1034 if (type
== wxEVT_SCROLLWIN_TOP
)
1036 if (orient
== wxHORIZONTAL
)
1037 scrollinc
= -m_viewportW
;
1039 scrollinc
= -m_viewportH
;
1042 if (type
== wxEVT_SCROLLWIN_BOTTOM
)
1044 if (orient
== wxHORIZONTAL
)
1045 scrollinc
= m_viewportW
;
1047 scrollinc
= m_viewportH
;
1050 if (type
== wxEVT_SCROLLWIN_LINEUP
)
1055 if (type
== wxEVT_SCROLLWIN_LINEDOWN
)
1060 if (type
== wxEVT_SCROLLWIN_PAGEUP
)
1065 if (type
== wxEVT_SCROLLWIN_PAGEDOWN
)
1070 if (type
== wxEVT_SCROLLWIN_THUMBTRACK
)
1072 if (orient
== wxHORIZONTAL
)
1074 scrollinc
= pos
- m_thumbX
;
1079 scrollinc
= pos
- m_thumbY
;
1084 if (type
== wxEVT_SCROLLWIN_THUMBRELEASE
)
1086 m_thumbX
= m_viewportW
;
1087 m_thumbY
= m_viewportH
;
1090 #if defined(__WXGTK__) || defined(__WXMOTIF__)
1091 // wxGTK and wxMotif update the thumb automatically (wxMSW doesn't);
1092 // so reset it back as we always want it to be in the same position.
1093 if (type
!= wxEVT_SCROLLWIN_THUMBTRACK
)
1095 SetScrollbar(wxHORIZONTAL
, m_viewportW
, m_viewportW
, 3 * m_viewportW
);
1096 SetScrollbar(wxVERTICAL
, m_viewportH
, m_viewportH
, 3 * m_viewportH
);
1100 if (scrollinc
== 0) return;
1102 // scroll the window and adjust the viewport
1103 if (orient
== wxHORIZONTAL
)
1105 m_viewportX
+= scrollinc
;
1106 ScrollWindow( -m_cellsize
* scrollinc
, 0, (const wxRect
*) NULL
);
1110 m_viewportY
+= scrollinc
;
1111 ScrollWindow( 0, -m_cellsize
* scrollinc
, (const wxRect
*) NULL
);
1115 void LifeCanvas::OnEraseBackground(wxEraseEvent
& WXUNUSED(event
))
1117 // do nothing. I just don't want the background to be erased, you know.