1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets validator sample
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // See online help for an overview of validators. In general, a
12 // validator transfers data between a control and a variable.
13 // It may also test for validity of a string transferred to or
14 // from a text control. All validators transfer data, but not
15 // all test validity, so don't be confused by the name.
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
22 #endif // __BORLANDC__
31 #include "wx/valgen.h"
32 #include "wx/valtext.h"
33 #include "wx/valnum.h"
35 #ifndef wxHAS_IMAGES_IN_RESOURCES
36 #include "../sample.xpm"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
45 wxString g_listbox_choices
[] =
46 {wxT("one"), wxT("two"), wxT("three")};
48 wxString g_combobox_choices
[] =
49 {wxT("yes"), wxT("no (doesn't validate)"), wxT("maybe (doesn't validate)")};
51 wxString g_radiobox_choices
[] =
52 {wxT("green"), wxT("yellow"), wxT("red")};
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
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_string2
= "Valid text";
65 m_listbox_choices
.Add(0);
67 m_doubleValue
= 12354.31;
70 // ----------------------------------------------------------------------------
71 // MyComboBoxValidator
72 // ----------------------------------------------------------------------------
74 bool MyComboBoxValidator::Validate(wxWindow
*WXUNUSED(parent
))
76 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox
)));
78 wxComboBox
* cb
= (wxComboBox
*)GetWindow();
79 if (cb
->GetValue() == g_combobox_choices
[1] ||
80 cb
->GetValue() == g_combobox_choices
[2])
82 // we accept any string != g_combobox_choices[1|2] !
84 wxLogError("Invalid combo box text!");
89 *m_var
= cb
->GetValue();
94 bool MyComboBoxValidator::TransferToWindow()
96 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox
)));
100 wxComboBox
* cb
= (wxComboBox
*)GetWindow();
104 cb
->SetValue(*m_var
);
110 bool MyComboBoxValidator::TransferFromWindow()
112 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox
)));
116 wxComboBox
* cb
= (wxComboBox
*)GetWindow();
120 *m_var
= cb
->GetValue();
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
134 if ( !wxApp::OnInit() )
137 // Create and display the main frame window.
138 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, wxT("Validator Test"),
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
150 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
151 EVT_MENU(VALIDATE_TEST_DIALOG
, MyFrame::OnTestDialog
)
152 EVT_MENU(VALIDATE_TOGGLE_BELL
, MyFrame::OnToggleBell
)
155 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
)
156 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
159 SetIcon(wxICON(sample
));
161 // Create a listbox to display the validated data.
162 m_listbox
= new wxListBox(this, wxID_ANY
);
163 m_listbox
->Append(wxString(wxT("Try 'File|Test' to see how validators work.")));
165 wxMenu
*file_menu
= new wxMenu
;
167 file_menu
->Append(VALIDATE_TEST_DIALOG
, wxT("&Test dialog..."), wxT("Demonstrate validators"));
168 file_menu
->AppendCheckItem(VALIDATE_TOGGLE_BELL
, wxT("&Bell on error"), wxT("Toggle bell on error"));
169 file_menu
->AppendSeparator();
170 file_menu
->Append(wxID_EXIT
, wxT("E&xit"));
172 wxMenuBar
*menu_bar
= new wxMenuBar
;
173 menu_bar
->Append(file_menu
, wxT("&File"));
174 SetMenuBar(menu_bar
);
176 // All validators share a common (static) flag that controls
177 // whether they beep on error. Here we turn it off:
178 wxValidator::SuppressBellOnError(m_silent
);
179 file_menu
->Check(VALIDATE_TOGGLE_BELL
, !wxValidator::IsSilent());
183 #endif // wxUSE_STATUSBAR
186 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
191 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
193 // The validators defined in the dialog implementation bind controls
194 // and variables together. Values are transferred between them behind
195 // the scenes, so here we don't have to query the controls for their
197 MyDialog
dialog(this, wxT("Validator demonstration"));
199 // When the dialog is displayed, validators automatically transfer
200 // data from variables to their corresponding controls.
201 if ( dialog
.ShowModal() == wxID_OK
)
203 // 'OK' was pressed, so controls that have validators are
204 // automatically transferred to the variables we specified
205 // when we created the validators.
207 m_listbox
->Append(wxString(wxT("string: ")) + g_data
.m_string
);
208 m_listbox
->Append(wxString(wxT("string #2: ")) + g_data
.m_string2
);
210 for(unsigned int i
= 0; i
< g_data
.m_listbox_choices
.GetCount(); ++i
)
212 int j
= g_data
.m_listbox_choices
[i
];
213 m_listbox
->Append(wxString(wxT("listbox choice(s): ")) + g_listbox_choices
[j
]);
216 wxString
checkbox_state(g_data
.m_checkbox_state
? wxT("checked") : wxT("unchecked"));
217 m_listbox
->Append(wxString(wxT("checkbox: ")) + checkbox_state
);
218 m_listbox
->Append(wxString(wxT("combobox: ")) + g_data
.m_combobox_choice
);
219 m_listbox
->Append(wxString(wxT("radiobox: ")) + g_radiobox_choices
[g_data
.m_radiobox_choice
]);
221 m_listbox
->Append(wxString::Format("integer value: %d", g_data
.m_intValue
));
222 m_listbox
->Append(wxString::Format("double value: %.3f", g_data
.m_doubleValue
));
226 void MyFrame::OnToggleBell(wxCommandEvent
& event
)
228 m_silent
= !m_silent
;
229 wxValidator::SuppressBellOnError(m_silent
);
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 MyDialog::MyDialog( wxWindow
*parent
, const wxString
& title
,
238 const wxPoint
& pos
, const wxSize
& size
, const long WXUNUSED(style
) ) :
239 wxDialog(parent
, VALIDATE_DIALOG_ID
, title
, pos
, size
, wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
)
241 // setup the flex grid sizer
242 // -------------------------
244 wxFlexGridSizer
*flexgridsizer
= new wxFlexGridSizer(3, 2, 5, 5);
246 // Create and add controls to sizers. Note that a member variable
247 // of g_data is bound to each control upon construction. There is
248 // currently no easy way to substitute a different validator or a
249 // different transfer variable after a control has been constructed.
251 // Pointers to some of these controls are saved in member variables
252 // so that we can use them elsewhere, like this one.
253 m_text
= new wxTextCtrl(this, VALIDATE_TEXT
, wxEmptyString
,
254 wxDefaultPosition
, wxDefaultSize
, 0,
255 wxTextValidator(wxFILTER_ALPHA
, &g_data
.m_string
));
256 m_text
->SetToolTip("uses wxTextValidator with wxFILTER_ALPHA");
257 flexgridsizer
->Add(m_text
, 1, wxGROW
);
260 // Now set a wxTextValidator with an explicit list of characters NOT allowed:
261 wxTextValidator
textVal(wxFILTER_EMPTY
|wxFILTER_EXCLUDE_LIST
, &g_data
.m_string2
);
262 textVal
.SetCharExcludes("bcwyz");
264 new wxTextCtrl(this, VALIDATE_TEXT2
, wxEmptyString
,
265 wxDefaultPosition
, wxDefaultSize
, 0, textVal
);
266 txt2
->SetToolTip("uses wxTextValidator with wxFILTER_EMPTY|wxFILTER_EXCLUDE_LIST (to exclude 'bcwyz')");
267 flexgridsizer
->Add(txt2
, 1, wxGROW
);
269 flexgridsizer
->Add(new wxListBox((wxWindow
*)this, VALIDATE_LIST
,
270 wxDefaultPosition
, wxDefaultSize
,
271 3, g_listbox_choices
, wxLB_MULTIPLE
,
272 wxGenericValidator(&g_data
.m_listbox_choices
)),
275 m_combobox
= new wxComboBox(this, VALIDATE_COMBO
, wxEmptyString
,
276 wxDefaultPosition
, wxDefaultSize
,
277 3, g_combobox_choices
, 0L,
278 MyComboBoxValidator(&g_data
.m_combobox_choice
));
279 m_combobox
->SetToolTip("uses a custom validator (MyComboBoxValidator)");
280 flexgridsizer
->Add(m_combobox
, 1, wxALIGN_CENTER
);
282 // This wxCheckBox* doesn't need to be assigned to any pointer
283 // because we don't use it elsewhere--it can be anonymous.
284 // We don't need any such pointer to query its state, which
285 // can be gotten directly from g_data.
286 flexgridsizer
->Add(new wxCheckBox(this, VALIDATE_CHECK
, wxT("Sample checkbox"),
287 wxDefaultPosition
, wxDefaultSize
, 0,
288 wxGenericValidator(&g_data
.m_checkbox_state
)),
289 1, wxALIGN_CENTER
|wxALL
, 15);
291 flexgridsizer
->AddGrowableCol(0);
292 flexgridsizer
->AddGrowableCol(1);
293 flexgridsizer
->AddGrowableRow(1);
296 // setup the button sizer
297 // ----------------------
299 wxStdDialogButtonSizer
*btn
= new wxStdDialogButtonSizer();
300 btn
->AddButton(new wxButton(this, wxID_OK
));
301 btn
->AddButton(new wxButton(this, wxID_CANCEL
));
304 // setup a sizer with the controls for numeric validators
305 // ------------------------------------------------------
307 wxIntegerValidator
<int> valInt(&g_data
.m_intValue
,
308 wxNUM_VAL_THOUSANDS_SEPARATOR
|
309 wxNUM_VAL_ZERO_AS_BLANK
);
310 valInt
.SetMin(0); // Only allow positive numbers
312 m_numericTextInt
= new wxTextCtrl
322 m_numericTextInt
->SetToolTip("uses wxIntegerValidator to accept positive "
325 m_numericTextDouble
= new wxTextCtrl
333 wxMakeFloatingPointValidator
336 &g_data
.m_doubleValue
,
337 wxNUM_VAL_THOUSANDS_SEPARATOR
|
338 wxNUM_VAL_NO_TRAILING_ZEROES
341 m_numericTextDouble
->SetToolTip("uses wxFloatingPointValidator with 3 decimals");
342 wxBoxSizer
*numSizer
= new wxBoxSizer( wxHORIZONTAL
);
343 numSizer
->Add( m_numericTextInt
, 1, wxALL
, 10 );
344 numSizer
->Add( m_numericTextDouble
, 1, wxALL
, 10 );
348 // setup the main sizer
349 // --------------------
351 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
353 mainsizer
->Add(flexgridsizer
, 1, wxGROW
| wxALL
, 10);
355 mainsizer
->Add(new wxRadioBox((wxWindow
*)this, VALIDATE_RADIO
, wxT("Pick a color"),
356 wxDefaultPosition
, wxDefaultSize
,
357 3, g_radiobox_choices
, 1, wxRA_SPECIFY_ROWS
,
358 wxGenericValidator(&g_data
.m_radiobox_choice
)),
359 0, wxGROW
| wxLEFT
|wxBOTTOM
|wxRIGHT
, 10);
361 mainsizer
->Add( numSizer
, 0, wxGROW
| wxALL
);
363 mainsizer
->Add(btn
, 0, wxGROW
| wxALL
, 10);
366 mainsizer
->SetSizeHints(this);
368 // make the dialog a bit bigger than its minimal size:
369 SetSize(GetBestSize()*1.5);
372 bool MyDialog::TransferDataToWindow()
374 bool r
= wxDialog::TransferDataToWindow();
376 // These function calls have to be made here, after the
377 // dialog has been created.
379 m_combobox
->SetSelection(0);