]>
Commit | Line | Data |
---|---|---|
89c684ef JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: valgen.cpp | |
3 | // Purpose: wxGenericValidator class | |
4 | // Author: Kevin Smith | |
5 | // Modified by: | |
6 | // Created: Jan 22 1999 | |
913df6f2 | 7 | // RCS-ID: |
89c684ef JS |
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__ | |
ce4169a4 | 20 | #pragma hdrstop |
89c684ef JS |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
ce4169a4 RR |
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" | |
e90196a5 | 44 | #include "wx/slider.h" |
89c684ef JS |
45 | #endif |
46 | ||
750b78ba | 47 | #ifndef __WIN16__ |
ce4169a4 RR |
48 | #include "wx/spinbutt.h" |
49 | #include "wx/checklst.h" | |
750b78ba | 50 | #endif |
89c684ef JS |
51 | |
52 | #include "wx/valgen.h" | |
53 | ||
54 | wxGenericValidator::wxGenericValidator(bool *val) | |
55 | { | |
ce4169a4 RR |
56 | Initialize(); |
57 | m_pBool = val; | |
89c684ef JS |
58 | } |
59 | ||
60 | wxGenericValidator::wxGenericValidator(int *val) | |
61 | { | |
ce4169a4 RR |
62 | Initialize(); |
63 | m_pInt = val; | |
89c684ef JS |
64 | } |
65 | ||
66 | wxGenericValidator::wxGenericValidator(wxString *val) | |
67 | { | |
ce4169a4 RR |
68 | Initialize(); |
69 | m_pString = val; | |
89c684ef JS |
70 | } |
71 | ||
72 | wxGenericValidator::wxGenericValidator(wxArrayInt *val) | |
73 | { | |
ce4169a4 RR |
74 | Initialize(); |
75 | m_pArrayInt = val; | |
89c684ef JS |
76 | } |
77 | ||
78 | wxGenericValidator::wxGenericValidator(const wxGenericValidator& val) | |
79 | { | |
ce4169a4 | 80 | Copy(val); |
89c684ef JS |
81 | } |
82 | ||
83 | bool wxGenericValidator::Copy(const wxGenericValidator& val) | |
84 | { | |
e90196a5 | 85 | wxValidator::Copy(val); |
89c684ef | 86 | |
e90196a5 RR |
87 | m_pBool = val.m_pBool; |
88 | m_pInt = val.m_pInt; | |
89 | m_pString = val.m_pString; | |
90 | m_pArrayInt = val.m_pArrayInt; | |
89c684ef | 91 | |
e90196a5 | 92 | return TRUE; |
89c684ef JS |
93 | } |
94 | ||
95 | wxGenericValidator::~wxGenericValidator() | |
96 | { | |
97 | } | |
98 | ||
99 | // Called to transfer data to the window | |
100 | bool wxGenericValidator::TransferToWindow(void) | |
101 | { | |
e90196a5 RR |
102 | if ( !m_validatorWindow ) |
103 | return FALSE; | |
89c684ef | 104 | |
e90196a5 | 105 | // bool controls |
dcf924a3 | 106 | #if wxUSE_CHECKBOX |
e90196a5 | 107 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) ) |
89c684ef | 108 | { |
e90196a5 RR |
109 | wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow; |
110 | if (m_pBool) | |
111 | { | |
112 | pControl->SetValue(*m_pBool); | |
113 | return TRUE; | |
114 | } | |
115 | } else | |
dcf924a3 RR |
116 | #endif |
117 | #if wxUSE_RADIOBTN | |
e90196a5 | 118 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) ) |
89c684ef | 119 | { |
e90196a5 RR |
120 | wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow; |
121 | if (m_pBool) | |
122 | { | |
123 | pControl->SetValue(*m_pBool) ; | |
124 | return TRUE; | |
125 | } | |
126 | } else | |
dcf924a3 | 127 | #endif |
e90196a5 RR |
128 | |
129 | // int controls | |
dcf924a3 | 130 | #if wxUSE_GAUGE |
e90196a5 | 131 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) ) |
89c684ef | 132 | { |
e90196a5 RR |
133 | wxGauge* pControl = (wxGauge*) m_validatorWindow; |
134 | if (m_pInt) | |
135 | { | |
136 | pControl->SetValue(*m_pInt); | |
137 | return TRUE; | |
138 | } | |
139 | } else | |
dcf924a3 RR |
140 | #endif |
141 | #if wxUSE_RADIOBOX | |
e90196a5 | 142 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) ) |
89c684ef | 143 | { |
e90196a5 RR |
144 | wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow; |
145 | if (m_pInt) | |
146 | { | |
147 | pControl->SetSelection(*m_pInt) ; | |
148 | return TRUE; | |
149 | } | |
150 | } else | |
dcf924a3 RR |
151 | #endif |
152 | #if wxUSE_SCROLLBAR | |
e90196a5 | 153 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) ) |
89c684ef | 154 | { |
e90196a5 RR |
155 | wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow; |
156 | if (m_pInt) | |
157 | { | |
158 | pControl->SetThumbPosition(*m_pInt) ; | |
159 | return TRUE; | |
160 | } | |
161 | } else | |
dcf924a3 RR |
162 | #endif |
163 | #if wxUSE_SPINBTN | |
750b78ba | 164 | #ifndef __WIN16__ |
e90196a5 | 165 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) ) |
89c684ef | 166 | { |
e90196a5 RR |
167 | wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow; |
168 | if (m_pInt) | |
169 | { | |
170 | pControl->SetValue(*m_pInt) ; | |
171 | return TRUE; | |
172 | } | |
173 | } else | |
dcf924a3 | 174 | #endif |
750b78ba | 175 | #endif |
e90196a5 RR |
176 | #if wxUSE_SLIDER |
177 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) ) | |
178 | { | |
179 | wxSlider* pControl = (wxSlider*) m_validatorWindow; | |
180 | if (m_pInt) | |
181 | { | |
182 | pControl->SetValue(*m_pInt) ; | |
183 | return TRUE; | |
184 | } | |
185 | } else | |
186 | #endif | |
187 | ||
89c684ef | 188 | // string controls |
e90196a5 RR |
189 | #if 1 |
190 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) ) | |
89c684ef | 191 | { |
e90196a5 | 192 | wxButton* pControl = (wxButton*) m_validatorWindow; |
89c684ef | 193 | if (m_pString) |
e90196a5 RR |
194 | { |
195 | pControl->SetLabel(*m_pString) ; | |
196 | return TRUE; | |
197 | } | |
198 | } else | |
199 | #endif | |
200 | #if wxUSE_COMBOBOX | |
201 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) ) | |
89c684ef | 202 | { |
e90196a5 RR |
203 | wxComboBox* pControl = (wxComboBox*) m_validatorWindow; |
204 | if (m_pString) | |
205 | { | |
206 | pControl->SetValue(*m_pString) ; | |
207 | return TRUE; | |
208 | } | |
209 | } else | |
dcf924a3 | 210 | #endif |
ce4169a4 | 211 | #if wxUSE_CHOICE |
e90196a5 | 212 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) ) |
89c684ef | 213 | { |
e90196a5 RR |
214 | wxChoice* pControl = (wxChoice*) m_validatorWindow; |
215 | if (m_pInt) | |
216 | { | |
217 | pControl->SetSelection(*m_pInt) ; | |
218 | return TRUE; | |
219 | } | |
220 | } else | |
ce4169a4 | 221 | #endif |
e90196a5 | 222 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) ) |
89c684ef | 223 | { |
e90196a5 | 224 | wxStaticText* pControl = (wxStaticText*) m_validatorWindow; |
89c684ef | 225 | if (m_pString) |
e90196a5 RR |
226 | { |
227 | pControl->SetLabel(*m_pString) ; | |
228 | return TRUE; | |
229 | } | |
230 | } else | |
231 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
89c684ef | 232 | { |
e90196a5 RR |
233 | wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow; |
234 | if (m_pString) | |
235 | { | |
236 | pControl->SetValue(*m_pString) ; | |
237 | return TRUE; | |
238 | } | |
239 | } else | |
dcf924a3 | 240 | #if wxUSE_CHECKLISTBOX |
750b78ba | 241 | #ifndef __WIN16__ |
89c684ef JS |
242 | // array controls |
243 | // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox | |
244 | // MUST come first: | |
dcf924a3 | 245 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) ) |
89c684ef JS |
246 | { |
247 | wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow; | |
248 | if (m_pArrayInt) | |
249 | { | |
250 | // clear all selections | |
251 | int i; | |
252 | for (i = 0 ; i < pControl->Number(); ++i) | |
253 | pControl->Check(i, FALSE); | |
254 | // select each item in our array | |
255 | unsigned u; | |
256 | for (u = 0; u < m_pArrayInt->Count(); ++u) | |
257 | pControl->Check(m_pArrayInt->Item(u)); | |
258 | return TRUE; | |
259 | } | |
260 | else | |
261 | return FALSE; | |
dcf924a3 | 262 | } else |
750b78ba | 263 | #endif |
dcf924a3 RR |
264 | #endif |
265 | #if wxUSE_LISTBOX | |
e90196a5 | 266 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) ) |
89c684ef | 267 | { |
e90196a5 RR |
268 | wxListBox* pControl = (wxListBox*) m_validatorWindow; |
269 | if (m_pArrayInt) | |
270 | { | |
271 | // clear all selections | |
272 | int i; | |
273 | for (i = 0 ; i < pControl->Number(); ++i) | |
274 | pControl->Deselect(i); | |
275 | // select each item in our array | |
276 | unsigned u; | |
277 | for (u = 0; u < m_pArrayInt->Count(); ++u) | |
278 | pControl->SetSelection(m_pArrayInt->Item(u)); | |
279 | return TRUE; | |
280 | } | |
281 | } else | |
dcf924a3 | 282 | #endif |
89c684ef JS |
283 | |
284 | // unrecognized control, or bad pointer | |
dcf924a3 | 285 | return FALSE; |
89c684ef JS |
286 | return FALSE; |
287 | } | |
288 | ||
e90196a5 | 289 | // Called to transfer data from the window |
89c684ef JS |
290 | bool wxGenericValidator::TransferFromWindow(void) |
291 | { | |
292 | if ( !m_validatorWindow ) | |
293 | return FALSE; | |
294 | ||
295 | // bool controls | |
dcf924a3 | 296 | #if wxUSE_CHECKBOX |
89c684ef JS |
297 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) ) |
298 | { | |
299 | wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow; | |
300 | if (m_pBool) | |
301 | { | |
302 | *m_pBool = pControl->GetValue() ; | |
303 | return TRUE; | |
304 | } | |
913df6f2 | 305 | } else |
dcf924a3 RR |
306 | #endif |
307 | #if wxUSE_RADIOBTN | |
308 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) ) | |
89c684ef JS |
309 | { |
310 | wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow; | |
311 | if (m_pBool) | |
312 | { | |
313 | *m_pBool = pControl->GetValue() ; | |
314 | return TRUE; | |
315 | } | |
dcf924a3 RR |
316 | } else |
317 | #endif | |
89c684ef | 318 | // int controls |
dcf924a3 RR |
319 | #if wxUSE_GAUGE |
320 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) ) | |
89c684ef JS |
321 | { |
322 | wxGauge* pControl = (wxGauge*) m_validatorWindow; | |
323 | if (m_pInt) | |
324 | { | |
325 | *m_pInt = pControl->GetValue() ; | |
326 | return TRUE; | |
327 | } | |
913df6f2 | 328 | } else |
dcf924a3 RR |
329 | #endif |
330 | #if wxUSE_RADIOBOX | |
331 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) ) | |
89c684ef JS |
332 | { |
333 | wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow; | |
334 | if (m_pInt) | |
335 | { | |
336 | *m_pInt = pControl->GetSelection() ; | |
337 | return TRUE; | |
338 | } | |
913df6f2 | 339 | } else |
dcf924a3 RR |
340 | #endif |
341 | #if wxUSE_SCROLLBAR | |
342 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) ) | |
89c684ef JS |
343 | { |
344 | wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow; | |
345 | if (m_pInt) | |
346 | { | |
347 | *m_pInt = pControl->GetThumbPosition() ; | |
348 | return TRUE; | |
349 | } | |
dcf924a3 RR |
350 | } else |
351 | #endif | |
352 | #if wxUSE_SPINBTN | |
750b78ba | 353 | #ifndef __WIN16__ |
dcf924a3 | 354 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) ) |
89c684ef JS |
355 | { |
356 | wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow; | |
357 | if (m_pInt) | |
358 | { | |
359 | *m_pInt = pControl->GetValue() ; | |
360 | return TRUE; | |
361 | } | |
dcf924a3 RR |
362 | } else |
363 | #endif | |
e90196a5 RR |
364 | #endif |
365 | #if wxUSE_SLIDER | |
366 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxSlider)) ) | |
367 | { | |
368 | wxSlider* pControl = (wxSlider*) m_validatorWindow; | |
369 | if (m_pInt) | |
370 | { | |
371 | pControl->SetValue(*m_pInt) ; | |
372 | return TRUE; | |
373 | } | |
374 | } else | |
750b78ba | 375 | #endif |
89c684ef | 376 | // string controls |
dcf924a3 | 377 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) ) |
89c684ef JS |
378 | { |
379 | wxButton* pControl = (wxButton*) m_validatorWindow; | |
380 | if (m_pString) | |
381 | { | |
382 | *m_pString = pControl->GetLabel() ; | |
383 | return TRUE; | |
384 | } | |
385 | } | |
913df6f2 | 386 | else |
dcf924a3 RR |
387 | #if wxUSE_COMBOBOX |
388 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) ) | |
89c684ef JS |
389 | { |
390 | wxComboBox* pControl = (wxComboBox*) m_validatorWindow; | |
391 | if (m_pString) | |
392 | { | |
393 | *m_pString = pControl->GetValue() ; | |
394 | return TRUE; | |
395 | } | |
913df6f2 | 396 | } else |
dcf924a3 | 397 | #endif |
ce4169a4 | 398 | #if wxUSE_CHOICE |
dcf924a3 | 399 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) ) |
89c684ef JS |
400 | { |
401 | wxChoice* pControl = (wxChoice*) m_validatorWindow; | |
402 | if (m_pInt) | |
403 | { | |
404 | *m_pInt = pControl->GetSelection() ; | |
405 | return TRUE; | |
406 | } | |
913df6f2 | 407 | } else |
ce4169a4 | 408 | #endif |
dcf924a3 | 409 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) ) |
89c684ef JS |
410 | { |
411 | wxStaticText* pControl = (wxStaticText*) m_validatorWindow; | |
412 | if (m_pString) | |
413 | { | |
414 | *m_pString = pControl->GetLabel() ; | |
415 | return TRUE; | |
416 | } | |
913df6f2 | 417 | } else |
dcf924a3 | 418 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) |
89c684ef JS |
419 | { |
420 | wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow; | |
421 | if (m_pString) | |
422 | { | |
423 | *m_pString = pControl->GetValue() ; | |
424 | return TRUE; | |
425 | } | |
dcf924a3 | 426 | } else |
e90196a5 | 427 | #if wxUSE_CHECKLISTBOX |
750b78ba | 428 | #ifndef __WIN16__ |
89c684ef JS |
429 | // array controls |
430 | // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox | |
431 | // MUST come first: | |
dcf924a3 | 432 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) ) |
89c684ef JS |
433 | { |
434 | wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow; | |
435 | if (m_pArrayInt) | |
436 | { | |
437 | // clear our array | |
438 | m_pArrayInt->Clear(); | |
439 | // add each selected item to our array | |
440 | int i; | |
441 | for (i = 0 ; i < pControl->Number(); ++i) | |
442 | if (pControl->IsChecked(i)) | |
443 | m_pArrayInt->Add(i); | |
444 | return TRUE; | |
445 | } | |
446 | else | |
447 | return FALSE; | |
dcf924a3 RR |
448 | } else |
449 | #endif | |
750b78ba | 450 | #endif |
dcf924a3 RR |
451 | #if wxUSE_LISTBOX |
452 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) ) | |
89c684ef JS |
453 | { |
454 | wxListBox* pControl = (wxListBox*) m_validatorWindow; | |
455 | if (m_pArrayInt) | |
456 | { | |
457 | // clear our array | |
458 | m_pArrayInt->Clear(); | |
459 | // add each selected item to our array | |
460 | int i; | |
461 | for (i = 0 ; i < pControl->Number(); ++i) | |
462 | if (pControl->Selected(i)) | |
463 | m_pArrayInt->Add(i); | |
464 | return TRUE; | |
465 | } | |
dcf924a3 RR |
466 | } else |
467 | #endif | |
89c684ef JS |
468 | |
469 | // unrecognized control, or bad pointer | |
dcf924a3 | 470 | return FALSE; |
89c684ef JS |
471 | return FALSE; |
472 | } | |
473 | ||
474 | /* | |
475 | Called by constructors to initialize ALL data members | |
476 | */ | |
477 | void wxGenericValidator::Initialize() | |
478 | { | |
ce4169a4 RR |
479 | m_pBool = 0; |
480 | m_pInt = 0; | |
481 | m_pString = 0; | |
482 | m_pArrayInt = 0; | |
89c684ef JS |
483 | } |
484 | ||
ce4169a4 RR |
485 | #endif |
486 | // wxUSE_VALIDATORS | |
913df6f2 | 487 |