Added RTTI macros
[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 #if wxUSE_SPINCTRL && !defined(__WIN16__)
48 #include "wx/spinctrl.h"
49 #endif
50 #if wxUSE_SPINBTN && !defined(__WIN16__)
51 #include "wx/spinbutt.h"
52 #endif
53 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
54 #include "wx/checklst.h"
55 #endif
56
57 #include "wx/valgen.h"
58
59 IMPLEMENT_CLASS(wxGenericValidator, wxValidator)
60
61 wxGenericValidator::wxGenericValidator(bool *val)
62 {
63 Initialize();
64 m_pBool = val;
65 }
66
67 wxGenericValidator::wxGenericValidator(int *val)
68 {
69 Initialize();
70 m_pInt = val;
71 }
72
73 wxGenericValidator::wxGenericValidator(wxString *val)
74 {
75 Initialize();
76 m_pString = val;
77 }
78
79 wxGenericValidator::wxGenericValidator(wxArrayInt *val)
80 {
81 Initialize();
82 m_pArrayInt = val;
83 }
84
85 wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
86 : wxValidator()
87 {
88 Copy(val);
89 }
90
91 bool wxGenericValidator::Copy(const wxGenericValidator& val)
92 {
93 wxValidator::Copy(val);
94
95 m_pBool = val.m_pBool;
96 m_pInt = val.m_pInt;
97 m_pString = val.m_pString;
98 m_pArrayInt = val.m_pArrayInt;
99
100 return TRUE;
101 }
102
103 wxGenericValidator::~wxGenericValidator()
104 {
105 }
106
107 // Called to transfer data to the window
108 bool wxGenericValidator::TransferToWindow(void)
109 {
110 if ( !m_validatorWindow )
111 return FALSE;
112
113 // bool controls
114 #if wxUSE_CHECKBOX
115 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
116 {
117 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
118 if (m_pBool)
119 {
120 pControl->SetValue(*m_pBool);
121 return TRUE;
122 }
123 } else
124 #endif
125 #if wxUSE_RADIOBTN
126 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
127 {
128 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
129 if (m_pBool)
130 {
131 pControl->SetValue(*m_pBool) ;
132 return TRUE;
133 }
134 } else
135 #endif
136
137 // int controls
138 #if wxUSE_GAUGE
139 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
140 {
141 wxGauge* pControl = (wxGauge*) m_validatorWindow;
142 if (m_pInt)
143 {
144 pControl->SetValue(*m_pInt);
145 return TRUE;
146 }
147 } else
148 #endif
149 #if wxUSE_RADIOBOX
150 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
151 {
152 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
153 if (m_pInt)
154 {
155 pControl->SetSelection(*m_pInt) ;
156 return TRUE;
157 }
158 } else
159 #endif
160 #if wxUSE_SCROLLBAR
161 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
162 {
163 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
164 if (m_pInt)
165 {
166 pControl->SetThumbPosition(*m_pInt) ;
167 return TRUE;
168 }
169 } else
170 #endif
171 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
172 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
173 {
174 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
175 if (m_pInt)
176 {
177 pControl->SetValue(*m_pInt);
178 return TRUE;
179 }
180 } else
181 #endif
182 #if wxUSE_SPINBTN && !defined(__WIN16__)
183 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
184 {
185 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
186 if (m_pInt)
187 {
188 pControl->SetValue(*m_pInt) ;
189 return TRUE;
190 }
191 } else
192 #endif
193 #if wxUSE_SLIDER
194 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
195 {
196 wxSlider* pControl = (wxSlider*) m_validatorWindow;
197 if (m_pInt)
198 {
199 pControl->SetValue(*m_pInt) ;
200 return TRUE;
201 }
202 } else
203 #endif
204
205 // string controls
206 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
207 {
208 wxButton* pControl = (wxButton*) m_validatorWindow;
209 if (m_pString)
210 {
211 pControl->SetLabel(*m_pString) ;
212 return TRUE;
213 }
214 } else
215 #if wxUSE_COMBOBOX
216 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
217 {
218 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
219 if (m_pInt)
220 {
221 pControl->SetSelection(*m_pInt) ;
222 return TRUE;
223 }
224 else if (m_pString)
225 {
226 if (pControl->FindString(* m_pString) > -1)
227 {
228 pControl->SetStringSelection(* m_pString);
229 }
230 else
231 {
232 pControl->SetValue(* m_pString);
233 }
234 return TRUE;
235 }
236 } else
237 #endif
238 #if wxUSE_CHOICE
239 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
240 {
241 wxChoice* pControl = (wxChoice*) m_validatorWindow;
242 if (m_pInt)
243 {
244 pControl->SetSelection(*m_pInt) ;
245 return TRUE;
246 }
247 else if (m_pString)
248 {
249 if (pControl->FindString(* m_pString) > -1)
250 {
251 pControl->SetStringSelection(* m_pString);
252 }
253 return TRUE;
254 }
255 } else
256 #endif
257 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
258 {
259 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
260 if (m_pString)
261 {
262 pControl->SetLabel(*m_pString) ;
263 return TRUE;
264 }
265 } else
266 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
267 {
268 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
269 if (m_pString)
270 {
271 pControl->SetValue(*m_pString) ;
272 return TRUE;
273 }
274 else if (m_pInt)
275 {
276 wxString str;
277 str.Printf(wxT("%d"), *m_pInt);
278 pControl->SetValue(str);
279 return TRUE;
280 }
281 } else
282 // array controls
283 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
284 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
285 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
286 {
287 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
288 if (m_pArrayInt)
289 {
290 // clear all selections
291 size_t i,
292 count = pControl->GetCount();
293 for ( i = 0 ; i < count; i++ )
294 pControl->Check(i, FALSE);
295
296 // select each item in our array
297 count = m_pArrayInt->GetCount();
298 for ( i = 0 ; i < count; i++ )
299 pControl->Check(m_pArrayInt->Item(i));
300
301 return TRUE;
302 }
303 else
304 return FALSE;
305 } else
306 #endif
307 #if wxUSE_LISTBOX
308 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
309 {
310 wxListBox* pControl = (wxListBox*) m_validatorWindow;
311 if (m_pArrayInt)
312 {
313 // clear all selections
314 size_t i,
315 count = pControl->GetCount();
316 for ( i = 0 ; i < count; i++ )
317 pControl->Deselect(i);
318
319 // select each item in our array
320 count = m_pArrayInt->GetCount();
321 for ( i = 0 ; i < count; i++ )
322 pControl->SetSelection(m_pArrayInt->Item(i));
323
324 return TRUE;
325 }
326 } else
327 #endif
328 ; // to match the last 'else' above
329
330 // unrecognized control, or bad pointer
331 return FALSE;
332 }
333
334 // Called to transfer data from the window
335 bool wxGenericValidator::TransferFromWindow(void)
336 {
337 if ( !m_validatorWindow )
338 return FALSE;
339
340 // bool controls
341 #if wxUSE_CHECKBOX
342 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
343 {
344 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
345 if (m_pBool)
346 {
347 *m_pBool = pControl->GetValue() ;
348 return TRUE;
349 }
350 } else
351 #endif
352 #if wxUSE_RADIOBTN
353 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
354 {
355 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
356 if (m_pBool)
357 {
358 *m_pBool = pControl->GetValue() ;
359 return TRUE;
360 }
361 } else
362 #endif
363 // int controls
364 #if wxUSE_GAUGE
365 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
366 {
367 wxGauge* pControl = (wxGauge*) m_validatorWindow;
368 if (m_pInt)
369 {
370 *m_pInt = pControl->GetValue() ;
371 return TRUE;
372 }
373 } else
374 #endif
375 #if wxUSE_RADIOBOX
376 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
377 {
378 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
379 if (m_pInt)
380 {
381 *m_pInt = pControl->GetSelection() ;
382 return TRUE;
383 }
384 } else
385 #endif
386 #if wxUSE_SCROLLBAR
387 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
388 {
389 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
390 if (m_pInt)
391 {
392 *m_pInt = pControl->GetThumbPosition() ;
393 return TRUE;
394 }
395 } else
396 #endif
397 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
398 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
399 {
400 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
401 if (m_pInt)
402 {
403 *m_pInt=pControl->GetValue();
404 return TRUE;
405 }
406 } else
407 #endif
408 #if wxUSE_SPINBTN && !defined(__WIN16__)
409 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
410 {
411 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
412 if (m_pInt)
413 {
414 *m_pInt = pControl->GetValue() ;
415 return TRUE;
416 }
417 } else
418 #endif
419 #if wxUSE_SLIDER
420 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
421 {
422 wxSlider* pControl = (wxSlider*) m_validatorWindow;
423 if (m_pInt)
424 {
425 *m_pInt = pControl->GetValue() ;
426 return TRUE;
427 }
428 } else
429 #endif
430 // string controls
431 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
432 {
433 wxButton* pControl = (wxButton*) m_validatorWindow;
434 if (m_pString)
435 {
436 *m_pString = pControl->GetLabel() ;
437 return TRUE;
438 }
439 }
440 else
441 #if wxUSE_COMBOBOX
442 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
443 {
444 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
445 if (m_pInt)
446 {
447 *m_pInt = pControl->GetSelection() ;
448 return TRUE;
449 }
450 else if (m_pString)
451 {
452 *m_pString = pControl->GetValue();
453 return TRUE;
454 }
455 } else
456 #endif
457 #if wxUSE_CHOICE
458 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
459 {
460 wxChoice* pControl = (wxChoice*) m_validatorWindow;
461 if (m_pInt)
462 {
463 *m_pInt = pControl->GetSelection() ;
464 return TRUE;
465 }
466 else if (m_pString)
467 {
468 *m_pString = pControl->GetStringSelection();
469 return TRUE;
470 }
471 } else
472 #endif
473 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
474 {
475 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
476 if (m_pString)
477 {
478 *m_pString = pControl->GetLabel() ;
479 return TRUE;
480 }
481 } else
482 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
483 {
484 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
485 if (m_pString)
486 {
487 *m_pString = pControl->GetValue() ;
488 return TRUE;
489 }
490 else if (m_pInt)
491 {
492 *m_pInt = wxAtoi(pControl->GetValue());
493 return TRUE;
494 }
495 } else
496 // array controls
497 #if wxUSE_CHECKLISTBOX
498 #ifndef __WIN16__
499 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
500 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
501 {
502 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
503 if (m_pArrayInt)
504 {
505 // clear our array
506 m_pArrayInt->Clear();
507
508 // add each selected item to our array
509 size_t i,
510 count = pControl->GetCount();
511 for ( i = 0; i < count; i++ )
512 {
513 if (pControl->IsChecked(i))
514 m_pArrayInt->Add(i);
515 }
516
517 return TRUE;
518 }
519 else
520 return FALSE;
521 } else
522 #endif
523 #endif
524 #if wxUSE_LISTBOX
525 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
526 {
527 wxListBox* pControl = (wxListBox*) m_validatorWindow;
528 if (m_pArrayInt)
529 {
530 // clear our array
531 m_pArrayInt->Clear();
532
533 // add each selected item to our array
534 size_t i,
535 count = pControl->GetCount();
536 for ( i = 0; i < count; i++ )
537 {
538 if (pControl->Selected(i))
539 m_pArrayInt->Add(i);
540 }
541
542 return TRUE;
543 }
544 } else
545 #endif
546
547 // unrecognized control, or bad pointer
548 return FALSE;
549 return FALSE;
550 }
551
552 /*
553 Called by constructors to initialize ALL data members
554 */
555 void wxGenericValidator::Initialize()
556 {
557 m_pBool = 0;
558 m_pInt = 0;
559 m_pString = 0;
560 m_pArrayInt = 0;
561 }
562
563 #endif
564 // wxUSE_VALIDATORS
565