]> git.saurik.com Git - wxWidgets.git/blame - src/common/valgen.cpp
Minor header cleaning.
[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
388aa6e2
JS
124#if wxUSE_TOGGLEBTN
125 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
126 {
127 wxToggleButton * pControl = (wxToggleButton *) m_validatorWindow;
40ff126a
WS
128 if (m_pBool)
129 {
130 pControl->SetValue(*m_pBool);
131 return true;
132 }
388aa6e2
JS
133 } else
134#endif
e90196a5
RR
135
136 // int controls
dcf924a3 137#if wxUSE_GAUGE
e90196a5 138 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef 139 {
e90196a5 140 wxGauge* pControl = (wxGauge*) m_validatorWindow;
3ca6a5f0
BP
141 if (m_pInt)
142 {
143 pControl->SetValue(*m_pInt);
cab1a605 144 return true;
3ca6a5f0 145 }
e90196a5 146 } else
dcf924a3
RR
147#endif
148#if wxUSE_RADIOBOX
e90196a5 149 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef 150 {
e90196a5 151 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
3ca6a5f0
BP
152 if (m_pInt)
153 {
154 pControl->SetSelection(*m_pInt) ;
cab1a605 155 return true;
3ca6a5f0 156 }
e90196a5 157 } else
dcf924a3
RR
158#endif
159#if wxUSE_SCROLLBAR
e90196a5 160 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef 161 {
e90196a5 162 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
3ca6a5f0
BP
163 if (m_pInt)
164 {
165 pControl->SetThumbPosition(*m_pInt) ;
cab1a605 166 return true;
3ca6a5f0 167 }
e90196a5 168 } else
dcf924a3 169#endif
3a5bcc4d 170#if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
a55e0ebc
JS
171 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
172 {
173 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
174 if (m_pInt)
175 {
176 pControl->SetValue(*m_pInt);
cab1a605 177 return true;
a55e0ebc
JS
178 }
179 } else
180#endif
3a5bcc4d 181#if wxUSE_SPINBTN
e90196a5 182 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef 183 {
e90196a5 184 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
3ca6a5f0
BP
185 if (m_pInt)
186 {
187 pControl->SetValue(*m_pInt) ;
cab1a605 188 return true;
3ca6a5f0 189 }
e90196a5 190 } else
dcf924a3 191#endif
e90196a5
RR
192#if wxUSE_SLIDER
193 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
194 {
195 wxSlider* pControl = (wxSlider*) m_validatorWindow;
196 if (m_pInt)
197 {
3ca6a5f0 198 pControl->SetValue(*m_pInt) ;
cab1a605 199 return true;
3ca6a5f0 200 }
e90196a5
RR
201 } else
202#endif
203
3ca6a5f0 204 // string controls
d7260478 205#if wxUSE_BUTTON
e90196a5 206 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef 207 {
e90196a5 208 wxButton* pControl = (wxButton*) m_validatorWindow;
3ca6a5f0
BP
209 if (m_pString)
210 {
211 pControl->SetLabel(*m_pString) ;
cab1a605 212 return true;
3ca6a5f0 213 }
e90196a5 214 } else
d7260478 215#endif
e90196a5
RR
216#if wxUSE_COMBOBOX
217 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef 218 {
e90196a5 219 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
f6bcfd97
BP
220 if (m_pInt)
221 {
222 pControl->SetSelection(*m_pInt) ;
cab1a605 223 return true;
f6bcfd97
BP
224 }
225 else if (m_pString)
226 {
cab1a605 227 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
f6bcfd97
BP
228 {
229 pControl->SetStringSelection(* m_pString);
230 }
780fb83c 231 if ((m_validatorWindow->GetWindowStyle() & wxCB_READONLY) == 0)
7cc3cec9
JS
232 {
233 pControl->SetValue(* m_pString);
234 }
cab1a605 235 return true;
f6bcfd97 236 }
e90196a5 237 } else
ce4169a4 238#endif
a55e0ebc
JS
239#if wxUSE_CHOICE
240 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
f6bcfd97 241 {
a55e0ebc 242 wxChoice* pControl = (wxChoice*) m_validatorWindow;
f6bcfd97
BP
243 if (m_pInt)
244 {
245 pControl->SetSelection(*m_pInt) ;
cab1a605 246 return true;
f6bcfd97
BP
247 }
248 else if (m_pString)
249 {
cab1a605 250 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
f6bcfd97
BP
251 {
252 pControl->SetStringSelection(* m_pString);
253 }
cab1a605 254 return true;
f6bcfd97
BP
255 }
256 } else
a55e0ebc 257#endif
001e76a9 258#if wxUSE_STATTEXT
e90196a5 259 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef 260 {
e90196a5 261 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
f6bcfd97
BP
262 if (m_pString)
263 {
264 pControl->SetLabel(*m_pString) ;
cab1a605 265 return true;
f6bcfd97 266 }
3ca6a5f0 267 } else
001e76a9 268#endif
d7260478 269#if wxUSE_TEXTCTRL
e90196a5 270 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef 271 {
e90196a5 272 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
f6bcfd97
BP
273 if (m_pString)
274 {
275 pControl->SetValue(*m_pString) ;
cab1a605 276 return true;
f6bcfd97
BP
277 }
278 else if (m_pInt)
279 {
280 wxString str;
66b3ec7f 281 str.Printf(wxT("%d"), *m_pInt);
f6bcfd97 282 pControl->SetValue(str);
cab1a605 283 return true;
f6bcfd97 284 }
e90196a5 285 } else
d7260478 286#endif
388aa6e2 287
1e6feb95 288 // array controls
3a5bcc4d 289#if wxUSE_CHECKLISTBOX
1e6feb95
VZ
290 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
291 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef 292 {
1e6feb95
VZ
293 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
294 if (m_pArrayInt)
295 {
296 // clear all selections
297 size_t i,
298 count = pControl->GetCount();
299 for ( i = 0 ; i < count; i++ )
cab1a605 300 pControl->Check(i, false);
1e6feb95
VZ
301
302 // select each item in our array
303 count = m_pArrayInt->GetCount();
304 for ( i = 0 ; i < count; i++ )
305 pControl->Check(m_pArrayInt->Item(i));
306
cab1a605 307 return true;
1e6feb95 308 }
3ca6a5f0 309 else
cab1a605 310 return false;
1e6feb95 311 } else
750b78ba 312#endif
dcf924a3 313#if wxUSE_LISTBOX
e90196a5 314 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef 315 {
e90196a5 316 wxListBox* pControl = (wxListBox*) m_validatorWindow;
3ca6a5f0
BP
317 if (m_pArrayInt)
318 {
319 // clear all selections
1e6feb95
VZ
320 size_t i,
321 count = pControl->GetCount();
322 for ( i = 0 ; i < count; i++ )
3ca6a5f0 323 pControl->Deselect(i);
1e6feb95 324
3ca6a5f0 325 // select each item in our array
1e6feb95
VZ
326 count = m_pArrayInt->GetCount();
327 for ( i = 0 ; i < count; i++ )
328 pControl->SetSelection(m_pArrayInt->Item(i));
329
cab1a605 330 return true;
3ca6a5f0 331 }
e90196a5 332 } else
dcf924a3 333#endif
3ca6a5f0 334 ; // to match the last 'else' above
89c684ef
JS
335
336 // unrecognized control, or bad pointer
cab1a605 337 return false;
89c684ef
JS
338}
339
e90196a5 340// Called to transfer data from the window
89c684ef
JS
341bool wxGenericValidator::TransferFromWindow(void)
342{
7448de8d
WS
343 if ( !m_validatorWindow )
344 return false;
89c684ef 345
7448de8d 346 // BOOL CONTROLS **************************************
dcf924a3 347#if wxUSE_CHECKBOX
7448de8d 348 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
89c684ef 349 {
7448de8d
WS
350 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
351 if (m_pBool)
352 {
353 *m_pBool = pControl->GetValue() ;
354 return true;
355 }
356 } else
dcf924a3
RR
357#endif
358#if wxUSE_RADIOBTN
7448de8d 359 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef 360 {
7448de8d
WS
361 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
362 if (m_pBool)
363 {
364 *m_pBool = pControl->GetValue() ;
365 return true;
366 }
367 } else
dcf924a3 368#endif
388aa6e2
JS
369#if wxUSE_TOGGLEBTN
370 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
371 {
40ff126a
WS
372 wxToggleButton *pControl = (wxToggleButton *) m_validatorWindow;
373 if (m_pBool)
374 {
375 *m_pBool = pControl->GetValue() ;
376 return true;
377 }
388aa6e2
JS
378 } else
379#endif
7448de8d
WS
380
381 // INT CONTROLS ***************************************
dcf924a3 382#if wxUSE_GAUGE
7448de8d 383 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef 384 {
7448de8d
WS
385 wxGauge* pControl = (wxGauge*) m_validatorWindow;
386 if (m_pInt)
387 {
388 *m_pInt = pControl->GetValue() ;
389 return true;
390 }
391 } else
dcf924a3
RR
392#endif
393#if wxUSE_RADIOBOX
7448de8d 394 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef 395 {
7448de8d
WS
396 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
397 if (m_pInt)
398 {
399 *m_pInt = pControl->GetSelection() ;
400 return true;
401 }
402 } else
dcf924a3
RR
403#endif
404#if wxUSE_SCROLLBAR
7448de8d 405 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef 406 {
7448de8d
WS
407 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
408 if (m_pInt)
409 {
410 *m_pInt = pControl->GetThumbPosition() ;
411 return true;
412 }
413 } else
dcf924a3 414#endif
3a5bcc4d 415#if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
a55e0ebc
JS
416 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
417 {
418 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
419 if (m_pInt)
420 {
421 *m_pInt=pControl->GetValue();
cab1a605 422 return true;
a55e0ebc
JS
423 }
424 } else
425#endif
3a5bcc4d 426#if wxUSE_SPINBTN
7448de8d 427 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef 428 {
7448de8d
WS
429 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
430 if (m_pInt)
431 {
432 *m_pInt = pControl->GetValue() ;
433 return true;
434 }
435 } else
dcf924a3 436#endif
e90196a5 437#if wxUSE_SLIDER
7448de8d 438 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
e90196a5 439 {
7448de8d
WS
440 wxSlider* pControl = (wxSlider*) m_validatorWindow;
441 if (m_pInt)
442 {
443 *m_pInt = pControl->GetValue() ;
444 return true;
445 }
446 } else
750b78ba 447#endif
7448de8d
WS
448
449 // STRING CONTROLS ************************************
d7260478 450#if wxUSE_BUTTON
7448de8d 451 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef 452 {
7448de8d
WS
453 wxButton* pControl = (wxButton*) m_validatorWindow;
454 if (m_pString)
455 {
456 *m_pString = pControl->GetLabel() ;
457 return true;
458 }
459 } else
d7260478 460#endif
dcf924a3 461#if wxUSE_COMBOBOX
7448de8d 462 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
f6bcfd97 463 {
7448de8d
WS
464 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
465 if (m_pInt)
466 {
467 *m_pInt = pControl->GetSelection() ;
468 return true;
469 }
470 else if (m_pString)
471 {
472 if (m_validatorWindow->GetWindowStyle() & wxCB_READONLY)
473 *m_pString = pControl->GetStringSelection();
474 else
475 *m_pString = pControl->GetValue();
476 return true;
477 }
478 } else
dcf924a3 479#endif
ce4169a4 480#if wxUSE_CHOICE
7448de8d 481 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
f6bcfd97 482 {
7448de8d
WS
483 wxChoice* pControl = (wxChoice*) m_validatorWindow;
484 if (m_pInt)
485 {
486 *m_pInt = pControl->GetSelection() ;
487 return true;
488 }
489 else if (m_pString)
490 {
491 *m_pString = pControl->GetStringSelection();
492 return true;
493 }
494 } else
ce4169a4 495#endif
001e76a9 496#if wxUSE_STATTEXT
7448de8d 497 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef 498 {
7448de8d
WS
499 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
500 if (m_pString)
501 {
502 *m_pString = pControl->GetLabel() ;
503 return true;
504 }
505 } else
001e76a9 506#endif
d7260478 507#if wxUSE_TEXTCTRL
7448de8d 508 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
f6bcfd97 509 {
7448de8d
WS
510 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
511 if (m_pString)
512 {
513 *m_pString = pControl->GetValue() ;
514 return true;
515 }
516 else if (m_pInt)
517 {
518 *m_pInt = wxAtoi(pControl->GetValue());
519 return true;
520 }
521 } else
d7260478 522#endif
7448de8d
WS
523
524 // ARRAY CONTROLS *************************************
e90196a5 525#if wxUSE_CHECKLISTBOX
7448de8d
WS
526 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
527 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef 528 {
7448de8d
WS
529 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
530 if (m_pArrayInt)
531 {
532 // clear our array
533 m_pArrayInt->Clear();
534
535 // add each selected item to our array
536 size_t i,
537 count = pControl->GetCount();
538 for ( i = 0; i < count; i++ )
539 {
540 if (pControl->IsChecked(i))
541 m_pArrayInt->Add(i);
542 }
543
544 return true;
545 }
546 else
547 return false;
548 } else
dcf924a3 549#endif
dcf924a3 550#if wxUSE_LISTBOX
7448de8d 551 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef 552 {
7448de8d
WS
553 wxListBox* pControl = (wxListBox*) m_validatorWindow;
554 if (m_pArrayInt)
555 {
556 // clear our array
557 m_pArrayInt->Clear();
1e6feb95 558
7448de8d
WS
559 // add each selected item to our array
560 size_t i,
561 count = pControl->GetCount();
562 for ( i = 0; i < count; i++ )
563 {
40ff126a 564 if (pControl->IsSelected(i))
7448de8d
WS
565 m_pArrayInt->Add(i);
566 }
1e6feb95 567
7448de8d
WS
568 return true;
569 }
570 } else
dcf924a3 571#endif
89c684ef 572
7448de8d
WS
573 // unrecognized control, or bad pointer
574 return false;
575
cab1a605 576 return false;
89c684ef
JS
577}
578
579/*
580 Called by constructors to initialize ALL data members
581*/
582void wxGenericValidator::Initialize()
583{
ce4169a4
RR
584 m_pBool = 0;
585 m_pInt = 0;
586 m_pString = 0;
587 m_pArrayInt = 0;
89c684ef
JS
588}
589
ce4169a4
RR
590#endif
591 // wxUSE_VALIDATORS