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