]> git.saurik.com Git - wxWidgets.git/blob - demos/forty/playerdg.cpp
Some tweaks
[wxWidgets.git] / demos / 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 EVT_CLOSE(PlayerSelectionDialog::OnCloseWindow)
41 END_EVENT_TABLE()
42
43 PlayerSelectionDialog::PlayerSelectionDialog(
44 wxWindow* parent,
45 ScoreFile* file
46 ) :
47 wxDialog(parent, wxID_ANY, _T("Player Selection"),
48 wxDefaultPosition, wxDefaultSize,
49 wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
50 m_scoreFile(file)
51 {
52 wxStaticText* msg = new wxStaticText(this, wxID_ANY, _T("Please select a name or type a new one:"));
53
54 wxListBox* list = new wxListBox(
55 this, ID_LISTBOX,
56 wxDefaultPosition, wxDefaultSize,
57 0, 0,
58 wxLB_SINGLE
59 );
60
61 wxArrayString players;
62 m_scoreFile->GetPlayerList(players);
63 for (unsigned int i = 0; i < players.Count(); i++)
64 {
65 list->Append(players[i]);
66 }
67
68 m_textField = new wxTextCtrl(this, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize, 0);
69
70 m_OK = new wxButton(this, wxID_OK, _T("OK"));
71 m_cancel = new wxButton(this, wxID_CANCEL, _T("Cancel"));
72
73 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
74 button_sizer->Add( m_OK, 0, wxALL, 10 );
75 button_sizer->Add( m_cancel, 0, wxALL, 10 );
76
77 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
78 topsizer->Add( msg, 0, wxALL , 10 );
79 topsizer->Add( list, 1, wxEXPAND | wxLEFT | wxRIGHT, 10 );
80 topsizer->Add( m_textField, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10 );
81 topsizer->Add( button_sizer, 0, wxALIGN_LEFT );
82
83 SetSizer( topsizer );
84
85 topsizer->SetSizeHints( this );
86
87 CentreOnParent();
88 }
89
90 PlayerSelectionDialog::~PlayerSelectionDialog()
91 {
92 }
93
94 void PlayerSelectionDialog::OnSize(wxSizeEvent& WXUNUSED(event))
95 {
96 Layout();
97 }
98
99 const wxString& PlayerSelectionDialog::GetPlayersName()
100 {
101 /*
102 m_player = "";
103 Show(true);
104 */
105 return m_player;
106 }
107
108 void PlayerSelectionDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
109 {
110 m_player = _T("");
111 EndModal(wxID_CANCEL);
112 }
113
114 void PlayerSelectionDialog::SelectCallback(wxCommandEvent& event)
115 {
116 if (event.GetEventType() == wxEVT_COMMAND_LISTBOX_SELECTED)
117 {
118 // if (event.IsSelection())
119 m_textField->SetValue(event.GetString());
120 }
121 }
122
123 void PlayerSelectionDialog::ButtonCallback(wxCommandEvent& event)
124 {
125 if (event.GetId() == wxID_OK)
126 {
127 wxString name = m_textField->GetValue();
128 if (!name.IsNull() && name.Length() > 0)
129 {
130 if (name.Contains(_T('@')))
131 {
132 wxMessageBox(_T("Names should not contain the '@' character"), _T("Forty Thieves"));
133 }
134 else
135 {
136 m_player = name;
137 EndModal(wxID_OK);
138 }
139 }
140 else
141 {
142 wxMessageBox(_T("Please enter your name"), _T("Forty Thieves"));
143 }
144 }
145 else
146 {
147 m_player = _T("");
148 EndModal(wxID_CANCEL);
149 }
150 }