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