]> git.saurik.com Git - wxWidgets.git/blob - samples/forty/playerdg.cpp
Added ODBC database classes and sample from RemStar (sample needs work for wxWin 2)
[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(250, 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 #if 1
63 // Robert Roebling
64
65 int numPlayers = 0;
66 wxString* players = 0;
67 m_scoreFile->GetPlayerList(&players, numPlayers);
68 for (int i = 0; i < numPlayers; i++)
69 {
70 list->Append(players[i]);
71 }
72 delete players;
73 #endif
74
75 m_textField = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize, 0);
76
77 m_OK = new wxButton(this, wxID_OK, "OK");
78 m_cancel = new wxButton(this, wxID_CANCEL, "Cancel");
79
80 wxLayoutConstraints* layout;
81
82 // Constrain the msg at the top of the window
83 layout = new wxLayoutConstraints;
84 layout->left.SameAs (this, wxLeft, 10);
85 layout->top.SameAs (this, wxTop, 10);
86 layout->height.AsIs();
87 layout->width.AsIs();
88 msg->SetConstraints(layout);
89
90 // Constrain the OK button
91 layout = new wxLayoutConstraints;
92 layout->left.SameAs (this, wxLeft, 10);
93 layout->bottom.SameAs (this, wxBottom, 10);
94 layout->height.AsIs();
95 layout->width.AsIs();
96 m_OK->SetConstraints(layout);
97
98 // Constrain the OK button
99 layout = new wxLayoutConstraints;
100 layout->left.RightOf (m_OK, 10);
101 layout->bottom.SameAs (this, wxBottom, 10);
102 layout->height.AsIs();
103 layout->width.AsIs();
104 m_cancel->SetConstraints(layout);
105
106 // Constrain the Name text entry field
107 layout = new wxLayoutConstraints;
108 layout->left.SameAs (this, wxLeft, 10);
109 layout->right.SameAs (this, wxRight, 10);
110 layout->bottom.SameAs (m_OK, wxTop, 10);
111 layout->height.AsIs();
112 m_textField->SetConstraints(layout);
113
114 // Constrain the list of players
115 layout = new wxLayoutConstraints;
116 layout->left.SameAs (this, wxLeft, 10);
117 layout->right.SameAs (this, wxRight, 10);
118 layout->top.Below (msg, 10);
119 layout->bottom.SameAs (m_textField, wxTop, 10);
120 list->SetConstraints(layout);
121
122 wxString prevPlayer = m_scoreFile->GetPreviousPlayer();
123 if (prevPlayer.Length() > 0)
124 {
125 list->SetStringSelection(prevPlayer);
126 m_textField->SetValue(prevPlayer);
127 }
128
129 Layout();
130 }
131
132 PlayerSelectionDialog::~PlayerSelectionDialog()
133 {
134 }
135
136 void PlayerSelectionDialog::OnSize(wxSizeEvent& event)
137 {
138 Layout();
139 }
140
141 const wxString& PlayerSelectionDialog::GetPlayersName()
142 {
143 /*
144 m_player = "";
145 Show(TRUE);
146 */
147 return m_player;
148 }
149
150 bool PlayerSelectionDialog::OnClose()
151 {
152 // hide the dialog
153 // NB don't return TRUE otherwise delete is called
154 m_player = "";
155 Show(FALSE);
156 return FALSE;
157 }
158
159 void PlayerSelectionDialog::SelectCallback(wxCommandEvent& event)
160 {
161 if (event.GetEventType() == wxEVT_COMMAND_LISTBOX_SELECTED)
162 {
163 #ifdef __WXGTK__
164 if (event.IsSelection())
165 #endif
166 m_textField->SetValue(event.GetString());
167 }
168 }
169
170 void PlayerSelectionDialog::ButtonCallback(wxCommandEvent& event)
171 {
172 if (event.GetId() == wxID_OK)
173 {
174 wxString name = m_textField->GetValue();
175 if (!name.IsNull() && name.Length() > 0)
176 {
177 if (name.Contains('@'))
178 {
179 wxMessageBox("Names should not contain the '@' character", "Forty Thieves");
180 }
181 else
182 {
183 m_player = name;
184 Show(FALSE);
185 }
186 }
187 else
188 {
189 wxMessageBox("Please enter your name", "Forty Thieves");
190 }
191 }
192 else
193 {
194 m_player = "";
195 Show(FALSE);
196 }
197 }