]> git.saurik.com Git - wxWidgets.git/blame - src/common/valgen.cpp
Implement CanRead
[wxWidgets.git] / src / common / valgen.cpp
CommitLineData
89c684ef
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: valgen.cpp
3// Purpose: wxGenericValidator class
4// Author: Kevin Smith
5// Modified by:
6// Created: Jan 22 1999
913df6f2 7// RCS-ID:
89c684ef
JS
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__
ce4169a4 20 #pragma hdrstop
89c684ef
JS
21#endif
22
23#ifndef WX_PRECOMP
ce4169a4
RR
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"
89c684ef
JS
44#endif
45
750b78ba 46#ifndef __WIN16__
ce4169a4
RR
47 #include "wx/spinbutt.h"
48 #include "wx/checklst.h"
750b78ba 49#endif
89c684ef
JS
50
51#include "wx/valgen.h"
52
53wxGenericValidator::wxGenericValidator(bool *val)
54{
ce4169a4
RR
55 Initialize();
56 m_pBool = val;
89c684ef
JS
57}
58
59wxGenericValidator::wxGenericValidator(int *val)
60{
ce4169a4
RR
61 Initialize();
62 m_pInt = val;
89c684ef
JS
63}
64
65wxGenericValidator::wxGenericValidator(wxString *val)
66{
ce4169a4
RR
67 Initialize();
68 m_pString = val;
89c684ef
JS
69}
70
71wxGenericValidator::wxGenericValidator(wxArrayInt *val)
72{
ce4169a4
RR
73 Initialize();
74 m_pArrayInt = val;
89c684ef
JS
75}
76
77wxGenericValidator::wxGenericValidator(const wxGenericValidator& val)
78{
ce4169a4 79 Copy(val);
89c684ef
JS
80}
81
82bool 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
94wxGenericValidator::~wxGenericValidator()
95{
96}
97
98// Called to transfer data to the window
99bool wxGenericValidator::TransferToWindow(void)
100{
101 if ( !m_validatorWindow )
102 return FALSE;
103
104 // bool controls
dcf924a3 105#if wxUSE_CHECKBOX
89c684ef
JS
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 }
dcf924a3
RR
114 } else
115#endif
116#if wxUSE_RADIOBTN
117 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef
JS
118 {
119 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
120 if (m_pBool)
121 {
122 pControl->SetValue(*m_pBool) ;
123 return TRUE;
124 }
dcf924a3
RR
125 } else
126#endif
89c684ef 127 // int controls
dcf924a3
RR
128#if wxUSE_GAUGE
129 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef
JS
130 {
131 wxGauge* pControl = (wxGauge*) m_validatorWindow;
132 if (m_pInt)
133 {
134 pControl->SetValue(*m_pInt) ;
135 return TRUE;
136 }
137 }
913df6f2 138 else
dcf924a3
RR
139#endif
140#if wxUSE_RADIOBOX
141 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef
JS
142 {
143 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
144 if (m_pInt)
145 {
146 pControl->SetSelection(*m_pInt) ;
147 return TRUE;
148 }
149 }
913df6f2 150 else
dcf924a3
RR
151#endif
152#if wxUSE_SCROLLBAR
153 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef
JS
154 {
155 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
156 if (m_pInt)
157 {
158 pControl->SetThumbPosition(*m_pInt) ;
159 return TRUE;
160 }
dcf924a3
RR
161 } else
162#endif
163#if wxUSE_SPINBTN
750b78ba 164#ifndef __WIN16__
dcf924a3 165 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef
JS
166 {
167 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
913df6f2 168 if (m_pInt)
89c684ef
JS
169 {
170 pControl->SetValue(*m_pInt) ;
171 return TRUE;
172 }
dcf924a3
RR
173 } else
174#endif
750b78ba 175#endif
89c684ef 176 // string controls
dcf924a3 177 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef
JS
178 {
179 wxButton* pControl = (wxButton*) m_validatorWindow;
180 if (m_pString)
181 {
182 pControl->SetLabel(*m_pString) ;
183 return TRUE;
184 }
913df6f2 185 } else
dcf924a3
RR
186#if wxUSE_COMBOBOX
187 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef
JS
188 {
189 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
190 if (m_pString)
191 {
192 pControl->SetValue(*m_pString) ;
193 return TRUE;
194 }
ce4169a4 195 } else
dcf924a3 196#endif
ce4169a4
RR
197#if wxUSE_CHOICE
198 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
89c684ef
JS
199 {
200 wxChoice* pControl = (wxChoice*) m_validatorWindow;
201 if (m_pInt)
202 {
203 pControl->SetSelection(*m_pInt) ;
204 return TRUE;
205 }
fae05df5 206 } else
ce4169a4 207#endif
fae05df5 208 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef
JS
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 }
dcf924a3
RR
225 } else
226#if wxUSE_CHECKLISTBOX
750b78ba 227#ifndef __WIN16__
89c684ef
JS
228 // array controls
229 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
230 // MUST come first:
dcf924a3 231 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef
JS
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;
dcf924a3 248 } else
750b78ba 249#endif
dcf924a3
RR
250#endif
251#if wxUSE_LISTBOX
252 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef
JS
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 }
dcf924a3
RR
267 } else
268#endif
89c684ef
JS
269
270 // unrecognized control, or bad pointer
dcf924a3 271 return FALSE;
89c684ef
JS
272 return FALSE;
273}
274
275// Called to transfer data to the window
276bool wxGenericValidator::TransferFromWindow(void)
277{
278 if ( !m_validatorWindow )
279 return FALSE;
280
281 // bool controls
dcf924a3 282#if wxUSE_CHECKBOX
89c684ef
JS
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 }
913df6f2 291 } else
dcf924a3
RR
292#endif
293#if wxUSE_RADIOBTN
294 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioButton)) )
89c684ef
JS
295 {
296 wxRadioButton* pControl = (wxRadioButton*) m_validatorWindow;
297 if (m_pBool)
298 {
299 *m_pBool = pControl->GetValue() ;
300 return TRUE;
301 }
dcf924a3
RR
302 } else
303#endif
89c684ef 304 // int controls
dcf924a3
RR
305#if wxUSE_GAUGE
306 if (m_validatorWindow->IsKindOf(CLASSINFO(wxGauge)) )
89c684ef
JS
307 {
308 wxGauge* pControl = (wxGauge*) m_validatorWindow;
309 if (m_pInt)
310 {
311 *m_pInt = pControl->GetValue() ;
312 return TRUE;
313 }
913df6f2 314 } else
dcf924a3
RR
315#endif
316#if wxUSE_RADIOBOX
317 if (m_validatorWindow->IsKindOf(CLASSINFO(wxRadioBox)) )
89c684ef
JS
318 {
319 wxRadioBox* pControl = (wxRadioBox*) m_validatorWindow;
320 if (m_pInt)
321 {
322 *m_pInt = pControl->GetSelection() ;
323 return TRUE;
324 }
913df6f2 325 } else
dcf924a3
RR
326#endif
327#if wxUSE_SCROLLBAR
328 if (m_validatorWindow->IsKindOf(CLASSINFO(wxScrollBar)) )
89c684ef
JS
329 {
330 wxScrollBar* pControl = (wxScrollBar*) m_validatorWindow;
331 if (m_pInt)
332 {
333 *m_pInt = pControl->GetThumbPosition() ;
334 return TRUE;
335 }
dcf924a3
RR
336 } else
337#endif
338#if wxUSE_SPINBTN
750b78ba 339#ifndef __WIN16__
dcf924a3 340 if (m_validatorWindow->IsKindOf(CLASSINFO(wxSpinButton)) )
89c684ef
JS
341 {
342 wxSpinButton* pControl = (wxSpinButton*) m_validatorWindow;
343 if (m_pInt)
344 {
345 *m_pInt = pControl->GetValue() ;
346 return TRUE;
347 }
dcf924a3
RR
348 } else
349#endif
750b78ba 350#endif
89c684ef 351 // string controls
dcf924a3 352 if (m_validatorWindow->IsKindOf(CLASSINFO(wxButton)) )
89c684ef
JS
353 {
354 wxButton* pControl = (wxButton*) m_validatorWindow;
355 if (m_pString)
356 {
357 *m_pString = pControl->GetLabel() ;
358 return TRUE;
359 }
360 }
913df6f2 361 else
dcf924a3
RR
362#if wxUSE_COMBOBOX
363 if (m_validatorWindow->IsKindOf(CLASSINFO(wxComboBox)) )
89c684ef
JS
364 {
365 wxComboBox* pControl = (wxComboBox*) m_validatorWindow;
366 if (m_pString)
367 {
368 *m_pString = pControl->GetValue() ;
369 return TRUE;
370 }
913df6f2 371 } else
dcf924a3 372#endif
ce4169a4 373#if wxUSE_CHOICE
dcf924a3 374 if (m_validatorWindow->IsKindOf(CLASSINFO(wxChoice)) )
89c684ef
JS
375 {
376 wxChoice* pControl = (wxChoice*) m_validatorWindow;
377 if (m_pInt)
378 {
379 *m_pInt = pControl->GetSelection() ;
380 return TRUE;
381 }
913df6f2 382 } else
ce4169a4 383#endif
dcf924a3 384 if (m_validatorWindow->IsKindOf(CLASSINFO(wxStaticText)) )
89c684ef
JS
385 {
386 wxStaticText* pControl = (wxStaticText*) m_validatorWindow;
387 if (m_pString)
388 {
389 *m_pString = pControl->GetLabel() ;
390 return TRUE;
391 }
913df6f2 392 } else
dcf924a3 393 if (m_validatorWindow->IsKindOf(CLASSINFO(wxTextCtrl)) )
89c684ef
JS
394 {
395 wxTextCtrl* pControl = (wxTextCtrl*) m_validatorWindow;
396 if (m_pString)
397 {
398 *m_pString = pControl->GetValue() ;
399 return TRUE;
400 }
dcf924a3
RR
401 } else
402#if wxUSE_LISTBOX
750b78ba 403#ifndef __WIN16__
89c684ef
JS
404 // array controls
405 // NOTE: wxCheckListBox isa wxListBox, so wxCheckListBox
406 // MUST come first:
dcf924a3 407 if (m_validatorWindow->IsKindOf(CLASSINFO(wxCheckListBox)) )
89c684ef
JS
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;
dcf924a3
RR
423 } else
424#endif
750b78ba 425#endif
dcf924a3
RR
426#if wxUSE_LISTBOX
427 if (m_validatorWindow->IsKindOf(CLASSINFO(wxListBox)) )
89c684ef
JS
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 }
dcf924a3
RR
441 } else
442#endif
89c684ef
JS
443
444 // unrecognized control, or bad pointer
dcf924a3 445 return FALSE;
89c684ef
JS
446 return FALSE;
447}
448
449/*
450 Called by constructors to initialize ALL data members
451*/
452void wxGenericValidator::Initialize()
453{
ce4169a4
RR
454 m_pBool = 0;
455 m_pInt = 0;
456 m_pString = 0;
457 m_pArrayInt = 0;
89c684ef
JS
458}
459
ce4169a4
RR
460#endif
461 // wxUSE_VALIDATORS
913df6f2 462