]>
Commit | Line | Data |
---|---|---|
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 | 43 | MyData g_data; |
457814b5 | 44 | |
2b61c41b | 45 | wxString g_listbox_choices[] = |
7df07b10 | 46 | {wxT("one"), wxT("two"), wxT("three")}; |
2b61c41b VZ |
47 | |
48 | wxString g_combobox_choices[] = | |
7df07b10 | 49 | {wxT("yes"), wxT("no"), wxT("maybe")}; |
2b61c41b VZ |
50 | |
51 | wxString g_radiobox_choices[] = | |
7df07b10 | 52 | {wxT("green"), wxT("yellow"), wxT("red")}; |
2b61c41b VZ |
53 | |
54 | // ---------------------------------------------------------------------------- | |
55 | // MyData | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | MyData::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 | |
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. | |
7df07b10 | 65 | m_string = wxT("Spaces are invalid here"); |
2b61c41b VZ |
66 | m_listbox_choices.Add(0); |
67 | } | |
457814b5 | 68 | |
2b61c41b VZ |
69 | // ---------------------------------------------------------------------------- |
70 | // MyApp | |
71 | // ---------------------------------------------------------------------------- | |
a994f81b | 72 | |
2b61c41b | 73 | IMPLEMENT_APP(MyApp) |
a994f81b | 74 | |
2b61c41b VZ |
75 | bool MyApp::OnInit() |
76 | { | |
45e6e6f8 VZ |
77 | if ( !wxApp::OnInit() ) |
78 | return false; | |
79 | ||
2b61c41b | 80 | // Create and display the main frame window. |
7df07b10 MB |
81 | MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("Validator Test"), |
82 | 50, 50, 300, 250); | |
2b61c41b VZ |
83 | frame->Show(true); |
84 | SetTopWindow(frame); | |
85 | return true; | |
a994f81b VZ |
86 | } |
87 | ||
2b61c41b VZ |
88 | // ---------------------------------------------------------------------------- |
89 | // MyFrame | |
90 | // ---------------------------------------------------------------------------- | |
91 | ||
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) | |
96 | END_EVENT_TABLE() | |
97 | ||
7df07b10 | 98 | MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int h) |
0bcd4039 | 99 | : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)), |
2b61c41b | 100 | m_silent(true) |
a994f81b | 101 | { |
0bcd4039 | 102 | SetIcon(wxICON(sample)); |
2b61c41b VZ |
103 | |
104 | // Create a listbox to display the validated data. | |
0bcd4039 | 105 | m_listbox = new wxListBox(this, wxID_ANY); |
2b61c41b | 106 | m_listbox->Append(wxString(_T("Try 'File|Test' to see how validators work."))); |
457814b5 | 107 | |
2b61c41b | 108 | wxMenu *file_menu = new wxMenu; |
457814b5 | 109 | |
7df07b10 | 110 | file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test"), wxT("Demonstrate validators")); |
2153bf89 | 111 | file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error")); |
2b61c41b | 112 | file_menu->AppendSeparator(); |
7df07b10 | 113 | file_menu->Append(wxID_EXIT, wxT("E&xit")); |
457814b5 | 114 | |
2b61c41b | 115 | wxMenuBar *menu_bar = new wxMenuBar; |
7df07b10 | 116 | menu_bar->Append(file_menu, wxT("File")); |
2b61c41b | 117 | SetMenuBar(menu_bar); |
457814b5 | 118 | |
2b61c41b VZ |
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()); | |
457814b5 | 123 | |
8520f137 | 124 | #if wxUSE_STATUSBAR |
2b61c41b | 125 | CreateStatusBar(1); |
8520f137 | 126 | #endif // wxUSE_STATUSBAR |
457814b5 JS |
127 | } |
128 | ||
cb43b372 | 129 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 130 | { |
2b61c41b | 131 | Close(true); |
457814b5 JS |
132 | } |
133 | ||
cb43b372 | 134 | void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 135 | { |
2b61c41b VZ |
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 | |
139 | // values. | |
7df07b10 | 140 | MyDialog dialog(this, wxT("Validator demonstration")); |
2b61c41b VZ |
141 | |
142 | // When the dialog is displayed, validators automatically transfer | |
143 | // data from variables to their corresponding controls. | |
144 | if ( dialog.ShowModal() == wxID_OK ) | |
145 | { | |
146 | // 'OK' was pressed, so controls that have validators are | |
147 | // automatically transferred to the variables we specified | |
148 | // when we created the validators. | |
149 | m_listbox->Clear(); | |
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) | |
152 | { | |
153 | int j = g_data.m_listbox_choices[i]; | |
154 | m_listbox->Append(wxString(_T("listbox choice(s): ")) + g_listbox_choices[j]); | |
155 | } | |
156 | ||
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]); | |
161 | } | |
a994f81b VZ |
162 | } |
163 | ||
2b61c41b | 164 | void MyFrame::OnToggleBell(wxCommandEvent& event) |
a994f81b | 165 | { |
2b61c41b VZ |
166 | m_silent = !m_silent; |
167 | wxValidator::SetBellOnError(m_silent); | |
a994f81b | 168 | event.Skip(); |
457814b5 JS |
169 | } |
170 | ||
2b61c41b VZ |
171 | // ---------------------------------------------------------------------------- |
172 | // MyDialog | |
173 | // ---------------------------------------------------------------------------- | |
174 | ||
a994f81b | 175 | MyDialog::MyDialog( wxWindow *parent, const wxString& title, |
cb43b372 | 176 | const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) : |
2a21ac15 | 177 | wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER) |
457814b5 | 178 | { |
e01ea38b FM |
179 | // setup the flex grid sizer |
180 | // ------------------------- | |
181 | ||
2b61c41b VZ |
182 | wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(2, 2, 5, 5); |
183 | ||
184 | // Create and add controls to sizers. Note that a member variable | |
185 | // of g_data is bound to each control upon construction. There is | |
186 | // currently no easy way to substitute a different validator or a | |
187 | // different transfer variable after a control has been constructed. | |
188 | ||
189 | // Pointers to some of these controls are saved in member variables | |
190 | // so that we can use them elsewhere, like this one. | |
0bcd4039 | 191 | text = new wxTextCtrl(this, VALIDATE_TEXT, wxEmptyString, |
e01ea38b FM |
192 | wxPoint(10, 10), wxSize(120, wxDefaultCoord), 0, |
193 | wxTextValidator(wxFILTER_ALPHA, &g_data.m_string)); | |
2b61c41b VZ |
194 | flexgridsizer->Add(text); |
195 | ||
196 | // This wxCheckBox* doesn't need to be assigned to any pointer | |
197 | // because we don't use it elsewhere--it can be anonymous. | |
198 | // We don't need any such pointer to query its state, which | |
199 | // can be gotten directly from g_data. | |
7df07b10 | 200 | flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"), |
e01ea38b FM |
201 | wxPoint(130, 10), wxSize(120, wxDefaultCoord), 0, |
202 | wxGenericValidator(&g_data.m_checkbox_state))); | |
2b61c41b VZ |
203 | |
204 | flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST, | |
e01ea38b FM |
205 | wxPoint(10, 30), wxSize(120, wxDefaultCoord), |
206 | 3, g_listbox_choices, wxLB_MULTIPLE, | |
207 | wxGenericValidator(&g_data.m_listbox_choices))); | |
2b61c41b | 208 | |
0bcd4039 | 209 | combobox = new wxComboBox((wxWindow*)this, VALIDATE_COMBO, wxEmptyString, |
e01ea38b FM |
210 | wxPoint(130, 30), wxSize(120, wxDefaultCoord), |
211 | 3, g_combobox_choices, 0L, | |
212 | wxGenericValidator(&g_data.m_combobox_choice)); | |
2b61c41b VZ |
213 | flexgridsizer->Add(combobox); |
214 | ||
2b61c41b | 215 | |
e01ea38b FM |
216 | // setup the button sizer |
217 | // ---------------------- | |
218 | ||
219 | wxStdDialogButtonSizer *btn = new wxStdDialogButtonSizer(); | |
220 | btn->AddButton(new wxButton(this, wxID_OK)); | |
221 | btn->AddButton(new wxButton(this, wxID_CANCEL)); | |
222 | btn->Realize(); | |
2b61c41b | 223 | |
2b61c41b | 224 | |
e01ea38b FM |
225 | // setup the main sizer |
226 | // -------------------- | |
227 | ||
228 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); | |
229 | ||
230 | mainsizer->Add(flexgridsizer, 1, wxGROW | wxALL, 10); | |
231 | ||
232 | mainsizer->Add(new wxRadioBox((wxWindow*)this, VALIDATE_RADIO, wxT("Pick a color"), | |
233 | wxPoint(10, 100), wxDefaultSize, | |
234 | 3, g_radiobox_choices, 1, wxRA_SPECIFY_ROWS, | |
235 | wxGenericValidator(&g_data.m_radiobox_choice)), | |
236 | 0, wxGROW | wxALL, 10); | |
2b61c41b | 237 | |
e01ea38b | 238 | mainsizer->Add(btn, 0, wxGROW | wxALL, 10); |
2b61c41b VZ |
239 | |
240 | SetSizer(mainsizer); | |
241 | mainsizer->SetSizeHints(this); | |
242 | } | |
457814b5 | 243 | |
2b61c41b VZ |
244 | bool MyDialog::TransferDataToWindow() |
245 | { | |
246 | bool r = wxDialog::TransferDataToWindow(); | |
247 | // These function calls have to be made here, after the | |
248 | // dialog has been created. | |
249 | text->SetFocus(); | |
250 | combobox->SetSelection(0); | |
251 | return r; | |
457814b5 JS |
252 | } |
253 |