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
74 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
76 /*static*/ wxXmlResource
*wxXmlResource::Get()
79 ms_instance
= new wxXmlResource
;
83 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
85 wxXmlResource
*old
= ms_instance
;
90 wxXmlResource::wxXmlResource(int flags
, const wxString
& domain
)
94 m_data
= new wxXmlResourceDataRecords
;
98 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
102 m_data
= new wxXmlResourceDataRecords
;
107 wxXmlResource::~wxXmlResource()
111 for ( wxXmlResourceDataRecords::iterator i
= m_data
->begin();
112 i
!= m_data
->end(); ++i
)
119 void wxXmlResource::SetDomain(const wxString
& domain
)
126 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
128 wxString
fnd(filename
);
130 // NB: as Load() and Unload() accept both filenames and URLs (should
131 // probably be changed to filenames only, but embedded resources
132 // currently rely on its ability to handle URLs - FIXME) we need to
133 // determine whether found name is filename and not URL and this is the
134 // fastest/simplest way to do it
135 if (wxFileName::FileExists(fnd
))
137 // Make the name absolute filename, because the app may
138 // change working directory later:
143 fnd
= fn
.GetFullPath();
146 fnd
= wxFileSystem::FileNameToURL(fnd
);
156 bool wxXmlResource::IsArchive(const wxString
& filename
)
158 const wxString fnd
= filename
.Lower();
160 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
163 #endif // wxUSE_FILESYSTEM
165 bool wxXmlResource::Load(const wxString
& filemask
)
168 bool iswild
= wxIsWild(filemask
);
173 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
174 # define wxXmlFindNext fsys.FindNext()
176 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
177 # define wxXmlFindNext wxFindNextFile()
180 fnd
= wxXmlFindFirst
;
185 fnd
= ConvertFileNameToURL(fnd
);
188 if ( IsArchive(fnd
) )
190 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
192 else // a single resource URL
193 #endif // wxUSE_FILESYSTEM
195 wxXmlResourceDataRecord
*drec
= new wxXmlResourceDataRecord
;
197 Data().push_back(drec
);
205 # undef wxXmlFindFirst
206 # undef wxXmlFindNext
207 return rt
&& UpdateResources();
210 bool wxXmlResource::Unload(const wxString
& filename
)
212 wxASSERT_MSG( !wxIsWild(filename
),
213 _T("wildcards not supported by wxXmlResource::Unload()") );
215 wxString fnd
= ConvertFileNameToURL(filename
);
217 const bool isArchive
= IsArchive(fnd
);
220 #endif // wxUSE_FILESYSTEM
222 bool unloaded
= false;
223 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
224 i
!= Data().end(); ++i
)
229 if ( (*i
)->File
.StartsWith(fnd
) )
231 // don't break from the loop, we can have other matching files
233 else // a single resource URL
234 #endif // wxUSE_FILESYSTEM
236 if ( (*i
)->File
== fnd
)
242 // no sense in continuing, there is only one file with this URL
252 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
254 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
256 m_handlers
.push_back(handler
);
257 handler
->SetParentResource(this);
260 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
262 m_handlers
.insert(m_handlers
.begin(), handler
);
263 handler
->SetParentResource(this);
268 void wxXmlResource::ClearHandlers()
270 for ( wxVector
<wxXmlResourceHandler
*>::iterator i
= m_handlers
.begin();
271 i
!= m_handlers
.end(); ++i
)
277 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
279 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
284 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
286 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
292 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
294 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
299 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
301 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
304 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
306 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
311 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
313 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
316 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
318 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
321 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
323 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
326 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
328 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
331 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
333 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
334 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
337 if (bmp
) { rt
= *bmp
; delete bmp
; }
341 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
343 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
344 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
347 if (icon
) { rt
= *icon
; delete icon
; }
352 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
354 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
357 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
359 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
363 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
364 wxWindow
*control
, wxWindow
*parent
)
367 parent
= control
->GetParent();
368 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
371 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
374 return control
->Reparent(container
);
378 static void ProcessPlatformProperty(wxXmlNode
*node
)
383 wxXmlNode
*c
= node
->GetChildren();
387 if (!c
->GetAttribute(wxT("platform"), &s
))
391 wxStringTokenizer
tkn(s
, wxT(" |"));
393 while (tkn
.HasMoreTokens())
395 s
= tkn
.GetNextToken();
397 if (s
== wxT("win")) isok
= true;
399 #if defined(__MAC__) || defined(__APPLE__)
400 if (s
== wxT("mac")) isok
= true;
401 #elif defined(__UNIX__)
402 if (s
== wxT("unix")) isok
= true;
405 if (s
== wxT("os2")) isok
= true;
415 ProcessPlatformProperty(c
);
420 wxXmlNode
*c2
= c
->GetNext();
421 node
->RemoveChild(c
);
430 bool wxXmlResource::UpdateResources()
434 # if wxUSE_FILESYSTEM
435 wxFSFile
*file
= NULL
;
440 wxString
encoding(wxT("UTF-8"));
441 #if !wxUSE_UNICODE && wxUSE_INTL
442 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
444 // In case we are not using wxLocale to translate strings, convert the
445 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
446 // is on, because it could break wxGetTranslation lookup.
447 encoding
= wxLocale::GetSystemEncodingName();
451 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
452 i
!= Data().end(); ++i
)
454 wxXmlResourceDataRecord
* const rec
= *i
;
456 modif
= (rec
->Doc
== NULL
);
458 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
460 # if wxUSE_FILESYSTEM
461 file
= fsys
.OpenFile(rec
->File
);
463 modif
= file
&& file
->GetModificationTime() > rec
->Time
;
464 # else // wxUSE_DATETIME
466 # endif // wxUSE_DATETIME
469 wxLogError(_("Cannot open file '%s'."), rec
->File
);
474 # else // wxUSE_FILESYSTEM
476 modif
= wxDateTime(wxFileModificationTime(rec
->File
)) > rec
->Time
;
477 # else // wxUSE_DATETIME
479 # endif // wxUSE_DATETIME
480 # endif // wxUSE_FILESYSTEM
485 wxLogTrace(_T("xrc"), _T("opening file '%s'"), rec
->File
);
487 wxInputStream
*stream
= NULL
;
489 # if wxUSE_FILESYSTEM
490 file
= fsys
.OpenFile(rec
->File
);
492 stream
= file
->GetStream();
494 stream
= new wxFileInputStream(rec
->File
);
500 rec
->Doc
= new wxXmlDocument
;
502 if (!stream
|| !rec
->Doc
->Load(*stream
, encoding
))
504 wxLogError(_("Cannot load resources from file '%s'."),
509 else if (rec
->Doc
->GetRoot()->GetName() != wxT("resource"))
511 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), rec
->File
);
519 wxString verstr
= rec
->Doc
->GetRoot()->GetAttribute(
520 wxT("version"), wxT("0.0.0.0"));
521 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
522 &v1
, &v2
, &v3
, &v4
) == 4)
523 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
528 if (m_version
!= version
)
530 wxLogError(_("Resource files must have same version number!"));
534 ProcessPlatformProperty(rec
->Doc
->GetRoot());
537 rec
->Time
= file
->GetModificationTime();
538 #else // wxUSE_FILESYSTEM
539 rec
->Time
= wxDateTime(wxFileModificationTime(rec
->File
));
540 #endif // wxUSE_FILESYSTEM
541 #endif // wxUSE_DATETIME
544 # if wxUSE_FILESYSTEM
557 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
558 const wxString
& name
,
559 const wxString
& classname
,
565 // first search for match at the top-level nodes (as this is
566 // where the resource is most commonly looked for):
567 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
569 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
570 (node
->GetName() == wxT("object") ||
571 node
->GetName() == wxT("object_ref")) &&
572 node
->GetAttribute(wxT("name"), &dummy
) && dummy
== name
)
574 wxString
cls(node
->GetAttribute(wxT("class"), wxEmptyString
));
575 if (!classname
|| cls
== classname
)
577 // object_ref may not have 'class' attribute:
578 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
580 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
583 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
585 refNode
->GetAttribute(wxT("class"), wxEmptyString
) == classname
)
594 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
596 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
597 (node
->GetName() == wxT("object") ||
598 node
->GetName() == wxT("object_ref")) )
600 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
609 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
610 const wxString
& classname
,
613 UpdateResources(); //ensure everything is up-to-date
616 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
617 f
!= Data().end(); ++f
)
619 wxXmlResourceDataRecord
* const rec
= *f
;
620 if ( rec
->Doc
== NULL
|| rec
->Doc
->GetRoot() == NULL
)
623 wxXmlNode
* found
= DoFindResource(rec
->Doc
->GetRoot(),
624 name
, classname
, recursive
);
628 m_curFileSystem
.ChangePathTo(rec
->File
);
634 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
635 name
.c_str(), classname
.c_str());
639 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
642 for ( wxXmlAttribute
*attr
= with
.GetAttributes();
643 attr
; attr
= attr
->GetNext() )
645 wxXmlAttribute
*dattr
;
646 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
649 if ( dattr
->GetName() == attr
->GetName() )
651 dattr
->SetValue(attr
->GetValue());
657 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
660 // Merge child nodes:
661 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
663 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
666 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
668 if ( dnode
->GetName() == node
->GetName() &&
669 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
670 dnode
->GetType() == node
->GetType() )
672 MergeNodes(*dnode
, *node
);
679 static const wxChar
*AT_END
= wxT("end");
680 wxString insert_pos
= node
->GetAttribute(wxT("insert_at"), AT_END
);
681 if ( insert_pos
== AT_END
)
683 dest
.AddChild(new wxXmlNode(*node
));
685 else if ( insert_pos
== wxT("begin") )
687 dest
.InsertChild(new wxXmlNode(*node
), dest
.GetChildren());
692 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().length() )
693 dest
.SetContent(with
.GetContent());
696 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
698 wxXmlResourceHandler
*handlerToUse
)
700 if (node
== NULL
) return NULL
;
702 // handling of referenced resource
703 if ( node
->GetName() == wxT("object_ref") )
705 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
706 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
710 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
715 wxXmlNode
copy(*refNode
);
716 MergeNodes(copy
, *node
);
718 return CreateResFromNode(©
, parent
, instance
);
723 if (handlerToUse
->CanHandle(node
))
725 return handlerToUse
->CreateResource(node
, parent
, instance
);
728 else if (node
->GetName() == wxT("object"))
730 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
731 h
!= m_handlers
.end(); ++h
)
733 wxXmlResourceHandler
*handler
= *h
;
734 if (handler
->CanHandle(node
))
735 return handler
->CreateResource(node
, parent
, instance
);
739 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
740 node
->GetName().c_str(),
741 node
->GetAttribute(wxT("class"), wxEmptyString
).c_str());
746 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
748 // this is a class so that it can be forward-declared
751 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
753 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
755 if (!ms_subclassFactories
)
757 ms_subclassFactories
= new wxXmlSubclassFactories
;
759 ms_subclassFactories
->push_back(factory
);
762 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
765 ~wxXmlSubclassFactoryCXX() {}
767 wxObject
*Create(const wxString
& className
)
769 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
772 return classInfo
->CreateObject();
781 wxXmlResourceHandler::wxXmlResourceHandler()
782 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
783 m_parentAsWindow(NULL
)
788 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
790 wxXmlNode
*myNode
= m_node
;
791 wxString myClass
= m_class
;
792 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
793 wxWindow
*myParentAW
= m_parentAsWindow
;
795 m_instance
= instance
;
796 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
797 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
799 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
800 if (!subclass
.empty())
802 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
803 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
805 m_instance
= (*i
)->Create(subclass
);
812 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
813 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
814 subclass
.c_str(), name
.c_str());
820 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
822 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
824 wxObject
*returned
= DoCreateResource();
828 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
829 m_instance
= myInstance
;
835 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
837 m_styleNames
.Add(name
);
838 m_styleValues
.Add(value
);
843 void wxXmlResourceHandler::AddWindowStyles()
845 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
847 // the border styles all have the old and new names, recognize both for now
848 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
849 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
850 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
851 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
852 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
853 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
855 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
856 XRC_ADD_STYLE(wxWANTS_CHARS
);
857 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
858 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
859 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
860 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
861 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
862 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
867 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
869 return (GetParamNode(param
) != NULL
);
873 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
875 wxString s
= GetParamValue(param
);
877 if (!s
) return defaults
;
879 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
883 while (tkn
.HasMoreTokens())
885 fl
= tkn
.GetNextToken();
886 index
= m_styleNames
.Index(fl
);
887 if (index
!= wxNOT_FOUND
)
888 style
|= m_styleValues
[index
];
890 wxLogError(_("Unknown style flag ") + fl
);
897 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
899 wxXmlNode
*parNode
= GetParamNode(param
);
900 wxString
str1(GetNodeContent(parNode
));
905 // VS: First version of XRC resources used $ instead of & (which is
906 // illegal in XML), but later I realized that '_' fits this purpose
907 // much better (because &File means "File with F underlined").
908 if (m_resource
->CompareVersion(2,3,0,1) < 0)
913 for (dt
= str1
.c_str(); *dt
; dt
++)
915 // Remap amp_char to &, map double amp_char to amp_char (for things
916 // like "&File..." -- this is illegal in XML, so we use "_File..."):
919 if ( *(++dt
) == amp_char
)
922 str2
<< wxT('&') << *dt
;
924 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
925 else if (*dt
== wxT('\\'))
941 // "\\" wasn't translated to "\" prior to 2.5.3.0:
942 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
947 // else fall-through to default: branch below
950 str2
<< wxT('\\') << *dt
;
956 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
958 if (translate
&& parNode
&&
959 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
961 return wxGetTranslation(str2
, m_resource
->GetDomain());
968 // The string is internally stored as UTF-8, we have to convert
969 // it into system's default encoding so that it can be displayed:
970 return wxString(str2
.wc_str(wxConvUTF8
), wxConvLocal
);
975 // If wxXRC_USE_LOCALE is not set, then the string is already in
976 // system's default encoding in ANSI build, so we don't have to
977 // do anything special here.
983 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
986 wxString str1
= GetParamValue(param
);
988 if (!str1
.ToLong(&value
))
994 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
996 wxString str
= GetParamValue(param
);
999 // strings in XRC always use C locale but wxString::ToDouble() uses the
1000 // current one, so transform the string to it supposing that the only
1001 // difference between them is the decimal separator
1003 // TODO: use wxString::ToCDouble() when we have it
1004 str
.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1005 wxLOCALE_CAT_NUMBER
));
1006 #endif // wxUSE_INTL
1009 if (!str
.ToDouble(&value
))
1012 return wx_truncate_cast(float, value
);
1016 int wxXmlResourceHandler::GetID()
1018 return wxXmlResource::GetXRCID(GetName());
1023 wxString
wxXmlResourceHandler::GetName()
1025 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1030 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1032 wxString v
= GetParamValue(param
);
1034 if (!v
) return defaultv
;
1036 return (v
== wxT("1"));
1040 static wxColour
GetSystemColour(const wxString
& name
)
1044 #define SYSCLR(clr) \
1045 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1046 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1047 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1048 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1049 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1050 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1051 SYSCLR(wxSYS_COLOUR_MENU
)
1052 SYSCLR(wxSYS_COLOUR_WINDOW
)
1053 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1054 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1055 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1056 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1057 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1058 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1059 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1060 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1061 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1062 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1063 SYSCLR(wxSYS_COLOUR_3DFACE
)
1064 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1065 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1066 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1067 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1068 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1069 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1070 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1071 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1072 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1073 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1074 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1075 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1076 SYSCLR(wxSYS_COLOUR_INFOBK
)
1077 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1078 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1079 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1080 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1081 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1082 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1086 return wxNullColour
;
1089 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1091 wxString v
= GetParamValue(param
);
1098 // wxString -> wxColour conversion
1101 // the colour doesn't use #RRGGBB format, check if it is symbolic
1103 clr
= GetSystemColour(v
);
1107 wxLogError(_("XRC resource: Incorrect colour specification '%s' for attribute '%s'."),
1108 v
.c_str(), param
.c_str());
1109 return wxNullColour
;
1117 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1118 const wxArtClient
& defaultArtClient
,
1121 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1122 wxXmlNode
*bmpNode
= GetParamNode(param
);
1125 wxString sid
= bmpNode
->GetAttribute(wxT("stock_id"), wxEmptyString
);
1128 wxString scl
= bmpNode
->GetAttribute(wxT("stock_client"), wxEmptyString
);
1130 scl
= defaultArtClient
;
1132 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1135 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1137 if ( stockArt
.Ok() )
1142 /* ...or load the bitmap from file: */
1143 wxString name
= GetParamValue(param
);
1144 if (name
.empty()) return wxNullBitmap
;
1145 #if wxUSE_FILESYSTEM
1146 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1149 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1151 return wxNullBitmap
;
1153 wxImage
img(*(fsfile
->GetStream()));
1161 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1163 return wxNullBitmap
;
1165 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1166 return wxBitmap(img
);
1169 #if wxUSE_ANIMATIONCTRL
1170 wxAnimation
wxXmlResourceHandler::GetAnimation(const wxString
& param
)
1174 /* load the animation from file: */
1175 wxString name
= GetParamValue(param
);
1176 if (name
.empty()) return wxNullAnimation
;
1177 #if wxUSE_FILESYSTEM
1178 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1181 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1183 return wxNullAnimation
;
1185 ani
.Load(*(fsfile
->GetStream()));
1193 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1195 return wxNullAnimation
;
1200 #endif // wxUSE_ANIMATIONCTRL
1204 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1205 const wxArtClient
& defaultArtClient
,
1209 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1215 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1217 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1219 wxXmlNode
*n
= m_node
->GetChildren();
1223 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1232 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1234 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1239 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1241 wxXmlNode
*n
= node
;
1242 if (n
== NULL
) return wxEmptyString
;
1243 n
= n
->GetChildren();
1247 if (n
->GetType() == wxXML_TEXT_NODE
||
1248 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1249 return n
->GetContent();
1252 return wxEmptyString
;
1257 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1260 return GetNodeContent(m_node
);
1262 return GetNodeContent(GetParamNode(param
));
1267 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1268 wxWindow
*windowToUse
)
1270 wxString s
= GetParamValue(param
);
1271 if (s
.empty()) s
= wxT("-1,-1");
1275 is_dlg
= s
[s
.length()-1] == wxT('d');
1276 if (is_dlg
) s
.RemoveLast();
1278 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1279 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1281 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1282 return wxDefaultSize
;
1289 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1291 else if (m_parentAsWindow
)
1293 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1297 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1298 return wxDefaultSize
;
1302 return wxSize(sx
, sy
);
1307 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1309 wxSize sz
= GetSize(param
);
1310 return wxPoint(sz
.x
, sz
.y
);
1315 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1317 wxWindow
*windowToUse
)
1319 wxString s
= GetParamValue(param
);
1320 if (s
.empty()) return defaultv
;
1324 is_dlg
= s
[s
.length()-1] == wxT('d');
1325 if (is_dlg
) s
.RemoveLast();
1329 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1337 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1339 else if (m_parentAsWindow
)
1341 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1345 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1354 // Get system font index using indexname
1355 static wxFont
GetSystemFont(const wxString
& name
)
1359 #define SYSFNT(fnt) \
1360 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1361 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1362 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1363 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1364 SYSFNT(wxSYS_SYSTEM_FONT
)
1365 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1366 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1367 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1368 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1375 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1377 wxXmlNode
*font_node
= GetParamNode(param
);
1378 if (font_node
== NULL
)
1380 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1384 wxXmlNode
*oldnode
= m_node
;
1391 bool hasSize
= HasParam(wxT("size"));
1393 isize
= GetLong(wxT("size"), -1);
1396 int istyle
= wxNORMAL
;
1397 bool hasStyle
= HasParam(wxT("style"));
1400 wxString style
= GetParamValue(wxT("style"));
1401 if (style
== wxT("italic"))
1403 else if (style
== wxT("slant"))
1408 int iweight
= wxNORMAL
;
1409 bool hasWeight
= HasParam(wxT("weight"));
1412 wxString weight
= GetParamValue(wxT("weight"));
1413 if (weight
== wxT("bold"))
1415 else if (weight
== wxT("light"))
1420 bool hasUnderlined
= HasParam(wxT("underlined"));
1421 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1423 // family and facename
1424 int ifamily
= wxDEFAULT
;
1425 bool hasFamily
= HasParam(wxT("family"));
1428 wxString family
= GetParamValue(wxT("family"));
1429 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1430 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1431 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1432 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1433 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1434 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1439 bool hasFacename
= HasParam(wxT("face"));
1442 wxString faces
= GetParamValue(wxT("face"));
1443 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1444 wxStringTokenizer
tk(faces
, wxT(","));
1445 while (tk
.HasMoreTokens())
1447 int index
= facenames
.Index(tk
.GetNextToken(), false);
1448 if (index
!= wxNOT_FOUND
)
1450 facename
= facenames
[index
];
1457 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1458 bool hasEncoding
= HasParam(wxT("encoding"));
1461 wxString encoding
= GetParamValue(wxT("encoding"));
1462 wxFontMapper mapper
;
1463 if (!encoding
.empty())
1464 enc
= mapper
.CharsetToEncoding(encoding
);
1465 if (enc
== wxFONTENCODING_SYSTEM
)
1466 enc
= wxFONTENCODING_DEFAULT
;
1469 // is this font based on a system font?
1470 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1474 if (hasSize
&& isize
!= -1)
1475 font
.SetPointSize(isize
);
1476 else if (HasParam(wxT("relativesize")))
1477 font
.SetPointSize(int(font
.GetPointSize() *
1478 GetFloat(wxT("relativesize"))));
1481 font
.SetStyle(istyle
);
1483 font
.SetWeight(iweight
);
1485 font
.SetUnderlined(underlined
);
1487 font
.SetFamily(ifamily
);
1489 font
.SetFaceName(facename
);
1491 font
.SetDefaultEncoding(enc
);
1493 else // not based on system font
1495 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1496 ifamily
, istyle
, iweight
,
1497 underlined
, facename
, enc
);
1505 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1507 //FIXME : add cursor
1509 if (HasParam(wxT("exstyle")))
1510 // Have to OR it with existing style, since
1511 // some implementations (e.g. wxGTK) use the extra style
1513 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1514 if (HasParam(wxT("bg")))
1515 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1516 if (HasParam(wxT("fg")))
1517 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1518 if (GetBool(wxT("enabled"), 1) == 0)
1520 if (GetBool(wxT("focused"), 0) == 1)
1522 if (GetBool(wxT("hidden"), 0) == 1)
1525 if (HasParam(wxT("tooltip")))
1526 wnd
->SetToolTip(GetText(wxT("tooltip")));
1528 if (HasParam(wxT("font")))
1529 wnd
->SetFont(GetFont());
1530 if (HasParam(wxT("help")))
1531 wnd
->SetHelpText(GetText(wxT("help")));
1535 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1537 wxXmlNode
*n
= m_node
->GetChildren();
1541 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1542 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1544 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1545 this_hnd_only
? this : NULL
);
1552 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1555 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1556 wxXmlNode
*n
= root
->GetChildren();
1560 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1562 CreateResource(n
, parent
, NULL
);
1574 // --------------- XRCID implementation -----------------------------
1576 #define XRCID_TABLE_SIZE 1024
1581 /* Hold the id so that once an id is allocated for a name, it
1582 does not get created again by NewControlId at least
1583 until we are done with it */
1589 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1591 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1595 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1596 index
%= XRCID_TABLE_SIZE
;
1598 XRCID_record
*oldrec
= NULL
;
1599 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1601 if (wxStrcmp(rec
->key
, str_id
) == 0)
1608 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1609 &XRCID_Records
[index
] : &oldrec
->next
;
1610 *rec_var
= new XRCID_record
;
1611 (*rec_var
)->key
= wxStrdup(str_id
);
1612 (*rec_var
)->next
= NULL
;
1615 if (value_if_not_found
!= wxID_NONE
)
1616 (*rec_var
)->id
= value_if_not_found
;
1619 int asint
= wxStrtol(str_id
, &end
, 10);
1620 if (*str_id
&& *end
== 0)
1622 // if str_id was integer, keep it verbosely:
1623 (*rec_var
)->id
= asint
;
1627 (*rec_var
)->id
= wxWindowBase::NewControlId();
1631 return (*rec_var
)->id
;
1634 static void AddStdXRCID_Records();
1637 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
1639 static bool s_stdIDsAdded
= false;
1641 if ( !s_stdIDsAdded
)
1643 s_stdIDsAdded
= true;
1644 AddStdXRCID_Records();
1647 return XRCID_Lookup(str_id
, value_if_not_found
);
1651 static void CleanXRCID_Record(XRCID_record
*rec
)
1655 CleanXRCID_Record(rec
->next
);
1662 static void CleanXRCID_Records()
1664 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1666 CleanXRCID_Record(XRCID_Records
[i
]);
1667 XRCID_Records
[i
] = NULL
;
1671 static void AddStdXRCID_Records()
1673 #define stdID(id) XRCID_Lookup(#id, id)
1677 stdID(wxID_SEPARATOR
);
1690 stdID(wxID_PRINT_SETUP
);
1691 stdID(wxID_PAGE_SETUP
);
1692 stdID(wxID_PREVIEW
);
1694 stdID(wxID_HELP_CONTENTS
);
1695 stdID(wxID_HELP_COMMANDS
);
1696 stdID(wxID_HELP_PROCEDURES
);
1697 stdID(wxID_HELP_CONTEXT
);
1698 stdID(wxID_CLOSE_ALL
);
1699 stdID(wxID_PREFERENCES
);
1706 stdID(wxID_DUPLICATE
);
1707 stdID(wxID_SELECTALL
);
1709 stdID(wxID_REPLACE
);
1710 stdID(wxID_REPLACE_ALL
);
1711 stdID(wxID_PROPERTIES
);
1712 stdID(wxID_VIEW_DETAILS
);
1713 stdID(wxID_VIEW_LARGEICONS
);
1714 stdID(wxID_VIEW_SMALLICONS
);
1715 stdID(wxID_VIEW_LIST
);
1716 stdID(wxID_VIEW_SORTDATE
);
1717 stdID(wxID_VIEW_SORTNAME
);
1718 stdID(wxID_VIEW_SORTSIZE
);
1719 stdID(wxID_VIEW_SORTTYPE
);
1735 stdID(wxID_FORWARD
);
1736 stdID(wxID_BACKWARD
);
1737 stdID(wxID_DEFAULT
);
1741 stdID(wxID_CONTEXT_HELP
);
1742 stdID(wxID_YESTOALL
);
1743 stdID(wxID_NOTOALL
);
1752 stdID(wxID_REFRESH
);
1757 stdID(wxID_JUSTIFY_CENTER
);
1758 stdID(wxID_JUSTIFY_FILL
);
1759 stdID(wxID_JUSTIFY_RIGHT
);
1760 stdID(wxID_JUSTIFY_LEFT
);
1761 stdID(wxID_UNDERLINE
);
1763 stdID(wxID_UNINDENT
);
1764 stdID(wxID_ZOOM_100
);
1765 stdID(wxID_ZOOM_FIT
);
1766 stdID(wxID_ZOOM_IN
);
1767 stdID(wxID_ZOOM_OUT
);
1768 stdID(wxID_UNDELETE
);
1769 stdID(wxID_REVERT_TO_SAVED
);
1770 stdID(wxID_SYSTEM_MENU
);
1771 stdID(wxID_CLOSE_FRAME
);
1772 stdID(wxID_MOVE_FRAME
);
1773 stdID(wxID_RESIZE_FRAME
);
1774 stdID(wxID_MAXIMIZE_FRAME
);
1775 stdID(wxID_ICONIZE_FRAME
);
1776 stdID(wxID_RESTORE_FRAME
);
1785 // --------------- module and globals -----------------------------
1787 // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
1788 // can happen that some XRC records have been created because of the use of
1789 // XRCID() in event tables, which happens during static objects initialization,
1790 // but then the application initialization failed and so the wx modules were
1791 // neither initialized nor cleaned up -- this static object does the cleanup in
1793 static struct wxXRCStaticCleanup
1795 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
1798 class wxXmlResourceModule
: public wxModule
1800 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1802 wxXmlResourceModule() {}
1805 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1810 delete wxXmlResource::Set(NULL
);
1811 if(wxXmlResource::ms_subclassFactories
)
1813 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
1814 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
1818 wxDELETE(wxXmlResource::ms_subclassFactories
);
1820 CleanXRCID_Records();
1824 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1827 // When wxXml is loaded dynamically after the application is already running
1828 // then the built-in module system won't pick this one up. Add it manually.
1829 void wxXmlInitResourceModule()
1831 wxModule
* module = new wxXmlResourceModule
;
1833 wxModule::RegisterModule(module);