]> git.saurik.com Git - wxWidgets.git/blame - samples/validate/validate.cpp
stop usign a deprecated method, and ensure that the imagelist
[wxWidgets.git] / samples / validate / validate.cpp
CommitLineData
457814b5
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: validate.cpp
2b61c41b 3// Purpose: wxWindows 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 18#ifdef __GNUG__
2b61c41b
VZ
19# pragma implementation
20#endif // __GNUG__
457814b5
JS
21
22// For compilers that support precompilation, includes "wx/wx.h".
23#include "wx/wxprec.h"
24
25#ifdef __BORLANDC__
2b61c41b
VZ
26# pragma hdrstop
27#endif // __BORLANDC__
457814b5
JS
28
29#ifndef WX_PRECOMP
2b61c41b
VZ
30# include "wx/wx.h"
31#endif // WX_PRECOMP
457814b5
JS
32
33#include "validate.h"
34
2b61c41b
VZ
35#include "wx/sizer.h"
36#include "wx/valgen.h"
37#include "wx/valtext.h"
457814b5 38
2b61c41b
VZ
39// ----------------------------------------------------------------------------
40// Global data
41// ----------------------------------------------------------------------------
457814b5 42
a994f81b 43MyData g_data;
457814b5 44
2b61c41b
VZ
45wxString g_listbox_choices[] =
46 {"one", "two", "three"};
47
48wxString g_combobox_choices[] =
49 {"yes", "no", "maybe"};
50
51wxString g_radiobox_choices[] =
52 {"green", "yellow", "red"};
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
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}
457814b5 68
2b61c41b
VZ
69// ----------------------------------------------------------------------------
70// MyApp
71// ----------------------------------------------------------------------------
a994f81b 72
2b61c41b 73IMPLEMENT_APP(MyApp)
a994f81b 74
2b61c41b
VZ
75bool 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;
a994f81b
VZ
82}
83
2b61c41b
VZ
84// ----------------------------------------------------------------------------
85// MyFrame
86// ----------------------------------------------------------------------------
87
88BEGIN_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)
92END_EVENT_TABLE()
93
94MyFrame::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)
a994f81b 97{
2049ba38 98#ifdef __WXMSW__
2b61c41b
VZ
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.")));
457814b5 105
2b61c41b 106 wxMenu *file_menu = new wxMenu;
457814b5 107
2b61c41b
VZ
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");
457814b5 112
2b61c41b
VZ
113 wxMenuBar *menu_bar = new wxMenuBar;
114 menu_bar->Append(file_menu, "File");
115 SetMenuBar(menu_bar);
457814b5 116
2b61c41b
VZ
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());
457814b5 121
2b61c41b 122 CreateStatusBar(1);
457814b5
JS
123}
124
cb43b372 125void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 126{
2b61c41b 127 Close(true);
457814b5
JS
128}
129
cb43b372 130void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
457814b5 131{
2b61c41b
VZ
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 }
a994f81b
VZ
158}
159
2b61c41b 160void MyFrame::OnToggleBell(wxCommandEvent& event)
a994f81b 161{
2b61c41b
VZ
162 m_silent = !m_silent;
163 wxValidator::SetBellOnError(m_silent);
a994f81b 164 event.Skip();
457814b5
JS
165}
166
2b61c41b
VZ
167// ----------------------------------------------------------------------------
168// MyDialog
169// ----------------------------------------------------------------------------
170
a994f81b 171MyDialog::MyDialog( wxWindow *parent, const wxString& title,
cb43b372 172 const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
2b61c41b 173 wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL|wxRESIZE_BORDER)
457814b5 174{
2b61c41b
VZ
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}
457814b5 230
2b61c41b
VZ
231bool 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;
457814b5
JS
239}
240