Distrib changes.
[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/defs.h"
25 #endif
26
27 #if wxUSE_VALIDATORS
28
29 #ifndef WX_PRECOMP
30 #include "wx/utils.h"
31 #include "wx/intl.h"
32 #include "wx/dynarray.h"
33 #include "wx/choice.h"
34 #include "wx/combobox.h"
35 #include "wx/radiobox.h"
36 #include "wx/radiobut.h"
37 #include "wx/checkbox.h"
38 #include "wx/scrolbar.h"
39 #include "wx/gauge.h"
40 #include "wx/stattext.h"
41 #include "wx/textctrl.h"
42 #include "wx/button.h"
43 #include "wx/listbox.h"
44 #include "wx/slider.h"
45 #endif
46
47 #ifndef __WIN16__
48 #include "wx/spinbutt.h"
49 #include "wx/checklst.h"
50 #endif
51
52 #include "wx/valgen.h"
53
54 wxGenericValidator::wxGenericValidator(bool *val)
55 {
56 Initialize();
57 m_pBool = val;
58 }
59
60 wxGenericValidator::wxGenericValidator(int *val)
61 {
62 Initialize();
63 m_pInt = val;
64 }
65
66 wxGenericValidator::wxGenericValidator(wxString *val)
67 {
68 Initialize();
69 m_pString = val;
70 }
71
72 wxGenericValidator::wxGenericValidator(wxArrayInt *val)
73 {
74 Initialize();
75 m_pArrayInt = val;
76 }
77
78 wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
79 {
80 Copy(val);
81 }
82
83 bool wxGenericValidator::Copy(const wxGenericValidator& val)
84 {
85 wxValidator::Copy(val);
86
87 m_pBool = val.m_pBool;
88 m_pInt = val.m_pInt;
89 m_pString = val.m_pString;
90 m_pArrayInt = val.m_pArrayInt;
91
92 return TRUE;
93 }
94
95 wxGenericValidator::~wxGenericValidator()
96 {
97 }
98
99 // Called to transfer data to the window
100 bool wxGenericValidator::TransferToWindow(void)
101 {
102 if ( !m_validatorWindow )
103 return FALSE;
104
105 // bool controls
106 #if wxUSE_CHECKBOX
107 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
108 {
109 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
110 if (m_pBool)
111 {
112 pControl->SetValue(*m_pBool);
113 return TRUE;
114 }
115 } else
116 #endif
117 #if wxUSE_RADIOBTN
118 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
119 {
120 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
121 if (m_pBool)
122 {
123 pControl->SetValue(*m_pBool) ;
124 return TRUE;
125 }
126 } else
127 #endif
128
129 // int controls
130 #if wxUSE_GAUGE
131 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
132 {
133 wxGauge* pControl = (wxGauge*) m_validatorWindow;
134 if (m_pInt)
135 {
136 pControl->SetValue(*m_pInt);
137 return TRUE;
138 }
139 } else
140 #endif
141 #if wxUSE_RADIOBOX
142 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
143 {
144 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
145 if (m_pInt)
146 {
147 pControl->SetSelection(*m_pInt) ;
148 return TRUE;
149 }
150 } else
151 #endif
152 #if wxUSE_SCROLLBAR
153 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
154 {
155 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
156 if (m_pInt)
157 {
158 pControl->SetThumbPosition(*m_pInt) ;
159 return TRUE;
160 }
161 } else
162 #endif
163 #if wxUSE_SPINBTN
164 #ifndef __WIN16__
165 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
166 {
167 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
168 if (m_pInt)
169 {
170 pControl->SetValue(*m_pInt) ;
171 return TRUE;
172 }
173 } else
174 #endif
175 #endif
176 #if wxUSE_SLIDER
177 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
178 {
179 wxSlider* pControl = (wxSlider*) m_validatorWindow;
180 if (m_pInt)
181 {
182 pControl->SetValue(*m_pInt) ;
183 return TRUE;
184 }
185 } else
186 #endif
187
188 // string controls
189 #if 1
190 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
191 {
192 wxButton* pControl = (wxButton*) m_validatorWindow;
193 if (m_pString)
194 {
195 pControl->SetLabel(*m_pString) ;
196 return TRUE;
197 }
198 } else
199 #endif
200 #if wxUSE_COMBOBOX
201 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
202 {
203 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
204 if (m_pString)
205 {
206 pControl->SetValue(*m_pString) ;
207 return TRUE;
208 }
209 } else
210 #endif
211 #if wxUSE_CHOICE
212 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
213 {
214 wxChoice* pControl = (wxChoice*) m_validatorWindow;
215 if (m_pInt)
216 {
217 pControl->SetSelection(*m_pInt) ;
218 return TRUE;
219 }
220 } else
221 #endif
222 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
223 {
224 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
225 if (m_pString)
226 {
227 pControl->SetLabel(*m_pString) ;
228 return TRUE;
229 }
230 } else
231 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
232 {
233 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
234 if (m_pString)
235 {
236 pControl->SetValue(*m_pString) ;
237 return TRUE;
238 }
239 } else
240 #if wxUSE_CHECKLISTBOX
241 #ifndef __WIN16__
242 // array controls
243 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
244 // MUST come first:
245 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
246 {
247 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
248 if (m_pArrayInt)
249 {
250 // clear all selections
251 int i;
252 for (i = 0 ; i < pControl->Number(); ++i)
253 pControl->Check(i, FALSE);
254 // select each item in our array
255 unsigned u;
256 for (u = 0; u < m_pArrayInt->Count(); ++u)
257 pControl->Check(m_pArrayInt->Item(u));
258 return TRUE;
259 }
260 else
261 return FALSE;
262 } else
263 #endif
264 #endif
265 #if wxUSE_LISTBOX
266 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
267 {
268 wxListBox* pControl = (wxListBox*) m_validatorWindow;
269 if (m_pArrayInt)
270 {
271 // clear all selections
272 int i;
273 for (i = 0 ; i < pControl->Number(); ++i)
274 pControl->Deselect(i);
275 // select each item in our array
276 unsigned u;
277 for (u = 0; u < m_pArrayInt->Count(); ++u)
278 pControl->SetSelection(m_pArrayInt->Item(u));
279 return TRUE;
280 }
281 } else
282 #endif
283
284 // unrecognized control, or bad pointer
285 return FALSE;
286 return FALSE;
287 }
288
289 // Called to transfer data from the window
290 bool wxGenericValidator::TransferFromWindow(void)
291 {
292 if ( !m_validatorWindow )
293 return FALSE;
294
295 // bool controls
296 #if wxUSE_CHECKBOX
297 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
298 {
299 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
300 if (m_pBool)
301 {
302 *m_pBool = pControl->GetValue() ;
303 return TRUE;
304 }
305 } else
306 #endif
307 #if wxUSE_RADIOBTN
308 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
309 {
310 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
311 if (m_pBool)
312 {
313 *m_pBool = pControl->GetValue() ;
314 return TRUE;
315 }
316 } else
317 #endif
318 // int controls
319 #if wxUSE_GAUGE
320 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
321 {
322 wxGauge* pControl = (wxGauge*) m_validatorWindow;
323 if (m_pInt)
324 {
325 *m_pInt = pControl->GetValue() ;
326 return TRUE;
327 }
328 } else
329 #endif
330 #if wxUSE_RADIOBOX
331 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
332 {
333 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
334 if (m_pInt)
335 {
336 *m_pInt = pControl->GetSelection() ;
337 return TRUE;
338 }
339 } else
340 #endif
341 #if wxUSE_SCROLLBAR
342 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
343 {
344 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
345 if (m_pInt)
346 {
347 *m_pInt = pControl->GetThumbPosition() ;
348 return TRUE;
349 }
350 } else
351 #endif
352 #if wxUSE_SPINBTN
353 #ifndef __WIN16__
354 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
355 {
356 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
357 if (m_pInt)
358 {
359 *m_pInt = pControl->GetValue() ;
360 return TRUE;
361 }
362 } else
363 #endif
364 #endif
365 #if wxUSE_SLIDER
366 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
367 {
368 wxSlider* pControl = (wxSlider*) m_validatorWindow;
369 if (m_pInt)
370 {
371 pControl->SetValue(*m_pInt) ;
372 return TRUE;
373 }
374 } else
375 #endif
376 // string controls
377 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
378 {
379 wxButton* pControl = (wxButton*) m_validatorWindow;
380 if (m_pString)
381 {
382 *m_pString = pControl->GetLabel() ;
383 return TRUE;
384 }
385 }
386 else
387 #if wxUSE_COMBOBOX
388 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
389 {
390 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
391 if (m_pString)
392 {
393 *m_pString = pControl->GetValue() ;
394 return TRUE;
395 }
396 } else
397 #endif
398 #if wxUSE_CHOICE
399 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
400 {
401 wxChoice* pControl = (wxChoice*) m_validatorWindow;
402 if (m_pInt)
403 {
404 *m_pInt = pControl->GetSelection() ;
405 return TRUE;
406 }
407 } else
408 #endif
409 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
410 {
411 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
412 if (m_pString)
413 {
414 *m_pString = pControl->GetLabel() ;
415 return TRUE;
416 }
417 } else
418 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
419 {
420 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
421 if (m_pString)
422 {
423 *m_pString = pControl->GetValue() ;
424 return TRUE;
425 }
426 } else
427 #if wxUSE_CHECKLISTBOX
428 #ifndef __WIN16__
429 // array controls
430 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
431 // MUST come first:
432 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
433 {
434 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
435 if (m_pArrayInt)
436 {
437 // clear our array
438 m_pArrayInt->Clear();
439 // add each selected item to our array
440 int i;
441 for (i = 0 ; i < pControl->Number(); ++i)
442 if (pControl->IsChecked(i))
443 m_pArrayInt->Add(i);
444 return TRUE;
445 }
446 else
447 return FALSE;
448 } else
449 #endif
450 #endif
451 #if wxUSE_LISTBOX
452 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
453 {
454 wxListBox* pControl = (wxListBox*) m_validatorWindow;
455 if (m_pArrayInt)
456 {
457 // clear our array
458 m_pArrayInt->Clear();
459 // add each selected item to our array
460 int i;
461 for (i = 0 ; i < pControl->Number(); ++i)
462 if (pControl->Selected(i))
463 m_pArrayInt->Add(i);
464 return TRUE;
465 }
466 } else
467 #endif
468
469 // unrecognized control, or bad pointer
470 return FALSE;
471 return FALSE;
472 }
473
474 /*
475 Called by constructors to initialize ALL data members
476 */
477 void wxGenericValidator::Initialize()
478 {
479 m_pBool = 0;
480 m_pInt = 0;
481 m_pString = 0;
482 m_pArrayInt = 0;
483 }
484
485 #endif
486 // wxUSE_VALIDATORS
487