More Unicode compilation 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 #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 #ifndef __WIN16__
48 #include "wx/spinbutt.h"
49 #if wxUSE_CHECKLISTBOX
50 #include "wx/checklst.h"
51 #endif
52 #endif
53
54 #include "wx/valgen.h"
55
56 wxGenericValidator::wxGenericValidator(bool *val)
57 {
58 Initialize();
59 m_pBool = val;
60 }
61
62 wxGenericValidator::wxGenericValidator(int *val)
63 {
64 Initialize();
65 m_pInt = val;
66 }
67
68 wxGenericValidator::wxGenericValidator(wxString *val)
69 {
70 Initialize();
71 m_pString = val;
72 }
73
74 wxGenericValidator::wxGenericValidator(wxArrayInt *val)
75 {
76 Initialize();
77 m_pArrayInt = val;
78 }
79
80 wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
81 {
82 Copy(val);
83 }
84
85 bool wxGenericValidator::Copy(const wxGenericValidator& val)
86 {
87 wxValidator::Copy(val);
88
89 m_pBool = val.m_pBool;
90 m_pInt = val.m_pInt;
91 m_pString = val.m_pString;
92 m_pArrayInt = val.m_pArrayInt;
93
94 return TRUE;
95 }
96
97 wxGenericValidator::~wxGenericValidator()
98 {
99 }
100
101 // Called to transfer data to the window
102 bool wxGenericValidator::TransferToWindow(void)
103 {
104 if ( !m_validatorWindow )
105 return FALSE;
106
107 // bool controls
108 #if wxUSE_CHECKBOX
109 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
110 {
111 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
112 if (m_pBool)
113 {
114 pControl->SetValue(*m_pBool);
115 return TRUE;
116 }
117 } else
118 #endif
119 #if wxUSE_RADIOBTN
120 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
121 {
122 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
123 if (m_pBool)
124 {
125 pControl->SetValue(*m_pBool) ;
126 return TRUE;
127 }
128 } else
129 #endif
130
131 // int controls
132 #if wxUSE_GAUGE
133 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
134 {
135 wxGauge* pControl = (wxGauge*) m_validatorWindow;
136 if (m_pInt)
137 {
138 pControl->SetValue(*m_pInt);
139 return TRUE;
140 }
141 } else
142 #endif
143 #if wxUSE_RADIOBOX
144 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
145 {
146 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
147 if (m_pInt)
148 {
149 pControl->SetSelection(*m_pInt) ;
150 return TRUE;
151 }
152 } else
153 #endif
154 #if wxUSE_SCROLLBAR
155 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
156 {
157 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
158 if (m_pInt)
159 {
160 pControl->SetThumbPosition(*m_pInt) ;
161 return TRUE;
162 }
163 } else
164 #endif
165 #if wxUSE_SPINBTN
166 #ifndef __WIN16__
167 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
168 {
169 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
170 if (m_pInt)
171 {
172 pControl->SetValue(*m_pInt) ;
173 return TRUE;
174 }
175 } else
176 #endif
177 #endif
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
190 // string controls
191 #if 1
192 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
193 {
194 wxButton* pControl = (wxButton*) m_validatorWindow;
195 if (m_pString)
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)) )
204 {
205 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
206 if (m_pString)
207 {
208 pControl->SetValue(*m_pString) ;
209 return TRUE;
210 }
211 } else
212 #endif
213 #if wxUSE_CHOICE
214 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
215 {
216 wxChoice* pControl = (wxChoice*) 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 return TRUE;
229 }
230 } else
231 #endif
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
249 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
250 {
251 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
252 if (m_pString)
253 {
254 pControl->SetLabel(*m_pString) ;
255 return TRUE;
256 }
257 } else
258 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
259 {
260 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
261 if (m_pString)
262 {
263 pControl->SetValue(*m_pString) ;
264 return TRUE;
265 }
266 else if (m_pInt)
267 {
268 wxString str;
269 str.Printf(wxT("%d"), *m_pInt);
270 pControl->SetValue(str);
271 return TRUE;
272 }
273 } else
274 #if wxUSE_CHECKLISTBOX
275 #ifndef __WIN16__
276 // array controls
277 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
278 // MUST come first:
279 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
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;
296 } else
297 #endif
298 #endif
299 #if wxUSE_LISTBOX
300 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
301 {
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
316 #endif
317
318 // unrecognized control, or bad pointer
319 return FALSE;
320 return FALSE;
321 }
322
323 // Called to transfer data from the window
324 bool wxGenericValidator::TransferFromWindow(void)
325 {
326 if ( !m_validatorWindow )
327 return FALSE;
328
329 // bool controls
330 #if wxUSE_CHECKBOX
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 }
339 } else
340 #endif
341 #if wxUSE_RADIOBTN
342 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
343 {
344 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
345 if (m_pBool)
346 {
347 *m_pBool = pControl->GetValue() ;
348 return TRUE;
349 }
350 } else
351 #endif
352 // int controls
353 #if wxUSE_GAUGE
354 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
355 {
356 wxGauge* pControl = (wxGauge*) m_validatorWindow;
357 if (m_pInt)
358 {
359 *m_pInt = pControl->GetValue() ;
360 return TRUE;
361 }
362 } else
363 #endif
364 #if wxUSE_RADIOBOX
365 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
366 {
367 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
368 if (m_pInt)
369 {
370 *m_pInt = pControl->GetSelection() ;
371 return TRUE;
372 }
373 } else
374 #endif
375 #if wxUSE_SCROLLBAR
376 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
377 {
378 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
379 if (m_pInt)
380 {
381 *m_pInt = pControl->GetThumbPosition() ;
382 return TRUE;
383 }
384 } else
385 #endif
386 #if wxUSE_SPINBTN
387 #ifndef __WIN16__
388 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
389 {
390 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
391 if (m_pInt)
392 {
393 *m_pInt = pControl->GetValue() ;
394 return TRUE;
395 }
396 } else
397 #endif
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
409 #endif
410 // string controls
411 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
412 {
413 wxButton* pControl = (wxButton*) m_validatorWindow;
414 if (m_pString)
415 {
416 *m_pString = pControl->GetLabel() ;
417 return TRUE;
418 }
419 }
420 else
421 #if wxUSE_COMBOBOX
422 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
423 {
424 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
425 if (m_pString)
426 {
427 *m_pString = pControl->GetValue() ;
428 return TRUE;
429 }
430 else if (m_pString)
431 {
432 *m_pString = pControl->GetStringSelection();
433 return TRUE;
434 }
435 } else
436 #endif
437 #if wxUSE_CHOICE
438 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
439 {
440 wxChoice* pControl = (wxChoice*) m_validatorWindow;
441 if (m_pInt)
442 {
443 *m_pInt = pControl->GetSelection() ;
444 return TRUE;
445 }
446 else if (m_pString)
447 {
448 *m_pString = pControl->GetStringSelection();
449 return TRUE;
450 }
451 } else
452 #endif
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
467 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
468 {
469 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
470 if (m_pString)
471 {
472 *m_pString = pControl->GetLabel() ;
473 return TRUE;
474 }
475 } else
476 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
477 {
478 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
479 if (m_pString)
480 {
481 *m_pString = pControl->GetValue() ;
482 return TRUE;
483 }
484 else if (m_pInt)
485 {
486 *m_pInt = wxAtoi(pControl->GetValue());
487 return TRUE;
488 }
489 } else
490 #if wxUSE_CHECKLISTBOX
491 #ifndef __WIN16__
492 // array controls
493 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
494 // MUST come first:
495 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
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;
511 } else
512 #endif
513 #endif
514 #if wxUSE_LISTBOX
515 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
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 }
529 } else
530 #endif
531
532 // unrecognized control, or bad pointer
533 return FALSE;
534 return FALSE;
535 }
536
537 /*
538 Called by constructors to initialize ALL data members
539 */
540 void wxGenericValidator::Initialize()
541 {
542 m_pBool = 0;
543 m_pInt = 0;
544 m_pString = 0;
545 m_pArrayInt = 0;
546 }
547
548 #endif
549 // wxUSE_VALIDATORS
550