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