1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets validator sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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.
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
23 #endif // __BORLANDC__
32 #include "wx/valgen.h"
33 #include "wx/valtext.h"
34 #include "wx/valnum.h"
36 #ifndef wxHAS_IMAGES_IN_RESOURCES
37 #include "../sample.xpm"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
46 wxString g_listbox_choices
[] =
47 {wxT("one"), wxT("two"), wxT("three")};
49 wxString g_combobox_choices
[] =
50 {wxT("yes"), wxT("no (doesn't validate)"), wxT("maybe (doesn't validate)")};
52 wxString g_radiobox_choices
[] =
53 {wxT("green"), wxT("yellow"), wxT("red")};
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
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);
68 m_doubleValue
= 12354.31;
71 // ----------------------------------------------------------------------------
72 // MyComboBoxValidator
73 // ----------------------------------------------------------------------------
75 bool MyComboBoxValidator::Validate(wxWindow
*WXUNUSED(parent
))
77 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox
)));
79 wxComboBox
* cb
= (wxComboBox
*)GetWindow();
80 if (cb
->GetValue() == g_combobox_choices
[1] ||
81 cb
->GetValue() == g_combobox_choices
[2])
83 // we accept any string != g_combobox_choices[1|2] !
85 wxLogError("Invalid combo box text!");
90 *m_var
= cb
->GetValue();
95 bool MyComboBoxValidator::TransferToWindow()
97 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox
)));
101 wxComboBox
* cb
= (wxComboBox
*)GetWindow();
105 cb
->SetValue(*m_var
);
111 bool MyComboBoxValidator::TransferFromWindow()
113 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox
)));
117 wxComboBox
* cb
= (wxComboBox
*)GetWindow();
121 *m_var
= cb
->GetValue();
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
135 if ( !wxApp::OnInit() )
138 // Create and display the main frame window.
139 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, wxT("Validator Test"),
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
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
)
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
)),
160 SetIcon(wxICON(sample
));
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.")));
166 wxMenu
*file_menu
= new wxMenu
;
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"));
173 wxMenuBar
*menu_bar
= new wxMenuBar
;
174 menu_bar
->Append(file_menu
, wxT("&File"));
175 SetMenuBar(menu_bar
);
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());
184 #endif // wxUSE_STATUSBAR
187 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
192 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
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
198 MyDialog
dialog(this, wxT("Validator demonstration"));
200 // When the dialog is displayed, validators automatically transfer
201 // data from variables to their corresponding controls.
202 if ( dialog
.ShowModal() == wxID_OK
)
204 // 'OK' was pressed, so controls that have validators are
205 // automatically transferred to the variables we specified
206 // when we created the validators.
208 m_listbox
->Append(wxString(wxT("string: ")) + g_data
.m_string
);
209 m_listbox
->Append(wxString(wxT("string #2: ")) + g_data
.m_string2
);
211 for(unsigned int i
= 0; i
< g_data
.m_listbox_choices
.GetCount(); ++i
)
213 int j
= g_data
.m_listbox_choices
[i
];
214 m_listbox
->Append(wxString(wxT("listbox choice(s): ")) + g_listbox_choices
[j
]);
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
]);
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
));
227 void MyFrame::OnToggleBell(wxCommandEvent
& event
)
229 m_silent
= !m_silent
;
230 wxValidator::SuppressBellOnError(m_silent
);
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
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
)
242 // setup the flex grid sizer
243 // -------------------------
245 wxFlexGridSizer
*flexgridsizer
= new wxFlexGridSizer(3, 2, 5, 5);
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.
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
);
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");
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
);
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
)),
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
);
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);
292 flexgridsizer
->AddGrowableCol(0);
293 flexgridsizer
->AddGrowableCol(1);
294 flexgridsizer
->AddGrowableRow(1);
297 // setup the button sizer
298 // ----------------------
300 wxStdDialogButtonSizer
*btn
= new wxStdDialogButtonSizer();
301 btn
->AddButton(new wxButton(this, wxID_OK
));
302 btn
->AddButton(new wxButton(this, wxID_CANCEL
));
305 // setup a sizer with the controls for numeric validators
306 // ------------------------------------------------------
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
313 m_numericTextInt
= new wxTextCtrl
323 m_numericTextInt
->SetToolTip("uses wxIntegerValidator to accept positive "
326 m_numericTextDouble
= new wxTextCtrl
334 wxMakeFloatingPointValidator
337 &g_data
.m_doubleValue
,
338 wxNUM_VAL_THOUSANDS_SEPARATOR
|
339 wxNUM_VAL_NO_TRAILING_ZEROES
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 );
349 // setup the main sizer
350 // --------------------
352 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
354 mainsizer
->Add(flexgridsizer
, 1, wxGROW
| wxALL
, 10);
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);
362 mainsizer
->Add( numSizer
, 0, wxGROW
| wxALL
);
364 mainsizer
->Add(btn
, 0, wxGROW
| wxALL
, 10);
367 mainsizer
->SetSizeHints(this);
369 // make the dialog a bit bigger than its minimal size:
370 SetSize(GetBestSize()*1.5);
373 bool MyDialog::TransferDataToWindow()
375 bool r
= wxDialog::TransferDataToWindow();
377 // These function calls have to be made here, after the
378 // dialog has been created.
380 m_combobox
->SetSelection(0);