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 wxXmlNode
copy(*refNode
);
848 MergeNodesOver(copy
, *node
, GetFileNameFromNode(node
, Data()));
850 // remember referenced object's file, see GetFileNameFromNode()
851 copy
.AddAttribute(ATTR_INPUT_FILENAME
,
852 GetFileNameFromNode(refNode
, Data()));
854 return CreateResFromNode(©
, parent
, instance
);
859 if (handlerToUse
->CanHandle(node
))
861 return handlerToUse
->CreateResource(node
, parent
, instance
);
864 else if (node
->GetName() == wxT("object"))
866 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
867 h
!= m_handlers
.end(); ++h
)
869 wxXmlResourceHandler
*handler
= *h
;
870 if (handler
->CanHandle(node
))
871 return handler
->CreateResource(node
, parent
, instance
);
880 "no handler found for XML node \"%s\" (class \"%s\")",
882 node
->GetAttribute("class", wxEmptyString
)
889 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
891 // this is a class so that it can be forward-declared
894 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
896 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
898 if (!ms_subclassFactories
)
900 ms_subclassFactories
= new wxXmlSubclassFactories
;
902 ms_subclassFactories
->push_back(factory
);
905 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
908 ~wxXmlSubclassFactoryCXX() {}
910 wxObject
*Create(const wxString
& className
)
912 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
915 return classInfo
->CreateObject();
924 wxXmlResourceHandler::wxXmlResourceHandler()
925 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
926 m_parentAsWindow(NULL
)
931 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
933 wxXmlNode
*myNode
= m_node
;
934 wxString myClass
= m_class
;
935 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
936 wxWindow
*myParentAW
= m_parentAsWindow
;
938 m_instance
= instance
;
939 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
940 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
942 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
943 if (!subclass
.empty())
945 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
946 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
948 m_instance
= (*i
)->Create(subclass
);
955 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
961 "subclass \"%s\" not found for resource \"%s\", not subclassing",
970 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
972 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
974 wxObject
*returned
= DoCreateResource();
978 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
979 m_instance
= myInstance
;
985 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
987 m_styleNames
.Add(name
);
988 m_styleValues
.Add(value
);
993 void wxXmlResourceHandler::AddWindowStyles()
995 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
997 // the border styles all have the old and new names, recognize both for now
998 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
999 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
1000 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
1001 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
1002 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
1003 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
1005 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
1006 XRC_ADD_STYLE(wxWANTS_CHARS
);
1007 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
1008 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
1009 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
1010 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
1011 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
1012 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
1017 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
1019 return (GetParamNode(param
) != NULL
);
1023 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
1025 wxString s
= GetParamValue(param
);
1027 if (!s
) return defaults
;
1029 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
1033 while (tkn
.HasMoreTokens())
1035 fl
= tkn
.GetNextToken();
1036 index
= m_styleNames
.Index(fl
);
1037 if (index
!= wxNOT_FOUND
)
1039 style
|= m_styleValues
[index
];
1046 wxString::Format("unknown style flag \"%s\"", fl
)
1055 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
1057 wxXmlNode
*parNode
= GetParamNode(param
);
1058 wxString
str1(GetNodeContent(parNode
));
1061 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1062 const bool escapeBackslash
= (m_resource
->CompareVersion(2,5,3,0) >= 0);
1064 // VS: First version of XRC resources used $ instead of & (which is
1065 // illegal in XML), but later I realized that '_' fits this purpose
1066 // much better (because &File means "File with F underlined").
1067 const wxChar amp_char
= (m_resource
->CompareVersion(2,3,0,1) < 0)
1070 for ( wxString::const_iterator dt
= str1
.begin(); dt
!= str1
.end(); ++dt
)
1072 // Remap amp_char to &, map double amp_char to amp_char (for things
1073 // like "&File..." -- this is illegal in XML, so we use "_File..."):
1074 if ( *dt
== amp_char
)
1076 if ( *(++dt
) == amp_char
)
1079 str2
<< wxT('&') << *dt
;
1081 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
1082 else if ( *dt
== wxT('\\') )
1084 switch ( (*(++dt
)).GetValue() )
1099 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1100 if ( escapeBackslash
)
1105 // else fall-through to default: branch below
1108 str2
<< wxT('\\') << *dt
;
1118 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
1120 if (translate
&& parNode
&&
1121 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
1123 return wxGetTranslation(str2
, m_resource
->GetDomain());
1130 // The string is internally stored as UTF-8, we have to convert
1131 // it into system's default encoding so that it can be displayed:
1132 return wxString(str2
.wc_str(wxConvUTF8
), wxConvLocal
);
1137 // If wxXRC_USE_LOCALE is not set, then the string is already in
1138 // system's default encoding in ANSI build, so we don't have to
1139 // do anything special here.
1145 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
1148 wxString str1
= GetParamValue(param
);
1150 if (!str1
.ToLong(&value
))
1156 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
1158 wxString str
= GetParamValue(param
);
1161 // strings in XRC always use C locale but wxString::ToDouble() uses the
1162 // current one, so transform the string to it supposing that the only
1163 // difference between them is the decimal separator
1165 // TODO: use wxString::ToCDouble() when we have it
1166 str
.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1167 wxLOCALE_CAT_NUMBER
));
1168 #endif // wxUSE_INTL
1171 if (!str
.ToDouble(&value
))
1174 return wx_truncate_cast(float, value
);
1178 int wxXmlResourceHandler::GetID()
1180 return wxXmlResource::GetXRCID(GetName());
1185 wxString
wxXmlResourceHandler::GetName()
1187 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1192 bool wxXmlResourceHandler::GetBoolAttr(const wxString
& attr
, bool defaultv
)
1195 return m_node
->GetAttribute(attr
, &v
) ? v
== '1' : defaultv
;
1198 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1200 const wxString v
= GetParamValue(param
);
1202 return v
.empty() ? defaultv
: (v
== '1');
1206 static wxColour
GetSystemColour(const wxString
& name
)
1210 #define SYSCLR(clr) \
1211 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1212 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1213 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1214 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1215 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1216 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1217 SYSCLR(wxSYS_COLOUR_MENU
)
1218 SYSCLR(wxSYS_COLOUR_WINDOW
)
1219 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1220 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1221 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1222 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1223 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1224 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1225 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1226 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1227 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1228 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1229 SYSCLR(wxSYS_COLOUR_3DFACE
)
1230 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1231 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1232 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1233 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1234 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1235 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1236 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1237 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1238 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1239 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1240 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1241 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1242 SYSCLR(wxSYS_COLOUR_INFOBK
)
1243 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1244 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1245 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1246 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1247 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1248 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1252 return wxNullColour
;
1255 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1257 wxString v
= GetParamValue(param
);
1264 // wxString -> wxColour conversion
1267 // the colour doesn't use #RRGGBB format, check if it is symbolic
1269 clr
= GetSystemColour(v
);
1276 wxString::Format("incorrect colour specification \"%s\"", v
)
1278 return wxNullColour
;
1287 // if 'param' has stock_id/stock_client, extracts them and returns true
1288 bool GetStockArtAttrs(const wxXmlNode
*paramNode
,
1289 const wxString
& defaultArtClient
,
1290 wxString
& art_id
, wxString
& art_client
)
1294 art_id
= paramNode
->GetAttribute("stock_id", "");
1296 if ( !art_id
.empty() )
1298 art_id
= wxART_MAKE_ART_ID_FROM_STR(art_id
);
1300 art_client
= paramNode
->GetAttribute("stock_client", "");
1301 if ( art_client
.empty() )
1302 art_client
= defaultArtClient
;
1304 art_client
= wxART_MAKE_CLIENT_ID_FROM_STR(art_client
);
1313 } // anonymous namespace
1315 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1316 const wxArtClient
& defaultArtClient
,
1319 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1320 wxString art_id
, art_client
;
1321 if ( GetStockArtAttrs(GetParamNode(param
), defaultArtClient
,
1322 art_id
, art_client
) )
1324 wxBitmap
stockArt(wxArtProvider::GetBitmap(art_id
, art_client
, size
));
1325 if ( stockArt
.Ok() )
1329 /* ...or load the bitmap from file: */
1330 wxString name
= GetParamValue(param
);
1331 if (name
.empty()) return wxNullBitmap
;
1332 #if wxUSE_FILESYSTEM
1333 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1339 wxString::Format("cannot open bitmap resource \"%s\"", name
)
1341 return wxNullBitmap
;
1343 wxImage
img(*(fsfile
->GetStream()));
1354 wxString::Format("cannot create bitmap from \"%s\"", name
)
1356 return wxNullBitmap
;
1358 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1359 return wxBitmap(img
);
1363 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1364 const wxArtClient
& defaultArtClient
,
1368 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1372 wxIconBundle
wxXmlResourceHandler::GetIconBundle(const wxString
& param
,
1373 const wxArtClient
& defaultArtClient
)
1375 wxString art_id
, art_client
;
1376 if ( GetStockArtAttrs(GetParamNode(param
), defaultArtClient
,
1377 art_id
, art_client
) )
1379 wxIconBundle
stockArt(wxArtProvider::GetIconBundle(art_id
, art_client
));
1380 if ( stockArt
.IsOk() )
1384 const wxString name
= GetParamValue(param
);
1386 return wxNullIconBundle
;
1388 #if wxUSE_FILESYSTEM
1389 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1390 if ( fsfile
== NULL
)
1395 wxString::Format("cannot open icon resource \"%s\"", name
)
1397 return wxNullIconBundle
;
1400 wxIconBundle
bundle(*(fsfile
->GetStream()));
1403 wxIconBundle
bundle(name
);
1406 if ( !bundle
.IsOk() )
1411 wxString::Format("cannot create icon from \"%s\"", name
)
1413 return wxNullIconBundle
;
1420 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1422 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1424 wxXmlNode
*n
= m_node
->GetChildren();
1428 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1430 // TODO: check that there are no other properties/parameters with
1431 // the same name and log an error if there are (can't do this
1432 // right now as I'm not sure if it's not going to break code
1433 // using this function in unintentional way (i.e. for
1434 // accessing other things than properties), for example
1435 // wxBitmapComboBoxXmlHandler almost surely does
1443 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1445 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1450 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1452 wxXmlNode
*n
= node
;
1453 if (n
== NULL
) return wxEmptyString
;
1454 n
= n
->GetChildren();
1458 if (n
->GetType() == wxXML_TEXT_NODE
||
1459 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1460 return n
->GetContent();
1463 return wxEmptyString
;
1468 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1471 return GetNodeContent(m_node
);
1473 return GetNodeContent(GetParamNode(param
));
1478 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1479 wxWindow
*windowToUse
)
1481 wxString s
= GetParamValue(param
);
1482 if (s
.empty()) s
= wxT("-1,-1");
1486 is_dlg
= s
[s
.length()-1] == wxT('d');
1487 if (is_dlg
) s
.RemoveLast();
1489 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1490 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1495 wxString::Format("cannot parse coordinates value \"%s\"", s
)
1497 return wxDefaultSize
;
1504 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1506 else if (m_parentAsWindow
)
1508 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1515 "cannot convert dialog units: dialog unknown"
1517 return wxDefaultSize
;
1521 return wxSize(sx
, sy
);
1526 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1528 wxSize sz
= GetSize(param
);
1529 return wxPoint(sz
.x
, sz
.y
);
1534 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1536 wxWindow
*windowToUse
)
1538 wxString s
= GetParamValue(param
);
1539 if (s
.empty()) return defaultv
;
1543 is_dlg
= s
[s
.length()-1] == wxT('d');
1544 if (is_dlg
) s
.RemoveLast();
1551 wxString::Format("cannot parse dimension value \"%s\"", s
)
1560 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1562 else if (m_parentAsWindow
)
1564 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1571 "cannot convert dialog units: dialog unknown"
1581 // Get system font index using indexname
1582 static wxFont
GetSystemFont(const wxString
& name
)
1586 #define SYSFNT(fnt) \
1587 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1588 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1589 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1590 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1591 SYSFNT(wxSYS_SYSTEM_FONT
)
1592 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1593 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1594 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1601 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1603 wxXmlNode
*font_node
= GetParamNode(param
);
1604 if (font_node
== NULL
)
1607 wxString::Format("cannot find font node \"%s\"", param
));
1611 wxXmlNode
*oldnode
= m_node
;
1618 bool hasSize
= HasParam(wxT("size"));
1620 isize
= GetLong(wxT("size"), -1);
1623 int istyle
= wxNORMAL
;
1624 bool hasStyle
= HasParam(wxT("style"));
1627 wxString style
= GetParamValue(wxT("style"));
1628 if (style
== wxT("italic"))
1630 else if (style
== wxT("slant"))
1635 int iweight
= wxNORMAL
;
1636 bool hasWeight
= HasParam(wxT("weight"));
1639 wxString weight
= GetParamValue(wxT("weight"));
1640 if (weight
== wxT("bold"))
1642 else if (weight
== wxT("light"))
1647 bool hasUnderlined
= HasParam(wxT("underlined"));
1648 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1650 // family and facename
1651 int ifamily
= wxDEFAULT
;
1652 bool hasFamily
= HasParam(wxT("family"));
1655 wxString family
= GetParamValue(wxT("family"));
1656 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1657 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1658 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1659 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1660 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1661 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1666 bool hasFacename
= HasParam(wxT("face"));
1669 wxString faces
= GetParamValue(wxT("face"));
1670 wxStringTokenizer
tk(faces
, wxT(","));
1672 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1673 while (tk
.HasMoreTokens())
1675 int index
= facenames
.Index(tk
.GetNextToken(), false);
1676 if (index
!= wxNOT_FOUND
)
1678 facename
= facenames
[index
];
1682 #else // !wxUSE_FONTENUM
1683 // just use the first face name if we can't check its availability:
1684 if (tk
.HasMoreTokens())
1685 facename
= tk
.GetNextToken();
1686 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
1690 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1691 bool hasEncoding
= HasParam(wxT("encoding"));
1694 wxString encoding
= GetParamValue(wxT("encoding"));
1695 wxFontMapper mapper
;
1696 if (!encoding
.empty())
1697 enc
= mapper
.CharsetToEncoding(encoding
);
1698 if (enc
== wxFONTENCODING_SYSTEM
)
1699 enc
= wxFONTENCODING_DEFAULT
;
1702 // is this font based on a system font?
1703 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1707 if (hasSize
&& isize
!= -1)
1708 font
.SetPointSize(isize
);
1709 else if (HasParam(wxT("relativesize")))
1710 font
.SetPointSize(int(font
.GetPointSize() *
1711 GetFloat(wxT("relativesize"))));
1714 font
.SetStyle(istyle
);
1716 font
.SetWeight(iweight
);
1718 font
.SetUnderlined(underlined
);
1720 font
.SetFamily(ifamily
);
1722 font
.SetFaceName(facename
);
1724 font
.SetDefaultEncoding(enc
);
1726 else // not based on system font
1728 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1729 ifamily
, istyle
, iweight
,
1730 underlined
, facename
, enc
);
1738 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1740 //FIXME : add cursor
1742 if (HasParam(wxT("exstyle")))
1743 // Have to OR it with existing style, since
1744 // some implementations (e.g. wxGTK) use the extra style
1746 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1747 if (HasParam(wxT("bg")))
1748 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1749 if (HasParam(wxT("fg")))
1750 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1751 if (GetBool(wxT("enabled"), 1) == 0)
1753 if (GetBool(wxT("focused"), 0) == 1)
1755 if (GetBool(wxT("hidden"), 0) == 1)
1758 if (HasParam(wxT("tooltip")))
1759 wnd
->SetToolTip(GetText(wxT("tooltip")));
1761 if (HasParam(wxT("font")))
1762 wnd
->SetFont(GetFont());
1763 if (HasParam(wxT("help")))
1764 wnd
->SetHelpText(GetText(wxT("help")));
1768 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1770 for ( wxXmlNode
*n
= m_node
->GetChildren(); n
; n
= n
->GetNext() )
1772 if ( IsObjectNode(n
) )
1774 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1775 this_hnd_only
? this : NULL
);
1781 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1784 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1785 wxXmlNode
*n
= root
->GetChildren();
1789 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1791 CreateResource(n
, parent
, NULL
);
1798 //-----------------------------------------------------------------------------
1800 //-----------------------------------------------------------------------------
1802 void wxXmlResourceHandler::ReportError(const wxString
& message
)
1804 m_resource
->ReportError(m_node
, message
);
1807 void wxXmlResourceHandler::ReportError(wxXmlNode
*context
,
1808 const wxString
& message
)
1810 m_resource
->ReportError(context
? context
: m_node
, message
);
1813 void wxXmlResourceHandler::ReportParamError(const wxString
& param
,
1814 const wxString
& message
)
1816 m_resource
->ReportError(GetParamNode(param
), message
);
1819 void wxXmlResource::ReportError(wxXmlNode
*context
, const wxString
& message
)
1823 DoReportError("", NULL
, message
);
1827 // We need to find out the file that 'context' is part of. Performance of
1828 // this code is not critical, so we simply find the root XML node and
1829 // compare it with all loaded XRC files.
1830 const wxString filename
= GetFileNameFromNode(context
, Data());
1832 DoReportError(filename
, context
, message
);
1835 void wxXmlResource::DoReportError(const wxString
& xrcFile
, wxXmlNode
*position
,
1836 const wxString
& message
)
1838 const int line
= position
? position
->GetLineNumber() : -1;
1841 if ( !xrcFile
.empty() )
1842 loc
= xrcFile
+ ':';
1844 loc
+= wxString::Format("%d:", line
);
1848 wxLogError("XRC error: %s%s", loc
, message
);
1852 //-----------------------------------------------------------------------------
1853 // XRCID implementation
1854 //-----------------------------------------------------------------------------
1856 #define XRCID_TABLE_SIZE 1024
1861 /* Hold the id so that once an id is allocated for a name, it
1862 does not get created again by NewControlId at least
1863 until we are done with it */
1869 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1871 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1875 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1876 index
%= XRCID_TABLE_SIZE
;
1878 XRCID_record
*oldrec
= NULL
;
1879 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1881 if (wxStrcmp(rec
->key
, str_id
) == 0)
1888 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1889 &XRCID_Records
[index
] : &oldrec
->next
;
1890 *rec_var
= new XRCID_record
;
1891 (*rec_var
)->key
= wxStrdup(str_id
);
1892 (*rec_var
)->next
= NULL
;
1895 if (value_if_not_found
!= wxID_NONE
)
1896 (*rec_var
)->id
= value_if_not_found
;
1899 int asint
= wxStrtol(str_id
, &end
, 10);
1900 if (*str_id
&& *end
== 0)
1902 // if str_id was integer, keep it verbosely:
1903 (*rec_var
)->id
= asint
;
1907 (*rec_var
)->id
= wxWindowBase::NewControlId();
1911 return (*rec_var
)->id
;
1914 static void AddStdXRCID_Records();
1917 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
1919 static bool s_stdIDsAdded
= false;
1921 if ( !s_stdIDsAdded
)
1923 s_stdIDsAdded
= true;
1924 AddStdXRCID_Records();
1927 return XRCID_Lookup(str_id
, value_if_not_found
);
1931 wxString
wxXmlResource::FindXRCIDById(int numId
)
1933 for ( int i
= 0; i
< XRCID_TABLE_SIZE
; i
++ )
1935 for ( XRCID_record
*rec
= XRCID_Records
[i
]; rec
; rec
= rec
->next
)
1937 if ( rec
->id
== numId
)
1938 return wxString(rec
->key
);
1945 static void CleanXRCID_Record(XRCID_record
*rec
)
1949 CleanXRCID_Record(rec
->next
);
1956 static void CleanXRCID_Records()
1958 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1960 CleanXRCID_Record(XRCID_Records
[i
]);
1961 XRCID_Records
[i
] = NULL
;
1965 static void AddStdXRCID_Records()
1967 #define stdID(id) XRCID_Lookup(#id, id)
1971 stdID(wxID_SEPARATOR
);
1984 stdID(wxID_PRINT_SETUP
);
1985 stdID(wxID_PAGE_SETUP
);
1986 stdID(wxID_PREVIEW
);
1988 stdID(wxID_HELP_CONTENTS
);
1989 stdID(wxID_HELP_COMMANDS
);
1990 stdID(wxID_HELP_PROCEDURES
);
1991 stdID(wxID_HELP_CONTEXT
);
1992 stdID(wxID_CLOSE_ALL
);
1993 stdID(wxID_PREFERENCES
);
2000 stdID(wxID_DUPLICATE
);
2001 stdID(wxID_SELECTALL
);
2003 stdID(wxID_REPLACE
);
2004 stdID(wxID_REPLACE_ALL
);
2005 stdID(wxID_PROPERTIES
);
2006 stdID(wxID_VIEW_DETAILS
);
2007 stdID(wxID_VIEW_LARGEICONS
);
2008 stdID(wxID_VIEW_SMALLICONS
);
2009 stdID(wxID_VIEW_LIST
);
2010 stdID(wxID_VIEW_SORTDATE
);
2011 stdID(wxID_VIEW_SORTNAME
);
2012 stdID(wxID_VIEW_SORTSIZE
);
2013 stdID(wxID_VIEW_SORTTYPE
);
2029 stdID(wxID_FORWARD
);
2030 stdID(wxID_BACKWARD
);
2031 stdID(wxID_DEFAULT
);
2035 stdID(wxID_CONTEXT_HELP
);
2036 stdID(wxID_YESTOALL
);
2037 stdID(wxID_NOTOALL
);
2046 stdID(wxID_REFRESH
);
2051 stdID(wxID_JUSTIFY_CENTER
);
2052 stdID(wxID_JUSTIFY_FILL
);
2053 stdID(wxID_JUSTIFY_RIGHT
);
2054 stdID(wxID_JUSTIFY_LEFT
);
2055 stdID(wxID_UNDERLINE
);
2057 stdID(wxID_UNINDENT
);
2058 stdID(wxID_ZOOM_100
);
2059 stdID(wxID_ZOOM_FIT
);
2060 stdID(wxID_ZOOM_IN
);
2061 stdID(wxID_ZOOM_OUT
);
2062 stdID(wxID_UNDELETE
);
2063 stdID(wxID_REVERT_TO_SAVED
);
2064 stdID(wxID_SYSTEM_MENU
);
2065 stdID(wxID_CLOSE_FRAME
);
2066 stdID(wxID_MOVE_FRAME
);
2067 stdID(wxID_RESIZE_FRAME
);
2068 stdID(wxID_MAXIMIZE_FRAME
);
2069 stdID(wxID_ICONIZE_FRAME
);
2070 stdID(wxID_RESTORE_FRAME
);
2072 stdID(wxID_CONVERT
);
2073 stdID(wxID_EXECUTE
);
2075 stdID(wxID_HARDDISK
);
2081 stdID(wxID_JUMP_TO
);
2082 stdID(wxID_NETWORK
);
2083 stdID(wxID_SELECT_COLOR
);
2084 stdID(wxID_SELECT_FONT
);
2085 stdID(wxID_SORT_ASCENDING
);
2086 stdID(wxID_SORT_DESCENDING
);
2087 stdID(wxID_SPELL_CHECK
);
2088 stdID(wxID_STRIKETHROUGH
);
2097 //-----------------------------------------------------------------------------
2098 // module and globals
2099 //-----------------------------------------------------------------------------
2101 // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2102 // can happen that some XRC records have been created because of the use of
2103 // XRCID() in event tables, which happens during static objects initialization,
2104 // but then the application initialization failed and so the wx modules were
2105 // neither initialized nor cleaned up -- this static object does the cleanup in
2107 static struct wxXRCStaticCleanup
2109 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2112 class wxXmlResourceModule
: public wxModule
2114 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
2116 wxXmlResourceModule() {}
2119 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
2124 delete wxXmlResource::Set(NULL
);
2125 if(wxXmlResource::ms_subclassFactories
)
2127 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
2128 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
2132 wxDELETE(wxXmlResource::ms_subclassFactories
);
2134 CleanXRCID_Records();
2138 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
2141 // When wxXml is loaded dynamically after the application is already running
2142 // then the built-in module system won't pick this one up. Add it manually.
2143 void wxXmlInitResourceModule()
2145 wxModule
* module = new wxXmlResourceModule
;
2147 wxModule::RegisterModule(module);