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: playerdg.cpp 50452 2007-12-03 09:45:13Z JS $
8// Copyright:   (c) 1993-1998 Chris Breeze
9// Licence:     wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
23#include "scorefil.h"
24#include "playerdg.h"
25
26const int ID_LISTBOX = 101;
27
28BEGIN_EVENT_TABLE(PlayerSelectionDialog, wxDialog)
29    EVT_SIZE(PlayerSelectionDialog::OnSize)
30    EVT_BUTTON(wxID_OK, PlayerSelectionDialog::ButtonCallback)
31    EVT_BUTTON(wxID_CANCEL, PlayerSelectionDialog::ButtonCallback)
32    EVT_LISTBOX(ID_LISTBOX, PlayerSelectionDialog::SelectCallback)
33    EVT_CLOSE(PlayerSelectionDialog::OnCloseWindow)
34END_EVENT_TABLE()
35
36PlayerSelectionDialog::PlayerSelectionDialog(
37                            wxWindow* parent,
38                            ScoreFile* file
39                            ) :
40    wxDialog(parent, wxID_ANY, _T("Player Selection"), wxDefaultPosition),
41    m_scoreFile(file)
42{
43    wxStaticText* msg = new wxStaticText(this, wxID_ANY, _T("Please select a name or type a new one:"));
44
45    wxListBox* list = new wxListBox(
46                        this, ID_LISTBOX,
47                        wxDefaultPosition, wxSize(-1, 150),
48                        0, 0,
49                        wxLB_SINGLE
50                        );
51
52    wxArrayString players;
53    m_scoreFile->GetPlayerList(players);
54    for (unsigned int i = 0; i < players.Count(); i++)
55    {
56        list->Append(players[i]);
57    }
58
59    m_textField = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize);
60
61    m_OK = new wxButton(this, wxID_OK);
62    m_cancel = new wxButton(this, wxID_CANCEL);
63
64    wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
65    button_sizer->Add( m_OK, 0, wxALL, 10 );
66    button_sizer->Add( m_cancel, 0, wxALL, 10 );
67
68    wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
69    topsizer->Add( msg, 0, wxALL , 10 );
70    topsizer->Add( list, 1, wxEXPAND | wxLEFT | wxRIGHT, 10 );
71    topsizer->Add( m_textField, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10 );
72    topsizer->Add( button_sizer, 0, wxALIGN_LEFT );
73
74    SetSizer( topsizer );
75
76    topsizer->SetSizeHints( this );
77
78    CentreOnParent();
79
80    m_OK->SetDefault();
81}
82
83void PlayerSelectionDialog::OnSize(wxSizeEvent& WXUNUSED(event))
84{
85    Layout();
86}
87
88const wxString& PlayerSelectionDialog::GetPlayersName()
89{
90/*
91    m_player = wxEmptyString;
92    Show(true);
93*/
94    return m_player;
95}
96
97void PlayerSelectionDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
98{
99    m_player = wxEmptyString;
100    EndModal(wxID_CANCEL);
101}
102
103void PlayerSelectionDialog::SelectCallback(wxCommandEvent& event)
104{
105    if (event.GetEventType() == wxEVT_COMMAND_LISTBOX_SELECTED)
106    {
107//        if (event.IsSelection())
108        m_textField->SetValue(event.GetString());
109    }
110}
111
112void PlayerSelectionDialog::ButtonCallback(wxCommandEvent& event)
113{
114    if (event.GetId() == wxID_OK)
115    {
116        wxString name = m_textField->GetValue();
117        if (!name.IsNull() && name.Length() > 0)
118        {
119            if (name.Contains(_T('@')))
120            {
121                wxMessageBox(_T("Names should not contain the '@' character"), _T("Forty Thieves"));
122            }
123            else
124            {
125                m_player = name;
126                EndModal(wxID_OK);
127            }
128        }
129        else
130        {
131             wxMessageBox(_T("Please enter your name"), _T("Forty Thieves"));
132        }
133    }
134    else
135    {
136        m_player = wxEmptyString;
137        EndModal(wxID_CANCEL);
138    }
139}
140