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