]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xmlres.cpp
remove unneeded wxFindSuitableParent()
[wxWidgets.git] / src / xrc / xmlres.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xmlres.cpp
3 // Purpose: XRC resources
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
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_XRC
19
20 #include "wx/xrc/xmlres.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/intl.h"
24 #include "wx/log.h"
25 #include "wx/panel.h"
26 #include "wx/frame.h"
27 #include "wx/dialog.h"
28 #include "wx/settings.h"
29 #include "wx/bitmap.h"
30 #include "wx/image.h"
31 #include "wx/module.h"
32 #include "wx/wxcrtvararg.h"
33 #endif
34
35 #ifndef __WXWINCE__
36 #include <locale.h>
37 #endif
38
39 #include "wx/vector.h"
40 #include "wx/wfstream.h"
41 #include "wx/filesys.h"
42 #include "wx/filename.h"
43 #include "wx/tokenzr.h"
44 #include "wx/fontenum.h"
45 #include "wx/fontmap.h"
46 #include "wx/artprov.h"
47 #include "wx/imaglist.h"
48 #include "wx/dir.h"
49 #include "wx/xml/xml.h"
50
51
52 class wxXmlResourceDataRecord
53 {
54 public:
55 wxXmlResourceDataRecord() : Doc(NULL) {
56 #if wxUSE_DATETIME
57 Time = wxDateTime::Now();
58 #endif
59 }
60 ~wxXmlResourceDataRecord() {delete Doc;}
61
62 wxString File;
63 wxXmlDocument *Doc;
64 #if wxUSE_DATETIME
65 wxDateTime Time;
66 #endif
67 };
68
69 class wxXmlResourceDataRecords : public wxVector<wxXmlResourceDataRecord*>
70 {
71 // this is a class so that it can be forward-declared
72 };
73
74 namespace
75 {
76
77 // helper used by DoFindResource() and elsewhere: returns true if this is an
78 // object or object_ref node
79 //
80 // node must be non-NULL
81 inline bool IsObjectNode(wxXmlNode *node)
82 {
83 return node->GetType() == wxXML_ELEMENT_NODE &&
84 (node->GetName() == wxS("object") ||
85 node->GetName() == wxS("object_ref"));
86 }
87
88 // special XML attribute with name of input file, see GetFileNameFromNode()
89 const char *ATTR_INPUT_FILENAME = "__wx:filename";
90
91 // helper to get filename corresponding to an XML node
92 wxString
93 GetFileNameFromNode(wxXmlNode *node, const wxXmlResourceDataRecords& files)
94 {
95 // this loop does two things: it looks for ATTR_INPUT_FILENAME among
96 // parents and if it isn't used, it finds the root of the XML tree 'node'
97 // is in
98 for ( ;; )
99 {
100 // in some rare cases (specifically, when an <object_ref> is used, see
101 // wxXmlResource::CreateResFromNode() and MergeNodesOver()), we work
102 // with XML nodes that are not rooted in any document from 'files'
103 // (because a new node was created by CreateResFromNode() to merge the
104 // content of <object_ref> and the referenced <object>); in that case,
105 // we hack around the problem by putting the information about input
106 // file into a custom attribute
107 if ( node->HasAttribute(ATTR_INPUT_FILENAME) )
108 return node->GetAttribute(ATTR_INPUT_FILENAME);
109
110 if ( !node->GetParent() )
111 break; // we found the root of this XML tree
112
113 node = node->GetParent();
114 }
115
116 // NB: 'node' now points to the root of XML document
117
118 for ( wxXmlResourceDataRecords::const_iterator i = files.begin();
119 i != files.end(); ++i )
120 {
121 if ( (*i)->Doc->GetRoot() == node )
122 {
123 return (*i)->File;
124 }
125 }
126
127 return wxEmptyString; // not found
128 }
129
130 } // anonymous namespace
131
132
133 wxXmlResource *wxXmlResource::ms_instance = NULL;
134
135 /*static*/ wxXmlResource *wxXmlResource::Get()
136 {
137 if ( !ms_instance )
138 ms_instance = new wxXmlResource;
139 return ms_instance;
140 }
141
142 /*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
143 {
144 wxXmlResource *old = ms_instance;
145 ms_instance = res;
146 return old;
147 }
148
149 wxXmlResource::wxXmlResource(int flags, const wxString& domain)
150 {
151 m_flags = flags;
152 m_version = -1;
153 m_data = new wxXmlResourceDataRecords;
154 SetDomain(domain);
155 }
156
157 wxXmlResource::wxXmlResource(const wxString& filemask, int flags, const wxString& domain)
158 {
159 m_flags = flags;
160 m_version = -1;
161 m_data = new wxXmlResourceDataRecords;
162 SetDomain(domain);
163 Load(filemask);
164 }
165
166 wxXmlResource::~wxXmlResource()
167 {
168 ClearHandlers();
169
170 for ( wxXmlResourceDataRecords::iterator i = m_data->begin();
171 i != m_data->end(); ++i )
172 {
173 delete *i;
174 }
175 delete m_data;
176 }
177
178 void wxXmlResource::SetDomain(const wxString& domain)
179 {
180 m_domain = domain;
181 }
182
183
184 /* static */
185 wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
186 {
187 wxString fnd(filename);
188
189 // NB: as Load() and Unload() accept both filenames and URLs (should
190 // probably be changed to filenames only, but embedded resources
191 // currently rely on its ability to handle URLs - FIXME) we need to
192 // determine whether found name is filename and not URL and this is the
193 // fastest/simplest way to do it
194 if (wxFileName::FileExists(fnd))
195 {
196 // Make the name absolute filename, because the app may
197 // change working directory later:
198 wxFileName fn(fnd);
199 if (fn.IsRelative())
200 {
201 fn.MakeAbsolute();
202 fnd = fn.GetFullPath();
203 }
204 #if wxUSE_FILESYSTEM
205 fnd = wxFileSystem::FileNameToURL(fnd);
206 #endif
207 }
208
209 return fnd;
210 }
211
212 #if wxUSE_FILESYSTEM
213
214 /* static */
215 bool wxXmlResource::IsArchive(const wxString& filename)
216 {
217 const wxString fnd = filename.Lower();
218
219 return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs"));
220 }
221
222 #endif // wxUSE_FILESYSTEM
223
224 bool wxXmlResource::LoadFile(const wxFileName& file)
225 {
226 #if wxUSE_FILESYSTEM
227 return Load(wxFileSystem::FileNameToURL(file));
228 #else
229 return Load(file.GetFullPath());
230 #endif
231 }
232
233 bool wxXmlResource::LoadAllFiles(const wxString& dirname)
234 {
235 bool ok = true;
236 wxArrayString files;
237
238 wxDir::GetAllFiles(dirname, &files, "*.xrc");
239
240 for ( wxArrayString::const_iterator i = files.begin(); i != files.end(); ++i )
241 {
242 if ( !LoadFile(*i) )
243 ok = false;
244 }
245
246 return ok;
247 }
248
249 bool wxXmlResource::Load(const wxString& filemask_)
250 {
251 wxString filemask = ConvertFileNameToURL(filemask_);
252
253 #if wxUSE_FILESYSTEM
254 wxFileSystem fsys;
255 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
256 # define wxXmlFindNext fsys.FindNext()
257 #else
258 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
259 # define wxXmlFindNext wxFindNextFile()
260 #endif
261 wxString fnd = wxXmlFindFirst;
262 if ( fnd.empty() )
263 {
264 wxLogError(_("Cannot load resources from '%s'."), filemask);
265 return false;
266 }
267
268 while (!fnd.empty())
269 {
270 #if wxUSE_FILESYSTEM
271 if ( IsArchive(fnd) )
272 {
273 if ( !Load(fnd + wxT("#zip:*.xrc")) )
274 return false;
275 }
276 else // a single resource URL
277 #endif // wxUSE_FILESYSTEM
278 {
279 wxXmlResourceDataRecord *drec = new wxXmlResourceDataRecord;
280 drec->File = fnd;
281 Data().push_back(drec);
282 }
283
284 fnd = wxXmlFindNext;
285 }
286 # undef wxXmlFindFirst
287 # undef wxXmlFindNext
288
289 return UpdateResources();
290 }
291
292 bool wxXmlResource::Unload(const wxString& filename)
293 {
294 wxASSERT_MSG( !wxIsWild(filename),
295 _T("wildcards not supported by wxXmlResource::Unload()") );
296
297 wxString fnd = ConvertFileNameToURL(filename);
298 #if wxUSE_FILESYSTEM
299 const bool isArchive = IsArchive(fnd);
300 if ( isArchive )
301 fnd += _T("#zip:");
302 #endif // wxUSE_FILESYSTEM
303
304 bool unloaded = false;
305 for ( wxXmlResourceDataRecords::iterator i = Data().begin();
306 i != Data().end(); ++i )
307 {
308 #if wxUSE_FILESYSTEM
309 if ( isArchive )
310 {
311 if ( (*i)->File.StartsWith(fnd) )
312 unloaded = true;
313 // don't break from the loop, we can have other matching files
314 }
315 else // a single resource URL
316 #endif // wxUSE_FILESYSTEM
317 {
318 if ( (*i)->File == fnd )
319 {
320 delete *i;
321 Data().erase(i);
322 unloaded = true;
323
324 // no sense in continuing, there is only one file with this URL
325 break;
326 }
327 }
328 }
329
330 return unloaded;
331 }
332
333
334 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject)
335
336 void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
337 {
338 m_handlers.push_back(handler);
339 handler->SetParentResource(this);
340 }
341
342 void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
343 {
344 m_handlers.insert(m_handlers.begin(), handler);
345 handler->SetParentResource(this);
346 }
347
348
349
350 void wxXmlResource::ClearHandlers()
351 {
352 for ( wxVector<wxXmlResourceHandler*>::iterator i = m_handlers.begin();
353 i != m_handlers.end(); ++i )
354 delete *i;
355 m_handlers.clear();
356 }
357
358
359 wxMenu *wxXmlResource::LoadMenu(const wxString& name)
360 {
361 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
362 }
363
364
365
366 wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
367 {
368 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
369 }
370
371
372
373 #if wxUSE_TOOLBAR
374 wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
375 {
376 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
377 }
378 #endif
379
380
381 wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
382 {
383 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
384 }
385
386 bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
387 {
388 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
389 }
390
391
392
393 wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
394 {
395 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
396 }
397
398 bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
399 {
400 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
401 }
402
403 wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
404 {
405 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
406 }
407
408 bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
409 {
410 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
411 }
412
413 wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
414 {
415 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
416 FindResource(name, wxT("wxBitmap")), NULL, NULL);
417 wxBitmap rt;
418
419 if (bmp) { rt = *bmp; delete bmp; }
420 return rt;
421 }
422
423 wxIcon wxXmlResource::LoadIcon(const wxString& name)
424 {
425 wxIcon *icon = (wxIcon*)CreateResFromNode(
426 FindResource(name, wxT("wxIcon")), NULL, NULL);
427 wxIcon rt;
428
429 if (icon) { rt = *icon; delete icon; }
430 return rt;
431 }
432
433
434 wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname)
435 {
436 return CreateResFromNode(FindResource(name, classname), parent, NULL);
437 }
438
439 bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname)
440 {
441 return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL;
442 }
443
444
445 bool wxXmlResource::AttachUnknownControl(const wxString& name,
446 wxWindow *control, wxWindow *parent)
447 {
448 if (parent == NULL)
449 parent = control->GetParent();
450 wxWindow *container = parent->FindWindow(name + wxT("_container"));
451 if (!container)
452 {
453 wxLogError("Cannot find container for unknown control '%s'.", name);
454 return false;
455 }
456 return control->Reparent(container);
457 }
458
459
460 static void ProcessPlatformProperty(wxXmlNode *node)
461 {
462 wxString s;
463 bool isok;
464
465 wxXmlNode *c = node->GetChildren();
466 while (c)
467 {
468 isok = false;
469 if (!c->GetAttribute(wxT("platform"), &s))
470 isok = true;
471 else
472 {
473 wxStringTokenizer tkn(s, wxT(" |"));
474
475 while (tkn.HasMoreTokens())
476 {
477 s = tkn.GetNextToken();
478 #ifdef __WINDOWS__
479 if (s == wxT("win")) isok = true;
480 #endif
481 #if defined(__MAC__) || defined(__APPLE__)
482 if (s == wxT("mac")) isok = true;
483 #elif defined(__UNIX__)
484 if (s == wxT("unix")) isok = true;
485 #endif
486 #ifdef __OS2__
487 if (s == wxT("os2")) isok = true;
488 #endif
489
490 if (isok)
491 break;
492 }
493 }
494
495 if (isok)
496 {
497 ProcessPlatformProperty(c);
498 c = c->GetNext();
499 }
500 else
501 {
502 wxXmlNode *c2 = c->GetNext();
503 node->RemoveChild(c);
504 delete c;
505 c = c2;
506 }
507 }
508 }
509
510
511
512 bool wxXmlResource::UpdateResources()
513 {
514 bool rt = true;
515 bool modif;
516 # if wxUSE_FILESYSTEM
517 wxFSFile *file = NULL;
518 wxUnusedVar(file);
519 wxFileSystem fsys;
520 # endif
521
522 wxString encoding(wxT("UTF-8"));
523 #if !wxUSE_UNICODE && wxUSE_INTL
524 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
525 {
526 // In case we are not using wxLocale to translate strings, convert the
527 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
528 // is on, because it could break wxGetTranslation lookup.
529 encoding = wxLocale::GetSystemEncodingName();
530 }
531 #endif
532
533 for ( wxXmlResourceDataRecords::iterator i = Data().begin();
534 i != Data().end(); ++i )
535 {
536 wxXmlResourceDataRecord* const rec = *i;
537
538 modif = (rec->Doc == NULL);
539
540 if (!modif && !(m_flags & wxXRC_NO_RELOADING))
541 {
542 # if wxUSE_FILESYSTEM
543 file = fsys.OpenFile(rec->File);
544 # if wxUSE_DATETIME
545 modif = file && file->GetModificationTime() > rec->Time;
546 # else // wxUSE_DATETIME
547 modif = true;
548 # endif // wxUSE_DATETIME
549 if (!file)
550 {
551 wxLogError(_("Cannot open file '%s'."), rec->File);
552 rt = false;
553 }
554 wxDELETE(file);
555 wxUnusedVar(file);
556 # else // wxUSE_FILESYSTEM
557 # if wxUSE_DATETIME
558 modif = wxDateTime(wxFileModificationTime(rec->File)) > rec->Time;
559 # else // wxUSE_DATETIME
560 modif = true;
561 # endif // wxUSE_DATETIME
562 # endif // wxUSE_FILESYSTEM
563 }
564
565 if (modif)
566 {
567 wxLogTrace(_T("xrc"), _T("opening file '%s'"), rec->File);
568
569 wxInputStream *stream = NULL;
570
571 # if wxUSE_FILESYSTEM
572 file = fsys.OpenFile(rec->File);
573 if (file)
574 stream = file->GetStream();
575 # else
576 stream = new wxFileInputStream(rec->File);
577 # endif
578
579 if (stream)
580 {
581 delete rec->Doc;
582 rec->Doc = new wxXmlDocument;
583 }
584 if (!stream || !stream->IsOk() || !rec->Doc->Load(*stream, encoding))
585 {
586 wxLogError(_("Cannot load resources from file '%s'."),
587 rec->File);
588 wxDELETE(rec->Doc);
589 rt = false;
590 }
591 else if (rec->Doc->GetRoot()->GetName() != wxT("resource"))
592 {
593 ReportError
594 (
595 rec->Doc->GetRoot(),
596 "invalid XRC resource, doesn't have root node <resource>"
597 );
598 wxDELETE(rec->Doc);
599 rt = false;
600 }
601 else
602 {
603 long version;
604 int v1, v2, v3, v4;
605 wxString verstr = rec->Doc->GetRoot()->GetAttribute(
606 wxT("version"), wxT("0.0.0.0"));
607 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
608 &v1, &v2, &v3, &v4) == 4)
609 version = v1*256*256*256+v2*256*256+v3*256+v4;
610 else
611 version = 0;
612 if (m_version == -1)
613 m_version = version;
614 if (m_version != version)
615 {
616 wxLogError("Resource files must have same version number.");
617 rt = false;
618 }
619
620 ProcessPlatformProperty(rec->Doc->GetRoot());
621 #if wxUSE_DATETIME
622 #if wxUSE_FILESYSTEM
623 rec->Time = file->GetModificationTime();
624 #else // wxUSE_FILESYSTEM
625 rec->Time = wxDateTime(wxFileModificationTime(rec->File));
626 #endif // wxUSE_FILESYSTEM
627 #endif // wxUSE_DATETIME
628 }
629
630 # if wxUSE_FILESYSTEM
631 wxDELETE(file);
632 wxUnusedVar(file);
633 # else
634 wxDELETE(stream);
635 # endif
636 }
637 }
638
639 return rt;
640 }
641
642 wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
643 const wxString& name,
644 const wxString& classname,
645 bool recursive) const
646 {
647 wxXmlNode *node;
648
649 // first search for match at the top-level nodes (as this is
650 // where the resource is most commonly looked for):
651 for (node = parent->GetChildren(); node; node = node->GetNext())
652 {
653 if ( IsObjectNode(node) && node->GetAttribute(wxS("name")) == name )
654 {
655 // empty class name matches everything
656 if ( classname.empty() )
657 return node;
658
659 wxString cls(node->GetAttribute(wxS("class")));
660
661 // object_ref may not have 'class' attribute:
662 if (cls.empty() && node->GetName() == wxS("object_ref"))
663 {
664 wxString refName = node->GetAttribute(wxS("ref"));
665 if (refName.empty())
666 continue;
667
668 const wxXmlNode * const refNode = GetResourceNode(refName);
669 if ( refNode )
670 cls = refNode->GetAttribute(wxS("class"));
671 }
672
673 if ( cls == classname )
674 return node;
675 }
676 }
677
678 // then recurse in child nodes
679 if ( recursive )
680 {
681 for (node = parent->GetChildren(); node; node = node->GetNext())
682 {
683 if ( IsObjectNode(node) )
684 {
685 wxXmlNode* found = DoFindResource(node, name, classname, true);
686 if ( found )
687 return found;
688 }
689 }
690 }
691
692 return NULL;
693 }
694
695 wxXmlNode *wxXmlResource::FindResource(const wxString& name,
696 const wxString& classname,
697 bool recursive)
698 {
699 wxString path;
700 wxXmlNode * const
701 node = GetResourceNodeAndLocation(name, classname, recursive, &path);
702
703 if ( !node )
704 {
705 ReportError
706 (
707 NULL,
708 wxString::Format
709 (
710 "XRC resource \"%s\" (class \"%s\") not found",
711 name, classname
712 )
713 );
714 }
715 #if wxUSE_FILESYSTEM
716 else // node was found
717 {
718 // ensure that relative paths work correctly when loading this node
719 // (which should happen as soon as we return as FindResource() result
720 // is always passed to CreateResFromNode())
721 m_curFileSystem.ChangePathTo(path);
722 }
723 #endif // wxUSE_FILESYSTEM
724
725 return node;
726 }
727
728 wxXmlNode *
729 wxXmlResource::GetResourceNodeAndLocation(const wxString& name,
730 const wxString& classname,
731 bool recursive,
732 wxString *path) const
733 {
734 // ensure everything is up-to-date: this is needed to support on-remand
735 // reloading of XRC files
736 const_cast<wxXmlResource *>(this)->UpdateResources();
737
738 for ( wxXmlResourceDataRecords::const_iterator f = Data().begin();
739 f != Data().end(); ++f )
740 {
741 wxXmlResourceDataRecord *const rec = *f;
742 wxXmlDocument * const doc = rec->Doc;
743 if ( !doc || !doc->GetRoot() )
744 continue;
745
746 wxXmlNode * const
747 found = DoFindResource(doc->GetRoot(), name, classname, recursive);
748 if ( found )
749 {
750 if ( path )
751 *path = rec->File;
752
753 return found;
754 }
755 }
756
757 return NULL;
758 }
759
760 static void MergeNodesOver(wxXmlNode& dest, wxXmlNode& overwriteWith,
761 const wxString& overwriteFilename)
762 {
763 // Merge attributes:
764 for ( wxXmlAttribute *attr = overwriteWith.GetAttributes();
765 attr; attr = attr->GetNext() )
766 {
767 wxXmlAttribute *dattr;
768 for (dattr = dest.GetAttributes(); dattr; dattr = dattr->GetNext())
769 {
770
771 if ( dattr->GetName() == attr->GetName() )
772 {
773 dattr->SetValue(attr->GetValue());
774 break;
775 }
776 }
777
778 if ( !dattr )
779 dest.AddAttribute(attr->GetName(), attr->GetValue());
780 }
781
782 // Merge child nodes:
783 for (wxXmlNode* node = overwriteWith.GetChildren(); node; node = node->GetNext())
784 {
785 wxString name = node->GetAttribute(wxT("name"), wxEmptyString);
786 wxXmlNode *dnode;
787
788 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
789 {
790 if ( dnode->GetName() == node->GetName() &&
791 dnode->GetAttribute(wxT("name"), wxEmptyString) == name &&
792 dnode->GetType() == node->GetType() )
793 {
794 MergeNodesOver(*dnode, *node, overwriteFilename);
795 break;
796 }
797 }
798
799 if ( !dnode )
800 {
801 wxXmlNode *copyOfNode = new wxXmlNode(*node);
802 // remember referenced object's file, see GetFileNameFromNode()
803 copyOfNode->AddAttribute(ATTR_INPUT_FILENAME, overwriteFilename);
804
805 static const wxChar *AT_END = wxT("end");
806 wxString insert_pos = node->GetAttribute(wxT("insert_at"), AT_END);
807 if ( insert_pos == AT_END )
808 {
809 dest.AddChild(copyOfNode);
810 }
811 else if ( insert_pos == wxT("begin") )
812 {
813 dest.InsertChild(copyOfNode, dest.GetChildren());
814 }
815 }
816 }
817
818 if ( dest.GetType() == wxXML_TEXT_NODE && overwriteWith.GetContent().length() )
819 dest.SetContent(overwriteWith.GetContent());
820 }
821
822 wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
823 wxObject *instance,
824 wxXmlResourceHandler *handlerToUse)
825 {
826 if (node == NULL) return NULL;
827
828 // handling of referenced resource
829 if ( node->GetName() == wxT("object_ref") )
830 {
831 wxString refName = node->GetAttribute(wxT("ref"), wxEmptyString);
832 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
833
834 if ( !refNode )
835 {
836 ReportError
837 (
838 node,
839 wxString::Format
840 (
841 "referenced object node with ref=\"%s\" not found",
842 refName
843 )
844 );
845 return NULL;
846 }
847
848 if ( !node->GetChildren() )
849 {
850 // In the typical, simple case, <object_ref> is used to link
851 // to another node and doesn't have any content of its own that
852 // would overwrite linked object's properties. In this case,
853 // we can simply create the resource from linked node.
854
855 return CreateResFromNode(refNode, parent, instance);
856 }
857 else
858 {
859 // In the more complicated (but rare) case, <object_ref> has
860 // subnodes that partially overwrite content of the referenced
861 // object. In this case, we need to merge both XML trees and
862 // load the resource from result of the merge.
863
864 wxXmlNode copy(*refNode);
865 MergeNodesOver(copy, *node, GetFileNameFromNode(node, Data()));
866
867 // remember referenced object's file, see GetFileNameFromNode()
868 copy.AddAttribute(ATTR_INPUT_FILENAME,
869 GetFileNameFromNode(refNode, Data()));
870
871 return CreateResFromNode(&copy, parent, instance);
872 }
873 }
874
875 if (handlerToUse)
876 {
877 if (handlerToUse->CanHandle(node))
878 {
879 return handlerToUse->CreateResource(node, parent, instance);
880 }
881 }
882 else if (node->GetName() == wxT("object"))
883 {
884 for ( wxVector<wxXmlResourceHandler*>::iterator h = m_handlers.begin();
885 h != m_handlers.end(); ++h )
886 {
887 wxXmlResourceHandler *handler = *h;
888 if (handler->CanHandle(node))
889 return handler->CreateResource(node, parent, instance);
890 }
891 }
892
893 ReportError
894 (
895 node,
896 wxString::Format
897 (
898 "no handler found for XML node \"%s\" (class \"%s\")",
899 node->GetName(),
900 node->GetAttribute("class", wxEmptyString)
901 )
902 );
903 return NULL;
904 }
905
906
907 class wxXmlSubclassFactories : public wxVector<wxXmlSubclassFactory*>
908 {
909 // this is a class so that it can be forward-declared
910 };
911
912 wxXmlSubclassFactories *wxXmlResource::ms_subclassFactories = NULL;
913
914 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
915 {
916 if (!ms_subclassFactories)
917 {
918 ms_subclassFactories = new wxXmlSubclassFactories;
919 }
920 ms_subclassFactories->push_back(factory);
921 }
922
923 class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
924 {
925 public:
926 ~wxXmlSubclassFactoryCXX() {}
927
928 wxObject *Create(const wxString& className)
929 {
930 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
931
932 if (classInfo)
933 return classInfo->CreateObject();
934 else
935 return NULL;
936 }
937 };
938
939
940
941
942 wxXmlResourceHandler::wxXmlResourceHandler()
943 : m_node(NULL), m_parent(NULL), m_instance(NULL),
944 m_parentAsWindow(NULL)
945 {}
946
947
948
949 wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
950 {
951 wxXmlNode *myNode = m_node;
952 wxString myClass = m_class;
953 wxObject *myParent = m_parent, *myInstance = m_instance;
954 wxWindow *myParentAW = m_parentAsWindow;
955
956 m_instance = instance;
957 if (!m_instance && node->HasAttribute(wxT("subclass")) &&
958 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
959 {
960 wxString subclass = node->GetAttribute(wxT("subclass"), wxEmptyString);
961 if (!subclass.empty())
962 {
963 for (wxXmlSubclassFactories::iterator i = wxXmlResource::ms_subclassFactories->begin();
964 i != wxXmlResource::ms_subclassFactories->end(); ++i)
965 {
966 m_instance = (*i)->Create(subclass);
967 if (m_instance)
968 break;
969 }
970
971 if (!m_instance)
972 {
973 wxString name = node->GetAttribute(wxT("name"), wxEmptyString);
974 ReportError
975 (
976 node,
977 wxString::Format
978 (
979 "subclass \"%s\" not found for resource \"%s\", not subclassing",
980 subclass, name
981 )
982 );
983 }
984 }
985 }
986
987 m_node = node;
988 m_class = node->GetAttribute(wxT("class"), wxEmptyString);
989 m_parent = parent;
990 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
991
992 wxObject *returned = DoCreateResource();
993
994 m_node = myNode;
995 m_class = myClass;
996 m_parent = myParent; m_parentAsWindow = myParentAW;
997 m_instance = myInstance;
998
999 return returned;
1000 }
1001
1002
1003 void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
1004 {
1005 m_styleNames.Add(name);
1006 m_styleValues.Add(value);
1007 }
1008
1009
1010
1011 void wxXmlResourceHandler::AddWindowStyles()
1012 {
1013 XRC_ADD_STYLE(wxCLIP_CHILDREN);
1014
1015 // the border styles all have the old and new names, recognize both for now
1016 XRC_ADD_STYLE(wxSIMPLE_BORDER); XRC_ADD_STYLE(wxBORDER_SIMPLE);
1017 XRC_ADD_STYLE(wxSUNKEN_BORDER); XRC_ADD_STYLE(wxBORDER_SUNKEN);
1018 XRC_ADD_STYLE(wxDOUBLE_BORDER); XRC_ADD_STYLE(wxBORDER_DOUBLE);
1019 XRC_ADD_STYLE(wxRAISED_BORDER); XRC_ADD_STYLE(wxBORDER_RAISED);
1020 XRC_ADD_STYLE(wxSTATIC_BORDER); XRC_ADD_STYLE(wxBORDER_STATIC);
1021 XRC_ADD_STYLE(wxNO_BORDER); XRC_ADD_STYLE(wxBORDER_NONE);
1022
1023 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
1024 XRC_ADD_STYLE(wxWANTS_CHARS);
1025 XRC_ADD_STYLE(wxTAB_TRAVERSAL);
1026 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
1027 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE);
1028 XRC_ADD_STYLE(wxALWAYS_SHOW_SB);
1029 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS);
1030 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
1031 }
1032
1033
1034
1035 bool wxXmlResourceHandler::HasParam(const wxString& param)
1036 {
1037 return (GetParamNode(param) != NULL);
1038 }
1039
1040
1041 int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
1042 {
1043 wxString s = GetParamValue(param);
1044
1045 if (!s) return defaults;
1046
1047 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
1048 int style = 0;
1049 int index;
1050 wxString fl;
1051 while (tkn.HasMoreTokens())
1052 {
1053 fl = tkn.GetNextToken();
1054 index = m_styleNames.Index(fl);
1055 if (index != wxNOT_FOUND)
1056 {
1057 style |= m_styleValues[index];
1058 }
1059 else
1060 {
1061 ReportParamError
1062 (
1063 param,
1064 wxString::Format("unknown style flag \"%s\"", fl)
1065 );
1066 }
1067 }
1068 return style;
1069 }
1070
1071
1072
1073 wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
1074 {
1075 wxXmlNode *parNode = GetParamNode(param);
1076 wxString str1(GetNodeContent(parNode));
1077 wxString str2;
1078
1079 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1080 const bool escapeBackslash = (m_resource->CompareVersion(2,5,3,0) >= 0);
1081
1082 // VS: First version of XRC resources used $ instead of & (which is
1083 // illegal in XML), but later I realized that '_' fits this purpose
1084 // much better (because &File means "File with F underlined").
1085 const wxChar amp_char = (m_resource->CompareVersion(2,3,0,1) < 0)
1086 ? '$' : '_';
1087
1088 for ( wxString::const_iterator dt = str1.begin(); dt != str1.end(); ++dt )
1089 {
1090 // Remap amp_char to &, map double amp_char to amp_char (for things
1091 // like "&File..." -- this is illegal in XML, so we use "_File..."):
1092 if ( *dt == amp_char )
1093 {
1094 if ( *(++dt) == amp_char )
1095 str2 << amp_char;
1096 else
1097 str2 << wxT('&') << *dt;
1098 }
1099 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
1100 else if ( *dt == wxT('\\') )
1101 {
1102 switch ( (*(++dt)).GetValue() )
1103 {
1104 case wxT('n'):
1105 str2 << wxT('\n');
1106 break;
1107
1108 case wxT('t'):
1109 str2 << wxT('\t');
1110 break;
1111
1112 case wxT('r'):
1113 str2 << wxT('\r');
1114 break;
1115
1116 case wxT('\\') :
1117 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1118 if ( escapeBackslash )
1119 {
1120 str2 << wxT('\\');
1121 break;
1122 }
1123 // else fall-through to default: branch below
1124
1125 default:
1126 str2 << wxT('\\') << *dt;
1127 break;
1128 }
1129 }
1130 else
1131 {
1132 str2 << *dt;
1133 }
1134 }
1135
1136 if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
1137 {
1138 if (translate && parNode &&
1139 parNode->GetAttribute(wxT("translate"), wxEmptyString) != wxT("0"))
1140 {
1141 return wxGetTranslation(str2, m_resource->GetDomain());
1142 }
1143 else
1144 {
1145 #if wxUSE_UNICODE
1146 return str2;
1147 #else
1148 // The string is internally stored as UTF-8, we have to convert
1149 // it into system's default encoding so that it can be displayed:
1150 return wxString(str2.wc_str(wxConvUTF8), wxConvLocal);
1151 #endif
1152 }
1153 }
1154
1155 // If wxXRC_USE_LOCALE is not set, then the string is already in
1156 // system's default encoding in ANSI build, so we don't have to
1157 // do anything special here.
1158 return str2;
1159 }
1160
1161
1162
1163 long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
1164 {
1165 long value;
1166 wxString str1 = GetParamValue(param);
1167
1168 if (!str1.ToLong(&value))
1169 value = defaultv;
1170
1171 return value;
1172 }
1173
1174 float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv)
1175 {
1176 wxString str = GetParamValue(param);
1177
1178 #if wxUSE_INTL
1179 // strings in XRC always use C locale but wxString::ToDouble() uses the
1180 // current one, so transform the string to it supposing that the only
1181 // difference between them is the decimal separator
1182 //
1183 // TODO: use wxString::ToCDouble() when we have it
1184 str.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
1185 wxLOCALE_CAT_NUMBER));
1186 #endif // wxUSE_INTL
1187
1188 double value;
1189 if (!str.ToDouble(&value))
1190 value = defaultv;
1191
1192 return wx_truncate_cast(float, value);
1193 }
1194
1195
1196 int wxXmlResourceHandler::GetID()
1197 {
1198 return wxXmlResource::GetXRCID(GetName());
1199 }
1200
1201
1202
1203 wxString wxXmlResourceHandler::GetName()
1204 {
1205 return m_node->GetAttribute(wxT("name"), wxT("-1"));
1206 }
1207
1208
1209
1210 bool wxXmlResourceHandler::GetBoolAttr(const wxString& attr, bool defaultv)
1211 {
1212 wxString v;
1213 return m_node->GetAttribute(attr, &v) ? v == '1' : defaultv;
1214 }
1215
1216 bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
1217 {
1218 const wxString v = GetParamValue(param);
1219
1220 return v.empty() ? defaultv : (v == '1');
1221 }
1222
1223
1224 static wxColour GetSystemColour(const wxString& name)
1225 {
1226 if (!name.empty())
1227 {
1228 #define SYSCLR(clr) \
1229 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1230 SYSCLR(wxSYS_COLOUR_SCROLLBAR)
1231 SYSCLR(wxSYS_COLOUR_BACKGROUND)
1232 SYSCLR(wxSYS_COLOUR_DESKTOP)
1233 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION)
1234 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION)
1235 SYSCLR(wxSYS_COLOUR_MENU)
1236 SYSCLR(wxSYS_COLOUR_WINDOW)
1237 SYSCLR(wxSYS_COLOUR_WINDOWFRAME)
1238 SYSCLR(wxSYS_COLOUR_MENUTEXT)
1239 SYSCLR(wxSYS_COLOUR_WINDOWTEXT)
1240 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT)
1241 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER)
1242 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER)
1243 SYSCLR(wxSYS_COLOUR_APPWORKSPACE)
1244 SYSCLR(wxSYS_COLOUR_HIGHLIGHT)
1245 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT)
1246 SYSCLR(wxSYS_COLOUR_BTNFACE)
1247 SYSCLR(wxSYS_COLOUR_3DFACE)
1248 SYSCLR(wxSYS_COLOUR_BTNSHADOW)
1249 SYSCLR(wxSYS_COLOUR_3DSHADOW)
1250 SYSCLR(wxSYS_COLOUR_GRAYTEXT)
1251 SYSCLR(wxSYS_COLOUR_BTNTEXT)
1252 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
1253 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT)
1254 SYSCLR(wxSYS_COLOUR_BTNHILIGHT)
1255 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT)
1256 SYSCLR(wxSYS_COLOUR_3DHILIGHT)
1257 SYSCLR(wxSYS_COLOUR_3DDKSHADOW)
1258 SYSCLR(wxSYS_COLOUR_3DLIGHT)
1259 SYSCLR(wxSYS_COLOUR_INFOTEXT)
1260 SYSCLR(wxSYS_COLOUR_INFOBK)
1261 SYSCLR(wxSYS_COLOUR_LISTBOX)
1262 SYSCLR(wxSYS_COLOUR_HOTLIGHT)
1263 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
1264 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
1265 SYSCLR(wxSYS_COLOUR_MENUHILIGHT)
1266 SYSCLR(wxSYS_COLOUR_MENUBAR)
1267 #undef SYSCLR
1268 }
1269
1270 return wxNullColour;
1271 }
1272
1273 wxColour wxXmlResourceHandler::GetColour(const wxString& param, const wxColour& defaultv)
1274 {
1275 wxString v = GetParamValue(param);
1276
1277 if ( v.empty() )
1278 return defaultv;
1279
1280 wxColour clr;
1281
1282 // wxString -> wxColour conversion
1283 if (!clr.Set(v))
1284 {
1285 // the colour doesn't use #RRGGBB format, check if it is symbolic
1286 // colour name:
1287 clr = GetSystemColour(v);
1288 if (clr.Ok())
1289 return clr;
1290
1291 ReportParamError
1292 (
1293 param,
1294 wxString::Format("incorrect colour specification \"%s\"", v)
1295 );
1296 return wxNullColour;
1297 }
1298
1299 return clr;
1300 }
1301
1302 namespace
1303 {
1304
1305 // if 'param' has stock_id/stock_client, extracts them and returns true
1306 bool GetStockArtAttrs(const wxXmlNode *paramNode,
1307 const wxString& defaultArtClient,
1308 wxString& art_id, wxString& art_client)
1309 {
1310 if ( paramNode )
1311 {
1312 art_id = paramNode->GetAttribute("stock_id", "");
1313
1314 if ( !art_id.empty() )
1315 {
1316 art_id = wxART_MAKE_ART_ID_FROM_STR(art_id);
1317
1318 art_client = paramNode->GetAttribute("stock_client", "");
1319 if ( art_client.empty() )
1320 art_client = defaultArtClient;
1321 else
1322 art_client = wxART_MAKE_CLIENT_ID_FROM_STR(art_client);
1323
1324 return true;
1325 }
1326 }
1327
1328 return false;
1329 }
1330
1331 } // anonymous namespace
1332
1333 wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
1334 const wxArtClient& defaultArtClient,
1335 wxSize size)
1336 {
1337 return GetBitmap(GetParamNode(param), defaultArtClient, size);
1338 }
1339
1340 wxBitmap wxXmlResourceHandler::GetBitmap(const wxXmlNode* node,
1341 const wxArtClient& defaultArtClient,
1342 wxSize size)
1343 {
1344 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1345 wxString art_id, art_client;
1346 if ( GetStockArtAttrs(node, defaultArtClient,
1347 art_id, art_client) )
1348 {
1349 wxBitmap stockArt(wxArtProvider::GetBitmap(art_id, art_client, size));
1350 if ( stockArt.Ok() )
1351 return stockArt;
1352 }
1353
1354 /* ...or load the bitmap from file: */
1355 wxString name = GetParamValue(node);
1356 if (name.empty()) return wxNullBitmap;
1357 #if wxUSE_FILESYSTEM
1358 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
1359 if (fsfile == NULL)
1360 {
1361 ReportParamError
1362 (
1363 node->GetName(),
1364 wxString::Format("cannot open bitmap resource \"%s\"", name)
1365 );
1366 return wxNullBitmap;
1367 }
1368 wxImage img(*(fsfile->GetStream()));
1369 delete fsfile;
1370 #else
1371 wxImage img(name);
1372 #endif
1373
1374 if (!img.Ok())
1375 {
1376 ReportParamError
1377 (
1378 node->GetName(),
1379 wxString::Format("cannot create bitmap from \"%s\"", name)
1380 );
1381 return wxNullBitmap;
1382 }
1383 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
1384 return wxBitmap(img);
1385 }
1386
1387
1388 wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
1389 const wxArtClient& defaultArtClient,
1390 wxSize size)
1391 {
1392 return GetIcon(GetParamNode(param), defaultArtClient, size);
1393 }
1394
1395 wxIcon wxXmlResourceHandler::GetIcon(const wxXmlNode* node,
1396 const wxArtClient& defaultArtClient,
1397 wxSize size)
1398 {
1399 wxIcon icon;
1400 icon.CopyFromBitmap(GetBitmap(node, defaultArtClient, size));
1401 return icon;
1402 }
1403
1404
1405 wxIconBundle wxXmlResourceHandler::GetIconBundle(const wxString& param,
1406 const wxArtClient& defaultArtClient)
1407 {
1408 wxString art_id, art_client;
1409 if ( GetStockArtAttrs(GetParamNode(param), defaultArtClient,
1410 art_id, art_client) )
1411 {
1412 wxIconBundle stockArt(wxArtProvider::GetIconBundle(art_id, art_client));
1413 if ( stockArt.IsOk() )
1414 return stockArt;
1415 }
1416
1417 const wxString name = GetParamValue(param);
1418 if ( name.empty() )
1419 return wxNullIconBundle;
1420
1421 #if wxUSE_FILESYSTEM
1422 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
1423 if ( fsfile == NULL )
1424 {
1425 ReportParamError
1426 (
1427 param,
1428 wxString::Format("cannot open icon resource \"%s\"", name)
1429 );
1430 return wxNullIconBundle;
1431 }
1432
1433 wxIconBundle bundle(*(fsfile->GetStream()));
1434 delete fsfile;
1435 #else
1436 wxIconBundle bundle(name);
1437 #endif
1438
1439 if ( !bundle.IsOk() )
1440 {
1441 ReportParamError
1442 (
1443 param,
1444 wxString::Format("cannot create icon from \"%s\"", name)
1445 );
1446 return wxNullIconBundle;
1447 }
1448
1449 return bundle;
1450 }
1451
1452
1453 wxImageList *wxXmlResourceHandler::GetImageList(const wxString& param)
1454 {
1455 wxXmlNode * const imagelist_node = GetParamNode(param);
1456 if ( !imagelist_node )
1457 return NULL;
1458
1459 wxXmlNode * const oldnode = m_node;
1460 m_node = imagelist_node;
1461
1462 // size
1463 wxSize size = GetSize();
1464 size.SetDefaults(wxSize(wxSystemSettings::GetMetric(wxSYS_ICON_X),
1465 wxSystemSettings::GetMetric(wxSYS_ICON_Y)));
1466
1467 // mask: true by default
1468 bool mask = HasParam(wxT("mask")) ? GetBool(wxT("mask"), true) : true;
1469
1470 // now we have everything we need to create the image list
1471 wxImageList *imagelist = new wxImageList(size.x, size.y, mask);
1472
1473 // add images
1474 wxString parambitmap = wxT("bitmap");
1475 if ( HasParam(parambitmap) )
1476 {
1477 wxXmlNode *n = m_node->GetChildren();
1478 while (n)
1479 {
1480 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == parambitmap)
1481 {
1482 // add icon instead of bitmap to keep the bitmap mask
1483 imagelist->Add(GetIcon(n));
1484 }
1485 n = n->GetNext();
1486 }
1487 }
1488
1489 m_node = oldnode;
1490 return imagelist;
1491 }
1492
1493 wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
1494 {
1495 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
1496
1497 wxXmlNode *n = m_node->GetChildren();
1498
1499 while (n)
1500 {
1501 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
1502 {
1503 // TODO: check that there are no other properties/parameters with
1504 // the same name and log an error if there are (can't do this
1505 // right now as I'm not sure if it's not going to break code
1506 // using this function in unintentional way (i.e. for
1507 // accessing other things than properties), for example
1508 // wxBitmapComboBoxXmlHandler almost surely does
1509 return n;
1510 }
1511 n = n->GetNext();
1512 }
1513 return NULL;
1514 }
1515
1516 bool wxXmlResourceHandler::IsOfClass(wxXmlNode *node, const wxString& classname)
1517 {
1518 return node->GetAttribute(wxT("class"), wxEmptyString) == classname;
1519 }
1520
1521
1522
1523 wxString wxXmlResourceHandler::GetNodeContent(const wxXmlNode *node)
1524 {
1525 const wxXmlNode *n = node;
1526 if (n == NULL) return wxEmptyString;
1527 n = n->GetChildren();
1528
1529 while (n)
1530 {
1531 if (n->GetType() == wxXML_TEXT_NODE ||
1532 n->GetType() == wxXML_CDATA_SECTION_NODE)
1533 return n->GetContent();
1534 n = n->GetNext();
1535 }
1536 return wxEmptyString;
1537 }
1538
1539
1540
1541 wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
1542 {
1543 if (param.empty())
1544 return GetNodeContent(m_node);
1545 else
1546 return GetNodeContent(GetParamNode(param));
1547 }
1548
1549 wxString wxXmlResourceHandler::GetParamValue(const wxXmlNode* node)
1550 {
1551 return GetNodeContent(node);
1552 }
1553
1554
1555 wxSize wxXmlResourceHandler::GetSize(const wxString& param,
1556 wxWindow *windowToUse)
1557 {
1558 wxString s = GetParamValue(param);
1559 if (s.empty()) s = wxT("-1,-1");
1560 bool is_dlg;
1561 long sx, sy = 0;
1562
1563 is_dlg = s[s.length()-1] == wxT('d');
1564 if (is_dlg) s.RemoveLast();
1565
1566 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
1567 !s.AfterLast(wxT(',')).ToLong(&sy))
1568 {
1569 ReportParamError
1570 (
1571 param,
1572 wxString::Format("cannot parse coordinates value \"%s\"", s)
1573 );
1574 return wxDefaultSize;
1575 }
1576
1577 if (is_dlg)
1578 {
1579 if (windowToUse)
1580 {
1581 return wxDLG_UNIT(windowToUse, wxSize(sx, sy));
1582 }
1583 else if (m_parentAsWindow)
1584 {
1585 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
1586 }
1587 else
1588 {
1589 ReportParamError
1590 (
1591 param,
1592 "cannot convert dialog units: dialog unknown"
1593 );
1594 return wxDefaultSize;
1595 }
1596 }
1597
1598 return wxSize(sx, sy);
1599 }
1600
1601
1602
1603 wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
1604 {
1605 wxSize sz = GetSize(param);
1606 return wxPoint(sz.x, sz.y);
1607 }
1608
1609
1610
1611 wxCoord wxXmlResourceHandler::GetDimension(const wxString& param,
1612 wxCoord defaultv,
1613 wxWindow *windowToUse)
1614 {
1615 wxString s = GetParamValue(param);
1616 if (s.empty()) return defaultv;
1617 bool is_dlg;
1618 long sx;
1619
1620 is_dlg = s[s.length()-1] == wxT('d');
1621 if (is_dlg) s.RemoveLast();
1622
1623 if (!s.ToLong(&sx))
1624 {
1625 ReportParamError
1626 (
1627 param,
1628 wxString::Format("cannot parse dimension value \"%s\"", s)
1629 );
1630 return defaultv;
1631 }
1632
1633 if (is_dlg)
1634 {
1635 if (windowToUse)
1636 {
1637 return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x;
1638 }
1639 else if (m_parentAsWindow)
1640 {
1641 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
1642 }
1643 else
1644 {
1645 ReportParamError
1646 (
1647 param,
1648 "cannot convert dialog units: dialog unknown"
1649 );
1650 return defaultv;
1651 }
1652 }
1653
1654 return sx;
1655 }
1656
1657
1658 // Get system font index using indexname
1659 static wxFont GetSystemFont(const wxString& name)
1660 {
1661 if (!name.empty())
1662 {
1663 #define SYSFNT(fnt) \
1664 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1665 SYSFNT(wxSYS_OEM_FIXED_FONT)
1666 SYSFNT(wxSYS_ANSI_FIXED_FONT)
1667 SYSFNT(wxSYS_ANSI_VAR_FONT)
1668 SYSFNT(wxSYS_SYSTEM_FONT)
1669 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT)
1670 SYSFNT(wxSYS_SYSTEM_FIXED_FONT)
1671 SYSFNT(wxSYS_DEFAULT_GUI_FONT)
1672 #undef SYSFNT
1673 }
1674
1675 return wxNullFont;
1676 }
1677
1678 wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1679 {
1680 wxXmlNode *font_node = GetParamNode(param);
1681 if (font_node == NULL)
1682 {
1683 ReportError(
1684 wxString::Format("cannot find font node \"%s\"", param));
1685 return wxNullFont;
1686 }
1687
1688 wxXmlNode *oldnode = m_node;
1689 m_node = font_node;
1690
1691 // font attributes:
1692
1693 // size
1694 int isize = -1;
1695 bool hasSize = HasParam(wxT("size"));
1696 if (hasSize)
1697 isize = GetLong(wxT("size"), -1);
1698
1699 // style
1700 int istyle = wxNORMAL;
1701 bool hasStyle = HasParam(wxT("style"));
1702 if (hasStyle)
1703 {
1704 wxString style = GetParamValue(wxT("style"));
1705 if (style == wxT("italic"))
1706 istyle = wxITALIC;
1707 else if (style == wxT("slant"))
1708 istyle = wxSLANT;
1709 }
1710
1711 // weight
1712 int iweight = wxNORMAL;
1713 bool hasWeight = HasParam(wxT("weight"));
1714 if (hasWeight)
1715 {
1716 wxString weight = GetParamValue(wxT("weight"));
1717 if (weight == wxT("bold"))
1718 iweight = wxBOLD;
1719 else if (weight == wxT("light"))
1720 iweight = wxLIGHT;
1721 }
1722
1723 // underline
1724 bool hasUnderlined = HasParam(wxT("underlined"));
1725 bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false;
1726
1727 // family and facename
1728 int ifamily = wxDEFAULT;
1729 bool hasFamily = HasParam(wxT("family"));
1730 if (hasFamily)
1731 {
1732 wxString family = GetParamValue(wxT("family"));
1733 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1734 else if (family == wxT("roman")) ifamily = wxROMAN;
1735 else if (family == wxT("script")) ifamily = wxSCRIPT;
1736 else if (family == wxT("swiss")) ifamily = wxSWISS;
1737 else if (family == wxT("modern")) ifamily = wxMODERN;
1738 else if (family == wxT("teletype")) ifamily = wxTELETYPE;
1739 }
1740
1741
1742 wxString facename;
1743 bool hasFacename = HasParam(wxT("face"));
1744 if (hasFacename)
1745 {
1746 wxString faces = GetParamValue(wxT("face"));
1747 wxStringTokenizer tk(faces, wxT(","));
1748 #if wxUSE_FONTENUM
1749 wxArrayString facenames(wxFontEnumerator::GetFacenames());
1750 while (tk.HasMoreTokens())
1751 {
1752 int index = facenames.Index(tk.GetNextToken(), false);
1753 if (index != wxNOT_FOUND)
1754 {
1755 facename = facenames[index];
1756 break;
1757 }
1758 }
1759 #else // !wxUSE_FONTENUM
1760 // just use the first face name if we can't check its availability:
1761 if (tk.HasMoreTokens())
1762 facename = tk.GetNextToken();
1763 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
1764 }
1765
1766 // encoding
1767 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1768 bool hasEncoding = HasParam(wxT("encoding"));
1769 if (hasEncoding)
1770 {
1771 wxString encoding = GetParamValue(wxT("encoding"));
1772 wxFontMapper mapper;
1773 if (!encoding.empty())
1774 enc = mapper.CharsetToEncoding(encoding);
1775 if (enc == wxFONTENCODING_SYSTEM)
1776 enc = wxFONTENCODING_DEFAULT;
1777 }
1778
1779 // is this font based on a system font?
1780 wxFont font = GetSystemFont(GetParamValue(wxT("sysfont")));
1781
1782 if (font.Ok())
1783 {
1784 if (hasSize && isize != -1)
1785 font.SetPointSize(isize);
1786 else if (HasParam(wxT("relativesize")))
1787 font.SetPointSize(int(font.GetPointSize() *
1788 GetFloat(wxT("relativesize"))));
1789
1790 if (hasStyle)
1791 font.SetStyle(istyle);
1792 if (hasWeight)
1793 font.SetWeight(iweight);
1794 if (hasUnderlined)
1795 font.SetUnderlined(underlined);
1796 if (hasFamily)
1797 font.SetFamily(ifamily);
1798 if (hasFacename)
1799 font.SetFaceName(facename);
1800 if (hasEncoding)
1801 font.SetDefaultEncoding(enc);
1802 }
1803 else // not based on system font
1804 {
1805 font = wxFont(isize == -1 ? wxNORMAL_FONT->GetPointSize() : isize,
1806 ifamily, istyle, iweight,
1807 underlined, facename, enc);
1808 }
1809
1810 m_node = oldnode;
1811 return font;
1812 }
1813
1814
1815 void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1816 {
1817 //FIXME : add cursor
1818
1819 if (HasParam(wxT("exstyle")))
1820 // Have to OR it with existing style, since
1821 // some implementations (e.g. wxGTK) use the extra style
1822 // during creation
1823 wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle")));
1824 if (HasParam(wxT("bg")))
1825 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1826 if (HasParam(wxT("fg")))
1827 wnd->SetForegroundColour(GetColour(wxT("fg")));
1828 if (GetBool(wxT("enabled"), 1) == 0)
1829 wnd->Enable(false);
1830 if (GetBool(wxT("focused"), 0) == 1)
1831 wnd->SetFocus();
1832 if (GetBool(wxT("hidden"), 0) == 1)
1833 wnd->Show(false);
1834 #if wxUSE_TOOLTIPS
1835 if (HasParam(wxT("tooltip")))
1836 wnd->SetToolTip(GetText(wxT("tooltip")));
1837 #endif
1838 if (HasParam(wxT("font")))
1839 wnd->SetFont(GetFont());
1840 if (HasParam(wxT("help")))
1841 wnd->SetHelpText(GetText(wxT("help")));
1842 }
1843
1844
1845 void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1846 {
1847 for ( wxXmlNode *n = m_node->GetChildren(); n; n = n->GetNext() )
1848 {
1849 if ( IsObjectNode(n) )
1850 {
1851 m_resource->CreateResFromNode(n, parent, NULL,
1852 this_hnd_only ? this : NULL);
1853 }
1854 }
1855 }
1856
1857
1858 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1859 {
1860 wxXmlNode *root;
1861 if (rootnode == NULL) root = m_node; else root = rootnode;
1862 wxXmlNode *n = root->GetChildren();
1863
1864 while (n)
1865 {
1866 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1867 {
1868 CreateResource(n, parent, NULL);
1869 }
1870 n = n->GetNext();
1871 }
1872 }
1873
1874
1875 //-----------------------------------------------------------------------------
1876 // errors reporting
1877 //-----------------------------------------------------------------------------
1878
1879 void wxXmlResourceHandler::ReportError(const wxString& message)
1880 {
1881 m_resource->ReportError(m_node, message);
1882 }
1883
1884 void wxXmlResourceHandler::ReportError(wxXmlNode *context,
1885 const wxString& message)
1886 {
1887 m_resource->ReportError(context ? context : m_node, message);
1888 }
1889
1890 void wxXmlResourceHandler::ReportParamError(const wxString& param,
1891 const wxString& message)
1892 {
1893 m_resource->ReportError(GetParamNode(param), message);
1894 }
1895
1896 void wxXmlResource::ReportError(wxXmlNode *context, const wxString& message)
1897 {
1898 if ( !context )
1899 {
1900 DoReportError("", NULL, message);
1901 return;
1902 }
1903
1904 // We need to find out the file that 'context' is part of. Performance of
1905 // this code is not critical, so we simply find the root XML node and
1906 // compare it with all loaded XRC files.
1907 const wxString filename = GetFileNameFromNode(context, Data());
1908
1909 DoReportError(filename, context, message);
1910 }
1911
1912 void wxXmlResource::DoReportError(const wxString& xrcFile, wxXmlNode *position,
1913 const wxString& message)
1914 {
1915 const int line = position ? position->GetLineNumber() : -1;
1916
1917 wxString loc;
1918 if ( !xrcFile.empty() )
1919 loc = xrcFile + ':';
1920 if ( line != -1 )
1921 loc += wxString::Format("%d:", line);
1922 if ( !loc.empty() )
1923 loc += ' ';
1924
1925 wxLogError("XRC error: %s%s", loc, message);
1926 }
1927
1928
1929 //-----------------------------------------------------------------------------
1930 // XRCID implementation
1931 //-----------------------------------------------------------------------------
1932
1933 #define XRCID_TABLE_SIZE 1024
1934
1935
1936 struct XRCID_record
1937 {
1938 /* Hold the id so that once an id is allocated for a name, it
1939 does not get created again by NewControlId at least
1940 until we are done with it */
1941 wxWindowIDRef id;
1942 char *key;
1943 XRCID_record *next;
1944 };
1945
1946 static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
1947
1948 static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE)
1949 {
1950 int index = 0;
1951
1952 for (const char *c = str_id; *c != '\0'; c++) index += (int)*c;
1953 index %= XRCID_TABLE_SIZE;
1954
1955 XRCID_record *oldrec = NULL;
1956 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
1957 {
1958 if (wxStrcmp(rec->key, str_id) == 0)
1959 {
1960 return rec->id;
1961 }
1962 oldrec = rec;
1963 }
1964
1965 XRCID_record **rec_var = (oldrec == NULL) ?
1966 &XRCID_Records[index] : &oldrec->next;
1967 *rec_var = new XRCID_record;
1968 (*rec_var)->key = wxStrdup(str_id);
1969 (*rec_var)->next = NULL;
1970
1971 char *end;
1972 if (value_if_not_found != wxID_NONE)
1973 (*rec_var)->id = value_if_not_found;
1974 else
1975 {
1976 int asint = wxStrtol(str_id, &end, 10);
1977 if (*str_id && *end == 0)
1978 {
1979 // if str_id was integer, keep it verbosely:
1980 (*rec_var)->id = asint;
1981 }
1982 else
1983 {
1984 (*rec_var)->id = wxWindowBase::NewControlId();
1985 }
1986 }
1987
1988 return (*rec_var)->id;
1989 }
1990
1991 static void AddStdXRCID_Records();
1992
1993 /*static*/
1994 int wxXmlResource::DoGetXRCID(const char *str_id, int value_if_not_found)
1995 {
1996 static bool s_stdIDsAdded = false;
1997
1998 if ( !s_stdIDsAdded )
1999 {
2000 s_stdIDsAdded = true;
2001 AddStdXRCID_Records();
2002 }
2003
2004 return XRCID_Lookup(str_id, value_if_not_found);
2005 }
2006
2007 /* static */
2008 wxString wxXmlResource::FindXRCIDById(int numId)
2009 {
2010 for ( int i = 0; i < XRCID_TABLE_SIZE; i++ )
2011 {
2012 for ( XRCID_record *rec = XRCID_Records[i]; rec; rec = rec->next )
2013 {
2014 if ( rec->id == numId )
2015 return wxString(rec->key);
2016 }
2017 }
2018
2019 return wxString();
2020 }
2021
2022 static void CleanXRCID_Record(XRCID_record *rec)
2023 {
2024 if (rec)
2025 {
2026 CleanXRCID_Record(rec->next);
2027
2028 free(rec->key);
2029 delete rec;
2030 }
2031 }
2032
2033 static void CleanXRCID_Records()
2034 {
2035 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
2036 {
2037 CleanXRCID_Record(XRCID_Records[i]);
2038 XRCID_Records[i] = NULL;
2039 }
2040 }
2041
2042 static void AddStdXRCID_Records()
2043 {
2044 #define stdID(id) XRCID_Lookup(#id, id)
2045 stdID(-1);
2046
2047 stdID(wxID_ANY);
2048 stdID(wxID_SEPARATOR);
2049
2050 stdID(wxID_OPEN);
2051 stdID(wxID_CLOSE);
2052 stdID(wxID_NEW);
2053 stdID(wxID_SAVE);
2054 stdID(wxID_SAVEAS);
2055 stdID(wxID_REVERT);
2056 stdID(wxID_EXIT);
2057 stdID(wxID_UNDO);
2058 stdID(wxID_REDO);
2059 stdID(wxID_HELP);
2060 stdID(wxID_PRINT);
2061 stdID(wxID_PRINT_SETUP);
2062 stdID(wxID_PAGE_SETUP);
2063 stdID(wxID_PREVIEW);
2064 stdID(wxID_ABOUT);
2065 stdID(wxID_HELP_CONTENTS);
2066 stdID(wxID_HELP_COMMANDS);
2067 stdID(wxID_HELP_PROCEDURES);
2068 stdID(wxID_HELP_CONTEXT);
2069 stdID(wxID_CLOSE_ALL);
2070 stdID(wxID_PREFERENCES);
2071 stdID(wxID_EDIT);
2072 stdID(wxID_CUT);
2073 stdID(wxID_COPY);
2074 stdID(wxID_PASTE);
2075 stdID(wxID_CLEAR);
2076 stdID(wxID_FIND);
2077 stdID(wxID_DUPLICATE);
2078 stdID(wxID_SELECTALL);
2079 stdID(wxID_DELETE);
2080 stdID(wxID_REPLACE);
2081 stdID(wxID_REPLACE_ALL);
2082 stdID(wxID_PROPERTIES);
2083 stdID(wxID_VIEW_DETAILS);
2084 stdID(wxID_VIEW_LARGEICONS);
2085 stdID(wxID_VIEW_SMALLICONS);
2086 stdID(wxID_VIEW_LIST);
2087 stdID(wxID_VIEW_SORTDATE);
2088 stdID(wxID_VIEW_SORTNAME);
2089 stdID(wxID_VIEW_SORTSIZE);
2090 stdID(wxID_VIEW_SORTTYPE);
2091 stdID(wxID_FILE1);
2092 stdID(wxID_FILE2);
2093 stdID(wxID_FILE3);
2094 stdID(wxID_FILE4);
2095 stdID(wxID_FILE5);
2096 stdID(wxID_FILE6);
2097 stdID(wxID_FILE7);
2098 stdID(wxID_FILE8);
2099 stdID(wxID_FILE9);
2100 stdID(wxID_OK);
2101 stdID(wxID_CANCEL);
2102 stdID(wxID_APPLY);
2103 stdID(wxID_YES);
2104 stdID(wxID_NO);
2105 stdID(wxID_STATIC);
2106 stdID(wxID_FORWARD);
2107 stdID(wxID_BACKWARD);
2108 stdID(wxID_DEFAULT);
2109 stdID(wxID_MORE);
2110 stdID(wxID_SETUP);
2111 stdID(wxID_RESET);
2112 stdID(wxID_CONTEXT_HELP);
2113 stdID(wxID_YESTOALL);
2114 stdID(wxID_NOTOALL);
2115 stdID(wxID_ABORT);
2116 stdID(wxID_RETRY);
2117 stdID(wxID_IGNORE);
2118 stdID(wxID_ADD);
2119 stdID(wxID_REMOVE);
2120 stdID(wxID_UP);
2121 stdID(wxID_DOWN);
2122 stdID(wxID_HOME);
2123 stdID(wxID_REFRESH);
2124 stdID(wxID_STOP);
2125 stdID(wxID_INDEX);
2126 stdID(wxID_BOLD);
2127 stdID(wxID_ITALIC);
2128 stdID(wxID_JUSTIFY_CENTER);
2129 stdID(wxID_JUSTIFY_FILL);
2130 stdID(wxID_JUSTIFY_RIGHT);
2131 stdID(wxID_JUSTIFY_LEFT);
2132 stdID(wxID_UNDERLINE);
2133 stdID(wxID_INDENT);
2134 stdID(wxID_UNINDENT);
2135 stdID(wxID_ZOOM_100);
2136 stdID(wxID_ZOOM_FIT);
2137 stdID(wxID_ZOOM_IN);
2138 stdID(wxID_ZOOM_OUT);
2139 stdID(wxID_UNDELETE);
2140 stdID(wxID_REVERT_TO_SAVED);
2141 stdID(wxID_SYSTEM_MENU);
2142 stdID(wxID_CLOSE_FRAME);
2143 stdID(wxID_MOVE_FRAME);
2144 stdID(wxID_RESIZE_FRAME);
2145 stdID(wxID_MAXIMIZE_FRAME);
2146 stdID(wxID_ICONIZE_FRAME);
2147 stdID(wxID_RESTORE_FRAME);
2148 stdID(wxID_CDROM);
2149 stdID(wxID_CONVERT);
2150 stdID(wxID_EXECUTE);
2151 stdID(wxID_FLOPPY);
2152 stdID(wxID_HARDDISK);
2153 stdID(wxID_BOTTOM);
2154 stdID(wxID_FIRST);
2155 stdID(wxID_LAST);
2156 stdID(wxID_TOP);
2157 stdID(wxID_INFO);
2158 stdID(wxID_JUMP_TO);
2159 stdID(wxID_NETWORK);
2160 stdID(wxID_SELECT_COLOR);
2161 stdID(wxID_SELECT_FONT);
2162 stdID(wxID_SORT_ASCENDING);
2163 stdID(wxID_SORT_DESCENDING);
2164 stdID(wxID_SPELL_CHECK);
2165 stdID(wxID_STRIKETHROUGH);
2166
2167 #undef stdID
2168 }
2169
2170
2171
2172
2173
2174 //-----------------------------------------------------------------------------
2175 // module and globals
2176 //-----------------------------------------------------------------------------
2177
2178 // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2179 // can happen that some XRC records have been created because of the use of
2180 // XRCID() in event tables, which happens during static objects initialization,
2181 // but then the application initialization failed and so the wx modules were
2182 // neither initialized nor cleaned up -- this static object does the cleanup in
2183 // this case
2184 static struct wxXRCStaticCleanup
2185 {
2186 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2187 } s_staticCleanup;
2188
2189 class wxXmlResourceModule: public wxModule
2190 {
2191 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
2192 public:
2193 wxXmlResourceModule() {}
2194 bool OnInit()
2195 {
2196 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
2197 return true;
2198 }
2199 void OnExit()
2200 {
2201 delete wxXmlResource::Set(NULL);
2202 if(wxXmlResource::ms_subclassFactories)
2203 {
2204 for ( wxXmlSubclassFactories::iterator i = wxXmlResource::ms_subclassFactories->begin();
2205 i != wxXmlResource::ms_subclassFactories->end(); ++i )
2206 {
2207 delete *i;
2208 }
2209 wxDELETE(wxXmlResource::ms_subclassFactories);
2210 }
2211 CleanXRCID_Records();
2212 }
2213 };
2214
2215 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
2216
2217
2218 // When wxXml is loaded dynamically after the application is already running
2219 // then the built-in module system won't pick this one up. Add it manually.
2220 void wxXmlInitResourceModule()
2221 {
2222 wxModule* module = new wxXmlResourceModule;
2223 module->Init();
2224 wxModule::RegisterModule(module);
2225 }
2226
2227 #endif // wxUSE_XRC