]> git.saurik.com Git - wxWidgets.git/blame - samples/validate/validate.cpp
Fix referencing of cairo_t returned from wxDCImpl::GetCairoContext().
[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
6aa89a22 7// Copyright: (c) Julian Smart
526954c5 8// Licence: wxWindows licence
457814b5
JS
9/////////////////////////////////////////////////////////////////////////////
10
2b61c41b
VZ
11// See online help for an overview of validators. In general, a
12// validator transfers data between a control and a variable.
13// It may also test for validity of a string transferred to or
14// from a text control. All validators transfer data, but not
15// all test validity, so don't be confused by the name.
16
457814b5
JS
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
0bcd4039 21 #pragma hdrstop
2b61c41b 22#endif // __BORLANDC__
457814b5
JS
23
24#ifndef WX_PRECOMP
0bcd4039 25 #include "wx/wx.h"
2b61c41b 26#endif // WX_PRECOMP
457814b5
JS
27
28#include "validate.h"
29
2b61c41b
VZ
30#include "wx/sizer.h"
31#include "wx/valgen.h"
32#include "wx/valtext.h"
a54cf371 33#include "wx/valnum.h"
457814b5 34
e7092398 35#ifndef wxHAS_IMAGES_IN_RESOURCES
0bcd4039
WS
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 65 m_listbox_choices.Add(0);
a54cf371
VZ
66 m_intValue = 0;
67 m_doubleValue = 12354.31;
2b61c41b 68}
457814b5 69
2e7a6b03
FM
70// ----------------------------------------------------------------------------
71// MyComboBoxValidator
72// ----------------------------------------------------------------------------
73
74bool MyComboBoxValidator::Validate(wxWindow *WXUNUSED(parent))
75{
76 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
77
78 wxComboBox* cb = (wxComboBox*)GetWindow();
79 if (cb->GetValue() == g_combobox_choices[1] ||
80 cb->GetValue() == g_combobox_choices[2])
81 {
82 // we accept any string != g_combobox_choices[1|2] !
83
84 wxLogError("Invalid combo box text!");
85 return false;
86 }
87
88 if (m_var)
89 *m_var = cb->GetValue();
90
91 return true;
92}
93
94bool MyComboBoxValidator::TransferToWindow()
95{
96 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
97
98 if ( m_var )
99 {
100 wxComboBox* cb = (wxComboBox*)GetWindow();
101 if ( !cb )
102 return false;
103
104 cb->SetValue(*m_var);
105 }
106
107 return true;
108}
109
110bool MyComboBoxValidator::TransferFromWindow()
111{
112 wxASSERT(GetWindow()->IsKindOf(CLASSINFO(wxComboBox)));
113
114 if ( m_var )
115 {
116 wxComboBox* cb = (wxComboBox*)GetWindow();
117 if ( !cb )
118 return false;
119
120 *m_var = cb->GetValue();
121 }
122
123 return true;
124}
125
2b61c41b
VZ
126// ----------------------------------------------------------------------------
127// MyApp
128// ----------------------------------------------------------------------------
a994f81b 129
2b61c41b 130IMPLEMENT_APP(MyApp)
a994f81b 131
2b61c41b
VZ
132bool MyApp::OnInit()
133{
45e6e6f8
VZ
134 if ( !wxApp::OnInit() )
135 return false;
136
2b61c41b 137 // Create and display the main frame window.
7df07b10
MB
138 MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("Validator Test"),
139 50, 50, 300, 250);
2b61c41b 140 frame->Show(true);
18f42b94 141
2b61c41b 142 return true;
a994f81b
VZ
143}
144
2b61c41b
VZ
145// ----------------------------------------------------------------------------
146// MyFrame
147// ----------------------------------------------------------------------------
148
149BEGIN_EVENT_TABLE(MyFrame, wxFrame)
150 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
151 EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog)
152 EVT_MENU(VALIDATE_TOGGLE_BELL, MyFrame::OnToggleBell)
153END_EVENT_TABLE()
154
7df07b10 155MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int h)
0bcd4039 156 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
2b61c41b 157 m_silent(true)
a994f81b 158{
0bcd4039 159 SetIcon(wxICON(sample));
2b61c41b
VZ
160
161 // Create a listbox to display the validated data.
0bcd4039 162 m_listbox = new wxListBox(this, wxID_ANY);
9a83f860 163 m_listbox->Append(wxString(wxT("Try 'File|Test' to see how validators work.")));
457814b5 164
2b61c41b 165 wxMenu *file_menu = new wxMenu;
457814b5 166
fcd209b6 167 file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test dialog..."), wxT("Demonstrate validators"));
2153bf89 168 file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error"));
2b61c41b 169 file_menu->AppendSeparator();
7df07b10 170 file_menu->Append(wxID_EXIT, wxT("E&xit"));
457814b5 171
2b61c41b 172 wxMenuBar *menu_bar = new wxMenuBar;
2e7a6b03 173 menu_bar->Append(file_menu, wxT("&File"));
2b61c41b 174 SetMenuBar(menu_bar);
457814b5 175
2b61c41b
VZ
176 // All validators share a common (static) flag that controls
177 // whether they beep on error. Here we turn it off:
c27181d1 178 wxValidator::SuppressBellOnError(m_silent);
2b61c41b 179 file_menu->Check(VALIDATE_TOGGLE_BELL, !wxValidator::IsSilent());
457814b5 180
8520f137 181#if wxUSE_STATUSBAR
2b61c41b 182 CreateStatusBar(1);
8520f137 183#endif // wxUSE_STATUSBAR
457814b5
JS
184}
185
cb43b372 186void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 187{
2b61c41b 188 Close(true);
457814b5
JS
189}
190
cb43b372 191void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
457814b5 192{
2b61c41b
VZ
193 // The validators defined in the dialog implementation bind controls
194 // and variables together. Values are transferred between them behind
195 // the scenes, so here we don't have to query the controls for their
196 // values.
7df07b10 197 MyDialog dialog(this, wxT("Validator demonstration"));
2b61c41b
VZ
198
199 // When the dialog is displayed, validators automatically transfer
200 // data from variables to their corresponding controls.
201 if ( dialog.ShowModal() == wxID_OK )
202 {
203 // 'OK' was pressed, so controls that have validators are
204 // automatically transferred to the variables we specified
205 // when we created the validators.
206 m_listbox->Clear();
9a83f860
VZ
207 m_listbox->Append(wxString(wxT("string: ")) + g_data.m_string);
208 m_listbox->Append(wxString(wxT("string #2: ")) + g_data.m_string2);
fcd209b6 209
2b61c41b
VZ
210 for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i)
211 {
212 int j = g_data.m_listbox_choices[i];
9a83f860 213 m_listbox->Append(wxString(wxT("listbox choice(s): ")) + g_listbox_choices[j]);
2b61c41b
VZ
214 }
215
9a83f860
VZ
216 wxString checkbox_state(g_data.m_checkbox_state ? wxT("checked") : wxT("unchecked"));
217 m_listbox->Append(wxString(wxT("checkbox: ")) + checkbox_state);
218 m_listbox->Append(wxString(wxT("combobox: ")) + g_data.m_combobox_choice);
219 m_listbox->Append(wxString(wxT("radiobox: ")) + g_radiobox_choices[g_data.m_radiobox_choice]);
a54cf371
VZ
220
221 m_listbox->Append(wxString::Format("integer value: %d", g_data.m_intValue));
222 m_listbox->Append(wxString::Format("double value: %.3f", g_data.m_doubleValue));
2b61c41b 223 }
a994f81b
VZ
224}
225
2b61c41b 226void MyFrame::OnToggleBell(wxCommandEvent& event)
a994f81b 227{
2b61c41b 228 m_silent = !m_silent;
c27181d1 229 wxValidator::SuppressBellOnError(m_silent);
a994f81b 230 event.Skip();
457814b5
JS
231}
232
2b61c41b
VZ
233// ----------------------------------------------------------------------------
234// MyDialog
235// ----------------------------------------------------------------------------
236
a994f81b 237MyDialog::MyDialog( wxWindow *parent, const wxString& title,
cb43b372 238 const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
2a21ac15 239 wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
457814b5 240{
e01ea38b
FM
241 // setup the flex grid sizer
242 // -------------------------
243
fcd209b6 244 wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(3, 2, 5, 5);
2b61c41b
VZ
245
246 // Create and add controls to sizers. Note that a member variable
247 // of g_data is bound to each control upon construction. There is
248 // currently no easy way to substitute a different validator or a
249 // different transfer variable after a control has been constructed.
250
251 // Pointers to some of these controls are saved in member variables
252 // so that we can use them elsewhere, like this one.
2e7a6b03
FM
253 m_text = new wxTextCtrl(this, VALIDATE_TEXT, wxEmptyString,
254 wxDefaultPosition, wxDefaultSize, 0,
255 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
58fa61db 256 m_text->SetToolTip("uses wxTextValidator with wxFILTER_ALPHA");
2e7a6b03 257 flexgridsizer->Add(m_text, 1, wxGROW);
2b61c41b 258
fcd209b6
FM
259
260 // Now set a wxTextValidator with an explicit list of characters NOT allowed:
58fa61db 261 wxTextValidator textVal(wxFILTER_EMPTY|wxFILTER_EXCLUDE_LIST, &g_data.m_string2);
fcd209b6 262 textVal.SetCharExcludes("bcwyz");
58fa61db
FM
263 wxTextCtrl* txt2 =
264 new wxTextCtrl(this, VALIDATE_TEXT2, wxEmptyString,
265 wxDefaultPosition, wxDefaultSize, 0, textVal);
06f89fe4 266 txt2->SetToolTip("uses wxTextValidator with wxFILTER_EMPTY|wxFILTER_EXCLUDE_LIST (to exclude 'bcwyz')");
58fa61db 267 flexgridsizer->Add(txt2, 1, wxGROW);
2b61c41b
VZ
268
269 flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST,
06f89fe4 270 wxDefaultPosition, wxDefaultSize,
e01ea38b 271 3, g_listbox_choices, wxLB_MULTIPLE,
2e7a6b03
FM
272 wxGenericValidator(&g_data.m_listbox_choices)),
273 1, wxGROW);
2b61c41b 274
2e7a6b03 275 m_combobox = new wxComboBox(this, VALIDATE_COMBO, wxEmptyString,
06f89fe4 276 wxDefaultPosition, wxDefaultSize,
e01ea38b 277 3, g_combobox_choices, 0L,
2e7a6b03 278 MyComboBoxValidator(&g_data.m_combobox_choice));
58fa61db 279 m_combobox->SetToolTip("uses a custom validator (MyComboBoxValidator)");
2e7a6b03
FM
280 flexgridsizer->Add(m_combobox, 1, wxALIGN_CENTER);
281
fcd209b6
FM
282 // This wxCheckBox* doesn't need to be assigned to any pointer
283 // because we don't use it elsewhere--it can be anonymous.
284 // We don't need any such pointer to query its state, which
285 // can be gotten directly from g_data.
286 flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"),
06f89fe4 287 wxDefaultPosition, wxDefaultSize, 0,
fcd209b6 288 wxGenericValidator(&g_data.m_checkbox_state)),
06f89fe4 289 1, wxALIGN_CENTER|wxALL, 15);
fcd209b6 290
2e7a6b03
FM
291 flexgridsizer->AddGrowableCol(0);
292 flexgridsizer->AddGrowableCol(1);
293 flexgridsizer->AddGrowableRow(1);
2b61c41b 294
2b61c41b 295
e01ea38b
FM
296 // setup the button sizer
297 // ----------------------
2e7a6b03 298
e01ea38b
FM
299 wxStdDialogButtonSizer *btn = new wxStdDialogButtonSizer();
300 btn->AddButton(new wxButton(this, wxID_OK));
301 btn->AddButton(new wxButton(this, wxID_CANCEL));
302 btn->Realize();
2b61c41b 303
a54cf371
VZ
304 // setup a sizer with the controls for numeric validators
305 // ------------------------------------------------------
306
307 wxIntegerValidator<int> valInt(&g_data.m_intValue,
308 wxNUM_VAL_THOUSANDS_SEPARATOR |
309 wxNUM_VAL_ZERO_AS_BLANK);
310 valInt.SetMin(0); // Only allow positive numbers
311
312 m_numericTextInt = new wxTextCtrl
313 (
314 this,
315 wxID_ANY,
316 "",
317 wxDefaultPosition,
318 wxDefaultSize,
319 wxTE_RIGHT,
320 valInt
321 );
322 m_numericTextInt->SetToolTip("uses wxIntegerValidator to accept positive "
323 "integers only");
324
325 m_numericTextDouble = new wxTextCtrl
326 (
327 this,
328 wxID_ANY,
329 "",
330 wxDefaultPosition,
331 wxDefaultSize,
332 wxTE_RIGHT,
333 wxMakeFloatingPointValidator
334 (
335 3,
336 &g_data.m_doubleValue,
337 wxNUM_VAL_THOUSANDS_SEPARATOR |
338 wxNUM_VAL_NO_TRAILING_ZEROES
339 )
340 );
341 m_numericTextDouble->SetToolTip("uses wxFloatingPointValidator with 3 decimals");
342 wxBoxSizer *numSizer = new wxBoxSizer( wxHORIZONTAL );
343 numSizer->Add( m_numericTextInt, 1, wxALL, 10 );
344 numSizer->Add( m_numericTextDouble, 1, wxALL, 10 );
345
346
2b61c41b 347
e01ea38b
FM
348 // setup the main sizer
349 // --------------------
2e7a6b03 350
e01ea38b
FM
351 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
352
353 mainsizer->Add(flexgridsizer, 1, wxGROW | wxALL, 10);
354
355 mainsizer->Add(new wxRadioBox((wxWindow*)this, VALIDATE_RADIO, wxT("Pick a color"),
06f89fe4 356 wxDefaultPosition, wxDefaultSize,
e01ea38b
FM
357 3, g_radiobox_choices, 1, wxRA_SPECIFY_ROWS,
358 wxGenericValidator(&g_data.m_radiobox_choice)),
2e7a6b03 359 0, wxGROW | wxLEFT|wxBOTTOM|wxRIGHT, 10);
2b61c41b 360
a54cf371
VZ
361 mainsizer->Add( numSizer, 0, wxGROW | wxALL );
362
e01ea38b 363 mainsizer->Add(btn, 0, wxGROW | wxALL, 10);
2b61c41b
VZ
364
365 SetSizer(mainsizer);
366 mainsizer->SetSizeHints(this);
2e7a6b03
FM
367
368 // make the dialog a bit bigger than its minimal size:
369 SetSize(GetBestSize()*1.5);
2b61c41b 370}
457814b5 371
2b61c41b
VZ
372bool MyDialog::TransferDataToWindow()
373{
374 bool r = wxDialog::TransferDataToWindow();
2e7a6b03 375
2b61c41b
VZ
376 // These function calls have to be made here, after the
377 // dialog has been created.
2e7a6b03
FM
378 m_text->SetFocus();
379 m_combobox->SetSelection(0);
380
2b61c41b 381 return r;
457814b5
JS
382}
383