playerdg.cpp 3.7 KB

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