]> git.saurik.com Git - wxWidgets.git/blame - src/common/valgen.cpp
fixed incorrect use of wxVector<> in wxXRC
[wxWidgets.git] / src / common / valgen.cpp
CommitLineData
89c684ef 1/////////////////////////////////////////////////////////////////////////////
40ff126a 2// Name: src/common/valgen.cpp
89c684ef
JS
3// Purpose: wxGenericValidator class
4// Author: Kevin Smith
5// Modified by:
6// Created: Jan 22 1999
7448de8d 7// RCS-ID: $Id$
89c684ef 8// Copyright: (c) 1999 Kevin Smith
7448de8d 9// Licence: wxWindows licence
89c684ef
JS
10/////////////////////////////////////////////////////////////////////////////
11
89c684ef
JS
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
8898456d 16 #pragma hdrstop
ce4169a4
RR
17#endif
18
19#if wxUSE_VALIDATORS
20
21#ifndef WX_PRECOMP
ad9835c9 22 #include "wx/dynarray.h"
8898456d
WS
23 #include "wx/utils.h"
24 #include "wx/intl.h"
8898456d
WS
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"
e6e83933 37 #include "wx/checklst.h"
89c684ef
JS
38#endif
39
388aa6e2 40#include "wx/spinctrl.h"
4ded51f2 41
cab1a605 42#if wxUSE_SPINBTN
ad9835c9 43 #include "wx/spinbutt.h"
750b78ba 44#endif
388aa6e2 45#if wxUSE_TOGGLEBTN
ad9835c9 46 #include "wx/tglbtn.h"
388aa6e2 47#endif
89c684ef
JS
48
49#include "wx/valgen.h"
50
5cf69f76
JS
51IMPLEMENT_CLASS(wxGenericValidator, wxValidator)
52
89c684ef
JS
53wxGenericValidator::wxGenericValidator(bool *val)
54{
ce4169a4
RR
55 Initialize();
56 m_pBool = val;
89c684ef
JS
57}
58
59wxGenericValidator::wxGenericValidator(int *val)
60{
ce4169a4
RR
61 Initialize();
62 m_pInt = val;
89c684ef
JS
63}
64
65wxGenericValidator::wxGenericValidator(wxString *val)
66{
ce4169a4
RR
67 Initialize();
68 m_pString = val;
89c684ef
JS
69}
70
71wxGenericValidator::wxGenericValidator(wxArrayInt *val)
72{
ce4169a4
RR
73 Initialize();
74 m_pArrayInt = val;
89c684ef
JS
75}
76
77wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
d84afea9 78 : wxValidator()
89c684ef 79{
ce4169a4 80 Copy(val);
89c684ef
JS
81}
82
83bool wxGenericValidator::Copy(const wxGenericValidator& val)
84{
e90196a5 85 wxValidator::Copy(val);
89c684ef 86
e90196a5
RR
87 m_pBool = val.m_pBool;
88 m_pInt = val.m_pInt;
89 m_pString = val.m_pString;
90 m_pArrayInt = val.m_pArrayInt;
89c684ef 91
cab1a605 92 return true;
89c684ef
JS
93}
94
89c684ef
JS
95// Called to transfer data to the window
96bool wxGenericValidator::TransferToWindow(void)
97{
e90196a5 98 if ( !m_validatorWindow )
cab1a605 99 return false;
89c684ef 100
e90196a5 101 // bool controls
dcf924a3 102#if wxUSE_CHECKBOX
e90196a5 103 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
89c684ef 104 {
e90196a5
RR
105 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
106 if (m_pBool)
3ca6a5f0
BP
107 {
108 pControl->SetValue(*m_pBool);
cab1a605 109 return true;
3ca6a5f0 110 }
e90196a5 111 } else
dcf924a3
RR
112#endif
113#if wxUSE_RADIOBTN
e90196a5 114 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef 115 {
e90196a5 116 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
3ca6a5f0
BP
117 if (m_pBool)
118 {
119 pControl->SetValue(*m_pBool) ;
cab1a605 120 return true;
3ca6a5f0 121 }
e90196a5 122 } else
dcf924a3 123#endif
10ff9c61 124
388aa6e2
JS
125#if wxUSE_TOGGLEBTN
126 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
127 {
128 wxToggleButton * pControl = (wxToggleButton *) m_validatorWindow;
40ff126a
WS
129 if (m_pBool)
130 {
131 pControl->SetValue(*m_pBool);
132 return true;
133 }
388aa6e2 134 } else
10ff9c61
RR
135#if defined(__WXMAC__) || defined(__WXGTK20__)
136 if (m_validatorWindow->IsKindOf(CLASSINFO(wxBitmapToggleButton)) )
137 {
138 wxBitmapToggleButton * pControl = (wxBitmapToggleButton *) m_validatorWindow;
139 if (m_pBool)
140 {
141 pControl->SetValue(*m_pBool);
142 return true;
143 }
144 } else
145#endif
388aa6e2 146#endif
e90196a5
RR
147
148 // int controls
dcf924a3 149#if wxUSE_GAUGE
e90196a5 150 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef 151 {
e90196a5 152 wxGauge* pControl = (wxGauge*) m_validatorWindow;
3ca6a5f0
BP
153 if (m_pInt)
154 {
155 pControl->SetValue(*m_pInt);
cab1a605 156 return true;
3ca6a5f0 157 }
e90196a5 158 } else
dcf924a3
RR
159#endif
160#if wxUSE_RADIOBOX
e90196a5 161 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef 162 {
e90196a5 163 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
3ca6a5f0
BP
164 if (m_pInt)
165 {
166 pControl->SetSelection(*m_pInt) ;
cab1a605 167 return true;
3ca6a5f0 168 }
e90196a5 169 } else
dcf924a3
RR
170#endif
171#if wxUSE_SCROLLBAR
e90196a5 172 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef 173 {
e90196a5 174 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
3ca6a5f0
BP
175 if (m_pInt)
176 {
177 pControl->SetThumbPosition(*m_pInt) ;
cab1a605 178 return true;
3ca6a5f0 179 }
e90196a5 180 } else
dcf924a3 181#endif
3a5bcc4d 182#if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
a55e0ebc
JS
183 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
184 {
185 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
186 if (m_pInt)
187 {
188 pControl->SetValue(*m_pInt);
cab1a605 189 return true;
a55e0ebc
JS
190 }
191 } else
192#endif
3a5bcc4d 193#if wxUSE_SPINBTN
e90196a5 194 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef 195 {
e90196a5 196 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
3ca6a5f0
BP
197 if (m_pInt)
198 {
199 pControl->SetValue(*m_pInt) ;
cab1a605 200 return true;
3ca6a5f0 201 }
e90196a5 202 } else
dcf924a3 203#endif
e90196a5
RR
204#if wxUSE_SLIDER
205 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
206 {
207 wxSlider* pControl = (wxSlider*) m_validatorWindow;
208 if (m_pInt)
209 {
3ca6a5f0 210 pControl->SetValue(*m_pInt) ;
cab1a605 211 return true;
3ca6a5f0 212 }
e90196a5
RR
213 } else
214#endif
215
3ca6a5f0 216 // string controls
d7260478 217#if wxUSE_BUTTON
e90196a5 218 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef 219 {
e90196a5 220 wxButton* pControl = (wxButton*) m_validatorWindow;
3ca6a5f0
BP
221 if (m_pString)
222 {
223 pControl->SetLabel(*m_pString) ;
cab1a605 224 return true;
3ca6a5f0 225 }
e90196a5 226 } else
d7260478 227#endif
e90196a5
RR
228#if wxUSE_COMBOBOX
229 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef 230 {
e90196a5 231 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
f6bcfd97
BP
232 if (m_pInt)
233 {
234 pControl->SetSelection(*m_pInt) ;
cab1a605 235 return true;
f6bcfd97
BP
236 }
237 else if (m_pString)
238 {
cab1a605 239 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
f6bcfd97
BP
240 {
241 pControl->SetStringSelection(* m_pString);
242 }
780fb83c 243 if ((m_validatorWindow->GetWindowStyle() & wxCB_READONLY) == 0)
7cc3cec9
JS
244 {
245 pControl->SetValue(* m_pString);
246 }
cab1a605 247 return true;
f6bcfd97 248 }
e90196a5 249 } else
ce4169a4 250#endif
a55e0ebc
JS
251#if wxUSE_CHOICE
252 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
f6bcfd97 253 {
a55e0ebc 254 wxChoice* pControl = (wxChoice*) m_validatorWindow;
f6bcfd97
BP
255 if (m_pInt)
256 {
257 pControl->SetSelection(*m_pInt) ;
cab1a605 258 return true;
f6bcfd97
BP
259 }
260 else if (m_pString)
261 {
cab1a605 262 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
f6bcfd97
BP
263 {
264 pControl->SetStringSelection(* m_pString);
265 }
cab1a605 266 return true;
f6bcfd97
BP
267 }
268 } else
a55e0ebc 269#endif
001e76a9 270#if wxUSE_STATTEXT
e90196a5 271 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef 272 {
e90196a5 273 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
f6bcfd97
BP
274 if (m_pString)
275 {
276 pControl->SetLabel(*m_pString) ;
cab1a605 277 return true;
f6bcfd97 278 }
3ca6a5f0 279 } else
001e76a9 280#endif
d7260478 281#if wxUSE_TEXTCTRL
e90196a5 282 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef 283 {
e90196a5 284 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
f6bcfd97
BP
285 if (m_pString)
286 {
287 pControl->SetValue(*m_pString) ;
cab1a605 288 return true;
f6bcfd97
BP
289 }
290 else if (m_pInt)
291 {
292 wxString str;
66b3ec7f 293 str.Printf(wxT("%d"), *m_pInt);
f6bcfd97 294 pControl->SetValue(str);
cab1a605 295 return true;
f6bcfd97 296 }
e90196a5 297 } else
d7260478 298#endif
388aa6e2 299
1e6feb95 300 // array controls
3a5bcc4d 301#if wxUSE_CHECKLISTBOX
1e6feb95
VZ
302 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
303 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef 304 {
1e6feb95
VZ
305 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
306 if (m_pArrayInt)
307 {
308 // clear all selections
309 size_t i,
310 count = pControl->GetCount();
311 for ( i = 0 ; i < count; i++ )
cab1a605 312 pControl->Check(i, false);
1e6feb95
VZ
313
314 // select each item in our array
315 count = m_pArrayInt->GetCount();
316 for ( i = 0 ; i < count; i++ )
317 pControl->Check(m_pArrayInt->Item(i));
318
cab1a605 319 return true;
1e6feb95 320 }
3ca6a5f0 321 else
cab1a605 322 return false;
1e6feb95 323 } else
750b78ba 324#endif
dcf924a3 325#if wxUSE_LISTBOX
e90196a5 326 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef 327 {
e90196a5 328 wxListBox* pControl = (wxListBox*) m_validatorWindow;
3ca6a5f0
BP
329 if (m_pArrayInt)
330 {
331 // clear all selections
1e6feb95
VZ
332 size_t i,
333 count = pControl->GetCount();
334 for ( i = 0 ; i < count; i++ )
3ca6a5f0 335 pControl->Deselect(i);
1e6feb95 336
3ca6a5f0 337 // select each item in our array
1e6feb95
VZ
338 count = m_pArrayInt->GetCount();
339 for ( i = 0 ; i < count; i++ )
340 pControl->SetSelection(m_pArrayInt->Item(i));
341
cab1a605 342 return true;
3ca6a5f0 343 }
e90196a5 344 } else
dcf924a3 345#endif
3ca6a5f0 346 ; // to match the last 'else' above
89c684ef
JS
347
348 // unrecognized control, or bad pointer
cab1a605 349 return false;
89c684ef
JS
350}
351
e90196a5 352// Called to transfer data from the window
89c684ef
JS
353bool wxGenericValidator::TransferFromWindow(void)
354{
7448de8d
WS
355 if ( !m_validatorWindow )
356 return false;
89c684ef 357
7448de8d 358 // BOOL CONTROLS **************************************
dcf924a3 359#if wxUSE_CHECKBOX
7448de8d 360 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
89c684ef 361 {
7448de8d
WS
362 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
363 if (m_pBool)
364 {
365 *m_pBool = pControl->GetValue() ;
366 return true;
367 }
368 } else
dcf924a3
RR
369#endif
370#if wxUSE_RADIOBTN
7448de8d 371 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef 372 {
7448de8d
WS
373 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
374 if (m_pBool)
375 {
376 *m_pBool = pControl->GetValue() ;
377 return true;
378 }
379 } else
dcf924a3 380#endif
388aa6e2
JS
381#if wxUSE_TOGGLEBTN
382 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
383 {
40ff126a
WS
384 wxToggleButton *pControl = (wxToggleButton *) m_validatorWindow;
385 if (m_pBool)
386 {
387 *m_pBool = pControl->GetValue() ;
388 return true;
389 }
388aa6e2
JS
390 } else
391#endif
7448de8d
WS
392
393 // INT CONTROLS ***************************************
dcf924a3 394#if wxUSE_GAUGE
7448de8d 395 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef 396 {
7448de8d
WS
397 wxGauge* pControl = (wxGauge*) m_validatorWindow;
398 if (m_pInt)
399 {
400 *m_pInt = pControl->GetValue() ;
401 return true;
402 }
403 } else
dcf924a3
RR
404#endif
405#if wxUSE_RADIOBOX
7448de8d 406 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef 407 {
7448de8d
WS
408 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
409 if (m_pInt)
410 {
411 *m_pInt = pControl->GetSelection() ;
412 return true;
413 }
414 } else
dcf924a3
RR
415#endif
416#if wxUSE_SCROLLBAR
7448de8d 417 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef 418 {
7448de8d
WS
419 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
420 if (m_pInt)
421 {
422 *m_pInt = pControl->GetThumbPosition() ;
423 return true;
424 }
425 } else
dcf924a3 426#endif
3a5bcc4d 427#if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
a55e0ebc
JS
428 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
429 {
430 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
431 if (m_pInt)
432 {
433 *m_pInt=pControl->GetValue();
cab1a605 434 return true;
a55e0ebc
JS
435 }
436 } else
437#endif
3a5bcc4d 438#if wxUSE_SPINBTN
7448de8d 439 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef 440 {
7448de8d
WS
441 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
442 if (m_pInt)
443 {
444 *m_pInt = pControl->GetValue() ;
445 return true;
446 }
447 } else
dcf924a3 448#endif
e90196a5 449#if wxUSE_SLIDER
7448de8d 450 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
e90196a5 451 {
7448de8d
WS
452 wxSlider* pControl = (wxSlider*) m_validatorWindow;
453 if (m_pInt)
454 {
455 *m_pInt = pControl->GetValue() ;
456 return true;
457 }
458 } else
750b78ba 459#endif
7448de8d
WS
460
461 // STRING CONTROLS ************************************
d7260478 462#if wxUSE_BUTTON
7448de8d 463 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef 464 {
7448de8d
WS
465 wxButton* pControl = (wxButton*) m_validatorWindow;
466 if (m_pString)
467 {
468 *m_pString = pControl->GetLabel() ;
469 return true;
470 }
471 } else
d7260478 472#endif
dcf924a3 473#if wxUSE_COMBOBOX
7448de8d 474 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
f6bcfd97 475 {
7448de8d
WS
476 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
477 if (m_pInt)
478 {
479 *m_pInt = pControl->GetSelection() ;
480 return true;
481 }
482 else if (m_pString)
483 {
484 if (m_validatorWindow->GetWindowStyle() & wxCB_READONLY)
485 *m_pString = pControl->GetStringSelection();
486 else
487 *m_pString = pControl->GetValue();
488 return true;
489 }
490 } else
dcf924a3 491#endif
ce4169a4 492#if wxUSE_CHOICE
7448de8d 493 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
f6bcfd97 494 {
7448de8d
WS
495 wxChoice* pControl = (wxChoice*) m_validatorWindow;
496 if (m_pInt)
497 {
498 *m_pInt = pControl->GetSelection() ;
499 return true;
500 }
501 else if (m_pString)
502 {
503 *m_pString = pControl->GetStringSelection();
504 return true;
505 }
506 } else
ce4169a4 507#endif
001e76a9 508#if wxUSE_STATTEXT
7448de8d 509 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef 510 {
7448de8d
WS
511 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
512 if (m_pString)
513 {
514 *m_pString = pControl->GetLabel() ;
515 return true;
516 }
517 } else
001e76a9 518#endif
d7260478 519#if wxUSE_TEXTCTRL
7448de8d 520 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
f6bcfd97 521 {
7448de8d
WS
522 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
523 if (m_pString)
524 {
525 *m_pString = pControl->GetValue() ;
526 return true;
527 }
528 else if (m_pInt)
529 {
530 *m_pInt = wxAtoi(pControl->GetValue());
531 return true;
532 }
533 } else
d7260478 534#endif
7448de8d
WS
535
536 // ARRAY CONTROLS *************************************
e90196a5 537#if wxUSE_CHECKLISTBOX
7448de8d
WS
538 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
539 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef 540 {
7448de8d
WS
541 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
542 if (m_pArrayInt)
543 {
544 // clear our array
545 m_pArrayInt->Clear();
546
547 // add each selected item to our array
548 size_t i,
549 count = pControl->GetCount();
550 for ( i = 0; i < count; i++ )
551 {
552 if (pControl->IsChecked(i))
553 m_pArrayInt->Add(i);
554 }
555
556 return true;
557 }
558 else
559 return false;
560 } else
dcf924a3 561#endif
dcf924a3 562#if wxUSE_LISTBOX
7448de8d 563 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef 564 {
7448de8d
WS
565 wxListBox* pControl = (wxListBox*) m_validatorWindow;
566 if (m_pArrayInt)
567 {
568 // clear our array
569 m_pArrayInt->Clear();
1e6feb95 570
7448de8d
WS
571 // add each selected item to our array
572 size_t i,
573 count = pControl->GetCount();
574 for ( i = 0; i < count; i++ )
575 {
40ff126a 576 if (pControl->IsSelected(i))
7448de8d
WS
577 m_pArrayInt->Add(i);
578 }
1e6feb95 579
7448de8d
WS
580 return true;
581 }
582 } else
dcf924a3 583#endif
89c684ef 584
7448de8d
WS
585 // unrecognized control, or bad pointer
586 return false;
587
cab1a605 588 return false;
89c684ef
JS
589}
590
591/*
592 Called by constructors to initialize ALL data members
593*/
594void wxGenericValidator::Initialize()
595{
ce4169a4
RR
596 m_pBool = 0;
597 m_pInt = 0;
598 m_pString = 0;
599 m_pArrayInt = 0;
89c684ef
JS
600}
601
ce4169a4
RR
602#endif
603 // wxUSE_VALIDATORS