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