]> git.saurik.com Git - wxWidgets.git/blame - src/common/valgen.cpp
fixed 3 infinite loops -- the person who tested this code must have had a lot of...
[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
3ca6a5f0 9// Licence: wxWindows licence
89c684ef
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
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
a55e0ebc
JS
47#if wxUSE_SPINCTRL && !defined(__WIN16__)
48 #include "wx/spinctrl.h"
49#endif
50#if wxUSE_SPINBTN && !defined(__WIN16__)
ce4169a4 51 #include "wx/spinbutt.h"
750b78ba 52#endif
a55e0ebc
JS
53#if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
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
e90196a5 100 return TRUE;
89c684ef
JS
101}
102
103wxGenericValidator::~wxGenericValidator()
104{
105}
106
107// Called to transfer data to the window
108bool wxGenericValidator::TransferToWindow(void)
109{
e90196a5
RR
110 if ( !m_validatorWindow )
111 return FALSE;
89c684ef 112
e90196a5 113 // bool controls
dcf924a3 114#if wxUSE_CHECKBOX
e90196a5 115 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
89c684ef 116 {
e90196a5
RR
117 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
118 if (m_pBool)
3ca6a5f0
BP
119 {
120 pControl->SetValue(*m_pBool);
121 return TRUE;
122 }
e90196a5 123 } else
dcf924a3
RR
124#endif
125#if wxUSE_RADIOBTN
e90196a5 126 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef 127 {
e90196a5 128 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
3ca6a5f0
BP
129 if (m_pBool)
130 {
131 pControl->SetValue(*m_pBool) ;
132 return TRUE;
133 }
e90196a5 134 } else
dcf924a3 135#endif
e90196a5
RR
136
137 // int controls
dcf924a3 138#if wxUSE_GAUGE
e90196a5 139 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef 140 {
e90196a5 141 wxGauge* pControl = (wxGauge*) m_validatorWindow;
3ca6a5f0
BP
142 if (m_pInt)
143 {
144 pControl->SetValue(*m_pInt);
145 return TRUE;
146 }
e90196a5 147 } else
dcf924a3
RR
148#endif
149#if wxUSE_RADIOBOX
e90196a5 150 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef 151 {
e90196a5 152 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
3ca6a5f0
BP
153 if (m_pInt)
154 {
155 pControl->SetSelection(*m_pInt) ;
156 return TRUE;
157 }
e90196a5 158 } else
dcf924a3
RR
159#endif
160#if wxUSE_SCROLLBAR
e90196a5 161 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef 162 {
e90196a5 163 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
3ca6a5f0
BP
164 if (m_pInt)
165 {
166 pControl->SetThumbPosition(*m_pInt) ;
167 return TRUE;
168 }
e90196a5 169 } else
dcf924a3 170#endif
5e679c40 171#if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
a55e0ebc
JS
172 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
173 {
174 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
175 if (m_pInt)
176 {
177 pControl->SetValue(*m_pInt);
178 return TRUE;
179 }
180 } else
181#endif
182#if wxUSE_SPINBTN && !defined(__WIN16__)
e90196a5 183 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef 184 {
e90196a5 185 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
3ca6a5f0
BP
186 if (m_pInt)
187 {
188 pControl->SetValue(*m_pInt) ;
189 return TRUE;
190 }
e90196a5 191 } else
dcf924a3 192#endif
e90196a5
RR
193#if wxUSE_SLIDER
194 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
195 {
196 wxSlider* pControl = (wxSlider*) m_validatorWindow;
197 if (m_pInt)
198 {
3ca6a5f0
BP
199 pControl->SetValue(*m_pInt) ;
200 return TRUE;
201 }
e90196a5
RR
202 } else
203#endif
204
3ca6a5f0 205 // string controls
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) ;
212 return TRUE;
213 }
e90196a5 214 } else
e90196a5
RR
215#if wxUSE_COMBOBOX
216 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef 217 {
e90196a5 218 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
f6bcfd97
BP
219 if (m_pInt)
220 {
221 pControl->SetSelection(*m_pInt) ;
222 return TRUE;
223 }
224 else if (m_pString)
225 {
226 if (pControl->FindString(* m_pString) > -1)
227 {
228 pControl->SetStringSelection(* m_pString);
229 }
7cc3cec9
JS
230 else
231 {
232 pControl->SetValue(* m_pString);
233 }
f6bcfd97
BP
234 return TRUE;
235 }
e90196a5 236 } else
ce4169a4 237#endif
a55e0ebc
JS
238#if wxUSE_CHOICE
239 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
f6bcfd97 240 {
a55e0ebc 241 wxChoice* pControl = (wxChoice*) m_validatorWindow;
f6bcfd97
BP
242 if (m_pInt)
243 {
244 pControl->SetSelection(*m_pInt) ;
245 return TRUE;
246 }
247 else if (m_pString)
248 {
249 if (pControl->FindString(* m_pString) > -1)
250 {
251 pControl->SetStringSelection(* m_pString);
252 }
253 return TRUE;
254 }
255 } else
a55e0ebc 256#endif
e90196a5 257 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef 258 {
e90196a5 259 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
f6bcfd97
BP
260 if (m_pString)
261 {
262 pControl->SetLabel(*m_pString) ;
263 return TRUE;
264 }
3ca6a5f0 265 } else
e90196a5 266 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef 267 {
e90196a5 268 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
f6bcfd97
BP
269 if (m_pString)
270 {
271 pControl->SetValue(*m_pString) ;
272 return TRUE;
273 }
274 else if (m_pInt)
275 {
276 wxString str;
66b3ec7f 277 str.Printf(wxT("%d"), *m_pInt);
f6bcfd97
BP
278 pControl->SetValue(str);
279 return TRUE;
280 }
e90196a5 281 } else
1e6feb95 282 // array controls
3ca6a5f0 283#if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
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++ )
294 pControl->Check(i, FALSE);
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
301 return TRUE;
302 }
3ca6a5f0 303 else
1e6feb95
VZ
304 return FALSE;
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
3ca6a5f0
BP
324 return TRUE;
325 }
e90196a5 326 } else
dcf924a3 327#endif
3ca6a5f0 328 ; // to match the last 'else' above
89c684ef
JS
329
330 // unrecognized control, or bad pointer
331 return FALSE;
332}
333
e90196a5 334// Called to transfer data from the window
89c684ef
JS
335bool wxGenericValidator::TransferFromWindow(void)
336{
337 if ( !m_validatorWindow )
338 return FALSE;
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() ;
348 return TRUE;
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() ;
359 return TRUE;
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() ;
371 return TRUE;
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() ;
382 return TRUE;
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() ;
393 return TRUE;
394 }
dcf924a3
RR
395 } else
396#endif
5e679c40 397#if wxUSE_SPINCTRL && !defined(__WIN16__) && !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();
404 return TRUE;
405 }
406 } else
407#endif
408#if wxUSE_SPINBTN && !defined(__WIN16__)
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() ;
415 return TRUE;
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() ;
e90196a5
RR
426 return TRUE;
427 }
428 } else
750b78ba 429#endif
89c684ef 430 // string controls
dcf924a3 431 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef
JS
432 {
433 wxButton* pControl = (wxButton*) m_validatorWindow;
3ca6a5f0 434 if (m_pString)
89c684ef
JS
435 {
436 *m_pString = pControl->GetLabel() ;
437 return TRUE;
438 }
439 }
913df6f2 440 else
dcf924a3
RR
441#if wxUSE_COMBOBOX
442 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef
JS
443 {
444 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
3ca6a5f0 445 if (m_pInt)
89c684ef 446 {
3ca6a5f0 447 *m_pInt = pControl->GetSelection() ;
89c684ef
JS
448 return TRUE;
449 }
f6bcfd97
BP
450 else if (m_pString)
451 {
d491523b 452 *m_pString = pControl->GetValue();
f6bcfd97
BP
453 return TRUE;
454 }
913df6f2 455 } else
dcf924a3 456#endif
ce4169a4 457#if wxUSE_CHOICE
dcf924a3 458 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
89c684ef
JS
459 {
460 wxChoice* pControl = (wxChoice*) m_validatorWindow;
3ca6a5f0 461 if (m_pInt)
89c684ef
JS
462 {
463 *m_pInt = pControl->GetSelection() ;
464 return TRUE;
465 }
f6bcfd97
BP
466 else if (m_pString)
467 {
468 *m_pString = pControl->GetStringSelection();
469 return TRUE;
470 }
913df6f2 471 } else
ce4169a4 472#endif
dcf924a3 473 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef
JS
474 {
475 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
3ca6a5f0 476 if (m_pString)
89c684ef
JS
477 {
478 *m_pString = pControl->GetLabel() ;
479 return TRUE;
480 }
913df6f2 481 } else
dcf924a3 482 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef
JS
483 {
484 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
3ca6a5f0 485 if (m_pString)
89c684ef
JS
486 {
487 *m_pString = pControl->GetValue() ;
488 return TRUE;
489 }
f6bcfd97
BP
490 else if (m_pInt)
491 {
66b3ec7f 492 *m_pInt = wxAtoi(pControl->GetValue());
f6bcfd97
BP
493 return TRUE;
494 }
dcf924a3 495 } else
1e6feb95 496 // array controls
e90196a5 497#if wxUSE_CHECKLISTBOX
750b78ba 498#ifndef __WIN16__
1e6feb95 499 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
dcf924a3 500 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef
JS
501 {
502 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
1e6feb95 503 if (m_pArrayInt)
89c684ef
JS
504 {
505 // clear our array
506 m_pArrayInt->Clear();
1e6feb95 507
89c684ef 508 // add each selected item to our array
1e6feb95
VZ
509 size_t i,
510 count = pControl->GetCount();
511 for ( i = 0; i < count; i++ )
512 {
89c684ef
JS
513 if (pControl->IsChecked(i))
514 m_pArrayInt->Add(i);
1e6feb95
VZ
515 }
516
89c684ef
JS
517 return TRUE;
518 }
1e6feb95
VZ
519 else
520 return FALSE;
dcf924a3
RR
521 } else
522#endif
750b78ba 523#endif
dcf924a3
RR
524#if wxUSE_LISTBOX
525 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef
JS
526 {
527 wxListBox* pControl = (wxListBox*) m_validatorWindow;
3ca6a5f0 528 if (m_pArrayInt)
89c684ef
JS
529 {
530 // clear our array
531 m_pArrayInt->Clear();
1e6feb95 532
89c684ef 533 // add each selected item to our array
1e6feb95
VZ
534 size_t i,
535 count = pControl->GetCount();
536 for ( i = 0; i < count; i++ )
537 {
89c684ef
JS
538 if (pControl->Selected(i))
539 m_pArrayInt->Add(i);
1e6feb95
VZ
540 }
541
89c684ef
JS
542 return TRUE;
543 }
dcf924a3
RR
544 } else
545#endif
89c684ef
JS
546
547 // unrecognized control, or bad pointer
dcf924a3 548 return FALSE;
89c684ef
JS
549 return FALSE;
550}
551
552/*
553 Called by constructors to initialize ALL data members
554*/
555void wxGenericValidator::Initialize()
556{
ce4169a4
RR
557 m_pBool = 0;
558 m_pInt = 0;
559 m_pString = 0;
560 m_pArrayInt = 0;
89c684ef
JS
561}
562
ce4169a4
RR
563#endif
564 // wxUSE_VALIDATORS
913df6f2 565