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