textdlgg.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/generic/textdlgg.cpp
  3. // Purpose: wxTextEntryDialog
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 04/01/98
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #if wxUSE_TEXTDLG
  22. #include "wx/generic/textdlgg.h"
  23. #ifndef WX_PRECOMP
  24. #include "wx/utils.h"
  25. #include "wx/dialog.h"
  26. #include "wx/button.h"
  27. #include "wx/stattext.h"
  28. #include "wx/textctrl.h"
  29. #include "wx/intl.h"
  30. #include "wx/sizer.h"
  31. #endif
  32. #if wxUSE_STATLINE
  33. #include "wx/statline.h"
  34. #endif
  35. const char wxGetTextFromUserPromptStr[] = "Input Text";
  36. const char wxGetPasswordFromUserPromptStr[] = "Enter Password";
  37. // ----------------------------------------------------------------------------
  38. // constants
  39. // ----------------------------------------------------------------------------
  40. static const int wxID_TEXT = 3000;
  41. // ============================================================================
  42. // implementation
  43. // ============================================================================
  44. // ----------------------------------------------------------------------------
  45. // wxTextEntryDialog
  46. // ----------------------------------------------------------------------------
  47. BEGIN_EVENT_TABLE(wxTextEntryDialog, wxDialog)
  48. EVT_BUTTON(wxID_OK, wxTextEntryDialog::OnOK)
  49. END_EVENT_TABLE()
  50. IMPLEMENT_CLASS(wxTextEntryDialog, wxDialog)
  51. bool wxTextEntryDialog::Create(wxWindow *parent,
  52. const wxString& message,
  53. const wxString& caption,
  54. const wxString& value,
  55. long style,
  56. const wxPoint& pos)
  57. {
  58. if ( !wxDialog::Create(GetParentForModalDialog(parent, style),
  59. wxID_ANY, caption,
  60. pos, wxDefaultSize,
  61. wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) )
  62. {
  63. return false;
  64. }
  65. m_dialogStyle = style;
  66. m_value = value;
  67. wxBeginBusyCursor();
  68. wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
  69. wxSizerFlags flagsBorder2;
  70. flagsBorder2.DoubleBorder();
  71. #if wxUSE_STATTEXT
  72. // 1) text message
  73. topsizer->Add(CreateTextSizer(message), flagsBorder2);
  74. #endif
  75. // 2) text ctrl
  76. m_textctrl = new wxTextCtrl(this, wxID_TEXT, value,
  77. wxDefaultPosition, wxSize(300, wxDefaultCoord),
  78. style & ~wxTextEntryDialogStyle);
  79. topsizer->Add(m_textctrl,
  80. wxSizerFlags(style & wxTE_MULTILINE ? 1 : 0).
  81. Expand().
  82. TripleBorder(wxLEFT | wxRIGHT));
  83. // 3) buttons if any
  84. wxSizer *buttonSizer = CreateSeparatedButtonSizer(style & (wxOK | wxCANCEL));
  85. if ( buttonSizer )
  86. {
  87. topsizer->Add(buttonSizer, wxSizerFlags(flagsBorder2).Expand());
  88. }
  89. SetAutoLayout( true );
  90. SetSizer( topsizer );
  91. topsizer->SetSizeHints( this );
  92. topsizer->Fit( this );
  93. if ( style & wxCENTRE )
  94. Centre( wxBOTH );
  95. m_textctrl->SelectAll();
  96. m_textctrl->SetFocus();
  97. wxEndBusyCursor();
  98. return true;
  99. }
  100. bool wxTextEntryDialog::TransferDataToWindow()
  101. {
  102. m_textctrl->SetValue(m_value);
  103. return wxDialog::TransferDataToWindow();
  104. }
  105. bool wxTextEntryDialog::TransferDataFromWindow()
  106. {
  107. m_value = m_textctrl->GetValue();
  108. return wxDialog::TransferDataFromWindow();
  109. }
  110. void wxTextEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
  111. {
  112. if ( Validate() && TransferDataFromWindow() )
  113. {
  114. EndModal( wxID_OK );
  115. }
  116. }
  117. void wxTextEntryDialog::SetMaxLength(unsigned long len)
  118. {
  119. m_textctrl->SetMaxLength(len);
  120. }
  121. void wxTextEntryDialog::SetValue(const wxString& val)
  122. {
  123. m_value = val;
  124. m_textctrl->SetValue(val);
  125. }
  126. #if wxUSE_VALIDATORS
  127. #if WXWIN_COMPATIBILITY_2_8
  128. void wxTextEntryDialog::SetTextValidator( long style )
  129. {
  130. SetTextValidator((wxTextValidatorStyle)style);
  131. }
  132. #endif
  133. void wxTextEntryDialog::SetTextValidator( wxTextValidatorStyle style )
  134. {
  135. SetTextValidator(wxTextValidator(style));
  136. }
  137. void wxTextEntryDialog::SetTextValidator( const wxTextValidator& validator )
  138. {
  139. m_textctrl->SetValidator( validator );
  140. }
  141. #endif // wxUSE_VALIDATORS
  142. // ----------------------------------------------------------------------------
  143. // wxPasswordEntryDialog
  144. // ----------------------------------------------------------------------------
  145. IMPLEMENT_CLASS(wxPasswordEntryDialog, wxTextEntryDialog)
  146. wxPasswordEntryDialog::wxPasswordEntryDialog(wxWindow *parent,
  147. const wxString& message,
  148. const wxString& caption,
  149. const wxString& value,
  150. long style,
  151. const wxPoint& pos)
  152. : wxTextEntryDialog(parent, message, caption, value,
  153. style | wxTE_PASSWORD, pos)
  154. {
  155. // Only change from wxTextEntryDialog is the password style
  156. }
  157. #endif // wxUSE_TEXTDLG