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