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