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