1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows validator sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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.
19 # pragma implementation
22 // For compilers that support precompilation, includes "wx/wx.h".
23 #include "wx/wxprec.h"
27 #endif // __BORLANDC__
36 #include "wx/valgen.h"
37 #include "wx/valtext.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
45 wxString g_listbox_choices
[] =
46 {"one", "two", "three"};
48 wxString g_combobox_choices
[] =
49 {"yes", "no", "maybe"};
51 wxString g_radiobox_choices
[] =
52 {"green", "yellow", "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
= "Spaces are invalid here";
66 m_listbox_choices
.Add(0);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
77 // Create and display the main frame window.
78 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "Validator Test", 50, 50, 300, 250);
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
89 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
90 EVT_MENU(VALIDATE_TEST_DIALOG
, MyFrame::OnTestDialog
)
91 EVT_MENU(VALIDATE_TOGGLE_BELL
, MyFrame::OnToggleBell
)
94 MyFrame::MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
)
95 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
99 SetIcon(wxIcon(_T("mondrian")));
102 // Create a listbox to display the validated data.
103 m_listbox
= new wxListBox(this, -1);
104 m_listbox
->Append(wxString(_T("Try 'File|Test' to see how validators work.")));
106 wxMenu
*file_menu
= new wxMenu
;
108 file_menu
->Append(VALIDATE_TEST_DIALOG
, "&Test", "Demonstrate validators");
109 file_menu
->Append(VALIDATE_TOGGLE_BELL
, "&Bell on error", "Toggle bell on error", true);
110 file_menu
->AppendSeparator();
111 file_menu
->Append(wxID_EXIT
, "E&xit");
113 wxMenuBar
*menu_bar
= new wxMenuBar
;
114 menu_bar
->Append(file_menu
, "File");
115 SetMenuBar(menu_bar
);
117 // All validators share a common (static) flag that controls
118 // whether they beep on error. Here we turn it off:
119 wxValidator::SetBellOnError(m_silent
);
120 file_menu
->Check(VALIDATE_TOGGLE_BELL
, !wxValidator::IsSilent());
125 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
130 void MyFrame::OnTestDialog(wxCommandEvent
& WXUNUSED(event
))
132 // The validators defined in the dialog implementation bind controls
133 // and variables together. Values are transferred between them behind
134 // the scenes, so here we don't have to query the controls for their
136 MyDialog
dialog(this, "Validator demonstration");
138 // When the dialog is displayed, validators automatically transfer
139 // data from variables to their corresponding controls.
140 if ( dialog
.ShowModal() == wxID_OK
)
142 // 'OK' was pressed, so controls that have validators are
143 // automatically transferred to the variables we specified
144 // when we created the validators.
146 m_listbox
->Append(wxString(_T("string: ")) + g_data
.m_string
);
147 for(unsigned int i
= 0; i
< g_data
.m_listbox_choices
.GetCount(); ++i
)
149 int j
= g_data
.m_listbox_choices
[i
];
150 m_listbox
->Append(wxString(_T("listbox choice(s): ")) + g_listbox_choices
[j
]);
153 wxString
checkbox_state(g_data
.m_checkbox_state
? _T("checked") : _T("unchecked"));
154 m_listbox
->Append(wxString(_T("checkbox: ")) + checkbox_state
);
155 m_listbox
->Append(wxString(_T("combobox: ")) + g_data
.m_combobox_choice
);
156 m_listbox
->Append(wxString(_T("radiobox: ")) + g_radiobox_choices
[g_data
.m_radiobox_choice
]);
160 void MyFrame::OnToggleBell(wxCommandEvent
& event
)
162 m_silent
= !m_silent
;
163 wxValidator::SetBellOnError(m_silent
);
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 MyDialog::MyDialog( wxWindow
*parent
, const wxString
& title
,
172 const wxPoint
& pos
, const wxSize
& size
, const long WXUNUSED(style
) ) :
173 wxDialog(parent
, VALIDATE_DIALOG_ID
, title
, pos
, size
, wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
|wxRESIZE_BORDER
)
175 // Sizers automatically ensure a workable layout.
176 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
177 wxFlexGridSizer
*flexgridsizer
= new wxFlexGridSizer(2, 2, 5, 5);
179 // Create and add controls to sizers. Note that a member variable
180 // of g_data is bound to each control upon construction. There is
181 // currently no easy way to substitute a different validator or a
182 // different transfer variable after a control has been constructed.
184 // Pointers to some of these controls are saved in member variables
185 // so that we can use them elsewhere, like this one.
186 text
= new wxTextCtrl(this, VALIDATE_TEXT
, "",
187 wxPoint(10, 10), wxSize(120, -1), 0,
188 wxTextValidator(wxFILTER_ALPHA
, &g_data
.m_string
));
189 flexgridsizer
->Add(text
);
191 // This wxCheckBox* doesn't need to be assigned to any pointer
192 // because we don't use it elsewhere--it can be anonymous.
193 // We don't need any such pointer to query its state, which
194 // can be gotten directly from g_data.
195 flexgridsizer
->Add(new wxCheckBox(this, VALIDATE_CHECK
, "Sample checkbox",
196 wxPoint(130, 10), wxSize(120, -1), 0,
197 wxGenericValidator(&g_data
.m_checkbox_state
)));
199 flexgridsizer
->Add(new wxListBox((wxWindow
*)this, VALIDATE_LIST
,
200 wxPoint(10, 30), wxSize(120, -1),
201 3, g_listbox_choices
, wxLB_MULTIPLE
,
202 wxGenericValidator(&g_data
.m_listbox_choices
)));
204 combobox
= new wxComboBox((wxWindow
*)this, VALIDATE_COMBO
, "",
205 wxPoint(130, 30), wxSize(120, -1),
206 3, g_combobox_choices
, 0L,
207 wxGenericValidator(&g_data
.m_combobox_choice
));
208 flexgridsizer
->Add(combobox
);
210 mainsizer
->Add(flexgridsizer
, 1, wxGROW
| wxALL
, 10);
212 mainsizer
->Add(new wxRadioBox((wxWindow
*)this, VALIDATE_RADIO
, "Pick a color",
213 wxPoint(10, 100), wxSize(-1, -1),
214 3, g_radiobox_choices
, 1, wxRA_SPECIFY_ROWS
,
215 wxGenericValidator(&g_data
.m_radiobox_choice
)),
216 0, wxGROW
| wxALL
, 10);
218 wxGridSizer
*gridsizer
= new wxGridSizer(2, 2, 5, 5);
220 wxButton
*ok_button
= new wxButton(this, wxID_OK
, "OK", wxPoint(250, 70), wxSize(80, 30));
221 ok_button
->SetDefault();
222 gridsizer
->Add(ok_button
);
223 gridsizer
->Add(new wxButton(this, wxID_CANCEL
, "Cancel", wxPoint(250, 100), wxSize(80, 30)));
225 mainsizer
->Add(gridsizer
, 0, wxGROW
| wxALL
, 10);
228 mainsizer
->SetSizeHints(this);
231 bool MyDialog::TransferDataToWindow()
233 bool r
= wxDialog::TransferDataToWindow();
234 // These function calls have to be made here, after the
235 // dialog has been created.
237 combobox
->SetSelection(0);