]> git.saurik.com Git - wxWidgets.git/blame - samples/validate/validate.cpp
Fix comparisons of char and wchar_t with wxUniChar[Ref].
[wxWidgets.git] / samples / validate / validate.cpp
CommitLineData
457814b5
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: validate.cpp
be5a51fb 3// Purpose: wxWidgets validator sample
457814b5
JS
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
526954c5 9// Licence: wxWindows licence
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
2b61c41b
VZ
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
457814b5
JS
18// For compilers that support precompilation, includes "wx/wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
0bcd4039 22 #pragma hdrstop
2b61c41b 23#endif // __BORLANDC__
457814b5
JS
24
25#ifndef WX_PRECOMP
0bcd4039 26 #include "wx/wx.h"
2b61c41b 27#endif // WX_PRECOMP
457814b5
JS
28
29#include "validate.h"
30
2b61c41b
VZ
31#include "wx/sizer.h"
32#include "wx/valgen.h"
33#include "wx/valtext.h"
a54cf371 34#include "wx/valnum.h"
457814b5 35
e7092398 36#ifndef wxHAS_IMAGES_IN_RESOURCES
0bcd4039
WS
37 #include "../sample.xpm"
38#endif
39
2b61c41b
VZ
40// ----------------------------------------------------------------------------
41// Global data
42// ----------------------------------------------------------------------------
457814b5 43
a994f81b 44MyData g_data;
457814b5 45
2b61c41b 46wxString g_listbox_choices[] =
7df07b10 47 {wxT("one"), wxT("two"), wxT("three")};
2b61c41b
VZ
48
49wxString g_combobox_choices[] =
2e7a6b03 50 {wxT("yes"), wxT("no (doesn't validate)"), wxT("maybe (doesn't validate)")};
2b61c41b
VZ
51
52wxString g_radiobox_choices[] =
7df07b10 53 {wxT("green"), wxT("yellow"), wxT("red")};
2b61c41b
VZ
54
55// ----------------------------------------------------------------------------
56// MyData
57// ----------------------------------------------------------------------------
58
59MyData::MyData()
457814b5 60{
2b61c41b
VZ
61 // This string will be passed to an alpha-only validator, which
62 // will complain because spaces aren't alpha. Note that validation
2e7a6b03 63 // is performed only when 'OK' is pressed.
7df07b10 64 m_string = wxT("Spaces are invalid here");
fcd209b6 65 m_string2 = "Valid text";
2b61c41b 66 m_listbox_choices.Add(0);
a54cf371
VZ
67 m_intValue = 0;
68 m_doubleValue = 12354.31;
2b61c41b 69}
457814b5 70
2e7a6b03
FM
71// ----------------------------------------------------------------------------
72// MyComboBoxValidator
73// ----------------------------------------------------------------------------
74
75bool 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
95bool 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
111bool 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
2b61c41b
VZ
127// ----------------------------------------------------------------------------
128// MyApp
129// ----------------------------------------------------------------------------
a994f81b 130
2b61c41b 131IMPLEMENT_APP(MyApp)
a994f81b 132
2b61c41b
VZ
133bool MyApp::OnInit()
134{
45e6e6f8
VZ
135 if ( !wxApp::OnInit() )
136 return false;
137
2b61c41b 138 // Create and display the main frame window.
7df07b10
MB
139 MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("Validator Test"),
140 50, 50, 300, 250);
2b61c41b 141 frame->Show(true);
18f42b94 142
2b61c41b 143 return true;
a994f81b
VZ
144}
145
2b61c41b
VZ
146// ----------------------------------------------------------------------------
147// MyFrame
148// ----------------------------------------------------------------------------
149
150BEGIN_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)
154END_EVENT_TABLE()
155
7df07b10 156MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int h)
0bcd4039 157 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
2b61c41b 158 m_silent(true)
a994f81b 159{
0bcd4039 160 SetIcon(wxICON(sample));
2b61c41b
VZ
161
162 // Create a listbox to display the validated data.
0bcd4039 163 m_listbox = new wxListBox(this, wxID_ANY);
9a83f860 164 m_listbox->Append(wxString(wxT("Try 'File|Test' to see how validators work.")));
457814b5 165
2b61c41b 166 wxMenu *file_menu = new wxMenu;
457814b5 167
fcd209b6 168 file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test dialog..."), wxT("Demonstrate validators"));
2153bf89 169 file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error"));
2b61c41b 170 file_menu->AppendSeparator();
7df07b10 171 file_menu->Append(wxID_EXIT, wxT("E&xit"));
457814b5 172
2b61c41b 173 wxMenuBar *menu_bar = new wxMenuBar;
2e7a6b03 174 menu_bar->Append(file_menu, wxT("&File"));
2b61c41b 175 SetMenuBar(menu_bar);
457814b5 176
2b61c41b
VZ
177 // All validators share a common (static) flag that controls
178 // whether they beep on error. Here we turn it off:
c27181d1 179 wxValidator::SuppressBellOnError(m_silent);
2b61c41b 180 file_menu->Check(VALIDATE_TOGGLE_BELL, !wxValidator::IsSilent());
457814b5 181
8520f137 182#if wxUSE_STATUSBAR
2b61c41b 183 CreateStatusBar(1);
8520f137 184#endif // wxUSE_STATUSBAR
457814b5
JS
185}
186
cb43b372 187void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 188{
2b61c41b 189 Close(true);
457814b5
JS
190}
191
cb43b372 192void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
457814b5 193{
2b61c41b
VZ
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.
7df07b10 198 MyDialog dialog(this, wxT("Validator demonstration"));
2b61c41b
VZ
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();
9a83f860
VZ
208 m_listbox->Append(wxString(wxT("string: ")) + g_data.m_string);
209 m_listbox->Append(wxString(wxT("string #2: ")) + g_data.m_string2);
fcd209b6 210
2b61c41b
VZ
211 for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i)
212 {
213 int j = g_data.m_listbox_choices[i];
9a83f860 214 m_listbox->Append(wxString(wxT("listbox choice(s): ")) + g_listbox_choices[j]);
2b61c41b
VZ
215 }
216
9a83f860
VZ
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]);
a54cf371
VZ
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));
2b61c41b 224 }
a994f81b
VZ
225}
226
2b61c41b 227void MyFrame::OnToggleBell(wxCommandEvent& event)
a994f81b 228{
2b61c41b 229 m_silent = !m_silent;
c27181d1 230 wxValidator::SuppressBellOnError(m_silent);
a994f81b 231 event.Skip();
457814b5
JS
232}
233
2b61c41b
VZ
234// ----------------------------------------------------------------------------
235// MyDialog
236// ----------------------------------------------------------------------------
237
a994f81b 238MyDialog::MyDialog( wxWindow *parent, const wxString& title,
cb43b372 239 const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
2a21ac15 240 wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
457814b5 241{
e01ea38b
FM
242 // setup the flex grid sizer
243 // -------------------------
244
fcd209b6 245 wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(3, 2, 5, 5);
2b61c41b
VZ
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.
2e7a6b03
FM
254 m_text = new wxTextCtrl(this, VALIDATE_TEXT, wxEmptyString,
255 wxDefaultPosition, wxDefaultSize, 0,
256 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
58fa61db 257 m_text->SetToolTip("uses wxTextValidator with wxFILTER_ALPHA");
2e7a6b03 258 flexgridsizer->Add(m_text, 1, wxGROW);
2b61c41b 259
fcd209b6
FM
260
261 // Now set a wxTextValidator with an explicit list of characters NOT allowed:
58fa61db 262 wxTextValidator textVal(wxFILTER_EMPTY|wxFILTER_EXCLUDE_LIST, &g_data.m_string2);
fcd209b6 263 textVal.SetCharExcludes("bcwyz");
58fa61db
FM
264 wxTextCtrl* txt2 =
265 new wxTextCtrl(this, VALIDATE_TEXT2, wxEmptyString,
266 wxDefaultPosition, wxDefaultSize, 0, textVal);
06f89fe4 267 txt2->SetToolTip("uses wxTextValidator with wxFILTER_EMPTY|wxFILTER_EXCLUDE_LIST (to exclude 'bcwyz')");
58fa61db 268 flexgridsizer->Add(txt2, 1, wxGROW);
2b61c41b
VZ
269
270 flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST,
06f89fe4 271 wxDefaultPosition, wxDefaultSize,
e01ea38b 272 3, g_listbox_choices, wxLB_MULTIPLE,
2e7a6b03
FM
273 wxGenericValidator(&g_data.m_listbox_choices)),
274 1, wxGROW);
2b61c41b 275
2e7a6b03 276 m_combobox = new wxComboBox(this, VALIDATE_COMBO, wxEmptyString,
06f89fe4 277 wxDefaultPosition, wxDefaultSize,
e01ea38b 278 3, g_combobox_choices, 0L,
2e7a6b03 279 MyComboBoxValidator(&g_data.m_combobox_choice));
58fa61db 280 m_combobox->SetToolTip("uses a custom validator (MyComboBoxValidator)");
2e7a6b03
FM
281 flexgridsizer->Add(m_combobox, 1, wxALIGN_CENTER);
282
fcd209b6
FM
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"),
06f89fe4 288 wxDefaultPosition, wxDefaultSize, 0,
fcd209b6 289 wxGenericValidator(&g_data.m_checkbox_state)),
06f89fe4 290 1, wxALIGN_CENTER|wxALL, 15);
fcd209b6 291
2e7a6b03
FM
292 flexgridsizer->AddGrowableCol(0);
293 flexgridsizer->AddGrowableCol(1);
294 flexgridsizer->AddGrowableRow(1);
2b61c41b 295
2b61c41b 296
e01ea38b
FM
297 // setup the button sizer
298 // ----------------------
2e7a6b03 299
e01ea38b
FM
300 wxStdDialogButtonSizer *btn = new wxStdDialogButtonSizer();
301 btn->AddButton(new wxButton(this, wxID_OK));
302 btn->AddButton(new wxButton(this, wxID_CANCEL));
303 btn->Realize();
2b61c41b 304
a54cf371
VZ
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
2b61c41b 348
e01ea38b
FM
349 // setup the main sizer
350 // --------------------
2e7a6b03 351
e01ea38b
FM
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"),
06f89fe4 357 wxDefaultPosition, wxDefaultSize,
e01ea38b
FM
358 3, g_radiobox_choices, 1, wxRA_SPECIFY_ROWS,
359 wxGenericValidator(&g_data.m_radiobox_choice)),
2e7a6b03 360 0, wxGROW | wxLEFT|wxBOTTOM|wxRIGHT, 10);
2b61c41b 361
a54cf371
VZ
362 mainsizer->Add( numSizer, 0, wxGROW | wxALL );
363
e01ea38b 364 mainsizer->Add(btn, 0, wxGROW | wxALL, 10);
2b61c41b
VZ
365
366 SetSizer(mainsizer);
367 mainsizer->SetSizeHints(this);
2e7a6b03
FM
368
369 // make the dialog a bit bigger than its minimal size:
370 SetSize(GetBestSize()*1.5);
2b61c41b 371}
457814b5 372
2b61c41b
VZ
373bool MyDialog::TransferDataToWindow()
374{
375 bool r = wxDialog::TransferDataToWindow();
2e7a6b03 376
2b61c41b
VZ
377 // These function calls have to be made here, after the
378 // dialog has been created.
2e7a6b03
FM
379 m_text->SetFocus();
380 m_combobox->SetSelection(0);
381
2b61c41b 382 return r;
457814b5
JS
383}
384