Support float, double and file name values in wxGenericValidator.
[wxWidgets.git] / src / common / valgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valgen.cpp
3 // Purpose: wxGenericValidator class
4 // Author: Kevin Smith
5 // Modified by:
6 // Created: Jan 22 1999
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Kevin Smith
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_VALIDATORS
20
21 #ifndef WX_PRECOMP
22 #include "wx/dynarray.h"
23 #include "wx/utils.h"
24 #include "wx/intl.h"
25 #include "wx/choice.h"
26 #include "wx/combobox.h"
27 #include "wx/radiobox.h"
28 #include "wx/radiobut.h"
29 #include "wx/checkbox.h"
30 #include "wx/scrolbar.h"
31 #include "wx/gauge.h"
32 #include "wx/stattext.h"
33 #include "wx/textctrl.h"
34 #include "wx/button.h"
35 #include "wx/listbox.h"
36 #include "wx/slider.h"
37 #include "wx/checklst.h"
38 #endif
39
40 #include "wx/spinctrl.h"
41 // #include "wx/datectrl.h" -- can't use it in this (core) file for now
42
43 #if wxUSE_SPINBTN
44 #include "wx/spinbutt.h"
45 #endif
46 #if wxUSE_TOGGLEBTN
47 #include "wx/tglbtn.h"
48 #endif
49 #include "wx/filename.h"
50
51 #include "wx/valgen.h"
52
53 IMPLEMENT_CLASS(wxGenericValidator, wxValidator)
54
55 wxGenericValidator::wxGenericValidator(bool *val)
56 {
57 Initialize();
58 m_pBool = val;
59 }
60
61 wxGenericValidator::wxGenericValidator(int *val)
62 {
63 Initialize();
64 m_pInt = val;
65 }
66
67 wxGenericValidator::wxGenericValidator(wxString *val)
68 {
69 Initialize();
70 m_pString = val;
71 }
72
73 wxGenericValidator::wxGenericValidator(wxArrayInt *val)
74 {
75 Initialize();
76 m_pArrayInt = val;
77 }
78
79 #if wxUSE_DATETIME
80
81 wxGenericValidator::wxGenericValidator(wxDateTime *val)
82 {
83 Initialize();
84 m_pDateTime = val;
85 }
86
87 wxGenericValidator::wxGenericValidator(wxFileName *val)
88 {
89 Initialize();
90 m_pFileName = val;
91 }
92
93 wxGenericValidator::wxGenericValidator(float *val)
94 {
95 Initialize();
96 m_pFloat = val;
97 }
98
99 wxGenericValidator::wxGenericValidator(double *val)
100 {
101 Initialize();
102 m_pDouble = val;
103 }
104
105 #endif // wxUSE_DATETIME
106
107 wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
108 : wxValidator()
109 {
110 Copy(val);
111 }
112
113 bool wxGenericValidator::Copy(const wxGenericValidator& val)
114 {
115 wxValidator::Copy(val);
116
117 m_pBool = val.m_pBool;
118 m_pInt = val.m_pInt;
119 m_pString = val.m_pString;
120 m_pArrayInt = val.m_pArrayInt;
121 #if wxUSE_DATETIME
122 m_pDateTime = val.m_pDateTime;
123 #endif // wxUSE_DATETIME
124
125 return true;
126 }
127
128 // Called to transfer data to the window
129 bool wxGenericValidator::TransferToWindow(void)
130 {
131 if ( !m_validatorWindow )
132 return false;
133
134 // bool controls
135 #if wxUSE_CHECKBOX
136 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
137 {
138 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
139 if (m_pBool)
140 {
141 pControl->SetValue(*m_pBool);
142 return true;
143 }
144 } else
145 #endif
146 #if wxUSE_RADIOBTN
147 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
148 {
149 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
150 if (m_pBool)
151 {
152 pControl->SetValue(*m_pBool) ;
153 return true;
154 }
155 } else
156 #endif
157
158 #if wxUSE_TOGGLEBTN
159 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
160 {
161 wxToggleButton * pControl = (wxToggleButton *) m_validatorWindow;
162 if (m_pBool)
163 {
164 pControl->SetValue(*m_pBool);
165 return true;
166 }
167 } else
168 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
169 if (m_validatorWindow->IsKindOf(CLASSINFO(wxBitmapToggleButton)) )
170 {
171 wxBitmapToggleButton * pControl = (wxBitmapToggleButton *) m_validatorWindow;
172 if (m_pBool)
173 {
174 pControl->SetValue(*m_pBool);
175 return true;
176 }
177 } else
178 #endif
179 #endif
180
181 // int controls
182 #if wxUSE_GAUGE
183 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
184 {
185 wxGauge* pControl = (wxGauge*) m_validatorWindow;
186 if (m_pInt)
187 {
188 pControl->SetValue(*m_pInt);
189 return true;
190 }
191 } else
192 #endif
193 #if wxUSE_RADIOBOX
194 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
195 {
196 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
197 if (m_pInt)
198 {
199 pControl->SetSelection(*m_pInt) ;
200 return true;
201 }
202 } else
203 #endif
204 #if wxUSE_SCROLLBAR
205 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
206 {
207 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
208 if (m_pInt)
209 {
210 pControl->SetThumbPosition(*m_pInt) ;
211 return true;
212 }
213 } else
214 #endif
215 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
216 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
217 {
218 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
219 if (m_pInt)
220 {
221 pControl->SetValue(*m_pInt);
222 return true;
223 }
224 } else
225 #endif
226 #if wxUSE_SPINBTN
227 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
228 {
229 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
230 if (m_pInt)
231 {
232 pControl->SetValue(*m_pInt) ;
233 return true;
234 }
235 } else
236 #endif
237 #if wxUSE_SLIDER
238 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
239 {
240 wxSlider* pControl = (wxSlider*) m_validatorWindow;
241 if (m_pInt)
242 {
243 pControl->SetValue(*m_pInt) ;
244 return true;
245 }
246 } else
247 #endif
248
249 // date time controls
250 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
251 if (m_validatorWindow->IsKindOf(CLASSINFO(wxDatePickerCtrl)) )
252 {
253 wxDatePickerCtrl* pControl = (wxDatePickerCtrl*) m_validatorWindow;
254 if (m_pDateTime)
255 {
256 pControl->SetValue(*m_pDateTime) ;
257 return true;
258 }
259 } else
260 #endif
261
262 // string controls
263 #if wxUSE_BUTTON
264 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
265 {
266 wxButton* pControl = (wxButton*) m_validatorWindow;
267 if (m_pString)
268 {
269 pControl->SetLabel(*m_pString) ;
270 return true;
271 }
272 } else
273 #endif
274 #if wxUSE_COMBOBOX
275 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
276 {
277 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
278 if (m_pInt)
279 {
280 pControl->SetSelection(*m_pInt) ;
281 return true;
282 }
283 else if (m_pString)
284 {
285 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
286 {
287 pControl->SetStringSelection(* m_pString);
288 }
289 if ((m_validatorWindow->GetWindowStyle() & wxCB_READONLY) == 0)
290 {
291 pControl->SetValue(* m_pString);
292 }
293 return true;
294 }
295 } else
296 #endif
297 #if wxUSE_CHOICE
298 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
299 {
300 wxChoice* pControl = (wxChoice*) m_validatorWindow;
301 if (m_pInt)
302 {
303 pControl->SetSelection(*m_pInt) ;
304 return true;
305 }
306 else if (m_pString)
307 {
308 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
309 {
310 pControl->SetStringSelection(* m_pString);
311 }
312 return true;
313 }
314 } else
315 #endif
316 #if wxUSE_STATTEXT
317 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
318 {
319 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
320 if (m_pString)
321 {
322 pControl->SetLabel(*m_pString) ;
323 return true;
324 }
325 } else
326 #endif
327 #if wxUSE_TEXTCTRL
328 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
329 {
330 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
331 if (m_pString)
332 {
333 pControl->SetValue(*m_pString) ;
334 return true;
335 }
336 else if (m_pInt)
337 {
338 wxString str;
339 str.Printf(wxT("%d"), *m_pInt);
340 pControl->SetValue(str);
341 return true;
342 }
343 else if (m_pFileName)
344 {
345 pControl->SetValue(m_pFileName->GetFullPath());
346 return true;
347 }
348 else if (m_pFloat)
349 {
350 pControl->SetValue(wxString::Format(wxT("%g"), *m_pFloat));
351 return true;
352 }
353 else if (m_pDouble)
354 {
355 pControl->SetValue(wxString::Format(wxT("%g"), *m_pDouble));
356 return true;
357 }
358 } else
359 #endif
360
361 // array controls
362 #if wxUSE_CHECKLISTBOX
363 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
364 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
365 {
366 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
367 if (m_pArrayInt)
368 {
369 // clear all selections
370 size_t i,
371 count = pControl->GetCount();
372 for ( i = 0 ; i < count; i++ )
373 pControl->Check(i, false);
374
375 // select each item in our array
376 count = m_pArrayInt->GetCount();
377 for ( i = 0 ; i < count; i++ )
378 pControl->Check(m_pArrayInt->Item(i));
379
380 return true;
381 }
382 else
383 return false;
384 } else
385 #endif
386 #if wxUSE_LISTBOX
387 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
388 {
389 wxListBox* pControl = (wxListBox*) m_validatorWindow;
390 if (m_pArrayInt)
391 {
392 // clear all selections
393 size_t i,
394 count = pControl->GetCount();
395 for ( i = 0 ; i < count; i++ )
396 pControl->Deselect(i);
397
398 // select each item in our array
399 count = m_pArrayInt->GetCount();
400 for ( i = 0 ; i < count; i++ )
401 pControl->SetSelection(m_pArrayInt->Item(i));
402
403 return true;
404 }
405 } else
406 #endif
407 { // to match the last 'else' above
408 }
409
410 // unrecognized control, or bad pointer
411 return false;
412 }
413
414 // Called to transfer data from the window
415 bool wxGenericValidator::TransferFromWindow(void)
416 {
417 if ( !m_validatorWindow )
418 return false;
419
420 // BOOL CONTROLS **************************************
421 #if wxUSE_CHECKBOX
422 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
423 {
424 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
425 if (m_pBool)
426 {
427 *m_pBool = pControl->GetValue() ;
428 return true;
429 }
430 } else
431 #endif
432 #if wxUSE_RADIOBTN
433 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
434 {
435 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
436 if (m_pBool)
437 {
438 *m_pBool = pControl->GetValue() ;
439 return true;
440 }
441 } else
442 #endif
443 #if wxUSE_TOGGLEBTN
444 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
445 {
446 wxToggleButton *pControl = (wxToggleButton *) m_validatorWindow;
447 if (m_pBool)
448 {
449 *m_pBool = pControl->GetValue() ;
450 return true;
451 }
452 } else
453 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
454 if (m_validatorWindow->IsKindOf(CLASSINFO(wxBitmapToggleButton)) )
455 {
456 wxBitmapToggleButton *pControl = (wxBitmapToggleButton *) m_validatorWindow;
457 if (m_pBool)
458 {
459 *m_pBool = pControl->GetValue() ;
460 return true;
461 }
462 } else
463 #endif
464 #endif
465
466 // INT CONTROLS ***************************************
467 #if wxUSE_GAUGE
468 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
469 {
470 wxGauge* pControl = (wxGauge*) m_validatorWindow;
471 if (m_pInt)
472 {
473 *m_pInt = pControl->GetValue() ;
474 return true;
475 }
476 } else
477 #endif
478 #if wxUSE_RADIOBOX
479 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
480 {
481 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
482 if (m_pInt)
483 {
484 *m_pInt = pControl->GetSelection() ;
485 return true;
486 }
487 } else
488 #endif
489 #if wxUSE_SCROLLBAR
490 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
491 {
492 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
493 if (m_pInt)
494 {
495 *m_pInt = pControl->GetThumbPosition() ;
496 return true;
497 }
498 } else
499 #endif
500 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
501 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
502 {
503 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
504 if (m_pInt)
505 {
506 *m_pInt=pControl->GetValue();
507 return true;
508 }
509 } else
510 #endif
511 #if wxUSE_SPINBTN
512 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
513 {
514 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
515 if (m_pInt)
516 {
517 *m_pInt = pControl->GetValue() ;
518 return true;
519 }
520 } else
521 #endif
522 #if wxUSE_SLIDER
523 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
524 {
525 wxSlider* pControl = (wxSlider*) m_validatorWindow;
526 if (m_pInt)
527 {
528 *m_pInt = pControl->GetValue() ;
529 return true;
530 }
531 } else
532 #endif
533
534 // DATE TIME CONTROLS ************************************
535 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
536 if (m_validatorWindow->IsKindOf(CLASSINFO(wxDatePickerCtrl)) )
537 {
538 wxDatePickerCtrl* pControl = (wxDatePickerCtrl*) m_validatorWindow;
539 if (m_pDateTime)
540 {
541 *m_pDateTime = pControl->GetValue() ;
542 return true;
543 }
544 } else
545 #endif
546
547 // STRING CONTROLS ************************************
548 #if wxUSE_BUTTON
549 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
550 {
551 wxButton* pControl = (wxButton*) m_validatorWindow;
552 if (m_pString)
553 {
554 *m_pString = pControl->GetLabel() ;
555 return true;
556 }
557 } else
558 #endif
559 #if wxUSE_COMBOBOX
560 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
561 {
562 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
563 if (m_pInt)
564 {
565 *m_pInt = pControl->GetSelection() ;
566 return true;
567 }
568 else if (m_pString)
569 {
570 if (m_validatorWindow->GetWindowStyle() & wxCB_READONLY)
571 *m_pString = pControl->GetStringSelection();
572 else
573 *m_pString = pControl->GetValue();
574 return true;
575 }
576 } else
577 #endif
578 #if wxUSE_CHOICE
579 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
580 {
581 wxChoice* pControl = (wxChoice*) m_validatorWindow;
582 if (m_pInt)
583 {
584 *m_pInt = pControl->GetSelection() ;
585 return true;
586 }
587 else if (m_pString)
588 {
589 *m_pString = pControl->GetStringSelection();
590 return true;
591 }
592 } else
593 #endif
594 #if wxUSE_STATTEXT
595 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
596 {
597 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
598 if (m_pString)
599 {
600 *m_pString = pControl->GetLabel() ;
601 return true;
602 }
603 } else
604 #endif
605 #if wxUSE_TEXTCTRL
606 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
607 {
608 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
609 if (m_pString)
610 {
611 *m_pString = pControl->GetValue() ;
612 return true;
613 }
614 else if (m_pInt)
615 {
616 *m_pInt = wxAtoi(pControl->GetValue());
617 return true;
618 }
619 else if (m_pFileName)
620 {
621 m_pFileName->Assign(pControl->GetValue());
622 return true;
623 }
624 else if (m_pFloat)
625 {
626 *m_pFloat = (float)wxAtof(pControl->GetValue());
627 return true;
628 }
629 else if (m_pDouble)
630 {
631 *m_pDouble = wxAtof(pControl->GetValue());
632 return true;
633 }
634 } else
635 #endif
636
637 // ARRAY CONTROLS *************************************
638 #if wxUSE_CHECKLISTBOX
639 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
640 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
641 {
642 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
643 if (m_pArrayInt)
644 {
645 // clear our array
646 m_pArrayInt->Clear();
647
648 // add each selected item to our array
649 size_t i,
650 count = pControl->GetCount();
651 for ( i = 0; i < count; i++ )
652 {
653 if (pControl->IsChecked(i))
654 m_pArrayInt->Add(i);
655 }
656
657 return true;
658 }
659 else
660 return false;
661 } else
662 #endif
663 #if wxUSE_LISTBOX
664 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
665 {
666 wxListBox* pControl = (wxListBox*) m_validatorWindow;
667 if (m_pArrayInt)
668 {
669 // clear our array
670 m_pArrayInt->Clear();
671
672 // add each selected item to our array
673 size_t i,
674 count = pControl->GetCount();
675 for ( i = 0; i < count; i++ )
676 {
677 if (pControl->IsSelected(i))
678 m_pArrayInt->Add(i);
679 }
680
681 return true;
682 }
683 } else
684 #endif
685
686 // unrecognized control, or bad pointer
687 return false;
688
689 return false;
690 }
691
692 /*
693 Called by constructors to initialize ALL data members
694 */
695 void wxGenericValidator::Initialize()
696 {
697 m_pBool = NULL;
698 m_pInt = NULL;
699 m_pString = NULL;
700 m_pArrayInt = NULL;
701 #if wxUSE_DATETIME
702 m_pDateTime = NULL;
703 #endif // wxUSE_DATETIME
704 m_pFileName = NULL;
705 m_pFloat = NULL;
706 m_pDouble = NULL;
707 }
708
709 #endif // wxUSE_VALIDATORS