]> git.saurik.com Git - wxWidgets.git/blame - src/common/valgen.cpp
don't crash in wx(Flex)GridSizer with division by 0, assert instead; also factored...
[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
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
e90196a5 98 return TRUE;
89c684ef
JS
99}
100
101wxGenericValidator::~wxGenericValidator()
102{
103}
104
105// Called to transfer data to the window
106bool wxGenericValidator::TransferToWindow(void)
107{
e90196a5
RR
108 if ( !m_validatorWindow )
109 return FALSE;
89c684ef 110
e90196a5 111 // bool controls
dcf924a3 112#if wxUSE_CHECKBOX
e90196a5 113 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
89c684ef 114 {
e90196a5
RR
115 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
116 if (m_pBool)
3ca6a5f0
BP
117 {
118 pControl->SetValue(*m_pBool);
119 return TRUE;
120 }
e90196a5 121 } else
dcf924a3
RR
122#endif
123#if wxUSE_RADIOBTN
e90196a5 124 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef 125 {
e90196a5 126 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
3ca6a5f0
BP
127 if (m_pBool)
128 {
129 pControl->SetValue(*m_pBool) ;
130 return TRUE;
131 }
e90196a5 132 } else
dcf924a3 133#endif
e90196a5
RR
134
135 // int controls
dcf924a3 136#if wxUSE_GAUGE
e90196a5 137 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef 138 {
e90196a5 139 wxGauge* pControl = (wxGauge*) m_validatorWindow;
3ca6a5f0
BP
140 if (m_pInt)
141 {
142 pControl->SetValue(*m_pInt);
143 return TRUE;
144 }
e90196a5 145 } else
dcf924a3
RR
146#endif
147#if wxUSE_RADIOBOX
e90196a5 148 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef 149 {
e90196a5 150 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
3ca6a5f0
BP
151 if (m_pInt)
152 {
153 pControl->SetSelection(*m_pInt) ;
154 return TRUE;
155 }
e90196a5 156 } else
dcf924a3
RR
157#endif
158#if wxUSE_SCROLLBAR
e90196a5 159 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef 160 {
e90196a5 161 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
3ca6a5f0
BP
162 if (m_pInt)
163 {
164 pControl->SetThumbPosition(*m_pInt) ;
165 return TRUE;
166 }
e90196a5 167 } else
dcf924a3 168#endif
5e679c40 169#if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
a55e0ebc
JS
170 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
171 {
172 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
173 if (m_pInt)
174 {
175 pControl->SetValue(*m_pInt);
176 return TRUE;
177 }
178 } else
179#endif
180#if wxUSE_SPINBTN && !defined(__WIN16__)
e90196a5 181 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef 182 {
e90196a5 183 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
3ca6a5f0
BP
184 if (m_pInt)
185 {
186 pControl->SetValue(*m_pInt) ;
187 return TRUE;
188 }
e90196a5 189 } else
dcf924a3 190#endif
e90196a5
RR
191#if wxUSE_SLIDER
192 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
193 {
194 wxSlider* pControl = (wxSlider*) m_validatorWindow;
195 if (m_pInt)
196 {
3ca6a5f0
BP
197 pControl->SetValue(*m_pInt) ;
198 return TRUE;
199 }
e90196a5
RR
200 } else
201#endif
202
3ca6a5f0 203 // string controls
e90196a5 204 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef 205 {
e90196a5 206 wxButton* pControl = (wxButton*) m_validatorWindow;
3ca6a5f0
BP
207 if (m_pString)
208 {
209 pControl->SetLabel(*m_pString) ;
210 return TRUE;
211 }
e90196a5 212 } else
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) ;
220 return TRUE;
221 }
222 else if (m_pString)
223 {
224 if (pControl->FindString(* m_pString) > -1)
225 {
226 pControl->SetStringSelection(* m_pString);
227 }
7cc3cec9
JS
228 else
229 {
230 pControl->SetValue(* m_pString);
231 }
f6bcfd97
BP
232 return TRUE;
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) ;
243 return TRUE;
244 }
245 else if (m_pString)
246 {
247 if (pControl->FindString(* m_pString) > -1)
248 {
249 pControl->SetStringSelection(* m_pString);
250 }
251 return TRUE;
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) ;
261 return TRUE;
262 }
3ca6a5f0 263 } else
e90196a5 264 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef 265 {
e90196a5 266 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
f6bcfd97
BP
267 if (m_pString)
268 {
269 pControl->SetValue(*m_pString) ;
270 return TRUE;
271 }
272 else if (m_pInt)
273 {
274 wxString str;
66b3ec7f 275 str.Printf(wxT("%d"), *m_pInt);
f6bcfd97
BP
276 pControl->SetValue(str);
277 return TRUE;
278 }
e90196a5 279 } else
1e6feb95 280 // array controls
3ca6a5f0 281#if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
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++ )
292 pControl->Check(i, FALSE);
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
299 return TRUE;
300 }
3ca6a5f0 301 else
1e6feb95
VZ
302 return FALSE;
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
3ca6a5f0
BP
322 return TRUE;
323 }
e90196a5 324 } else
dcf924a3 325#endif
3ca6a5f0 326 ; // to match the last 'else' above
89c684ef
JS
327
328 // unrecognized control, or bad pointer
329 return FALSE;
330}
331
e90196a5 332// Called to transfer data from the window
89c684ef
JS
333bool wxGenericValidator::TransferFromWindow(void)
334{
335 if ( !m_validatorWindow )
336 return FALSE;
337
338 // bool controls
dcf924a3 339#if wxUSE_CHECKBOX
89c684ef
JS
340 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
341 {
342 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
3ca6a5f0 343 if (m_pBool)
89c684ef
JS
344 {
345 *m_pBool = pControl->GetValue() ;
346 return TRUE;
347 }
913df6f2 348 } else
dcf924a3
RR
349#endif
350#if wxUSE_RADIOBTN
351 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef
JS
352 {
353 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
3ca6a5f0 354 if (m_pBool)
89c684ef
JS
355 {
356 *m_pBool = pControl->GetValue() ;
357 return TRUE;
358 }
dcf924a3
RR
359 } else
360#endif
89c684ef 361 // int controls
dcf924a3
RR
362#if wxUSE_GAUGE
363 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef
JS
364 {
365 wxGauge* pControl = (wxGauge*) m_validatorWindow;
3ca6a5f0 366 if (m_pInt)
89c684ef
JS
367 {
368 *m_pInt = pControl->GetValue() ;
369 return TRUE;
370 }
913df6f2 371 } else
dcf924a3
RR
372#endif
373#if wxUSE_RADIOBOX
374 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef
JS
375 {
376 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
3ca6a5f0 377 if (m_pInt)
89c684ef
JS
378 {
379 *m_pInt = pControl->GetSelection() ;
380 return TRUE;
381 }
913df6f2 382 } else
dcf924a3
RR
383#endif
384#if wxUSE_SCROLLBAR
385 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef
JS
386 {
387 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
3ca6a5f0 388 if (m_pInt)
89c684ef
JS
389 {
390 *m_pInt = pControl->GetThumbPosition() ;
391 return TRUE;
392 }
dcf924a3
RR
393 } else
394#endif
5e679c40 395#if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
a55e0ebc
JS
396 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
397 {
398 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
399 if (m_pInt)
400 {
401 *m_pInt=pControl->GetValue();
402 return TRUE;
403 }
404 } else
405#endif
406#if wxUSE_SPINBTN && !defined(__WIN16__)
dcf924a3 407 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef
JS
408 {
409 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
3ca6a5f0 410 if (m_pInt)
89c684ef
JS
411 {
412 *m_pInt = pControl->GetValue() ;
413 return TRUE;
414 }
dcf924a3
RR
415 } else
416#endif
e90196a5
RR
417#if wxUSE_SLIDER
418 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
419 {
420 wxSlider* pControl = (wxSlider*) m_validatorWindow;
421 if (m_pInt)
422 {
ca36c2cc 423 *m_pInt = pControl->GetValue() ;
e90196a5
RR
424 return TRUE;
425 }
426 } else
750b78ba 427#endif
89c684ef 428 // string controls
dcf924a3 429 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef
JS
430 {
431 wxButton* pControl = (wxButton*) m_validatorWindow;
3ca6a5f0 432 if (m_pString)
89c684ef
JS
433 {
434 *m_pString = pControl->GetLabel() ;
435 return TRUE;
436 }
437 }
913df6f2 438 else
dcf924a3
RR
439#if wxUSE_COMBOBOX
440 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef
JS
441 {
442 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
3ca6a5f0 443 if (m_pInt)
89c684ef 444 {
3ca6a5f0 445 *m_pInt = pControl->GetSelection() ;
89c684ef
JS
446 return TRUE;
447 }
f6bcfd97
BP
448 else if (m_pString)
449 {
d491523b 450 *m_pString = pControl->GetValue();
f6bcfd97
BP
451 return TRUE;
452 }
913df6f2 453 } else
dcf924a3 454#endif
ce4169a4 455#if wxUSE_CHOICE
dcf924a3 456 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
89c684ef
JS
457 {
458 wxChoice* pControl = (wxChoice*) m_validatorWindow;
3ca6a5f0 459 if (m_pInt)
89c684ef
JS
460 {
461 *m_pInt = pControl->GetSelection() ;
462 return TRUE;
463 }
f6bcfd97
BP
464 else if (m_pString)
465 {
466 *m_pString = pControl->GetStringSelection();
467 return TRUE;
468 }
913df6f2 469 } else
ce4169a4 470#endif
dcf924a3 471 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef
JS
472 {
473 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
3ca6a5f0 474 if (m_pString)
89c684ef
JS
475 {
476 *m_pString = pControl->GetLabel() ;
477 return TRUE;
478 }
913df6f2 479 } else
dcf924a3 480 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef
JS
481 {
482 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
3ca6a5f0 483 if (m_pString)
89c684ef
JS
484 {
485 *m_pString = pControl->GetValue() ;
486 return TRUE;
487 }
f6bcfd97
BP
488 else if (m_pInt)
489 {
66b3ec7f 490 *m_pInt = wxAtoi(pControl->GetValue());
f6bcfd97
BP
491 return TRUE;
492 }
dcf924a3 493 } else
1e6feb95 494 // array controls
e90196a5 495#if wxUSE_CHECKLISTBOX
750b78ba 496#ifndef __WIN16__
1e6feb95 497 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
dcf924a3 498 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef
JS
499 {
500 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
1e6feb95 501 if (m_pArrayInt)
89c684ef
JS
502 {
503 // clear our array
504 m_pArrayInt->Clear();
1e6feb95 505
89c684ef 506 // add each selected item to our array
1e6feb95
VZ
507 size_t i,
508 count = pControl->GetCount();
509 for ( i = 0; i < count; i++ )
510 {
89c684ef
JS
511 if (pControl->IsChecked(i))
512 m_pArrayInt->Add(i);
1e6feb95
VZ
513 }
514
89c684ef
JS
515 return TRUE;
516 }
1e6feb95
VZ
517 else
518 return FALSE;
dcf924a3
RR
519 } else
520#endif
750b78ba 521#endif
dcf924a3
RR
522#if wxUSE_LISTBOX
523 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef
JS
524 {
525 wxListBox* pControl = (wxListBox*) m_validatorWindow;
3ca6a5f0 526 if (m_pArrayInt)
89c684ef
JS
527 {
528 // clear our array
529 m_pArrayInt->Clear();
1e6feb95 530
89c684ef 531 // add each selected item to our array
1e6feb95
VZ
532 size_t i,
533 count = pControl->GetCount();
534 for ( i = 0; i < count; i++ )
535 {
89c684ef
JS
536 if (pControl->Selected(i))
537 m_pArrayInt->Add(i);
1e6feb95
VZ
538 }
539
89c684ef
JS
540 return TRUE;
541 }
dcf924a3
RR
542 } else
543#endif
89c684ef
JS
544
545 // unrecognized control, or bad pointer
dcf924a3 546 return FALSE;
89c684ef
JS
547 return FALSE;
548}
549
550/*
551 Called by constructors to initialize ALL data members
552*/
553void wxGenericValidator::Initialize()
554{
ce4169a4
RR
555 m_pBool = 0;
556 m_pInt = 0;
557 m_pString = 0;
558 m_pArrayInt = 0;
89c684ef
JS
559}
560
ce4169a4
RR
561#endif
562 // wxUSE_VALIDATORS
913df6f2 563