]> git.saurik.com Git - wxWidgets.git/blame - src/common/valgen.cpp
May as well also add a GTK_CLASS_TYPE macro for compatibility.
[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
JS
8// Copyright: (c) 1999 Kevin Smith
9// Licence: wxWindows licence
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
750b78ba 47#ifndef __WIN16__
ce4169a4 48 #include "wx/spinbutt.h"
f6bcfd97 49#if wxUSE_CHECKLISTBOX
ce4169a4 50 #include "wx/checklst.h"
750b78ba 51#endif
f6bcfd97 52#endif
89c684ef
JS
53
54#include "wx/valgen.h"
55
56wxGenericValidator::wxGenericValidator(bool *val)
57{
ce4169a4
RR
58 Initialize();
59 m_pBool = val;
89c684ef
JS
60}
61
62wxGenericValidator::wxGenericValidator(int *val)
63{
ce4169a4
RR
64 Initialize();
65 m_pInt = val;
89c684ef
JS
66}
67
68wxGenericValidator::wxGenericValidator(wxString *val)
69{
ce4169a4
RR
70 Initialize();
71 m_pString = val;
89c684ef
JS
72}
73
74wxGenericValidator::wxGenericValidator(wxArrayInt *val)
75{
ce4169a4
RR
76 Initialize();
77 m_pArrayInt = val;
89c684ef
JS
78}
79
80wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
81{
ce4169a4 82 Copy(val);
89c684ef
JS
83}
84
85bool wxGenericValidator::Copy(const wxGenericValidator& val)
86{
e90196a5 87 wxValidator::Copy(val);
89c684ef 88
e90196a5
RR
89 m_pBool = val.m_pBool;
90 m_pInt = val.m_pInt;
91 m_pString = val.m_pString;
92 m_pArrayInt = val.m_pArrayInt;
89c684ef 93
e90196a5 94 return TRUE;
89c684ef
JS
95}
96
97wxGenericValidator::~wxGenericValidator()
98{
99}
100
101// Called to transfer data to the window
102bool wxGenericValidator::TransferToWindow(void)
103{
e90196a5
RR
104 if ( !m_validatorWindow )
105 return FALSE;
89c684ef 106
e90196a5 107 // bool controls
dcf924a3 108#if wxUSE_CHECKBOX
e90196a5 109 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
89c684ef 110 {
e90196a5
RR
111 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
112 if (m_pBool)
113 {
114 pControl->SetValue(*m_pBool);
115 return TRUE;
116 }
117 } else
dcf924a3
RR
118#endif
119#if wxUSE_RADIOBTN
e90196a5 120 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef 121 {
e90196a5
RR
122 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
123 if (m_pBool)
124 {
125 pControl->SetValue(*m_pBool) ;
126 return TRUE;
127 }
128 } else
dcf924a3 129#endif
e90196a5
RR
130
131 // int controls
dcf924a3 132#if wxUSE_GAUGE
e90196a5 133 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef 134 {
e90196a5
RR
135 wxGauge* pControl = (wxGauge*) m_validatorWindow;
136 if (m_pInt)
137 {
138 pControl->SetValue(*m_pInt);
139 return TRUE;
140 }
141 } else
dcf924a3
RR
142#endif
143#if wxUSE_RADIOBOX
e90196a5 144 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef 145 {
e90196a5
RR
146 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
147 if (m_pInt)
148 {
149 pControl->SetSelection(*m_pInt) ;
150 return TRUE;
151 }
152 } else
dcf924a3
RR
153#endif
154#if wxUSE_SCROLLBAR
e90196a5 155 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef 156 {
e90196a5
RR
157 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
158 if (m_pInt)
159 {
160 pControl->SetThumbPosition(*m_pInt) ;
161 return TRUE;
162 }
163 } else
dcf924a3
RR
164#endif
165#if wxUSE_SPINBTN
750b78ba 166#ifndef __WIN16__
e90196a5 167 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef 168 {
e90196a5
RR
169 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
170 if (m_pInt)
171 {
172 pControl->SetValue(*m_pInt) ;
173 return TRUE;
174 }
175 } else
dcf924a3 176#endif
750b78ba 177#endif
e90196a5
RR
178#if wxUSE_SLIDER
179 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
180 {
181 wxSlider* pControl = (wxSlider*) m_validatorWindow;
182 if (m_pInt)
183 {
184 pControl->SetValue(*m_pInt) ;
185 return TRUE;
186 }
187 } else
188#endif
189
89c684ef 190 // string controls
e90196a5
RR
191#if 1
192 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef 193 {
e90196a5 194 wxButton* pControl = (wxButton*) m_validatorWindow;
89c684ef 195 if (m_pString)
e90196a5
RR
196 {
197 pControl->SetLabel(*m_pString) ;
198 return TRUE;
199 }
200 } else
201#endif
202#if wxUSE_COMBOBOX
203 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef 204 {
e90196a5
RR
205 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
206 if (m_pString)
207 {
208 pControl->SetValue(*m_pString) ;
209 return TRUE;
210 }
211 } else
dcf924a3 212#endif
ce4169a4 213#if wxUSE_CHOICE
e90196a5 214 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
89c684ef 215 {
e90196a5 216 wxChoice* pControl = (wxChoice*) 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
f6bcfd97
BP
232 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
233 {
234 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
235 if (m_pInt)
236 {
237 pControl->SetSelection(*m_pInt) ;
238 return TRUE;
239 }
240 else if (m_pString)
241 {
242 if (pControl->FindString(* m_pString) > -1)
243 {
244 pControl->SetStringSelection(* m_pString);
245 }
246 return TRUE;
247 }
248 } else
e90196a5 249 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef 250 {
e90196a5 251 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
f6bcfd97
BP
252 if (m_pString)
253 {
254 pControl->SetLabel(*m_pString) ;
255 return TRUE;
256 }
e90196a5
RR
257 } else
258 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef 259 {
e90196a5 260 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
f6bcfd97
BP
261 if (m_pString)
262 {
263 pControl->SetValue(*m_pString) ;
264 return TRUE;
265 }
266 else if (m_pInt)
267 {
268 wxString str;
66b3ec7f 269 str.Printf(wxT("%d"), *m_pInt);
f6bcfd97
BP
270 pControl->SetValue(str);
271 return TRUE;
272 }
e90196a5 273 } else
dcf924a3 274#if wxUSE_CHECKLISTBOX
750b78ba 275#ifndef __WIN16__
89c684ef
JS
276 // array controls
277 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
278 // MUST come first:
dcf924a3 279 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef
JS
280 {
281 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
282 if (m_pArrayInt)
283 {
284 // clear all selections
285 int i;
286 for (i = 0 ; i < pControl->Number(); ++i)
287 pControl->Check(i, FALSE);
288 // select each item in our array
289 unsigned u;
290 for (u = 0; u < m_pArrayInt->Count(); ++u)
291 pControl->Check(m_pArrayInt->Item(u));
292 return TRUE;
293 }
294 else
295 return FALSE;
dcf924a3 296 } else
750b78ba 297#endif
dcf924a3
RR
298#endif
299#if wxUSE_LISTBOX
e90196a5 300 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef 301 {
e90196a5
RR
302 wxListBox* pControl = (wxListBox*) m_validatorWindow;
303 if (m_pArrayInt)
304 {
305 // clear all selections
306 int i;
307 for (i = 0 ; i < pControl->Number(); ++i)
308 pControl->Deselect(i);
309 // select each item in our array
310 unsigned u;
311 for (u = 0; u < m_pArrayInt->Count(); ++u)
312 pControl->SetSelection(m_pArrayInt->Item(u));
313 return TRUE;
314 }
315 } else
dcf924a3 316#endif
89c684ef
JS
317
318 // unrecognized control, or bad pointer
dcf924a3 319 return FALSE;
89c684ef
JS
320 return FALSE;
321}
322
e90196a5 323// Called to transfer data from the window
89c684ef
JS
324bool wxGenericValidator::TransferFromWindow(void)
325{
326 if ( !m_validatorWindow )
327 return FALSE;
328
329 // bool controls
dcf924a3 330#if wxUSE_CHECKBOX
89c684ef
JS
331 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
332 {
333 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
334 if (m_pBool)
335 {
336 *m_pBool = pControl->GetValue() ;
337 return TRUE;
338 }
913df6f2 339 } else
dcf924a3
RR
340#endif
341#if wxUSE_RADIOBTN
342 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef
JS
343 {
344 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
345 if (m_pBool)
346 {
347 *m_pBool = pControl->GetValue() ;
348 return TRUE;
349 }
dcf924a3
RR
350 } else
351#endif
89c684ef 352 // int controls
dcf924a3
RR
353#if wxUSE_GAUGE
354 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef
JS
355 {
356 wxGauge* pControl = (wxGauge*) m_validatorWindow;
357 if (m_pInt)
358 {
359 *m_pInt = pControl->GetValue() ;
360 return TRUE;
361 }
913df6f2 362 } else
dcf924a3
RR
363#endif
364#if wxUSE_RADIOBOX
365 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef
JS
366 {
367 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
368 if (m_pInt)
369 {
370 *m_pInt = pControl->GetSelection() ;
371 return TRUE;
372 }
913df6f2 373 } else
dcf924a3
RR
374#endif
375#if wxUSE_SCROLLBAR
376 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef
JS
377 {
378 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
379 if (m_pInt)
380 {
381 *m_pInt = pControl->GetThumbPosition() ;
382 return TRUE;
383 }
dcf924a3
RR
384 } else
385#endif
386#if wxUSE_SPINBTN
750b78ba 387#ifndef __WIN16__
dcf924a3 388 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef
JS
389 {
390 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
391 if (m_pInt)
392 {
393 *m_pInt = pControl->GetValue() ;
394 return TRUE;
395 }
dcf924a3
RR
396 } else
397#endif
e90196a5
RR
398#endif
399#if wxUSE_SLIDER
400 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
401 {
402 wxSlider* pControl = (wxSlider*) m_validatorWindow;
403 if (m_pInt)
404 {
405 pControl->SetValue(*m_pInt) ;
406 return TRUE;
407 }
408 } else
750b78ba 409#endif
89c684ef 410 // string controls
dcf924a3 411 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef
JS
412 {
413 wxButton* pControl = (wxButton*) m_validatorWindow;
414 if (m_pString)
415 {
416 *m_pString = pControl->GetLabel() ;
417 return TRUE;
418 }
419 }
913df6f2 420 else
dcf924a3
RR
421#if wxUSE_COMBOBOX
422 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef
JS
423 {
424 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
425 if (m_pString)
426 {
427 *m_pString = pControl->GetValue() ;
428 return TRUE;
429 }
f6bcfd97
BP
430 else if (m_pString)
431 {
432 *m_pString = pControl->GetStringSelection();
433 return TRUE;
434 }
913df6f2 435 } else
dcf924a3 436#endif
ce4169a4 437#if wxUSE_CHOICE
dcf924a3 438 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
89c684ef
JS
439 {
440 wxChoice* pControl = (wxChoice*) m_validatorWindow;
441 if (m_pInt)
442 {
443 *m_pInt = pControl->GetSelection() ;
444 return TRUE;
445 }
f6bcfd97
BP
446 else if (m_pString)
447 {
448 *m_pString = pControl->GetStringSelection();
449 return TRUE;
450 }
913df6f2 451 } else
ce4169a4 452#endif
f6bcfd97
BP
453 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
454 {
455 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
456 if (m_pInt)
457 {
458 *m_pInt = pControl->GetSelection() ;
459 return TRUE;
460 }
461 else if (m_pString)
462 {
463 *m_pString = pControl->GetStringSelection();
464 return TRUE;
465 }
466 } else
dcf924a3 467 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef
JS
468 {
469 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
470 if (m_pString)
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;
479 if (m_pString)
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
e90196a5 490#if wxUSE_CHECKLISTBOX
750b78ba 491#ifndef __WIN16__
89c684ef
JS
492 // array controls
493 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
494 // MUST come first:
dcf924a3 495 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef
JS
496 {
497 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
498 if (m_pArrayInt)
499 {
500 // clear our array
501 m_pArrayInt->Clear();
502 // add each selected item to our array
503 int i;
504 for (i = 0 ; i < pControl->Number(); ++i)
505 if (pControl->IsChecked(i))
506 m_pArrayInt->Add(i);
507 return TRUE;
508 }
509 else
510 return FALSE;
dcf924a3
RR
511 } else
512#endif
750b78ba 513#endif
dcf924a3
RR
514#if wxUSE_LISTBOX
515 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef
JS
516 {
517 wxListBox* pControl = (wxListBox*) m_validatorWindow;
518 if (m_pArrayInt)
519 {
520 // clear our array
521 m_pArrayInt->Clear();
522 // add each selected item to our array
523 int i;
524 for (i = 0 ; i < pControl->Number(); ++i)
525 if (pControl->Selected(i))
526 m_pArrayInt->Add(i);
527 return TRUE;
528 }
dcf924a3
RR
529 } else
530#endif
89c684ef
JS
531
532 // unrecognized control, or bad pointer
dcf924a3 533 return FALSE;
89c684ef
JS
534 return FALSE;
535}
536
537/*
538 Called by constructors to initialize ALL data members
539*/
540void wxGenericValidator::Initialize()
541{
ce4169a4
RR
542 m_pBool = 0;
543 m_pInt = 0;
544 m_pString = 0;
545 m_pArrayInt = 0;
89c684ef
JS
546}
547
ce4169a4
RR
548#endif
549 // wxUSE_VALIDATORS
913df6f2 550