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
; }
435 wxXmlResource::DoLoadObject(wxWindow
*parent
,
436 const wxString
& name
,
437 const wxString
& classname
,
440 wxXmlNode
* const node
= FindResource(name
, classname
, recursive
);
442 return node
? DoCreateResFromNode(*node
, parent
, NULL
) : NULL
;
446 wxXmlResource::DoLoadObject(wxObject
*instance
,
448 const wxString
& name
,
449 const wxString
& classname
,
452 wxXmlNode
* const node
= FindResource(name
, classname
, recursive
);
454 return node
&& DoCreateResFromNode(*node
, parent
, instance
) != NULL
;
458 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
459 wxWindow
*control
, wxWindow
*parent
)
462 parent
= control
->GetParent();
463 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
466 wxLogError("Cannot find container for unknown control '%s'.", name
);
469 return control
->Reparent(container
);
473 static void ProcessPlatformProperty(wxXmlNode
*node
)
478 wxXmlNode
*c
= node
->GetChildren();
482 if (!c
->GetAttribute(wxT("platform"), &s
))
486 wxStringTokenizer
tkn(s
, wxT(" |"));
488 while (tkn
.HasMoreTokens())
490 s
= tkn
.GetNextToken();
492 if (s
== wxT("win")) isok
= true;
494 #if defined(__MAC__) || defined(__APPLE__)
495 if (s
== wxT("mac")) isok
= true;
496 #elif defined(__UNIX__)
497 if (s
== wxT("unix")) isok
= true;
500 if (s
== wxT("os2")) isok
= true;
510 ProcessPlatformProperty(c
);
515 wxXmlNode
*c2
= c
->GetNext();
516 node
->RemoveChild(c
);
525 bool wxXmlResource::UpdateResources()
529 # if wxUSE_FILESYSTEM
530 wxFSFile
*file
= NULL
;
535 wxString
encoding(wxT("UTF-8"));
536 #if !wxUSE_UNICODE && wxUSE_INTL
537 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
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();
546 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
547 i
!= Data().end(); ++i
)
549 wxXmlResourceDataRecord
* const rec
= *i
;
551 modif
= (rec
->Doc
== NULL
);
553 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
555 # if wxUSE_FILESYSTEM
556 file
= fsys
.OpenFile(rec
->File
);
558 modif
= file
&& file
->GetModificationTime() > rec
->Time
;
559 # else // wxUSE_DATETIME
561 # endif // wxUSE_DATETIME
564 wxLogError(_("Cannot open file '%s'."), rec
->File
);
569 # else // wxUSE_FILESYSTEM
571 modif
= wxDateTime(wxFileModificationTime(rec
->File
)) > rec
->Time
;
572 # else // wxUSE_DATETIME
574 # endif // wxUSE_DATETIME
575 # endif // wxUSE_FILESYSTEM
580 wxLogTrace(wxT("xrc"), wxT("opening file '%s'"), rec
->File
);
582 wxInputStream
*stream
= NULL
;
584 # if wxUSE_FILESYSTEM
585 file
= fsys
.OpenFile(rec
->File
);
587 stream
= file
->GetStream();
589 stream
= new wxFileInputStream(rec
->File
);
595 rec
->Doc
= new wxXmlDocument
;
597 if (!stream
|| !stream
->IsOk() || !rec
->Doc
->Load(*stream
, encoding
))
599 wxLogError(_("Cannot load resources from file '%s'."),
604 else if (rec
->Doc
->GetRoot()->GetName() != wxT("resource"))
609 "invalid XRC resource, doesn't have root node <resource>"
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
;
627 if (m_version
!= version
)
629 wxLogError("Resource files must have same version number.");
633 ProcessPlatformProperty(rec
->Doc
->GetRoot());
636 rec
->Time
= file
->GetModificationTime();
637 #else // wxUSE_FILESYSTEM
638 rec
->Time
= wxDateTime(wxFileModificationTime(rec
->File
));
639 #endif // wxUSE_FILESYSTEM
640 #endif // wxUSE_DATETIME
643 # if wxUSE_FILESYSTEM
655 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
656 const wxString
& name
,
657 const wxString
& classname
,
658 bool recursive
) const
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())
666 if ( IsObjectNode(node
) && node
->GetAttribute(wxS("name")) == name
)
668 // empty class name matches everything
669 if ( classname
.empty() )
672 wxString
cls(node
->GetAttribute(wxS("class")));
674 // object_ref may not have 'class' attribute:
675 if (cls
.empty() && node
->GetName() == wxS("object_ref"))
677 wxString refName
= node
->GetAttribute(wxS("ref"));
681 const wxXmlNode
* const refNode
= GetResourceNode(refName
);
683 cls
= refNode
->GetAttribute(wxS("class"));
686 if ( cls
== classname
)
691 // then recurse in child nodes
694 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
696 if ( IsObjectNode(node
) )
698 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
708 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
709 const wxString
& classname
,
714 node
= GetResourceNodeAndLocation(name
, classname
, recursive
, &path
);
723 "XRC resource \"%s\" (class \"%s\") not found",
729 else // node was found
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
);
736 #endif // wxUSE_FILESYSTEM
742 wxXmlResource::GetResourceNodeAndLocation(const wxString
& name
,
743 const wxString
& classname
,
745 wxString
*path
) const
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();
751 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
752 f
!= Data().end(); ++f
)
754 wxXmlResourceDataRecord
*const rec
= *f
;
755 wxXmlDocument
* const doc
= rec
->Doc
;
756 if ( !doc
|| !doc
->GetRoot() )
760 found
= DoFindResource(doc
->GetRoot(), name
, classname
, recursive
);
773 static void MergeNodesOver(wxXmlNode
& dest
, wxXmlNode
& overwriteWith
,
774 const wxString
& overwriteFilename
)
777 for ( wxXmlAttribute
*attr
= overwriteWith
.GetAttributes();
778 attr
; attr
= attr
->GetNext() )
780 wxXmlAttribute
*dattr
;
781 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
784 if ( dattr
->GetName() == attr
->GetName() )
786 dattr
->SetValue(attr
->GetValue());
792 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
795 // Merge child nodes:
796 for (wxXmlNode
* node
= overwriteWith
.GetChildren(); node
; node
= node
->GetNext())
798 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
801 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
803 if ( dnode
->GetName() == node
->GetName() &&
804 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
805 dnode
->GetType() == node
->GetType() )
807 MergeNodesOver(*dnode
, *node
, overwriteFilename
);
814 wxXmlNode
*copyOfNode
= new wxXmlNode(*node
);
815 // remember referenced object's file, see GetFileNameFromNode()
816 copyOfNode
->AddAttribute(ATTR_INPUT_FILENAME
, overwriteFilename
);
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
)
822 dest
.AddChild(copyOfNode
);
824 else if ( insert_pos
== wxT("begin") )
826 dest
.InsertChild(copyOfNode
, dest
.GetChildren());
831 if ( dest
.GetType() == wxXML_TEXT_NODE
&& overwriteWith
.GetContent().length() )
832 dest
.SetContent(overwriteWith
.GetContent());
836 wxXmlResource::DoCreateResFromNode(wxXmlNode
& node
,
839 wxXmlResourceHandler
*handlerToUse
)
841 // handling of referenced resource
842 if ( node
.GetName() == wxT("object_ref") )
844 wxString refName
= node
.GetAttribute(wxT("ref"), wxEmptyString
);
845 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
854 "referenced object node with ref=\"%s\" not found",
861 if ( !node
.GetChildren() )
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.
868 return DoCreateResFromNode(*refNode
, parent
, instance
);
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.
877 wxXmlNode
copy(*refNode
);
878 MergeNodesOver(copy
, node
, GetFileNameFromNode(&node
, Data()));
880 // remember referenced object's file, see GetFileNameFromNode()
881 copy
.AddAttribute(ATTR_INPUT_FILENAME
,
882 GetFileNameFromNode(refNode
, Data()));
884 return DoCreateResFromNode(copy
, parent
, instance
);
890 if (handlerToUse
->CanHandle(&node
))
892 return handlerToUse
->CreateResource(&node
, parent
, instance
);
895 else if (node
.GetName() == wxT("object"))
897 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
898 h
!= m_handlers
.end(); ++h
)
900 wxXmlResourceHandler
*handler
= *h
;
901 if (handler
->CanHandle(&node
))
902 return handler
->CreateResource(&node
, parent
, instance
);
911 "no handler found for XML node \"%s\" (class \"%s\")",
913 node
.GetAttribute("class", wxEmptyString
)
920 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
922 // this is a class so that it can be forward-declared
925 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
927 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
929 if (!ms_subclassFactories
)
931 ms_subclassFactories
= new wxXmlSubclassFactories
;
933 ms_subclassFactories
->push_back(factory
);
936 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
939 ~wxXmlSubclassFactoryCXX() {}
941 wxObject
*Create(const wxString
& className
)
943 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
946 return classInfo
->CreateObject();
955 wxXmlResourceHandler::wxXmlResourceHandler()
956 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
957 m_parentAsWindow(NULL
)
962 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
964 wxXmlNode
*myNode
= m_node
;
965 wxString myClass
= m_class
;
966 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
967 wxWindow
*myParentAW
= m_parentAsWindow
;
969 m_instance
= instance
;
970 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
971 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
973 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
974 if (!subclass
.empty())
976 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
977 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
979 m_instance
= (*i
)->Create(subclass
);
986 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
992 "subclass \"%s\" not found for resource \"%s\", not subclassing",
1001 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
1003 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
1005 wxObject
*returned
= DoCreateResource();
1009 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
1010 m_instance
= myInstance
;
1016 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
1018 m_styleNames
.Add(name
);
1019 m_styleValues
.Add(value
);
1024 void wxXmlResourceHandler::AddWindowStyles()
1026 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
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
); // deprecated
1032 XRC_ADD_STYLE(wxBORDER_THEME
);
1033 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
1034 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
1035 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
1037 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
1038 XRC_ADD_STYLE(wxWANTS_CHARS
);
1039 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
1040 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
1041 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
1042 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
1043 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
1044 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
1049 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
1051 return (GetParamNode(param
) != NULL
);
1055 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
1057 wxString s
= GetParamValue(param
);
1059 if (!s
) return defaults
;
1061 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
1065 while (tkn
.HasMoreTokens())
1067 fl
= tkn
.GetNextToken();
1068 index
= m_styleNames
.Index(fl
);
1069 if (index
!= wxNOT_FOUND
)
1071 style
|= m_styleValues
[index
];
1078 wxString::Format("unknown style flag \"%s\"", fl
)
1087 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
1089 wxXmlNode
*parNode
= GetParamNode(param
);
1090 wxString
str1(GetNodeContent(parNode
));
1093 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1094 const bool escapeBackslash
= (m_resource
->CompareVersion(2,5,3,0) >= 0);
1096 // VS: First version of XRC resources used $ instead of & (which is
1097 // illegal in XML), but later I realized that '_' fits this purpose
1098 // much better (because &File means "File with F underlined").
1099 const wxChar amp_char
= (m_resource
->CompareVersion(2,3,0,1) < 0)
1102 for ( wxString::const_iterator dt
= str1
.begin(); dt
!= str1
.end(); ++dt
)
1104 // Remap amp_char to &, map double amp_char to amp_char (for things
1105 // like "&File..." -- this is illegal in XML, so we use "_File..."):
1106 if ( *dt
== amp_char
)
1108 if ( *(++dt
) == amp_char
)
1111 str2
<< wxT('&') << *dt
;
1113 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
1114 else if ( *dt
== wxT('\\') )
1116 switch ( (*(++dt
)).GetValue() )
1131 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1132 if ( escapeBackslash
)
1137 // else fall-through to default: branch below
1140 str2
<< wxT('\\') << *dt
;
1150 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
1152 if (translate
&& parNode
&&
1153 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
1155 return wxGetTranslation(str2
, m_resource
->GetDomain());
1162 // The string is internally stored as UTF-8, we have to convert
1163 // it into system's default encoding so that it can be displayed:
1164 return wxString(str2
.wc_str(wxConvUTF8
), wxConvLocal
);
1169 // If wxXRC_USE_LOCALE is not set, then the string is already in
1170 // system's default encoding in ANSI build, so we don't have to
1171 // do anything special here.
1177 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
1180 wxString str1
= GetParamValue(param
);
1182 if (!str1
.ToLong(&value
))
1188 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
1190 wxString str
= GetParamValue(param
);
1193 // strings in XRC always use C locale but wxString::ToDouble() uses the
1194 // current one, so transform the string to it supposing that the only
1195 // difference between them is the decimal separator
1197 // TODO: use wxString::ToCDouble() when we have it
1198 str
.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1199 wxLOCALE_CAT_NUMBER
));
1200 #endif // wxUSE_INTL
1203 if (!str
.ToDouble(&value
))
1206 return wx_truncate_cast(float, value
);
1210 int wxXmlResourceHandler::GetID()
1212 return wxXmlResource::GetXRCID(GetName());
1217 wxString
wxXmlResourceHandler::GetName()
1219 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1224 bool wxXmlResourceHandler::GetBoolAttr(const wxString
& attr
, bool defaultv
)
1227 return m_node
->GetAttribute(attr
, &v
) ? v
== '1' : defaultv
;
1230 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1232 const wxString v
= GetParamValue(param
);
1234 return v
.empty() ? defaultv
: (v
== '1');
1238 static wxColour
GetSystemColour(const wxString
& name
)
1242 #define SYSCLR(clr) \
1243 if (name == wxT(#clr)) return wxSystemSettings::GetColour(clr);
1244 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1245 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1246 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1247 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1248 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1249 SYSCLR(wxSYS_COLOUR_MENU
)
1250 SYSCLR(wxSYS_COLOUR_WINDOW
)
1251 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1252 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1253 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1254 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1255 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1256 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1257 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1258 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1259 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1260 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1261 SYSCLR(wxSYS_COLOUR_3DFACE
)
1262 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1263 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1264 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1265 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1266 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1267 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1268 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1269 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1270 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1271 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1272 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1273 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1274 SYSCLR(wxSYS_COLOUR_INFOBK
)
1275 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1276 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1277 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1278 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1279 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1280 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1284 return wxNullColour
;
1287 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1289 wxString v
= GetParamValue(param
);
1296 // wxString -> wxColour conversion
1299 // the colour doesn't use #RRGGBB format, check if it is symbolic
1301 clr
= GetSystemColour(v
);
1308 wxString::Format("incorrect colour specification \"%s\"", v
)
1310 return wxNullColour
;
1319 // if 'param' has stock_id/stock_client, extracts them and returns true
1320 bool GetStockArtAttrs(const wxXmlNode
*paramNode
,
1321 const wxString
& defaultArtClient
,
1322 wxString
& art_id
, wxString
& art_client
)
1326 art_id
= paramNode
->GetAttribute("stock_id", "");
1328 if ( !art_id
.empty() )
1330 art_id
= wxART_MAKE_ART_ID_FROM_STR(art_id
);
1332 art_client
= paramNode
->GetAttribute("stock_client", "");
1333 if ( art_client
.empty() )
1334 art_client
= defaultArtClient
;
1336 art_client
= wxART_MAKE_CLIENT_ID_FROM_STR(art_client
);
1345 } // anonymous namespace
1347 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1348 const wxArtClient
& defaultArtClient
,
1351 // it used to be possible to pass an empty string here to indicate that the
1352 // bitmap name should be read from this node itself but this is not
1353 // supported any more because GetBitmap(m_node) can be used directly
1355 wxASSERT_MSG( !param
.empty(), "bitmap parameter name can't be empty" );
1357 const wxXmlNode
* const node
= GetParamNode(param
);
1361 // this is not an error as bitmap parameter could be optional
1362 return wxNullBitmap
;
1365 return GetBitmap(node
, defaultArtClient
, size
);
1368 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxXmlNode
* node
,
1369 const wxArtClient
& defaultArtClient
,
1372 wxCHECK_MSG( node
, wxNullBitmap
, "bitmap node can't be NULL" );
1374 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1375 wxString art_id
, art_client
;
1376 if ( GetStockArtAttrs(node
, defaultArtClient
,
1377 art_id
, art_client
) )
1379 wxBitmap
stockArt(wxArtProvider::GetBitmap(art_id
, art_client
, size
));
1380 if ( stockArt
.Ok() )
1384 /* ...or load the bitmap from file: */
1385 wxString name
= GetParamValue(node
);
1386 if (name
.empty()) return wxNullBitmap
;
1387 #if wxUSE_FILESYSTEM
1388 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1394 wxString::Format("cannot open bitmap resource \"%s\"", name
)
1396 return wxNullBitmap
;
1398 wxImage
img(*(fsfile
->GetStream()));
1409 wxString::Format("cannot create bitmap from \"%s\"", name
)
1411 return wxNullBitmap
;
1413 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1414 return wxBitmap(img
);
1418 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1419 const wxArtClient
& defaultArtClient
,
1422 // see comment in GetBitmap(wxString) overload
1423 wxASSERT_MSG( !param
.empty(), "icon parameter name can't be empty" );
1425 const wxXmlNode
* const node
= GetParamNode(param
);
1429 // this is not an error as icon parameter could be optional
1433 return GetIcon(node
, defaultArtClient
, size
);
1436 wxIcon
wxXmlResourceHandler::GetIcon(const wxXmlNode
* node
,
1437 const wxArtClient
& defaultArtClient
,
1441 icon
.CopyFromBitmap(GetBitmap(node
, defaultArtClient
, size
));
1446 wxIconBundle
wxXmlResourceHandler::GetIconBundle(const wxString
& param
,
1447 const wxArtClient
& defaultArtClient
)
1449 wxString art_id
, art_client
;
1450 if ( GetStockArtAttrs(GetParamNode(param
), defaultArtClient
,
1451 art_id
, art_client
) )
1453 wxIconBundle
stockArt(wxArtProvider::GetIconBundle(art_id
, art_client
));
1454 if ( stockArt
.IsOk() )
1458 const wxString name
= GetParamValue(param
);
1460 return wxNullIconBundle
;
1462 #if wxUSE_FILESYSTEM
1463 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1464 if ( fsfile
== NULL
)
1469 wxString::Format("cannot open icon resource \"%s\"", name
)
1471 return wxNullIconBundle
;
1474 wxIconBundle
bundle(*(fsfile
->GetStream()));
1477 wxIconBundle
bundle(name
);
1480 if ( !bundle
.IsOk() )
1485 wxString::Format("cannot create icon from \"%s\"", name
)
1487 return wxNullIconBundle
;
1494 wxImageList
*wxXmlResourceHandler::GetImageList(const wxString
& param
)
1496 wxXmlNode
* const imagelist_node
= GetParamNode(param
);
1497 if ( !imagelist_node
)
1500 wxXmlNode
* const oldnode
= m_node
;
1501 m_node
= imagelist_node
;
1504 wxSize size
= GetSize();
1505 size
.SetDefaults(wxSize(wxSystemSettings::GetMetric(wxSYS_ICON_X
),
1506 wxSystemSettings::GetMetric(wxSYS_ICON_Y
)));
1508 // mask: true by default
1509 bool mask
= HasParam(wxT("mask")) ? GetBool(wxT("mask"), true) : true;
1511 // now we have everything we need to create the image list
1512 wxImageList
*imagelist
= new wxImageList(size
.x
, size
.y
, mask
);
1515 wxString parambitmap
= wxT("bitmap");
1516 if ( HasParam(parambitmap
) )
1518 wxXmlNode
*n
= m_node
->GetChildren();
1521 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == parambitmap
)
1523 // add icon instead of bitmap to keep the bitmap mask
1524 imagelist
->Add(GetIcon(n
));
1534 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1536 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1538 wxXmlNode
*n
= m_node
->GetChildren();
1542 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1544 // TODO: check that there are no other properties/parameters with
1545 // the same name and log an error if there are (can't do this
1546 // right now as I'm not sure if it's not going to break code
1547 // using this function in unintentional way (i.e. for
1548 // accessing other things than properties), for example
1549 // wxBitmapComboBoxXmlHandler almost surely does
1557 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1559 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1564 wxString
wxXmlResourceHandler::GetNodeContent(const wxXmlNode
*node
)
1566 const wxXmlNode
*n
= node
;
1567 if (n
== NULL
) return wxEmptyString
;
1568 n
= n
->GetChildren();
1572 if (n
->GetType() == wxXML_TEXT_NODE
||
1573 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1574 return n
->GetContent();
1577 return wxEmptyString
;
1582 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1585 return GetNodeContent(m_node
);
1587 return GetNodeContent(GetParamNode(param
));
1590 wxString
wxXmlResourceHandler::GetParamValue(const wxXmlNode
* node
)
1592 return GetNodeContent(node
);
1596 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1597 wxWindow
*windowToUse
)
1599 wxString s
= GetParamValue(param
);
1600 if (s
.empty()) s
= wxT("-1,-1");
1604 is_dlg
= s
[s
.length()-1] == wxT('d');
1605 if (is_dlg
) s
.RemoveLast();
1607 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1608 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1613 wxString::Format("cannot parse coordinates value \"%s\"", s
)
1615 return wxDefaultSize
;
1622 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1624 else if (m_parentAsWindow
)
1626 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1633 "cannot convert dialog units: dialog unknown"
1635 return wxDefaultSize
;
1639 return wxSize(sx
, sy
);
1644 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1646 wxSize sz
= GetSize(param
);
1647 return wxPoint(sz
.x
, sz
.y
);
1652 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1654 wxWindow
*windowToUse
)
1656 wxString s
= GetParamValue(param
);
1657 if (s
.empty()) return defaultv
;
1661 is_dlg
= s
[s
.length()-1] == wxT('d');
1662 if (is_dlg
) s
.RemoveLast();
1669 wxString::Format("cannot parse dimension value \"%s\"", s
)
1678 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1680 else if (m_parentAsWindow
)
1682 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1689 "cannot convert dialog units: dialog unknown"
1699 // Get system font index using indexname
1700 static wxFont
GetSystemFont(const wxString
& name
)
1704 #define SYSFNT(fnt) \
1705 if (name == wxT(#fnt)) return wxSystemSettings::GetFont(fnt);
1706 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1707 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1708 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1709 SYSFNT(wxSYS_SYSTEM_FONT
)
1710 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1711 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1712 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1719 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1721 wxXmlNode
*font_node
= GetParamNode(param
);
1722 if (font_node
== NULL
)
1725 wxString::Format("cannot find font node \"%s\"", param
));
1729 wxXmlNode
*oldnode
= m_node
;
1736 bool hasSize
= HasParam(wxT("size"));
1738 isize
= GetLong(wxT("size"), -1);
1741 int istyle
= wxNORMAL
;
1742 bool hasStyle
= HasParam(wxT("style"));
1745 wxString style
= GetParamValue(wxT("style"));
1746 if (style
== wxT("italic"))
1748 else if (style
== wxT("slant"))
1753 int iweight
= wxNORMAL
;
1754 bool hasWeight
= HasParam(wxT("weight"));
1757 wxString weight
= GetParamValue(wxT("weight"));
1758 if (weight
== wxT("bold"))
1760 else if (weight
== wxT("light"))
1765 bool hasUnderlined
= HasParam(wxT("underlined"));
1766 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1768 // family and facename
1769 int ifamily
= wxDEFAULT
;
1770 bool hasFamily
= HasParam(wxT("family"));
1773 wxString family
= GetParamValue(wxT("family"));
1774 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1775 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1776 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1777 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1778 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1779 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1784 bool hasFacename
= HasParam(wxT("face"));
1787 wxString faces
= GetParamValue(wxT("face"));
1788 wxStringTokenizer
tk(faces
, wxT(","));
1790 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1791 while (tk
.HasMoreTokens())
1793 int index
= facenames
.Index(tk
.GetNextToken(), false);
1794 if (index
!= wxNOT_FOUND
)
1796 facename
= facenames
[index
];
1800 #else // !wxUSE_FONTENUM
1801 // just use the first face name if we can't check its availability:
1802 if (tk
.HasMoreTokens())
1803 facename
= tk
.GetNextToken();
1804 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
1808 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1809 bool hasEncoding
= HasParam(wxT("encoding"));
1813 wxString encoding
= GetParamValue(wxT("encoding"));
1814 wxFontMapper mapper
;
1815 if (!encoding
.empty())
1816 enc
= mapper
.CharsetToEncoding(encoding
);
1817 if (enc
== wxFONTENCODING_SYSTEM
)
1818 enc
= wxFONTENCODING_DEFAULT
;
1820 #endif // wxUSE_FONTMAP
1822 // is this font based on a system font?
1823 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1827 if (hasSize
&& isize
!= -1)
1828 font
.SetPointSize(isize
);
1829 else if (HasParam(wxT("relativesize")))
1830 font
.SetPointSize(int(font
.GetPointSize() *
1831 GetFloat(wxT("relativesize"))));
1834 font
.SetStyle(istyle
);
1836 font
.SetWeight(iweight
);
1838 font
.SetUnderlined(underlined
);
1840 font
.SetFamily(ifamily
);
1842 font
.SetFaceName(facename
);
1844 font
.SetDefaultEncoding(enc
);
1846 else // not based on system font
1848 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1849 ifamily
, istyle
, iweight
,
1850 underlined
, facename
, enc
);
1858 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1860 //FIXME : add cursor
1862 if (HasParam(wxT("exstyle")))
1863 // Have to OR it with existing style, since
1864 // some implementations (e.g. wxGTK) use the extra style
1866 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1867 if (HasParam(wxT("bg")))
1868 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1869 if (HasParam(wxT("ownbg")))
1870 wnd
->SetOwnBackgroundColour(GetColour(wxT("ownbg")));
1871 if (HasParam(wxT("fg")))
1872 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1873 if (HasParam(wxT("ownfg")))
1874 wnd
->SetOwnForegroundColour(GetColour(wxT("ownfg")));
1875 if (GetBool(wxT("enabled"), 1) == 0)
1877 if (GetBool(wxT("focused"), 0) == 1)
1879 if (GetBool(wxT("hidden"), 0) == 1)
1882 if (HasParam(wxT("tooltip")))
1883 wnd
->SetToolTip(GetText(wxT("tooltip")));
1885 if (HasParam(wxT("font")))
1886 wnd
->SetFont(GetFont(wxT("font")));
1887 if (HasParam(wxT("ownfont")))
1888 wnd
->SetOwnFont(GetFont(wxT("ownfont")));
1889 if (HasParam(wxT("help")))
1890 wnd
->SetHelpText(GetText(wxT("help")));
1894 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1896 for ( wxXmlNode
*n
= m_node
->GetChildren(); n
; n
= n
->GetNext() )
1898 if ( IsObjectNode(n
) )
1900 m_resource
->DoCreateResFromNode(*n
, parent
, NULL
,
1901 this_hnd_only
? this : NULL
);
1907 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1910 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1911 wxXmlNode
*n
= root
->GetChildren();
1915 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1917 CreateResource(n
, parent
, NULL
);
1924 //-----------------------------------------------------------------------------
1926 //-----------------------------------------------------------------------------
1928 void wxXmlResourceHandler::ReportError(const wxString
& message
)
1930 m_resource
->ReportError(m_node
, message
);
1933 void wxXmlResourceHandler::ReportError(wxXmlNode
*context
,
1934 const wxString
& message
)
1936 m_resource
->ReportError(context
? context
: m_node
, message
);
1939 void wxXmlResourceHandler::ReportParamError(const wxString
& param
,
1940 const wxString
& message
)
1942 m_resource
->ReportError(GetParamNode(param
), message
);
1945 void wxXmlResource::ReportError(wxXmlNode
*context
, const wxString
& message
)
1949 DoReportError("", NULL
, message
);
1953 // We need to find out the file that 'context' is part of. Performance of
1954 // this code is not critical, so we simply find the root XML node and
1955 // compare it with all loaded XRC files.
1956 const wxString filename
= GetFileNameFromNode(context
, Data());
1958 DoReportError(filename
, context
, message
);
1961 void wxXmlResource::DoReportError(const wxString
& xrcFile
, wxXmlNode
*position
,
1962 const wxString
& message
)
1964 const int line
= position
? position
->GetLineNumber() : -1;
1967 if ( !xrcFile
.empty() )
1968 loc
= xrcFile
+ ':';
1970 loc
+= wxString::Format("%d:", line
);
1974 wxLogError("XRC error: %s%s", loc
, message
);
1978 //-----------------------------------------------------------------------------
1979 // XRCID implementation
1980 //-----------------------------------------------------------------------------
1982 #define XRCID_TABLE_SIZE 1024
1987 /* Hold the id so that once an id is allocated for a name, it
1988 does not get created again by NewControlId at least
1989 until we are done with it */
1995 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1997 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
2001 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
2002 index
%= XRCID_TABLE_SIZE
;
2004 XRCID_record
*oldrec
= NULL
;
2005 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
2007 if (wxStrcmp(rec
->key
, str_id
) == 0)
2014 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
2015 &XRCID_Records
[index
] : &oldrec
->next
;
2016 *rec_var
= new XRCID_record
;
2017 (*rec_var
)->key
= wxStrdup(str_id
);
2018 (*rec_var
)->next
= NULL
;
2021 if (value_if_not_found
!= wxID_NONE
)
2022 (*rec_var
)->id
= value_if_not_found
;
2025 int asint
= wxStrtol(str_id
, &end
, 10);
2026 if (*str_id
&& *end
== 0)
2028 // if str_id was integer, keep it verbosely:
2029 (*rec_var
)->id
= asint
;
2033 (*rec_var
)->id
= wxWindowBase::NewControlId();
2037 return (*rec_var
)->id
;
2043 // flag indicating whether standard XRC ids were already initialized
2044 static bool gs_stdIDsAdded
= false;
2046 void AddStdXRCID_Records()
2048 #define stdID(id) XRCID_Lookup(#id, id)
2052 stdID(wxID_SEPARATOR
);
2065 stdID(wxID_PRINT_SETUP
);
2066 stdID(wxID_PAGE_SETUP
);
2067 stdID(wxID_PREVIEW
);
2069 stdID(wxID_HELP_CONTENTS
);
2070 stdID(wxID_HELP_COMMANDS
);
2071 stdID(wxID_HELP_PROCEDURES
);
2072 stdID(wxID_HELP_CONTEXT
);
2073 stdID(wxID_CLOSE_ALL
);
2074 stdID(wxID_PREFERENCES
);
2081 stdID(wxID_DUPLICATE
);
2082 stdID(wxID_SELECTALL
);
2084 stdID(wxID_REPLACE
);
2085 stdID(wxID_REPLACE_ALL
);
2086 stdID(wxID_PROPERTIES
);
2087 stdID(wxID_VIEW_DETAILS
);
2088 stdID(wxID_VIEW_LARGEICONS
);
2089 stdID(wxID_VIEW_SMALLICONS
);
2090 stdID(wxID_VIEW_LIST
);
2091 stdID(wxID_VIEW_SORTDATE
);
2092 stdID(wxID_VIEW_SORTNAME
);
2093 stdID(wxID_VIEW_SORTSIZE
);
2094 stdID(wxID_VIEW_SORTTYPE
);
2110 stdID(wxID_FORWARD
);
2111 stdID(wxID_BACKWARD
);
2112 stdID(wxID_DEFAULT
);
2116 stdID(wxID_CONTEXT_HELP
);
2117 stdID(wxID_YESTOALL
);
2118 stdID(wxID_NOTOALL
);
2127 stdID(wxID_REFRESH
);
2132 stdID(wxID_JUSTIFY_CENTER
);
2133 stdID(wxID_JUSTIFY_FILL
);
2134 stdID(wxID_JUSTIFY_RIGHT
);
2135 stdID(wxID_JUSTIFY_LEFT
);
2136 stdID(wxID_UNDERLINE
);
2138 stdID(wxID_UNINDENT
);
2139 stdID(wxID_ZOOM_100
);
2140 stdID(wxID_ZOOM_FIT
);
2141 stdID(wxID_ZOOM_IN
);
2142 stdID(wxID_ZOOM_OUT
);
2143 stdID(wxID_UNDELETE
);
2144 stdID(wxID_REVERT_TO_SAVED
);
2145 stdID(wxID_SYSTEM_MENU
);
2146 stdID(wxID_CLOSE_FRAME
);
2147 stdID(wxID_MOVE_FRAME
);
2148 stdID(wxID_RESIZE_FRAME
);
2149 stdID(wxID_MAXIMIZE_FRAME
);
2150 stdID(wxID_ICONIZE_FRAME
);
2151 stdID(wxID_RESTORE_FRAME
);
2153 stdID(wxID_CONVERT
);
2154 stdID(wxID_EXECUTE
);
2156 stdID(wxID_HARDDISK
);
2162 stdID(wxID_JUMP_TO
);
2163 stdID(wxID_NETWORK
);
2164 stdID(wxID_SELECT_COLOR
);
2165 stdID(wxID_SELECT_FONT
);
2166 stdID(wxID_SORT_ASCENDING
);
2167 stdID(wxID_SORT_DESCENDING
);
2168 stdID(wxID_SPELL_CHECK
);
2169 stdID(wxID_STRIKETHROUGH
);
2174 } // anonymous namespace
2178 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
2180 if ( !gs_stdIDsAdded
)
2182 gs_stdIDsAdded
= true;
2183 AddStdXRCID_Records();
2186 return XRCID_Lookup(str_id
, value_if_not_found
);
2190 wxString
wxXmlResource::FindXRCIDById(int numId
)
2192 for ( int i
= 0; i
< XRCID_TABLE_SIZE
; i
++ )
2194 for ( XRCID_record
*rec
= XRCID_Records
[i
]; rec
; rec
= rec
->next
)
2196 if ( rec
->id
== numId
)
2197 return wxString(rec
->key
);
2204 static void CleanXRCID_Record(XRCID_record
*rec
)
2208 CleanXRCID_Record(rec
->next
);
2215 static void CleanXRCID_Records()
2217 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
2219 CleanXRCID_Record(XRCID_Records
[i
]);
2220 XRCID_Records
[i
] = NULL
;
2223 gs_stdIDsAdded
= false;
2227 //-----------------------------------------------------------------------------
2228 // module and globals
2229 //-----------------------------------------------------------------------------
2231 // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2232 // can happen that some XRC records have been created because of the use of
2233 // XRCID() in event tables, which happens during static objects initialization,
2234 // but then the application initialization failed and so the wx modules were
2235 // neither initialized nor cleaned up -- this static object does the cleanup in
2237 static struct wxXRCStaticCleanup
2239 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2242 class wxXmlResourceModule
: public wxModule
2244 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
2246 wxXmlResourceModule() {}
2249 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
2254 delete wxXmlResource::Set(NULL
);
2255 if(wxXmlResource::ms_subclassFactories
)
2257 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
2258 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
2262 wxDELETE(wxXmlResource::ms_subclassFactories
);
2264 CleanXRCID_Records();
2268 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
2271 // When wxXml is loaded dynamically after the application is already running
2272 // then the built-in module system won't pick this one up. Add it manually.
2273 void wxXmlInitResourceModule()
2275 wxModule
* module = new wxXmlResourceModule
;
2277 wxModule::RegisterModule(module);