]> git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
Add another test for the insertion point position after SetValue().
[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(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
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
376 // unrecognized control, or bad pointer
377 return false;
378 }
379
380 // Called to transfer data from the window
381 bool wxGenericValidator::TransferFromWindow(void)
382 {
383 if ( !m_validatorWindow )
384 return false;
385
386 // BOOL CONTROLS **************************************
387 #if wxUSE_CHECKBOX
388 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
389 {
390 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
391 if (m_pBool)
392 {
393 *m_pBool = pControl->GetValue() ;
394 return true;
395 }
396 } else
397 #endif
398 #if wxUSE_RADIOBTN
399 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
400 {
401 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
402 if (m_pBool)
403 {
404 *m_pBool = pControl->GetValue() ;
405 return true;
406 }
407 } else
408 #endif
409 #if wxUSE_TOGGLEBTN
410 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
411 {
412 wxToggleButton *pControl = (wxToggleButton *) m_validatorWindow;
413 if (m_pBool)
414 {
415 *m_pBool = pControl->GetValue() ;
416 return true;
417 }
418 } else
419 #if (defined(__WXMAC__) || defined(__WXMSW__) || defined(__WXGTK20__)) && !defined(__WXUNIVERSAL__)
420 if (m_validatorWindow->IsKindOf(CLASSINFO(wxBitmapToggleButton)) )
421 {
422 wxBitmapToggleButton *pControl = (wxBitmapToggleButton *) m_validatorWindow;
423 if (m_pBool)
424 {
425 *m_pBool = pControl->GetValue() ;
426 return true;
427 }
428 } else
429 #endif
430 #endif
431
432 // INT CONTROLS ***************************************
433 #if wxUSE_GAUGE
434 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
435 {
436 wxGauge* pControl = (wxGauge*) m_validatorWindow;
437 if (m_pInt)
438 {
439 *m_pInt = pControl->GetValue() ;
440 return true;
441 }
442 } else
443 #endif
444 #if wxUSE_RADIOBOX
445 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
446 {
447 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
448 if (m_pInt)
449 {
450 *m_pInt = pControl->GetSelection() ;
451 return true;
452 }
453 } else
454 #endif
455 #if wxUSE_SCROLLBAR
456 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
457 {
458 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
459 if (m_pInt)
460 {
461 *m_pInt = pControl->GetThumbPosition() ;
462 return true;
463 }
464 } else
465 #endif
466 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
467 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
468 {
469 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
470 if (m_pInt)
471 {
472 *m_pInt=pControl->GetValue();
473 return true;
474 }
475 } else
476 #endif
477 #if wxUSE_SPINBTN
478 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
479 {
480 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
481 if (m_pInt)
482 {
483 *m_pInt = pControl->GetValue() ;
484 return true;
485 }
486 } else
487 #endif
488 #if wxUSE_SLIDER
489 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
490 {
491 wxSlider* pControl = (wxSlider*) m_validatorWindow;
492 if (m_pInt)
493 {
494 *m_pInt = pControl->GetValue() ;
495 return true;
496 }
497 } else
498 #endif
499
500 // DATE TIME CONTROLS ************************************
501 #if 0 // wxUSE_DATEPICKCTRL -- temporary fix for shared build linking
502 if (m_validatorWindow->IsKindOf(CLASSINFO(wxDatePickerCtrl)) )
503 {
504 wxDatePickerCtrl* pControl = (wxDatePickerCtrl*) m_validatorWindow;
505 if (m_pDateTime)
506 {
507 *m_pDateTime = pControl->GetValue() ;
508 return true;
509 }
510 } else
511 #endif
512
513 // STRING CONTROLS ************************************
514 #if wxUSE_BUTTON
515 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
516 {
517 wxButton* pControl = (wxButton*) m_validatorWindow;
518 if (m_pString)
519 {
520 *m_pString = pControl->GetLabel() ;
521 return true;
522 }
523 } else
524 #endif
525 #if wxUSE_COMBOBOX
526 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
527 {
528 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
529 if (m_pInt)
530 {
531 *m_pInt = pControl->GetSelection() ;
532 return true;
533 }
534 else if (m_pString)
535 {
536 if (m_validatorWindow->GetWindowStyle() & wxCB_READONLY)
537 *m_pString = pControl->GetStringSelection();
538 else
539 *m_pString = pControl->GetValue();
540 return true;
541 }
542 } else
543 #endif
544 #if wxUSE_CHOICE
545 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
546 {
547 wxChoice* pControl = (wxChoice*) m_validatorWindow;
548 if (m_pInt)
549 {
550 *m_pInt = pControl->GetSelection() ;
551 return true;
552 }
553 else if (m_pString)
554 {
555 *m_pString = pControl->GetStringSelection();
556 return true;
557 }
558 } else
559 #endif
560 #if wxUSE_STATTEXT
561 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
562 {
563 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
564 if (m_pString)
565 {
566 *m_pString = pControl->GetLabel() ;
567 return true;
568 }
569 } else
570 #endif
571 #if wxUSE_TEXTCTRL
572 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
573 {
574 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
575 if (m_pString)
576 {
577 *m_pString = pControl->GetValue() ;
578 return true;
579 }
580 else if (m_pInt)
581 {
582 *m_pInt = wxAtoi(pControl->GetValue());
583 return true;
584 }
585 } else
586 #endif
587
588 // ARRAY CONTROLS *************************************
589 #if wxUSE_CHECKLISTBOX
590 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
591 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
592 {
593 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
594 if (m_pArrayInt)
595 {
596 // clear our array
597 m_pArrayInt->Clear();
598
599 // add each selected item to our array
600 size_t i,
601 count = pControl->GetCount();
602 for ( i = 0; i < count; i++ )
603 {
604 if (pControl->IsChecked(i))
605 m_pArrayInt->Add(i);
606 }
607
608 return true;
609 }
610 else
611 return false;
612 } else
613 #endif
614 #if wxUSE_LISTBOX
615 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
616 {
617 wxListBox* pControl = (wxListBox*) m_validatorWindow;
618 if (m_pArrayInt)
619 {
620 // clear our array
621 m_pArrayInt->Clear();
622
623 // add each selected item to our array
624 size_t i,
625 count = pControl->GetCount();
626 for ( i = 0; i < count; i++ )
627 {
628 if (pControl->IsSelected(i))
629 m_pArrayInt->Add(i);
630 }
631
632 return true;
633 }
634 } else
635 #endif
636
637 // unrecognized control, or bad pointer
638 return false;
639
640 return false;
641 }
642
643 /*
644 Called by constructors to initialize ALL data members
645 */
646 void wxGenericValidator::Initialize()
647 {
648 m_pBool = 0;
649 m_pInt = 0;
650 m_pString = 0;
651 m_pArrayInt = 0;
652 #if wxUSE_DATETIME
653 m_pDateTime = 0;
654 #endif // wxUSE_DATETIME
655 }
656
657 #endif // wxUSE_VALIDATORS