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