]>
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 | |
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/utils.h" | |
25 | #include "wx/intl.h" | |
f7f1f70f | 26 | #include "wx/wx.h" |
89c684ef JS |
27 | #endif |
28 | ||
29 | #include "wx/spinbutt.h" | |
30 | #include "wx/checklst.h" | |
31 | ||
32 | #include "wx/valgen.h" | |
33 | ||
34 | wxGenericValidator::wxGenericValidator(bool *val) | |
35 | { | |
36 | Initialize(); | |
37 | m_pBool = val; | |
38 | } | |
39 | ||
40 | wxGenericValidator::wxGenericValidator(int *val) | |
41 | { | |
42 | Initialize(); | |
43 | m_pInt = val; | |
44 | } | |
45 | ||
46 | wxGenericValidator::wxGenericValidator(wxString *val) | |
47 | { | |
48 | Initialize(); | |
49 | m_pString = val; | |
50 | } | |
51 | ||
52 | wxGenericValidator::wxGenericValidator(wxArrayInt *val) | |
53 | { | |
54 | Initialize(); | |
55 | m_pArrayInt = val; | |
56 | } | |
57 | ||
58 | wxGenericValidator::wxGenericValidator(const wxGenericValidator& val) | |
59 | { | |
60 | Copy(val); | |
61 | } | |
62 | ||
63 | bool wxGenericValidator::Copy(const wxGenericValidator& val) | |
64 | { | |
65 | wxValidator::Copy(val); | |
66 | ||
67 | m_pBool = val.m_pBool; | |
68 | m_pInt = val.m_pInt; | |
69 | m_pString = val.m_pString; | |
70 | m_pArrayInt = val.m_pArrayInt; | |
71 | ||
72 | return TRUE; | |
73 | } | |
74 | ||
75 | wxGenericValidator::~wxGenericValidator() | |
76 | { | |
77 | } | |
78 | ||
79 | // Called to transfer data to the window | |
80 | bool wxGenericValidator::TransferToWindow(void) | |
81 | { | |
82 | if ( !m_validatorWindow ) | |
83 | return FALSE; | |
84 | ||
85 | // bool controls | |
86 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) ) | |
87 | { | |
88 | wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow; | |
89 | if (m_pBool) | |
90 | { | |
91 | pControl->SetValue(*m_pBool) ; | |
92 | return TRUE; | |
93 | } | |
94 | } | |
95 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) ) | |
96 | { | |
97 | wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow; | |
98 | if (m_pBool) | |
99 | { | |
100 | pControl->SetValue(*m_pBool) ; | |
101 | return TRUE; | |
102 | } | |
103 | } | |
104 | // int controls | |
105 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) ) | |
106 | { | |
107 | wxGauge* pControl = (wxGauge*) m_validatorWindow; | |
108 | if (m_pInt) | |
109 | { | |
110 | pControl->SetValue(*m_pInt) ; | |
111 | return TRUE; | |
112 | } | |
113 | } | |
114 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) ) | |
115 | { | |
116 | wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow; | |
117 | if (m_pInt) | |
118 | { | |
119 | pControl->SetSelection(*m_pInt) ; | |
120 | return TRUE; | |
121 | } | |
122 | } | |
123 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) ) | |
124 | { | |
125 | wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow; | |
126 | if (m_pInt) | |
127 | { | |
128 | pControl->SetThumbPosition(*m_pInt) ; | |
129 | return TRUE; | |
130 | } | |
131 | } | |
132 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) ) | |
133 | { | |
134 | wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow; | |
135 | if (m_pInt) | |
136 | { | |
137 | pControl->SetValue(*m_pInt) ; | |
138 | return TRUE; | |
139 | } | |
140 | } | |
141 | // string controls | |
142 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) ) | |
143 | { | |
144 | wxButton* pControl = (wxButton*) m_validatorWindow; | |
145 | if (m_pString) | |
146 | { | |
147 | pControl->SetLabel(*m_pString) ; | |
148 | return TRUE; | |
149 | } | |
150 | } | |
151 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) ) | |
152 | { | |
153 | wxComboBox* pControl = (wxComboBox*) m_validatorWindow; | |
154 | if (m_pString) | |
155 | { | |
156 | pControl->SetValue(*m_pString) ; | |
157 | return TRUE; | |
158 | } | |
159 | } | |
160 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) ) | |
161 | { | |
162 | wxChoice* pControl = (wxChoice*) m_validatorWindow; | |
163 | if (m_pInt) | |
164 | { | |
165 | pControl->SetSelection(*m_pInt) ; | |
166 | return TRUE; | |
167 | } | |
168 | } | |
169 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) ) | |
170 | { | |
171 | wxStaticText* pControl = (wxStaticText*) m_validatorWindow; | |
172 | if (m_pString) | |
173 | { | |
174 | pControl->SetLabel(*m_pString) ; | |
175 | return TRUE; | |
176 | } | |
177 | } | |
178 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
179 | { | |
180 | wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow; | |
181 | if (m_pString) | |
182 | { | |
183 | pControl->SetValue(*m_pString) ; | |
184 | return TRUE; | |
185 | } | |
186 | } | |
187 | // array controls | |
188 | // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox | |
189 | // MUST come first: | |
190 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) ) | |
191 | { | |
192 | wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow; | |
193 | if (m_pArrayInt) | |
194 | { | |
195 | // clear all selections | |
196 | int i; | |
197 | for (i = 0 ; i < pControl->Number(); ++i) | |
198 | pControl->Check(i, FALSE); | |
199 | // select each item in our array | |
200 | unsigned u; | |
201 | for (u = 0; u < m_pArrayInt->Count(); ++u) | |
202 | pControl->Check(m_pArrayInt->Item(u)); | |
203 | return TRUE; | |
204 | } | |
205 | else | |
206 | return FALSE; | |
207 | } | |
208 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) ) | |
209 | { | |
210 | wxListBox* pControl = (wxListBox*) m_validatorWindow; | |
211 | if (m_pArrayInt) | |
212 | { | |
213 | // clear all selections | |
214 | int i; | |
215 | for (i = 0 ; i < pControl->Number(); ++i) | |
216 | pControl->Deselect(i); | |
217 | // select each item in our array | |
218 | unsigned u; | |
219 | for (u = 0; u < m_pArrayInt->Count(); ++u) | |
220 | pControl->SetSelection(m_pArrayInt->Item(u)); | |
221 | return TRUE; | |
222 | } | |
223 | } | |
224 | ||
225 | // unrecognized control, or bad pointer | |
226 | return FALSE; | |
227 | } | |
228 | ||
229 | // Called to transfer data to the window | |
230 | bool wxGenericValidator::TransferFromWindow(void) | |
231 | { | |
232 | if ( !m_validatorWindow ) | |
233 | return FALSE; | |
234 | ||
235 | // bool controls | |
236 | if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)) ) | |
237 | { | |
238 | wxCheckBox* pControl = (wxCheckBox*) m_validatorWindow; | |
239 | if (m_pBool) | |
240 | { | |
241 | *m_pBool = pControl->GetValue() ; | |
242 | return TRUE; | |
243 | } | |
244 | } | |
245 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) ) | |
246 | { | |
247 | wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow; | |
248 | if (m_pBool) | |
249 | { | |
250 | *m_pBool = pControl->GetValue() ; | |
251 | return TRUE; | |
252 | } | |
253 | } | |
254 | // int controls | |
255 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) ) | |
256 | { | |
257 | wxGauge* pControl = (wxGauge*) m_validatorWindow; | |
258 | if (m_pInt) | |
259 | { | |
260 | *m_pInt = pControl->GetValue() ; | |
261 | return TRUE; | |
262 | } | |
263 | } | |
264 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) ) | |
265 | { | |
266 | wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow; | |
267 | if (m_pInt) | |
268 | { | |
269 | *m_pInt = pControl->GetSelection() ; | |
270 | return TRUE; | |
271 | } | |
272 | } | |
273 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) ) | |
274 | { | |
275 | wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow; | |
276 | if (m_pInt) | |
277 | { | |
278 | *m_pInt = pControl->GetThumbPosition() ; | |
279 | return TRUE; | |
280 | } | |
281 | } | |
282 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) ) | |
283 | { | |
284 | wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow; | |
285 | if (m_pInt) | |
286 | { | |
287 | *m_pInt = pControl->GetValue() ; | |
288 | return TRUE; | |
289 | } | |
290 | } | |
291 | // string controls | |
292 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) ) | |
293 | { | |
294 | wxButton* pControl = (wxButton*) m_validatorWindow; | |
295 | if (m_pString) | |
296 | { | |
297 | *m_pString = pControl->GetLabel() ; | |
298 | return TRUE; | |
299 | } | |
300 | } | |
301 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) ) | |
302 | { | |
303 | wxComboBox* pControl = (wxComboBox*) m_validatorWindow; | |
304 | if (m_pString) | |
305 | { | |
306 | *m_pString = pControl->GetValue() ; | |
307 | return TRUE; | |
308 | } | |
309 | } | |
310 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) ) | |
311 | { | |
312 | wxChoice* pControl = (wxChoice*) m_validatorWindow; | |
313 | if (m_pInt) | |
314 | { | |
315 | *m_pInt = pControl->GetSelection() ; | |
316 | return TRUE; | |
317 | } | |
318 | } | |
319 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) ) | |
320 | { | |
321 | wxStaticText* pControl = (wxStaticText*) m_validatorWindow; | |
322 | if (m_pString) | |
323 | { | |
324 | *m_pString = pControl->GetLabel() ; | |
325 | return TRUE; | |
326 | } | |
327 | } | |
328 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) ) | |
329 | { | |
330 | wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow; | |
331 | if (m_pString) | |
332 | { | |
333 | *m_pString = pControl->GetValue() ; | |
334 | return TRUE; | |
335 | } | |
336 | } | |
337 | // array controls | |
338 | // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox | |
339 | // MUST come first: | |
340 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) ) | |
341 | { | |
342 | wxCheckListBox* pControl = (wxCheckListBox*) m_validatorWindow; | |
343 | if (m_pArrayInt) | |
344 | { | |
345 | // clear our array | |
346 | m_pArrayInt->Clear(); | |
347 | // add each selected item to our array | |
348 | int i; | |
349 | for (i = 0 ; i < pControl->Number(); ++i) | |
350 | if (pControl->IsChecked(i)) | |
351 | m_pArrayInt->Add(i); | |
352 | return TRUE; | |
353 | } | |
354 | else | |
355 | return FALSE; | |
356 | } | |
357 | else if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) ) | |
358 | { | |
359 | wxListBox* pControl = (wxListBox*) m_validatorWindow; | |
360 | if (m_pArrayInt) | |
361 | { | |
362 | // clear our array | |
363 | m_pArrayInt->Clear(); | |
364 | // add each selected item to our array | |
365 | int i; | |
366 | for (i = 0 ; i < pControl->Number(); ++i) | |
367 | if (pControl->Selected(i)) | |
368 | m_pArrayInt->Add(i); | |
369 | return TRUE; | |
370 | } | |
371 | } | |
372 | ||
373 | // unrecognized control, or bad pointer | |
374 | return FALSE; | |
375 | } | |
376 | ||
377 | /* | |
378 | Called by constructors to initialize ALL data members | |
379 | */ | |
380 | void wxGenericValidator::Initialize() | |
381 | { | |
382 | m_pBool = 0; | |
383 | m_pInt = 0; | |
384 | m_pString = 0; | |
385 | m_pArrayInt = 0; | |
386 | } | |
387 |