1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xmlres.cpp
3 // Purpose: XRC resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/xrc/xmlres.h"
27 #include "wx/dialog.h"
28 #include "wx/settings.h"
29 #include "wx/bitmap.h"
31 #include "wx/module.h"
32 #include "wx/wxcrtvararg.h"
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"
49 #include "wx/xml/xml.h"
52 class wxXmlResourceDataRecord
55 wxXmlResourceDataRecord() : Doc(NULL
) {
57 Time
= wxDateTime::Now();
60 ~wxXmlResourceDataRecord() {delete Doc
;}
69 class wxXmlResourceDataRecords
: public wxVector
<wxXmlResourceDataRecord
*>
71 // this is a class so that it can be forward-declared
77 // helper used by DoFindResource() and elsewhere: returns true if this is an
78 // object or object_ref node
80 // node must be non-NULL
81 inline bool IsObjectNode(wxXmlNode
*node
)
83 return node
->GetType() == wxXML_ELEMENT_NODE
&&
84 (node
->GetName() == wxS("object") ||
85 node
->GetName() == wxS("object_ref"));
88 // special XML attribute with name of input file, see GetFileNameFromNode()
89 const char *ATTR_INPUT_FILENAME
= "__wx:filename";
91 // helper to get filename corresponding to an XML node
93 GetFileNameFromNode(wxXmlNode
*node
, const wxXmlResourceDataRecords
& files
)
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'
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
);
110 if ( !node
->GetParent() )
111 break; // we found the root of this XML tree
113 node
= node
->GetParent();
116 // NB: 'node' now points to the root of XML document
118 for ( wxXmlResourceDataRecords::const_iterator i
= files
.begin();
119 i
!= files
.end(); ++i
)
121 if ( (*i
)->Doc
->GetRoot() == node
)
127 return wxEmptyString
; // not found
130 } // anonymous namespace
133 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
135 /*static*/ wxXmlResource
*wxXmlResource::Get()
138 ms_instance
= new wxXmlResource
;
142 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
144 wxXmlResource
*old
= ms_instance
;
149 wxXmlResource::wxXmlResource(int flags
, const wxString
& domain
)
153 m_data
= new wxXmlResourceDataRecords
;
157 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
161 m_data
= new wxXmlResourceDataRecords
;
166 wxXmlResource::~wxXmlResource()
170 for ( wxXmlResourceDataRecords::iterator i
= m_data
->begin();
171 i
!= m_data
->end(); ++i
)
178 void wxXmlResource::SetDomain(const wxString
& domain
)
185 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
187 wxString
fnd(filename
);
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
))
196 // Make the name absolute filename, because the app may
197 // change working directory later:
202 fnd
= fn
.GetFullPath();
205 fnd
= wxFileSystem::FileNameToURL(fnd
);
215 bool wxXmlResource::IsArchive(const wxString
& filename
)
217 const wxString fnd
= filename
.Lower();
219 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
222 #endif // wxUSE_FILESYSTEM
224 bool wxXmlResource::LoadFile(const wxFileName
& file
)
227 return Load(wxFileSystem::FileNameToURL(file
));
229 return Load(file
.GetFullPath());
233 bool wxXmlResource::LoadAllFiles(const wxString
& dirname
)
238 wxDir::GetAllFiles(dirname
, &files
, "*.xrc");
240 for ( wxArrayString::const_iterator i
= files
.begin(); i
!= files
.end(); ++i
)
249 bool wxXmlResource::Load(const wxString
& filemask_
)
251 wxString filemask
= ConvertFileNameToURL(filemask_
);
255 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
256 # define wxXmlFindNext fsys.FindNext()
258 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
259 # define wxXmlFindNext wxFindNextFile()
261 wxString fnd
= wxXmlFindFirst
;
264 wxLogError(_("Cannot load resources from '%s'."), filemask
);
271 if ( IsArchive(fnd
) )
273 if ( !Load(fnd
+ wxT("#zip:*.xrc")) )
276 else // a single resource URL
277 #endif // wxUSE_FILESYSTEM
279 wxXmlResourceDataRecord
*drec
= new wxXmlResourceDataRecord
;
281 Data().push_back(drec
);
286 # undef wxXmlFindFirst
287 # undef wxXmlFindNext
289 return UpdateResources();
292 bool wxXmlResource::Unload(const wxString
& filename
)
294 wxASSERT_MSG( !wxIsWild(filename
),
295 wxT("wildcards not supported by wxXmlResource::Unload()") );
297 wxString fnd
= ConvertFileNameToURL(filename
);
299 const bool isArchive
= IsArchive(fnd
);
302 #endif // wxUSE_FILESYSTEM
304 bool unloaded
= false;
305 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
306 i
!= Data().end(); ++i
)
311 if ( (*i
)->File
.StartsWith(fnd
) )
313 // don't break from the loop, we can have other matching files
315 else // a single resource URL
316 #endif // wxUSE_FILESYSTEM
318 if ( (*i
)->File
== fnd
)
324 // no sense in continuing, there is only one file with this URL
334 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
336 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
338 m_handlers
.push_back(handler
);
339 handler
->SetParentResource(this);
342 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
344 m_handlers
.insert(m_handlers
.begin(), handler
);
345 handler
->SetParentResource(this);
350 void wxXmlResource::ClearHandlers()
352 for ( wxVector
<wxXmlResourceHandler
*>::iterator i
= m_handlers
.begin();
353 i
!= m_handlers
.end(); ++i
)
359 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
361 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
366 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
368 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
374 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
376 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
381 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
383 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
386 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
388 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
393 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
395 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
398 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
400 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
403 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
405 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
408 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
410 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
413 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
415 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
416 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
419 if (bmp
) { rt
= *bmp
; delete bmp
; }
423 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
425 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
426 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
429 if (icon
) { rt
= *icon
; delete icon
; }
434 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
436 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
439 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
441 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
445 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
446 wxWindow
*control
, wxWindow
*parent
)
449 parent
= control
->GetParent();
450 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
453 wxLogError("Cannot find container for unknown control '%s'.", name
);
456 return control
->Reparent(container
);
460 static void ProcessPlatformProperty(wxXmlNode
*node
)
465 wxXmlNode
*c
= node
->GetChildren();
469 if (!c
->GetAttribute(wxT("platform"), &s
))
473 wxStringTokenizer
tkn(s
, wxT(" |"));
475 while (tkn
.HasMoreTokens())
477 s
= tkn
.GetNextToken();
479 if (s
== wxT("win")) isok
= true;
481 #if defined(__MAC__) || defined(__APPLE__)
482 if (s
== wxT("mac")) isok
= true;
483 #elif defined(__UNIX__)
484 if (s
== wxT("unix")) isok
= true;
487 if (s
== wxT("os2")) isok
= true;
497 ProcessPlatformProperty(c
);
502 wxXmlNode
*c2
= c
->GetNext();
503 node
->RemoveChild(c
);
512 bool wxXmlResource::UpdateResources()
516 # if wxUSE_FILESYSTEM
517 wxFSFile
*file
= NULL
;
522 wxString
encoding(wxT("UTF-8"));
523 #if !wxUSE_UNICODE && wxUSE_INTL
524 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
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();
533 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
534 i
!= Data().end(); ++i
)
536 wxXmlResourceDataRecord
* const rec
= *i
;
538 modif
= (rec
->Doc
== NULL
);
540 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
542 # if wxUSE_FILESYSTEM
543 file
= fsys
.OpenFile(rec
->File
);
545 modif
= file
&& file
->GetModificationTime() > rec
->Time
;
546 # else // wxUSE_DATETIME
548 # endif // wxUSE_DATETIME
551 wxLogError(_("Cannot open file '%s'."), rec
->File
);
556 # else // wxUSE_FILESYSTEM
558 modif
= wxDateTime(wxFileModificationTime(rec
->File
)) > rec
->Time
;
559 # else // wxUSE_DATETIME
561 # endif // wxUSE_DATETIME
562 # endif // wxUSE_FILESYSTEM
567 wxLogTrace(wxT("xrc"), wxT("opening file '%s'"), rec
->File
);
569 wxInputStream
*stream
= NULL
;
571 # if wxUSE_FILESYSTEM
572 file
= fsys
.OpenFile(rec
->File
);
574 stream
= file
->GetStream();
576 stream
= new wxFileInputStream(rec
->File
);
582 rec
->Doc
= new wxXmlDocument
;
584 if (!stream
|| !stream
->IsOk() || !rec
->Doc
->Load(*stream
, encoding
))
586 wxLogError(_("Cannot load resources from file '%s'."),
591 else if (rec
->Doc
->GetRoot()->GetName() != wxT("resource"))
596 "invalid XRC resource, doesn't have root node <resource>"
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
;
614 if (m_version
!= version
)
616 wxLogError("Resource files must have same version number.");
620 ProcessPlatformProperty(rec
->Doc
->GetRoot());
623 rec
->Time
= file
->GetModificationTime();
624 #else // wxUSE_FILESYSTEM
625 rec
->Time
= wxDateTime(wxFileModificationTime(rec
->File
));
626 #endif // wxUSE_FILESYSTEM
627 #endif // wxUSE_DATETIME
630 # if wxUSE_FILESYSTEM
642 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
643 const wxString
& name
,
644 const wxString
& classname
,
645 bool recursive
) const
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())
653 if ( IsObjectNode(node
) && node
->GetAttribute(wxS("name")) == name
)
655 // empty class name matches everything
656 if ( classname
.empty() )
659 wxString
cls(node
->GetAttribute(wxS("class")));
661 // object_ref may not have 'class' attribute:
662 if (cls
.empty() && node
->GetName() == wxS("object_ref"))
664 wxString refName
= node
->GetAttribute(wxS("ref"));
668 const wxXmlNode
* const refNode
= GetResourceNode(refName
);
670 cls
= refNode
->GetAttribute(wxS("class"));
673 if ( cls
== classname
)
678 // then recurse in child nodes
681 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
683 if ( IsObjectNode(node
) )
685 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
695 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
696 const wxString
& classname
,
701 node
= GetResourceNodeAndLocation(name
, classname
, recursive
, &path
);
710 "XRC resource \"%s\" (class \"%s\") not found",
716 else // node was found
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
);
723 #endif // wxUSE_FILESYSTEM
729 wxXmlResource::GetResourceNodeAndLocation(const wxString
& name
,
730 const wxString
& classname
,
732 wxString
*path
) const
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();
738 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
739 f
!= Data().end(); ++f
)
741 wxXmlResourceDataRecord
*const rec
= *f
;
742 wxXmlDocument
* const doc
= rec
->Doc
;
743 if ( !doc
|| !doc
->GetRoot() )
747 found
= DoFindResource(doc
->GetRoot(), name
, classname
, recursive
);
760 static void MergeNodesOver(wxXmlNode
& dest
, wxXmlNode
& overwriteWith
,
761 const wxString
& overwriteFilename
)
764 for ( wxXmlAttribute
*attr
= overwriteWith
.GetAttributes();
765 attr
; attr
= attr
->GetNext() )
767 wxXmlAttribute
*dattr
;
768 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
771 if ( dattr
->GetName() == attr
->GetName() )
773 dattr
->SetValue(attr
->GetValue());
779 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
782 // Merge child nodes:
783 for (wxXmlNode
* node
= overwriteWith
.GetChildren(); node
; node
= node
->GetNext())
785 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
788 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
790 if ( dnode
->GetName() == node
->GetName() &&
791 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
792 dnode
->GetType() == node
->GetType() )
794 MergeNodesOver(*dnode
, *node
, overwriteFilename
);
801 wxXmlNode
*copyOfNode
= new wxXmlNode(*node
);
802 // remember referenced object's file, see GetFileNameFromNode()
803 copyOfNode
->AddAttribute(ATTR_INPUT_FILENAME
, overwriteFilename
);
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
)
809 dest
.AddChild(copyOfNode
);
811 else if ( insert_pos
== wxT("begin") )
813 dest
.InsertChild(copyOfNode
, dest
.GetChildren());
818 if ( dest
.GetType() == wxXML_TEXT_NODE
&& overwriteWith
.GetContent().length() )
819 dest
.SetContent(overwriteWith
.GetContent());
822 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
824 wxXmlResourceHandler
*handlerToUse
)
826 if (node
== NULL
) return NULL
;
828 // handling of referenced resource
829 if ( node
->GetName() == wxT("object_ref") )
831 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
832 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
841 "referenced object node with ref=\"%s\" not found",
848 if ( !node
->GetChildren() )
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.
855 return CreateResFromNode(refNode
, parent
, instance
);
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.
864 wxXmlNode
copy(*refNode
);
865 MergeNodesOver(copy
, *node
, GetFileNameFromNode(node
, Data()));
867 // remember referenced object's file, see GetFileNameFromNode()
868 copy
.AddAttribute(ATTR_INPUT_FILENAME
,
869 GetFileNameFromNode(refNode
, Data()));
871 return CreateResFromNode(©
, parent
, instance
);
877 if (handlerToUse
->CanHandle(node
))
879 return handlerToUse
->CreateResource(node
, parent
, instance
);
882 else if (node
->GetName() == wxT("object"))
884 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
885 h
!= m_handlers
.end(); ++h
)
887 wxXmlResourceHandler
*handler
= *h
;
888 if (handler
->CanHandle(node
))
889 return handler
->CreateResource(node
, parent
, instance
);
898 "no handler found for XML node \"%s\" (class \"%s\")",
900 node
->GetAttribute("class", wxEmptyString
)
907 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
909 // this is a class so that it can be forward-declared
912 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
914 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
916 if (!ms_subclassFactories
)
918 ms_subclassFactories
= new wxXmlSubclassFactories
;
920 ms_subclassFactories
->push_back(factory
);
923 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
926 ~wxXmlSubclassFactoryCXX() {}
928 wxObject
*Create(const wxString
& className
)
930 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
933 return classInfo
->CreateObject();
942 wxXmlResourceHandler::wxXmlResourceHandler()
943 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
944 m_parentAsWindow(NULL
)
949 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
951 wxXmlNode
*myNode
= m_node
;
952 wxString myClass
= m_class
;
953 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
954 wxWindow
*myParentAW
= m_parentAsWindow
;
956 m_instance
= instance
;
957 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
958 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
960 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
961 if (!subclass
.empty())
963 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
964 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
966 m_instance
= (*i
)->Create(subclass
);
973 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
979 "subclass \"%s\" not found for resource \"%s\", not subclassing",
988 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
990 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
992 wxObject
*returned
= DoCreateResource();
996 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
997 m_instance
= myInstance
;
1003 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
1005 m_styleNames
.Add(name
);
1006 m_styleValues
.Add(value
);
1011 void wxXmlResourceHandler::AddWindowStyles()
1013 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
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
);
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
);
1035 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
1037 return (GetParamNode(param
) != NULL
);
1041 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
1043 wxString s
= GetParamValue(param
);
1045 if (!s
) return defaults
;
1047 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
1051 while (tkn
.HasMoreTokens())
1053 fl
= tkn
.GetNextToken();
1054 index
= m_styleNames
.Index(fl
);
1055 if (index
!= wxNOT_FOUND
)
1057 style
|= m_styleValues
[index
];
1064 wxString::Format("unknown style flag \"%s\"", fl
)
1073 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
1075 wxXmlNode
*parNode
= GetParamNode(param
);
1076 wxString
str1(GetNodeContent(parNode
));
1079 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1080 const bool escapeBackslash
= (m_resource
->CompareVersion(2,5,3,0) >= 0);
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)
1088 for ( wxString::const_iterator dt
= str1
.begin(); dt
!= str1
.end(); ++dt
)
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
)
1094 if ( *(++dt
) == amp_char
)
1097 str2
<< wxT('&') << *dt
;
1099 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
1100 else if ( *dt
== wxT('\\') )
1102 switch ( (*(++dt
)).GetValue() )
1117 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1118 if ( escapeBackslash
)
1123 // else fall-through to default: branch below
1126 str2
<< wxT('\\') << *dt
;
1136 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
1138 if (translate
&& parNode
&&
1139 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
1141 return wxGetTranslation(str2
, m_resource
->GetDomain());
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
);
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.
1163 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
1166 wxString str1
= GetParamValue(param
);
1168 if (!str1
.ToLong(&value
))
1174 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
1176 wxString str
= GetParamValue(param
);
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
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
1189 if (!str
.ToDouble(&value
))
1192 return wx_truncate_cast(float, value
);
1196 int wxXmlResourceHandler::GetID()
1198 return wxXmlResource::GetXRCID(GetName());
1203 wxString
wxXmlResourceHandler::GetName()
1205 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1210 bool wxXmlResourceHandler::GetBoolAttr(const wxString
& attr
, bool defaultv
)
1213 return m_node
->GetAttribute(attr
, &v
) ? v
== '1' : defaultv
;
1216 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1218 const wxString v
= GetParamValue(param
);
1220 return v
.empty() ? defaultv
: (v
== '1');
1224 static wxColour
GetSystemColour(const wxString
& name
)
1228 #define SYSCLR(clr) \
1229 if (name == wxT(#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
)
1270 return wxNullColour
;
1273 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1275 wxString v
= GetParamValue(param
);
1282 // wxString -> wxColour conversion
1285 // the colour doesn't use #RRGGBB format, check if it is symbolic
1287 clr
= GetSystemColour(v
);
1294 wxString::Format("incorrect colour specification \"%s\"", v
)
1296 return wxNullColour
;
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
)
1312 art_id
= paramNode
->GetAttribute("stock_id", "");
1314 if ( !art_id
.empty() )
1316 art_id
= wxART_MAKE_ART_ID_FROM_STR(art_id
);
1318 art_client
= paramNode
->GetAttribute("stock_client", "");
1319 if ( art_client
.empty() )
1320 art_client
= defaultArtClient
;
1322 art_client
= wxART_MAKE_CLIENT_ID_FROM_STR(art_client
);
1331 } // anonymous namespace
1333 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1334 const wxArtClient
& defaultArtClient
,
1337 // it used to be possible to pass an empty string here to indicate that the
1338 // bitmap name should be read from this node itself but this is not
1339 // supported any more because GetBitmap(m_node) can be used directly
1341 wxASSERT_MSG( !param
.empty(), "bitmap parameter name can't be empty" );
1343 const wxXmlNode
* const node
= GetParamNode(param
);
1347 // this is not an error as bitmap parameter could be optional
1348 return wxNullBitmap
;
1351 return GetBitmap(node
, defaultArtClient
, size
);
1354 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxXmlNode
* node
,
1355 const wxArtClient
& defaultArtClient
,
1358 wxCHECK_MSG( node
, wxNullBitmap
, "bitmap node can't be NULL" );
1360 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1361 wxString art_id
, art_client
;
1362 if ( GetStockArtAttrs(node
, defaultArtClient
,
1363 art_id
, art_client
) )
1365 wxBitmap
stockArt(wxArtProvider::GetBitmap(art_id
, art_client
, size
));
1366 if ( stockArt
.Ok() )
1370 /* ...or load the bitmap from file: */
1371 wxString name
= GetParamValue(node
);
1372 if (name
.empty()) return wxNullBitmap
;
1373 #if wxUSE_FILESYSTEM
1374 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1380 wxString::Format("cannot open bitmap resource \"%s\"", name
)
1382 return wxNullBitmap
;
1384 wxImage
img(*(fsfile
->GetStream()));
1395 wxString::Format("cannot create bitmap from \"%s\"", name
)
1397 return wxNullBitmap
;
1399 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1400 return wxBitmap(img
);
1404 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1405 const wxArtClient
& defaultArtClient
,
1408 // see comment in GetBitmap(wxString) overload
1409 wxASSERT_MSG( !param
.empty(), "icon parameter name can't be empty" );
1411 const wxXmlNode
* const node
= GetParamNode(param
);
1415 // this is not an error as icon parameter could be optional
1419 return GetIcon(node
, defaultArtClient
, size
);
1422 wxIcon
wxXmlResourceHandler::GetIcon(const wxXmlNode
* node
,
1423 const wxArtClient
& defaultArtClient
,
1427 icon
.CopyFromBitmap(GetBitmap(node
, defaultArtClient
, size
));
1432 wxIconBundle
wxXmlResourceHandler::GetIconBundle(const wxString
& param
,
1433 const wxArtClient
& defaultArtClient
)
1435 wxString art_id
, art_client
;
1436 if ( GetStockArtAttrs(GetParamNode(param
), defaultArtClient
,
1437 art_id
, art_client
) )
1439 wxIconBundle
stockArt(wxArtProvider::GetIconBundle(art_id
, art_client
));
1440 if ( stockArt
.IsOk() )
1444 const wxString name
= GetParamValue(param
);
1446 return wxNullIconBundle
;
1448 #if wxUSE_FILESYSTEM
1449 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1450 if ( fsfile
== NULL
)
1455 wxString::Format("cannot open icon resource \"%s\"", name
)
1457 return wxNullIconBundle
;
1460 wxIconBundle
bundle(*(fsfile
->GetStream()));
1463 wxIconBundle
bundle(name
);
1466 if ( !bundle
.IsOk() )
1471 wxString::Format("cannot create icon from \"%s\"", name
)
1473 return wxNullIconBundle
;
1480 wxImageList
*wxXmlResourceHandler::GetImageList(const wxString
& param
)
1482 wxXmlNode
* const imagelist_node
= GetParamNode(param
);
1483 if ( !imagelist_node
)
1486 wxXmlNode
* const oldnode
= m_node
;
1487 m_node
= imagelist_node
;
1490 wxSize size
= GetSize();
1491 size
.SetDefaults(wxSize(wxSystemSettings::GetMetric(wxSYS_ICON_X
),
1492 wxSystemSettings::GetMetric(wxSYS_ICON_Y
)));
1494 // mask: true by default
1495 bool mask
= HasParam(wxT("mask")) ? GetBool(wxT("mask"), true) : true;
1497 // now we have everything we need to create the image list
1498 wxImageList
*imagelist
= new wxImageList(size
.x
, size
.y
, mask
);
1501 wxString parambitmap
= wxT("bitmap");
1502 if ( HasParam(parambitmap
) )
1504 wxXmlNode
*n
= m_node
->GetChildren();
1507 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == parambitmap
)
1509 // add icon instead of bitmap to keep the bitmap mask
1510 imagelist
->Add(GetIcon(n
));
1520 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1522 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1524 wxXmlNode
*n
= m_node
->GetChildren();
1528 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1530 // TODO: check that there are no other properties/parameters with
1531 // the same name and log an error if there are (can't do this
1532 // right now as I'm not sure if it's not going to break code
1533 // using this function in unintentional way (i.e. for
1534 // accessing other things than properties), for example
1535 // wxBitmapComboBoxXmlHandler almost surely does
1543 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1545 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1550 wxString
wxXmlResourceHandler::GetNodeContent(const wxXmlNode
*node
)
1552 const wxXmlNode
*n
= node
;
1553 if (n
== NULL
) return wxEmptyString
;
1554 n
= n
->GetChildren();
1558 if (n
->GetType() == wxXML_TEXT_NODE
||
1559 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1560 return n
->GetContent();
1563 return wxEmptyString
;
1568 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1571 return GetNodeContent(m_node
);
1573 return GetNodeContent(GetParamNode(param
));
1576 wxString
wxXmlResourceHandler::GetParamValue(const wxXmlNode
* node
)
1578 return GetNodeContent(node
);
1582 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1583 wxWindow
*windowToUse
)
1585 wxString s
= GetParamValue(param
);
1586 if (s
.empty()) s
= wxT("-1,-1");
1590 is_dlg
= s
[s
.length()-1] == wxT('d');
1591 if (is_dlg
) s
.RemoveLast();
1593 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1594 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1599 wxString::Format("cannot parse coordinates value \"%s\"", s
)
1601 return wxDefaultSize
;
1608 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1610 else if (m_parentAsWindow
)
1612 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1619 "cannot convert dialog units: dialog unknown"
1621 return wxDefaultSize
;
1625 return wxSize(sx
, sy
);
1630 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1632 wxSize sz
= GetSize(param
);
1633 return wxPoint(sz
.x
, sz
.y
);
1638 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1640 wxWindow
*windowToUse
)
1642 wxString s
= GetParamValue(param
);
1643 if (s
.empty()) return defaultv
;
1647 is_dlg
= s
[s
.length()-1] == wxT('d');
1648 if (is_dlg
) s
.RemoveLast();
1655 wxString::Format("cannot parse dimension value \"%s\"", s
)
1664 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1666 else if (m_parentAsWindow
)
1668 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1675 "cannot convert dialog units: dialog unknown"
1685 // Get system font index using indexname
1686 static wxFont
GetSystemFont(const wxString
& name
)
1690 #define SYSFNT(fnt) \
1691 if (name == wxT(#fnt)) return wxSystemSettings::GetFont(fnt);
1692 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1693 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1694 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1695 SYSFNT(wxSYS_SYSTEM_FONT
)
1696 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1697 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1698 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1705 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1707 wxXmlNode
*font_node
= GetParamNode(param
);
1708 if (font_node
== NULL
)
1711 wxString::Format("cannot find font node \"%s\"", param
));
1715 wxXmlNode
*oldnode
= m_node
;
1722 bool hasSize
= HasParam(wxT("size"));
1724 isize
= GetLong(wxT("size"), -1);
1727 int istyle
= wxNORMAL
;
1728 bool hasStyle
= HasParam(wxT("style"));
1731 wxString style
= GetParamValue(wxT("style"));
1732 if (style
== wxT("italic"))
1734 else if (style
== wxT("slant"))
1739 int iweight
= wxNORMAL
;
1740 bool hasWeight
= HasParam(wxT("weight"));
1743 wxString weight
= GetParamValue(wxT("weight"));
1744 if (weight
== wxT("bold"))
1746 else if (weight
== wxT("light"))
1751 bool hasUnderlined
= HasParam(wxT("underlined"));
1752 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1754 // family and facename
1755 int ifamily
= wxDEFAULT
;
1756 bool hasFamily
= HasParam(wxT("family"));
1759 wxString family
= GetParamValue(wxT("family"));
1760 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1761 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1762 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1763 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1764 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1765 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1770 bool hasFacename
= HasParam(wxT("face"));
1773 wxString faces
= GetParamValue(wxT("face"));
1774 wxStringTokenizer
tk(faces
, wxT(","));
1776 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1777 while (tk
.HasMoreTokens())
1779 int index
= facenames
.Index(tk
.GetNextToken(), false);
1780 if (index
!= wxNOT_FOUND
)
1782 facename
= facenames
[index
];
1786 #else // !wxUSE_FONTENUM
1787 // just use the first face name if we can't check its availability:
1788 if (tk
.HasMoreTokens())
1789 facename
= tk
.GetNextToken();
1790 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
1794 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1795 bool hasEncoding
= HasParam(wxT("encoding"));
1799 wxString encoding
= GetParamValue(wxT("encoding"));
1800 wxFontMapper mapper
;
1801 if (!encoding
.empty())
1802 enc
= mapper
.CharsetToEncoding(encoding
);
1803 if (enc
== wxFONTENCODING_SYSTEM
)
1804 enc
= wxFONTENCODING_DEFAULT
;
1806 #endif // wxUSE_FONTMAP
1808 // is this font based on a system font?
1809 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1813 if (hasSize
&& isize
!= -1)
1814 font
.SetPointSize(isize
);
1815 else if (HasParam(wxT("relativesize")))
1816 font
.SetPointSize(int(font
.GetPointSize() *
1817 GetFloat(wxT("relativesize"))));
1820 font
.SetStyle(istyle
);
1822 font
.SetWeight(iweight
);
1824 font
.SetUnderlined(underlined
);
1826 font
.SetFamily(ifamily
);
1828 font
.SetFaceName(facename
);
1830 font
.SetDefaultEncoding(enc
);
1832 else // not based on system font
1834 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1835 ifamily
, istyle
, iweight
,
1836 underlined
, facename
, enc
);
1844 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1846 //FIXME : add cursor
1848 if (HasParam(wxT("exstyle")))
1849 // Have to OR it with existing style, since
1850 // some implementations (e.g. wxGTK) use the extra style
1852 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1853 if (HasParam(wxT("bg")))
1854 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1855 if (HasParam(wxT("ownbg")))
1856 wnd
->SetOwnBackgroundColour(GetColour(wxT("ownbg")));
1857 if (HasParam(wxT("fg")))
1858 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1859 if (HasParam(wxT("ownfg")))
1860 wnd
->SetOwnForegroundColour(GetColour(wxT("ownfg")));
1861 if (GetBool(wxT("enabled"), 1) == 0)
1863 if (GetBool(wxT("focused"), 0) == 1)
1865 if (GetBool(wxT("hidden"), 0) == 1)
1868 if (HasParam(wxT("tooltip")))
1869 wnd
->SetToolTip(GetText(wxT("tooltip")));
1871 if (HasParam(wxT("font")))
1872 wnd
->SetFont(GetFont(wxT("font")));
1873 if (HasParam(wxT("ownfont")))
1874 wnd
->SetOwnFont(GetFont(wxT("ownfont")));
1875 if (HasParam(wxT("help")))
1876 wnd
->SetHelpText(GetText(wxT("help")));
1880 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1882 for ( wxXmlNode
*n
= m_node
->GetChildren(); n
; n
= n
->GetNext() )
1884 if ( IsObjectNode(n
) )
1886 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1887 this_hnd_only
? this : NULL
);
1893 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1896 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1897 wxXmlNode
*n
= root
->GetChildren();
1901 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1903 CreateResource(n
, parent
, NULL
);
1910 //-----------------------------------------------------------------------------
1912 //-----------------------------------------------------------------------------
1914 void wxXmlResourceHandler::ReportError(const wxString
& message
)
1916 m_resource
->ReportError(m_node
, message
);
1919 void wxXmlResourceHandler::ReportError(wxXmlNode
*context
,
1920 const wxString
& message
)
1922 m_resource
->ReportError(context
? context
: m_node
, message
);
1925 void wxXmlResourceHandler::ReportParamError(const wxString
& param
,
1926 const wxString
& message
)
1928 m_resource
->ReportError(GetParamNode(param
), message
);
1931 void wxXmlResource::ReportError(wxXmlNode
*context
, const wxString
& message
)
1935 DoReportError("", NULL
, message
);
1939 // We need to find out the file that 'context' is part of. Performance of
1940 // this code is not critical, so we simply find the root XML node and
1941 // compare it with all loaded XRC files.
1942 const wxString filename
= GetFileNameFromNode(context
, Data());
1944 DoReportError(filename
, context
, message
);
1947 void wxXmlResource::DoReportError(const wxString
& xrcFile
, wxXmlNode
*position
,
1948 const wxString
& message
)
1950 const int line
= position
? position
->GetLineNumber() : -1;
1953 if ( !xrcFile
.empty() )
1954 loc
= xrcFile
+ ':';
1956 loc
+= wxString::Format("%d:", line
);
1960 wxLogError("XRC error: %s%s", loc
, message
);
1964 //-----------------------------------------------------------------------------
1965 // XRCID implementation
1966 //-----------------------------------------------------------------------------
1968 #define XRCID_TABLE_SIZE 1024
1973 /* Hold the id so that once an id is allocated for a name, it
1974 does not get created again by NewControlId at least
1975 until we are done with it */
1981 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1983 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1987 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1988 index
%= XRCID_TABLE_SIZE
;
1990 XRCID_record
*oldrec
= NULL
;
1991 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1993 if (wxStrcmp(rec
->key
, str_id
) == 0)
2000 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
2001 &XRCID_Records
[index
] : &oldrec
->next
;
2002 *rec_var
= new XRCID_record
;
2003 (*rec_var
)->key
= wxStrdup(str_id
);
2004 (*rec_var
)->next
= NULL
;
2007 if (value_if_not_found
!= wxID_NONE
)
2008 (*rec_var
)->id
= value_if_not_found
;
2011 int asint
= wxStrtol(str_id
, &end
, 10);
2012 if (*str_id
&& *end
== 0)
2014 // if str_id was integer, keep it verbosely:
2015 (*rec_var
)->id
= asint
;
2019 (*rec_var
)->id
= wxWindowBase::NewControlId();
2023 return (*rec_var
)->id
;
2029 // flag indicating whether standard XRC ids were already initialized
2030 static bool gs_stdIDsAdded
= false;
2032 void AddStdXRCID_Records()
2034 #define stdID(id) XRCID_Lookup(#id, id)
2038 stdID(wxID_SEPARATOR
);
2051 stdID(wxID_PRINT_SETUP
);
2052 stdID(wxID_PAGE_SETUP
);
2053 stdID(wxID_PREVIEW
);
2055 stdID(wxID_HELP_CONTENTS
);
2056 stdID(wxID_HELP_COMMANDS
);
2057 stdID(wxID_HELP_PROCEDURES
);
2058 stdID(wxID_HELP_CONTEXT
);
2059 stdID(wxID_CLOSE_ALL
);
2060 stdID(wxID_PREFERENCES
);
2067 stdID(wxID_DUPLICATE
);
2068 stdID(wxID_SELECTALL
);
2070 stdID(wxID_REPLACE
);
2071 stdID(wxID_REPLACE_ALL
);
2072 stdID(wxID_PROPERTIES
);
2073 stdID(wxID_VIEW_DETAILS
);
2074 stdID(wxID_VIEW_LARGEICONS
);
2075 stdID(wxID_VIEW_SMALLICONS
);
2076 stdID(wxID_VIEW_LIST
);
2077 stdID(wxID_VIEW_SORTDATE
);
2078 stdID(wxID_VIEW_SORTNAME
);
2079 stdID(wxID_VIEW_SORTSIZE
);
2080 stdID(wxID_VIEW_SORTTYPE
);
2096 stdID(wxID_FORWARD
);
2097 stdID(wxID_BACKWARD
);
2098 stdID(wxID_DEFAULT
);
2102 stdID(wxID_CONTEXT_HELP
);
2103 stdID(wxID_YESTOALL
);
2104 stdID(wxID_NOTOALL
);
2113 stdID(wxID_REFRESH
);
2118 stdID(wxID_JUSTIFY_CENTER
);
2119 stdID(wxID_JUSTIFY_FILL
);
2120 stdID(wxID_JUSTIFY_RIGHT
);
2121 stdID(wxID_JUSTIFY_LEFT
);
2122 stdID(wxID_UNDERLINE
);
2124 stdID(wxID_UNINDENT
);
2125 stdID(wxID_ZOOM_100
);
2126 stdID(wxID_ZOOM_FIT
);
2127 stdID(wxID_ZOOM_IN
);
2128 stdID(wxID_ZOOM_OUT
);
2129 stdID(wxID_UNDELETE
);
2130 stdID(wxID_REVERT_TO_SAVED
);
2131 stdID(wxID_SYSTEM_MENU
);
2132 stdID(wxID_CLOSE_FRAME
);
2133 stdID(wxID_MOVE_FRAME
);
2134 stdID(wxID_RESIZE_FRAME
);
2135 stdID(wxID_MAXIMIZE_FRAME
);
2136 stdID(wxID_ICONIZE_FRAME
);
2137 stdID(wxID_RESTORE_FRAME
);
2139 stdID(wxID_CONVERT
);
2140 stdID(wxID_EXECUTE
);
2142 stdID(wxID_HARDDISK
);
2148 stdID(wxID_JUMP_TO
);
2149 stdID(wxID_NETWORK
);
2150 stdID(wxID_SELECT_COLOR
);
2151 stdID(wxID_SELECT_FONT
);
2152 stdID(wxID_SORT_ASCENDING
);
2153 stdID(wxID_SORT_DESCENDING
);
2154 stdID(wxID_SPELL_CHECK
);
2155 stdID(wxID_STRIKETHROUGH
);
2160 } // anonymous namespace
2164 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
2166 if ( !gs_stdIDsAdded
)
2168 gs_stdIDsAdded
= true;
2169 AddStdXRCID_Records();
2172 return XRCID_Lookup(str_id
, value_if_not_found
);
2176 wxString
wxXmlResource::FindXRCIDById(int numId
)
2178 for ( int i
= 0; i
< XRCID_TABLE_SIZE
; i
++ )
2180 for ( XRCID_record
*rec
= XRCID_Records
[i
]; rec
; rec
= rec
->next
)
2182 if ( rec
->id
== numId
)
2183 return wxString(rec
->key
);
2190 static void CleanXRCID_Record(XRCID_record
*rec
)
2194 CleanXRCID_Record(rec
->next
);
2201 static void CleanXRCID_Records()
2203 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
2205 CleanXRCID_Record(XRCID_Records
[i
]);
2206 XRCID_Records
[i
] = NULL
;
2209 gs_stdIDsAdded
= false;
2213 //-----------------------------------------------------------------------------
2214 // module and globals
2215 //-----------------------------------------------------------------------------
2217 // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2218 // can happen that some XRC records have been created because of the use of
2219 // XRCID() in event tables, which happens during static objects initialization,
2220 // but then the application initialization failed and so the wx modules were
2221 // neither initialized nor cleaned up -- this static object does the cleanup in
2223 static struct wxXRCStaticCleanup
2225 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2228 class wxXmlResourceModule
: public wxModule
2230 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
2232 wxXmlResourceModule() {}
2235 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
2240 delete wxXmlResource::Set(NULL
);
2241 if(wxXmlResource::ms_subclassFactories
)
2243 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
2244 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
2248 wxDELETE(wxXmlResource::ms_subclassFactories
);
2250 CleanXRCID_Records();
2254 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
2257 // When wxXml is loaded dynamically after the application is already running
2258 // then the built-in module system won't pick this one up. Add it manually.
2259 void wxXmlInitResourceModule()
2261 wxModule
* module = new wxXmlResourceModule
;
2263 wxModule::RegisterModule(module);