Add wxDocManager::Get{Views,Documents,Templates}Vector().
[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 licence
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 #include "wx/valnum.h"
35
36 #ifndef wxHAS_IMAGES_IN_RESOURCES
37 #include "../sample.xpm"
38 #endif
39
40 // ----------------------------------------------------------------------------
41 // Global data
42 // ----------------------------------------------------------------------------
43
44 MyData g_data;
45
46 wxString g_listbox_choices[] =
47 {wxT("one"), wxT("two"), wxT("three")};
48
49 wxString g_combobox_choices[] =
50 {wxT("yes"), wxT("no (doesn't validate)"), wxT("maybe (doesn't validate)")};
51
52 wxString g_radiobox_choices[] =
53 {wxT("green"), wxT("yellow"), wxT("red")};
54
55 // ----------------------------------------------------------------------------
56 // MyData
57 // ----------------------------------------------------------------------------
58
59 MyData::MyData()
60 {
61 // This string will be passed to an alpha-only validator, which
62 // will complain because spaces aren't alpha. Note that validation
63 // is performed only when 'OK' is pressed.
64 m_string = wxT("Spaces are invalid here");
65 m_string2 = "Valid text";
66 m_listbox_choices.Add(0);
67 m_intValue = 0;
68 m_doubleValue = 12354.31;
69 }
70
71 // ----------------------------------------------------------------------------
72 // MyComboBoxValidator
73 // ----------------------------------------------------------------------------
74
75 bool MyComboBoxValidator::Validate(wxWindow *WXUNUSED(parent))
76 {
77 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
78
79 wxComboBox* cb = (wxComboBox*)GetWindow();
80 if (cb->GetValue() == g_combobox_choices[1] ||
81 cb->GetValue() == g_combobox_choices[2])
82 {
83 // we accept any string != g_combobox_choices[1|2] !
84
85 wxLogError("Invalid combo box text!");
86 return false;
87 }
88
89 if (m_var)
90 *m_var = cb->GetValue();
91
92 return true;
93 }
94
95 bool MyComboBoxValidator::TransferToWindow()
96 {
97 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
98
99 if ( m_var )
100 {
101 wxComboBox* cb = (wxComboBox*)GetWindow();
102 if ( !cb )
103 return false;
104
105 cb->SetValue(*m_var);
106 }
107
108 return true;
109 }
110
111 bool MyComboBoxValidator::TransferFromWindow()
112 {
113 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
114
115 if ( m_var )
116 {
117 wxComboBox* cb = (wxComboBox*)GetWindow();
118 if ( !cb )
119 return false;
120
121 *m_var = cb->GetValue();
122 }
123
124 return true;
125 }
126
127 // ----------------------------------------------------------------------------
128 // MyApp
129 // ----------------------------------------------------------------------------
130
131 IMPLEMENT_APP(MyApp)
132
133 bool MyApp::OnInit()
134 {
135 if ( !wxApp::OnInit() )
136 return false;
137
138 // Create and display the main frame window.
139 MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("Validator Test"),
140 50, 50, 300, 250);
141 frame->Show(true);
142
143 return true;
144 }
145
146 // ----------------------------------------------------------------------------
147 // MyFrame
148 // ----------------------------------------------------------------------------
149
150 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
151 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
152 EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog)
153 EVT_MENU(VALIDATE_TOGGLE_BELL, MyFrame::OnToggleBell)
154 END_EVENT_TABLE()
155
156 MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int h)
157 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
158 m_silent(true)
159 {
160 SetIcon(wxICON(sample));
161
162 // Create a listbox to display the validated data.
163 m_listbox = new wxListBox(this, wxID_ANY);
164 m_listbox->Append(wxString(wxT("Try 'File|Test' to see how validators work.")));
165
166 wxMenu *file_menu = new wxMenu;
167
168 file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test dialog..."), wxT("Demonstrate validators"));
169 file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error"));
170 file_menu->AppendSeparator();
171 file_menu->Append(wxID_EXIT, wxT("E&xit"));
172
173 wxMenuBar *menu_bar = new wxMenuBar;
174 menu_bar->Append(file_menu, wxT("&File"));
175 SetMenuBar(menu_bar);
176
177 // All validators share a common (static) flag that controls
178 // whether they beep on error. Here we turn it off:
179 wxValidator::SuppressBellOnError(m_silent);
180 file_menu->Check(VALIDATE_TOGGLE_BELL, !wxValidator::IsSilent());
181
182 #if wxUSE_STATUSBAR
183 CreateStatusBar(1);
184 #endif // wxUSE_STATUSBAR
185 }
186
187 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
188 {
189 Close(true);
190 }
191
192 void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
193 {
194 // The validators defined in the dialog implementation bind controls
195 // and variables together. Values are transferred between them behind
196 // the scenes, so here we don't have to query the controls for their
197 // values.
198 MyDialog dialog(this, wxT("Validator demonstration"));
199
200 // When the dialog is displayed, validators automatically transfer
201 // data from variables to their corresponding controls.
202 if ( dialog.ShowModal() == wxID_OK )
203 {
204 // 'OK' was pressed, so controls that have validators are
205 // automatically transferred to the variables we specified
206 // when we created the validators.
207 m_listbox->Clear();
208 m_listbox->Append(wxString(wxT("string: ")) + g_data.m_string);
209 m_listbox->Append(wxString(wxT("string #2: ")) + g_data.m_string2);
210
211 for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i)
212 {
213 int j = g_data.m_listbox_choices[i];
214 m_listbox->Append(wxString(wxT("listbox choice(s): ")) + g_listbox_choices[j]);
215 }
216
217 wxString checkbox_state(g_data.m_checkbox_state ? wxT("checked") : wxT("unchecked"));
218 m_listbox->Append(wxString(wxT("checkbox: ")) + checkbox_state);
219 m_listbox->Append(wxString(wxT("combobox: ")) + g_data.m_combobox_choice);
220 m_listbox->Append(wxString(wxT("radiobox: ")) + g_radiobox_choices[g_data.m_radiobox_choice]);
221
222 m_listbox->Append(wxString::Format("integer value: %d", g_data.m_intValue));
223 m_listbox->Append(wxString::Format("double value: %.3f", g_data.m_doubleValue));
224 }
225 }
226
227 void MyFrame::OnToggleBell(wxCommandEvent& event)
228 {
229 m_silent = !m_silent;
230 wxValidator::SuppressBellOnError(m_silent);
231 event.Skip();
232 }
233
234 // ----------------------------------------------------------------------------
235 // MyDialog
236 // ----------------------------------------------------------------------------
237
238 MyDialog::MyDialog( wxWindow *parent, const wxString& title,
239 const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
240 wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
241 {
242 // setup the flex grid sizer
243 // -------------------------
244
245 wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(3, 2, 5, 5);
246
247 // Create and add controls to sizers. Note that a member variable
248 // of g_data is bound to each control upon construction. There is
249 // currently no easy way to substitute a different validator or a
250 // different transfer variable after a control has been constructed.
251
252 // Pointers to some of these controls are saved in member variables
253 // so that we can use them elsewhere, like this one.
254 m_text = new wxTextCtrl(this, VALIDATE_TEXT, wxEmptyString,
255 wxDefaultPosition, wxDefaultSize, 0,
256 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
257 m_text->SetToolTip("uses wxTextValidator with wxFILTER_ALPHA");
258 flexgridsizer->Add(m_text, 1, wxGROW);
259
260
261 // Now set a wxTextValidator with an explicit list of characters NOT allowed:
262 wxTextValidator textVal(wxFILTER_EMPTY|wxFILTER_EXCLUDE_LIST, &g_data.m_string2);
263 textVal.SetCharExcludes("bcwyz");
264 wxTextCtrl* txt2 =
265 new wxTextCtrl(this, VALIDATE_TEXT2, wxEmptyString,
266 wxDefaultPosition, wxDefaultSize, 0, textVal);
267 txt2->SetToolTip("uses wxTextValidator with wxFILTER_EMPTY|wxFILTER_EXCLUDE_LIST (to exclude 'bcwyz')");
268 flexgridsizer->Add(txt2, 1, wxGROW);
269
270 flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST,
271 wxDefaultPosition, wxDefaultSize,
272 3, g_listbox_choices, wxLB_MULTIPLE,
273 wxGenericValidator(&g_data.m_listbox_choices)),
274 1, wxGROW);
275
276 m_combobox = new wxComboBox(this, VALIDATE_COMBO, wxEmptyString,
277 wxDefaultPosition, wxDefaultSize,
278 3, g_combobox_choices, 0L,
279 MyComboBoxValidator(&g_data.m_combobox_choice));
280 m_combobox->SetToolTip("uses a custom validator (MyComboBoxValidator)");
281 flexgridsizer->Add(m_combobox, 1, wxALIGN_CENTER);
282
283 // This wxCheckBox* doesn't need to be assigned to any pointer
284 // because we don't use it elsewhere--it can be anonymous.
285 // We don't need any such pointer to query its state, which
286 // can be gotten directly from g_data.
287 flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"),
288 wxDefaultPosition, wxDefaultSize, 0,
289 wxGenericValidator(&g_data.m_checkbox_state)),
290 1, wxALIGN_CENTER|wxALL, 15);
291
292 flexgridsizer->AddGrowableCol(0);
293 flexgridsizer->AddGrowableCol(1);
294 flexgridsizer->AddGrowableRow(1);
295
296
297 // setup the button sizer
298 // ----------------------
299
300 wxStdDialogButtonSizer *btn = new wxStdDialogButtonSizer();
301 btn->AddButton(new wxButton(this, wxID_OK));
302 btn->AddButton(new wxButton(this, wxID_CANCEL));
303 btn->Realize();
304
305 // setup a sizer with the controls for numeric validators
306 // ------------------------------------------------------
307
308 wxIntegerValidator<int> valInt(&g_data.m_intValue,
309 wxNUM_VAL_THOUSANDS_SEPARATOR |
310 wxNUM_VAL_ZERO_AS_BLANK);
311 valInt.SetMin(0); // Only allow positive numbers
312
313 m_numericTextInt = new wxTextCtrl
314 (
315 this,
316 wxID_ANY,
317 "",
318 wxDefaultPosition,
319 wxDefaultSize,
320 wxTE_RIGHT,
321 valInt
322 );
323 m_numericTextInt->SetToolTip("uses wxIntegerValidator to accept positive "
324 "integers only");
325
326 m_numericTextDouble = new wxTextCtrl
327 (
328 this,
329 wxID_ANY,
330 "",
331 wxDefaultPosition,
332 wxDefaultSize,
333 wxTE_RIGHT,
334 wxMakeFloatingPointValidator
335 (
336 3,
337 &g_data.m_doubleValue,
338 wxNUM_VAL_THOUSANDS_SEPARATOR |
339 wxNUM_VAL_NO_TRAILING_ZEROES
340 )
341 );
342 m_numericTextDouble->SetToolTip("uses wxFloatingPointValidator with 3 decimals");
343 wxBoxSizer *numSizer = new wxBoxSizer( wxHORIZONTAL );
344 numSizer->Add( m_numericTextInt, 1, wxALL, 10 );
345 numSizer->Add( m_numericTextDouble, 1, wxALL, 10 );
346
347
348
349 // setup the main sizer
350 // --------------------
351
352 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
353
354 mainsizer->Add(flexgridsizer, 1, wxGROW | wxALL, 10);
355
356 mainsizer->Add(new wxRadioBox((wxWindow*)this, VALIDATE_RADIO, wxT("Pick a color"),
357 wxDefaultPosition, wxDefaultSize,
358 3, g_radiobox_choices, 1, wxRA_SPECIFY_ROWS,
359 wxGenericValidator(&g_data.m_radiobox_choice)),
360 0, wxGROW | wxLEFT|wxBOTTOM|wxRIGHT, 10);
361
362 mainsizer->Add( numSizer, 0, wxGROW | wxALL );
363
364 mainsizer->Add(btn, 0, wxGROW | wxALL, 10);
365
366 SetSizer(mainsizer);
367 mainsizer->SetSizeHints(this);
368
369 // make the dialog a bit bigger than its minimal size:
370 SetSize(GetBestSize()*1.5);
371 }
372
373 bool MyDialog::TransferDataToWindow()
374 {
375 bool r = wxDialog::TransferDataToWindow();
376
377 // These function calls have to be made here, after the
378 // dialog has been created.
379 m_text->SetFocus();
380 m_combobox->SetSelection(0);
381
382 return r;
383 }
384