]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/valgen.cpp
Corrected dangerous cast.
[wxWidgets.git] / src / common / valgen.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: valgen.cpp
3// Purpose: wxGenericValidator class
4// Author: Kevin Smith
5// Modified by:
6// Created: Jan 22 1999
7// RCS-ID:
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__
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
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"
44 #include "wx/slider.h"
45#endif
46
47#if wxUSE_SPINCTRL && !defined(__WIN16__)
48 #include "wx/spinctrl.h"
49#endif
50#if wxUSE_SPINBTN && !defined(__WIN16__)
51 #include "wx/spinbutt.h"
52#endif
53#if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
54 #include "wx/checklst.h"
55#endif
56
57#include "wx/valgen.h"
58
59wxGenericValidator::wxGenericValidator(bool *val)
60{
61 Initialize();
62 m_pBool = val;
63}
64
65wxGenericValidator::wxGenericValidator(int *val)
66{
67 Initialize();
68 m_pInt = val;
69}
70
71wxGenericValidator::wxGenericValidator(wxString *val)
72{
73 Initialize();
74 m_pString = val;
75}
76
77wxGenericValidator::wxGenericValidator(wxArrayInt *val)
78{
79 Initialize();
80 m_pArrayInt = val;
81}
82
83wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
84 : wxValidator()
85{
86 Copy(val);
87}
88
89bool wxGenericValidator::Copy(const wxGenericValidator& val)
90{
91 wxValidator::Copy(val);
92
93 m_pBool = val.m_pBool;
94 m_pInt = val.m_pInt;
95 m_pString = val.m_pString;
96 m_pArrayInt = val.m_pArrayInt;
97
98 return TRUE;
99}
100
101wxGenericValidator::~wxGenericValidator()
102{
103}
104
105// Called to transfer data to the window
106bool wxGenericValidator::TransferToWindow(void)
107{
108 if ( !m_validatorWindow )
109 return FALSE;
110
111 // bool controls
112#if wxUSE_CHECKBOX
113 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
114 {
115 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
116 if (m_pBool)
117 {
118 pControl->SetValue(*m_pBool);
119 return TRUE;
120 }
121 } else
122#endif
123#if wxUSE_RADIOBTN
124 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
125 {
126 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
127 if (m_pBool)
128 {
129 pControl->SetValue(*m_pBool) ;
130 return TRUE;
131 }
132 } else
133#endif
134
135 // int controls
136#if wxUSE_GAUGE
137 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
138 {
139 wxGauge* pControl = (wxGauge*) m_validatorWindow;
140 if (m_pInt)
141 {
142 pControl->SetValue(*m_pInt);
143 return TRUE;
144 }
145 } else
146#endif
147#if wxUSE_RADIOBOX
148 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
149 {
150 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
151 if (m_pInt)
152 {
153 pControl->SetSelection(*m_pInt) ;
154 return TRUE;
155 }
156 } else
157#endif
158#if wxUSE_SCROLLBAR
159 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
160 {
161 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
162 if (m_pInt)
163 {
164 pControl->SetThumbPosition(*m_pInt) ;
165 return TRUE;
166 }
167 } else
168#endif
169#if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
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__)
181 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
182 {
183 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
184 if (m_pInt)
185 {
186 pControl->SetValue(*m_pInt) ;
187 return TRUE;
188 }
189 } else
190#endif
191#if wxUSE_SLIDER
192 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
193 {
194 wxSlider* pControl = (wxSlider*) m_validatorWindow;
195 if (m_pInt)
196 {
197 pControl->SetValue(*m_pInt) ;
198 return TRUE;
199 }
200 } else
201#endif
202
203 // string controls
204 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
205 {
206 wxButton* pControl = (wxButton*) m_validatorWindow;
207 if (m_pString)
208 {
209 pControl->SetLabel(*m_pString) ;
210 return TRUE;
211 }
212 } else
213#if wxUSE_COMBOBOX
214 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
215 {
216 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
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 else
229 {
230 pControl->SetValue(* m_pString);
231 }
232 return TRUE;
233 }
234 } else
235#endif
236#if wxUSE_CHOICE
237 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
238 {
239 wxChoice* pControl = (wxChoice*) m_validatorWindow;
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
254#endif
255 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
256 {
257 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
258 if (m_pString)
259 {
260 pControl->SetLabel(*m_pString) ;
261 return TRUE;
262 }
263 } else
264 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
265 {
266 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
267 if (m_pString)
268 {
269 pControl->SetValue(*m_pString) ;
270 return TRUE;
271 }
272 else if (m_pInt)
273 {
274 wxString str;
275 str.Printf(wxT("%d"), *m_pInt);
276 pControl->SetValue(str);
277 return TRUE;
278 }
279 } else
280 // array controls
281#if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
282 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
283 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
284 {
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 }
301 else
302 return FALSE;
303 } else
304#endif
305#if wxUSE_LISTBOX
306 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
307 {
308 wxListBox* pControl = (wxListBox*) m_validatorWindow;
309 if (m_pArrayInt)
310 {
311 // clear all selections
312 size_t i,
313 count = pControl->GetCount();
314 for ( i = 0 ; i < count; i++ )
315 pControl->Deselect(i);
316
317 // select each item in our array
318 count = m_pArrayInt->GetCount();
319 for ( i = 0 ; i < count; i++ )
320 pControl->SetSelection(m_pArrayInt->Item(i));
321
322 return TRUE;
323 }
324 } else
325#endif
326 ; // to match the last 'else' above
327
328 // unrecognized control, or bad pointer
329 return FALSE;
330}
331
332// Called to transfer data from the window
333bool wxGenericValidator::TransferFromWindow(void)
334{
335 if ( !m_validatorWindow )
336 return FALSE;
337
338 // bool controls
339#if wxUSE_CHECKBOX
340 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
341 {
342 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
343 if (m_pBool)
344 {
345 *m_pBool = pControl->GetValue() ;
346 return TRUE;
347 }
348 } else
349#endif
350#if wxUSE_RADIOBTN
351 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
352 {
353 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
354 if (m_pBool)
355 {
356 *m_pBool = pControl->GetValue() ;
357 return TRUE;
358 }
359 } else
360#endif
361 // int controls
362#if wxUSE_GAUGE
363 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
364 {
365 wxGauge* pControl = (wxGauge*) m_validatorWindow;
366 if (m_pInt)
367 {
368 *m_pInt = pControl->GetValue() ;
369 return TRUE;
370 }
371 } else
372#endif
373#if wxUSE_RADIOBOX
374 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
375 {
376 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
377 if (m_pInt)
378 {
379 *m_pInt = pControl->GetSelection() ;
380 return TRUE;
381 }
382 } else
383#endif
384#if wxUSE_SCROLLBAR
385 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
386 {
387 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
388 if (m_pInt)
389 {
390 *m_pInt = pControl->GetThumbPosition() ;
391 return TRUE;
392 }
393 } else
394#endif
395#if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
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__)
407 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
408 {
409 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
410 if (m_pInt)
411 {
412 *m_pInt = pControl->GetValue() ;
413 return TRUE;
414 }
415 } else
416#endif
417#if wxUSE_SLIDER
418 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
419 {
420 wxSlider* pControl = (wxSlider*) m_validatorWindow;
421 if (m_pInt)
422 {
423 *m_pInt = pControl->GetValue() ;
424 return TRUE;
425 }
426 } else
427#endif
428 // string controls
429 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
430 {
431 wxButton* pControl = (wxButton*) m_validatorWindow;
432 if (m_pString)
433 {
434 *m_pString = pControl->GetLabel() ;
435 return TRUE;
436 }
437 }
438 else
439#if wxUSE_COMBOBOX
440 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
441 {
442 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
443 if (m_pInt)
444 {
445 *m_pInt = pControl->GetSelection() ;
446 return TRUE;
447 }
448 else if (m_pString)
449 {
450 *m_pString = pControl->GetValue();
451 return TRUE;
452 }
453 } else
454#endif
455#if wxUSE_CHOICE
456 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
457 {
458 wxChoice* pControl = (wxChoice*) m_validatorWindow;
459 if (m_pInt)
460 {
461 *m_pInt = pControl->GetSelection() ;
462 return TRUE;
463 }
464 else if (m_pString)
465 {
466 *m_pString = pControl->GetStringSelection();
467 return TRUE;
468 }
469 } else
470#endif
471 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
472 {
473 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
474 if (m_pString)
475 {
476 *m_pString = pControl->GetLabel() ;
477 return TRUE;
478 }
479 } else
480 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
481 {
482 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
483 if (m_pString)
484 {
485 *m_pString = pControl->GetValue() ;
486 return TRUE;
487 }
488 else if (m_pInt)
489 {
490 *m_pInt = wxAtoi(pControl->GetValue());
491 return TRUE;
492 }
493 } else
494 // array controls
495#if wxUSE_CHECKLISTBOX
496#ifndef __WIN16__
497 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
498 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
499 {
500 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
501 if (m_pArrayInt)
502 {
503 // clear our array
504 m_pArrayInt->Clear();
505
506 // add each selected item to our array
507 size_t i,
508 count = pControl->GetCount();
509 for ( i = 0; i < count; i++ )
510 {
511 if (pControl->IsChecked(i))
512 m_pArrayInt->Add(i);
513 }
514
515 return TRUE;
516 }
517 else
518 return FALSE;
519 } else
520#endif
521#endif
522#if wxUSE_LISTBOX
523 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
524 {
525 wxListBox* pControl = (wxListBox*) m_validatorWindow;
526 if (m_pArrayInt)
527 {
528 // clear our array
529 m_pArrayInt->Clear();
530
531 // add each selected item to our array
532 size_t i,
533 count = pControl->GetCount();
534 for ( i = 0; i < count; i++ )
535 {
536 if (pControl->Selected(i))
537 m_pArrayInt->Add(i);
538 }
539
540 return TRUE;
541 }
542 } else
543#endif
544
545 // unrecognized control, or bad pointer
546 return FALSE;
547 return FALSE;
548}
549
550/*
551 Called by constructors to initialize ALL data members
552*/
553void wxGenericValidator::Initialize()
554{
555 m_pBool = 0;
556 m_pInt = 0;
557 m_pString = 0;
558 m_pArrayInt = 0;
559}
560
561#endif
562 // wxUSE_VALIDATORS
563