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