]> git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
fix for handling from Francesco
[wxWidgets.git] / src / common / valgen.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/valgen.cpp
3 // Purpose: wxGenericValidator class
4 // Author: Kevin Smith
5 // Modified by:
6 // Created: Jan 22 1999
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Kevin Smith
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_VALIDATORS
20
21 #ifndef WX_PRECOMP
22 #include "wx/dynarray.h"
23 #include "wx/utils.h"
24 #include "wx/intl.h"
25 #include "wx/choice.h"
26 #include "wx/combobox.h"
27 #include "wx/radiobox.h"
28 #include "wx/radiobut.h"
29 #include "wx/checkbox.h"
30 #include "wx/scrolbar.h"
31 #include "wx/gauge.h"
32 #include "wx/stattext.h"
33 #include "wx/textctrl.h"
34 #include "wx/button.h"
35 #include "wx/listbox.h"
36 #include "wx/slider.h"
37 #include "wx/checklst.h"
38 #endif
39
40 #include "wx/spinctrl.h"
41
42 #if wxUSE_SPINBTN
43 #include "wx/spinbutt.h"
44 #endif
45 #if wxUSE_TOGGLEBTN
46 #include "wx/tglbtn.h"
47 #endif
48
49 #include "wx/valgen.h"
50
51 IMPLEMENT_CLASS(wxGenericValidator, wxValidator)
52
53 wxGenericValidator::wxGenericValidator(bool *val)
54 {
55 Initialize();
56 m_pBool = val;
57 }
58
59 wxGenericValidator::wxGenericValidator(int *val)
60 {
61 Initialize();
62 m_pInt = val;
63 }
64
65 wxGenericValidator::wxGenericValidator(wxString *val)
66 {
67 Initialize();
68 m_pString = val;
69 }
70
71 wxGenericValidator::wxGenericValidator(wxArrayInt *val)
72 {
73 Initialize();
74 m_pArrayInt = val;
75 }
76
77 wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
78 : wxValidator()
79 {
80 Copy(val);
81 }
82
83 bool wxGenericValidator::Copy(const wxGenericValidator& val)
84 {
85 wxValidator::Copy(val);
86
87 m_pBool = val.m_pBool;
88 m_pInt = val.m_pInt;
89 m_pString = val.m_pString;
90 m_pArrayInt = val.m_pArrayInt;
91
92 return true;
93 }
94
95 // Called to transfer data to the window
96 bool wxGenericValidator::TransferToWindow(void)
97 {
98 if ( !m_validatorWindow )
99 return false;
100
101 // bool controls
102 #if wxUSE_CHECKBOX
103 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
104 {
105 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
106 if (m_pBool)
107 {
108 pControl->SetValue(*m_pBool);
109 return true;
110 }
111 } else
112 #endif
113 #if wxUSE_RADIOBTN
114 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
115 {
116 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
117 if (m_pBool)
118 {
119 pControl->SetValue(*m_pBool) ;
120 return true;
121 }
122 } else
123 #endif
124 #if wxUSE_TOGGLEBTN
125 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
126 {
127 wxToggleButton * pControl = (wxToggleButton *) m_validatorWindow;
128 if (m_pBool)
129 {
130 pControl->SetValue(*m_pBool);
131 return true;
132 }
133 } else
134 #endif
135
136 // int controls
137 #if wxUSE_GAUGE
138 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
139 {
140 wxGauge* pControl = (wxGauge*) m_validatorWindow;
141 if (m_pInt)
142 {
143 pControl->SetValue(*m_pInt);
144 return true;
145 }
146 } else
147 #endif
148 #if wxUSE_RADIOBOX
149 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
150 {
151 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
152 if (m_pInt)
153 {
154 pControl->SetSelection(*m_pInt) ;
155 return true;
156 }
157 } else
158 #endif
159 #if wxUSE_SCROLLBAR
160 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
161 {
162 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
163 if (m_pInt)
164 {
165 pControl->SetThumbPosition(*m_pInt) ;
166 return true;
167 }
168 } else
169 #endif
170 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
171 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
172 {
173 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
174 if (m_pInt)
175 {
176 pControl->SetValue(*m_pInt);
177 return true;
178 }
179 } else
180 #endif
181 #if wxUSE_SPINBTN
182 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
183 {
184 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
185 if (m_pInt)
186 {
187 pControl->SetValue(*m_pInt) ;
188 return true;
189 }
190 } else
191 #endif
192 #if wxUSE_SLIDER
193 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
194 {
195 wxSlider* pControl = (wxSlider*) m_validatorWindow;
196 if (m_pInt)
197 {
198 pControl->SetValue(*m_pInt) ;
199 return true;
200 }
201 } else
202 #endif
203
204 // string controls
205 #if wxUSE_BUTTON
206 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
207 {
208 wxButton* pControl = (wxButton*) m_validatorWindow;
209 if (m_pString)
210 {
211 pControl->SetLabel(*m_pString) ;
212 return true;
213 }
214 } else
215 #endif
216 #if wxUSE_COMBOBOX
217 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
218 {
219 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
220 if (m_pInt)
221 {
222 pControl->SetSelection(*m_pInt) ;
223 return true;
224 }
225 else if (m_pString)
226 {
227 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
228 {
229 pControl->SetStringSelection(* m_pString);
230 }
231 if ((m_validatorWindow->GetWindowStyle() & wxCB_READONLY) == 0)
232 {
233 pControl->SetValue(* m_pString);
234 }
235 return true;
236 }
237 } else
238 #endif
239 #if wxUSE_CHOICE
240 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
241 {
242 wxChoice* pControl = (wxChoice*) m_validatorWindow;
243 if (m_pInt)
244 {
245 pControl->SetSelection(*m_pInt) ;
246 return true;
247 }
248 else if (m_pString)
249 {
250 if (pControl->FindString(* m_pString) != wxNOT_FOUND)
251 {
252 pControl->SetStringSelection(* m_pString);
253 }
254 return true;
255 }
256 } else
257 #endif
258 #if wxUSE_STATTEXT
259 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
260 {
261 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
262 if (m_pString)
263 {
264 pControl->SetLabel(*m_pString) ;
265 return true;
266 }
267 } else
268 #endif
269 #if wxUSE_TEXTCTRL
270 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
271 {
272 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
273 if (m_pString)
274 {
275 pControl->SetValue(*m_pString) ;
276 return true;
277 }
278 else if (m_pInt)
279 {
280 wxString str;
281 str.Printf(wxT("%d"), *m_pInt);
282 pControl->SetValue(str);
283 return true;
284 }
285 } else
286 #endif
287
288 // array controls
289 #if wxUSE_CHECKLISTBOX
290 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
291 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
292 {
293 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
294 if (m_pArrayInt)
295 {
296 // clear all selections
297 size_t i,
298 count = pControl->GetCount();
299 for ( i = 0 ; i < count; i++ )
300 pControl->Check(i, false);
301
302 // select each item in our array
303 count = m_pArrayInt->GetCount();
304 for ( i = 0 ; i < count; i++ )
305 pControl->Check(m_pArrayInt->Item(i));
306
307 return true;
308 }
309 else
310 return false;
311 } else
312 #endif
313 #if wxUSE_LISTBOX
314 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
315 {
316 wxListBox* pControl = (wxListBox*) m_validatorWindow;
317 if (m_pArrayInt)
318 {
319 // clear all selections
320 size_t i,
321 count = pControl->GetCount();
322 for ( i = 0 ; i < count; i++ )
323 pControl->Deselect(i);
324
325 // select each item in our array
326 count = m_pArrayInt->GetCount();
327 for ( i = 0 ; i < count; i++ )
328 pControl->SetSelection(m_pArrayInt->Item(i));
329
330 return true;
331 }
332 } else
333 #endif
334 ; // to match the last 'else' above
335
336 // unrecognized control, or bad pointer
337 return false;
338 }
339
340 // Called to transfer data from the window
341 bool wxGenericValidator::TransferFromWindow(void)
342 {
343 if ( !m_validatorWindow )
344 return false;
345
346 // BOOL CONTROLS **************************************
347 #if wxUSE_CHECKBOX
348 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
349 {
350 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
351 if (m_pBool)
352 {
353 *m_pBool = pControl->GetValue() ;
354 return true;
355 }
356 } else
357 #endif
358 #if wxUSE_RADIOBTN
359 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
360 {
361 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
362 if (m_pBool)
363 {
364 *m_pBool = pControl->GetValue() ;
365 return true;
366 }
367 } else
368 #endif
369 #if wxUSE_TOGGLEBTN
370 if (m_validatorWindow->IsKindOf(CLASSINFO(wxToggleButton)) )
371 {
372 wxToggleButton *pControl = (wxToggleButton *) m_validatorWindow;
373 if (m_pBool)
374 {
375 *m_pBool = pControl->GetValue() ;
376 return true;
377 }
378 } else
379 #endif
380
381 // INT CONTROLS ***************************************
382 #if wxUSE_GAUGE
383 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
384 {
385 wxGauge* pControl = (wxGauge*) m_validatorWindow;
386 if (m_pInt)
387 {
388 *m_pInt = pControl->GetValue() ;
389 return true;
390 }
391 } else
392 #endif
393 #if wxUSE_RADIOBOX
394 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
395 {
396 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
397 if (m_pInt)
398 {
399 *m_pInt = pControl->GetSelection() ;
400 return true;
401 }
402 } else
403 #endif
404 #if wxUSE_SCROLLBAR
405 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
406 {
407 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
408 if (m_pInt)
409 {
410 *m_pInt = pControl->GetThumbPosition() ;
411 return true;
412 }
413 } else
414 #endif
415 #if wxUSE_SPINCTRL && !defined(__WXMOTIF__)
416 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
417 {
418 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
419 if (m_pInt)
420 {
421 *m_pInt=pControl->GetValue();
422 return true;
423 }
424 } else
425 #endif
426 #if wxUSE_SPINBTN
427 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
428 {
429 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
430 if (m_pInt)
431 {
432 *m_pInt = pControl->GetValue() ;
433 return true;
434 }
435 } else
436 #endif
437 #if wxUSE_SLIDER
438 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
439 {
440 wxSlider* pControl = (wxSlider*) m_validatorWindow;
441 if (m_pInt)
442 {
443 *m_pInt = pControl->GetValue() ;
444 return true;
445 }
446 } else
447 #endif
448
449 // STRING CONTROLS ************************************
450 #if wxUSE_BUTTON
451 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
452 {
453 wxButton* pControl = (wxButton*) m_validatorWindow;
454 if (m_pString)
455 {
456 *m_pString = pControl->GetLabel() ;
457 return true;
458 }
459 } else
460 #endif
461 #if wxUSE_COMBOBOX
462 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
463 {
464 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
465 if (m_pInt)
466 {
467 *m_pInt = pControl->GetSelection() ;
468 return true;
469 }
470 else if (m_pString)
471 {
472 if (m_validatorWindow->GetWindowStyle() & wxCB_READONLY)
473 *m_pString = pControl->GetStringSelection();
474 else
475 *m_pString = pControl->GetValue();
476 return true;
477 }
478 } else
479 #endif
480 #if wxUSE_CHOICE
481 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
482 {
483 wxChoice* pControl = (wxChoice*) m_validatorWindow;
484 if (m_pInt)
485 {
486 *m_pInt = pControl->GetSelection() ;
487 return true;
488 }
489 else if (m_pString)
490 {
491 *m_pString = pControl->GetStringSelection();
492 return true;
493 }
494 } else
495 #endif
496 #if wxUSE_STATTEXT
497 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
498 {
499 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
500 if (m_pString)
501 {
502 *m_pString = pControl->GetLabel() ;
503 return true;
504 }
505 } else
506 #endif
507 #if wxUSE_TEXTCTRL
508 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
509 {
510 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
511 if (m_pString)
512 {
513 *m_pString = pControl->GetValue() ;
514 return true;
515 }
516 else if (m_pInt)
517 {
518 *m_pInt = wxAtoi(pControl->GetValue());
519 return true;
520 }
521 } else
522 #endif
523
524 // ARRAY CONTROLS *************************************
525 #if wxUSE_CHECKLISTBOX
526 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
527 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
528 {
529 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
530 if (m_pArrayInt)
531 {
532 // clear our array
533 m_pArrayInt->Clear();
534
535 // add each selected item to our array
536 size_t i,
537 count = pControl->GetCount();
538 for ( i = 0; i < count; i++ )
539 {
540 if (pControl->IsChecked(i))
541 m_pArrayInt->Add(i);
542 }
543
544 return true;
545 }
546 else
547 return false;
548 } else
549 #endif
550 #if wxUSE_LISTBOX
551 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
552 {
553 wxListBox* pControl = (wxListBox*) m_validatorWindow;
554 if (m_pArrayInt)
555 {
556 // clear our array
557 m_pArrayInt->Clear();
558
559 // add each selected item to our array
560 size_t i,
561 count = pControl->GetCount();
562 for ( i = 0; i < count; i++ )
563 {
564 if (pControl->IsSelected(i))
565 m_pArrayInt->Add(i);
566 }
567
568 return true;
569 }
570 } else
571 #endif
572
573 // unrecognized control, or bad pointer
574 return false;
575
576 return false;
577 }
578
579 /*
580 Called by constructors to initialize ALL data members
581 */
582 void wxGenericValidator::Initialize()
583 {
584 m_pBool = 0;
585 m_pInt = 0;
586 m_pString = 0;
587 m_pArrayInt = 0;
588 }
589
590 #endif
591 // wxUSE_VALIDATORS