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
)
241 // no sense in continuing, there is only one file with this URL
251 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
253 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
255 m_handlers
.push_back(handler
);
256 handler
->SetParentResource(this);
259 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
261 m_handlers
.insert(m_handlers
.begin(), handler
);
262 handler
->SetParentResource(this);
267 void wxXmlResource::ClearHandlers()
269 for ( wxVector
<wxXmlResourceHandler
*>::iterator i
= m_handlers
.begin();
270 i
!= m_handlers
.end(); ++i
)
276 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
278 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
283 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
285 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
291 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
293 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
298 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
300 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
303 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
305 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
310 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
312 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
315 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
317 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
320 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
322 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
325 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
327 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
330 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
332 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
333 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
336 if (bmp
) { rt
= *bmp
; delete bmp
; }
340 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
342 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
343 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
346 if (icon
) { rt
= *icon
; delete icon
; }
351 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
353 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
356 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
358 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
362 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
363 wxWindow
*control
, wxWindow
*parent
)
366 parent
= control
->GetParent();
367 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
370 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
373 return control
->Reparent(container
);
377 static void ProcessPlatformProperty(wxXmlNode
*node
)
382 wxXmlNode
*c
= node
->GetChildren();
386 if (!c
->GetAttribute(wxT("platform"), &s
))
390 wxStringTokenizer
tkn(s
, wxT(" |"));
392 while (tkn
.HasMoreTokens())
394 s
= tkn
.GetNextToken();
396 if (s
== wxT("win")) isok
= true;
398 #if defined(__MAC__) || defined(__APPLE__)
399 if (s
== wxT("mac")) isok
= true;
400 #elif defined(__UNIX__)
401 if (s
== wxT("unix")) isok
= true;
404 if (s
== wxT("os2")) isok
= true;
414 ProcessPlatformProperty(c
);
419 wxXmlNode
*c2
= c
->GetNext();
420 node
->RemoveChild(c
);
429 bool wxXmlResource::UpdateResources()
433 # if wxUSE_FILESYSTEM
434 wxFSFile
*file
= NULL
;
439 wxString
encoding(wxT("UTF-8"));
440 #if !wxUSE_UNICODE && wxUSE_INTL
441 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
443 // In case we are not using wxLocale to translate strings, convert the
444 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
445 // is on, because it could break wxGetTranslation lookup.
446 encoding
= wxLocale::GetSystemEncodingName();
450 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
451 i
!= Data().end(); ++i
)
453 wxXmlResourceDataRecord
* const rec
= *i
;
455 modif
= (rec
->Doc
== NULL
);
457 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
459 # if wxUSE_FILESYSTEM
460 file
= fsys
.OpenFile(rec
->File
);
462 modif
= file
&& file
->GetModificationTime() > rec
->Time
;
463 # else // wxUSE_DATETIME
465 # endif // wxUSE_DATETIME
468 wxLogError(_("Cannot open file '%s'."), rec
->File
);
473 # else // wxUSE_FILESYSTEM
475 modif
= wxDateTime(wxFileModificationTime(rec
->File
)) > rec
->Time
;
476 # else // wxUSE_DATETIME
478 # endif // wxUSE_DATETIME
479 # endif // wxUSE_FILESYSTEM
484 wxLogTrace(_T("xrc"), _T("opening file '%s'"), rec
->File
);
486 wxInputStream
*stream
= NULL
;
488 # if wxUSE_FILESYSTEM
489 file
= fsys
.OpenFile(rec
->File
);
491 stream
= file
->GetStream();
493 stream
= new wxFileInputStream(rec
->File
);
499 rec
->Doc
= new wxXmlDocument
;
501 if (!stream
|| !rec
->Doc
->Load(*stream
, encoding
))
503 wxLogError(_("Cannot load resources from file '%s'."),
508 else if (rec
->Doc
->GetRoot()->GetName() != wxT("resource"))
510 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), rec
->File
);
518 wxString verstr
= rec
->Doc
->GetRoot()->GetAttribute(
519 wxT("version"), wxT("0.0.0.0"));
520 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
521 &v1
, &v2
, &v3
, &v4
) == 4)
522 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
527 if (m_version
!= version
)
529 wxLogError(_("Resource files must have same version number!"));
533 ProcessPlatformProperty(rec
->Doc
->GetRoot());
536 rec
->Time
= file
->GetModificationTime();
537 #else // wxUSE_FILESYSTEM
538 rec
->Time
= wxDateTime(wxFileModificationTime(rec
->File
));
539 #endif // wxUSE_FILESYSTEM
540 #endif // wxUSE_DATETIME
543 # if wxUSE_FILESYSTEM
556 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
557 const wxString
& name
,
558 const wxString
& classname
,
564 // first search for match at the top-level nodes (as this is
565 // where the resource is most commonly looked for):
566 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
568 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
569 (node
->GetName() == wxT("object") ||
570 node
->GetName() == wxT("object_ref")) &&
571 node
->GetAttribute(wxT("name"), &dummy
) && dummy
== name
)
573 wxString
cls(node
->GetAttribute(wxT("class"), wxEmptyString
));
574 if (!classname
|| cls
== classname
)
576 // object_ref may not have 'class' attribute:
577 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
579 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
582 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
584 refNode
->GetAttribute(wxT("class"), wxEmptyString
) == classname
)
593 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
595 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
596 (node
->GetName() == wxT("object") ||
597 node
->GetName() == wxT("object_ref")) )
599 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
608 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
609 const wxString
& classname
,
612 UpdateResources(); //ensure everything is up-to-date
615 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
616 f
!= Data().end(); ++f
)
618 wxXmlResourceDataRecord
* const rec
= *f
;
619 if ( rec
->Doc
== NULL
|| rec
->Doc
->GetRoot() == NULL
)
622 wxXmlNode
* found
= DoFindResource(rec
->Doc
->GetRoot(),
623 name
, classname
, recursive
);
627 m_curFileSystem
.ChangePathTo(rec
->File
);
633 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
634 name
.c_str(), classname
.c_str());
638 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
641 for ( wxXmlAttribute
*attr
= with
.GetAttributes();
642 attr
; attr
= attr
->GetNext() )
644 wxXmlAttribute
*dattr
;
645 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
648 if ( dattr
->GetName() == attr
->GetName() )
650 dattr
->SetValue(attr
->GetValue());
656 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
659 // Merge child nodes:
660 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
662 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
665 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
667 if ( dnode
->GetName() == node
->GetName() &&
668 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
669 dnode
->GetType() == node
->GetType() )
671 MergeNodes(*dnode
, *node
);
678 static const wxChar
*AT_END
= wxT("end");
679 wxString insert_pos
= node
->GetAttribute(wxT("insert_at"), AT_END
);
680 if ( insert_pos
== AT_END
)
682 dest
.AddChild(new wxXmlNode(*node
));
684 else if ( insert_pos
== wxT("begin") )
686 dest
.InsertChild(new wxXmlNode(*node
), dest
.GetChildren());
691 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().length() )
692 dest
.SetContent(with
.GetContent());
695 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
697 wxXmlResourceHandler
*handlerToUse
)
699 if (node
== NULL
) return NULL
;
701 // handling of referenced resource
702 if ( node
->GetName() == wxT("object_ref") )
704 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
705 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
709 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
714 wxXmlNode
copy(*refNode
);
715 MergeNodes(copy
, *node
);
717 return CreateResFromNode(©
, parent
, instance
);
722 if (handlerToUse
->CanHandle(node
))
724 return handlerToUse
->CreateResource(node
, parent
, instance
);
727 else if (node
->GetName() == wxT("object"))
729 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
730 h
!= m_handlers
.end(); ++h
)
732 wxXmlResourceHandler
*handler
= *h
;
733 if (handler
->CanHandle(node
))
734 return handler
->CreateResource(node
, parent
, instance
);
738 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
739 node
->GetName().c_str(),
740 node
->GetAttribute(wxT("class"), wxEmptyString
).c_str());
745 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
747 // this is a class so that it can be forward-declared
750 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
752 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
754 if (!ms_subclassFactories
)
756 ms_subclassFactories
= new wxXmlSubclassFactories
;
758 ms_subclassFactories
->push_back(factory
);
761 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
764 ~wxXmlSubclassFactoryCXX() {}
766 wxObject
*Create(const wxString
& className
)
768 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
771 return classInfo
->CreateObject();
780 wxXmlResourceHandler::wxXmlResourceHandler()
781 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
782 m_parentAsWindow(NULL
)
787 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
789 wxXmlNode
*myNode
= m_node
;
790 wxString myClass
= m_class
;
791 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
792 wxWindow
*myParentAW
= m_parentAsWindow
;
794 m_instance
= instance
;
795 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
796 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
798 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
799 if (!subclass
.empty())
801 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
802 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
804 m_instance
= (*i
)->Create(subclass
);
811 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
812 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
813 subclass
.c_str(), name
.c_str());
819 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
821 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
823 wxObject
*returned
= DoCreateResource();
827 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
828 m_instance
= myInstance
;
834 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
836 m_styleNames
.Add(name
);
837 m_styleValues
.Add(value
);
842 void wxXmlResourceHandler::AddWindowStyles()
844 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
846 // the border styles all have the old and new names, recognize both for now
847 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
848 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
849 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
850 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
851 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
852 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
854 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
855 XRC_ADD_STYLE(wxWANTS_CHARS
);
856 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
857 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
858 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
859 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
860 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
861 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
866 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
868 return (GetParamNode(param
) != NULL
);
872 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
874 wxString s
= GetParamValue(param
);
876 if (!s
) return defaults
;
878 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
882 while (tkn
.HasMoreTokens())
884 fl
= tkn
.GetNextToken();
885 index
= m_styleNames
.Index(fl
);
886 if (index
!= wxNOT_FOUND
)
887 style
|= m_styleValues
[index
];
889 wxLogError(_("Unknown style flag ") + fl
);
896 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
898 wxXmlNode
*parNode
= GetParamNode(param
);
899 wxString
str1(GetNodeContent(parNode
));
904 // VS: First version of XRC resources used $ instead of & (which is
905 // illegal in XML), but later I realized that '_' fits this purpose
906 // much better (because &File means "File with F underlined").
907 if (m_resource
->CompareVersion(2,3,0,1) < 0)
912 for (dt
= str1
.c_str(); *dt
; dt
++)
914 // Remap amp_char to &, map double amp_char to amp_char (for things
915 // like "&File..." -- this is illegal in XML, so we use "_File..."):
918 if ( *(++dt
) == amp_char
)
921 str2
<< wxT('&') << *dt
;
923 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
924 else if (*dt
== wxT('\\'))
940 // "\\" wasn't translated to "\" prior to 2.5.3.0:
941 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
946 // else fall-through to default: branch below
949 str2
<< wxT('\\') << *dt
;
955 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
957 if (translate
&& parNode
&&
958 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
960 return wxGetTranslation(str2
, m_resource
->GetDomain());
967 // The string is internally stored as UTF-8, we have to convert
968 // it into system's default encoding so that it can be displayed:
969 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
974 // If wxXRC_USE_LOCALE is not set, then the string is already in
975 // system's default encoding in ANSI build, so we don't have to
976 // do anything special here.
982 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
985 wxString str1
= GetParamValue(param
);
987 if (!str1
.ToLong(&value
))
993 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
995 wxString str
= GetParamValue(param
);
997 // strings in XRC always use C locale but wxString::ToDouble() uses the
998 // current one, so transform the string to it supposing that the only
999 // difference between them is the decimal separator
1001 // TODO: use wxString::ToCDouble() when we have it
1002 str
.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
1003 wxLOCALE_CAT_NUMBER
));
1006 if (!str
.ToDouble(&value
))
1009 return wx_truncate_cast(float, value
);
1013 int wxXmlResourceHandler::GetID()
1015 return wxXmlResource::GetXRCID(GetName());
1020 wxString
wxXmlResourceHandler::GetName()
1022 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1027 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1029 wxString v
= GetParamValue(param
);
1031 if (!v
) return defaultv
;
1033 return (v
== wxT("1"));
1037 static wxColour
GetSystemColour(const wxString
& name
)
1041 #define SYSCLR(clr) \
1042 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1043 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1044 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1045 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1046 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1047 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1048 SYSCLR(wxSYS_COLOUR_MENU
)
1049 SYSCLR(wxSYS_COLOUR_WINDOW
)
1050 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1051 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1052 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1053 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1054 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1055 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1056 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1057 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1058 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1059 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1060 SYSCLR(wxSYS_COLOUR_3DFACE
)
1061 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1062 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1063 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1064 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1065 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1066 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1067 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1068 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1069 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1070 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1071 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1072 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1073 SYSCLR(wxSYS_COLOUR_INFOBK
)
1074 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1075 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1076 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1077 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1078 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1079 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1083 return wxNullColour
;
1086 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1088 wxString v
= GetParamValue(param
);
1095 // wxString -> wxColour conversion
1098 // the colour doesn't use #RRGGBB format, check if it is symbolic
1100 clr
= GetSystemColour(v
);
1104 wxLogError(_("XRC resource: Incorrect colour specification '%s' for attribute '%s'."),
1105 v
.c_str(), param
.c_str());
1106 return wxNullColour
;
1114 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1115 const wxArtClient
& defaultArtClient
,
1118 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1119 wxXmlNode
*bmpNode
= GetParamNode(param
);
1122 wxString sid
= bmpNode
->GetAttribute(wxT("stock_id"), wxEmptyString
);
1125 wxString scl
= bmpNode
->GetAttribute(wxT("stock_client"), wxEmptyString
);
1127 scl
= defaultArtClient
;
1129 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1132 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1134 if ( stockArt
.Ok() )
1139 /* ...or load the bitmap from file: */
1140 wxString name
= GetParamValue(param
);
1141 if (name
.empty()) return wxNullBitmap
;
1142 #if wxUSE_FILESYSTEM
1143 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1146 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1148 return wxNullBitmap
;
1150 wxImage
img(*(fsfile
->GetStream()));
1158 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1160 return wxNullBitmap
;
1162 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1163 return wxBitmap(img
);
1166 #if wxUSE_ANIMATIONCTRL
1167 wxAnimation
wxXmlResourceHandler::GetAnimation(const wxString
& param
)
1171 /* load the animation from file: */
1172 wxString name
= GetParamValue(param
);
1173 if (name
.empty()) return wxNullAnimation
;
1174 #if wxUSE_FILESYSTEM
1175 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1178 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1180 return wxNullAnimation
;
1182 ani
.Load(*(fsfile
->GetStream()));
1190 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1192 return wxNullAnimation
;
1197 #endif // wxUSE_ANIMATIONCTRL
1201 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1202 const wxArtClient
& defaultArtClient
,
1206 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1212 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1214 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1216 wxXmlNode
*n
= m_node
->GetChildren();
1220 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1229 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1231 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1236 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1238 wxXmlNode
*n
= node
;
1239 if (n
== NULL
) return wxEmptyString
;
1240 n
= n
->GetChildren();
1244 if (n
->GetType() == wxXML_TEXT_NODE
||
1245 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1246 return n
->GetContent();
1249 return wxEmptyString
;
1254 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1257 return GetNodeContent(m_node
);
1259 return GetNodeContent(GetParamNode(param
));
1264 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1265 wxWindow
*windowToUse
)
1267 wxString s
= GetParamValue(param
);
1268 if (s
.empty()) s
= wxT("-1,-1");
1272 is_dlg
= s
[s
.length()-1] == wxT('d');
1273 if (is_dlg
) s
.RemoveLast();
1275 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1276 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1278 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1279 return wxDefaultSize
;
1286 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1288 else if (m_parentAsWindow
)
1290 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1294 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1295 return wxDefaultSize
;
1299 return wxSize(sx
, sy
);
1304 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1306 wxSize sz
= GetSize(param
);
1307 return wxPoint(sz
.x
, sz
.y
);
1312 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1314 wxWindow
*windowToUse
)
1316 wxString s
= GetParamValue(param
);
1317 if (s
.empty()) return defaultv
;
1321 is_dlg
= s
[s
.length()-1] == wxT('d');
1322 if (is_dlg
) s
.RemoveLast();
1326 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1334 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1336 else if (m_parentAsWindow
)
1338 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1342 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1351 // Get system font index using indexname
1352 static wxFont
GetSystemFont(const wxString
& name
)
1356 #define SYSFNT(fnt) \
1357 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1358 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1359 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1360 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1361 SYSFNT(wxSYS_SYSTEM_FONT
)
1362 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1363 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1364 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1365 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1372 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1374 wxXmlNode
*font_node
= GetParamNode(param
);
1375 if (font_node
== NULL
)
1377 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1381 wxXmlNode
*oldnode
= m_node
;
1388 bool hasSize
= HasParam(wxT("size"));
1390 isize
= GetLong(wxT("size"), -1);
1393 int istyle
= wxNORMAL
;
1394 bool hasStyle
= HasParam(wxT("style"));
1397 wxString style
= GetParamValue(wxT("style"));
1398 if (style
== wxT("italic"))
1400 else if (style
== wxT("slant"))
1405 int iweight
= wxNORMAL
;
1406 bool hasWeight
= HasParam(wxT("weight"));
1409 wxString weight
= GetParamValue(wxT("weight"));
1410 if (weight
== wxT("bold"))
1412 else if (weight
== wxT("light"))
1417 bool hasUnderlined
= HasParam(wxT("underlined"));
1418 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1420 // family and facename
1421 int ifamily
= wxDEFAULT
;
1422 bool hasFamily
= HasParam(wxT("family"));
1425 wxString family
= GetParamValue(wxT("family"));
1426 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1427 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1428 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1429 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1430 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1431 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1436 bool hasFacename
= HasParam(wxT("face"));
1439 wxString faces
= GetParamValue(wxT("face"));
1440 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1441 wxStringTokenizer
tk(faces
, wxT(","));
1442 while (tk
.HasMoreTokens())
1444 int index
= facenames
.Index(tk
.GetNextToken(), false);
1445 if (index
!= wxNOT_FOUND
)
1447 facename
= facenames
[index
];
1454 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1455 bool hasEncoding
= HasParam(wxT("encoding"));
1458 wxString encoding
= GetParamValue(wxT("encoding"));
1459 wxFontMapper mapper
;
1460 if (!encoding
.empty())
1461 enc
= mapper
.CharsetToEncoding(encoding
);
1462 if (enc
== wxFONTENCODING_SYSTEM
)
1463 enc
= wxFONTENCODING_DEFAULT
;
1466 // is this font based on a system font?
1467 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1471 if (hasSize
&& isize
!= -1)
1472 font
.SetPointSize(isize
);
1473 else if (HasParam(wxT("relativesize")))
1474 font
.SetPointSize(int(font
.GetPointSize() *
1475 GetFloat(wxT("relativesize"))));
1478 font
.SetStyle(istyle
);
1480 font
.SetWeight(iweight
);
1482 font
.SetUnderlined(underlined
);
1484 font
.SetFamily(ifamily
);
1486 font
.SetFaceName(facename
);
1488 font
.SetDefaultEncoding(enc
);
1490 else // not based on system font
1492 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1493 ifamily
, istyle
, iweight
,
1494 underlined
, facename
, enc
);
1502 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1504 //FIXME : add cursor
1506 if (HasParam(wxT("exstyle")))
1507 // Have to OR it with existing style, since
1508 // some implementations (e.g. wxGTK) use the extra style
1510 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1511 if (HasParam(wxT("bg")))
1512 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1513 if (HasParam(wxT("fg")))
1514 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1515 if (GetBool(wxT("enabled"), 1) == 0)
1517 if (GetBool(wxT("focused"), 0) == 1)
1519 if (GetBool(wxT("hidden"), 0) == 1)
1522 if (HasParam(wxT("tooltip")))
1523 wnd
->SetToolTip(GetText(wxT("tooltip")));
1525 if (HasParam(wxT("font")))
1526 wnd
->SetFont(GetFont());
1527 if (HasParam(wxT("help")))
1528 wnd
->SetHelpText(GetText(wxT("help")));
1532 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1534 wxXmlNode
*n
= m_node
->GetChildren();
1538 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1539 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1541 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1542 this_hnd_only
? this : NULL
);
1549 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1552 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1553 wxXmlNode
*n
= root
->GetChildren();
1557 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1559 CreateResource(n
, parent
, NULL
);
1571 // --------------- XRCID implementation -----------------------------
1573 #define XRCID_TABLE_SIZE 1024
1578 /* Hold the id so that once an id is allocated for a name, it
1579 does not get created again by NewControlId at least
1580 until we are done with it */
1586 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1588 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1592 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1593 index
%= XRCID_TABLE_SIZE
;
1595 XRCID_record
*oldrec
= NULL
;
1596 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1598 if (wxStrcmp(rec
->key
, str_id
) == 0)
1605 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1606 &XRCID_Records
[index
] : &oldrec
->next
;
1607 *rec_var
= new XRCID_record
;
1608 (*rec_var
)->key
= wxStrdup(str_id
);
1609 (*rec_var
)->next
= NULL
;
1612 if (value_if_not_found
!= wxID_NONE
)
1613 (*rec_var
)->id
= value_if_not_found
;
1616 int asint
= wxStrtol(str_id
, &end
, 10);
1617 if (*str_id
&& *end
== 0)
1619 // if str_id was integer, keep it verbosely:
1620 (*rec_var
)->id
= asint
;
1624 (*rec_var
)->id
= wxWindowBase::NewControlId();
1628 return (*rec_var
)->id
;
1631 static void AddStdXRCID_Records();
1634 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
1636 static bool s_stdIDsAdded
= false;
1638 if ( !s_stdIDsAdded
)
1640 s_stdIDsAdded
= true;
1641 AddStdXRCID_Records();
1644 return XRCID_Lookup(str_id
, value_if_not_found
);
1648 static void CleanXRCID_Record(XRCID_record
*rec
)
1652 CleanXRCID_Record(rec
->next
);
1659 static void CleanXRCID_Records()
1661 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1663 CleanXRCID_Record(XRCID_Records
[i
]);
1664 XRCID_Records
[i
] = NULL
;
1668 static void AddStdXRCID_Records()
1670 #define stdID(id) XRCID_Lookup(#id, id)
1674 stdID(wxID_SEPARATOR
);
1687 stdID(wxID_PRINT_SETUP
);
1688 stdID(wxID_PAGE_SETUP
);
1689 stdID(wxID_PREVIEW
);
1691 stdID(wxID_HELP_CONTENTS
);
1692 stdID(wxID_HELP_COMMANDS
);
1693 stdID(wxID_HELP_PROCEDURES
);
1694 stdID(wxID_HELP_CONTEXT
);
1695 stdID(wxID_CLOSE_ALL
);
1696 stdID(wxID_PREFERENCES
);
1703 stdID(wxID_DUPLICATE
);
1704 stdID(wxID_SELECTALL
);
1706 stdID(wxID_REPLACE
);
1707 stdID(wxID_REPLACE_ALL
);
1708 stdID(wxID_PROPERTIES
);
1709 stdID(wxID_VIEW_DETAILS
);
1710 stdID(wxID_VIEW_LARGEICONS
);
1711 stdID(wxID_VIEW_SMALLICONS
);
1712 stdID(wxID_VIEW_LIST
);
1713 stdID(wxID_VIEW_SORTDATE
);
1714 stdID(wxID_VIEW_SORTNAME
);
1715 stdID(wxID_VIEW_SORTSIZE
);
1716 stdID(wxID_VIEW_SORTTYPE
);
1732 stdID(wxID_FORWARD
);
1733 stdID(wxID_BACKWARD
);
1734 stdID(wxID_DEFAULT
);
1738 stdID(wxID_CONTEXT_HELP
);
1739 stdID(wxID_YESTOALL
);
1740 stdID(wxID_NOTOALL
);
1749 stdID(wxID_REFRESH
);
1754 stdID(wxID_JUSTIFY_CENTER
);
1755 stdID(wxID_JUSTIFY_FILL
);
1756 stdID(wxID_JUSTIFY_RIGHT
);
1757 stdID(wxID_JUSTIFY_LEFT
);
1758 stdID(wxID_UNDERLINE
);
1760 stdID(wxID_UNINDENT
);
1761 stdID(wxID_ZOOM_100
);
1762 stdID(wxID_ZOOM_FIT
);
1763 stdID(wxID_ZOOM_IN
);
1764 stdID(wxID_ZOOM_OUT
);
1765 stdID(wxID_UNDELETE
);
1766 stdID(wxID_REVERT_TO_SAVED
);
1767 stdID(wxID_SYSTEM_MENU
);
1768 stdID(wxID_CLOSE_FRAME
);
1769 stdID(wxID_MOVE_FRAME
);
1770 stdID(wxID_RESIZE_FRAME
);
1771 stdID(wxID_MAXIMIZE_FRAME
);
1772 stdID(wxID_ICONIZE_FRAME
);
1773 stdID(wxID_RESTORE_FRAME
);
1782 // --------------- module and globals -----------------------------
1784 class wxXmlResourceModule
: public wxModule
1786 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1788 wxXmlResourceModule() {}
1791 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1796 delete wxXmlResource::Set(NULL
);
1797 if(wxXmlResource::ms_subclassFactories
)
1799 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
1800 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
1804 wxDELETE(wxXmlResource::ms_subclassFactories
);
1806 CleanXRCID_Records();
1810 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1813 // When wxXml is loaded dynamically after the application is already running
1814 // then the built-in module system won't pick this one up. Add it manually.
1815 void wxXmlInitResourceModule()
1817 wxModule
* module = new wxXmlResourceModule
;
1819 wxModule::RegisterModule(module);