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