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