]> git.saurik.com Git - wxWidgets.git/blame - demos/forty/playerdg.cpp
more fixes
[wxWidgets.git] / demos / forty / playerdg.cpp
CommitLineData
63cafd27
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: playerdg.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 "scorefil.h"
31#include "playerdg.h"
32
33const int ID_LISTBOX = 101;
34
35BEGIN_EVENT_TABLE(PlayerSelectionDialog, wxDialog)
36 EVT_SIZE(PlayerSelectionDialog::OnSize)
37 EVT_BUTTON(wxID_OK, PlayerSelectionDialog::ButtonCallback)
38 EVT_BUTTON(wxID_CANCEL, PlayerSelectionDialog::ButtonCallback)
39 EVT_LISTBOX(ID_LISTBOX, PlayerSelectionDialog::SelectCallback)
e3065973 40 EVT_CLOSE(PlayerSelectionDialog::OnCloseWindow)
63cafd27
JS
41END_EVENT_TABLE()
42
43PlayerSelectionDialog::PlayerSelectionDialog(
44 wxWindow* parent,
45 ScoreFile* file
46 ) :
f37c24e0 47 wxDialog(parent, -1, _T("Player Selection"),
66bd6b93 48 wxDefaultPosition, wxSize(320, 200),
63cafd27
JS
49 wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
50 m_scoreFile(file)
51{
52 // enable constraints
53 SetAutoLayout (TRUE);
54
f37c24e0 55 wxStaticText* msg = new wxStaticText(this, -1, _T("Please select a name or type a new one:"));
63cafd27
JS
56
57 wxListBox* list = new wxListBox(
58 this, ID_LISTBOX,
59 wxDefaultPosition, wxDefaultSize,
60 0, 0,
61 wxLB_SINGLE
62 );
7b5408ea 63
54ff4a70
RR
64 wxArrayString players;
65 m_scoreFile->GetPlayerList(players);
cb43b372 66 for (unsigned int i = 0; i < players.Count(); i++)
63cafd27
JS
67 {
68 list->Append(players[i]);
69 }
63cafd27 70
f37c24e0 71 m_textField = new wxTextCtrl(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, 0);
63cafd27 72
f37c24e0
MB
73 m_OK = new wxButton(this, wxID_OK, _T("OK"));
74 m_cancel = new wxButton(this, wxID_CANCEL, _T("Cancel"));
63cafd27
JS
75
76 wxLayoutConstraints* layout;
77
78 // Constrain the msg at the top of the window
79 layout = new wxLayoutConstraints;
80 layout->left.SameAs (this, wxLeft, 10);
81 layout->top.SameAs (this, wxTop, 10);
82 layout->height.AsIs();
83 layout->width.AsIs();
84 msg->SetConstraints(layout);
85
86 // Constrain the OK button
87 layout = new wxLayoutConstraints;
88 layout->left.SameAs (this, wxLeft, 10);
89 layout->bottom.SameAs (this, wxBottom, 10);
90 layout->height.AsIs();
91 layout->width.AsIs();
92 m_OK->SetConstraints(layout);
93
94 // Constrain the OK button
95 layout = new wxLayoutConstraints;
96 layout->left.RightOf (m_OK, 10);
97 layout->bottom.SameAs (this, wxBottom, 10);
98 layout->height.AsIs();
99 layout->width.AsIs();
100 m_cancel->SetConstraints(layout);
101
102 // Constrain the Name text entry field
103 layout = new wxLayoutConstraints;
104 layout->left.SameAs (this, wxLeft, 10);
105 layout->right.SameAs (this, wxRight, 10);
106 layout->bottom.SameAs (m_OK, wxTop, 10);
107 layout->height.AsIs();
108 m_textField->SetConstraints(layout);
109
110 // Constrain the list of players
111 layout = new wxLayoutConstraints;
112 layout->left.SameAs (this, wxLeft, 10);
113 layout->right.SameAs (this, wxRight, 10);
114 layout->top.Below (msg, 10);
115 layout->bottom.SameAs (m_textField, wxTop, 10);
116 list->SetConstraints(layout);
117
118 wxString prevPlayer = m_scoreFile->GetPreviousPlayer();
281b0186 119 if ((prevPlayer.Length() > 0) && (list->FindString(prevPlayer) != -1))
63cafd27
JS
120 {
121 list->SetStringSelection(prevPlayer);
122 m_textField->SetValue(prevPlayer);
123 }
124
5cdeff75
JS
125 m_textField->SetFocus();
126
63cafd27 127 Layout();
82ea63e6
RR
128
129 CentreOnParent();
63cafd27
JS
130}
131
132PlayerSelectionDialog::~PlayerSelectionDialog()
133{
134}
135
cb43b372 136void PlayerSelectionDialog::OnSize(wxSizeEvent& WXUNUSED(event))
63cafd27
JS
137{
138 Layout();
139}
140
141const wxString& PlayerSelectionDialog::GetPlayersName()
142{
7b5408ea 143/*
63cafd27
JS
144 m_player = "";
145 Show(TRUE);
7b5408ea 146*/
63cafd27
JS
147 return m_player;
148}
149
e3065973 150void PlayerSelectionDialog::OnCloseWindow(wxCloseEvent& event)
63cafd27 151{
f37c24e0 152 m_player = _T("");
e3065973 153 EndModal(wxID_CANCEL);
63cafd27
JS
154}
155
156void PlayerSelectionDialog::SelectCallback(wxCommandEvent& event)
157{
158 if (event.GetEventType() == wxEVT_COMMAND_LISTBOX_SELECTED)
159 {
3502e687 160// if (event.IsSelection())
63cafd27
JS
161 m_textField->SetValue(event.GetString());
162 }
163}
164
165void PlayerSelectionDialog::ButtonCallback(wxCommandEvent& event)
166{
167 if (event.GetId() == wxID_OK)
168 {
169 wxString name = m_textField->GetValue();
170 if (!name.IsNull() && name.Length() > 0)
171 {
f37c24e0 172 if (name.Contains(_T('@')))
63cafd27 173 {
f37c24e0 174 wxMessageBox(_T("Names should not contain the '@' character"), _T("Forty Thieves"));
63cafd27
JS
175 }
176 else
177 {
178 m_player = name;
cba2db0c 179 EndModal(wxID_OK);
63cafd27
JS
180 }
181 }
182 else
183 {
f37c24e0 184 wxMessageBox(_T("Please enter your name"), _T("Forty Thieves"));
63cafd27
JS
185 }
186 }
187 else
188 {
f37c24e0 189 m_player = _T("");
cba2db0c 190 EndModal(wxID_CANCEL);
63cafd27
JS
191 }
192}