]> git.saurik.com Git - wxWidgets.git/blame - samples/validate/validate.cpp
check default library directories in WX_PATH_FIND_LIBRARIES; do *not* add default...
[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
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
830f7bc4 18#if defined(__GNUG__) && !defined(__APPLE__)
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 45wxString g_listbox_choices[] =
7df07b10 46 {wxT("one"), wxT("two"), wxT("three")};
2b61c41b
VZ
47
48wxString g_combobox_choices[] =
7df07b10 49 {wxT("yes"), wxT("no"), wxT("maybe")};
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
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 73IMPLEMENT_APP(MyApp)
a994f81b 74
2b61c41b
VZ
75bool MyApp::OnInit()
76{
77 // Create and display the main frame window.
7df07b10
MB
78 MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("Validator Test"),
79 50, 50, 300, 250);
2b61c41b
VZ
80 frame->Show(true);
81 SetTopWindow(frame);
82 return true;
a994f81b
VZ
83}
84
2b61c41b
VZ
85// ----------------------------------------------------------------------------
86// MyFrame
87// ----------------------------------------------------------------------------
88
89BEGIN_EVENT_TABLE(MyFrame, wxFrame)
90 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
91 EVT_MENU(VALIDATE_TEST_DIALOG, MyFrame::OnTestDialog)
92 EVT_MENU(VALIDATE_TOGGLE_BELL, MyFrame::OnToggleBell)
93END_EVENT_TABLE()
94
7df07b10 95MyFrame::MyFrame(wxFrame *frame, const wxString&title, int x, int y, int w, int h)
2b61c41b
VZ
96 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)),
97 m_silent(true)
a994f81b 98{
2049ba38 99#ifdef __WXMSW__
2b61c41b
VZ
100 SetIcon(wxIcon(_T("mondrian")));
101#endif // __WXMSW__
102
103 // Create a listbox to display the validated data.
104 m_listbox = new wxListBox(this, -1);
105 m_listbox->Append(wxString(_T("Try 'File|Test' to see how validators work.")));
457814b5 106
2b61c41b 107 wxMenu *file_menu = new wxMenu;
457814b5 108
7df07b10 109 file_menu->Append(VALIDATE_TEST_DIALOG, wxT("&Test"), wxT("Demonstrate validators"));
2153bf89 110 file_menu->AppendCheckItem(VALIDATE_TOGGLE_BELL, wxT("&Bell on error"), wxT("Toggle bell on error"));
2b61c41b 111 file_menu->AppendSeparator();
7df07b10 112 file_menu->Append(wxID_EXIT, wxT("E&xit"));
457814b5 113
2b61c41b 114 wxMenuBar *menu_bar = new wxMenuBar;
7df07b10 115 menu_bar->Append(file_menu, wxT("File"));
2b61c41b 116 SetMenuBar(menu_bar);
457814b5 117
2b61c41b
VZ
118 // All validators share a common (static) flag that controls
119 // whether they beep on error. Here we turn it off:
120 wxValidator::SetBellOnError(m_silent);
121 file_menu->Check(VALIDATE_TOGGLE_BELL, !wxValidator::IsSilent());
457814b5 122
8520f137 123#if wxUSE_STATUSBAR
2b61c41b 124 CreateStatusBar(1);
8520f137 125#endif // wxUSE_STATUSBAR
457814b5
JS
126}
127
cb43b372 128void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
457814b5 129{
2b61c41b 130 Close(true);
457814b5
JS
131}
132
cb43b372 133void MyFrame::OnTestDialog(wxCommandEvent& WXUNUSED(event))
457814b5 134{
2b61c41b
VZ
135 // The validators defined in the dialog implementation bind controls
136 // and variables together. Values are transferred between them behind
137 // the scenes, so here we don't have to query the controls for their
138 // values.
7df07b10 139 MyDialog dialog(this, wxT("Validator demonstration"));
2b61c41b
VZ
140
141 // When the dialog is displayed, validators automatically transfer
142 // data from variables to their corresponding controls.
143 if ( dialog.ShowModal() == wxID_OK )
144 {
145 // 'OK' was pressed, so controls that have validators are
146 // automatically transferred to the variables we specified
147 // when we created the validators.
148 m_listbox->Clear();
149 m_listbox->Append(wxString(_T("string: ")) + g_data.m_string);
150 for(unsigned int i = 0; i < g_data.m_listbox_choices.GetCount(); ++i)
151 {
152 int j = g_data.m_listbox_choices[i];
153 m_listbox->Append(wxString(_T("listbox choice(s): ")) + g_listbox_choices[j]);
154 }
155
156 wxString checkbox_state(g_data.m_checkbox_state ? _T("checked") : _T("unchecked"));
157 m_listbox->Append(wxString(_T("checkbox: ")) + checkbox_state);
158 m_listbox->Append(wxString(_T("combobox: ")) + g_data.m_combobox_choice);
159 m_listbox->Append(wxString(_T("radiobox: ")) + g_radiobox_choices[g_data.m_radiobox_choice]);
160 }
a994f81b
VZ
161}
162
2b61c41b 163void MyFrame::OnToggleBell(wxCommandEvent& event)
a994f81b 164{
2b61c41b
VZ
165 m_silent = !m_silent;
166 wxValidator::SetBellOnError(m_silent);
a994f81b 167 event.Skip();
457814b5
JS
168}
169
2b61c41b
VZ
170// ----------------------------------------------------------------------------
171// MyDialog
172// ----------------------------------------------------------------------------
173
a994f81b 174MyDialog::MyDialog( wxWindow *parent, const wxString& title,
cb43b372 175 const wxPoint& pos, const wxSize& size, const long WXUNUSED(style) ) :
2a21ac15 176 wxDialog(parent, VALIDATE_DIALOG_ID, title, pos, size, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
457814b5 177{
2b61c41b
VZ
178 // Sizers automatically ensure a workable layout.
179 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
180 wxFlexGridSizer *flexgridsizer = new wxFlexGridSizer(2, 2, 5, 5);
181
182 // Create and add controls to sizers. Note that a member variable
183 // of g_data is bound to each control upon construction. There is
184 // currently no easy way to substitute a different validator or a
185 // different transfer variable after a control has been constructed.
186
187 // Pointers to some of these controls are saved in member variables
188 // so that we can use them elsewhere, like this one.
7df07b10 189 text = new wxTextCtrl(this, VALIDATE_TEXT, wxT(""),
2b61c41b
VZ
190 wxPoint(10, 10), wxSize(120, -1), 0,
191 wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));
192 flexgridsizer->Add(text);
193
194 // This wxCheckBox* doesn't need to be assigned to any pointer
195 // because we don't use it elsewhere--it can be anonymous.
196 // We don't need any such pointer to query its state, which
197 // can be gotten directly from g_data.
7df07b10 198 flexgridsizer->Add(new wxCheckBox(this, VALIDATE_CHECK, wxT("Sample checkbox"),
2b61c41b
VZ
199 wxPoint(130, 10), wxSize(120, -1), 0,
200 wxGenericValidator(&g_data.m_checkbox_state)));
201
202 flexgridsizer->Add(new wxListBox((wxWindow*)this, VALIDATE_LIST,
203 wxPoint(10, 30), wxSize(120, -1),
204 3, g_listbox_choices, wxLB_MULTIPLE,
205 wxGenericValidator(&g_data.m_listbox_choices)));
206
7df07b10 207 combobox = new wxComboBox((wxWindow*)this, VALIDATE_COMBO, wxT(""),
2b61c41b
VZ
208 wxPoint(130, 30), wxSize(120, -1),
209 3, g_combobox_choices, 0L,
210 wxGenericValidator(&g_data.m_combobox_choice));
211 flexgridsizer->Add(combobox);
212
213 mainsizer->Add(flexgridsizer, 1, wxGROW | wxALL, 10);
214
7df07b10 215 mainsizer->Add(new wxRadioBox((wxWindow*)this, VALIDATE_RADIO, wxT("Pick a color"),
2b61c41b
VZ
216 wxPoint(10, 100), wxSize(-1, -1),
217 3, g_radiobox_choices, 1, wxRA_SPECIFY_ROWS,
218 wxGenericValidator(&g_data.m_radiobox_choice)),
219 0, wxGROW | wxALL, 10);
220
221 wxGridSizer *gridsizer = new wxGridSizer(2, 2, 5, 5);
222
7df07b10 223 wxButton *ok_button = new wxButton(this, wxID_OK, wxT("OK"), wxPoint(250, 70), wxSize(80, 30));
2b61c41b
VZ
224 ok_button->SetDefault();
225 gridsizer->Add(ok_button);
7df07b10 226 gridsizer->Add(new wxButton(this, wxID_CANCEL, wxT("Cancel"), wxPoint(250, 100), wxSize(80, 30)));
2b61c41b
VZ
227
228 mainsizer->Add(gridsizer, 0, wxGROW | wxALL, 10);
229
230 SetSizer(mainsizer);
231 mainsizer->SetSizeHints(this);
232}
457814b5 233
2b61c41b
VZ
234bool MyDialog::TransferDataToWindow()
235{
236 bool r = wxDialog::TransferDataToWindow();
237 // These function calls have to be made here, after the
238 // dialog has been created.
239 text->SetFocus();
240 combobox->SetSelection(0);
241 return r;
457814b5
JS
242}
243