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