add an example of a custom wxValidator-derived class; cleanup a bit the dialog layout
[wxWidgets.git] / samples / validate / validate.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: validate.cpp
3 // Purpose: wxWidgets validator sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // See online help for an overview of validators. In general, a
13 // validator transfers data between a control and a variable.
14 // It may also test for validity of a string transferred to or
15 // from a text control. All validators transfer data, but not
16 // all test validity, so don't be confused by the name.
17
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif // __BORLANDC__
24
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif // WX_PRECOMP
28
29 #include "validate.h"
30
31 #include "wx/sizer.h"
32 #include "wx/valgen.h"
33 #include "wx/valtext.h"
34
35 #ifndef __WXMSW__
36 #include "../sample.xpm"
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // Global data
41 // ----------------------------------------------------------------------------
42
43 MyData g_data;
44
45 wxString g_listbox_choices[] =
46 {wxT("one"), wxT("two"), wxT("three")};
47
48 wxString g_combobox_choices[] =
49 {wxT("yes"), wxT("no (doesn't validate)"), wxT("maybe (doesn't validate)")};
50
51 wxString g_radiobox_choices[] =
52 {wxT("green"), wxT("yellow"), wxT("red")};
53
54 // ----------------------------------------------------------------------------
55 // MyData
56 // ----------------------------------------------------------------------------
57
58 MyData::MyData()
59 {
60 // This string will be passed to an alpha-only validator, which
61 // will complain because spaces aren't alpha. Note that validation
62 // is performed only when 'OK' is pressed.
63 m_string = wxT("Spaces are invalid here");
64 m_listbox_choices.Add(0);
65 }
66
67
68 // ----------------------------------------------------------------------------
69 // MyComboBoxValidator
70 // ----------------------------------------------------------------------------
71
72 bool MyComboBoxValidator::Validate(wxWindow *WXUNUSED(parent))
73 {
74 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
75
76 wxComboBox* cb = (wxComboBox*)GetWindow();
77 if (cb->GetValue() == g_combobox_choices[1] ||
78 cb->GetValue() == g_combobox_choices[2])
79 {
80 // we accept any string != g_combobox_choices[1|2] !
81
82 wxLogError("Invalid combo box text!");
83 return false;
84 }
85
86 if (m_var)
87 *m_var = cb->GetValue();
88
89 return true;
90 }
91
92 bool MyComboBoxValidator::TransferToWindow()
93 {
94 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
95
96 if ( m_var )
97 {
98 wxComboBox* cb = (wxComboBox*)GetWindow();
99 if ( !cb )
100 return false;
101
102 cb->SetValue(*m_var);
103 }
104
105 return true;
106 }
107
108 bool MyComboBoxValidator::TransferFromWindow()
109 {
110 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
111
112 if ( m_var )
113 {
114 wxComboBox* cb = (wxComboBox*)GetWindow();
115 if ( !cb )
116 return false;
117
118 *m_var = cb->GetValue();
119 }
120
121 return true;
122 }
123
124 // ----------------------------------------------------------------------------
125 // MyApp
126 // ----------------------------------------------------------------------------
127
128 IMPLEMENT_APP(MyApp)
129
130 bool MyApp::OnInit()
131 {
132 if ( !wxApp::OnInit() )
133 return false;
134
135 // Create and display the main frame window.
136 MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("Validator Test"),
137 50, 50, 300, 250);
138 frame->Show(true);
139 SetTopWindow(frame);
140 return true;
141 }
142
143 // ----------------------------------------------------------------------------
144 // MyFrame
145 // ----------------------------------------------------------------------------
146
147 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
148 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
149 EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog)
150 EVT_MENU(VALIDATE_TOGGLE_BELL, MyFrame::OnToggleBell)
151 END_EVENT_TABLE()
152
153 MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int h)
154 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
155 m_silent(true)
156 {
157 SetIcon(wxICON(sample));
158
159 // Create a listbox to display the validated data.
160 m_listbox = new wxListBox(this, wxID_ANY);
161 m_listbox->Append(wxString(_T("Try 'File|Test' to see how validators work.")));
162
163 wxMenu *file_menu = new wxMenu;
164
165 file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test"), wxT("Demonstrate validators"));
166 file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error"));
167 file_menu->AppendSeparator();
168 file_menu->Append(wxID_EXIT, wxT("E&xit"));
169
170 wxMenuBar *menu_bar = new wxMenuBar;
171 menu_bar->Append(file_menu, wxT("&File"));
172 SetMenuBar(menu_bar);
173
174 // All validators share a common (static) flag that controls
175 // whether they beep on error. Here we turn it off:
176 wxValidator::SetBellOnError(m_silent);
177 file_menu->Check(VALIDATE_TOGGLE_BELL, !wxValidator::IsSilent());
178
179 #if wxUSE_STATUSBAR
180 CreateStatusBar(1);
181 #endif // wxUSE_STATUSBAR
182 }
183
184 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
185 {
186 Close(true);
187 }
188
189 void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
190 {
191 // The validators defined in the dialog implementation bind controls
192 // and variables together. Values are transferred between them behind
193 // the scenes, so here we don't have to query the controls for their
194 // values.
195 MyDialog dialog(this, wxT("Validator demonstration"));
196
197 // When the dialog is displayed, validators automatically transfer
198 // data from variables to their corresponding controls.
199 if ( dialog.ShowModal() == wxID_OK )
200 {
201 // 'OK' was pressed, so controls that have validators are
202 // automatically transferred to the variables we specified
203 // when we created the validators.
204 m_listbox->Clear();
205 m_listbox->Append(wxString(_T("string: ")) + g_data.m_string);
206 for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i)
207 {
208 int j = g_data.m_listbox_choices[i];
209 m_listbox->Append(wxString(_T("listbox choice(s): ")) + g_listbox_choices[j]);
210 }
211
212 wxString checkbox_state(g_data.m_checkbox_state ? _T("checked") : _T("unchecked"));
213 m_listbox->Append(wxString(_T("checkbox: ")) + checkbox_state);
214 m_listbox->Append(wxString(_T("combobox: ")) + g_data.m_combobox_choice);
215 m_listbox->Append(wxString(_T("radiobox: ")) + g_radiobox_choices[g_data.m_radiobox_choice]);
216 }
217 }
218
219 void MyFrame::OnToggleBell(wxCommandEvent& event)
220 {
221 m_silent = !m_silent;
222 wxValidator::SetBellOnError(m_silent);
223 event.Skip();
224 }
225
226 // ----------------------------------------------------------------------------
227 // MyDialog
228 // ----------------------------------------------------------------------------
229
230 MyDialog::MyDialog( wxWindow *parent, const wxString& title,
231 const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
232 wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
233 {
234 // setup the flex grid sizer
235 // -------------------------
236
237 wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(2, 2, 5, 5);
238
239 // Create and add controls to sizers. Note that a member variable
240 // of g_data is bound to each control upon construction. There is
241 // currently no easy way to substitute a different validator or a
242 // different transfer variable after a control has been constructed.
243
244 // Pointers to some of these controls are saved in member variables
245 // so that we can use them elsewhere, like this one.
246 m_text = new wxTextCtrl(this, VALIDATE_TEXT, wxEmptyString,
247 wxDefaultPosition, wxDefaultSize, 0,
248 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
249 flexgridsizer->Add(m_text, 1, wxGROW);
250
251 // This wxCheckBox* doesn't need to be assigned to any pointer
252 // because we don't use it elsewhere--it can be anonymous.
253 // We don't need any such pointer to query its state, which
254 // can be gotten directly from g_data.
255 flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"),
256 wxPoint(130, 10), wxSize(120, wxDefaultCoord), 0,
257 wxGenericValidator(&g_data.m_checkbox_state)),
258 1, wxALIGN_CENTER);
259
260 flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST,
261 wxPoint(10, 30), wxSize(120, wxDefaultCoord),
262 3, g_listbox_choices, wxLB_MULTIPLE,
263 wxGenericValidator(&g_data.m_listbox_choices)),
264 1, wxGROW);
265
266 m_combobox = new wxComboBox(this, VALIDATE_COMBO, wxEmptyString,
267 wxPoint(130, 30), wxSize(120, wxDefaultCoord),
268 3, g_combobox_choices, 0L,
269 MyComboBoxValidator(&g_data.m_combobox_choice));
270 flexgridsizer->Add(m_combobox, 1, wxALIGN_CENTER);
271
272 flexgridsizer->AddGrowableCol(0);
273 flexgridsizer->AddGrowableCol(1);
274 flexgridsizer->AddGrowableRow(1);
275
276
277 // setup the button sizer
278 // ----------------------
279
280 wxStdDialogButtonSizer *btn = new wxStdDialogButtonSizer();
281 btn->AddButton(new wxButton(this, wxID_OK));
282 btn->AddButton(new wxButton(this, wxID_CANCEL));
283 btn->Realize();
284
285
286 // setup the main sizer
287 // --------------------
288
289 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
290
291 mainsizer->Add(flexgridsizer, 1, wxGROW | wxALL, 10);
292
293 mainsizer->Add(new wxRadioBox((wxWindow*)this, VALIDATE_RADIO, wxT("Pick a color"),
294 wxPoint(10, 100), wxDefaultSize,
295 3, g_radiobox_choices, 1, wxRA_SPECIFY_ROWS,
296 wxGenericValidator(&g_data.m_radiobox_choice)),
297 0, wxGROW | wxLEFT|wxBOTTOM|wxRIGHT, 10);
298
299 mainsizer->Add(btn, 0, wxGROW | wxALL, 10);
300
301 SetSizer(mainsizer);
302 mainsizer->SetSizeHints(this);
303
304 // make the dialog a bit bigger than its minimal size:
305 SetSize(GetBestSize()*1.5);
306 }
307
308 bool MyDialog::TransferDataToWindow()
309 {
310 bool r = wxDialog::TransferDataToWindow();
311
312 // These function calls have to be made here, after the
313 // dialog has been created.
314 m_text->SetFocus();
315 m_combobox->SetSelection(0);
316
317 return r;
318 }
319