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