]> git.saurik.com Git - wxWidgets.git/blob - samples/forty/playerdg.cpp
no message
[wxWidgets.git] / samples / forty / playerdg.cpp
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
33 const int ID_LISTBOX = 101;
34
35 BEGIN_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)
40 END_EVENT_TABLE()
41
42 PlayerSelectionDialog::PlayerSelectionDialog(
43 wxWindow* parent,
44 ScoreFile* file
45 ) :
46 wxDialog(parent, -1, "Player Selection",
47 wxDefaultPosition, wxSize(320, 200),
48 wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
49 m_scoreFile(file)
50 {
51 // enable constraints
52 SetAutoLayout (TRUE);
53
54 wxStaticText* msg = new wxStaticText(this, -1, "Please select a name from the list");
55
56 wxListBox* list = new wxListBox(
57 this, ID_LISTBOX,
58 wxDefaultPosition, wxDefaultSize,
59 0, 0,
60 wxLB_SINGLE
61 );
62
63 wxArrayString players;
64 m_scoreFile->GetPlayerList(players);
65 for (unsigned int i = 0; i < players.Count(); i++)
66 {
67 list->Append(players[i]);
68 }
69
70 m_textField = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize, 0);
71
72 m_OK = new wxButton(this, wxID_OK, "OK");
73 m_cancel = new wxButton(this, wxID_CANCEL, "Cancel");
74
75 wxLayoutConstraints* layout;
76
77 // Constrain the msg at the top of the window
78 layout = new wxLayoutConstraints;
79 layout->left.SameAs (this, wxLeft, 10);
80 layout->top.SameAs (this, wxTop, 10);
81 layout->height.AsIs();
82 layout->width.AsIs();
83 msg->SetConstraints(layout);
84
85 // Constrain the OK button
86 layout = new wxLayoutConstraints;
87 layout->left.SameAs (this, wxLeft, 10);
88 layout->bottom.SameAs (this, wxBottom, 10);
89 layout->height.AsIs();
90 layout->width.AsIs();
91 m_OK->SetConstraints(layout);
92
93 // Constrain the OK button
94 layout = new wxLayoutConstraints;
95 layout->left.RightOf (m_OK, 10);
96 layout->bottom.SameAs (this, wxBottom, 10);
97 layout->height.AsIs();
98 layout->width.AsIs();
99 m_cancel->SetConstraints(layout);
100
101 // Constrain the Name text entry field
102 layout = new wxLayoutConstraints;
103 layout->left.SameAs (this, wxLeft, 10);
104 layout->right.SameAs (this, wxRight, 10);
105 layout->bottom.SameAs (m_OK, wxTop, 10);
106 layout->height.AsIs();
107 m_textField->SetConstraints(layout);
108
109 // Constrain the list of players
110 layout = new wxLayoutConstraints;
111 layout->left.SameAs (this, wxLeft, 10);
112 layout->right.SameAs (this, wxRight, 10);
113 layout->top.Below (msg, 10);
114 layout->bottom.SameAs (m_textField, wxTop, 10);
115 list->SetConstraints(layout);
116
117 wxString prevPlayer = m_scoreFile->GetPreviousPlayer();
118 if (prevPlayer.Length() > 0)
119 {
120 list->SetStringSelection(prevPlayer);
121 m_textField->SetValue(prevPlayer);
122 }
123
124 Layout();
125 }
126
127 PlayerSelectionDialog::~PlayerSelectionDialog()
128 {
129 }
130
131 void PlayerSelectionDialog::OnSize(wxSizeEvent& WXUNUSED(event))
132 {
133 Layout();
134 }
135
136 const wxString& PlayerSelectionDialog::GetPlayersName()
137 {
138 /*
139 m_player = "";
140 Show(TRUE);
141 */
142 return m_player;
143 }
144
145 bool PlayerSelectionDialog::OnClose()
146 {
147 // hide the dialog
148 // NB don't return TRUE otherwise delete is called
149 m_player = "";
150 Show(FALSE);
151 return FALSE;
152 }
153
154 void PlayerSelectionDialog::SelectCallback(wxCommandEvent& event)
155 {
156 if (event.GetEventType() == wxEVT_COMMAND_LISTBOX_SELECTED)
157 {
158 #ifdef __WXGTK__
159 if (event.IsSelection())
160 #endif
161 m_textField->SetValue(event.GetString());
162 }
163 }
164
165 void 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 {
172 if (name.Contains('@'))
173 {
174 wxMessageBox("Names should not contain the '@' character", "Forty Thieves");
175 }
176 else
177 {
178 m_player = name;
179 Show(FALSE);
180 }
181 }
182 else
183 {
184 wxMessageBox("Please enter your name", "Forty Thieves");
185 }
186 }
187 else
188 {
189 m_player = "";
190 Show(FALSE);
191 }
192 }