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