1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets validator sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
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"
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"), wxT("maybe")};
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. It would be nice to
63 // enhance this so that validation would occur when the text
64 // control loses focus.
65 m_string
= wxT("Spaces are invalid here");
66 m_listbox_choices
.Add(0);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
77 if ( !wxApp::OnInit() )
80 // Create and display the main frame window.
81 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, wxT("Validator Test"),
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
93 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
94 EVT_MENU(VALIDATE_TEST_DIALOG
, MyFrame::OnTestDialog
)
95 EVT_MENU(VALIDATE_TOGGLE_BELL
, MyFrame::OnToggleBell
)
98 MyFrame::MyFrame(wxFrame
*frame
, const wxString
&title
, int x
, int y
, int w
, int h
)
99 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
102 SetIcon(wxICON(sample
));
104 // Create a listbox to display the validated data.
105 m_listbox
= new wxListBox(this, wxID_ANY
);
106 m_listbox
->Append(wxString(_T("Try 'File|Test' to see how validators work.")));
108 wxMenu
*file_menu
= new wxMenu
;
110 file_menu
->Append(VALIDATE_TEST_DIALOG
, wxT("&Test"), wxT("Demonstrate validators"));
111 file_menu
->AppendCheckItem(VALIDATE_TOGGLE_BELL
, wxT("&Bell on error"), wxT("Toggle bell on error"));
112 file_menu
->AppendSeparator();
113 file_menu
->Append(wxID_EXIT
, wxT("E&xit"));
115 wxMenuBar
*menu_bar
= new wxMenuBar
;
116 menu_bar
->Append(file_menu
, wxT("File"));
117 SetMenuBar(menu_bar
);
119 // All validators share a common (static) flag that controls
120 // whether they beep on error. Here we turn it off:
121 wxValidator::SetBellOnError(m_silent
);
122 file_menu
->Check(VALIDATE_TOGGLE_BELL
, !wxValidator::IsSilent());
126 #endif // wxUSE_STATUSBAR
129 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
134 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
136 // The validators defined in the dialog implementation bind controls
137 // and variables together. Values are transferred between them behind
138 // the scenes, so here we don't have to query the controls for their
140 MyDialog
dialog(this, wxT("Validator demonstration"));
142 // When the dialog is displayed, validators automatically transfer
143 // data from variables to their corresponding controls.
144 if ( dialog
.ShowModal() == wxID_OK
)
146 // 'OK' was pressed, so controls that have validators are
147 // automatically transferred to the variables we specified
148 // when we created the validators.
150 m_listbox
->Append(wxString(_T("string: ")) + g_data
.m_string
);
151 for(unsigned int i
= 0; i
< g_data
.m_listbox_choices
.GetCount(); ++i
)
153 int j
= g_data
.m_listbox_choices
[i
];
154 m_listbox
->Append(wxString(_T("listbox choice(s): ")) + g_listbox_choices
[j
]);
157 wxString
checkbox_state(g_data
.m_checkbox_state
? _T("checked") : _T("unchecked"));
158 m_listbox
->Append(wxString(_T("checkbox: ")) + checkbox_state
);
159 m_listbox
->Append(wxString(_T("combobox: ")) + g_data
.m_combobox_choice
);
160 m_listbox
->Append(wxString(_T("radiobox: ")) + g_radiobox_choices
[g_data
.m_radiobox_choice
]);
164 void MyFrame::OnToggleBell(wxCommandEvent
& event
)
166 m_silent
= !m_silent
;
167 wxValidator::SetBellOnError(m_silent
);
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 MyDialog::MyDialog( wxWindow
*parent
, const wxString
& title
,
176 const wxPoint
& pos
, const wxSize
& size
, const long WXUNUSED(style
) ) :
177 wxDialog(parent
, VALIDATE_DIALOG_ID
, title
, pos
, size
, wxDEFAULT_DIALOG_STYLE
|wxRESIZE_BORDER
)
179 // Sizers automatically ensure a workable layout.
180 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
181 wxFlexGridSizer
*flexgridsizer
= new wxFlexGridSizer(2, 2, 5, 5);
183 // Create and add controls to sizers. Note that a member variable
184 // of g_data is bound to each control upon construction. There is
185 // currently no easy way to substitute a different validator or a
186 // different transfer variable after a control has been constructed.
188 // Pointers to some of these controls are saved in member variables
189 // so that we can use them elsewhere, like this one.
190 text
= new wxTextCtrl(this, VALIDATE_TEXT
, wxEmptyString
,
191 wxPoint(10, 10), wxSize(120, wxDefaultCoord
), 0,
192 wxTextValidator(wxFILTER_ALPHA
, &g_data
.m_string
));
193 flexgridsizer
->Add(text
);
195 // This wxCheckBox* doesn't need to be assigned to any pointer
196 // because we don't use it elsewhere--it can be anonymous.
197 // We don't need any such pointer to query its state, which
198 // can be gotten directly from g_data.
199 flexgridsizer
->Add(new wxCheckBox(this, VALIDATE_CHECK
, wxT("Sample checkbox"),
200 wxPoint(130, 10), wxSize(120, wxDefaultCoord
), 0,
201 wxGenericValidator(&g_data
.m_checkbox_state
)));
203 flexgridsizer
->Add(new wxListBox((wxWindow
*)this, VALIDATE_LIST
,
204 wxPoint(10, 30), wxSize(120, wxDefaultCoord
),
205 3, g_listbox_choices
, wxLB_MULTIPLE
,
206 wxGenericValidator(&g_data
.m_listbox_choices
)));
208 combobox
= new wxComboBox((wxWindow
*)this, VALIDATE_COMBO
, wxEmptyString
,
209 wxPoint(130, 30), wxSize(120, wxDefaultCoord
),
210 3, g_combobox_choices
, 0L,
211 wxGenericValidator(&g_data
.m_combobox_choice
));
212 flexgridsizer
->Add(combobox
);
214 mainsizer
->Add(flexgridsizer
, 1, wxGROW
| wxALL
, 10);
216 mainsizer
->Add(new wxRadioBox((wxWindow
*)this, VALIDATE_RADIO
, wxT("Pick a color"),
217 wxPoint(10, 100), wxDefaultSize
,
218 3, g_radiobox_choices
, 1, wxRA_SPECIFY_ROWS
,
219 wxGenericValidator(&g_data
.m_radiobox_choice
)),
220 0, wxGROW
| wxALL
, 10);
222 wxGridSizer
*gridsizer
= new wxGridSizer(2, 2, 5, 5);
224 wxButton
*ok_button
= new wxButton(this, wxID_OK
, wxT("OK"), wxPoint(250, 70), wxSize(80, 30));
225 ok_button
->SetDefault();
226 gridsizer
->Add(ok_button
);
227 gridsizer
->Add(new wxButton(this, wxID_CANCEL
, wxT("Cancel"), wxPoint(250, 100), wxSize(80, 30)));
229 mainsizer
->Add(gridsizer
, 0, wxGROW
| wxALL
, 10);
232 mainsizer
->SetSizeHints(this);
235 bool MyDialog::TransferDataToWindow()
237 bool r
= wxDialog::TransferDataToWindow();
238 // These function calls have to be made here, after the
239 // dialog has been created.
241 combobox
->SetSelection(0);