Added wxGenericValidator & doc
[wxWidgets.git] / src / common / valgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: valgen.cpp
3 // Purpose: wxGenericValidator class
4 // Author: Kevin Smith
5 // Modified by:
6 // Created: Jan 22 1999
7 // RCS-ID:
8 // Copyright: (c) 1999 Kevin Smith
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "valgen.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/utils.h"
25 #include "wx/intl.h"
26 #endif
27
28 #include "wx/spinbutt.h"
29 #include "wx/checklst.h"
30
31 #include "wx/valgen.h"
32
33 wxGenericValidator::wxGenericValidator(bool *val)
34 {
35 Initialize();
36 m_pBool = val;
37 }
38
39 wxGenericValidator::wxGenericValidator(int *val)
40 {
41 Initialize();
42 m_pInt = val;
43 }
44
45 wxGenericValidator::wxGenericValidator(wxString *val)
46 {
47 Initialize();
48 m_pString = val;
49 }
50
51 wxGenericValidator::wxGenericValidator(wxArrayInt *val)
52 {
53 Initialize();
54 m_pArrayInt = val;
55 }
56
57 wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
58 {
59 Copy(val);
60 }
61
62 bool wxGenericValidator::Copy(const wxGenericValidator& val)
63 {
64 wxValidator::Copy(val);
65
66 m_pBool = val.m_pBool;
67 m_pInt = val.m_pInt;
68 m_pString = val.m_pString;
69 m_pArrayInt = val.m_pArrayInt;
70
71 return TRUE;
72 }
73
74 wxGenericValidator::~wxGenericValidator()
75 {
76 }
77
78 // Called to transfer data to the window
79 bool wxGenericValidator::TransferToWindow(void)
80 {
81 if ( !m_validatorWindow )
82 return FALSE;
83
84 // bool controls
85 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
86 {
87 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
88 if (m_pBool)
89 {
90 pControl->SetValue(*m_pBool) ;
91 return TRUE;
92 }
93 }
94 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
95 {
96 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
97 if (m_pBool)
98 {
99 pControl->SetValue(*m_pBool) ;
100 return TRUE;
101 }
102 }
103 // int controls
104 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
105 {
106 wxGauge* pControl = (wxGauge*) m_validatorWindow;
107 if (m_pInt)
108 {
109 pControl->SetValue(*m_pInt) ;
110 return TRUE;
111 }
112 }
113 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
114 {
115 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
116 if (m_pInt)
117 {
118 pControl->SetSelection(*m_pInt) ;
119 return TRUE;
120 }
121 }
122 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
123 {
124 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
125 if (m_pInt)
126 {
127 pControl->SetThumbPosition(*m_pInt) ;
128 return TRUE;
129 }
130 }
131 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
132 {
133 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
134 if (m_pInt)
135 {
136 pControl->SetValue(*m_pInt) ;
137 return TRUE;
138 }
139 }
140 // string controls
141 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
142 {
143 wxButton* pControl = (wxButton*) m_validatorWindow;
144 if (m_pString)
145 {
146 pControl->SetLabel(*m_pString) ;
147 return TRUE;
148 }
149 }
150 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
151 {
152 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
153 if (m_pString)
154 {
155 pControl->SetValue(*m_pString) ;
156 return TRUE;
157 }
158 }
159 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
160 {
161 wxChoice* pControl = (wxChoice*) m_validatorWindow;
162 if (m_pInt)
163 {
164 pControl->SetSelection(*m_pInt) ;
165 return TRUE;
166 }
167 }
168 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
169 {
170 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
171 if (m_pString)
172 {
173 pControl->SetLabel(*m_pString) ;
174 return TRUE;
175 }
176 }
177 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
178 {
179 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
180 if (m_pString)
181 {
182 pControl->SetValue(*m_pString) ;
183 return TRUE;
184 }
185 }
186 // array controls
187 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
188 // MUST come first:
189 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
190 {
191 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
192 if (m_pArrayInt)
193 {
194 // clear all selections
195 int i;
196 for (i = 0 ; i < pControl->Number(); ++i)
197 pControl->Check(i, FALSE);
198 // select each item in our array
199 unsigned u;
200 for (u = 0; u < m_pArrayInt->Count(); ++u)
201 pControl->Check(m_pArrayInt->Item(u));
202 return TRUE;
203 }
204 else
205 return FALSE;
206 }
207 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
208 {
209 wxListBox* pControl = (wxListBox*) m_validatorWindow;
210 if (m_pArrayInt)
211 {
212 // clear all selections
213 int i;
214 for (i = 0 ; i < pControl->Number(); ++i)
215 pControl->Deselect(i);
216 // select each item in our array
217 unsigned u;
218 for (u = 0; u < m_pArrayInt->Count(); ++u)
219 pControl->SetSelection(m_pArrayInt->Item(u));
220 return TRUE;
221 }
222 }
223
224 // unrecognized control, or bad pointer
225 return FALSE;
226 }
227
228 // Called to transfer data to the window
229 bool wxGenericValidator::TransferFromWindow(void)
230 {
231 if ( !m_validatorWindow )
232 return FALSE;
233
234 // bool controls
235 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
236 {
237 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
238 if (m_pBool)
239 {
240 *m_pBool = pControl->GetValue() ;
241 return TRUE;
242 }
243 }
244 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
245 {
246 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
247 if (m_pBool)
248 {
249 *m_pBool = pControl->GetValue() ;
250 return TRUE;
251 }
252 }
253 // int controls
254 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
255 {
256 wxGauge* pControl = (wxGauge*) m_validatorWindow;
257 if (m_pInt)
258 {
259 *m_pInt = pControl->GetValue() ;
260 return TRUE;
261 }
262 }
263 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
264 {
265 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
266 if (m_pInt)
267 {
268 *m_pInt = pControl->GetSelection() ;
269 return TRUE;
270 }
271 }
272 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
273 {
274 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
275 if (m_pInt)
276 {
277 *m_pInt = pControl->GetThumbPosition() ;
278 return TRUE;
279 }
280 }
281 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
282 {
283 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
284 if (m_pInt)
285 {
286 *m_pInt = pControl->GetValue() ;
287 return TRUE;
288 }
289 }
290 // string controls
291 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
292 {
293 wxButton* pControl = (wxButton*) m_validatorWindow;
294 if (m_pString)
295 {
296 *m_pString = pControl->GetLabel() ;
297 return TRUE;
298 }
299 }
300 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
301 {
302 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
303 if (m_pString)
304 {
305 *m_pString = pControl->GetValue() ;
306 return TRUE;
307 }
308 }
309 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
310 {
311 wxChoice* pControl = (wxChoice*) m_validatorWindow;
312 if (m_pInt)
313 {
314 *m_pInt = pControl->GetSelection() ;
315 return TRUE;
316 }
317 }
318 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
319 {
320 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
321 if (m_pString)
322 {
323 *m_pString = pControl->GetLabel() ;
324 return TRUE;
325 }
326 }
327 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
328 {
329 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
330 if (m_pString)
331 {
332 *m_pString = pControl->GetValue() ;
333 return TRUE;
334 }
335 }
336 // array controls
337 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
338 // MUST come first:
339 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
340 {
341 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
342 if (m_pArrayInt)
343 {
344 // clear our array
345 m_pArrayInt->Clear();
346 // add each selected item to our array
347 int i;
348 for (i = 0 ; i < pControl->Number(); ++i)
349 if (pControl->IsChecked(i))
350 m_pArrayInt->Add(i);
351 return TRUE;
352 }
353 else
354 return FALSE;
355 }
356 else if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
357 {
358 wxListBox* pControl = (wxListBox*) m_validatorWindow;
359 if (m_pArrayInt)
360 {
361 // clear our array
362 m_pArrayInt->Clear();
363 // add each selected item to our array
364 int i;
365 for (i = 0 ; i < pControl->Number(); ++i)
366 if (pControl->Selected(i))
367 m_pArrayInt->Add(i);
368 return TRUE;
369 }
370 }
371
372 // unrecognized control, or bad pointer
373 return FALSE;
374 }
375
376 /*
377 Called by constructors to initialize ALL data members
378 */
379 void wxGenericValidator::Initialize()
380 {
381 m_pBool = 0;
382 m_pInt = 0;
383 m_pString = 0;
384 m_pArrayInt = 0;
385 }
386