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