]> git.saurik.com Git - wxWidgets.git/blame - src/common/xti.cpp
added version checking to the renderers
[wxWidgets.git] / src / common / xti.cpp
CommitLineData
a095505c
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/xti.cpp
3// Purpose: runtime metadata information (extended class info
4// Author: Stefan Csomor
30fd71e6 5// Modified by:
a095505c
SC
6// Created: 27/07/03
7// RCS-ID: $Id$
8// Copyright: (c) 1997 Julian Smart
9// (c) 2003 Stefan Csomor
10// Licence: wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
14f355c2 13#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a095505c
SC
14#pragma implementation "xti.h"
15#endif
16
17// For compilers that support precompilation, includes "wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25 #include "wx/hash.h"
26 #include "wx/object.h"
27#endif
28
29#include "wx/xml/xml.h"
30#include "wx/tokenzr.h"
31
32#include <string.h>
33
30bfc425 34#if wxUSE_EXTENDED_RTTI
a095505c
SC
35
36// ----------------------------------------------------------------------------
37// Enum Support
38// ----------------------------------------------------------------------------
39
30fd71e6 40wxEnumData::wxEnumData( wxEnumMemberData* data )
a095505c 41{
30fd71e6 42 m_members = data ;
a095505c
SC
43 for ( m_count = 0; m_members[m_count].m_name ; m_count++)
44 {} ;
45}
46
47bool wxEnumData::HasEnumMemberValue(const wxChar *name, int *value)
48{
49 int i;
50 for (i = 0; m_members[i].m_name ; i++ )
51 {
52 if (!strcmp(name, m_members[i].m_name))
53 {
54 if ( value )
55 *value = m_members[i].m_value;
56 return true ;
57 }
58 }
59 return false ;
60}
61
62int wxEnumData::GetEnumMemberValue(const wxChar *name)
63{
64 int i;
65 for (i = 0; m_members[i].m_name ; i++ )
66 {
67 if (!strcmp(name, m_members[i].m_name))
68 {
69 return m_members[i].m_value;
70 }
71 }
0c03c79e 72 return 0 ;
a095505c
SC
73}
74
75const wxChar *wxEnumData::GetEnumMemberName(int value)
76{
77 int i;
78 for (i = 0; m_members[i].m_name ; i++)
79 if (value == m_members[i].m_value)
80 return m_members[i].m_name;
81
82 return wxT("") ;
83}
84
30fd71e6 85int wxEnumData::GetEnumMemberValueByIndex( int idx )
a095505c
SC
86{
87 // we should cache the count in order to avoid out-of-bounds errors
88 return m_members[idx].m_value ;
89}
90
30fd71e6 91const char * wxEnumData::GetEnumMemberNameByIndex( int idx )
a095505c
SC
92{
93 // we should cache the count in order to avoid out-of-bounds errors
94 return m_members[idx].m_name ;
95}
96
97// ----------------------------------------------------------------------------
98// Type Information
99// ----------------------------------------------------------------------------
100
0c03c79e 101template<> const wxTypeInfo* wxGetTypeInfo( void * )
a095505c
SC
102{
103 static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ;
104 return &s_typeInfo ;
105}
106
0c03c79e 107template<> const wxTypeInfo* wxGetTypeInfo( bool * )
a095505c
SC
108{
109 static wxBuiltInTypeInfo s_typeInfo( wxT_BOOL ) ;
110 return &s_typeInfo ;
111}
112
0c03c79e 113template<> const wxTypeInfo* wxGetTypeInfo( char * )
a095505c
SC
114{
115 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
116 return &s_typeInfo ;
117}
118
0c03c79e 119template<> const wxTypeInfo* wxGetTypeInfo( unsigned char * )
a095505c
SC
120{
121 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
122 return &s_typeInfo ;
123}
124
0c03c79e 125template<> const wxTypeInfo* wxGetTypeInfo( int * )
a095505c
SC
126{
127 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
128 return &s_typeInfo ;
129}
130
0c03c79e 131template<> const wxTypeInfo* wxGetTypeInfo( unsigned int * )
a095505c
SC
132{
133 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
134 return &s_typeInfo ;
135}
136
0c03c79e 137template<> const wxTypeInfo* wxGetTypeInfo( long * )
a095505c
SC
138{
139 static wxBuiltInTypeInfo s_typeInfo( wxT_CHAR ) ;
140 return &s_typeInfo ;
141}
142
0c03c79e 143template<> const wxTypeInfo* wxGetTypeInfo( unsigned long * )
a095505c
SC
144{
145 static wxBuiltInTypeInfo s_typeInfo( wxT_UCHAR ) ;
146 return &s_typeInfo ;
147}
148
0c03c79e 149template<> const wxTypeInfo* wxGetTypeInfo( float * )
a095505c
SC
150{
151 static wxBuiltInTypeInfo s_typeInfo( wxT_FLOAT ) ;
152 return &s_typeInfo ;
153}
154
0c03c79e 155template<> const wxTypeInfo* wxGetTypeInfo( double * )
a095505c
SC
156{
157 static wxBuiltInTypeInfo s_typeInfo( wxT_DOUBLE ) ;
158 return &s_typeInfo ;
159}
160
0c03c79e 161template<> const wxTypeInfo* wxGetTypeInfo( wxString * )
a095505c
SC
162{
163 static wxBuiltInTypeInfo s_typeInfo( wxT_STRING ) ;
164 return &s_typeInfo ;
165}
166
0c03c79e 167// this are compiler induced specialization which are never used anywhere
a095505c 168
16776ad9
SC
169WX_ILLEGAL_TYPE_SPECIALIZATION( char const * )
170WX_ILLEGAL_TYPE_SPECIALIZATION( char * )
171WX_ILLEGAL_TYPE_SPECIALIZATION( unsigned char * )
172WX_ILLEGAL_TYPE_SPECIALIZATION( int * )
173WX_ILLEGAL_TYPE_SPECIALIZATION( bool * )
174WX_ILLEGAL_TYPE_SPECIALIZATION( long * )
175WX_ILLEGAL_TYPE_SPECIALIZATION( wxString * )
f0b7eadf 176
a095505c 177// ----------------------------------------------------------------------------
30fd71e6 178// value streaming
a095505c
SC
179// ----------------------------------------------------------------------------
180
a095505c 181// streamer specializations
45212047 182// for all built-in types
a095505c 183
45212047 184// bool
a095505c 185
45212047
SC
186template<> void wxStringReadValue(const wxString &s , bool &data )
187{
188 int intdata ;
189 wxSscanf(s, _T("%d"), &intdata ) ;
190 data = bool(intdata) ;
191}
a095505c 192
45212047 193template<> void wxStringWriteValue(wxString &s , const bool &data )
a095505c 194{
45212047 195 s = wxString::Format("%d", data ) ;
a095505c
SC
196}
197
45212047
SC
198// char
199
200template<> void wxStringReadValue(const wxString &s , char &data )
a095505c 201{
45212047
SC
202 int intdata ;
203 wxSscanf(s, _T("%d"), &intdata ) ;
204 data = char(intdata) ;
205}
206
207template<> void wxStringWriteValue(wxString &s , const char &data )
208{
209 s = wxString::Format("%d", data ) ;
210}
211
212// unsigned char
213
214template<> void wxStringReadValue(const wxString &s , unsigned char &data )
215{
216 int intdata ;
217 wxSscanf(s, _T("%d"), &intdata ) ;
66c57129 218 data = (unsigned char)(intdata) ;
45212047
SC
219}
220
221template<> void wxStringWriteValue(wxString &s , const unsigned char &data )
222{
223 s = wxString::Format("%d", data ) ;
a095505c
SC
224}
225
30fd71e6 226// int
a095505c 227
30fd71e6 228template<> void wxStringReadValue(const wxString &s , int &data )
a095505c
SC
229{
230 wxSscanf(s, _T("%d"), &data ) ;
231}
232
30fd71e6 233template<> void wxStringWriteValue(wxString &s , const int &data )
a095505c
SC
234{
235 s = wxString::Format("%d", data ) ;
236}
237
45212047
SC
238// unsigned int
239
240template<> void wxStringReadValue(const wxString &s , unsigned int &data )
241{
242 wxSscanf(s, _T("%d"), &data ) ;
243}
244
245template<> void wxStringWriteValue(wxString &s , const unsigned int &data )
246{
247 s = wxString::Format("%d", data ) ;
248}
249
250// long
251
252template<> void wxStringReadValue(const wxString &s , long &data )
253{
254 wxSscanf(s, _T("%ld"), &data ) ;
255}
256
257template<> void wxStringWriteValue(wxString &s , const long &data )
258{
259 s = wxString::Format("%ld", data ) ;
260}
261
262// unsigned long
263
264template<> void wxStringReadValue(const wxString &s , unsigned long &data )
265{
266 wxSscanf(s, _T("%ld"), &data ) ;
267}
268
269template<> void wxStringWriteValue(wxString &s , const unsigned long &data )
270{
271 s = wxString::Format("%ld", data ) ;
272}
273
274// float
275
276template<> void wxStringReadValue(const wxString &s , float &data )
277{
278 wxSscanf(s, _T("%f"), &data ) ;
279}
280
281template<> void wxStringWriteValue(wxString &s , const float &data )
282{
283 s = wxString::Format("%f", data ) ;
284}
285
286// double
287
288template<> void wxStringReadValue(const wxString &s , double &data )
289{
290 wxSscanf(s, _T("%lf"), &data ) ;
291}
292
293template<> void wxStringWriteValue(wxString &s , const double &data )
294{
295 s = wxString::Format("%lf", data ) ;
296}
297
a095505c
SC
298// wxString
299
30fd71e6 300template<> void wxStringReadValue(const wxString &s , wxString &data )
a095505c
SC
301{
302 data = s ;
303}
304
30fd71e6 305template<> void wxStringWriteValue(wxString &s , const wxString &data )
a095505c
SC
306{
307 s = data ;
308}
309
310/*
311 Custom Data Streaming / Type Infos
312 we will have to add this for all wx non object types, but it is also an example
313 for custom data structures
314*/
315
316// wxPoint
317
30fd71e6 318template<> void wxStringReadValue(const wxString &s , wxPoint &data )
a095505c
SC
319{
320 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
321}
322
30fd71e6 323template<> void wxStringWriteValue(wxString &s , const wxPoint &data )
a095505c
SC
324{
325 s = wxString::Format("%d,%d", data.x , data.y ) ;
326}
327
f0b7eadf
SC
328template<> void wxStringReadValue(const wxString & , wxPoint* & )
329{
330 assert(0) ;
331}
332
333template<> void wxStringWriteValue(wxString & , wxPoint* const & )
334{
335 assert(0) ;
336}
337
a095505c
SC
338WX_CUSTOM_TYPE_INFO(wxPoint)
339
30fd71e6 340template<> void wxStringReadValue(const wxString &s , wxSize &data )
fbbdc52c
SC
341{
342 wxSscanf(s, _T("%d,%d"), &data.x , &data.y ) ;
343}
344
30fd71e6 345template<> void wxStringWriteValue(wxString &s , const wxSize &data )
fbbdc52c
SC
346{
347 s = wxString::Format("%d,%d", data.x , data.y ) ;
348}
349
f0b7eadf
SC
350template<> void wxStringReadValue(const wxString & , wxSize* & )
351{
352 assert(0) ;
353}
354
355template<> void wxStringWriteValue(wxString & , wxSize * const & )
356{
357 assert(0) ;
358}
359
fbbdc52c
SC
360WX_CUSTOM_TYPE_INFO(wxSize)
361
45212047
SC
362/*
363
364template<> void wxStringReadValue(const wxString &s , wxColour &data )
365{
366 // copied from VS xrc
367 unsigned long tmp = 0;
368
369 if (s.Length() != 7 || s[0u] != wxT('#') ||
370 wxSscanf(s.c_str(), wxT("#%lX"), &tmp) != 1)
371 {
372 wxLogError(_("String To Colour : Incorrect colour specification : %s"),
373 s.c_str() );
374 data = wxNullColour;
375 }
376 else
377 {
378 data = wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
379 (unsigned char) ((tmp & 0x00FF00) >> 8),
380 (unsigned char) ((tmp & 0x0000FF)));
381 }
382}
383
384template<> void wxStringWriteValue(wxString &s , const wxColour &data )
385{
386 s = wxString::Format("#%2X%2X%2X", data.Red() , data.Green() , data.Blue() ) ;
387}
388
389WX_CUSTOM_TYPE_INFO(wxColour)
390
391*/
392
a095505c
SC
393// removing header dependancy on string tokenizer
394
30fd71e6 395void wxSetStringToArray( const wxString &s , wxArrayString &array )
a095505c
SC
396{
397 wxStringTokenizer tokenizer(s, wxT("| \t\n"), wxTOKEN_STRTOK);
398 wxString flag;
399 array.Clear() ;
400 while (tokenizer.HasMoreTokens())
401 {
402 array.Add(tokenizer.GetNextToken()) ;
403 }
404}
405
406// ----------------------------------------------------------------------------
30fd71e6 407// wxClassInfo
a095505c
SC
408// ----------------------------------------------------------------------------
409
a095505c
SC
410const wxPropertyAccessor *wxClassInfo::FindAccessor(const char *PropertyName)
411{
fbbdc52c 412 const wxPropertyInfo* info = FindPropertyInfo( PropertyName ) ;
30fd71e6 413
a095505c
SC
414 if ( info )
415 return info->GetAccessor() ;
416
417 return NULL ;
418}
419
fbbdc52c 420const wxPropertyInfo *wxClassInfo::FindPropertyInfo (const char *PropertyName) const
a095505c
SC
421{
422 const wxPropertyInfo* info = GetFirstProperty() ;
423
424 while( info )
425 {
426 if ( strcmp( info->GetName() , PropertyName ) == 0 )
427 return info ;
428 info = info->GetNext() ;
429 }
430
431 const wxClassInfo** parents = GetParents() ;
432 for ( int i = 0 ; parents[i] ; ++ i )
433 {
fbbdc52c 434 if ( ( info = parents[i]->FindPropertyInfo( PropertyName ) ) != NULL )
a095505c
SC
435 return info ;
436 }
437
438 return 0;
439}
440
fbbdc52c
SC
441const wxHandlerInfo *wxClassInfo::FindHandlerInfo (const char *PropertyName) const
442{
443 const wxHandlerInfo* info = GetFirstHandler() ;
444
445 while( info )
446 {
447 if ( strcmp( info->GetName() , PropertyName ) == 0 )
448 return info ;
449 info = info->GetNext() ;
450 }
451
452 const wxClassInfo** parents = GetParents() ;
453 for ( int i = 0 ; parents[i] ; ++ i )
454 {
455 if ( ( info = parents[i]->FindHandlerInfo( PropertyName ) ) != NULL )
456 return info ;
457 }
458
459 return 0;
460}
461
462
a095505c
SC
463void wxClassInfo::SetProperty(wxObject *object, const char *propertyName, const wxxVariant &value)
464{
465 const wxPropertyAccessor *accessor;
466
467 accessor = FindAccessor(propertyName);
468 wxASSERT(accessor->HasSetter());
469 accessor->SetProperty( object , value ) ;
470}
471
472wxxVariant wxClassInfo::GetProperty(wxObject *object, const char *propertyName)
473{
474 const wxPropertyAccessor *accessor;
475
476 accessor = FindAccessor(propertyName);
477 wxASSERT(accessor->HasGetter());
478 return accessor->GetProperty(object);
479}
480
481/*
482VARIANT TO OBJECT
483*/
484
66c57129 485wxObject* wxxVariant::GetAsObject()
a095505c
SC
486{
487 const wxClassTypeInfo *ti = dynamic_cast<const wxClassTypeInfo*>( m_data->GetTypeInfo() ) ;
488 if ( ti )
489 return ti->GetClassInfo()->VariantToInstance(*this) ;
490 else
491 return NULL ;
492}
493
494
495#endif