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