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