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"
50 #include "wx/hashset.h"
53 class wxXmlResourceDataRecord
56 wxXmlResourceDataRecord() : Doc(NULL
) {
58 Time
= wxDateTime::Now();
61 ~wxXmlResourceDataRecord() {delete Doc
;}
70 class wxXmlResourceDataRecords
: public wxVector
<wxXmlResourceDataRecord
*>
72 // this is a class so that it can be forward-declared
75 WX_DECLARE_HASH_SET(int, wxIntegerHash
, wxIntegerEqual
, wxHashSetInt
);
77 class wxIdRange
// Holds data for a particular rangename
80 wxIdRange(const wxXmlNode
* node
,
81 const wxString
& rname
,
82 const wxString
& startno
,
83 const wxString
& rsize
);
85 // Note the existence of an item within the range
86 void NoteItem(const wxXmlNode
* node
, const wxString
& item
);
88 // The manager is telling us that it's finished adding items
89 void Finalise(const wxXmlNode
* node
);
91 wxString
GetName() const { return m_name
; }
92 bool IsFinalised() const { return m_finalised
; }
94 const wxString m_name
;
98 bool m_item_end_found
;
100 wxHashSetInt m_indices
;
102 friend class wxIdRangeManager
;
105 class wxIdRangeManager
109 // Gets the global resources object or creates one if none exists.
110 static wxIdRangeManager
*Get();
112 // Sets the global resources object and returns a pointer to the previous
113 // one (may be NULL).
114 static wxIdRangeManager
*Set(wxIdRangeManager
*res
);
116 // Create a new IDrange from this node
117 void AddRange(const wxXmlNode
* node
);
118 // Tell the IdRange that this item exists, and should be pre-allocated an ID
119 void NotifyRangeOfItem(const wxXmlNode
* node
, const wxString
& item
) const;
120 // Tells all IDranges that they're now complete, and can create their IDs
121 void FinaliseRanges(const wxXmlNode
* node
) const;
122 // Searches for a known IdRange matching 'name', returning its index or -1
123 int Find(const wxString
& rangename
) const;
124 // Removes, if it exists, an entry from the XRCID table. Used in id-ranges
125 // to replace defunct or statically-initialised entries with current values
126 static void RemoveXRCIDEntry(const wxString
& idstr
);
129 wxIdRange
* FindRangeForItem(const wxXmlNode
* node
,
130 const wxString
& item
,
131 wxString
& value
) const;
132 wxVector
<wxIdRange
*> m_IdRanges
;
135 static wxIdRangeManager
*ms_instance
;
141 // helper used by DoFindResource() and elsewhere: returns true if this is an
142 // object or object_ref node
144 // node must be non-NULL
145 inline bool IsObjectNode(wxXmlNode
*node
)
147 return node
->GetType() == wxXML_ELEMENT_NODE
&&
148 (node
->GetName() == wxS("object") ||
149 node
->GetName() == wxS("object_ref"));
152 // special XML attribute with name of input file, see GetFileNameFromNode()
153 const char *ATTR_INPUT_FILENAME
= "__wx:filename";
155 // helper to get filename corresponding to an XML node
157 GetFileNameFromNode(const wxXmlNode
*node
, const wxXmlResourceDataRecords
& files
)
159 // this loop does two things: it looks for ATTR_INPUT_FILENAME among
160 // parents and if it isn't used, it finds the root of the XML tree 'node'
164 // in some rare cases (specifically, when an <object_ref> is used, see
165 // wxXmlResource::CreateResFromNode() and MergeNodesOver()), we work
166 // with XML nodes that are not rooted in any document from 'files'
167 // (because a new node was created by CreateResFromNode() to merge the
168 // content of <object_ref> and the referenced <object>); in that case,
169 // we hack around the problem by putting the information about input
170 // file into a custom attribute
171 if ( node
->HasAttribute(ATTR_INPUT_FILENAME
) )
172 return node
->GetAttribute(ATTR_INPUT_FILENAME
);
174 if ( !node
->GetParent() )
175 break; // we found the root of this XML tree
177 node
= node
->GetParent();
180 // NB: 'node' now points to the root of XML document
182 for ( wxXmlResourceDataRecords::const_iterator i
= files
.begin();
183 i
!= files
.end(); ++i
)
185 if ( (*i
)->Doc
->GetRoot() == node
)
191 return wxEmptyString
; // not found
194 } // anonymous namespace
197 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
199 /*static*/ wxXmlResource
*wxXmlResource::Get()
202 ms_instance
= new wxXmlResource
;
206 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
208 wxXmlResource
*old
= ms_instance
;
213 wxXmlResource::wxXmlResource(int flags
, const wxString
& domain
)
217 m_data
= new wxXmlResourceDataRecords
;
221 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
225 m_data
= new wxXmlResourceDataRecords
;
230 wxXmlResource::~wxXmlResource()
234 for ( wxXmlResourceDataRecords::iterator i
= m_data
->begin();
235 i
!= m_data
->end(); ++i
)
242 void wxXmlResource::SetDomain(const wxString
& domain
)
249 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
251 wxString
fnd(filename
);
253 // NB: as Load() and Unload() accept both filenames and URLs (should
254 // probably be changed to filenames only, but embedded resources
255 // currently rely on its ability to handle URLs - FIXME) we need to
256 // determine whether found name is filename and not URL and this is the
257 // fastest/simplest way to do it
258 if (wxFileName::FileExists(fnd
))
260 // Make the name absolute filename, because the app may
261 // change working directory later:
266 fnd
= fn
.GetFullPath();
269 fnd
= wxFileSystem::FileNameToURL(fnd
);
279 bool wxXmlResource::IsArchive(const wxString
& filename
)
281 const wxString fnd
= filename
.Lower();
283 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
286 #endif // wxUSE_FILESYSTEM
288 bool wxXmlResource::LoadFile(const wxFileName
& file
)
291 return Load(wxFileSystem::FileNameToURL(file
));
293 return Load(file
.GetFullPath());
297 bool wxXmlResource::LoadAllFiles(const wxString
& dirname
)
302 wxDir::GetAllFiles(dirname
, &files
, "*.xrc");
304 for ( wxArrayString::const_iterator i
= files
.begin(); i
!= files
.end(); ++i
)
313 bool wxXmlResource::Load(const wxString
& filemask_
)
315 wxString filemask
= ConvertFileNameToURL(filemask_
);
319 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
320 # define wxXmlFindNext fsys.FindNext()
322 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
323 # define wxXmlFindNext wxFindNextFile()
325 wxString fnd
= wxXmlFindFirst
;
328 wxLogError(_("Cannot load resources from '%s'."), filemask
);
335 if ( IsArchive(fnd
) )
337 if ( !Load(fnd
+ wxT("#zip:*.xrc")) )
340 else // a single resource URL
341 #endif // wxUSE_FILESYSTEM
343 wxXmlResourceDataRecord
*drec
= new wxXmlResourceDataRecord
;
345 Data().push_back(drec
);
350 # undef wxXmlFindFirst
351 # undef wxXmlFindNext
353 return UpdateResources();
356 bool wxXmlResource::Unload(const wxString
& filename
)
358 wxASSERT_MSG( !wxIsWild(filename
),
359 wxT("wildcards not supported by wxXmlResource::Unload()") );
361 wxString fnd
= ConvertFileNameToURL(filename
);
363 const bool isArchive
= IsArchive(fnd
);
366 #endif // wxUSE_FILESYSTEM
368 bool unloaded
= false;
369 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
370 i
!= Data().end(); ++i
)
375 if ( (*i
)->File
.StartsWith(fnd
) )
377 // don't break from the loop, we can have other matching files
379 else // a single resource URL
380 #endif // wxUSE_FILESYSTEM
382 if ( (*i
)->File
== fnd
)
388 // no sense in continuing, there is only one file with this URL
398 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
400 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
402 m_handlers
.push_back(handler
);
403 handler
->SetParentResource(this);
406 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
408 m_handlers
.insert(m_handlers
.begin(), handler
);
409 handler
->SetParentResource(this);
414 void wxXmlResource::ClearHandlers()
416 for ( wxVector
<wxXmlResourceHandler
*>::iterator i
= m_handlers
.begin();
417 i
!= m_handlers
.end(); ++i
)
423 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
425 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
430 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
432 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
438 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
440 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
445 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
447 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
450 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
452 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
457 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
459 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
462 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
464 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
467 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
469 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
472 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
474 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
477 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
479 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
480 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
483 if (bmp
) { rt
= *bmp
; delete bmp
; }
487 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
489 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
490 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
493 if (icon
) { rt
= *icon
; delete icon
; }
499 wxXmlResource::DoLoadObject(wxWindow
*parent
,
500 const wxString
& name
,
501 const wxString
& classname
,
504 wxXmlNode
* const node
= FindResource(name
, classname
, recursive
);
506 return node
? DoCreateResFromNode(*node
, parent
, NULL
) : NULL
;
510 wxXmlResource::DoLoadObject(wxObject
*instance
,
512 const wxString
& name
,
513 const wxString
& classname
,
516 wxXmlNode
* const node
= FindResource(name
, classname
, recursive
);
518 return node
&& DoCreateResFromNode(*node
, parent
, instance
) != NULL
;
522 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
523 wxWindow
*control
, wxWindow
*parent
)
526 parent
= control
->GetParent();
527 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
530 wxLogError("Cannot find container for unknown control '%s'.", name
);
533 return control
->Reparent(container
);
537 static void ProcessPlatformProperty(wxXmlNode
*node
)
542 wxXmlNode
*c
= node
->GetChildren();
546 if (!c
->GetAttribute(wxT("platform"), &s
))
550 wxStringTokenizer
tkn(s
, wxT(" |"));
552 while (tkn
.HasMoreTokens())
554 s
= tkn
.GetNextToken();
556 if (s
== wxT("win")) isok
= true;
558 #if defined(__MAC__) || defined(__APPLE__)
559 if (s
== wxT("mac")) isok
= true;
560 #elif defined(__UNIX__)
561 if (s
== wxT("unix")) isok
= true;
564 if (s
== wxT("os2")) isok
= true;
574 ProcessPlatformProperty(c
);
579 wxXmlNode
*c2
= c
->GetNext();
580 node
->RemoveChild(c
);
587 static void PreprocessForIdRanges(wxXmlNode
*rootnode
)
589 // First go through the top level, looking for the names of ID ranges
590 // as processing items is a lot easier if names are already known
591 wxXmlNode
*c
= rootnode
->GetChildren();
594 if (c
->GetName() == wxT("ids-range"))
595 wxIdRangeManager::Get()->AddRange(c
);
599 // Next, examine every 'name' for the '[' that denotes an ID in a range
600 c
= rootnode
->GetChildren();
603 wxString name
= c
->GetAttribute(wxT("name"));
604 if (name
.find('[') != wxString::npos
)
605 wxIdRangeManager::Get()->NotifyRangeOfItem(rootnode
, name
);
607 // Do any children by recursion, then proceed to the next sibling
608 PreprocessForIdRanges(c
);
613 bool wxXmlResource::UpdateResources()
617 # if wxUSE_FILESYSTEM
618 wxFSFile
*file
= NULL
;
623 wxString
encoding(wxT("UTF-8"));
624 #if !wxUSE_UNICODE && wxUSE_INTL
625 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
627 // In case we are not using wxLocale to translate strings, convert the
628 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
629 // is on, because it could break wxGetTranslation lookup.
630 encoding
= wxLocale::GetSystemEncodingName();
634 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
635 i
!= Data().end(); ++i
)
637 wxXmlResourceDataRecord
* const rec
= *i
;
639 modif
= (rec
->Doc
== NULL
);
641 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
643 # if wxUSE_FILESYSTEM
644 file
= fsys
.OpenFile(rec
->File
);
646 modif
= file
&& file
->GetModificationTime() > rec
->Time
;
647 # else // wxUSE_DATETIME
649 # endif // wxUSE_DATETIME
652 wxLogError(_("Cannot open file '%s'."), rec
->File
);
657 # else // wxUSE_FILESYSTEM
659 modif
= wxDateTime(wxFileModificationTime(rec
->File
)) > rec
->Time
;
660 # else // wxUSE_DATETIME
662 # endif // wxUSE_DATETIME
663 # endif // wxUSE_FILESYSTEM
668 wxLogTrace(wxT("xrc"), wxT("opening file '%s'"), rec
->File
);
670 wxInputStream
*stream
= NULL
;
672 # if wxUSE_FILESYSTEM
673 file
= fsys
.OpenFile(rec
->File
);
675 stream
= file
->GetStream();
677 stream
= new wxFileInputStream(rec
->File
);
683 rec
->Doc
= new wxXmlDocument
;
685 if (!stream
|| !stream
->IsOk() || !rec
->Doc
->Load(*stream
, encoding
))
687 wxLogError(_("Cannot load resources from file '%s'."),
692 else if (rec
->Doc
->GetRoot()->GetName() != wxT("resource"))
697 "invalid XRC resource, doesn't have root node <resource>"
706 wxString verstr
= rec
->Doc
->GetRoot()->GetAttribute(
707 wxT("version"), wxT("0.0.0.0"));
708 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
709 &v1
, &v2
, &v3
, &v4
) == 4)
710 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
715 if (m_version
!= version
)
717 wxLogError("Resource files must have same version number.");
721 ProcessPlatformProperty(rec
->Doc
->GetRoot());
722 PreprocessForIdRanges(rec
->Doc
->GetRoot());
723 wxIdRangeManager::Get()->FinaliseRanges(rec
->Doc
->GetRoot());
726 rec
->Time
= file
->GetModificationTime();
727 #else // wxUSE_FILESYSTEM
728 rec
->Time
= wxDateTime(wxFileModificationTime(rec
->File
));
729 #endif // wxUSE_FILESYSTEM
730 #endif // wxUSE_DATETIME
733 # if wxUSE_FILESYSTEM
745 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
746 const wxString
& name
,
747 const wxString
& classname
,
748 bool recursive
) const
752 // first search for match at the top-level nodes (as this is
753 // where the resource is most commonly looked for):
754 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
756 if ( IsObjectNode(node
) && node
->GetAttribute(wxS("name")) == name
)
758 // empty class name matches everything
759 if ( classname
.empty() )
762 wxString
cls(node
->GetAttribute(wxS("class")));
764 // object_ref may not have 'class' attribute:
765 if (cls
.empty() && node
->GetName() == wxS("object_ref"))
767 wxString refName
= node
->GetAttribute(wxS("ref"));
771 const wxXmlNode
* const refNode
= GetResourceNode(refName
);
773 cls
= refNode
->GetAttribute(wxS("class"));
776 if ( cls
== classname
)
781 // then recurse in child nodes
784 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
786 if ( IsObjectNode(node
) )
788 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
798 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
799 const wxString
& classname
,
804 node
= GetResourceNodeAndLocation(name
, classname
, recursive
, &path
);
813 "XRC resource \"%s\" (class \"%s\") not found",
819 else // node was found
821 // ensure that relative paths work correctly when loading this node
822 // (which should happen as soon as we return as FindResource() result
823 // is always passed to CreateResFromNode())
824 m_curFileSystem
.ChangePathTo(path
);
826 #endif // wxUSE_FILESYSTEM
832 wxXmlResource::GetResourceNodeAndLocation(const wxString
& name
,
833 const wxString
& classname
,
835 wxString
*path
) const
837 // ensure everything is up-to-date: this is needed to support on-demand
838 // reloading of XRC files
839 const_cast<wxXmlResource
*>(this)->UpdateResources();
841 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
842 f
!= Data().end(); ++f
)
844 wxXmlResourceDataRecord
*const rec
= *f
;
845 wxXmlDocument
* const doc
= rec
->Doc
;
846 if ( !doc
|| !doc
->GetRoot() )
850 found
= DoFindResource(doc
->GetRoot(), name
, classname
, recursive
);
863 static void MergeNodesOver(wxXmlNode
& dest
, wxXmlNode
& overwriteWith
,
864 const wxString
& overwriteFilename
)
867 for ( wxXmlAttribute
*attr
= overwriteWith
.GetAttributes();
868 attr
; attr
= attr
->GetNext() )
870 wxXmlAttribute
*dattr
;
871 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
874 if ( dattr
->GetName() == attr
->GetName() )
876 dattr
->SetValue(attr
->GetValue());
882 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
885 // Merge child nodes:
886 for (wxXmlNode
* node
= overwriteWith
.GetChildren(); node
; node
= node
->GetNext())
888 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
891 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
893 if ( dnode
->GetName() == node
->GetName() &&
894 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
895 dnode
->GetType() == node
->GetType() )
897 MergeNodesOver(*dnode
, *node
, overwriteFilename
);
904 wxXmlNode
*copyOfNode
= new wxXmlNode(*node
);
905 // remember referenced object's file, see GetFileNameFromNode()
906 copyOfNode
->AddAttribute(ATTR_INPUT_FILENAME
, overwriteFilename
);
908 static const wxChar
*AT_END
= wxT("end");
909 wxString insert_pos
= node
->GetAttribute(wxT("insert_at"), AT_END
);
910 if ( insert_pos
== AT_END
)
912 dest
.AddChild(copyOfNode
);
914 else if ( insert_pos
== wxT("begin") )
916 dest
.InsertChild(copyOfNode
, dest
.GetChildren());
921 if ( dest
.GetType() == wxXML_TEXT_NODE
&& overwriteWith
.GetContent().length() )
922 dest
.SetContent(overwriteWith
.GetContent());
926 wxXmlResource::DoCreateResFromNode(wxXmlNode
& node
,
929 wxXmlResourceHandler
*handlerToUse
)
931 // handling of referenced resource
932 if ( node
.GetName() == wxT("object_ref") )
934 wxString refName
= node
.GetAttribute(wxT("ref"), wxEmptyString
);
935 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
944 "referenced object node with ref=\"%s\" not found",
951 if ( !node
.GetChildren() )
953 // In the typical, simple case, <object_ref> is used to link
954 // to another node and doesn't have any content of its own that
955 // would overwrite linked object's properties. In this case,
956 // we can simply create the resource from linked node.
958 return DoCreateResFromNode(*refNode
, parent
, instance
);
962 // In the more complicated (but rare) case, <object_ref> has
963 // subnodes that partially overwrite content of the referenced
964 // object. In this case, we need to merge both XML trees and
965 // load the resource from result of the merge.
967 wxXmlNode
copy(*refNode
);
968 MergeNodesOver(copy
, node
, GetFileNameFromNode(&node
, Data()));
970 // remember referenced object's file, see GetFileNameFromNode()
971 copy
.AddAttribute(ATTR_INPUT_FILENAME
,
972 GetFileNameFromNode(refNode
, Data()));
974 return DoCreateResFromNode(copy
, parent
, instance
);
980 if (handlerToUse
->CanHandle(&node
))
982 return handlerToUse
->CreateResource(&node
, parent
, instance
);
985 else if (node
.GetName() == wxT("object"))
987 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
988 h
!= m_handlers
.end(); ++h
)
990 wxXmlResourceHandler
*handler
= *h
;
991 if (handler
->CanHandle(&node
))
992 return handler
->CreateResource(&node
, parent
, instance
);
1001 "no handler found for XML node \"%s\" (class \"%s\")",
1003 node
.GetAttribute("class", wxEmptyString
)
1009 wxIdRange::wxIdRange(const wxXmlNode
* node
,
1010 const wxString
& rname
,
1011 const wxString
& startno
,
1012 const wxString
& rsize
)
1016 m_item_end_found(0),
1020 if ( startno
.ToLong(&l
) )
1028 wxXmlResource::Get()->ReportError
1031 "a negative id-range start parameter was given"
1037 wxXmlResource::Get()->ReportError
1040 "the id-range start parameter was malformed"
1045 if ( rsize
.ToULong(&ul
) )
1051 wxXmlResource::Get()->ReportError
1054 "the id-range size parameter was malformed"
1059 void wxIdRange::NoteItem(const wxXmlNode
* node
, const wxString
& item
)
1061 // Nothing gets added here, but the existence of each item is noted
1062 // thus getting an accurate count. 'item' will be either an integer e.g.
1063 // [0] [123]: will eventually create an XRCID as start+integer or [start]
1064 // or [end] which are synonyms for [0] or [range_size-1] respectively.
1065 wxString
content(item
.Mid(1, item
.length()-2));
1067 // Check that basename+item wasn't foo[]
1068 if (content
.empty())
1070 wxXmlResource::Get()->ReportError(node
, "an empty id-range item found");
1074 if (content
=="start")
1076 // "start" means [0], so store that in the set
1077 if (m_indices
.count(0) == 0)
1079 m_indices
.insert(0);
1083 wxXmlResource::Get()->ReportError
1086 "duplicate id-range item found"
1090 else if (content
=="end")
1092 // We can't yet be certain which XRCID this will be equivalent to, so
1093 // just note that there's an item with this name, in case we need to
1094 // inc the range size
1095 m_item_end_found
= true;
1099 // Anything else will be an integer, or rubbish
1101 if ( content
.ToULong(&l
) )
1103 if (m_indices
.count(l
) == 0)
1105 m_indices
.insert(l
);
1106 // Check that this item wouldn't fall outside the current range
1115 wxXmlResource::Get()->ReportError
1118 "duplicate id-range item found"
1125 wxXmlResource::Get()->ReportError
1128 "an id-range item had a malformed index"
1134 void wxIdRange::Finalise(const wxXmlNode
* node
)
1136 wxCHECK_RET( !IsFinalised(),
1137 "Trying to finalise an already-finalised range" );
1139 // Now we know about all the items, we can get an accurate range size
1140 // Expand any requested range-size if there were more items than would fit
1141 m_size
= wxMax(m_size
, m_indices
.size());
1143 // If an item is explicitly called foo[end], ensure it won't clash with
1145 if ( m_item_end_found
&& m_indices
.count(m_size
-1) )
1149 // This will happen if someone creates a range but no items in this xrc
1150 // file Report the error and abort, but don't finalise, in case items
1152 wxXmlResource::Get()->ReportError
1155 "trying to create an empty id-range"
1162 // This is the usual case, where the user didn't specify a start ID
1163 // So get the range using NewControlId().
1165 // NB: negative numbers, but NewControlId already returns the most
1167 m_start
= wxWindow::NewControlId(m_size
);
1168 wxCHECK_RET( m_start
!= wxID_NONE
,
1169 "insufficient IDs available to create range" );
1170 m_end
= m_start
+ m_size
- 1;
1174 // The user already specified a start value, which must be positive
1175 m_end
= m_start
+ m_size
- 1;
1178 // Create the XRCIDs
1179 for (int i
=m_start
; i
<= m_end
; ++i
)
1181 // First clear any pre-existing XRCID
1182 // Necessary for wxXmlResource::Unload() followed by Load()
1183 wxIdRangeManager::RemoveXRCIDEntry(
1184 m_name
+ wxString::Format("[%i]", i
-m_start
));
1186 // Use the second parameter of GetXRCID to force it to take the value i
1187 wxXmlResource::GetXRCID(m_name
+ wxString::Format("[%i]", i
-m_start
), i
);
1188 wxLogTrace("xrcrange",
1189 "integer = %i %s now returns %i",
1191 m_name
+ wxString::Format("[%i]", i
-m_start
),
1192 XRCID((m_name
+ wxString::Format("[%i]", i
-m_start
)).mb_str()));
1194 // and these special ones
1195 wxIdRangeManager::RemoveXRCIDEntry(m_name
+ "[start]");
1196 wxXmlResource::GetXRCID(m_name
+ "[start]", m_start
);
1197 wxIdRangeManager::RemoveXRCIDEntry(m_name
+ "[end]");
1198 wxXmlResource::GetXRCID(m_name
+ "[end]", m_end
);
1199 wxLogTrace("xrcrange","%s[start] = %i %s[end] = %i",
1200 m_name
.mb_str(),XRCID(wxString(m_name
+"[start]").mb_str()),
1201 m_name
.mb_str(),XRCID(wxString(m_name
+"[end]").mb_str()));
1206 wxIdRangeManager
*wxIdRangeManager::ms_instance
= NULL
;
1208 /*static*/ wxIdRangeManager
*wxIdRangeManager::Get()
1211 ms_instance
= new wxIdRangeManager
;
1215 /*static*/ wxIdRangeManager
*wxIdRangeManager::Set(wxIdRangeManager
*res
)
1217 wxIdRangeManager
*old
= ms_instance
;
1222 wxIdRangeManager::~wxIdRangeManager()
1224 for ( wxVector
<wxIdRange
*>::iterator i
= m_IdRanges
.begin();
1225 i
!= m_IdRanges
.end(); ++i
)
1234 void wxIdRangeManager::AddRange(const wxXmlNode
* node
)
1236 wxString name
= node
->GetAttribute("name");
1237 wxString start
= node
->GetAttribute("start", "0");
1238 wxString size
= node
->GetAttribute("size", "0");
1241 wxXmlResource::Get()->ReportError
1244 "xrc file contains an id-range without a name"
1249 int index
= Find(name
);
1250 if (index
== wxNOT_FOUND
)
1252 wxLogTrace("xrcrange",
1253 "Adding ID range, name=%s start=%s size=%s",
1256 m_IdRanges
.push_back(new wxIdRange(node
, name
, start
, size
));
1260 // There was already a range with this name. Let's hope this is
1261 // from an Unload()/(re)Load(), not an unintentional duplication
1262 wxLogTrace("xrcrange",
1263 "Replacing ID range, name=%s start=%s size=%s",
1266 wxIdRange
* oldrange
= m_IdRanges
.at(index
);
1267 m_IdRanges
.at(index
) = new wxIdRange(node
, name
, start
, size
);
1273 wxIdRangeManager::FindRangeForItem(const wxXmlNode
* node
,
1274 const wxString
& item
,
1275 wxString
& value
) const
1277 wxString basename
= item
.BeforeFirst('[');
1278 wxCHECK_MSG( !basename
.empty(), NULL
,
1279 "an id-range item without a range name" );
1281 int index
= Find(basename
);
1282 if (index
== wxNOT_FOUND
)
1284 // Don't assert just because we've found an unexpected foo[123]
1285 // Someone might just want such a name, nothing to do with ranges
1289 value
= item
.Mid(basename
.Len());
1290 if (value
.at(value
.length()-1)==']')
1292 return m_IdRanges
.at(index
);
1294 wxXmlResource::Get()->ReportError(node
, "a malformed id-range item");
1299 wxIdRangeManager::NotifyRangeOfItem(const wxXmlNode
* node
,
1300 const wxString
& item
) const
1303 wxIdRange
* range
= FindRangeForItem(node
, item
, value
);
1305 range
->NoteItem(node
, value
);
1308 int wxIdRangeManager::Find(const wxString
& rangename
) const
1310 for ( int i
=0; i
< (int)m_IdRanges
.size(); ++i
)
1312 if (m_IdRanges
.at(i
)->GetName() == rangename
)
1319 void wxIdRangeManager::FinaliseRanges(const wxXmlNode
* node
) const
1321 for ( wxVector
<wxIdRange
*>::const_iterator i
= m_IdRanges
.begin();
1322 i
!= m_IdRanges
.end(); ++i
)
1324 // Check if this range has already been finalised. Quite possible,
1325 // as FinaliseRanges() gets called for each .xrc file loaded
1326 if (!(*i
)->IsFinalised())
1328 wxLogTrace("xrcrange", "Finalising ID range %s", (*i
)->GetName());
1329 (*i
)->Finalise(node
);
1335 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
1337 // this is a class so that it can be forward-declared
1340 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
1342 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
1344 if (!ms_subclassFactories
)
1346 ms_subclassFactories
= new wxXmlSubclassFactories
;
1348 ms_subclassFactories
->push_back(factory
);
1351 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
1354 ~wxXmlSubclassFactoryCXX() {}
1356 wxObject
*Create(const wxString
& className
)
1358 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
1361 return classInfo
->CreateObject();
1370 wxXmlResourceHandler::wxXmlResourceHandler()
1371 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
1372 m_parentAsWindow(NULL
)
1377 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
1379 wxXmlNode
*myNode
= m_node
;
1380 wxString myClass
= m_class
;
1381 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
1382 wxWindow
*myParentAW
= m_parentAsWindow
;
1384 m_instance
= instance
;
1385 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
1386 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
1388 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
1389 if (!subclass
.empty())
1391 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
1392 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
1394 m_instance
= (*i
)->Create(subclass
);
1401 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
1407 "subclass \"%s\" not found for resource \"%s\", not subclassing",
1416 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
1418 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
1420 wxObject
*returned
= DoCreateResource();
1424 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
1425 m_instance
= myInstance
;
1431 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
1433 m_styleNames
.Add(name
);
1434 m_styleValues
.Add(value
);
1439 void wxXmlResourceHandler::AddWindowStyles()
1441 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
1443 // the border styles all have the old and new names, recognize both for now
1444 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
1445 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
1446 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
); // deprecated
1447 XRC_ADD_STYLE(wxBORDER_THEME
);
1448 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
1449 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
1450 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
1452 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
1453 XRC_ADD_STYLE(wxWANTS_CHARS
);
1454 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
1455 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
1456 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
1457 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
1458 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
1459 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
1464 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
1466 return (GetParamNode(param
) != NULL
);
1470 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
1472 wxString s
= GetParamValue(param
);
1474 if (!s
) return defaults
;
1476 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
1480 while (tkn
.HasMoreTokens())
1482 fl
= tkn
.GetNextToken();
1483 index
= m_styleNames
.Index(fl
);
1484 if (index
!= wxNOT_FOUND
)
1486 style
|= m_styleValues
[index
];
1493 wxString::Format("unknown style flag \"%s\"", fl
)
1502 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
1504 wxXmlNode
*parNode
= GetParamNode(param
);
1505 wxString
str1(GetNodeContent(parNode
));
1508 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1509 const bool escapeBackslash
= (m_resource
->CompareVersion(2,5,3,0) >= 0);
1511 // VS: First version of XRC resources used $ instead of & (which is
1512 // illegal in XML), but later I realized that '_' fits this purpose
1513 // much better (because &File means "File with F underlined").
1514 const wxChar amp_char
= (m_resource
->CompareVersion(2,3,0,1) < 0)
1517 for ( wxString::const_iterator dt
= str1
.begin(); dt
!= str1
.end(); ++dt
)
1519 // Remap amp_char to &, map double amp_char to amp_char (for things
1520 // like "&File..." -- this is illegal in XML, so we use "_File..."):
1521 if ( *dt
== amp_char
)
1523 if ( *(++dt
) == amp_char
)
1526 str2
<< wxT('&') << *dt
;
1528 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
1529 else if ( *dt
== wxT('\\') )
1531 switch ( (*(++dt
)).GetValue() )
1546 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1547 if ( escapeBackslash
)
1552 // else fall-through to default: branch below
1555 str2
<< wxT('\\') << *dt
;
1565 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
1567 if (translate
&& parNode
&&
1568 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
1570 return wxGetTranslation(str2
, m_resource
->GetDomain());
1577 // The string is internally stored as UTF-8, we have to convert
1578 // it into system's default encoding so that it can be displayed:
1579 return wxString(str2
.wc_str(wxConvUTF8
), wxConvLocal
);
1584 // If wxXRC_USE_LOCALE is not set, then the string is already in
1585 // system's default encoding in ANSI build, so we don't have to
1586 // do anything special here.
1592 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
1595 wxString str1
= GetParamValue(param
);
1597 if (!str1
.ToLong(&value
))
1603 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
1605 wxString str
= GetParamValue(param
);
1607 // strings in XRC always use C locale so make sure to use the
1608 // locale-independent wxString::ToCDouble() and not ToDouble() which uses
1609 // the current locale with a potentially different decimal point character
1611 if (!str
.ToCDouble(&value
))
1614 return wx_truncate_cast(float, value
);
1618 int wxXmlResourceHandler::GetID()
1620 return wxXmlResource::GetXRCID(GetName());
1625 wxString
wxXmlResourceHandler::GetName()
1627 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1632 bool wxXmlResourceHandler::GetBoolAttr(const wxString
& attr
, bool defaultv
)
1635 return m_node
->GetAttribute(attr
, &v
) ? v
== '1' : defaultv
;
1638 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1640 const wxString v
= GetParamValue(param
);
1642 return v
.empty() ? defaultv
: (v
== '1');
1646 static wxColour
GetSystemColour(const wxString
& name
)
1650 #define SYSCLR(clr) \
1651 if (name == wxT(#clr)) return wxSystemSettings::GetColour(clr);
1652 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1653 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1654 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1655 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1656 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1657 SYSCLR(wxSYS_COLOUR_MENU
)
1658 SYSCLR(wxSYS_COLOUR_WINDOW
)
1659 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1660 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1661 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1662 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1663 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1664 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1665 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1666 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1667 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1668 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1669 SYSCLR(wxSYS_COLOUR_3DFACE
)
1670 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1671 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1672 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1673 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1674 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1675 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1676 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1677 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1678 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1679 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1680 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1681 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1682 SYSCLR(wxSYS_COLOUR_INFOBK
)
1683 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1684 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1685 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1686 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1687 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1688 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1692 return wxNullColour
;
1695 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1697 wxString v
= GetParamValue(param
);
1704 // wxString -> wxColour conversion
1707 // the colour doesn't use #RRGGBB format, check if it is symbolic
1709 clr
= GetSystemColour(v
);
1716 wxString::Format("incorrect colour specification \"%s\"", v
)
1718 return wxNullColour
;
1727 // if 'param' has stock_id/stock_client, extracts them and returns true
1728 bool GetStockArtAttrs(const wxXmlNode
*paramNode
,
1729 const wxString
& defaultArtClient
,
1730 wxString
& art_id
, wxString
& art_client
)
1734 art_id
= paramNode
->GetAttribute("stock_id", "");
1736 if ( !art_id
.empty() )
1738 art_id
= wxART_MAKE_ART_ID_FROM_STR(art_id
);
1740 art_client
= paramNode
->GetAttribute("stock_client", "");
1741 if ( art_client
.empty() )
1742 art_client
= defaultArtClient
;
1744 art_client
= wxART_MAKE_CLIENT_ID_FROM_STR(art_client
);
1753 } // anonymous namespace
1755 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1756 const wxArtClient
& defaultArtClient
,
1759 // it used to be possible to pass an empty string here to indicate that the
1760 // bitmap name should be read from this node itself but this is not
1761 // supported any more because GetBitmap(m_node) can be used directly
1763 wxASSERT_MSG( !param
.empty(), "bitmap parameter name can't be empty" );
1765 const wxXmlNode
* const node
= GetParamNode(param
);
1769 // this is not an error as bitmap parameter could be optional
1770 return wxNullBitmap
;
1773 return GetBitmap(node
, defaultArtClient
, size
);
1776 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxXmlNode
* node
,
1777 const wxArtClient
& defaultArtClient
,
1780 wxCHECK_MSG( node
, wxNullBitmap
, "bitmap node can't be NULL" );
1782 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1783 wxString art_id
, art_client
;
1784 if ( GetStockArtAttrs(node
, defaultArtClient
,
1785 art_id
, art_client
) )
1787 wxBitmap
stockArt(wxArtProvider::GetBitmap(art_id
, art_client
, size
));
1788 if ( stockArt
.Ok() )
1792 /* ...or load the bitmap from file: */
1793 wxString name
= GetParamValue(node
);
1794 if (name
.empty()) return wxNullBitmap
;
1795 #if wxUSE_FILESYSTEM
1796 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1802 wxString::Format("cannot open bitmap resource \"%s\"", name
)
1804 return wxNullBitmap
;
1806 wxImage
img(*(fsfile
->GetStream()));
1817 wxString::Format("cannot create bitmap from \"%s\"", name
)
1819 return wxNullBitmap
;
1821 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1822 return wxBitmap(img
);
1826 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1827 const wxArtClient
& defaultArtClient
,
1830 // see comment in GetBitmap(wxString) overload
1831 wxASSERT_MSG( !param
.empty(), "icon parameter name can't be empty" );
1833 const wxXmlNode
* const node
= GetParamNode(param
);
1837 // this is not an error as icon parameter could be optional
1841 return GetIcon(node
, defaultArtClient
, size
);
1844 wxIcon
wxXmlResourceHandler::GetIcon(const wxXmlNode
* node
,
1845 const wxArtClient
& defaultArtClient
,
1849 icon
.CopyFromBitmap(GetBitmap(node
, defaultArtClient
, size
));
1854 wxIconBundle
wxXmlResourceHandler::GetIconBundle(const wxString
& param
,
1855 const wxArtClient
& defaultArtClient
)
1857 wxString art_id
, art_client
;
1858 if ( GetStockArtAttrs(GetParamNode(param
), defaultArtClient
,
1859 art_id
, art_client
) )
1861 wxIconBundle
stockArt(wxArtProvider::GetIconBundle(art_id
, art_client
));
1862 if ( stockArt
.IsOk() )
1866 const wxString name
= GetParamValue(param
);
1868 return wxNullIconBundle
;
1870 #if wxUSE_FILESYSTEM
1871 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1872 if ( fsfile
== NULL
)
1877 wxString::Format("cannot open icon resource \"%s\"", name
)
1879 return wxNullIconBundle
;
1882 wxIconBundle
bundle(*(fsfile
->GetStream()));
1885 wxIconBundle
bundle(name
);
1888 if ( !bundle
.IsOk() )
1893 wxString::Format("cannot create icon from \"%s\"", name
)
1895 return wxNullIconBundle
;
1902 wxImageList
*wxXmlResourceHandler::GetImageList(const wxString
& param
)
1904 wxXmlNode
* const imagelist_node
= GetParamNode(param
);
1905 if ( !imagelist_node
)
1908 wxXmlNode
* const oldnode
= m_node
;
1909 m_node
= imagelist_node
;
1911 // Get the size if we have it, otherwise we will use the size of the first
1913 wxSize size
= GetSize();
1915 // Start adding images, we'll create the image list when adding the first
1917 wxImageList
* imagelist
= NULL
;
1918 wxString parambitmap
= wxT("bitmap");
1919 if ( HasParam(parambitmap
) )
1921 wxXmlNode
*n
= m_node
->GetChildren();
1924 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == parambitmap
)
1926 wxIcon icon
= GetIcon(n
);
1929 // We need the real image list size to create it.
1930 if ( size
== wxDefaultSize
)
1931 size
= icon
.GetSize();
1933 // We use the mask by default.
1934 bool mask
= !HasParam(wxS("mask")) || GetBool(wxS("mask"));
1936 imagelist
= new wxImageList(size
.x
, size
.y
, mask
);
1939 // add icon instead of bitmap to keep the bitmap mask
1940 imagelist
->Add(icon
);
1950 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1952 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1954 wxXmlNode
*n
= m_node
->GetChildren();
1958 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1960 // TODO: check that there are no other properties/parameters with
1961 // the same name and log an error if there are (can't do this
1962 // right now as I'm not sure if it's not going to break code
1963 // using this function in unintentional way (i.e. for
1964 // accessing other things than properties), for example
1965 // wxBitmapComboBoxXmlHandler almost surely does
1974 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1976 return node
->GetAttribute(wxT("class")) == classname
;
1981 wxString
wxXmlResourceHandler::GetNodeContent(const wxXmlNode
*node
)
1983 const wxXmlNode
*n
= node
;
1984 if (n
== NULL
) return wxEmptyString
;
1985 n
= n
->GetChildren();
1989 if (n
->GetType() == wxXML_TEXT_NODE
||
1990 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1991 return n
->GetContent();
1994 return wxEmptyString
;
1999 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
2002 return GetNodeContent(m_node
);
2004 return GetNodeContent(GetParamNode(param
));
2007 wxString
wxXmlResourceHandler::GetParamValue(const wxXmlNode
* node
)
2009 return GetNodeContent(node
);
2013 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
2014 wxWindow
*windowToUse
)
2016 wxString s
= GetParamValue(param
);
2017 if (s
.empty()) s
= wxT("-1,-1");
2021 is_dlg
= s
[s
.length()-1] == wxT('d');
2022 if (is_dlg
) s
.RemoveLast();
2024 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
2025 !s
.AfterLast(wxT(',')).ToLong(&sy
))
2030 wxString::Format("cannot parse coordinates value \"%s\"", s
)
2032 return wxDefaultSize
;
2039 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
2041 else if (m_parentAsWindow
)
2043 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
2050 "cannot convert dialog units: dialog unknown"
2052 return wxDefaultSize
;
2056 return wxSize(sx
, sy
);
2061 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
2063 wxSize sz
= GetSize(param
);
2064 return wxPoint(sz
.x
, sz
.y
);
2069 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
2071 wxWindow
*windowToUse
)
2073 wxString s
= GetParamValue(param
);
2074 if (s
.empty()) return defaultv
;
2078 is_dlg
= s
[s
.length()-1] == wxT('d');
2079 if (is_dlg
) s
.RemoveLast();
2086 wxString::Format("cannot parse dimension value \"%s\"", s
)
2095 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
2097 else if (m_parentAsWindow
)
2099 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
2106 "cannot convert dialog units: dialog unknown"
2116 // Get system font index using indexname
2117 static wxFont
GetSystemFont(const wxString
& name
)
2121 #define SYSFNT(fnt) \
2122 if (name == wxT(#fnt)) return wxSystemSettings::GetFont(fnt);
2123 SYSFNT(wxSYS_OEM_FIXED_FONT
)
2124 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
2125 SYSFNT(wxSYS_ANSI_VAR_FONT
)
2126 SYSFNT(wxSYS_SYSTEM_FONT
)
2127 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
2128 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
2129 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
2136 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
2138 wxXmlNode
*font_node
= GetParamNode(param
);
2139 if (font_node
== NULL
)
2142 wxString::Format("cannot find font node \"%s\"", param
));
2146 wxXmlNode
*oldnode
= m_node
;
2153 bool hasSize
= HasParam(wxT("size"));
2155 isize
= GetLong(wxT("size"), -1);
2158 int istyle
= wxNORMAL
;
2159 bool hasStyle
= HasParam(wxT("style"));
2162 wxString style
= GetParamValue(wxT("style"));
2163 if (style
== wxT("italic"))
2165 else if (style
== wxT("slant"))
2170 int iweight
= wxNORMAL
;
2171 bool hasWeight
= HasParam(wxT("weight"));
2174 wxString weight
= GetParamValue(wxT("weight"));
2175 if (weight
== wxT("bold"))
2177 else if (weight
== wxT("light"))
2182 bool hasUnderlined
= HasParam(wxT("underlined"));
2183 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
2185 // family and facename
2186 int ifamily
= wxDEFAULT
;
2187 bool hasFamily
= HasParam(wxT("family"));
2190 wxString family
= GetParamValue(wxT("family"));
2191 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
2192 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
2193 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
2194 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
2195 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
2196 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
2201 bool hasFacename
= HasParam(wxT("face"));
2204 wxString faces
= GetParamValue(wxT("face"));
2205 wxStringTokenizer
tk(faces
, wxT(","));
2207 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
2208 while (tk
.HasMoreTokens())
2210 int index
= facenames
.Index(tk
.GetNextToken(), false);
2211 if (index
!= wxNOT_FOUND
)
2213 facename
= facenames
[index
];
2217 #else // !wxUSE_FONTENUM
2218 // just use the first face name if we can't check its availability:
2219 if (tk
.HasMoreTokens())
2220 facename
= tk
.GetNextToken();
2221 #endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
2225 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
2226 bool hasEncoding
= HasParam(wxT("encoding"));
2230 wxString encoding
= GetParamValue(wxT("encoding"));
2231 wxFontMapper mapper
;
2232 if (!encoding
.empty())
2233 enc
= mapper
.CharsetToEncoding(encoding
);
2234 if (enc
== wxFONTENCODING_SYSTEM
)
2235 enc
= wxFONTENCODING_DEFAULT
;
2237 #endif // wxUSE_FONTMAP
2239 // is this font based on a system font?
2240 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
2244 if (hasSize
&& isize
!= -1)
2245 font
.SetPointSize(isize
);
2246 else if (HasParam(wxT("relativesize")))
2247 font
.SetPointSize(int(font
.GetPointSize() *
2248 GetFloat(wxT("relativesize"))));
2251 font
.SetStyle(istyle
);
2253 font
.SetWeight(iweight
);
2255 font
.SetUnderlined(underlined
);
2257 font
.SetFamily(ifamily
);
2259 font
.SetFaceName(facename
);
2261 font
.SetDefaultEncoding(enc
);
2263 else // not based on system font
2265 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
2266 ifamily
, istyle
, iweight
,
2267 underlined
, facename
, enc
);
2275 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
2277 //FIXME : add cursor
2279 if (HasParam(wxT("exstyle")))
2280 // Have to OR it with existing style, since
2281 // some implementations (e.g. wxGTK) use the extra style
2283 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
2284 if (HasParam(wxT("bg")))
2285 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
2286 if (HasParam(wxT("ownbg")))
2287 wnd
->SetOwnBackgroundColour(GetColour(wxT("ownbg")));
2288 if (HasParam(wxT("fg")))
2289 wnd
->SetForegroundColour(GetColour(wxT("fg")));
2290 if (HasParam(wxT("ownfg")))
2291 wnd
->SetOwnForegroundColour(GetColour(wxT("ownfg")));
2292 if (GetBool(wxT("enabled"), 1) == 0)
2294 if (GetBool(wxT("focused"), 0) == 1)
2296 if (GetBool(wxT("hidden"), 0) == 1)
2299 if (HasParam(wxT("tooltip")))
2300 wnd
->SetToolTip(GetText(wxT("tooltip")));
2302 if (HasParam(wxT("font")))
2303 wnd
->SetFont(GetFont(wxT("font")));
2304 if (HasParam(wxT("ownfont")))
2305 wnd
->SetOwnFont(GetFont(wxT("ownfont")));
2306 if (HasParam(wxT("help")))
2307 wnd
->SetHelpText(GetText(wxT("help")));
2311 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
2313 for ( wxXmlNode
*n
= m_node
->GetChildren(); n
; n
= n
->GetNext() )
2315 if ( IsObjectNode(n
) )
2317 m_resource
->DoCreateResFromNode(*n
, parent
, NULL
,
2318 this_hnd_only
? this : NULL
);
2324 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
2327 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
2328 wxXmlNode
*n
= root
->GetChildren();
2332 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
2334 CreateResource(n
, parent
, NULL
);
2341 //-----------------------------------------------------------------------------
2343 //-----------------------------------------------------------------------------
2345 void wxXmlResourceHandler::ReportError(const wxString
& message
)
2347 m_resource
->ReportError(m_node
, message
);
2350 void wxXmlResourceHandler::ReportError(wxXmlNode
*context
,
2351 const wxString
& message
)
2353 m_resource
->ReportError(context
? context
: m_node
, message
);
2356 void wxXmlResourceHandler::ReportParamError(const wxString
& param
,
2357 const wxString
& message
)
2359 m_resource
->ReportError(GetParamNode(param
), message
);
2362 void wxXmlResource::ReportError(const wxXmlNode
*context
, const wxString
& message
)
2366 DoReportError("", NULL
, message
);
2370 // We need to find out the file that 'context' is part of. Performance of
2371 // this code is not critical, so we simply find the root XML node and
2372 // compare it with all loaded XRC files.
2373 const wxString filename
= GetFileNameFromNode(context
, Data());
2375 DoReportError(filename
, context
, message
);
2378 void wxXmlResource::DoReportError(const wxString
& xrcFile
, const wxXmlNode
*position
,
2379 const wxString
& message
)
2381 const int line
= position
? position
->GetLineNumber() : -1;
2384 if ( !xrcFile
.empty() )
2385 loc
= xrcFile
+ ':';
2387 loc
+= wxString::Format("%d:", line
);
2391 wxLogError("XRC error: %s%s", loc
, message
);
2395 //-----------------------------------------------------------------------------
2396 // XRCID implementation
2397 //-----------------------------------------------------------------------------
2399 #define XRCID_TABLE_SIZE 1024
2404 /* Hold the id so that once an id is allocated for a name, it
2405 does not get created again by NewControlId at least
2406 until we are done with it */
2412 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
2414 // Extremely simplistic hash function which probably ought to be replaced with
2415 // wxStringHash::stringHash().
2416 static inline unsigned XRCIdHash(const char *str_id
)
2420 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (unsigned int)*c
;
2421 index
%= XRCID_TABLE_SIZE
;
2426 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
2428 const unsigned index
= XRCIdHash(str_id
);
2431 XRCID_record
*oldrec
= NULL
;
2432 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
2434 if (wxStrcmp(rec
->key
, str_id
) == 0)
2441 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
2442 &XRCID_Records
[index
] : &oldrec
->next
;
2443 *rec_var
= new XRCID_record
;
2444 (*rec_var
)->key
= wxStrdup(str_id
);
2445 (*rec_var
)->next
= NULL
;
2448 if (value_if_not_found
!= wxID_NONE
)
2449 (*rec_var
)->id
= value_if_not_found
;
2452 int asint
= wxStrtol(str_id
, &end
, 10);
2453 if (*str_id
&& *end
== 0)
2455 // if str_id was integer, keep it verbosely:
2456 (*rec_var
)->id
= asint
;
2460 (*rec_var
)->id
= wxWindowBase::NewControlId();
2464 return (*rec_var
)->id
;
2470 // flag indicating whether standard XRC ids were already initialized
2471 static bool gs_stdIDsAdded
= false;
2473 void AddStdXRCID_Records()
2475 #define stdID(id) XRCID_Lookup(#id, id)
2479 stdID(wxID_SEPARATOR
);
2492 stdID(wxID_PRINT_SETUP
);
2493 stdID(wxID_PAGE_SETUP
);
2494 stdID(wxID_PREVIEW
);
2496 stdID(wxID_HELP_CONTENTS
);
2497 stdID(wxID_HELP_COMMANDS
);
2498 stdID(wxID_HELP_PROCEDURES
);
2499 stdID(wxID_HELP_CONTEXT
);
2500 stdID(wxID_CLOSE_ALL
);
2501 stdID(wxID_PREFERENCES
);
2508 stdID(wxID_DUPLICATE
);
2509 stdID(wxID_SELECTALL
);
2511 stdID(wxID_REPLACE
);
2512 stdID(wxID_REPLACE_ALL
);
2513 stdID(wxID_PROPERTIES
);
2514 stdID(wxID_VIEW_DETAILS
);
2515 stdID(wxID_VIEW_LARGEICONS
);
2516 stdID(wxID_VIEW_SMALLICONS
);
2517 stdID(wxID_VIEW_LIST
);
2518 stdID(wxID_VIEW_SORTDATE
);
2519 stdID(wxID_VIEW_SORTNAME
);
2520 stdID(wxID_VIEW_SORTSIZE
);
2521 stdID(wxID_VIEW_SORTTYPE
);
2537 stdID(wxID_FORWARD
);
2538 stdID(wxID_BACKWARD
);
2539 stdID(wxID_DEFAULT
);
2543 stdID(wxID_CONTEXT_HELP
);
2544 stdID(wxID_YESTOALL
);
2545 stdID(wxID_NOTOALL
);
2554 stdID(wxID_REFRESH
);
2559 stdID(wxID_JUSTIFY_CENTER
);
2560 stdID(wxID_JUSTIFY_FILL
);
2561 stdID(wxID_JUSTIFY_RIGHT
);
2562 stdID(wxID_JUSTIFY_LEFT
);
2563 stdID(wxID_UNDERLINE
);
2565 stdID(wxID_UNINDENT
);
2566 stdID(wxID_ZOOM_100
);
2567 stdID(wxID_ZOOM_FIT
);
2568 stdID(wxID_ZOOM_IN
);
2569 stdID(wxID_ZOOM_OUT
);
2570 stdID(wxID_UNDELETE
);
2571 stdID(wxID_REVERT_TO_SAVED
);
2572 stdID(wxID_SYSTEM_MENU
);
2573 stdID(wxID_CLOSE_FRAME
);
2574 stdID(wxID_MOVE_FRAME
);
2575 stdID(wxID_RESIZE_FRAME
);
2576 stdID(wxID_MAXIMIZE_FRAME
);
2577 stdID(wxID_ICONIZE_FRAME
);
2578 stdID(wxID_RESTORE_FRAME
);
2580 stdID(wxID_CONVERT
);
2581 stdID(wxID_EXECUTE
);
2583 stdID(wxID_HARDDISK
);
2589 stdID(wxID_JUMP_TO
);
2590 stdID(wxID_NETWORK
);
2591 stdID(wxID_SELECT_COLOR
);
2592 stdID(wxID_SELECT_FONT
);
2593 stdID(wxID_SORT_ASCENDING
);
2594 stdID(wxID_SORT_DESCENDING
);
2595 stdID(wxID_SPELL_CHECK
);
2596 stdID(wxID_STRIKETHROUGH
);
2601 } // anonymous namespace
2605 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
2607 if ( !gs_stdIDsAdded
)
2609 gs_stdIDsAdded
= true;
2610 AddStdXRCID_Records();
2613 return XRCID_Lookup(str_id
, value_if_not_found
);
2617 wxString
wxXmlResource::FindXRCIDById(int numId
)
2619 for ( int i
= 0; i
< XRCID_TABLE_SIZE
; i
++ )
2621 for ( XRCID_record
*rec
= XRCID_Records
[i
]; rec
; rec
= rec
->next
)
2623 if ( rec
->id
== numId
)
2624 return wxString(rec
->key
);
2632 void wxIdRangeManager::RemoveXRCIDEntry(const wxString
& idstr
)
2634 const char *str_id
= idstr
.mb_str();
2636 const unsigned index
= XRCIdHash(str_id
);
2638 XRCID_record
**p_previousrec
= &XRCID_Records
[index
];
2639 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
2641 if (wxStrcmp(rec
->key
, str_id
) == 0)
2643 // Found the item to be removed so delete its record; but first
2644 // remove it from the linked list.
2645 *p_previousrec
= rec
->next
;
2651 p_previousrec
= &rec
->next
;
2655 static void CleanXRCID_Record(XRCID_record
*rec
)
2659 CleanXRCID_Record(rec
->next
);
2666 static void CleanXRCID_Records()
2668 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
2670 CleanXRCID_Record(XRCID_Records
[i
]);
2671 XRCID_Records
[i
] = NULL
;
2674 gs_stdIDsAdded
= false;
2678 //-----------------------------------------------------------------------------
2679 // module and globals
2680 //-----------------------------------------------------------------------------
2682 // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2683 // can happen that some XRC records have been created because of the use of
2684 // XRCID() in event tables, which happens during static objects initialization,
2685 // but then the application initialization failed and so the wx modules were
2686 // neither initialized nor cleaned up -- this static object does the cleanup in
2688 static struct wxXRCStaticCleanup
2690 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2693 class wxXmlResourceModule
: public wxModule
2695 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
2697 wxXmlResourceModule() {}
2700 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
2705 delete wxXmlResource::Set(NULL
);
2706 delete wxIdRangeManager::Set(NULL
);
2707 if(wxXmlResource::ms_subclassFactories
)
2709 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
2710 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
2714 wxDELETE(wxXmlResource::ms_subclassFactories
);
2716 CleanXRCID_Records();
2720 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
2723 // When wxXml is loaded dynamically after the application is already running
2724 // then the built-in module system won't pick this one up. Add it manually.
2725 void wxXmlInitResourceModule()
2727 wxModule
* module = new wxXmlResourceModule
;
2729 wxModule::RegisterModule(module);