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