]> git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
fix child window redraw glitches during scrolling (bug 1944002)
[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"
42
43 #if wxUSE_SPINBTN
44 #include "wx/spinbutt.h"
45 #endif
46 #if wxUSE_TOGGLEBTN
47 #include "wx/tglbtn.h"
48 #endif
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(const wxGenericValidator& val)
89 : wxValidator()
90 {
91 Copy(val);
92 }
93
94 bool wxGenericValidator::Copy(const wxGenericValidator& val)
95 {
96 wxValidator::Copy(val);
97
98 m_pBool = val.m_pBool;
99 m_pInt = val.m_pInt;
100 m_pString = val.m_pString;
101 m_pArrayInt = val.m_pArrayInt;
102 #if wxUSE_DATETIME
103 m_pDateTime = val.m_pDateTime;
104 #endif // wxUSE_DATETIME
105
106 return true;
107 }
108
109 // Called to transfer data to the window
110 bool wxGenericValidator::TransferToWindow(void)
111 {
112 if ( !m_validatorWindow )
113 return false;
114
115 // bool controls
116 #if wxUSE_CHECKBOX
117 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
118 {
119 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
120 if (m_pBool)
121 {
122 pControl->SetValue(*m_pBool);
123 return true;
124 }
125 } else
126 #endif
127 #if wxUSE_RADIOBTN
128 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
129 {
130 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
131 if (m_pBool)
132 {
133 pControl->SetValue(*m_pBool) ;
134 return true;
135 }
136 } else
137 #endif
138
139 #if wxUSE_TOGGLEBTN
140 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
141 {
142 wxToggleButton * pControl = (wxToggleButton *) m_validatorWindow;
143 if (m_pBool)
144 {
145 pControl->SetValue(*m_pBool);
146 return true;
147 }
148 } else
149 #if defined(__WXMAC__) || defined(__WXGTK20__)
150 if (m_validatorWindow->IsKindOf(CLASSINFO(wxBitmapToggleButton)) )
151 {
152 wxBitmapToggleButton * pControl = (wxBitmapToggleButton *) m_validatorWindow;
153 if (m_pBool)
154 {
155 pControl->SetValue(*m_pBool);
156 return true;
157 }
158 } else
159 #endif
160 #endif
161
162 // int controls
163 #if wxUSE_GAUGE
164 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
165 {
166 wxGauge* pControl = (wxGauge*) m_validatorWindow;
167 if (m_pInt)
168 {
169 pControl->SetValue(*m_pInt);
170 return true;
171 }
172 } else
173 #endif
174 #if wxUSE_RADIOBOX
175 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
176 {
177 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
178 if (m_pInt)
179 {
180 pControl->SetSelection(*m_pInt) ;
181 return true;
182 }
183 } else
184 #endif
185 #if wxUSE_SCROLLBAR
186 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
187 {
188 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
189 if (m_pInt)
190 {
191 pControl->SetThumbPosition(*m_pInt) ;
192 return true;
193 }
194 } else
195 #endif
196 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
197 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
198 {
199 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
200 if (m_pInt)
201 {
202 pControl->SetValue(*m_pInt);
203 return true;
204 }
205 } else
206 #endif
207 #if wxUSE_SPINBTN
208 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
209 {
210 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
211 if (m_pInt)
212 {
213 pControl->SetValue(*m_pInt) ;
214 return true;
215 }
216 } else
217 #endif
218 #if wxUSE_SLIDER
219 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
220 {
221 wxSlider* pControl = (wxSlider*) m_validatorWindow;
222 if (m_pInt)
223 {
224 pControl->SetValue(*m_pInt) ;
225 return true;
226 }
227 } else
228 #endif
229
230 // date time controls
231 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
232 if (m_validatorWindow->IsKindOf(CLASSINFO(wxDatePickerCtrl)) )
233 {
234 wxDatePickerCtrl* pControl = (wxDatePickerCtrl*) m_validatorWindow;
235 if (m_pDateTime)
236 {
237 pControl->SetValue(*m_pDateTime) ;
238 return true;
239 }
240 } else
241 #endif
242
243 // string controls
244 #if wxUSE_BUTTON
245 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
246 {
247 wxButton* pControl = (wxButton*) m_validatorWindow;
248 if (m_pString)
249 {
250 pControl->SetLabel(*m_pString) ;
251 return true;
252 }
253 } else
254 #endif
255 #if wxUSE_COMBOBOX
256 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
257 {
258 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
259 if (m_pInt)
260 {
261 pControl->SetSelection(*m_pInt) ;
262 return true;
263 }
264 else if (m_pString)
265 {
266 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
267 {
268 pControl->SetStringSelection(* m_pString);
269 }
270 if ((m_validatorWindow->GetWindowStyle() & wxCB_READONLY) == 0)
271 {
272 pControl->SetValue(* m_pString);
273 }
274 return true;
275 }
276 } else
277 #endif
278 #if wxUSE_CHOICE
279 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
280 {
281 wxChoice* pControl = (wxChoice*) m_validatorWindow;
282 if (m_pInt)
283 {
284 pControl->SetSelection(*m_pInt) ;
285 return true;
286 }
287 else if (m_pString)
288 {
289 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
290 {
291 pControl->SetStringSelection(* m_pString);
292 }
293 return true;
294 }
295 } else
296 #endif
297 #if wxUSE_STATTEXT
298 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
299 {
300 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
301 if (m_pString)
302 {
303 pControl->SetLabel(*m_pString) ;
304 return true;
305 }
306 } else
307 #endif
308 #if wxUSE_TEXTCTRL
309 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
310 {
311 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
312 if (m_pString)
313 {
314 pControl->SetValue(*m_pString) ;
315 return true;
316 }
317 else if (m_pInt)
318 {
319 wxString str;
320 str.Printf(wxT("%d"), *m_pInt);
321 pControl->SetValue(str);
322 return true;
323 }
324 } else
325 #endif
326
327 // array controls
328 #if wxUSE_CHECKLISTBOX
329 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
330 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
331 {
332 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
333 if (m_pArrayInt)
334 {
335 // clear all selections
336 size_t i,
337 count = pControl->GetCount();
338 for ( i = 0 ; i < count; i++ )
339 pControl->Check(i, false);
340
341 // select each item in our array
342 count = m_pArrayInt->GetCount();
343 for ( i = 0 ; i < count; i++ )
344 pControl->Check(m_pArrayInt->Item(i));
345
346 return true;
347 }
348 else
349 return false;
350 } else
351 #endif
352 #if wxUSE_LISTBOX
353 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
354 {
355 wxListBox* pControl = (wxListBox*) m_validatorWindow;
356 if (m_pArrayInt)
357 {
358 // clear all selections
359 size_t i,
360 count = pControl->GetCount();
361 for ( i = 0 ; i < count; i++ )
362 pControl->Deselect(i);
363
364 // select each item in our array
365 count = m_pArrayInt->GetCount();
366 for ( i = 0 ; i < count; i++ )
367 pControl->SetSelection(m_pArrayInt->Item(i));
368
369 return true;
370 }
371 } else
372 #endif
373 ; // to match the last 'else' above
374
375 // unrecognized control, or bad pointer
376 return false;
377 }
378
379 // Called to transfer data from the window
380 bool wxGenericValidator::TransferFromWindow(void)
381 {
382 if ( !m_validatorWindow )
383 return false;
384
385 // BOOL CONTROLS **************************************
386 #if wxUSE_CHECKBOX
387 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
388 {
389 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
390 if (m_pBool)
391 {
392 *m_pBool = pControl->GetValue() ;
393 return true;
394 }
395 } else
396 #endif
397 #if wxUSE_RADIOBTN
398 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
399 {
400 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
401 if (m_pBool)
402 {
403 *m_pBool = pControl->GetValue() ;
404 return true;
405 }
406 } else
407 #endif
408 #if wxUSE_TOGGLEBTN
409 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
410 {
411 wxToggleButton *pControl = (wxToggleButton *) m_validatorWindow;
412 if (m_pBool)
413 {
414 *m_pBool = pControl->GetValue() ;
415 return true;
416 }
417 } else
418 #endif
419
420 // INT CONTROLS ***************************************
421 #if wxUSE_GAUGE
422 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
423 {
424 wxGauge* pControl = (wxGauge*) m_validatorWindow;
425 if (m_pInt)
426 {
427 *m_pInt = pControl->GetValue() ;
428 return true;
429 }
430 } else
431 #endif
432 #if wxUSE_RADIOBOX
433 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
434 {
435 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
436 if (m_pInt)
437 {
438 *m_pInt = pControl->GetSelection() ;
439 return true;
440 }
441 } else
442 #endif
443 #if wxUSE_SCROLLBAR
444 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
445 {
446 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
447 if (m_pInt)
448 {
449 *m_pInt = pControl->GetThumbPosition() ;
450 return true;
451 }
452 } else
453 #endif
454 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
455 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
456 {
457 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
458 if (m_pInt)
459 {
460 *m_pInt=pControl->GetValue();
461 return true;
462 }
463 } else
464 #endif
465 #if wxUSE_SPINBTN
466 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
467 {
468 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
469 if (m_pInt)
470 {
471 *m_pInt = pControl->GetValue() ;
472 return true;
473 }
474 } else
475 #endif
476 #if wxUSE_SLIDER
477 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
478 {
479 wxSlider* pControl = (wxSlider*) m_validatorWindow;
480 if (m_pInt)
481 {
482 *m_pInt = pControl->GetValue() ;
483 return true;
484 }
485 } else
486 #endif
487
488 // DATE TIME CONTROLS ************************************
489 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
490 if (m_validatorWindow->IsKindOf(CLASSINFO(wxDatePickerCtrl)) )
491 {
492 wxDatePickerCtrl* pControl = (wxDatePickerCtrl*) m_validatorWindow;
493 if (m_pDateTime)
494 {
495 *m_pDateTime = pControl->GetValue() ;
496 return true;
497 }
498 } else
499 #endif
500
501 // STRING CONTROLS ************************************
502 #if wxUSE_BUTTON
503 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
504 {
505 wxButton* pControl = (wxButton*) m_validatorWindow;
506 if (m_pString)
507 {
508 *m_pString = pControl->GetLabel() ;
509 return true;
510 }
511 } else
512 #endif
513 #if wxUSE_COMBOBOX
514 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
515 {
516 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
517 if (m_pInt)
518 {
519 *m_pInt = pControl->GetSelection() ;
520 return true;
521 }
522 else if (m_pString)
523 {
524 if (m_validatorWindow->GetWindowStyle() & wxCB_READONLY)
525 *m_pString = pControl->GetStringSelection();
526 else
527 *m_pString = pControl->GetValue();
528 return true;
529 }
530 } else
531 #endif
532 #if wxUSE_CHOICE
533 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
534 {
535 wxChoice* pControl = (wxChoice*) m_validatorWindow;
536 if (m_pInt)
537 {
538 *m_pInt = pControl->GetSelection() ;
539 return true;
540 }
541 else if (m_pString)
542 {
543 *m_pString = pControl->GetStringSelection();
544 return true;
545 }
546 } else
547 #endif
548 #if wxUSE_STATTEXT
549 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
550 {
551 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
552 if (m_pString)
553 {
554 *m_pString = pControl->GetLabel() ;
555 return true;
556 }
557 } else
558 #endif
559 #if wxUSE_TEXTCTRL
560 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
561 {
562 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
563 if (m_pString)
564 {
565 *m_pString = pControl->GetValue() ;
566 return true;
567 }
568 else if (m_pInt)
569 {
570 *m_pInt = wxAtoi(pControl->GetValue());
571 return true;
572 }
573 } else
574 #endif
575
576 // ARRAY CONTROLS *************************************
577 #if wxUSE_CHECKLISTBOX
578 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
579 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
580 {
581 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
582 if (m_pArrayInt)
583 {
584 // clear our array
585 m_pArrayInt->Clear();
586
587 // add each selected item to our array
588 size_t i,
589 count = pControl->GetCount();
590 for ( i = 0; i < count; i++ )
591 {
592 if (pControl->IsChecked(i))
593 m_pArrayInt->Add(i);
594 }
595
596 return true;
597 }
598 else
599 return false;
600 } else
601 #endif
602 #if wxUSE_LISTBOX
603 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
604 {
605 wxListBox* pControl = (wxListBox*) m_validatorWindow;
606 if (m_pArrayInt)
607 {
608 // clear our array
609 m_pArrayInt->Clear();
610
611 // add each selected item to our array
612 size_t i,
613 count = pControl->GetCount();
614 for ( i = 0; i < count; i++ )
615 {
616 if (pControl->IsSelected(i))
617 m_pArrayInt->Add(i);
618 }
619
620 return true;
621 }
622 } else
623 #endif
624
625 // unrecognized control, or bad pointer
626 return false;
627
628 return false;
629 }
630
631 /*
632 Called by constructors to initialize ALL data members
633 */
634 void wxGenericValidator::Initialize()
635 {
636 m_pBool = 0;
637 m_pInt = 0;
638 m_pString = 0;
639 m_pArrayInt = 0;
640 #if wxUSE_DATETIME
641 m_pDateTime = 0;
642 #endif // wxUSE_DATETIME
643 }
644
645 #endif // wxUSE_VALIDATORS