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