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