]> git.saurik.com Git - wxWidgets.git/blob - samples/forty/playerdg.cpp
Dialog Editor edits
[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 int numPlayers = 0;
63 wxString* players = 0;
64 m_scoreFile->GetPlayerList(&players, numPlayers);
65 for (int i = 0; i < numPlayers; i++)
66 {
67 list->Append(players[i]);
68 }
69 delete players;
70
71 m_textField = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize, 0);
72
73 m_OK = new wxButton(this, wxID_OK, "OK");
74 m_cancel = new wxButton(this, wxID_CANCEL, "Cancel");
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();
119 if (prevPlayer.Length() > 0)
120 {
121 list->SetStringSelection(prevPlayer);
122 m_textField->SetValue(prevPlayer);
123 }
124
125 Layout();
126 }
127
128 PlayerSelectionDialog::~PlayerSelectionDialog()
129 {
130 }
131
132 void PlayerSelectionDialog::OnSize(wxSizeEvent& event)
133 {
134 Layout();
135 }
136
137 const wxString& PlayerSelectionDialog::GetPlayersName()
138 {
139 m_player = "";
140 Show(TRUE);
141 return m_player;
142 }
143
144 bool PlayerSelectionDialog::OnClose()
145 {
146 // hide the dialog
147 // NB don't return TRUE otherwise delete is called
148 m_player = "";
149 Show(FALSE);
150 return FALSE;
151 }
152
153 void PlayerSelectionDialog::SelectCallback(wxCommandEvent& event)
154 {
155 if (event.GetEventType() == wxEVT_COMMAND_LISTBOX_SELECTED)
156 {
157 #ifdef __WXGTK__
158 if (event.IsSelection())
159 #endif
160 m_textField->SetValue(event.GetString());
161 }
162 }
163
164 void PlayerSelectionDialog::ButtonCallback(wxCommandEvent& event)
165 {
166 if (event.GetId() == wxID_OK)
167 {
168 wxString name = m_textField->GetValue();
169 if (!name.IsNull() && name.Length() > 0)
170 {
171 if (name.Contains('@'))
172 {
173 wxMessageBox("Names should not contain the '@' character", "Forty Thieves");
174 }
175 else
176 {
177 m_player = name;
178 Show(FALSE);
179 }
180 }
181 else
182 {
183 wxMessageBox("Please enter your name", "Forty Thieves");
184 }
185 }
186 else
187 {
188 m_player = "";
189 Show(FALSE);
190 }
191 }