]> git.saurik.com Git - wxWidgets.git/blob - demos/forty/canvas.cpp
renamed wxComboControl to wxComboCtrl and related wxUSE_XXX and configure switches...
[wxWidgets.git] / demos / forty / canvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: canvas.cpp
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
5 // Modified by:
6 // Created: 21/07/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 //---------------------------------------------------------------------------
11 // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
12 /////////////////////////////////////////////////////////////////////////////
13
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25 #include "forty.h"
26 #include "card.h"
27 #include "game.h"
28 #include "scorefil.h"
29 #include "playerdg.h"
30 #include "canvas.h"
31
32 BEGIN_EVENT_TABLE(FortyCanvas, wxScrolledWindow)
33 EVT_MOUSE_EVENTS(FortyCanvas::OnMouseEvent)
34 END_EVENT_TABLE()
35
36 FortyCanvas::FortyCanvas(wxWindow* parent, const wxPoint& pos, const wxSize& size) :
37 wxScrolledWindow(parent, wxID_ANY, pos, size),
38 m_helpingHand(true),
39 m_rightBtnUndo(true),
40 m_playerDialog(0),
41 m_leftBtnDown(false)
42 {
43 #ifdef __WXGTK__
44 m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
45 #else
46 m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
47 #endif
48 SetBackgroundColour(FortyApp::BackgroundColour());
49
50 m_handCursor = new wxCursor(wxCURSOR_HAND);
51 m_arrowCursor = new wxCursor(wxCURSOR_ARROW);
52
53 wxString name = wxTheApp->GetAppName();
54 if (name.Length() <= 0) name = _T("forty");
55 m_scoreFile = new ScoreFile(name);
56 m_game = new Game(0, 0, 0);
57 m_game->Deal();
58 }
59
60
61 FortyCanvas::~FortyCanvas()
62 {
63 UpdateScores();
64 delete m_game;
65 delete m_scoreFile;
66 delete m_handCursor;
67 delete m_arrowCursor;
68 }
69
70
71 /*
72 Write the current player's score back to the score file
73 */
74 void FortyCanvas::UpdateScores()
75 {
76 if (m_player.Length() > 0 && m_scoreFile && m_game)
77 {
78 m_scoreFile->WritePlayersScore(
79 m_player,
80 m_game->GetNumWins(),
81 m_game->GetNumGames(),
82 m_game->GetScore()
83 );
84 }
85 }
86
87
88 void FortyCanvas::OnDraw(wxDC& dc)
89 {
90 dc.SetFont(* m_font);
91 m_game->Redraw(dc);
92 #if 0
93 // if player name not set (and selection dialog is not displayed)
94 // then ask the player for their name
95 if (m_player.Length() == 0 && !m_playerDialog)
96 {
97 m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile);
98 m_playerDialog->ShowModal();
99 m_player = m_playerDialog->GetPlayersName();
100 if (m_player.Length() > 0)
101 {
102 // user entered a name - lookup their score
103 int wins, games, score;
104 m_scoreFile->ReadPlayersScore(m_player, wins, games, score);
105 m_game->NewPlayer(wins, games, score);
106 m_game->DisplayScore(dc);
107 m_playerDialog->Destroy();
108 m_playerDialog = 0;
109 Refresh(false);
110 }
111 else
112 {
113 // user cancelled the dialog - exit the app
114 ((wxFrame*)GetParent())->Close(true);
115 }
116 }
117 #endif
118 }
119
120 void FortyCanvas::ShowPlayerDialog()
121 {
122 // if player name not set (and selection dialog is not displayed)
123 // then ask the player for their name
124 if (m_player.Length() == 0 && !m_playerDialog)
125 {
126 m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile);
127 m_playerDialog->ShowModal();
128 m_player = m_playerDialog->GetPlayersName();
129 if (m_player.Length() > 0)
130 {
131 // user entered a name - lookup their score
132 int wins, games, score;
133 m_scoreFile->ReadPlayersScore(m_player, wins, games, score);
134 m_game->NewPlayer(wins, games, score);
135
136 wxClientDC dc(this);
137 dc.SetFont(* m_font);
138 m_game->DisplayScore(dc);
139 m_playerDialog->Destroy();
140 m_playerDialog = 0;
141 Refresh(false);
142 }
143 else
144 {
145 // user cancelled the dialog - exit the app
146 ((wxFrame*)GetParent())->Close(true);
147 }
148 }
149 }
150
151 /*
152 Called when the main frame is closed
153 */
154 bool FortyCanvas::OnCloseCanvas()
155 {
156 if (m_game->InPlay() &&
157 wxMessageBox(_T("Are you sure you want to\nabandon the current game?"),
158 _T("Warning"), wxYES_NO | wxICON_QUESTION) == wxNO)
159 {
160 return false;
161 }
162 return true;
163 }
164
165 void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
166 {
167 int mouseX = (int)event.GetX();
168 int mouseY = (int)event.GetY();
169
170 wxClientDC dc(this);
171 PrepareDC(dc);
172 dc.SetFont(* m_font);
173
174 if (event.LeftDClick())
175 {
176 if (m_leftBtnDown)
177 {
178 m_leftBtnDown = false;
179 ReleaseMouse();
180 m_game->LButtonUp(dc, mouseX, mouseY);
181 }
182 m_game->LButtonDblClk(dc, mouseX, mouseY);
183 }
184 else if (event.LeftDown())
185 {
186 if (!m_leftBtnDown)
187 {
188 m_leftBtnDown = true;
189 CaptureMouse();
190 m_game->LButtonDown(dc, mouseX, mouseY);
191 }
192 }
193 else if (event.LeftUp())
194 {
195 if (m_leftBtnDown)
196 {
197 m_leftBtnDown = false;
198 ReleaseMouse();
199 m_game->LButtonUp(dc, mouseX, mouseY);
200 }
201 }
202 else if (event.RightDown() && !event.LeftIsDown())
203 {
204 // only allow right button undo if m_rightBtnUndo is true
205 if (m_rightBtnUndo)
206 {
207 if (event.ControlDown() || event.ShiftDown())
208 {
209 m_game->Redo(dc);
210 }
211 else
212 {
213 m_game->Undo(dc);
214 }
215 }
216 }
217 else if (event.Dragging())
218 {
219 m_game->MouseMove(dc, mouseX, mouseY);
220 }
221
222 if (!event.LeftIsDown())
223 {
224 SetCursorStyle(mouseX, mouseY);
225 }
226 }
227
228 void FortyCanvas::SetCursorStyle(int x, int y)
229 {
230 // Only set cursor to a hand if 'helping hand' is enabled and
231 // the card under the cursor can go somewhere
232 if (m_game->CanYouGo(x, y) && m_helpingHand)
233 {
234 SetCursor(* m_handCursor);
235 }
236 else
237 {
238 SetCursor(* m_arrowCursor);
239 }
240
241 }
242
243 void FortyCanvas::NewGame()
244 {
245 m_game->Deal();
246 Refresh();
247 }
248
249 void FortyCanvas::Undo()
250 {
251 wxClientDC dc(this);
252 PrepareDC(dc);
253 dc.SetFont(* m_font);
254 m_game->Undo(dc);
255 }
256
257 void FortyCanvas::Redo()
258 {
259 wxClientDC dc(this);
260 PrepareDC(dc);
261 dc.SetFont(* m_font);
262 m_game->Redo(dc);
263 }
264
265 void FortyCanvas::LayoutGame()
266 {
267 m_game->Layout();
268 }