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