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