]> git.saurik.com Git - wxWidgets.git/blame - src/common/valgen.cpp
Applied patch [ 1192917 ] Grids with no row or col headers cause AV under wxGTK
[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
001e76a9 255#if wxUSE_STATTEXT
e90196a5 256 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef 257 {
e90196a5 258 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
f6bcfd97
BP
259 if (m_pString)
260 {
261 pControl->SetLabel(*m_pString) ;
cab1a605 262 return true;
f6bcfd97 263 }
3ca6a5f0 264 } else
001e76a9 265#endif
d7260478 266#if wxUSE_TEXTCTRL
e90196a5 267 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef 268 {
e90196a5 269 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
f6bcfd97
BP
270 if (m_pString)
271 {
272 pControl->SetValue(*m_pString) ;
cab1a605 273 return true;
f6bcfd97
BP
274 }
275 else if (m_pInt)
276 {
277 wxString str;
66b3ec7f 278 str.Printf(wxT("%d"), *m_pInt);
f6bcfd97 279 pControl->SetValue(str);
cab1a605 280 return true;
f6bcfd97 281 }
e90196a5 282 } else
d7260478 283#endif
1e6feb95 284 // array controls
3a5bcc4d 285#if wxUSE_CHECKLISTBOX
1e6feb95
VZ
286 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
287 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef 288 {
1e6feb95
VZ
289 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
290 if (m_pArrayInt)
291 {
292 // clear all selections
293 size_t i,
294 count = pControl->GetCount();
295 for ( i = 0 ; i < count; i++ )
cab1a605 296 pControl->Check(i, false);
1e6feb95
VZ
297
298 // select each item in our array
299 count = m_pArrayInt->GetCount();
300 for ( i = 0 ; i < count; i++ )
301 pControl->Check(m_pArrayInt->Item(i));
302
cab1a605 303 return true;
1e6feb95 304 }
3ca6a5f0 305 else
cab1a605 306 return false;
1e6feb95 307 } else
750b78ba 308#endif
dcf924a3 309#if wxUSE_LISTBOX
e90196a5 310 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef 311 {
e90196a5 312 wxListBox* pControl = (wxListBox*) m_validatorWindow;
3ca6a5f0
BP
313 if (m_pArrayInt)
314 {
315 // clear all selections
1e6feb95
VZ
316 size_t i,
317 count = pControl->GetCount();
318 for ( i = 0 ; i < count; i++ )
3ca6a5f0 319 pControl->Deselect(i);
1e6feb95 320
3ca6a5f0 321 // select each item in our array
1e6feb95
VZ
322 count = m_pArrayInt->GetCount();
323 for ( i = 0 ; i < count; i++ )
324 pControl->SetSelection(m_pArrayInt->Item(i));
325
cab1a605 326 return true;
3ca6a5f0 327 }
e90196a5 328 } else
dcf924a3 329#endif
3ca6a5f0 330 ; // to match the last 'else' above
89c684ef
JS
331
332 // unrecognized control, or bad pointer
cab1a605 333 return false;
89c684ef
JS
334}
335
e90196a5 336// Called to transfer data from the window
89c684ef
JS
337bool wxGenericValidator::TransferFromWindow(void)
338{
339 if ( !m_validatorWindow )
cab1a605 340 return false;
89c684ef
JS
341
342 // bool controls
dcf924a3 343#if wxUSE_CHECKBOX
89c684ef
JS
344 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
345 {
346 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
3ca6a5f0 347 if (m_pBool)
89c684ef
JS
348 {
349 *m_pBool = pControl->GetValue() ;
cab1a605 350 return true;
89c684ef 351 }
913df6f2 352 } else
dcf924a3
RR
353#endif
354#if wxUSE_RADIOBTN
355 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef
JS
356 {
357 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
3ca6a5f0 358 if (m_pBool)
89c684ef
JS
359 {
360 *m_pBool = pControl->GetValue() ;
cab1a605 361 return true;
89c684ef 362 }
dcf924a3
RR
363 } else
364#endif
89c684ef 365 // int controls
dcf924a3
RR
366#if wxUSE_GAUGE
367 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef
JS
368 {
369 wxGauge* pControl = (wxGauge*) m_validatorWindow;
3ca6a5f0 370 if (m_pInt)
89c684ef
JS
371 {
372 *m_pInt = pControl->GetValue() ;
cab1a605 373 return true;
89c684ef 374 }
913df6f2 375 } else
dcf924a3
RR
376#endif
377#if wxUSE_RADIOBOX
378 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef
JS
379 {
380 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
3ca6a5f0 381 if (m_pInt)
89c684ef
JS
382 {
383 *m_pInt = pControl->GetSelection() ;
cab1a605 384 return true;
89c684ef 385 }
913df6f2 386 } else
dcf924a3
RR
387#endif
388#if wxUSE_SCROLLBAR
389 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef
JS
390 {
391 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
3ca6a5f0 392 if (m_pInt)
89c684ef
JS
393 {
394 *m_pInt = pControl->GetThumbPosition() ;
cab1a605 395 return true;
89c684ef 396 }
dcf924a3
RR
397 } else
398#endif
3a5bcc4d 399#if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
a55e0ebc
JS
400 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
401 {
402 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
403 if (m_pInt)
404 {
405 *m_pInt=pControl->GetValue();
cab1a605 406 return true;
a55e0ebc
JS
407 }
408 } else
409#endif
3a5bcc4d 410#if wxUSE_SPINBTN
dcf924a3 411 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef
JS
412 {
413 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
3ca6a5f0 414 if (m_pInt)
89c684ef
JS
415 {
416 *m_pInt = pControl->GetValue() ;
cab1a605 417 return true;
89c684ef 418 }
dcf924a3
RR
419 } else
420#endif
e90196a5
RR
421#if wxUSE_SLIDER
422 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
423 {
424 wxSlider* pControl = (wxSlider*) m_validatorWindow;
425 if (m_pInt)
426 {
ca36c2cc 427 *m_pInt = pControl->GetValue() ;
cab1a605 428 return true;
e90196a5
RR
429 }
430 } else
750b78ba 431#endif
89c684ef 432 // string controls
d7260478 433#if wxUSE_BUTTON
dcf924a3 434 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef
JS
435 {
436 wxButton* pControl = (wxButton*) m_validatorWindow;
3ca6a5f0 437 if (m_pString)
89c684ef
JS
438 {
439 *m_pString = pControl->GetLabel() ;
cab1a605 440 return true;
89c684ef 441 }
d7260478
JS
442 } else
443#endif
dcf924a3
RR
444#if wxUSE_COMBOBOX
445 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef
JS
446 {
447 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
3ca6a5f0 448 if (m_pInt)
89c684ef 449 {
3ca6a5f0 450 *m_pInt = pControl->GetSelection() ;
cab1a605 451 return true;
89c684ef 452 }
f6bcfd97
BP
453 else if (m_pString)
454 {
780fb83c
JS
455 if (m_validatorWindow->GetWindowStyle() & wxCB_READONLY)
456 *m_pString = pControl->GetStringSelection();
457 else
458 *m_pString = pControl->GetValue();
cab1a605 459 return true;
f6bcfd97 460 }
913df6f2 461 } else
dcf924a3 462#endif
ce4169a4 463#if wxUSE_CHOICE
dcf924a3 464 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
89c684ef
JS
465 {
466 wxChoice* pControl = (wxChoice*) m_validatorWindow;
3ca6a5f0 467 if (m_pInt)
89c684ef
JS
468 {
469 *m_pInt = pControl->GetSelection() ;
cab1a605 470 return true;
89c684ef 471 }
f6bcfd97
BP
472 else if (m_pString)
473 {
474 *m_pString = pControl->GetStringSelection();
cab1a605 475 return true;
f6bcfd97 476 }
913df6f2 477 } else
ce4169a4 478#endif
001e76a9 479#if wxUSE_STATTEXT
dcf924a3 480 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef
JS
481 {
482 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
3ca6a5f0 483 if (m_pString)
89c684ef
JS
484 {
485 *m_pString = pControl->GetLabel() ;
cab1a605 486 return true;
89c684ef 487 }
913df6f2 488 } else
001e76a9 489#endif
d7260478 490#if wxUSE_TEXTCTRL
dcf924a3 491 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef
JS
492 {
493 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
3ca6a5f0 494 if (m_pString)
89c684ef
JS
495 {
496 *m_pString = pControl->GetValue() ;
cab1a605 497 return true;
89c684ef 498 }
f6bcfd97
BP
499 else if (m_pInt)
500 {
66b3ec7f 501 *m_pInt = wxAtoi(pControl->GetValue());
cab1a605 502 return true;
f6bcfd97 503 }
dcf924a3 504 } else
d7260478 505#endif
1e6feb95 506 // array controls
e90196a5 507#if wxUSE_CHECKLISTBOX
1e6feb95 508 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
dcf924a3 509 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef
JS
510 {
511 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
1e6feb95 512 if (m_pArrayInt)
89c684ef
JS
513 {
514 // clear our array
515 m_pArrayInt->Clear();
1e6feb95 516
89c684ef 517 // add each selected item to our array
1e6feb95
VZ
518 size_t i,
519 count = pControl->GetCount();
520 for ( i = 0; i < count; i++ )
521 {
89c684ef
JS
522 if (pControl->IsChecked(i))
523 m_pArrayInt->Add(i);
1e6feb95
VZ
524 }
525
cab1a605 526 return true;
89c684ef 527 }
1e6feb95 528 else
cab1a605 529 return false;
dcf924a3
RR
530 } else
531#endif
dcf924a3
RR
532#if wxUSE_LISTBOX
533 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef
JS
534 {
535 wxListBox* pControl = (wxListBox*) m_validatorWindow;
3ca6a5f0 536 if (m_pArrayInt)
89c684ef
JS
537 {
538 // clear our array
539 m_pArrayInt->Clear();
1e6feb95 540
89c684ef 541 // add each selected item to our array
1e6feb95
VZ
542 size_t i,
543 count = pControl->GetCount();
544 for ( i = 0; i < count; i++ )
545 {
89c684ef
JS
546 if (pControl->Selected(i))
547 m_pArrayInt->Add(i);
1e6feb95
VZ
548 }
549
cab1a605 550 return true;
89c684ef 551 }
dcf924a3
RR
552 } else
553#endif
89c684ef
JS
554
555 // unrecognized control, or bad pointer
cab1a605
WS
556 return false;
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