]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/canvas.cpp
Updated information on concurrent installs.
[wxWidgets.git] / demos / 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
010216e3 9// Licence: wxWindows licence
63cafd27 10//---------------------------------------------------------------------------
be5a51fb 11// Last modified: 22nd July 1998 - ported to wxWidgets 2.0
63cafd27
JS
12/////////////////////////////////////////////////////////////////////////////
13
63cafd27
JS
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
32BEGIN_EVENT_TABLE(FortyCanvas, wxScrolledWindow)
33 EVT_MOUSE_EVENTS(FortyCanvas::OnMouseEvent)
34END_EVENT_TABLE()
35
e0b5519a 36FortyCanvas::FortyCanvas(wxWindow* parent, const wxPoint& pos, const wxSize& size) :
010216e3
WS
37 wxScrolledWindow(parent, wxID_ANY, pos, size),
38 m_helpingHand(true),
39 m_rightBtnUndo(true),
40 m_playerDialog(0),
41 m_leftBtnDown(false)
63cafd27
JS
42{
43#ifdef __WXGTK__
010216e3 44 m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
63cafd27 45#else
010216e3 46 m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
63cafd27 47#endif
010216e3 48 SetBackgroundColour(FortyApp::BackgroundColour());
63cafd27 49
010216e3
WS
50 m_handCursor = new wxCursor(wxCURSOR_HAND);
51 m_arrowCursor = new wxCursor(wxCURSOR_ARROW);
63cafd27 52
010216e3
WS
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();
63cafd27
JS
58}
59
60
61FortyCanvas::~FortyCanvas()
62{
010216e3
WS
63 UpdateScores();
64 delete m_game;
65 delete m_scoreFile;
15bee36f
JS
66 delete m_handCursor;
67 delete m_arrowCursor;
63cafd27
JS
68}
69
70
71/*
72Write the current player's score back to the score file
73*/
74void FortyCanvas::UpdateScores()
75{
010216e3
WS
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 }
63cafd27
JS
85}
86
87
88void FortyCanvas::OnDraw(wxDC& dc)
89{
010216e3
WS
90 dc.SetFont(* m_font);
91 m_game->Redraw(dc);
868741e9 92#if 0
010216e3
WS
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 }
868741e9 117#endif
63cafd27
JS
118}
119
868741e9
JS
120void FortyCanvas::ShowPlayerDialog()
121{
010216e3
WS
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);
5d2ac6b8 135
010216e3
WS
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);
868741e9 142 }
010216e3
WS
143 else
144 {
145 // user cancelled the dialog - exit the app
146 ((wxFrame*)GetParent())->Close(true);
147 }
148 }
868741e9 149}
5d2ac6b8 150
63cafd27
JS
151/*
152Called when the main frame is closed
153*/
e3065973 154bool FortyCanvas::OnCloseCanvas()
63cafd27 155{
010216e3
WS
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 {
e0b5519a 160 return false;
010216e3
WS
161 }
162 return true;
63cafd27
JS
163}
164
165void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
166{
010216e3
WS
167 int mouseX = (int)event.GetX();
168 int mouseY = (int)event.GetY();
63cafd27 169
5d2ac6b8 170 wxClientDC dc(this);
010216e3
WS
171 PrepareDC(dc);
172 dc.SetFont(* m_font);
63cafd27 173
010216e3
WS
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 }
63cafd27 221
010216e3
WS
222 if (!event.LeftIsDown())
223 {
224 SetCursorStyle(mouseX, mouseY);
225 }
63cafd27
JS
226}
227
228void FortyCanvas::SetCursorStyle(int x, int y)
229{
010216e3 230 // Only set cursor to a hand if 'helping hand' is enabled and
5d2ac6b8 231 // the card under the cursor can go somewhere
010216e3
WS
232 if (m_game->CanYouGo(x, y) && m_helpingHand)
233 {
234 SetCursor(* m_handCursor);
235 }
236 else
237 {
238 SetCursor(* m_arrowCursor);
239 }
63cafd27
JS
240
241}
242
243void FortyCanvas::NewGame()
244{
010216e3
WS
245 m_game->Deal();
246 Refresh();
63cafd27
JS
247}
248
249void FortyCanvas::Undo()
250{
5d2ac6b8 251 wxClientDC dc(this);
010216e3
WS
252 PrepareDC(dc);
253 dc.SetFont(* m_font);
254 m_game->Undo(dc);
63cafd27
JS
255}
256
257void FortyCanvas::Redo()
258{
5d2ac6b8 259 wxClientDC dc(this);
010216e3
WS
260 PrepareDC(dc);
261 dc.SetFont(* m_font);
262 m_game->Redo(dc);
63cafd27 263}
fc799548
JS
264
265void FortyCanvas::LayoutGame()
266{
267 m_game->Layout();
268}