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