]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/canvas.cpp
removed GSocket_[Un]Streamed
[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
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
e0b5519a 41FortyCanvas::FortyCanvas(wxWindow* parent, const wxPoint& pos, const wxSize& size) :
010216e3
WS
42 wxScrolledWindow(parent, wxID_ANY, pos, size),
43 m_helpingHand(true),
44 m_rightBtnUndo(true),
45 m_playerDialog(0),
46 m_leftBtnDown(false)
63cafd27
JS
47{
48#ifdef __WXGTK__
010216e3 49 m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
63cafd27 50#else
010216e3 51 m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
63cafd27 52#endif
010216e3 53 SetBackgroundColour(FortyApp::BackgroundColour());
63cafd27 54
010216e3
WS
55 m_handCursor = new wxCursor(wxCURSOR_HAND);
56 m_arrowCursor = new wxCursor(wxCURSOR_ARROW);
63cafd27 57
010216e3
WS
58 wxString name = wxTheApp->GetAppName();
59 if (name.Length() <= 0) name = _T("forty");
60 m_scoreFile = new ScoreFile(name);
61 m_game = new Game(0, 0, 0);
62 m_game->Deal();
63cafd27
JS
63}
64
65
66FortyCanvas::~FortyCanvas()
67{
010216e3
WS
68 UpdateScores();
69 delete m_game;
70 delete m_scoreFile;
15bee36f
JS
71 delete m_handCursor;
72 delete m_arrowCursor;
63cafd27
JS
73}
74
75
76/*
77Write the current player's score back to the score file
78*/
79void FortyCanvas::UpdateScores()
80{
010216e3
WS
81 if (m_player.Length() > 0 && m_scoreFile && m_game)
82 {
83 m_scoreFile->WritePlayersScore(
84 m_player,
85 m_game->GetNumWins(),
86 m_game->GetNumGames(),
87 m_game->GetScore()
88 );
89 }
63cafd27
JS
90}
91
92
93void FortyCanvas::OnDraw(wxDC& dc)
94{
010216e3
WS
95 dc.SetFont(* m_font);
96 m_game->Redraw(dc);
868741e9 97#if 0
010216e3
WS
98 // if player name not set (and selection dialog is not displayed)
99 // then ask the player for their name
100 if (m_player.Length() == 0 && !m_playerDialog)
101 {
102 m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile);
103 m_playerDialog->ShowModal();
104 m_player = m_playerDialog->GetPlayersName();
105 if (m_player.Length() > 0)
106 {
107 // user entered a name - lookup their score
108 int wins, games, score;
109 m_scoreFile->ReadPlayersScore(m_player, wins, games, score);
110 m_game->NewPlayer(wins, games, score);
111 m_game->DisplayScore(dc);
112 m_playerDialog->Destroy();
113 m_playerDialog = 0;
114 Refresh(false);
115 }
116 else
117 {
118 // user cancelled the dialog - exit the app
119 ((wxFrame*)GetParent())->Close(true);
120 }
121 }
868741e9 122#endif
63cafd27
JS
123}
124
868741e9
JS
125void FortyCanvas::ShowPlayerDialog()
126{
010216e3
WS
127 // if player name not set (and selection dialog is not displayed)
128 // then ask the player for their name
129 if (m_player.Length() == 0 && !m_playerDialog)
130 {
131 m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile);
132 m_playerDialog->ShowModal();
133 m_player = m_playerDialog->GetPlayersName();
134 if (m_player.Length() > 0)
135 {
136 // user entered a name - lookup their score
137 int wins, games, score;
138 m_scoreFile->ReadPlayersScore(m_player, wins, games, score);
139 m_game->NewPlayer(wins, games, score);
868741e9 140
010216e3
WS
141 wxClientDC dc(this);
142 dc.SetFont(* m_font);
143 m_game->DisplayScore(dc);
144 m_playerDialog->Destroy();
145 m_playerDialog = 0;
146 Refresh(false);
868741e9 147 }
010216e3
WS
148 else
149 {
150 // user cancelled the dialog - exit the app
151 ((wxFrame*)GetParent())->Close(true);
152 }
153 }
868741e9
JS
154}
155
63cafd27
JS
156/*
157Called when the main frame is closed
158*/
e3065973 159bool FortyCanvas::OnCloseCanvas()
63cafd27 160{
010216e3
WS
161 if (m_game->InPlay() &&
162 wxMessageBox(_T("Are you sure you want to\nabandon the current game?"),
163 _T("Warning"), wxYES_NO | wxICON_QUESTION) == wxNO)
164 {
e0b5519a 165 return false;
010216e3
WS
166 }
167 return true;
63cafd27
JS
168}
169
170void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
171{
010216e3
WS
172 int mouseX = (int)event.GetX();
173 int mouseY = (int)event.GetY();
63cafd27 174
010216e3
WS
175 wxClientDC dc(this);
176 PrepareDC(dc);
177 dc.SetFont(* m_font);
63cafd27 178
010216e3
WS
179 if (event.LeftDClick())
180 {
181 if (m_leftBtnDown)
182 {
183 m_leftBtnDown = false;
184 ReleaseMouse();
185 m_game->LButtonUp(dc, mouseX, mouseY);
186 }
187 m_game->LButtonDblClk(dc, mouseX, mouseY);
188 }
189 else if (event.LeftDown())
190 {
191 if (!m_leftBtnDown)
192 {
193 m_leftBtnDown = true;
194 CaptureMouse();
195 m_game->LButtonDown(dc, mouseX, mouseY);
196 }
197 }
198 else if (event.LeftUp())
199 {
200 if (m_leftBtnDown)
201 {
202 m_leftBtnDown = false;
203 ReleaseMouse();
204 m_game->LButtonUp(dc, mouseX, mouseY);
205 }
206 }
207 else if (event.RightDown() && !event.LeftIsDown())
208 {
209 // only allow right button undo if m_rightBtnUndo is true
210 if (m_rightBtnUndo)
211 {
212 if (event.ControlDown() || event.ShiftDown())
213 {
214 m_game->Redo(dc);
215 }
216 else
217 {
218 m_game->Undo(dc);
219 }
220 }
221 }
222 else if (event.Dragging())
223 {
224 m_game->MouseMove(dc, mouseX, mouseY);
225 }
63cafd27 226
010216e3
WS
227 if (!event.LeftIsDown())
228 {
229 SetCursorStyle(mouseX, mouseY);
230 }
63cafd27
JS
231}
232
233void FortyCanvas::SetCursorStyle(int x, int y)
234{
010216e3
WS
235 // Only set cursor to a hand if 'helping hand' is enabled and
236 // the card under the cursor can go somewhere
237 if (m_game->CanYouGo(x, y) && m_helpingHand)
238 {
239 SetCursor(* m_handCursor);
240 }
241 else
242 {
243 SetCursor(* m_arrowCursor);
244 }
63cafd27
JS
245
246}
247
248void FortyCanvas::NewGame()
249{
010216e3
WS
250 m_game->Deal();
251 Refresh();
63cafd27
JS
252}
253
254void FortyCanvas::Undo()
255{
010216e3
WS
256 wxClientDC dc(this);
257 PrepareDC(dc);
258 dc.SetFont(* m_font);
259 m_game->Undo(dc);
63cafd27
JS
260}
261
262void FortyCanvas::Redo()
263{
010216e3
WS
264 wxClientDC dc(this);
265 PrepareDC(dc);
266 dc.SetFont(* m_font);
267 m_game->Redo(dc);
63cafd27 268}
fc799548
JS
269
270void FortyCanvas::LayoutGame()
271{
272 m_game->Layout();
273}