]> git.saurik.com Git - wxWidgets.git/blob - src/common/valgen.cpp
popupcmn is needed by wxGTK too, why was it excluded?
[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 // array controls
276 #if wxUSE_CHECKLISTBOX && !defined(__WIN16__)
277 // NOTE: wxCheckListBox is a wxListBox, so wxCheckListBox MUST come first:
278 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
279 {
280 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
281 if (m_pArrayInt)
282 {
283 // clear all selections
284 size_t i,
285 count = pControl->GetCount();
286 for ( i = 0 ; i < count; i++ )
287 pControl->Check(i, FALSE);
288
289 // select each item in our array
290 count = m_pArrayInt->GetCount();
291 for ( i = 0 ; i < count; i++ )
292 pControl->Check(m_pArrayInt->Item(i));
293
294 return TRUE;
295 }
296 else
297 return FALSE;
298 } else
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 size_t i,
308 count = pControl->GetCount();
309 for ( i = 0 ; i < count; i++ )
310 pControl->Deselect(i);
311
312 // select each item in our array
313 count = m_pArrayInt->GetCount();
314 for ( i = 0 ; i < count; i++ )
315 pControl->SetSelection(m_pArrayInt->Item(i));
316
317 return TRUE;
318 }
319 } else
320 #endif
321 ; // to match the last 'else' above
322
323 // unrecognized control, or bad pointer
324 return FALSE;
325 }
326
327 // Called to transfer data from the window
328 bool wxGenericValidator::TransferFromWindow(void)
329 {
330 if ( !m_validatorWindow )
331 return FALSE;
332
333 // bool controls
334 #if wxUSE_CHECKBOX
335 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) )
336 {
337 wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow;
338 if (m_pBool)
339 {
340 *m_pBool = pControl->GetValue() ;
341 return TRUE;
342 }
343 } else
344 #endif
345 #if wxUSE_RADIOBTN
346 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
347 {
348 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
349 if (m_pBool)
350 {
351 *m_pBool = pControl->GetValue() ;
352 return TRUE;
353 }
354 } else
355 #endif
356 // int controls
357 #if wxUSE_GAUGE
358 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
359 {
360 wxGauge* pControl = (wxGauge*) m_validatorWindow;
361 if (m_pInt)
362 {
363 *m_pInt = pControl->GetValue() ;
364 return TRUE;
365 }
366 } else
367 #endif
368 #if wxUSE_RADIOBOX
369 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
370 {
371 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
372 if (m_pInt)
373 {
374 *m_pInt = pControl->GetSelection() ;
375 return TRUE;
376 }
377 } else
378 #endif
379 #if wxUSE_SCROLLBAR
380 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
381 {
382 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
383 if (m_pInt)
384 {
385 *m_pInt = pControl->GetThumbPosition() ;
386 return TRUE;
387 }
388 } else
389 #endif
390 #if wxUSE_SPINCTRL && !defined(__WIN16__) && !defined(__WXMOTIF__)
391 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinCtrl)) )
392 {
393 wxSpinCtrl* pControl = (wxSpinCtrl*) m_validatorWindow;
394 if (m_pInt)
395 {
396 *m_pInt=pControl->GetValue();
397 return TRUE;
398 }
399 } else
400 #endif
401 #if wxUSE_SPINBTN && !defined(__WIN16__)
402 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
403 {
404 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
405 if (m_pInt)
406 {
407 *m_pInt = pControl->GetValue() ;
408 return TRUE;
409 }
410 } else
411 #endif
412 #if wxUSE_SLIDER
413 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) )
414 {
415 wxSlider* pControl = (wxSlider*) m_validatorWindow;
416 if (m_pInt)
417 {
418 *m_pInt = pControl->GetValue() ;
419 return TRUE;
420 }
421 } else
422 #endif
423 // string controls
424 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
425 {
426 wxButton* pControl = (wxButton*) m_validatorWindow;
427 if (m_pString)
428 {
429 *m_pString = pControl->GetLabel() ;
430 return TRUE;
431 }
432 }
433 else
434 #if wxUSE_COMBOBOX
435 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
436 {
437 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
438 if (m_pInt)
439 {
440 *m_pInt = pControl->GetSelection() ;
441 return TRUE;
442 }
443 else if (m_pString)
444 {
445 *m_pString = pControl->GetStringSelection();
446 return TRUE;
447 }
448 } else
449 #endif
450 #if wxUSE_CHOICE
451 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
452 {
453 wxChoice* pControl = (wxChoice*) m_validatorWindow;
454 if (m_pInt)
455 {
456 *m_pInt = pControl->GetSelection() ;
457 return TRUE;
458 }
459 else if (m_pString)
460 {
461 *m_pString = pControl->GetStringSelection();
462 return TRUE;
463 }
464 } else
465 #endif
466 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
467 {
468 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
469 if (m_pString)
470 {
471 *m_pString = pControl->GetLabel() ;
472 return TRUE;
473 }
474 } else
475 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
476 {
477 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
478 if (m_pString)
479 {
480 *m_pString = pControl->GetValue() ;
481 return TRUE;
482 }
483 else if (m_pInt)
484 {
485 *m_pInt = wxAtoi(pControl->GetValue());
486 return TRUE;
487 }
488 } else
489 // array controls
490 #if wxUSE_CHECKLISTBOX
491 #ifndef __WIN16__
492 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox MUST come first:
493 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
494 {
495 wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow;
496 if (m_pArrayInt)
497 {
498 // clear our array
499 m_pArrayInt->Clear();
500
501 // add each selected item to our array
502 size_t i,
503 count = pControl->GetCount();
504 for ( i = 0; i < count; i++ )
505 {
506 if (pControl->IsChecked(i))
507 m_pArrayInt->Add(i);
508 }
509
510 return TRUE;
511 }
512 else
513 return FALSE;
514 } else
515 #endif
516 #endif
517 #if wxUSE_LISTBOX
518 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
519 {
520 wxListBox* pControl = (wxListBox*) m_validatorWindow;
521 if (m_pArrayInt)
522 {
523 // clear our array
524 m_pArrayInt->Clear();
525
526 // add each selected item to our array
527 size_t i,
528 count = pControl->GetCount();
529 for ( i = 0; i < count; i++ )
530 {
531 if (pControl->Selected(i))
532 m_pArrayInt->Add(i);
533 }
534
535 return TRUE;
536 }
537 } else
538 #endif
539
540 // unrecognized control, or bad pointer
541 return FALSE;
542 return FALSE;
543 }
544
545 /*
546 Called by constructors to initialize ALL data members
547 */
548 void wxGenericValidator::Initialize()
549 {
550 m_pBool = 0;
551 m_pInt = 0;
552 m_pString = 0;
553 m_pArrayInt = 0;
554 }
555
556 #endif
557 // wxUSE_VALIDATORS
558