]> git.saurik.com Git - wxWidgets.git/blob - samples/validate/validate.cpp
another update, only 7 untranslated strings left
[wxWidgets.git] / samples / validate / validate.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: validate.cpp
3 // Purpose: wxWindows validator sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
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
18 #ifdef __GNUG__
19 # pragma implementation
20 #endif // __GNUG__
21
22 // For compilers that support precompilation, includes "wx/wx.h".
23 #include "wx/wxprec.h"
24
25 #ifdef __BORLANDC__
26 # pragma hdrstop
27 #endif // __BORLANDC__
28
29 #ifndef WX_PRECOMP
30 # include "wx/wx.h"
31 #endif // WX_PRECOMP
32
33 #include "validate.h"
34
35 #include "wx/sizer.h"
36 #include "wx/valgen.h"
37 #include "wx/valtext.h"
38
39 // ----------------------------------------------------------------------------
40 // Global data
41 // ----------------------------------------------------------------------------
42
43 MyData g_data;
44
45 wxString g_listbox_choices[] =
46 {"one", "two", "three"};
47
48 wxString g_combobox_choices[] =
49 {"yes", "no", "maybe"};
50
51 wxString g_radiobox_choices[] =
52 {"green", "yellow", "red"};
53
54 // ----------------------------------------------------------------------------
55 // MyData
56 // ----------------------------------------------------------------------------
57
58 MyData::MyData()
59 {
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);
67 }
68
69 // ----------------------------------------------------------------------------
70 // MyApp
71 // ----------------------------------------------------------------------------
72
73 IMPLEMENT_APP(MyApp)
74
75 bool MyApp::OnInit()
76 {
77 // Create and display the main frame window.
78 MyFrame *frame = new MyFrame((wxFrame *) NULL, "Validator Test", 50, 50, 300, 250);
79 frame->Show(true);
80 SetTopWindow(frame);
81 return true;
82 }
83
84 // ----------------------------------------------------------------------------
85 // MyFrame
86 // ----------------------------------------------------------------------------
87
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)
92 END_EVENT_TABLE()
93
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)),
96 m_silent(true)
97 {
98 #ifdef __WXMSW__
99 SetIcon(wxIcon(_T("mondrian")));
100 #endif // __WXMSW__
101
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.")));
105
106 wxMenu *file_menu = new wxMenu;
107
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");
112
113 wxMenuBar *menu_bar = new wxMenuBar;
114 menu_bar->Append(file_menu, "File");
115 SetMenuBar(menu_bar);
116
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());
121
122 CreateStatusBar(1);
123 }
124
125 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
126 {
127 Close(true);
128 }
129
130 void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
131 {
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
135 // values.
136 MyDialog dialog(this, "Validator demonstration");
137
138 // When the dialog is displayed, validators automatically transfer
139 // data from variables to their corresponding controls.
140 if ( dialog.ShowModal() == wxID_OK )
141 {
142 // 'OK' was pressed, so controls that have validators are
143 // automatically transferred to the variables we specified
144 // when we created the validators.
145 m_listbox->Clear();
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)
148 {
149 int j = g_data.m_listbox_choices[i];
150 m_listbox->Append(wxString(_T("listbox choice(s): ")) + g_listbox_choices[j]);
151 }
152
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]);
157 }
158 }
159
160 void MyFrame::OnToggleBell(wxCommandEvent& event)
161 {
162 m_silent = !m_silent;
163 wxValidator::SetBellOnError(m_silent);
164 event.Skip();
165 }
166
167 // ----------------------------------------------------------------------------
168 // MyDialog
169 // ----------------------------------------------------------------------------
170
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)
174 {
175 // Sizers automatically ensure a workable layout.
176 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
177 wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(2, 2, 5, 5);
178
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.
183
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);
190
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)));
198
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)));
203
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);
209
210 mainsizer->Add(flexgridsizer, 1, wxGROW | wxALL, 10);
211
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);
217
218 wxGridSizer *gridsizer = new wxGridSizer(2, 2, 5, 5);
219
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)));
224
225 mainsizer->Add(gridsizer, 0, wxGROW | wxALL, 10);
226
227 SetSizer(mainsizer);
228 mainsizer->SetSizeHints(this);
229 }
230
231 bool MyDialog::TransferDataToWindow()
232 {
233 bool r = wxDialog::TransferDataToWindow();
234 // These function calls have to be made here, after the
235 // dialog has been created.
236 text->SetFocus();
237 combobox->SetSelection(0);
238 return r;
239 }
240