]> git.saurik.com Git - wxWidgets.git/blob - samples/validate/validate.cpp
Applied patch [ 1305650 ] ODBC32.lib is not included for borland by wxpresets
[wxWidgets.git] / samples / validate / validate.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: validate.cpp
3 // Purpose: wxWidgets 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 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 # pragma hdrstop
23 #endif // __BORLANDC__
24
25 #ifndef WX_PRECOMP
26 # include "wx/wx.h"
27 #endif // WX_PRECOMP
28
29 #include "validate.h"
30
31 #include "wx/sizer.h"
32 #include "wx/valgen.h"
33 #include "wx/valtext.h"
34
35 // ----------------------------------------------------------------------------
36 // Global data
37 // ----------------------------------------------------------------------------
38
39 MyData g_data;
40
41 wxString g_listbox_choices[] =
42 {wxT("one"), wxT("two"), wxT("three")};
43
44 wxString g_combobox_choices[] =
45 {wxT("yes"), wxT("no"), wxT("maybe")};
46
47 wxString g_radiobox_choices[] =
48 {wxT("green"), wxT("yellow"), wxT("red")};
49
50 // ----------------------------------------------------------------------------
51 // MyData
52 // ----------------------------------------------------------------------------
53
54 MyData::MyData()
55 {
56 // This string will be passed to an alpha-only validator, which
57 // will complain because spaces aren't alpha. Note that validation
58 // is performed only when 'OK' is pressed. It would be nice to
59 // enhance this so that validation would occur when the text
60 // control loses focus.
61 m_string = wxT("Spaces are invalid here");
62 m_listbox_choices.Add(0);
63 }
64
65 // ----------------------------------------------------------------------------
66 // MyApp
67 // ----------------------------------------------------------------------------
68
69 IMPLEMENT_APP(MyApp)
70
71 bool MyApp::OnInit()
72 {
73 // Create and display the main frame window.
74 MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("Validator Test"),
75 50, 50, 300, 250);
76 frame->Show(true);
77 SetTopWindow(frame);
78 return true;
79 }
80
81 // ----------------------------------------------------------------------------
82 // MyFrame
83 // ----------------------------------------------------------------------------
84
85 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
86 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
87 EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog)
88 EVT_MENU(VALIDATE_TOGGLE_BELL, MyFrame::OnToggleBell)
89 END_EVENT_TABLE()
90
91 MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int h)
92 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)),
93 m_silent(true)
94 {
95 #ifdef __WXMSW__
96 SetIcon(wxIcon(_T("mondrian")));
97 #endif // __WXMSW__
98
99 // Create a listbox to display the validated data.
100 m_listbox = new wxListBox(this, -1);
101 m_listbox->Append(wxString(_T("Try 'File|Test' to see how validators work.")));
102
103 wxMenu *file_menu = new wxMenu;
104
105 file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test"), wxT("Demonstrate validators"));
106 file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error"));
107 file_menu->AppendSeparator();
108 file_menu->Append(wxID_EXIT, wxT("E&xit"));
109
110 wxMenuBar *menu_bar = new wxMenuBar;
111 menu_bar->Append(file_menu, wxT("File"));
112 SetMenuBar(menu_bar);
113
114 // All validators share a common (static) flag that controls
115 // whether they beep on error. Here we turn it off:
116 wxValidator::SetBellOnError(m_silent);
117 file_menu->Check(VALIDATE_TOGGLE_BELL, !wxValidator::IsSilent());
118
119 #if wxUSE_STATUSBAR
120 CreateStatusBar(1);
121 #endif // wxUSE_STATUSBAR
122 }
123
124 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
125 {
126 Close(true);
127 }
128
129 void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
130 {
131 // The validators defined in the dialog implementation bind controls
132 // and variables together. Values are transferred between them behind
133 // the scenes, so here we don't have to query the controls for their
134 // values.
135 MyDialog dialog(this, wxT("Validator demonstration"));
136
137 // When the dialog is displayed, validators automatically transfer
138 // data from variables to their corresponding controls.
139 if ( dialog.ShowModal() == wxID_OK )
140 {
141 // 'OK' was pressed, so controls that have validators are
142 // automatically transferred to the variables we specified
143 // when we created the validators.
144 m_listbox->Clear();
145 m_listbox->Append(wxString(_T("string: ")) + g_data.m_string);
146 for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i)
147 {
148 int j = g_data.m_listbox_choices[i];
149 m_listbox->Append(wxString(_T("listbox choice(s): ")) + g_listbox_choices[j]);
150 }
151
152 wxString checkbox_state(g_data.m_checkbox_state ? _T("checked") : _T("unchecked"));
153 m_listbox->Append(wxString(_T("checkbox: ")) + checkbox_state);
154 m_listbox->Append(wxString(_T("combobox: ")) + g_data.m_combobox_choice);
155 m_listbox->Append(wxString(_T("radiobox: ")) + g_radiobox_choices[g_data.m_radiobox_choice]);
156 }
157 }
158
159 void MyFrame::OnToggleBell(wxCommandEvent& event)
160 {
161 m_silent = !m_silent;
162 wxValidator::SetBellOnError(m_silent);
163 event.Skip();
164 }
165
166 // ----------------------------------------------------------------------------
167 // MyDialog
168 // ----------------------------------------------------------------------------
169
170 MyDialog::MyDialog( wxWindow *parent, const wxString& title,
171 const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
172 wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
173 {
174 // Sizers automatically ensure a workable layout.
175 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
176 wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(2, 2, 5, 5);
177
178 // Create and add controls to sizers. Note that a member variable
179 // of g_data is bound to each control upon construction. There is
180 // currently no easy way to substitute a different validator or a
181 // different transfer variable after a control has been constructed.
182
183 // Pointers to some of these controls are saved in member variables
184 // so that we can use them elsewhere, like this one.
185 text = new wxTextCtrl(this, VALIDATE_TEXT, wxT(""),
186 wxPoint(10, 10), wxSize(120, -1), 0,
187 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
188 flexgridsizer->Add(text);
189
190 // This wxCheckBox* doesn't need to be assigned to any pointer
191 // because we don't use it elsewhere--it can be anonymous.
192 // We don't need any such pointer to query its state, which
193 // can be gotten directly from g_data.
194 flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"),
195 wxPoint(130, 10), wxSize(120, -1), 0,
196 wxGenericValidator(&g_data.m_checkbox_state)));
197
198 flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST,
199 wxPoint(10, 30), wxSize(120, -1),
200 3, g_listbox_choices, wxLB_MULTIPLE,
201 wxGenericValidator(&g_data.m_listbox_choices)));
202
203 combobox = new wxComboBox((wxWindow*)this, VALIDATE_COMBO, wxT(""),
204 wxPoint(130, 30), wxSize(120, -1),
205 3, g_combobox_choices, 0L,
206 wxGenericValidator(&g_data.m_combobox_choice));
207 flexgridsizer->Add(combobox);
208
209 mainsizer->Add(flexgridsizer, 1, wxGROW | wxALL, 10);
210
211 mainsizer->Add(new wxRadioBox((wxWindow*)this, VALIDATE_RADIO, wxT("Pick a color"),
212 wxPoint(10, 100), wxSize(-1, -1),
213 3, g_radiobox_choices, 1, wxRA_SPECIFY_ROWS,
214 wxGenericValidator(&g_data.m_radiobox_choice)),
215 0, wxGROW | wxALL, 10);
216
217 wxGridSizer *gridsizer = new wxGridSizer(2, 2, 5, 5);
218
219 wxButton *ok_button = new wxButton(this, wxID_OK, wxT("OK"), wxPoint(250, 70), wxSize(80, 30));
220 ok_button->SetDefault();
221 gridsizer->Add(ok_button);
222 gridsizer->Add(new wxButton(this, wxID_CANCEL, wxT("Cancel"), wxPoint(250, 100), wxSize(80, 30)));
223
224 mainsizer->Add(gridsizer, 0, wxGROW | wxALL, 10);
225
226 SetSizer(mainsizer);
227 mainsizer->SetSizeHints(this);
228 }
229
230 bool MyDialog::TransferDataToWindow()
231 {
232 bool r = wxDialog::TransferDataToWindow();
233 // These function calls have to be made here, after the
234 // dialog has been created.
235 text->SetFocus();
236 combobox->SetSelection(0);
237 return r;
238 }
239