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