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"
48 #include "wx/xml/xml.h"
51 class wxXmlResourceDataRecord
54 wxXmlResourceDataRecord() : Doc(NULL
) {
56 Time
= wxDateTime::Now();
59 ~wxXmlResourceDataRecord() {delete Doc
;}
68 class wxXmlResourceDataRecords
: public wxVector
<wxXmlResourceDataRecord
*>
70 // this is a class so that it can be forward-declared
76 // helper used by DoFindResource() and elsewhere: returns true if this is an
77 // object or object_ref node
79 // node must be non-NULL
80 inline bool IsObjectNode(wxXmlNode
*node
)
82 return node
->GetType() == wxXML_ELEMENT_NODE
&&
83 (node
->GetName() == wxS("object") ||
84 node
->GetName() == wxS("object_ref"));
87 // special XML attribute with name of input file, see GetFileNameFromNode()
88 const char *ATTR_INPUT_FILENAME
= "__wx:filename";
90 // helper to get filename corresponding to an XML node
92 GetFileNameFromNode(wxXmlNode
*node
, const wxXmlResourceDataRecords
& files
)
94 // this loop does two things: it looks for ATTR_INPUT_FILENAME among
95 // parents and if it isn't used, it finds the root of the XML tree 'node'
99 // in some rare cases (specifically, when an <object_ref> is used, see
100 // wxXmlResource::CreateResFromNode() and MergeNodesOver()), we work
101 // with XML nodes that are not rooted in any document from 'files'
102 // (because a new node was created by CreateResFromNode() to merge the
103 // content of <object_ref> and the referenced <object>); in that case,
104 // we hack around the problem by putting the information about input
105 // file into a custom attribute
106 if ( node
->HasAttribute(ATTR_INPUT_FILENAME
) )
107 return node
->GetAttribute(ATTR_INPUT_FILENAME
);
109 if ( !node
->GetParent() )
110 break; // we found the root of this XML tree
112 node
= node
->GetParent();
115 // NB: 'node' now points to the root of XML document
117 for ( wxXmlResourceDataRecords::const_iterator i
= files
.begin();
118 i
!= files
.end(); ++i
)
120 if ( (*i
)->Doc
->GetRoot() == node
)
126 return wxEmptyString
; // not found
129 } // anonymous namespace
132 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
134 /*static*/ wxXmlResource
*wxXmlResource::Get()
137 ms_instance
= new wxXmlResource
;
141 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
143 wxXmlResource
*old
= ms_instance
;
148 wxXmlResource::wxXmlResource(int flags
, const wxString
& domain
)
152 m_data
= new wxXmlResourceDataRecords
;
156 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
160 m_data
= new wxXmlResourceDataRecords
;
165 wxXmlResource::~wxXmlResource()
169 for ( wxXmlResourceDataRecords::iterator i
= m_data
->begin();
170 i
!= m_data
->end(); ++i
)
177 void wxXmlResource::SetDomain(const wxString
& domain
)
184 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
186 wxString
fnd(filename
);
188 // NB: as Load() and Unload() accept both filenames and URLs (should
189 // probably be changed to filenames only, but embedded resources
190 // currently rely on its ability to handle URLs - FIXME) we need to
191 // determine whether found name is filename and not URL and this is the
192 // fastest/simplest way to do it
193 if (wxFileName::FileExists(fnd
))
195 // Make the name absolute filename, because the app may
196 // change working directory later:
201 fnd
= fn
.GetFullPath();
204 fnd
= wxFileSystem::FileNameToURL(fnd
);
214 bool wxXmlResource::IsArchive(const wxString
& filename
)
216 const wxString fnd
= filename
.Lower();
218 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
221 #endif // wxUSE_FILESYSTEM
223 bool wxXmlResource::LoadFile(const wxFileName
& file
)
226 return Load(wxFileSystem::FileNameToURL(file
));
228 return Load(file
.GetFullPath());
232 bool wxXmlResource::LoadAllFiles(const wxString
& dirname
)
237 wxDir::GetAllFiles(dirname
, &files
, "*.xrc");
239 for ( wxArrayString::const_iterator i
= files
.begin(); i
!= files
.end(); ++i
)
248 bool wxXmlResource::Load(const wxString
& filemask_
)
250 wxString filemask
= ConvertFileNameToURL(filemask_
);
254 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
255 # define wxXmlFindNext fsys.FindNext()
257 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
258 # define wxXmlFindNext wxFindNextFile()
260 wxString fnd
= wxXmlFindFirst
;
263 wxLogError(_("Cannot load resources from '%s'."), filemask
);
270 if ( IsArchive(fnd
) )
272 if ( !Load(fnd
+ wxT("#zip:*.xrc")) )
275 else // a single resource URL
276 #endif // wxUSE_FILESYSTEM
278 wxXmlResourceDataRecord
*drec
= new wxXmlResourceDataRecord
;
280 Data().push_back(drec
);
285 # undef wxXmlFindFirst
286 # undef wxXmlFindNext
288 return UpdateResources();
291 bool wxXmlResource::Unload(const wxString
& filename
)
293 wxASSERT_MSG( !wxIsWild(filename
),
294 _T("wildcards not supported by wxXmlResource::Unload()") );
296 wxString fnd
= ConvertFileNameToURL(filename
);
298 const bool isArchive
= IsArchive(fnd
);
301 #endif // wxUSE_FILESYSTEM
303 bool unloaded
= false;
304 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
305 i
!= Data().end(); ++i
)
310 if ( (*i
)->File
.StartsWith(fnd
) )
312 // don't break from the loop, we can have other matching files
314 else // a single resource URL
315 #endif // wxUSE_FILESYSTEM
317 if ( (*i
)->File
== fnd
)
323 // no sense in continuing, there is only one file with this URL
333 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
335 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
337 m_handlers
.push_back(handler
);
338 handler
->SetParentResource(this);
341 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
343 m_handlers
.insert(m_handlers
.begin(), handler
);
344 handler
->SetParentResource(this);
349 void wxXmlResource::ClearHandlers()
351 for ( wxVector
<wxXmlResourceHandler
*>::iterator i
= m_handlers
.begin();
352 i
!= m_handlers
.end(); ++i
)
358 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
360 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
365 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
367 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
373 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
375 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
380 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
382 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
385 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
387 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
392 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
394 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
397 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
399 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
402 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
404 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
407 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
409 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
412 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
414 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
415 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
418 if (bmp
) { rt
= *bmp
; delete bmp
; }
422 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
424 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
425 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
428 if (icon
) { rt
= *icon
; delete icon
; }
433 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
435 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
438 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
440 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
444 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
445 wxWindow
*control
, wxWindow
*parent
)
448 parent
= control
->GetParent();
449 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
452 wxLogError("Cannot find container for unknown control '%s'.", name
);
455 return control
->Reparent(container
);
459 static void ProcessPlatformProperty(wxXmlNode
*node
)
464 wxXmlNode
*c
= node
->GetChildren();
468 if (!c
->GetAttribute(wxT("platform"), &s
))
472 wxStringTokenizer
tkn(s
, wxT(" |"));
474 while (tkn
.HasMoreTokens())
476 s
= tkn
.GetNextToken();
478 if (s
== wxT("win")) isok
= true;
480 #if defined(__MAC__) || defined(__APPLE__)
481 if (s
== wxT("mac")) isok
= true;
482 #elif defined(__UNIX__)
483 if (s
== wxT("unix")) isok
= true;
486 if (s
== wxT("os2")) isok
= true;
496 ProcessPlatformProperty(c
);
501 wxXmlNode
*c2
= c
->GetNext();
502 node
->RemoveChild(c
);
511 bool wxXmlResource::UpdateResources()
515 # if wxUSE_FILESYSTEM
516 wxFSFile
*file
= NULL
;
521 wxString
encoding(wxT("UTF-8"));
522 #if !wxUSE_UNICODE && wxUSE_INTL
523 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
525 // In case we are not using wxLocale to translate strings, convert the
526 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
527 // is on, because it could break wxGetTranslation lookup.
528 encoding
= wxLocale::GetSystemEncodingName();
532 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
533 i
!= Data().end(); ++i
)
535 wxXmlResourceDataRecord
* const rec
= *i
;
537 modif
= (rec
->Doc
== NULL
);
539 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
541 # if wxUSE_FILESYSTEM
542 file
= fsys
.OpenFile(rec
->File
);
544 modif
= file
&& file
->GetModificationTime() > rec
->Time
;
545 # else // wxUSE_DATETIME
547 # endif // wxUSE_DATETIME
550 wxLogError(_("Cannot open file '%s'."), rec
->File
);
555 # else // wxUSE_FILESYSTEM
557 modif
= wxDateTime(wxFileModificationTime(rec
->File
)) > rec
->Time
;
558 # else // wxUSE_DATETIME
560 # endif // wxUSE_DATETIME
561 # endif // wxUSE_FILESYSTEM
566 wxLogTrace(_T("xrc"), _T("opening file '%s'"), rec
->File
);
568 wxInputStream
*stream
= NULL
;
570 # if wxUSE_FILESYSTEM
571 file
= fsys
.OpenFile(rec
->File
);
573 stream
= file
->GetStream();
575 stream
= new wxFileInputStream(rec
->File
);
581 rec
->Doc
= new wxXmlDocument
;
583 if (!stream
|| !stream
->IsOk() || !rec
->Doc
->Load(*stream
, encoding
))
585 wxLogError(_("Cannot load resources from file '%s'."),
590 else if (rec
->Doc
->GetRoot()->GetName() != wxT("resource"))
595 "invalid XRC resource, doesn't have root node <resource>"
604 wxString verstr
= rec
->Doc
->GetRoot()->GetAttribute(
605 wxT("version"), wxT("0.0.0.0"));
606 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
607 &v1
, &v2
, &v3
, &v4
) == 4)
608 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
613 if (m_version
!= version
)
615 wxLogError("Resource files must have same version number.");
619 ProcessPlatformProperty(rec
->Doc
->GetRoot());
622 rec
->Time
= file
->GetModificationTime();
623 #else // wxUSE_FILESYSTEM
624 rec
->Time
= wxDateTime(wxFileModificationTime(rec
->File
));
625 #endif // wxUSE_FILESYSTEM
626 #endif // wxUSE_DATETIME
629 # if wxUSE_FILESYSTEM
641 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
642 const wxString
& name
,
643 const wxString
& classname
,
644 bool recursive
) const
648 // first search for match at the top-level nodes (as this is
649 // where the resource is most commonly looked for):
650 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
652 if ( IsObjectNode(node
) && node
->GetAttribute(wxS("name")) == name
)
654 // empty class name matches everything
655 if ( classname
.empty() )
658 wxString
cls(node
->GetAttribute(wxS("class")));
660 // object_ref may not have 'class' attribute:
661 if (cls
.empty() && node
->GetName() == wxS("object_ref"))
663 wxString refName
= node
->GetAttribute(wxS("ref"));
667 const wxXmlNode
* const refNode
= GetResourceNode(refName
);
669 cls
= refNode
->GetAttribute(wxS("class"));
672 if ( cls
== classname
)
677 // then recurse in child nodes
680 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
682 if ( IsObjectNode(node
) )
684 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
694 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
695 const wxString
& classname
,
700 node
= GetResourceNodeAndLocation(name
, classname
, recursive
, &path
);
709 "XRC resource \"%s\" (class \"%s\") not found",
715 else // node was found
717 // ensure that relative paths work correctly when loading this node
718 // (which should happen as soon as we return as FindResource() result
719 // is always passed to CreateResFromNode())
720 m_curFileSystem
.ChangePathTo(path
);
722 #endif // wxUSE_FILESYSTEM
728 wxXmlResource::GetResourceNodeAndLocation(const wxString
& name
,
729 const wxString
& classname
,
731 wxString
*path
) const
733 // ensure everything is up-to-date: this is needed to support on-remand
734 // reloading of XRC files
735 const_cast<wxXmlResource
*>(this)->UpdateResources();
737 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
738 f
!= Data().end(); ++f
)
740 wxXmlResourceDataRecord
*const rec
= *f
;
741 wxXmlDocument
* const doc
= rec
->Doc
;
742 if ( !doc
|| !doc
->GetRoot() )
746 found
= DoFindResource(doc
->GetRoot(), name
, classname
, recursive
);
759 static void MergeNodesOver(wxXmlNode
& dest
, wxXmlNode
& overwriteWith
,
760 const wxString
& overwriteFilename
)
763 for ( wxXmlAttribute
*attr
= overwriteWith
.GetAttributes();
764 attr
; attr
= attr
->GetNext() )
766 wxXmlAttribute
*dattr
;
767 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
770 if ( dattr
->GetName() == attr
->GetName() )
772 dattr
->SetValue(attr
->GetValue());
778 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
781 // Merge child nodes:
782 for (wxXmlNode
* node
= overwriteWith
.GetChildren(); node
; node
= node
->GetNext())
784 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
787 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
789 if ( dnode
->GetName() == node
->GetName() &&
790 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
791 dnode
->GetType() == node
->GetType() )
793 MergeNodesOver(*dnode
, *node
, overwriteFilename
);
800 wxXmlNode
*copyOfNode
= new wxXmlNode(*node
);
801 // remember referenced object's file, see GetFileNameFromNode()
802 copyOfNode
->AddAttribute(ATTR_INPUT_FILENAME
, overwriteFilename
);
804 static const wxChar
*AT_END
= wxT("end");
805 wxString insert_pos
= node
->GetAttribute(wxT("insert_at"), AT_END
);
806 if ( insert_pos
== AT_END
)
808 dest
.AddChild(copyOfNode
);
810 else if ( insert_pos
== wxT("begin") )
812 dest
.InsertChild(copyOfNode
, dest
.GetChildren());
817 if ( dest
.GetType() == wxXML_TEXT_NODE
&& overwriteWith
.GetContent().length() )
818 dest
.SetContent(overwriteWith
.GetContent());
821 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
823 wxXmlResourceHandler
*handlerToUse
)
825 if (node
== NULL
) return NULL
;
827 // handling of referenced resource
828 if ( node
->GetName() == wxT("object_ref") )
830 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
831 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
840 "referenced object node with ref=\"%s\" not found",
847 if ( !node
->GetChildren() )
849 // In the typical, simple case, <object_ref> is used to link
850 // to another node and doesn't have any content of its own that
851 // would overwrite linked object's properties. In this case,
852 // we can simply create the resource from linked node.
854 return CreateResFromNode(refNode
, parent
, instance
);
858 // In the more complicated (but rare) case, <object_ref> has
859 // subnodes that partially overwrite content of the referenced
860 // object. In this case, we need to merge both XML trees and
861 // load the resource from result of the merge.
863 wxXmlNode
copy(*refNode
);
864 MergeNodesOver(copy
, *node
, GetFileNameFromNode(node
, Data()));
866 // remember referenced object's file, see GetFileNameFromNode()
867 copy
.AddAttribute(ATTR_INPUT_FILENAME
,
868 GetFileNameFromNode(refNode
, Data()));
870 return CreateResFromNode(©
, parent
, instance
);
876 if (handlerToUse
->CanHandle(node
))
878 return handlerToUse
->CreateResource(node
, parent
, instance
);
881 else if (node
->GetName() == wxT("object"))
883 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
884 h
!= m_handlers
.end(); ++h
)
886 wxXmlResourceHandler
*handler
= *h
;
887 if (handler
->CanHandle(node
))
888 return handler
->CreateResource(node
, parent
, instance
);
897 "no handler found for XML node \"%s\" (class \"%s\")",
899 node
->GetAttribute("class", wxEmptyString
)
906 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
908 // this is a class so that it can be forward-declared
911 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
913 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
915 if (!ms_subclassFactories
)
917 ms_subclassFactories
= new wxXmlSubclassFactories
;
919 ms_subclassFactories
->push_back(factory
);
922 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
925 ~wxXmlSubclassFactoryCXX() {}
927 wxObject
*Create(const wxString
& className
)
929 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
932 return classInfo
->CreateObject();
941 wxXmlResourceHandler::wxXmlResourceHandler()
942 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
943 m_parentAsWindow(NULL
)
948 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
950 wxXmlNode
*myNode
= m_node
;
951 wxString myClass
= m_class
;
952 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
953 wxWindow
*myParentAW
= m_parentAsWindow
;
955 m_instance
= instance
;
956 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
957 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
959 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
960 if (!subclass
.empty())
962 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
963 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
965 m_instance
= (*i
)->Create(subclass
);
972 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
978 "subclass \"%s\" not found for resource \"%s\", not subclassing",
987 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
989 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
991 wxObject
*returned
= DoCreateResource();
995 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
996 m_instance
= myInstance
;
1002 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
1004 m_styleNames
.Add(name
);
1005 m_styleValues
.Add(value
);
1010 void wxXmlResourceHandler::AddWindowStyles()
1012 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
1014 // the border styles all have the old and new names, recognize both for now
1015 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
1016 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
1017 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
1018 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
1019 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
1020 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
1022 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
1023 XRC_ADD_STYLE(wxWANTS_CHARS
);
1024 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
1025 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
1026 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
1027 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
1028 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
1029 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
1034 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
1036 return (GetParamNode(param
) != NULL
);
1040 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
1042 wxString s
= GetParamValue(param
);
1044 if (!s
) return defaults
;
1046 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
1050 while (tkn
.HasMoreTokens())
1052 fl
= tkn
.GetNextToken();
1053 index
= m_styleNames
.Index(fl
);
1054 if (index
!= wxNOT_FOUND
)
1056 style
|= m_styleValues
[index
];
1063 wxString::Format("unknown style flag \"%s\"", fl
)
1072 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
1074 wxXmlNode
*parNode
= GetParamNode(param
);
1075 wxString
str1(GetNodeContent(parNode
));
1078 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1079 const bool escapeBackslash
= (m_resource
->CompareVersion(2,5,3,0) >= 0);
1081 // VS: First version of XRC resources used $ instead of & (which is
1082 // illegal in XML), but later I realized that '_' fits this purpose
1083 // much better (because &File means "File with F underlined").
1084 const wxChar amp_char
= (m_resource
->CompareVersion(2,3,0,1) < 0)
1087 for ( wxString::const_iterator dt
= str1
.begin(); dt
!= str1
.end(); ++dt
)
1089 // Remap amp_char to &, map double amp_char to amp_char (for things
1090 // like "&File..." -- this is illegal in XML, so we use "_File..."):
1091 if ( *dt
== amp_char
)
1093 if ( *(++dt
) == amp_char
)
1096 str2
<< wxT('&') << *dt
;
1098 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
1099 else if ( *dt
== wxT('\\') )
1101 switch ( (*(++dt
)).GetValue() )
1116 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1117 if ( escapeBackslash
)
1122 // else fall-through to default: branch below
1125 str2
<< wxT('\\') << *dt
;
1135 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
1137 if (translate
&& parNode
&&
1138 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
1140 return wxGetTranslation(str2
, m_resource
->GetDomain());
1147 // The string is internally stored as UTF-8, we have to convert
1148 // it into system's default encoding so that it can be displayed:
1149 return wxString(str2
.wc_str(wxConvUTF8
), wxConvLocal
);
1154 // If wxXRC_USE_LOCALE is not set, then the string is already in
1155 // system's default encoding in ANSI build, so we don't have to
1156 // do anything special here.
1162 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
1165 wxString str1
= GetParamValue(param
);
1167 if (!str1
.ToLong(&value
))
1173 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
1175 wxString str
= GetParamValue(param
);
1178 // strings in XRC always use C locale but wxString::ToDouble() uses the
1179 // current one, so transform the string to it supposing that the only
1180 // difference between them is the decimal separator
1182 // TODO: use wxString::ToCDouble() when we have it
1183 str
.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1184 wxLOCALE_CAT_NUMBER
));
1185 #endif // wxUSE_INTL
1188 if (!str
.ToDouble(&value
))
1191 return wx_truncate_cast(float, value
);
1195 int wxXmlResourceHandler::GetID()
1197 return wxXmlResource::GetXRCID(GetName());
1202 wxString
wxXmlResourceHandler::GetName()
1204 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1209 bool wxXmlResourceHandler::GetBoolAttr(const wxString
& attr
, bool defaultv
)
1212 return m_node
->GetAttribute(attr
, &v
) ? v
== '1' : defaultv
;
1215 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1217 const wxString v
= GetParamValue(param
);
1219 return v
.empty() ? defaultv
: (v
== '1');
1223 static wxColour
GetSystemColour(const wxString
& name
)
1227 #define SYSCLR(clr) \
1228 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1229 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1230 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1231 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1232 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1233 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1234 SYSCLR(wxSYS_COLOUR_MENU
)
1235 SYSCLR(wxSYS_COLOUR_WINDOW
)
1236 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1237 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1238 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1239 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1240 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1241 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1242 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1243 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1244 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1245 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1246 SYSCLR(wxSYS_COLOUR_3DFACE
)
1247 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1248 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1249 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1250 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1251 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1252 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1253 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1254 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1255 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1256 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1257 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1258 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1259 SYSCLR(wxSYS_COLOUR_INFOBK
)
1260 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1261 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1262 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1263 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1264 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1265 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1269 return wxNullColour
;
1272 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1274 wxString v
= GetParamValue(param
);
1281 // wxString -> wxColour conversion
1284 // the colour doesn't use #RRGGBB format, check if it is symbolic
1286 clr
= GetSystemColour(v
);
1293 wxString::Format("incorrect colour specification \"%s\"", v
)
1295 return wxNullColour
;
1304 // if 'param' has stock_id/stock_client, extracts them and returns true
1305 bool GetStockArtAttrs(const wxXmlNode
*paramNode
,
1306 const wxString
& defaultArtClient
,
1307 wxString
& art_id
, wxString
& art_client
)
1311 art_id
= paramNode
->GetAttribute("stock_id", "");
1313 if ( !art_id
.empty() )
1315 art_id
= wxART_MAKE_ART_ID_FROM_STR(art_id
);
1317 art_client
= paramNode
->GetAttribute("stock_client", "");
1318 if ( art_client
.empty() )
1319 art_client
= defaultArtClient
;
1321 art_client
= wxART_MAKE_CLIENT_ID_FROM_STR(art_client
);
1330 } // anonymous namespace
1332 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1333 const wxArtClient
& defaultArtClient
,
1336 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1337 wxString art_id
, art_client
;
1338 if ( GetStockArtAttrs(GetParamNode(param
), defaultArtClient
,
1339 art_id
, art_client
) )
1341 wxBitmap
stockArt(wxArtProvider::GetBitmap(art_id
, art_client
, size
));
1342 if ( stockArt
.Ok() )
1346 /* ...or load the bitmap from file: */
1347 wxString name
= GetParamValue(param
);
1348 if (name
.empty()) return wxNullBitmap
;
1349 #if wxUSE_FILESYSTEM
1350 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1356 wxString::Format("cannot open bitmap resource \"%s\"", name
)
1358 return wxNullBitmap
;
1360 wxImage
img(*(fsfile
->GetStream()));
1371 wxString::Format("cannot create bitmap from \"%s\"", name
)
1373 return wxNullBitmap
;
1375 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1376 return wxBitmap(img
);
1380 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1381 const wxArtClient
& defaultArtClient
,
1385 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1389 wxIconBundle
wxXmlResourceHandler::GetIconBundle(const wxString
& param
,
1390 const wxArtClient
& defaultArtClient
)
1392 wxString art_id
, art_client
;
1393 if ( GetStockArtAttrs(GetParamNode(param
), defaultArtClient
,
1394 art_id
, art_client
) )
1396 wxIconBundle
stockArt(wxArtProvider::GetIconBundle(art_id
, art_client
));
1397 if ( stockArt
.IsOk() )
1401 const wxString name
= GetParamValue(param
);
1403 return wxNullIconBundle
;
1405 #if wxUSE_FILESYSTEM
1406 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1407 if ( fsfile
== NULL
)
1412 wxString::Format("cannot open icon resource \"%s\"", name
)
1414 return wxNullIconBundle
;
1417 wxIconBundle
bundle(*(fsfile
->GetStream()));
1420 wxIconBundle
bundle(name
);
1423 if ( !bundle
.IsOk() )
1428 wxString::Format("cannot create icon from \"%s\"", name
)
1430 return wxNullIconBundle
;
1437 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1439 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1441 wxXmlNode
*n
= m_node
->GetChildren();
1445 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1447 // TODO: check that there are no other properties/parameters with
1448 // the same name and log an error if there are (can't do this
1449 // right now as I'm not sure if it's not going to break code
1450 // using this function in unintentional way (i.e. for
1451 // accessing other things than properties), for example
1452 // wxBitmapComboBoxXmlHandler almost surely does
1460 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1462 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1467 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1469 wxXmlNode
*n
= node
;
1470 if (n
== NULL
) return wxEmptyString
;
1471 n
= n
->GetChildren();
1475 if (n
->GetType() == wxXML_TEXT_NODE
||
1476 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1477 return n
->GetContent();
1480 return wxEmptyString
;
1485 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1488 return GetNodeContent(m_node
);
1490 return GetNodeContent(GetParamNode(param
));
1495 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1496 wxWindow
*windowToUse
)
1498 wxString s
= GetParamValue(param
);
1499 if (s
.empty()) s
= wxT("-1,-1");
1503 is_dlg
= s
[s
.length()-1] == wxT('d');
1504 if (is_dlg
) s
.RemoveLast();
1506 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1507 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1512 wxString::Format("cannot parse coordinates value \"%s\"", s
)
1514 return wxDefaultSize
;
1521 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1523 else if (m_parentAsWindow
)
1525 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1532 "cannot convert dialog units: dialog unknown"
1534 return wxDefaultSize
;
1538 return wxSize(sx
, sy
);
1543 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1545 wxSize sz
= GetSize(param
);
1546 return wxPoint(sz
.x
, sz
.y
);
1551 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1553 wxWindow
*windowToUse
)
1555 wxString s
= GetParamValue(param
);
1556 if (s
.empty()) return defaultv
;
1560 is_dlg
= s
[s
.length()-1] == wxT('d');
1561 if (is_dlg
) s
.RemoveLast();
1568 wxString::Format("cannot parse dimension value \"%s\"", s
)
1577 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1579 else if (m_parentAsWindow
)
1581 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1588 "cannot convert dialog units: dialog unknown"
1598 // Get system font index using indexname
1599 static wxFont
GetSystemFont(const wxString
& name
)
1603 #define SYSFNT(fnt) \
1604 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1605 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1606 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1607 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1608 SYSFNT(wxSYS_SYSTEM_FONT
)
1609 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1610 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1611 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1618 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1620 wxXmlNode
*font_node
= GetParamNode(param
);
1621 if (font_node
== NULL
)
1624 wxString::Format("cannot find font node \"%s\"", param
));
1628 wxXmlNode
*oldnode
= m_node
;
1635 bool hasSize
= HasParam(wxT("size"));
1637 isize
= GetLong(wxT("size"), -1);
1640 int istyle
= wxNORMAL
;
1641 bool hasStyle
= HasParam(wxT("style"));
1644 wxString style
= GetParamValue(wxT("style"));
1645 if (style
== wxT("italic"))
1647 else if (style
== wxT("slant"))
1652 int iweight
= wxNORMAL
;
1653 bool hasWeight
= HasParam(wxT("weight"));
1656 wxString weight
= GetParamValue(wxT("weight"));
1657 if (weight
== wxT("bold"))
1659 else if (weight
== wxT("light"))
1664 bool hasUnderlined
= HasParam(wxT("underlined"));
1665 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1667 // family and facename
1668 int ifamily
= wxDEFAULT
;
1669 bool hasFamily
= HasParam(wxT("family"));
1672 wxString family
= GetParamValue(wxT("family"));
1673 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1674 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1675 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1676 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1677 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1678 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1683 bool hasFacename
= HasParam(wxT("face"));
1686 wxString faces
= GetParamValue(wxT("face"));
1687 wxStringTokenizer
tk(faces
, wxT(","));
1689 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1690 while (tk
.HasMoreTokens())
1692 int index
= facenames
.Index(tk
.GetNextToken(), false);
1693 if (index
!= wxNOT_FOUND
)
1695 facename
= facenames
[index
];
1699 #else // !wxUSE_FONTENUM
1700 // just use the first face name if we can't check its availability:
1701 if (tk
.HasMoreTokens())
1702 facename
= tk
.GetNextToken();
1703 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
1707 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1708 bool hasEncoding
= HasParam(wxT("encoding"));
1711 wxString encoding
= GetParamValue(wxT("encoding"));
1712 wxFontMapper mapper
;
1713 if (!encoding
.empty())
1714 enc
= mapper
.CharsetToEncoding(encoding
);
1715 if (enc
== wxFONTENCODING_SYSTEM
)
1716 enc
= wxFONTENCODING_DEFAULT
;
1719 // is this font based on a system font?
1720 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1724 if (hasSize
&& isize
!= -1)
1725 font
.SetPointSize(isize
);
1726 else if (HasParam(wxT("relativesize")))
1727 font
.SetPointSize(int(font
.GetPointSize() *
1728 GetFloat(wxT("relativesize"))));
1731 font
.SetStyle(istyle
);
1733 font
.SetWeight(iweight
);
1735 font
.SetUnderlined(underlined
);
1737 font
.SetFamily(ifamily
);
1739 font
.SetFaceName(facename
);
1741 font
.SetDefaultEncoding(enc
);
1743 else // not based on system font
1745 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1746 ifamily
, istyle
, iweight
,
1747 underlined
, facename
, enc
);
1755 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1757 //FIXME : add cursor
1759 if (HasParam(wxT("exstyle")))
1760 // Have to OR it with existing style, since
1761 // some implementations (e.g. wxGTK) use the extra style
1763 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1764 if (HasParam(wxT("bg")))
1765 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1766 if (HasParam(wxT("fg")))
1767 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1768 if (GetBool(wxT("enabled"), 1) == 0)
1770 if (GetBool(wxT("focused"), 0) == 1)
1772 if (GetBool(wxT("hidden"), 0) == 1)
1775 if (HasParam(wxT("tooltip")))
1776 wnd
->SetToolTip(GetText(wxT("tooltip")));
1778 if (HasParam(wxT("font")))
1779 wnd
->SetFont(GetFont());
1780 if (HasParam(wxT("help")))
1781 wnd
->SetHelpText(GetText(wxT("help")));
1785 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1787 for ( wxXmlNode
*n
= m_node
->GetChildren(); n
; n
= n
->GetNext() )
1789 if ( IsObjectNode(n
) )
1791 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1792 this_hnd_only
? this : NULL
);
1798 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1801 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1802 wxXmlNode
*n
= root
->GetChildren();
1806 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1808 CreateResource(n
, parent
, NULL
);
1815 //-----------------------------------------------------------------------------
1817 //-----------------------------------------------------------------------------
1819 void wxXmlResourceHandler::ReportError(const wxString
& message
)
1821 m_resource
->ReportError(m_node
, message
);
1824 void wxXmlResourceHandler::ReportError(wxXmlNode
*context
,
1825 const wxString
& message
)
1827 m_resource
->ReportError(context
? context
: m_node
, message
);
1830 void wxXmlResourceHandler::ReportParamError(const wxString
& param
,
1831 const wxString
& message
)
1833 m_resource
->ReportError(GetParamNode(param
), message
);
1836 void wxXmlResource::ReportError(wxXmlNode
*context
, const wxString
& message
)
1840 DoReportError("", NULL
, message
);
1844 // We need to find out the file that 'context' is part of. Performance of
1845 // this code is not critical, so we simply find the root XML node and
1846 // compare it with all loaded XRC files.
1847 const wxString filename
= GetFileNameFromNode(context
, Data());
1849 DoReportError(filename
, context
, message
);
1852 void wxXmlResource::DoReportError(const wxString
& xrcFile
, wxXmlNode
*position
,
1853 const wxString
& message
)
1855 const int line
= position
? position
->GetLineNumber() : -1;
1858 if ( !xrcFile
.empty() )
1859 loc
= xrcFile
+ ':';
1861 loc
+= wxString::Format("%d:", line
);
1865 wxLogError("XRC error: %s%s", loc
, message
);
1869 //-----------------------------------------------------------------------------
1870 // XRCID implementation
1871 //-----------------------------------------------------------------------------
1873 #define XRCID_TABLE_SIZE 1024
1878 /* Hold the id so that once an id is allocated for a name, it
1879 does not get created again by NewControlId at least
1880 until we are done with it */
1886 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1888 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1892 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1893 index
%= XRCID_TABLE_SIZE
;
1895 XRCID_record
*oldrec
= NULL
;
1896 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1898 if (wxStrcmp(rec
->key
, str_id
) == 0)
1905 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1906 &XRCID_Records
[index
] : &oldrec
->next
;
1907 *rec_var
= new XRCID_record
;
1908 (*rec_var
)->key
= wxStrdup(str_id
);
1909 (*rec_var
)->next
= NULL
;
1912 if (value_if_not_found
!= wxID_NONE
)
1913 (*rec_var
)->id
= value_if_not_found
;
1916 int asint
= wxStrtol(str_id
, &end
, 10);
1917 if (*str_id
&& *end
== 0)
1919 // if str_id was integer, keep it verbosely:
1920 (*rec_var
)->id
= asint
;
1924 (*rec_var
)->id
= wxWindowBase::NewControlId();
1928 return (*rec_var
)->id
;
1931 static void AddStdXRCID_Records();
1934 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
1936 static bool s_stdIDsAdded
= false;
1938 if ( !s_stdIDsAdded
)
1940 s_stdIDsAdded
= true;
1941 AddStdXRCID_Records();
1944 return XRCID_Lookup(str_id
, value_if_not_found
);
1948 wxString
wxXmlResource::FindXRCIDById(int numId
)
1950 for ( int i
= 0; i
< XRCID_TABLE_SIZE
; i
++ )
1952 for ( XRCID_record
*rec
= XRCID_Records
[i
]; rec
; rec
= rec
->next
)
1954 if ( rec
->id
== numId
)
1955 return wxString(rec
->key
);
1962 static void CleanXRCID_Record(XRCID_record
*rec
)
1966 CleanXRCID_Record(rec
->next
);
1973 static void CleanXRCID_Records()
1975 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1977 CleanXRCID_Record(XRCID_Records
[i
]);
1978 XRCID_Records
[i
] = NULL
;
1982 static void AddStdXRCID_Records()
1984 #define stdID(id) XRCID_Lookup(#id, id)
1988 stdID(wxID_SEPARATOR
);
2001 stdID(wxID_PRINT_SETUP
);
2002 stdID(wxID_PAGE_SETUP
);
2003 stdID(wxID_PREVIEW
);
2005 stdID(wxID_HELP_CONTENTS
);
2006 stdID(wxID_HELP_COMMANDS
);
2007 stdID(wxID_HELP_PROCEDURES
);
2008 stdID(wxID_HELP_CONTEXT
);
2009 stdID(wxID_CLOSE_ALL
);
2010 stdID(wxID_PREFERENCES
);
2017 stdID(wxID_DUPLICATE
);
2018 stdID(wxID_SELECTALL
);
2020 stdID(wxID_REPLACE
);
2021 stdID(wxID_REPLACE_ALL
);
2022 stdID(wxID_PROPERTIES
);
2023 stdID(wxID_VIEW_DETAILS
);
2024 stdID(wxID_VIEW_LARGEICONS
);
2025 stdID(wxID_VIEW_SMALLICONS
);
2026 stdID(wxID_VIEW_LIST
);
2027 stdID(wxID_VIEW_SORTDATE
);
2028 stdID(wxID_VIEW_SORTNAME
);
2029 stdID(wxID_VIEW_SORTSIZE
);
2030 stdID(wxID_VIEW_SORTTYPE
);
2046 stdID(wxID_FORWARD
);
2047 stdID(wxID_BACKWARD
);
2048 stdID(wxID_DEFAULT
);
2052 stdID(wxID_CONTEXT_HELP
);
2053 stdID(wxID_YESTOALL
);
2054 stdID(wxID_NOTOALL
);
2063 stdID(wxID_REFRESH
);
2068 stdID(wxID_JUSTIFY_CENTER
);
2069 stdID(wxID_JUSTIFY_FILL
);
2070 stdID(wxID_JUSTIFY_RIGHT
);
2071 stdID(wxID_JUSTIFY_LEFT
);
2072 stdID(wxID_UNDERLINE
);
2074 stdID(wxID_UNINDENT
);
2075 stdID(wxID_ZOOM_100
);
2076 stdID(wxID_ZOOM_FIT
);
2077 stdID(wxID_ZOOM_IN
);
2078 stdID(wxID_ZOOM_OUT
);
2079 stdID(wxID_UNDELETE
);
2080 stdID(wxID_REVERT_TO_SAVED
);
2081 stdID(wxID_SYSTEM_MENU
);
2082 stdID(wxID_CLOSE_FRAME
);
2083 stdID(wxID_MOVE_FRAME
);
2084 stdID(wxID_RESIZE_FRAME
);
2085 stdID(wxID_MAXIMIZE_FRAME
);
2086 stdID(wxID_ICONIZE_FRAME
);
2087 stdID(wxID_RESTORE_FRAME
);
2089 stdID(wxID_CONVERT
);
2090 stdID(wxID_EXECUTE
);
2092 stdID(wxID_HARDDISK
);
2098 stdID(wxID_JUMP_TO
);
2099 stdID(wxID_NETWORK
);
2100 stdID(wxID_SELECT_COLOR
);
2101 stdID(wxID_SELECT_FONT
);
2102 stdID(wxID_SORT_ASCENDING
);
2103 stdID(wxID_SORT_DESCENDING
);
2104 stdID(wxID_SPELL_CHECK
);
2105 stdID(wxID_STRIKETHROUGH
);
2114 //-----------------------------------------------------------------------------
2115 // module and globals
2116 //-----------------------------------------------------------------------------
2118 // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2119 // can happen that some XRC records have been created because of the use of
2120 // XRCID() in event tables, which happens during static objects initialization,
2121 // but then the application initialization failed and so the wx modules were
2122 // neither initialized nor cleaned up -- this static object does the cleanup in
2124 static struct wxXRCStaticCleanup
2126 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2129 class wxXmlResourceModule
: public wxModule
2131 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
2133 wxXmlResourceModule() {}
2136 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
2141 delete wxXmlResource::Set(NULL
);
2142 if(wxXmlResource::ms_subclassFactories
)
2144 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
2145 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
2149 wxDELETE(wxXmlResource::ms_subclassFactories
);
2151 CleanXRCID_Records();
2155 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
2158 // When wxXml is loaded dynamically after the application is already running
2159 // then the built-in module system won't pick this one up. Add it manually.
2160 void wxXmlInitResourceModule()
2162 wxModule
* module = new wxXmlResourceModule
;
2164 wxModule::RegisterModule(module);