]> git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
applied copy constructor fix to assignment operator as well
[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 : wxValidator()
85 {
86 Copy(val);
87 }
88
89 bool 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
101 wxGenericValidator::~wxGenericValidator()
102 {
103 }
104
105 // Called to transfer data to the window
106 bool 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 return TRUE;
229 }
230 } else
231 #endif
232 #if wxUSE_CHOICE
233 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
234 {
235 wxChoice* pControl = (wxChoice*) m_validatorWindow;
236 if (m_pInt)
237 {
238 pControl->SetSelection(*m_pInt) ;
239 return TRUE;
240 }
241 else if (m_pString)
242 {
243 if (pControl->FindString(* m_pString) > -1)
244 {
245 pControl->SetStringSelection(* m_pString);
246 }
247 return TRUE;
248 }
249 } else
250 #endif
251 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
252 {
253 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
254 if (m_pString)
255 {
256 pControl->SetLabel(*m_pString) ;
257 return TRUE;
258 }
259 } else
260 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
261 {
262 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
263 if (m_pString)
264 {
265 pControl->SetValue(*m_pString) ;
266 return TRUE;
267 }
268 else if (m_pInt)
269 {
270 wxString str;
271 str.Printf(wxT("%d"), *m_pInt);
272 pControl->SetValue(str);
273 return TRUE;
274 }
275 } else
276 // array controls
277 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
278 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox 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 size_t i,
286 count = pControl->GetCount();
287 for ( i = 0 ; i < count; i++ )
288 pControl->Check(i, FALSE);
289
290 // select each item in our array
291 count = m_pArrayInt->GetCount();
292 for ( i = 0 ; i < count; i++ )
293 pControl->Check(m_pArrayInt->Item(i));
294
295 return TRUE;
296 }
297 else
298 return FALSE;
299 } else
300 #endif
301 #if wxUSE_LISTBOX
302 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
303 {
304 wxListBox* pControl = (wxListBox*) m_validatorWindow;
305 if (m_pArrayInt)
306 {
307 // clear all selections
308 size_t i,
309 count = pControl->GetCount();
310 for ( i = 0 ; i < count; i++ )
311 pControl->Deselect(i);
312
313 // select each item in our array
314 count = m_pArrayInt->GetCount();
315 for ( i = 0 ; i < count; i++ )
316 pControl->SetSelection(m_pArrayInt->Item(i));
317
318 return TRUE;
319 }
320 } else
321 #endif
322 ; // to match the last 'else' above
323
324 // unrecognized control, or bad pointer
325 return FALSE;
326 }
327
328 // Called to transfer data from the window
329 bool wxGenericValidator::TransferFromWindow(void)
330 {
331 if ( !m_validatorWindow )
332 return FALSE;
333
334 // bool controls
335 #if wxUSE_CHECKBOX
336 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
337 {
338 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
339 if (m_pBool)
340 {
341 *m_pBool = pControl->GetValue() ;
342 return TRUE;
343 }
344 } else
345 #endif
346 #if wxUSE_RADIOBTN
347 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
348 {
349 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
350 if (m_pBool)
351 {
352 *m_pBool = pControl->GetValue() ;
353 return TRUE;
354 }
355 } else
356 #endif
357 // int controls
358 #if wxUSE_GAUGE
359 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
360 {
361 wxGauge* pControl = (wxGauge*) m_validatorWindow;
362 if (m_pInt)
363 {
364 *m_pInt = pControl->GetValue() ;
365 return TRUE;
366 }
367 } else
368 #endif
369 #if wxUSE_RADIOBOX
370 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
371 {
372 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
373 if (m_pInt)
374 {
375 *m_pInt = pControl->GetSelection() ;
376 return TRUE;
377 }
378 } else
379 #endif
380 #if wxUSE_SCROLLBAR
381 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
382 {
383 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
384 if (m_pInt)
385 {
386 *m_pInt = pControl->GetThumbPosition() ;
387 return TRUE;
388 }
389 } else
390 #endif
391 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
392 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
393 {
394 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
395 if (m_pInt)
396 {
397 *m_pInt=pControl->GetValue();
398 return TRUE;
399 }
400 } else
401 #endif
402 #if wxUSE_SPINBTN && !defined(__WIN16__)
403 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
404 {
405 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
406 if (m_pInt)
407 {
408 *m_pInt = pControl->GetValue() ;
409 return TRUE;
410 }
411 } else
412 #endif
413 #if wxUSE_SLIDER
414 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
415 {
416 wxSlider* pControl = (wxSlider*) m_validatorWindow;
417 if (m_pInt)
418 {
419 *m_pInt = pControl->GetValue() ;
420 return TRUE;
421 }
422 } else
423 #endif
424 // string controls
425 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
426 {
427 wxButton* pControl = (wxButton*) m_validatorWindow;
428 if (m_pString)
429 {
430 *m_pString = pControl->GetLabel() ;
431 return TRUE;
432 }
433 }
434 else
435 #if wxUSE_COMBOBOX
436 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
437 {
438 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
439 if (m_pInt)
440 {
441 *m_pInt = pControl->GetSelection() ;
442 return TRUE;
443 }
444 else if (m_pString)
445 {
446 *m_pString = pControl->GetValue();
447 return TRUE;
448 }
449 } else
450 #endif
451 #if wxUSE_CHOICE
452 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
453 {
454 wxChoice* pControl = (wxChoice*) m_validatorWindow;
455 if (m_pInt)
456 {
457 *m_pInt = pControl->GetSelection() ;
458 return TRUE;
459 }
460 else if (m_pString)
461 {
462 *m_pString = pControl->GetStringSelection();
463 return TRUE;
464 }
465 } else
466 #endif
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 // array controls
491 #if wxUSE_CHECKLISTBOX
492 #ifndef __WIN16__
493 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
494 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
495 {
496 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
497 if (m_pArrayInt)
498 {
499 // clear our array
500 m_pArrayInt->Clear();
501
502 // add each selected item to our array
503 size_t i,
504 count = pControl->GetCount();
505 for ( i = 0; i < count; i++ )
506 {
507 if (pControl->IsChecked(i))
508 m_pArrayInt->Add(i);
509 }
510
511 return TRUE;
512 }
513 else
514 return FALSE;
515 } else
516 #endif
517 #endif
518 #if wxUSE_LISTBOX
519 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
520 {
521 wxListBox* pControl = (wxListBox*) m_validatorWindow;
522 if (m_pArrayInt)
523 {
524 // clear our array
525 m_pArrayInt->Clear();
526
527 // add each selected item to our array
528 size_t i,
529 count = pControl->GetCount();
530 for ( i = 0; i < count; i++ )
531 {
532 if (pControl->Selected(i))
533 m_pArrayInt->Add(i);
534 }
535
536 return TRUE;
537 }
538 } else
539 #endif
540
541 // unrecognized control, or bad pointer
542 return FALSE;
543 return FALSE;
544 }
545
546 /*
547 Called by constructors to initialize ALL data members
548 */
549 void wxGenericValidator::Initialize()
550 {
551 m_pBool = 0;
552 m_pInt = 0;
553 m_pString = 0;
554 m_pArrayInt = 0;
555 }
556
557 #endif
558 // wxUSE_VALIDATORS
559