]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xmlres.cpp
check the validity of wxString pointer itself too
[wxWidgets.git] / src / xrc / xmlres.cpp
CommitLineData
78d14f80
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xmlres.cpp
b5d6954b 3// Purpose: XRC resources
78d14f80
VS
4// Author: Vaclav Slavik
5// Created: 2000/03/05
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Vaclav Slavik
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
c575e45a 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
78d14f80
VS
12#pragma implementation "xmlres.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
621be1ec 22#if wxUSE_XRC
a1e4ec87 23
e7a3a5a5 24#ifndef __WXWINCE__
1df61962 25#include <locale.h>
e7a3a5a5 26#endif
1df61962 27
78d14f80
VS
28#include "wx/dialog.h"
29#include "wx/panel.h"
30#include "wx/frame.h"
31#include "wx/wfstream.h"
32#include "wx/filesys.h"
317a0d73 33#include "wx/filename.h"
78d14f80
VS
34#include "wx/log.h"
35#include "wx/intl.h"
36#include "wx/tokenzr.h"
37#include "wx/fontenum.h"
38#include "wx/module.h"
39#include "wx/bitmap.h"
40#include "wx/image.h"
41#include "wx/fontmap.h"
af1337b0 42#include "wx/artprov.h"
32efded3 43#include "wx/settings.h"
78d14f80 44
34c6bbee 45#include "wx/xml/xml.h"
78d14f80
VS
46#include "wx/xrc/xmlres.h"
47
48#include "wx/arrimpl.cpp"
49WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
50
51
824e8eaa
VS
52wxXmlResource *wxXmlResource::ms_instance = NULL;
53
54/*static*/ wxXmlResource *wxXmlResource::Get()
55{
56 if ( !ms_instance )
57 ms_instance = new wxXmlResource;
58 return ms_instance;
59}
60
61/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
62{
63 wxXmlResource *old = ms_instance;
64 ms_instance = res;
65 return old;
66}
67
daa85ee3 68wxXmlResource::wxXmlResource(int flags)
78d14f80 69{
daa85ee3 70 m_flags = flags;
78d14f80
VS
71 m_version = -1;
72}
73
daa85ee3 74wxXmlResource::wxXmlResource(const wxString& filemask, int flags)
78d14f80 75{
daa85ee3 76 m_flags = flags;
78d14f80 77 m_version = -1;
78d14f80
VS
78 Load(filemask);
79}
80
81wxXmlResource::~wxXmlResource()
82{
83 ClearHandlers();
84}
85
86
60fd818a
VZ
87/* static */
88wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
89{
90 wxString fnd(filename);
91
92 // NB: as Load() and Unload() accept both filenames and URLs (should
93 // probably be changed to filenames only, but embedded resources
94 // currently rely on its ability to handle URLs - FIXME) we need to
95 // determine whether found name is filename and not URL and this is the
96 // fastest/simplest way to do it
97 if (wxFileName::FileExists(fnd))
98 {
99 // Make the name absolute filename, because the app may
100 // change working directory later:
101 wxFileName fn(fnd);
102 if (fn.IsRelative())
103 {
104 fn.MakeAbsolute();
105 fnd = fn.GetFullPath();
106 }
107#if wxUSE_FILESYSTEM
108 fnd = wxFileSystem::FileNameToURL(fnd);
109#endif
110 }
111
112 return fnd;
113}
114
115#if wxUSE_FILESYSTEM
116
117/* static */
118bool wxXmlResource::IsArchive(const wxString& filename)
119{
120 const wxString fnd = filename.Lower();
121
122 return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs"));
123}
124
125#endif // wxUSE_FILESYSTEM
126
78d14f80
VS
127bool wxXmlResource::Load(const wxString& filemask)
128{
129 wxString fnd;
130 wxXmlResourceDataRecord *drec;
131 bool iswild = wxIsWild(filemask);
f80ea77b 132 bool rt = true;
78d14f80
VS
133
134#if wxUSE_FILESYSTEM
135 wxFileSystem fsys;
136# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
137# define wxXmlFindNext fsys.FindNext()
138#else
139# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
140# define wxXmlFindNext wxFindNextFile()
141#endif
142 if (iswild)
143 fnd = wxXmlFindFirst;
144 else
145 fnd = filemask;
ec157c8f 146 while (!fnd.empty())
78d14f80 147 {
60fd818a 148 fnd = ConvertFileNameToURL(fnd);
b380439d 149
78d14f80 150#if wxUSE_FILESYSTEM
60fd818a 151 if ( IsArchive(fnd) )
78d14f80 152 {
f9d243a3 153 rt = rt && Load(fnd + wxT("#zip:*.xrc"));
78d14f80 154 }
60fd818a
VZ
155 else // a single resource URL
156#endif // wxUSE_FILESYSTEM
78d14f80
VS
157 {
158 drec = new wxXmlResourceDataRecord;
159 drec->File = fnd;
160 m_data.Add(drec);
161 }
162
163 if (iswild)
164 fnd = wxXmlFindNext;
165 else
166 fnd = wxEmptyString;
167 }
168# undef wxXmlFindFirst
169# undef wxXmlFindNext
d614f51b 170 return rt && UpdateResources();
78d14f80
VS
171}
172
60fd818a
VZ
173bool wxXmlResource::Unload(const wxString& filename)
174{
175 wxASSERT_MSG( !wxIsWild(filename),
176 _T("wildcards not supported by wxXmlResource::Unload()") );
177
178 wxString fnd = ConvertFileNameToURL(filename);
179#if wxUSE_FILESYSTEM
180 const bool isArchive = IsArchive(fnd);
181 if ( isArchive )
182 fnd += _T("#zip:");
183#endif // wxUSE_FILESYSTEM
184
185 bool unloaded = false;
186 const size_t count = m_data.GetCount();
187 for ( size_t i = 0; i < count; i++ )
188 {
189#if wxUSE_FILESYSTEM
190 if ( isArchive )
191 {
192 if ( m_data[i].File.StartsWith(fnd) )
193 unloaded = true;
194 // don't break from the loop, we can have other matching files
195 }
196 else // a single resource URL
197#endif // wxUSE_FILESYSTEM
198 {
199 if ( m_data[i].File == fnd )
200 {
201 m_data.RemoveAt(i);
202 unloaded = true;
203
204 // no sense in continuing, there is only one file with this URL
205 break;
206 }
207 }
208 }
209
210 return unloaded;
211}
212
78d14f80 213
854e189f 214IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject)
78d14f80
VS
215
216void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
217{
218 m_handlers.Append(handler);
219 handler->SetParentResource(this);
220}
221
92e898b0
RD
222void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
223{
224 m_handlers.Insert(handler);
225 handler->SetParentResource(this);
226}
227
78d14f80
VS
228
229
230void wxXmlResource::ClearHandlers()
231{
461932ae 232 WX_CLEAR_LIST(wxList, m_handlers);
78d14f80
VS
233}
234
235
78d14f80
VS
236wxMenu *wxXmlResource::LoadMenu(const wxString& name)
237{
238 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
239}
240
241
242
4a1b9596 243wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
78d14f80 244{
4a1b9596 245 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
78d14f80
VS
246}
247
248
249
4a1b9596 250#if wxUSE_TOOLBAR
78d14f80
VS
251wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
252{
253 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
254}
4a1b9596 255#endif
78d14f80
VS
256
257
258wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
259{
4dd75a6a 260 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
78d14f80
VS
261}
262
263bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
264{
265 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
266}
267
268
269
270wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
271{
272 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
273}
274
275bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
276{
277 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
278}
279
92e898b0
RD
280wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
281{
282 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
283}
284
78d14f80
VS
285bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
286{
287 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
288}
289
290wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
291{
292 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
293 FindResource(name, wxT("wxBitmap")), NULL, NULL);
294 wxBitmap rt;
295
296 if (bmp) { rt = *bmp; delete bmp; }
297 return rt;
298}
299
300wxIcon wxXmlResource::LoadIcon(const wxString& name)
301{
302 wxIcon *icon = (wxIcon*)CreateResFromNode(
303 FindResource(name, wxT("wxIcon")), NULL, NULL);
304 wxIcon rt;
305
306 if (icon) { rt = *icon; delete icon; }
307 return rt;
308}
309
92e898b0
RD
310
311wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname)
312{
313 return CreateResFromNode(FindResource(name, classname), parent, NULL);
314}
315
316bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname)
317{
318 return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL;
319}
320
321
78d14f80
VS
322bool wxXmlResource::AttachUnknownControl(const wxString& name,
323 wxWindow *control, wxWindow *parent)
324{
325 if (parent == NULL)
326 parent = control->GetParent();
327 wxWindow *container = parent->FindWindow(name + wxT("_container"));
328 if (!container)
329 {
330 wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str());
f80ea77b 331 return false;
78d14f80
VS
332 }
333 return control->Reparent(container);
334}
335
336
77b2f9b1 337static void ProcessPlatformProperty(wxXmlNode *node)
78d14f80
VS
338{
339 wxString s;
340 bool isok;
341
342 wxXmlNode *c = node->GetChildren();
343 while (c)
344 {
f80ea77b 345 isok = false;
78d14f80 346 if (!c->GetPropVal(wxT("platform"), &s))
f80ea77b 347 isok = true;
78d14f80
VS
348 else
349 {
2b5f62a0 350 wxStringTokenizer tkn(s, wxT(" |"));
78d14f80
VS
351
352 while (tkn.HasMoreTokens())
353 {
354 s = tkn.GetNextToken();
84389518 355#ifdef __WINDOWS__
d003330c
VS
356 if (s == wxT("win")) isok = true;
357#endif
b380439d 358#if defined(__MAC__) || defined(__APPLE__)
d003330c 359 if (s == wxT("mac")) isok = true;
b380439d
RD
360#elif defined(__UNIX__)
361 if (s == wxT("unix")) isok = true;
78d14f80 362#endif
d003330c
VS
363#ifdef __OS2__
364 if (s == wxT("os2")) isok = true;
365#endif
366
367 if (isok)
368 break;
78d14f80
VS
369 }
370 }
371
372 if (isok)
d7b1d73c 373 {
78d14f80 374 ProcessPlatformProperty(c);
d7b1d73c
VS
375 c = c->GetNext();
376 }
78d14f80
VS
377 else
378 {
d7b1d73c 379 wxXmlNode *c2 = c->GetNext();
db59a97c 380 node->RemoveChild(c);
78d14f80 381 delete c;
d7b1d73c 382 c = c2;
78d14f80 383 }
78d14f80
VS
384 }
385}
386
387
388
d614f51b 389bool wxXmlResource::UpdateResources()
78d14f80 390{
d614f51b 391 bool rt = true;
78d14f80
VS
392 bool modif;
393# if wxUSE_FILESYSTEM
394 wxFSFile *file = NULL;
19d0f58d 395 wxUnusedVar(file);
78d14f80
VS
396 wxFileSystem fsys;
397# endif
398
480505bc
VS
399 wxString encoding(wxT("UTF-8"));
400#if !wxUSE_UNICODE && wxUSE_INTL
401 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
402 {
75d38380
VS
403 // In case we are not using wxLocale to translate strings, convert the
404 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
405 // is on, because it could break wxGetTranslation lookup.
480505bc
VS
406 encoding = wxLocale::GetSystemEncodingName();
407 }
408#endif
409
78d14f80
VS
410 for (size_t i = 0; i < m_data.GetCount(); i++)
411 {
412 modif = (m_data[i].Doc == NULL);
413
648db587 414 if (!modif && !(m_flags & wxXRC_NO_RELOADING))
78d14f80
VS
415 {
416# if wxUSE_FILESYSTEM
417 file = fsys.OpenFile(m_data[i].File);
418 modif = file && file->GetModificationTime() > m_data[i].Time;
419 if (!file)
d614f51b 420 {
78d14f80 421 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
d614f51b
VS
422 rt = false;
423 }
78d14f80 424 wxDELETE(file);
19d0f58d 425 wxUnusedVar(file);
78d14f80
VS
426# else
427 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
428# endif
429 }
430
431 if (modif)
432 {
648db587
VS
433 wxLogTrace(_T("xrc"),
434 _T("opening file '%s'"), m_data[i].File.c_str());
435
480505bc 436 wxInputStream *stream = NULL;
78d14f80
VS
437
438# if wxUSE_FILESYSTEM
439 file = fsys.OpenFile(m_data[i].File);
f80ea77b
WS
440 if (file)
441 stream = file->GetStream();
78d14f80
VS
442# else
443 stream = new wxFileInputStream(m_data[i].File);
444# endif
445
446 if (stream)
447 {
448 delete m_data[i].Doc;
449 m_data[i].Doc = new wxXmlDocument;
450 }
4d876ee3 451 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
78d14f80 452 {
480505bc
VS
453 wxLogError(_("Cannot load resources from file '%s'."),
454 m_data[i].File.c_str());
78d14f80 455 wxDELETE(m_data[i].Doc);
d614f51b 456 rt = false;
78d14f80
VS
457 }
458 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
459 {
b5d6954b 460 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
78d14f80 461 wxDELETE(m_data[i].Doc);
d614f51b 462 rt = false;
78d14f80
VS
463 }
464 else
f80ea77b 465 {
78d14f80
VS
466 long version;
467 int v1, v2, v3, v4;
468 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
469 wxT("version"), wxT("0.0.0.0"));
470 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
471 &v1, &v2, &v3, &v4) == 4)
472 version = v1*256*256*256+v2*256*256+v3*256+v4;
473 else
474 version = 0;
475 if (m_version == -1)
476 m_version = version;
477 if (m_version != version)
d614f51b 478 {
78d14f80 479 wxLogError(_("Resource files must have same version number!"));
d614f51b
VS
480 rt = false;
481 }
78d14f80
VS
482
483 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
496f0a58 484#if wxUSE_FILESYSTEM
f80ea77b 485 m_data[i].Time = file->GetModificationTime();
496f0a58
VS
486#else
487 m_data[i].Time = wxDateTime(wxFileModificationTime(m_data[i].File));
488#endif
f80ea77b 489 }
78d14f80
VS
490
491# if wxUSE_FILESYSTEM
f80ea77b
WS
492 wxDELETE(file);
493 wxUnusedVar(file);
78d14f80 494# else
f80ea77b 495 wxDELETE(stream);
78d14f80
VS
496# endif
497 }
498 }
d614f51b
VS
499
500 return rt;
78d14f80
VS
501}
502
503
b272b6dc
RD
504wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
505 const wxString& name,
506 const wxString& classname,
47793ab8
VS
507 bool recursive)
508{
509 wxString dummy;
510 wxXmlNode *node;
511
512 // first search for match at the top-level nodes (as this is
513 // where the resource is most commonly looked for):
514 for (node = parent->GetChildren(); node; node = node->GetNext())
515 {
b272b6dc
RD
516 if ( node->GetType() == wxXML_ELEMENT_NODE &&
517 (node->GetName() == wxT("object") ||
47793ab8 518 node->GetName() == wxT("object_ref")) &&
2b5f62a0
VZ
519 node->GetPropVal(wxT("name"), &dummy) && dummy == name )
520 {
521 wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
522 if (!classname || cls == classname)
523 return node;
524 // object_ref may not have 'class' property:
525 if (cls.empty() && node->GetName() == wxT("object_ref"))
526 {
527 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
528 if (refName.empty())
529 continue;
f80ea77b 530 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
2b5f62a0
VZ
531 if (refNode &&
532 refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
533 {
534 return node;
535 }
536 }
537 }
47793ab8
VS
538 }
539
540 if ( recursive )
541 for (node = parent->GetChildren(); node; node = node->GetNext())
542 {
b272b6dc
RD
543 if ( node->GetType() == wxXML_ELEMENT_NODE &&
544 (node->GetName() == wxT("object") ||
47793ab8
VS
545 node->GetName() == wxT("object_ref")) )
546 {
f80ea77b 547 wxXmlNode* found = DoFindResource(node, name, classname, true);
47793ab8
VS
548 if ( found )
549 return found;
550 }
551 }
552
553 return NULL;
554}
78d14f80 555
b272b6dc 556wxXmlNode *wxXmlResource::FindResource(const wxString& name,
47793ab8
VS
557 const wxString& classname,
558 bool recursive)
78d14f80
VS
559{
560 UpdateResources(); //ensure everything is up-to-date
561
562 wxString dummy;
563 for (size_t f = 0; f < m_data.GetCount(); f++)
564 {
47793ab8
VS
565 if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
566 continue;
567
b272b6dc 568 wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
47793ab8
VS
569 name, classname, recursive);
570 if ( found )
571 {
78d14f80 572#if wxUSE_FILESYSTEM
47793ab8 573 m_curFileSystem.ChangePathTo(m_data[f].File);
78d14f80 574#endif
47793ab8
VS
575 return found;
576 }
78d14f80
VS
577 }
578
b5d6954b 579 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
78d14f80
VS
580 name.c_str(), classname.c_str());
581 return NULL;
582}
583
47793ab8
VS
584static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
585{
586 // Merge properties:
587 for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
588 {
589 wxXmlProperty *dprop;
590 for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
591 {
b272b6dc 592
47793ab8
VS
593 if ( dprop->GetName() == prop->GetName() )
594 {
595 dprop->SetValue(prop->GetValue());
596 break;
597 }
598 }
78d14f80 599
47793ab8
VS
600 if ( !dprop )
601 dest.AddProperty(prop->GetName(), prop->GetValue());
602 }
603
604 // Merge child nodes:
605 for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
606 {
607 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
608 wxXmlNode *dnode;
609
610 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
611 {
612 if ( dnode->GetName() == node->GetName() &&
2b5f62a0 613 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
47793ab8
VS
614 dnode->GetType() == node->GetType() )
615 {
616 MergeNodes(*dnode, *node);
617 break;
618 }
619 }
620
621 if ( !dnode )
622 dest.AddChild(new wxXmlNode(*node));
623 }
624
625 if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().Length() )
626 dest.SetContent(with.GetContent());
627}
78d14f80 628
317a0d73
VS
629wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
630 wxObject *instance,
631 wxXmlResourceHandler *handlerToUse)
78d14f80
VS
632{
633 if (node == NULL) return NULL;
634
47793ab8
VS
635 // handling of referenced resource
636 if ( node->GetName() == wxT("object_ref") )
637 {
638 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
f80ea77b 639 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
47793ab8
VS
640
641 if ( !refNode )
642 {
b272b6dc 643 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
47793ab8
VS
644 refName.c_str());
645 return NULL;
646 }
647
648 wxXmlNode copy(*refNode);
649 MergeNodes(copy, *node);
650
651 return CreateResFromNode(&copy, parent, instance);
652 }
653
78d14f80 654 wxXmlResourceHandler *handler;
317a0d73
VS
655
656 if (handlerToUse)
b380439d 657 {
317a0d73 658 if (handlerToUse->CanHandle(node))
78d14f80 659 {
317a0d73
VS
660 return handlerToUse->CreateResource(node, parent, instance);
661 }
662 }
663 else if (node->GetName() == wxT("object"))
b380439d 664 {
461932ae 665 wxList::compatibility_iterator ND = m_handlers.GetFirst();
317a0d73
VS
666 while (ND)
667 {
668 handler = (wxXmlResourceHandler*)ND->GetData();
669 if (handler->CanHandle(node))
670 {
671 return handler->CreateResource(node, parent, instance);
672 }
673 ND = ND->GetNext();
78d14f80 674 }
78d14f80
VS
675 }
676
677 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
678 node->GetName().c_str(),
679 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
680 return NULL;
681}
682
683
2b5f62a0
VZ
684#include "wx/listimpl.cpp"
685WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
686WX_DEFINE_LIST(wxXmlSubclassFactoriesList);
687
688wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;
689
690/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
691{
692 if (!ms_subclassFactories)
693 {
694 ms_subclassFactories = new wxXmlSubclassFactoriesList;
2b5f62a0
VZ
695 }
696 ms_subclassFactories->Append(factory);
697}
698
699class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
700{
701public:
702 ~wxXmlSubclassFactoryCXX() {}
703
704 wxObject *Create(const wxString& className)
705 {
706 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
707
708 if (classInfo)
709 return classInfo->CreateObject();
710 else
711 return NULL;
712 }
713};
714
715
716
78d14f80 717
78d14f80
VS
718wxXmlResourceHandler::wxXmlResourceHandler()
719 : m_node(NULL), m_parent(NULL), m_instance(NULL),
9a8d8c5a 720 m_parentAsWindow(NULL)
78d14f80
VS
721{}
722
723
724
725wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
726{
727 wxXmlNode *myNode = m_node;
728 wxString myClass = m_class;
729 wxObject *myParent = m_parent, *myInstance = m_instance;
9a8d8c5a 730 wxWindow *myParentAW = m_parentAsWindow;
78d14f80 731
daa85ee3 732 m_instance = instance;
b272b6dc 733 if (!m_instance && node->HasProp(wxT("subclass")) &&
daa85ee3
VS
734 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
735 {
736 wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
2b5f62a0 737 if (!subclass.empty())
daa85ee3 738 {
461932ae 739 for (wxXmlSubclassFactoriesList::compatibility_iterator i = wxXmlResource::ms_subclassFactories->GetFirst();
2b5f62a0
VZ
740 i; i = i->GetNext())
741 {
742 m_instance = i->GetData()->Create(subclass);
743 if (m_instance)
744 break;
745 }
daa85ee3 746
2b5f62a0
VZ
747 if (!m_instance)
748 {
749 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
750 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
751 subclass.c_str(), name.c_str());
752 }
753 }
daa85ee3
VS
754 }
755
78d14f80
VS
756 m_node = node;
757 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
758 m_parent = parent;
78d14f80 759 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
78d14f80
VS
760
761 wxObject *returned = DoCreateResource();
762
763 m_node = myNode;
764 m_class = myClass;
765 m_parent = myParent; m_parentAsWindow = myParentAW;
9a8d8c5a 766 m_instance = myInstance;
78d14f80
VS
767
768 return returned;
769}
770
771
772void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
773{
774 m_styleNames.Add(name);
775 m_styleValues.Add(value);
776}
777
778
779
780void wxXmlResourceHandler::AddWindowStyles()
781{
9dc579b3 782 XRC_ADD_STYLE(wxCLIP_CHILDREN);
daa85ee3
VS
783 XRC_ADD_STYLE(wxSIMPLE_BORDER);
784 XRC_ADD_STYLE(wxSUNKEN_BORDER);
785 XRC_ADD_STYLE(wxDOUBLE_BORDER);
786 XRC_ADD_STYLE(wxRAISED_BORDER);
787 XRC_ADD_STYLE(wxSTATIC_BORDER);
788 XRC_ADD_STYLE(wxNO_BORDER);
789 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
790 XRC_ADD_STYLE(wxWANTS_CHARS);
43840d8b 791 XRC_ADD_STYLE(wxTAB_TRAVERSAL);
daa85ee3 792 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
7539ba56 793 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE);
9f4ed861 794 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS);
b0802e64 795 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
78d14f80
VS
796}
797
798
799
800bool wxXmlResourceHandler::HasParam(const wxString& param)
801{
802 return (GetParamNode(param) != NULL);
803}
804
805
806int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
807{
808 wxString s = GetParamValue(param);
809
810 if (!s) return defaults;
811
2b5f62a0 812 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
78d14f80
VS
813 int style = 0;
814 int index;
815 wxString fl;
816 while (tkn.HasMoreTokens())
817 {
818 fl = tkn.GetNextToken();
819 index = m_styleNames.Index(fl);
820 if (index != wxNOT_FOUND)
821 style |= m_styleValues[index];
822 else
823 wxLogError(_("Unknown style flag ") + fl);
824 }
825 return style;
826}
827
828
829
ee1046d1 830wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
78d14f80 831{
7b56015f
VS
832 wxXmlNode *parNode = GetParamNode(param);
833 wxString str1(GetNodeContent(parNode));
78d14f80
VS
834 wxString str2;
835 const wxChar *dt;
836 wxChar amp_char;
837
b272b6dc
RD
838 // VS: First version of XRC resources used $ instead of & (which is
839 // illegal in XML), but later I realized that '_' fits this purpose
718cf160 840 // much better (because &File means "File with F underlined").
78d14f80
VS
841 if (m_resource->CompareVersion(2,3,0,1) < 0)
842 amp_char = wxT('$');
843 else
844 amp_char = wxT('_');
845
846 for (dt = str1.c_str(); *dt; dt++)
847 {
848 // Remap amp_char to &, map double amp_char to amp_char (for things
849 // like "&File..." -- this is illegal in XML, so we use "_File..."):
850 if (*dt == amp_char)
851 {
852 if ( *(++dt) == amp_char )
853 str2 << amp_char;
854 else
855 str2 << wxT('&') << *dt;
856 }
984c33c9 857 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
78d14f80
VS
858 else if (*dt == wxT('\\'))
859 switch (*(++dt))
860 {
984c33c9
VS
861 case wxT('n'):
862 str2 << wxT('\n');
863 break;
e7a3a5a5 864
984c33c9
VS
865 case wxT('t'):
866 str2 << wxT('\t');
867 break;
e7a3a5a5 868
984c33c9
VS
869 case wxT('r'):
870 str2 << wxT('\r');
871 break;
872
873 case wxT('\\') :
874 // "\\" wasn't translated to "\" prior to 2.5.3.0:
875 if (m_resource->CompareVersion(2,5,3,0) >= 0)
876 {
877 str2 << wxT('\\');
878 break;
879 }
880 // else fall-through to default: branch below
e7a3a5a5 881
984c33c9
VS
882 default:
883 str2 << wxT('\\') << *dt;
884 break;
78d14f80
VS
885 }
886 else str2 << *dt;
887 }
b272b6dc 888
7b56015f
VS
889 if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
890 {
891 if (translate && parNode &&
892 parNode->GetPropVal(wxT("translate"), wxEmptyString) != wxT("0"))
893 {
894 return wxGetTranslation(str2);
895 }
896 else
897 {
898#if wxUSE_UNICODE
899 return str2;
900#else
901 // The string is internally stored as UTF-8, we have to convert
902 // it into system's default encoding so that it can be displayed:
903 return wxString(str2.mb_str(wxConvUTF8), wxConvLocal);
904#endif
905 }
906 }
8516a98b
DS
907
908 // If wxXRC_USE_LOCALE is not set, then the string is already in
909 // system's default encoding in ANSI build, so we don't have to
910 // do anything special here.
911 return str2;
78d14f80
VS
912}
913
914
915
916long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
917{
918 long value;
919 wxString str1 = GetParamValue(param);
920
921 if (!str1.ToLong(&value))
922 value = defaultv;
923
924 return value;
925}
e7a3a5a5 926
1df61962
VS
927float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv)
928{
929 double value;
930 wxString str1 = GetParamValue(param);
931
e7a3a5a5 932#ifndef __WXWINCE__
1df61962 933 const char *prevlocale = setlocale(LC_NUMERIC, "C");
e7a3a5a5
WS
934#endif
935
1df61962
VS
936 if (!str1.ToDouble(&value))
937 value = defaultv;
938
e7a3a5a5 939#ifndef __WXWINCE__
1df61962 940 setlocale(LC_NUMERIC, prevlocale);
e7a3a5a5 941#endif
78d14f80 942
1df61962
VS
943 return value;
944}
78d14f80 945
af1337b0 946
78d14f80
VS
947int wxXmlResourceHandler::GetID()
948{
13de23f6 949 return wxXmlResource::GetXRCID(GetName());
78d14f80
VS
950}
951
952
af1337b0 953
78d14f80
VS
954wxString wxXmlResourceHandler::GetName()
955{
956 return m_node->GetPropVal(wxT("name"), wxT("-1"));
957}
958
959
960
961bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
962{
963 wxString v = GetParamValue(param);
964 v.MakeLower();
965 if (!v) return defaultv;
8516a98b
DS
966
967 return (v == wxT("1"));
78d14f80
VS
968}
969
970
1df61962
VS
971static wxColour GetSystemColour(const wxString& name)
972{
973 if (!name.empty())
974 {
975 #define SYSCLR(clr) \
976 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
977 SYSCLR(wxSYS_COLOUR_SCROLLBAR)
978 SYSCLR(wxSYS_COLOUR_BACKGROUND)
979 SYSCLR(wxSYS_COLOUR_DESKTOP)
980 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION)
981 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION)
982 SYSCLR(wxSYS_COLOUR_MENU)
983 SYSCLR(wxSYS_COLOUR_WINDOW)
984 SYSCLR(wxSYS_COLOUR_WINDOWFRAME)
985 SYSCLR(wxSYS_COLOUR_MENUTEXT)
986 SYSCLR(wxSYS_COLOUR_WINDOWTEXT)
987 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT)
988 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER)
989 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER)
990 SYSCLR(wxSYS_COLOUR_APPWORKSPACE)
991 SYSCLR(wxSYS_COLOUR_HIGHLIGHT)
992 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT)
993 SYSCLR(wxSYS_COLOUR_BTNFACE)
994 SYSCLR(wxSYS_COLOUR_3DFACE)
995 SYSCLR(wxSYS_COLOUR_BTNSHADOW)
996 SYSCLR(wxSYS_COLOUR_3DSHADOW)
997 SYSCLR(wxSYS_COLOUR_GRAYTEXT)
998 SYSCLR(wxSYS_COLOUR_BTNTEXT)
999 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
1000 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT)
1001 SYSCLR(wxSYS_COLOUR_BTNHILIGHT)
1002 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT)
1003 SYSCLR(wxSYS_COLOUR_3DHILIGHT)
1004 SYSCLR(wxSYS_COLOUR_3DDKSHADOW)
1005 SYSCLR(wxSYS_COLOUR_3DLIGHT)
1006 SYSCLR(wxSYS_COLOUR_INFOTEXT)
1007 SYSCLR(wxSYS_COLOUR_INFOBK)
1008 SYSCLR(wxSYS_COLOUR_LISTBOX)
1009 SYSCLR(wxSYS_COLOUR_HOTLIGHT)
1010 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
1011 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
1012 SYSCLR(wxSYS_COLOUR_MENUHILIGHT)
1013 SYSCLR(wxSYS_COLOUR_MENUBAR)
1014 #undef SYSCLR
1015 }
1016
1017 return wxNullColour;
1018}
78d14f80
VS
1019
1020wxColour wxXmlResourceHandler::GetColour(const wxString& param)
1021{
1022 wxString v = GetParamValue(param);
1df61962
VS
1023
1024 // find colour using HTML syntax (#RRGGBB)
78d14f80
VS
1025 unsigned long tmp = 0;
1026
1027 if (v.Length() != 7 || v[0u] != wxT('#') ||
1028 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
1029 {
1df61962
VS
1030 // the colour doesn't use #RRGGBB format, check if it is symbolic
1031 // colour name:
1032 wxColour clr = GetSystemColour(v);
1033 if (clr.Ok())
1034 return clr;
e7a3a5a5 1035
b5d6954b 1036 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
78d14f80
VS
1037 v.c_str(), param.c_str());
1038 return wxNullColour;
1039 }
1040
1041 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
1042 (unsigned char) ((tmp & 0x00FF00) >> 8),
1043 (unsigned char) ((tmp & 0x0000FF)));
1044}
1045
1046
1047
92e898b0 1048wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
db59a97c
VS
1049 const wxArtClient& defaultArtClient,
1050 wxSize size)
78d14f80 1051{
db59a97c
VS
1052 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1053 wxXmlNode *bmpNode = GetParamNode(param);
1054 if ( bmpNode )
af1337b0 1055 {
db59a97c
VS
1056 wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString);
1057 if ( !sid.empty() )
1058 {
b3e85292
VS
1059 wxString scl = bmpNode->GetPropVal(wxT("stock_client"), wxEmptyString);
1060 if (scl.empty())
1061 scl = defaultArtClient;
1062 else
1063 scl = wxART_MAKE_CLIENT_ID_FROM_STR(scl);
e7a3a5a5 1064
92e898b0 1065 wxBitmap stockArt =
db59a97c 1066 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid),
b3e85292 1067 scl, size);
db59a97c
VS
1068 if ( stockArt.Ok() )
1069 return stockArt;
1070 }
af1337b0
JS
1071 }
1072
92e898b0 1073 /* ...or load the bitmap from file: */
78d14f80 1074 wxString name = GetParamValue(param);
e7a3a5a5 1075 if (name.empty()) return wxNullBitmap;
78d14f80
VS
1076#if wxUSE_FILESYSTEM
1077 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
1078 if (fsfile == NULL)
1079 {
d0b50ad9
VS
1080 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1081 name.c_str());
78d14f80
VS
1082 return wxNullBitmap;
1083 }
1084 wxImage img(*(fsfile->GetStream()));
1085 delete fsfile;
1086#else
1087 wxImage img(GetParamValue(wxT("bitmap")));
1088#endif
af1337b0 1089
78d14f80
VS
1090 if (!img.Ok())
1091 {
1df61962
VS
1092 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1093 param.c_str());
78d14f80
VS
1094 return wxNullBitmap;
1095 }
1096 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
b272b6dc 1097 return wxBitmap(img);
af1337b0 1098
78d14f80
VS
1099}
1100
1101
1102
92e898b0 1103wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
db59a97c
VS
1104 const wxArtClient& defaultArtClient,
1105 wxSize size)
78d14f80 1106{
78d14f80 1107 wxIcon icon;
db59a97c 1108 icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
78d14f80
VS
1109 return icon;
1110}
1111
1112
1113
1114wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
1115{
2b5f62a0
VZ
1116 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
1117
78d14f80
VS
1118 wxXmlNode *n = m_node->GetChildren();
1119
1120 while (n)
1121 {
1122 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
1123 return n;
1124 n = n->GetNext();
1125 }
1126 return NULL;
1127}
1128
1129
1130wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
1131{
1132 wxXmlNode *n = node;
1133 if (n == NULL) return wxEmptyString;
1134 n = n->GetChildren();
1135
1136 while (n)
1137 {
1138 if (n->GetType() == wxXML_TEXT_NODE ||
1139 n->GetType() == wxXML_CDATA_SECTION_NODE)
1140 return n->GetContent();
1141 n = n->GetNext();
1142 }
1143 return wxEmptyString;
1144}
1145
1146
1147
1148wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
1149{
e7a3a5a5 1150 if (param.empty())
78d14f80
VS
1151 return GetNodeContent(m_node);
1152 else
1153 return GetNodeContent(GetParamNode(param));
1154}
1155
1156
1157
0c00c86f
VS
1158wxSize wxXmlResourceHandler::GetSize(const wxString& param,
1159 wxWindow *windowToUse)
78d14f80
VS
1160{
1161 wxString s = GetParamValue(param);
e7a3a5a5 1162 if (s.empty()) s = wxT("-1,-1");
78d14f80 1163 bool is_dlg;
d1f47235 1164 long sx, sy = 0;
78d14f80
VS
1165
1166 is_dlg = s[s.Length()-1] == wxT('d');
1167 if (is_dlg) s.RemoveLast();
1168
1169 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
1170 !s.AfterLast(wxT(',')).ToLong(&sy))
1171 {
00393283 1172 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
78d14f80
VS
1173 return wxDefaultSize;
1174 }
1175
1176 if (is_dlg)
1177 {
0c00c86f
VS
1178 if (windowToUse)
1179 {
1180 return wxDLG_UNIT(windowToUse, wxSize(sx, sy));
1181 }
1182 else if (m_parentAsWindow)
1183 {
78d14f80 1184 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
0c00c86f 1185 }
78d14f80
VS
1186 else
1187 {
1188 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1189 return wxDefaultSize;
1190 }
1191 }
8516a98b
DS
1192
1193 return wxSize(sx, sy);
78d14f80
VS
1194}
1195
1196
1197
1198wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
1199{
1200 wxSize sz = GetSize(param);
1201 return wxPoint(sz.x, sz.y);
1202}
1203
1204
1205
0c00c86f
VS
1206wxCoord wxXmlResourceHandler::GetDimension(const wxString& param,
1207 wxCoord defaultv,
1208 wxWindow *windowToUse)
78d14f80
VS
1209{
1210 wxString s = GetParamValue(param);
e7a3a5a5 1211 if (s.empty()) return defaultv;
78d14f80
VS
1212 bool is_dlg;
1213 long sx;
1214
1215 is_dlg = s[s.Length()-1] == wxT('d');
1216 if (is_dlg) s.RemoveLast();
1217
1218 if (!s.ToLong(&sx))
1219 {
00393283 1220 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
78d14f80
VS
1221 return defaultv;
1222 }
1223
1224 if (is_dlg)
1225 {
0c00c86f
VS
1226 if (windowToUse)
1227 {
1228 return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x;
1229 }
1230 else if (m_parentAsWindow)
1231 {
78d14f80 1232 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
0c00c86f 1233 }
78d14f80
VS
1234 else
1235 {
1236 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1237 return defaultv;
1238 }
1239 }
8516a98b
DS
1240
1241 return sx;
78d14f80
VS
1242}
1243
1244
1df61962
VS
1245// Get system font index using indexname
1246static wxFont GetSystemFont(const wxString& name)
1247{
1248 if (!name.empty())
1249 {
1250 #define SYSFNT(fnt) \
1251 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1252 SYSFNT(wxSYS_OEM_FIXED_FONT)
1253 SYSFNT(wxSYS_ANSI_FIXED_FONT)
1254 SYSFNT(wxSYS_ANSI_VAR_FONT)
1255 SYSFNT(wxSYS_SYSTEM_FONT)
1256 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT)
1257 SYSFNT(wxSYS_DEFAULT_PALETTE)
1258 SYSFNT(wxSYS_SYSTEM_FIXED_FONT)
1259 SYSFNT(wxSYS_DEFAULT_GUI_FONT)
1260 #undef SYSFNT
1261 }
1262
1263 return wxNullFont;
1264}
78d14f80
VS
1265
1266wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1267{
1268 wxXmlNode *font_node = GetParamNode(param);
1269 if (font_node == NULL)
1270 {
00393283 1271 wxLogError(_("Cannot find font node '%s'."), param.c_str());
78d14f80
VS
1272 return wxNullFont;
1273 }
1274
1275 wxXmlNode *oldnode = m_node;
1276 m_node = font_node;
1277
1df61962 1278 // font attributes:
78d14f80 1279
1df61962
VS
1280 // size
1281 int isize = wxDEFAULT;
1282 bool hasSize = HasParam(wxT("size"));
e7a3a5a5 1283 if (hasSize)
1df61962 1284 isize = GetLong(wxT("size"), wxDEFAULT);
78d14f80 1285
1df61962
VS
1286 // style
1287 int istyle = wxNORMAL;
1288 bool hasStyle = HasParam(wxT("style"));
1289 if (hasStyle)
1290 {
1291 wxString style = GetParamValue(wxT("style"));
e7a3a5a5 1292 if (style == wxT("italic"))
1df61962 1293 istyle = wxITALIC;
e7a3a5a5 1294 else if (style == wxT("slant"))
1df61962
VS
1295 istyle = wxSLANT;
1296 }
78d14f80 1297
1df61962
VS
1298 // weight
1299 int iweight = wxNORMAL;
1300 bool hasWeight = HasParam(wxT("weight"));
1301 if (hasWeight)
1302 {
1303 wxString weight = GetParamValue(wxT("weight"));
e7a3a5a5 1304 if (weight == wxT("bold"))
1df61962 1305 iweight = wxBOLD;
e7a3a5a5 1306 else if (weight == wxT("light"))
1df61962
VS
1307 iweight = wxLIGHT;
1308 }
e7a3a5a5 1309
1df61962
VS
1310 // underline
1311 bool hasUnderlined = HasParam(wxT("underlined"));
1312 bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false;
78d14f80 1313
1df61962
VS
1314 // family and facename
1315 int ifamily = wxDEFAULT;
1316 bool hasFamily = HasParam(wxT("family"));
1317 if (hasFamily)
78d14f80 1318 {
1df61962
VS
1319 wxString family = GetParamValue(wxT("family"));
1320 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1321 else if (family == wxT("roman")) ifamily = wxROMAN;
1322 else if (family == wxT("script")) ifamily = wxSCRIPT;
1323 else if (family == wxT("swiss")) ifamily = wxSWISS;
1324 else if (family == wxT("modern")) ifamily = wxMODERN;
1325 else if (family == wxT("teletype")) ifamily = wxTELETYPE;
1326 }
e7a3a5a5
WS
1327
1328
1df61962
VS
1329 wxString facename;
1330 bool hasFacename = HasParam(wxT("face"));
1331 if (hasFacename)
1332 {
1333 wxString faces = GetParamValue(wxT("face"));
1334 wxFontEnumerator enu;
1335 enu.EnumerateFacenames();
1336 wxStringTokenizer tk(faces, wxT(","));
1337 while (tk.HasMoreTokens())
78d14f80 1338 {
1df61962
VS
1339 int index = enu.GetFacenames()->Index(tk.GetNextToken(), false);
1340 if (index != wxNOT_FOUND)
1341 {
1342 facename = (*enu.GetFacenames())[index];
1343 break;
1344 }
78d14f80
VS
1345 }
1346 }
1347
1df61962
VS
1348 // encoding
1349 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1350 bool hasEncoding = HasParam(wxT("encoding"));
1351 if (hasEncoding)
1352 {
1353 wxString encoding = GetParamValue(wxT("encoding"));
1354 wxFontMapper mapper;
e7a3a5a5 1355 if (!encoding.empty())
1df61962
VS
1356 enc = mapper.CharsetToEncoding(encoding);
1357 if (enc == wxFONTENCODING_SYSTEM)
1358 enc = wxFONTENCODING_DEFAULT;
1359 }
78d14f80 1360
1df61962
VS
1361 // is this font based on a system font?
1362 wxFont sysfont = GetSystemFont(GetParamValue(wxT("sysfont")));
e7a3a5a5 1363
1df61962
VS
1364 if (sysfont.Ok())
1365 {
1366 if (hasSize)
1367 sysfont.SetPointSize(isize);
1368 else if (HasParam(wxT("relativesize")))
1369 sysfont.SetPointSize(int(sysfont.GetPointSize() *
1370 GetFloat(wxT("relativesize"))));
e7a3a5a5 1371
1df61962
VS
1372 if (hasStyle)
1373 sysfont.SetStyle(istyle);
1374 if (hasWeight)
1375 sysfont.SetWeight(iweight);
1376 if (hasUnderlined)
1377 sysfont.SetUnderlined(underlined);
1378 if (hasFamily)
1379 sysfont.SetFamily(ifamily);
1380 if (hasFacename)
1381 sysfont.SetFaceName(facename);
1382 if (hasEncoding)
1383 sysfont.SetDefaultEncoding(enc);
1384
1385 m_node = oldnode;
1386 return sysfont;
1387 }
8516a98b
DS
1388
1389 m_node = oldnode;
1390 return wxFont(isize, ifamily, istyle, iweight,
1391 underlined, facename, enc);
78d14f80
VS
1392}
1393
1394
1395void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1396{
1397 //FIXME : add cursor
1398
1399 if (HasParam(wxT("exstyle")))
0099f343
JS
1400 // Have to OR it with existing style, since
1401 // some implementations (e.g. wxGTK) use the extra style
1402 // during creation
1403 wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle")));
78d14f80
VS
1404 if (HasParam(wxT("bg")))
1405 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1406 if (HasParam(wxT("fg")))
1407 wnd->SetForegroundColour(GetColour(wxT("fg")));
1408 if (GetBool(wxT("enabled"), 1) == 0)
f80ea77b 1409 wnd->Enable(false);
78d14f80
VS
1410 if (GetBool(wxT("focused"), 0) == 1)
1411 wnd->SetFocus();
1412 if (GetBool(wxT("hidden"), 0) == 1)
f80ea77b 1413 wnd->Show(false);
78d14f80
VS
1414#if wxUSE_TOOLTIPS
1415 if (HasParam(wxT("tooltip")))
1416 wnd->SetToolTip(GetText(wxT("tooltip")));
1417#endif
1418 if (HasParam(wxT("font")))
1419 wnd->SetFont(GetFont());
1420}
1421
1422
1423void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1424{
1425 wxXmlNode *n = m_node->GetChildren();
1426
1427 while (n)
1428 {
1429 if (n->GetType() == wxXML_ELEMENT_NODE &&
0fa2e104 1430 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
78d14f80 1431 {
317a0d73
VS
1432 m_resource->CreateResFromNode(n, parent, NULL,
1433 this_hnd_only ? this : NULL);
78d14f80
VS
1434 }
1435 n = n->GetNext();
1436 }
1437}
1438
1439
1440void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1441{
1442 wxXmlNode *root;
1443 if (rootnode == NULL) root = m_node; else root = rootnode;
1444 wxXmlNode *n = root->GetChildren();
1445
1446 while (n)
1447 {
1448 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1449 {
1450 CreateResource(n, parent, NULL);
1451 }
1452 n = n->GetNext();
1453 }
1454}
1455
1456
1457
1458
1459
1460
1461
5ed345b7 1462// --------------- XRCID implementation -----------------------------
78d14f80 1463
5ed345b7 1464#define XRCID_TABLE_SIZE 1024
78d14f80
VS
1465
1466
5ed345b7 1467struct XRCID_record
78d14f80
VS
1468{
1469 int id;
00393283 1470 wxChar *key;
5ed345b7 1471 XRCID_record *next;
78d14f80
VS
1472};
1473
5ed345b7 1474static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 1475
13de23f6 1476static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = -2)
78d14f80 1477{
78d14f80
VS
1478 int index = 0;
1479
00393283 1480 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
5ed345b7 1481 index %= XRCID_TABLE_SIZE;
78d14f80 1482
5ed345b7 1483 XRCID_record *oldrec = NULL;
5ed345b7 1484 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 1485 {
00393283 1486 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
1487 {
1488 return rec->id;
1489 }
78d14f80
VS
1490 oldrec = rec;
1491 }
1492
5ed345b7
VS
1493 XRCID_record **rec_var = (oldrec == NULL) ?
1494 &XRCID_Records[index] : &oldrec->next;
1495 *rec_var = new XRCID_record;
00393283 1496 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
1497 (*rec_var)->next = NULL;
1498
85452d74 1499 wxChar *end;
13de23f6
VS
1500 if (value_if_not_found != -2)
1501 (*rec_var)->id = value_if_not_found;
85452d74
VS
1502 else
1503 {
13de23f6
VS
1504 int asint = wxStrtol(str_id, &end, 10);
1505 if (*str_id && *end == 0)
1506 {
1507 // if str_id was integer, keep it verbosely:
1508 (*rec_var)->id = asint;
1509 }
1510 else
1511 {
3a301301 1512 (*rec_var)->id = wxNewId();
13de23f6 1513 }
85452d74
VS
1514 }
1515
78d14f80
VS
1516 return (*rec_var)->id;
1517}
1518
3b2a000e
VS
1519static void AddStdXRCID_Records();
1520
13de23f6
VS
1521/*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
1522{
3b2a000e
VS
1523 static bool s_stdIDsAdded = false;
1524
1525 if ( !s_stdIDsAdded )
1526 {
1527 s_stdIDsAdded = true;
1528 AddStdXRCID_Records();
1529 }
1530
13de23f6
VS
1531 return XRCID_Lookup(str_id);
1532}
1533
78d14f80 1534
5ed345b7 1535static void CleanXRCID_Record(XRCID_record *rec)
78d14f80
VS
1536{
1537 if (rec)
1538 {
5ed345b7 1539 CleanXRCID_Record(rec->next);
00393283 1540 free(rec->key);
78d14f80
VS
1541 delete rec;
1542 }
1543}
1544
5ed345b7 1545static void CleanXRCID_Records()
78d14f80 1546{
5ed345b7 1547 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
139c5871 1548 {
5ed345b7 1549 CleanXRCID_Record(XRCID_Records[i]);
139c5871
VS
1550 XRCID_Records[i] = NULL;
1551 }
78d14f80
VS
1552}
1553
13de23f6
VS
1554static void AddStdXRCID_Records()
1555{
1556#define stdID(id) XRCID_Lookup(wxT(#id), id)
1557 stdID(-1);
c369d4f1
VS
1558
1559 stdID(wxID_ANY);
1560 stdID(wxID_SEPARATOR);
e7a3a5a5 1561
c369d4f1
VS
1562 stdID(wxID_OPEN);
1563 stdID(wxID_CLOSE);
1564 stdID(wxID_NEW);
1565 stdID(wxID_SAVE);
1566 stdID(wxID_SAVEAS);
1567 stdID(wxID_REVERT);
1568 stdID(wxID_EXIT);
1569 stdID(wxID_UNDO);
1570 stdID(wxID_REDO);
1571 stdID(wxID_HELP);
1572 stdID(wxID_PRINT);
1573 stdID(wxID_PRINT_SETUP);
1574 stdID(wxID_PREVIEW);
1575 stdID(wxID_ABOUT);
1576 stdID(wxID_HELP_CONTENTS);
1577 stdID(wxID_HELP_COMMANDS);
1578 stdID(wxID_HELP_PROCEDURES);
1579 stdID(wxID_HELP_CONTEXT);
13de23f6 1580 stdID(wxID_CLOSE_ALL);
c369d4f1
VS
1581 stdID(wxID_PREFERENCES);
1582 stdID(wxID_CUT);
1583 stdID(wxID_COPY);
1584 stdID(wxID_PASTE);
1585 stdID(wxID_CLEAR);
1586 stdID(wxID_FIND);
1587 stdID(wxID_DUPLICATE);
1588 stdID(wxID_SELECTALL);
1589 stdID(wxID_DELETE);
1590 stdID(wxID_REPLACE);
1591 stdID(wxID_REPLACE_ALL);
1592 stdID(wxID_PROPERTIES);
1593 stdID(wxID_VIEW_DETAILS);
1594 stdID(wxID_VIEW_LARGEICONS);
1595 stdID(wxID_VIEW_SMALLICONS);
1596 stdID(wxID_VIEW_LIST);
1597 stdID(wxID_VIEW_SORTDATE);
1598 stdID(wxID_VIEW_SORTNAME);
1599 stdID(wxID_VIEW_SORTSIZE);
1600 stdID(wxID_VIEW_SORTTYPE);
1601 stdID(wxID_FILE1);
1602 stdID(wxID_FILE2);
1603 stdID(wxID_FILE3);
1604 stdID(wxID_FILE4);
1605 stdID(wxID_FILE5);
1606 stdID(wxID_FILE6);
1607 stdID(wxID_FILE7);
1608 stdID(wxID_FILE8);
1609 stdID(wxID_FILE9);
1610 stdID(wxID_OK);
1611 stdID(wxID_CANCEL);
1612 stdID(wxID_APPLY);
1613 stdID(wxID_YES);
1614 stdID(wxID_NO);
1615 stdID(wxID_STATIC);
1616 stdID(wxID_FORWARD);
1617 stdID(wxID_BACKWARD);
1618 stdID(wxID_DEFAULT);
1619 stdID(wxID_MORE);
1620 stdID(wxID_SETUP);
1621 stdID(wxID_RESET);
1622 stdID(wxID_CONTEXT_HELP);
1623 stdID(wxID_YESTOALL);
1624 stdID(wxID_NOTOALL);
1625 stdID(wxID_ABORT);
1626 stdID(wxID_RETRY);
1627 stdID(wxID_IGNORE);
1628 stdID(wxID_ADD);
1629 stdID(wxID_REMOVE);
1630 stdID(wxID_UP);
1631 stdID(wxID_DOWN);
1632 stdID(wxID_HOME);
1633 stdID(wxID_REFRESH);
1634 stdID(wxID_STOP);
1635 stdID(wxID_INDEX);
1636 stdID(wxID_BOLD);
1637 stdID(wxID_ITALIC);
1638 stdID(wxID_JUSTIFY_CENTER);
1639 stdID(wxID_JUSTIFY_FILL);
1640 stdID(wxID_JUSTIFY_RIGHT);
1641 stdID(wxID_JUSTIFY_LEFT);
1642 stdID(wxID_UNDERLINE);
1643 stdID(wxID_INDENT);
1644 stdID(wxID_UNINDENT);
1645 stdID(wxID_ZOOM_100);
1646 stdID(wxID_ZOOM_FIT);
1647 stdID(wxID_ZOOM_IN);
1648 stdID(wxID_ZOOM_OUT);
1649 stdID(wxID_UNDELETE);
1650 stdID(wxID_REVERT_TO_SAVED);
1651 stdID(wxID_SYSTEM_MENU);
1652 stdID(wxID_CLOSE_FRAME);
1653 stdID(wxID_MOVE_FRAME);
1654 stdID(wxID_RESIZE_FRAME);
1655 stdID(wxID_MAXIMIZE_FRAME);
1656 stdID(wxID_ICONIZE_FRAME);
1657 stdID(wxID_RESTORE_FRAME);
1658
13de23f6
VS
1659#undef stdID
1660}
78d14f80
VS
1661
1662
1663
1664
1665
1666// --------------- module and globals -----------------------------
1667
78d14f80
VS
1668class wxXmlResourceModule: public wxModule
1669{
1670DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1671public:
1672 wxXmlResourceModule() {}
824e8eaa
VS
1673 bool OnInit()
1674 {
2b5f62a0 1675 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
f80ea77b 1676 return true;
824e8eaa 1677 }
78d14f80
VS
1678 void OnExit()
1679 {
1542c42e 1680 delete wxXmlResource::Set(NULL);
461932ae
MB
1681 if(wxXmlResource::ms_subclassFactories)
1682 WX_CLEAR_LIST(wxXmlSubclassFactoriesList, *wxXmlResource::ms_subclassFactories);
2b5f62a0 1683 wxDELETE(wxXmlResource::ms_subclassFactories);
5ed345b7 1684 CleanXRCID_Records();
78d14f80
VS
1685 }
1686};
1687
1688IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1689
1690
1691// When wxXml is loaded dynamically after the application is already running
1692// then the built-in module system won't pick this one up. Add it manually.
1693void wxXmlInitResourceModule()
1694{
1695 wxModule* module = new wxXmlResourceModule;
1696 module->Init();
1697 wxModule::RegisterModule(module);
1698}
a1e4ec87
VS
1699
1700#endif // wxUSE_XRC