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