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"
50 class wxXmlResourceDataRecord
53 wxXmlResourceDataRecord() : Doc(NULL
) {
55 Time
= wxDateTime::Now();
58 ~wxXmlResourceDataRecord() {delete Doc
;}
67 class wxXmlResourceDataRecords
: public wxVector
<wxXmlResourceDataRecord
>
69 // this is a class so that it can be forward-declared
73 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
75 /*static*/ wxXmlResource
*wxXmlResource::Get()
78 ms_instance
= new wxXmlResource
;
82 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
84 wxXmlResource
*old
= ms_instance
;
89 wxXmlResource::wxXmlResource(int flags
, const wxString
& domain
)
93 m_data
= new wxXmlResourceDataRecords
;
97 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
101 m_data
= new wxXmlResourceDataRecords
;
106 wxXmlResource::~wxXmlResource()
113 void wxXmlResource::SetDomain(const wxString
& domain
)
120 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
122 wxString
fnd(filename
);
124 // NB: as Load() and Unload() accept both filenames and URLs (should
125 // probably be changed to filenames only, but embedded resources
126 // currently rely on its ability to handle URLs - FIXME) we need to
127 // determine whether found name is filename and not URL and this is the
128 // fastest/simplest way to do it
129 if (wxFileName::FileExists(fnd
))
131 // Make the name absolute filename, because the app may
132 // change working directory later:
137 fnd
= fn
.GetFullPath();
140 fnd
= wxFileSystem::FileNameToURL(fnd
);
150 bool wxXmlResource::IsArchive(const wxString
& filename
)
152 const wxString fnd
= filename
.Lower();
154 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
157 #endif // wxUSE_FILESYSTEM
159 bool wxXmlResource::Load(const wxString
& filemask
)
162 bool iswild
= wxIsWild(filemask
);
167 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
168 # define wxXmlFindNext fsys.FindNext()
170 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
171 # define wxXmlFindNext wxFindNextFile()
174 fnd
= wxXmlFindFirst
;
179 fnd
= ConvertFileNameToURL(fnd
);
182 if ( IsArchive(fnd
) )
184 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
186 else // a single resource URL
187 #endif // wxUSE_FILESYSTEM
189 wxXmlResourceDataRecord drec
;
191 Data().push_back(drec
);
199 # undef wxXmlFindFirst
200 # undef wxXmlFindNext
201 return rt
&& UpdateResources();
204 bool wxXmlResource::Unload(const wxString
& filename
)
206 wxASSERT_MSG( !wxIsWild(filename
),
207 _T("wildcards not supported by wxXmlResource::Unload()") );
209 wxString fnd
= ConvertFileNameToURL(filename
);
211 const bool isArchive
= IsArchive(fnd
);
214 #endif // wxUSE_FILESYSTEM
216 bool unloaded
= false;
217 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
218 i
!= Data().end(); ++i
)
223 if ( i
->File
.StartsWith(fnd
) )
225 // don't break from the loop, we can have other matching files
227 else // a single resource URL
228 #endif // wxUSE_FILESYSTEM
230 if ( i
->File
== fnd
)
235 // no sense in continuing, there is only one file with this URL
245 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
247 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
249 m_handlers
.push_back(handler
);
250 handler
->SetParentResource(this);
253 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
255 m_handlers
.insert(m_handlers
.begin(), handler
);
256 handler
->SetParentResource(this);
261 void wxXmlResource::ClearHandlers()
263 for ( wxVector
<wxXmlResourceHandler
*>::iterator i
= m_handlers
.begin();
264 i
!= m_handlers
.end(); ++i
)
270 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
272 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
277 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
279 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
285 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
287 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
292 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
294 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
297 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
299 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
304 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
306 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
309 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
311 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
314 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
316 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
319 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
321 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
324 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
326 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
327 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
330 if (bmp
) { rt
= *bmp
; delete bmp
; }
334 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
336 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
337 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
340 if (icon
) { rt
= *icon
; delete icon
; }
345 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
347 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
350 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
352 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
356 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
357 wxWindow
*control
, wxWindow
*parent
)
360 parent
= control
->GetParent();
361 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
364 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
367 return control
->Reparent(container
);
371 static void ProcessPlatformProperty(wxXmlNode
*node
)
376 wxXmlNode
*c
= node
->GetChildren();
380 if (!c
->GetAttribute(wxT("platform"), &s
))
384 wxStringTokenizer
tkn(s
, wxT(" |"));
386 while (tkn
.HasMoreTokens())
388 s
= tkn
.GetNextToken();
390 if (s
== wxT("win")) isok
= true;
392 #if defined(__MAC__) || defined(__APPLE__)
393 if (s
== wxT("mac")) isok
= true;
394 #elif defined(__UNIX__)
395 if (s
== wxT("unix")) isok
= true;
398 if (s
== wxT("os2")) isok
= true;
408 ProcessPlatformProperty(c
);
413 wxXmlNode
*c2
= c
->GetNext();
414 node
->RemoveChild(c
);
423 bool wxXmlResource::UpdateResources()
427 # if wxUSE_FILESYSTEM
428 wxFSFile
*file
= NULL
;
433 wxString
encoding(wxT("UTF-8"));
434 #if !wxUSE_UNICODE && wxUSE_INTL
435 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
437 // In case we are not using wxLocale to translate strings, convert the
438 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
439 // is on, because it could break wxGetTranslation lookup.
440 encoding
= wxLocale::GetSystemEncodingName();
444 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
445 i
!= Data().end(); ++i
)
447 modif
= (i
->Doc
== NULL
);
449 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
451 # if wxUSE_FILESYSTEM
452 file
= fsys
.OpenFile(i
->File
);
454 modif
= file
&& file
->GetModificationTime() > i
->Time
;
455 # else // wxUSE_DATETIME
457 # endif // wxUSE_DATETIME
460 wxLogError(_("Cannot open file '%s'."), i
->File
.c_str());
465 # else // wxUSE_FILESYSTEM
467 modif
= wxDateTime(wxFileModificationTime(i
->File
)) > i
->Time
;
468 # else // wxUSE_DATETIME
470 # endif // wxUSE_DATETIME
471 # endif // wxUSE_FILESYSTEM
476 wxLogTrace(_T("xrc"),
477 _T("opening file '%s'"), i
->File
.c_str());
479 wxInputStream
*stream
= NULL
;
481 # if wxUSE_FILESYSTEM
482 file
= fsys
.OpenFile(i
->File
);
484 stream
= file
->GetStream();
486 stream
= new wxFileInputStream(i
->File
);
492 i
->Doc
= new wxXmlDocument
;
494 if (!stream
|| !i
->Doc
->Load(*stream
, encoding
))
496 wxLogError(_("Cannot load resources from file '%s'."),
501 else if (i
->Doc
->GetRoot()->GetName() != wxT("resource"))
503 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), i
->File
.c_str());
511 wxString verstr
= i
->Doc
->GetRoot()->GetAttribute(
512 wxT("version"), wxT("0.0.0.0"));
513 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
514 &v1
, &v2
, &v3
, &v4
) == 4)
515 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
520 if (m_version
!= version
)
522 wxLogError(_("Resource files must have same version number!"));
526 ProcessPlatformProperty(i
->Doc
->GetRoot());
529 i
->Time
= file
->GetModificationTime();
530 #else // wxUSE_FILESYSTEM
531 i
->Time
= wxDateTime(wxFileModificationTime(i
->File
));
532 #endif // wxUSE_FILESYSTEM
533 #endif // wxUSE_DATETIME
536 # if wxUSE_FILESYSTEM
549 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
550 const wxString
& name
,
551 const wxString
& classname
,
557 // first search for match at the top-level nodes (as this is
558 // where the resource is most commonly looked for):
559 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
561 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
562 (node
->GetName() == wxT("object") ||
563 node
->GetName() == wxT("object_ref")) &&
564 node
->GetAttribute(wxT("name"), &dummy
) && dummy
== name
)
566 wxString
cls(node
->GetAttribute(wxT("class"), wxEmptyString
));
567 if (!classname
|| cls
== classname
)
569 // object_ref may not have 'class' attribute:
570 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
572 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
575 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
577 refNode
->GetAttribute(wxT("class"), wxEmptyString
) == classname
)
586 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
588 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
589 (node
->GetName() == wxT("object") ||
590 node
->GetName() == wxT("object_ref")) )
592 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
601 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
602 const wxString
& classname
,
605 UpdateResources(); //ensure everything is up-to-date
608 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
609 f
!= Data().end(); ++f
)
611 if ( f
->Doc
== NULL
|| f
->Doc
->GetRoot() == NULL
)
614 wxXmlNode
* found
= DoFindResource(f
->Doc
->GetRoot(),
615 name
, classname
, recursive
);
619 m_curFileSystem
.ChangePathTo(f
->File
);
625 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
626 name
.c_str(), classname
.c_str());
630 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
633 for ( wxXmlAttribute
*attr
= with
.GetAttributes();
634 attr
; attr
= attr
->GetNext() )
636 wxXmlAttribute
*dattr
;
637 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
640 if ( dattr
->GetName() == attr
->GetName() )
642 dattr
->SetValue(attr
->GetValue());
648 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
651 // Merge child nodes:
652 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
654 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
657 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
659 if ( dnode
->GetName() == node
->GetName() &&
660 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
661 dnode
->GetType() == node
->GetType() )
663 MergeNodes(*dnode
, *node
);
670 static const wxChar
*AT_END
= wxT("end");
671 wxString insert_pos
= node
->GetAttribute(wxT("insert_at"), AT_END
);
672 if ( insert_pos
== AT_END
)
674 dest
.AddChild(new wxXmlNode(*node
));
676 else if ( insert_pos
== wxT("begin") )
678 dest
.InsertChild(new wxXmlNode(*node
), dest
.GetChildren());
683 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().length() )
684 dest
.SetContent(with
.GetContent());
687 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
689 wxXmlResourceHandler
*handlerToUse
)
691 if (node
== NULL
) return NULL
;
693 // handling of referenced resource
694 if ( node
->GetName() == wxT("object_ref") )
696 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
697 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
701 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
706 wxXmlNode
copy(*refNode
);
707 MergeNodes(copy
, *node
);
709 return CreateResFromNode(©
, parent
, instance
);
714 if (handlerToUse
->CanHandle(node
))
716 return handlerToUse
->CreateResource(node
, parent
, instance
);
719 else if (node
->GetName() == wxT("object"))
721 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
722 h
!= m_handlers
.end(); ++h
)
724 wxXmlResourceHandler
*handler
= *h
;
725 if (handler
->CanHandle(node
))
726 return handler
->CreateResource(node
, parent
, instance
);
730 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
731 node
->GetName().c_str(),
732 node
->GetAttribute(wxT("class"), wxEmptyString
).c_str());
737 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
739 // this is a class so that it can be forward-declared
742 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
744 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
746 if (!ms_subclassFactories
)
748 ms_subclassFactories
= new wxXmlSubclassFactories
;
750 ms_subclassFactories
->push_back(factory
);
753 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
756 ~wxXmlSubclassFactoryCXX() {}
758 wxObject
*Create(const wxString
& className
)
760 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
763 return classInfo
->CreateObject();
772 wxXmlResourceHandler::wxXmlResourceHandler()
773 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
774 m_parentAsWindow(NULL
)
779 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
781 wxXmlNode
*myNode
= m_node
;
782 wxString myClass
= m_class
;
783 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
784 wxWindow
*myParentAW
= m_parentAsWindow
;
786 m_instance
= instance
;
787 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
788 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
790 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
791 if (!subclass
.empty())
793 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
794 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
796 m_instance
= (*i
)->Create(subclass
);
803 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
804 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
805 subclass
.c_str(), name
.c_str());
811 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
813 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
815 wxObject
*returned
= DoCreateResource();
819 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
820 m_instance
= myInstance
;
826 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
828 m_styleNames
.Add(name
);
829 m_styleValues
.Add(value
);
834 void wxXmlResourceHandler::AddWindowStyles()
836 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
838 // the border styles all have the old and new names, recognize both for now
839 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
840 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
841 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
842 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
843 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
844 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
846 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
847 XRC_ADD_STYLE(wxWANTS_CHARS
);
848 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
849 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
850 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
851 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
852 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
853 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
858 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
860 return (GetParamNode(param
) != NULL
);
864 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
866 wxString s
= GetParamValue(param
);
868 if (!s
) return defaults
;
870 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
874 while (tkn
.HasMoreTokens())
876 fl
= tkn
.GetNextToken();
877 index
= m_styleNames
.Index(fl
);
878 if (index
!= wxNOT_FOUND
)
879 style
|= m_styleValues
[index
];
881 wxLogError(_("Unknown style flag ") + fl
);
888 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
890 wxXmlNode
*parNode
= GetParamNode(param
);
891 wxString
str1(GetNodeContent(parNode
));
896 // VS: First version of XRC resources used $ instead of & (which is
897 // illegal in XML), but later I realized that '_' fits this purpose
898 // much better (because &File means "File with F underlined").
899 if (m_resource
->CompareVersion(2,3,0,1) < 0)
904 for (dt
= str1
.c_str(); *dt
; dt
++)
906 // Remap amp_char to &, map double amp_char to amp_char (for things
907 // like "&File..." -- this is illegal in XML, so we use "_File..."):
910 if ( *(++dt
) == amp_char
)
913 str2
<< wxT('&') << *dt
;
915 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
916 else if (*dt
== wxT('\\'))
932 // "\\" wasn't translated to "\" prior to 2.5.3.0:
933 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
938 // else fall-through to default: branch below
941 str2
<< wxT('\\') << *dt
;
947 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
949 if (translate
&& parNode
&&
950 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
952 return wxGetTranslation(str2
, m_resource
->GetDomain());
959 // The string is internally stored as UTF-8, we have to convert
960 // it into system's default encoding so that it can be displayed:
961 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
966 // If wxXRC_USE_LOCALE is not set, then the string is already in
967 // system's default encoding in ANSI build, so we don't have to
968 // do anything special here.
974 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
977 wxString str1
= GetParamValue(param
);
979 if (!str1
.ToLong(&value
))
985 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
987 wxString str
= GetParamValue(param
);
989 // strings in XRC always use C locale but wxString::ToDouble() uses the
990 // current one, so transform the string to it supposing that the only
991 // difference between them is the decimal separator
993 // TODO: use wxString::ToCDouble() when we have it
994 str
.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
995 wxLOCALE_CAT_NUMBER
));
998 if (!str
.ToDouble(&value
))
1001 return wx_truncate_cast(float, value
);
1005 int wxXmlResourceHandler::GetID()
1007 return wxXmlResource::GetXRCID(GetName());
1012 wxString
wxXmlResourceHandler::GetName()
1014 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1019 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1021 wxString v
= GetParamValue(param
);
1023 if (!v
) return defaultv
;
1025 return (v
== wxT("1"));
1029 static wxColour
GetSystemColour(const wxString
& name
)
1033 #define SYSCLR(clr) \
1034 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1035 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1036 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1037 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1038 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1039 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1040 SYSCLR(wxSYS_COLOUR_MENU
)
1041 SYSCLR(wxSYS_COLOUR_WINDOW
)
1042 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1043 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1044 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1045 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1046 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1047 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1048 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1049 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1050 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1051 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1052 SYSCLR(wxSYS_COLOUR_3DFACE
)
1053 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1054 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1055 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1056 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1057 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1058 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1059 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1060 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1061 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1062 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1063 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1064 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1065 SYSCLR(wxSYS_COLOUR_INFOBK
)
1066 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1067 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1068 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1069 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1070 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1071 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1075 return wxNullColour
;
1078 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1080 wxString v
= GetParamValue(param
);
1087 // wxString -> wxColour conversion
1090 // the colour doesn't use #RRGGBB format, check if it is symbolic
1092 clr
= GetSystemColour(v
);
1096 wxLogError(_("XRC resource: Incorrect colour specification '%s' for attribute '%s'."),
1097 v
.c_str(), param
.c_str());
1098 return wxNullColour
;
1106 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1107 const wxArtClient
& defaultArtClient
,
1110 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1111 wxXmlNode
*bmpNode
= GetParamNode(param
);
1114 wxString sid
= bmpNode
->GetAttribute(wxT("stock_id"), wxEmptyString
);
1117 wxString scl
= bmpNode
->GetAttribute(wxT("stock_client"), wxEmptyString
);
1119 scl
= defaultArtClient
;
1121 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1124 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1126 if ( stockArt
.Ok() )
1131 /* ...or load the bitmap from file: */
1132 wxString name
= GetParamValue(param
);
1133 if (name
.empty()) return wxNullBitmap
;
1134 #if wxUSE_FILESYSTEM
1135 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1138 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1140 return wxNullBitmap
;
1142 wxImage
img(*(fsfile
->GetStream()));
1150 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1152 return wxNullBitmap
;
1154 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1155 return wxBitmap(img
);
1158 #if wxUSE_ANIMATIONCTRL
1159 wxAnimation
wxXmlResourceHandler::GetAnimation(const wxString
& param
)
1163 /* load the animation from file: */
1164 wxString name
= GetParamValue(param
);
1165 if (name
.empty()) return wxNullAnimation
;
1166 #if wxUSE_FILESYSTEM
1167 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1170 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1172 return wxNullAnimation
;
1174 ani
.Load(*(fsfile
->GetStream()));
1182 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1184 return wxNullAnimation
;
1189 #endif // wxUSE_ANIMATIONCTRL
1193 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1194 const wxArtClient
& defaultArtClient
,
1198 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1204 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1206 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1208 wxXmlNode
*n
= m_node
->GetChildren();
1212 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1221 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1223 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1228 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1230 wxXmlNode
*n
= node
;
1231 if (n
== NULL
) return wxEmptyString
;
1232 n
= n
->GetChildren();
1236 if (n
->GetType() == wxXML_TEXT_NODE
||
1237 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1238 return n
->GetContent();
1241 return wxEmptyString
;
1246 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1249 return GetNodeContent(m_node
);
1251 return GetNodeContent(GetParamNode(param
));
1256 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1257 wxWindow
*windowToUse
)
1259 wxString s
= GetParamValue(param
);
1260 if (s
.empty()) s
= wxT("-1,-1");
1264 is_dlg
= s
[s
.length()-1] == wxT('d');
1265 if (is_dlg
) s
.RemoveLast();
1267 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1268 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1270 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1271 return wxDefaultSize
;
1278 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1280 else if (m_parentAsWindow
)
1282 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1286 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1287 return wxDefaultSize
;
1291 return wxSize(sx
, sy
);
1296 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1298 wxSize sz
= GetSize(param
);
1299 return wxPoint(sz
.x
, sz
.y
);
1304 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1306 wxWindow
*windowToUse
)
1308 wxString s
= GetParamValue(param
);
1309 if (s
.empty()) return defaultv
;
1313 is_dlg
= s
[s
.length()-1] == wxT('d');
1314 if (is_dlg
) s
.RemoveLast();
1318 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1326 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1328 else if (m_parentAsWindow
)
1330 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1334 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1343 // Get system font index using indexname
1344 static wxFont
GetSystemFont(const wxString
& name
)
1348 #define SYSFNT(fnt) \
1349 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1350 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1351 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1352 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1353 SYSFNT(wxSYS_SYSTEM_FONT
)
1354 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1355 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1356 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1357 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1364 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1366 wxXmlNode
*font_node
= GetParamNode(param
);
1367 if (font_node
== NULL
)
1369 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1373 wxXmlNode
*oldnode
= m_node
;
1380 bool hasSize
= HasParam(wxT("size"));
1382 isize
= GetLong(wxT("size"), -1);
1385 int istyle
= wxNORMAL
;
1386 bool hasStyle
= HasParam(wxT("style"));
1389 wxString style
= GetParamValue(wxT("style"));
1390 if (style
== wxT("italic"))
1392 else if (style
== wxT("slant"))
1397 int iweight
= wxNORMAL
;
1398 bool hasWeight
= HasParam(wxT("weight"));
1401 wxString weight
= GetParamValue(wxT("weight"));
1402 if (weight
== wxT("bold"))
1404 else if (weight
== wxT("light"))
1409 bool hasUnderlined
= HasParam(wxT("underlined"));
1410 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1412 // family and facename
1413 int ifamily
= wxDEFAULT
;
1414 bool hasFamily
= HasParam(wxT("family"));
1417 wxString family
= GetParamValue(wxT("family"));
1418 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1419 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1420 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1421 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1422 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1423 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1428 bool hasFacename
= HasParam(wxT("face"));
1431 wxString faces
= GetParamValue(wxT("face"));
1432 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1433 wxStringTokenizer
tk(faces
, wxT(","));
1434 while (tk
.HasMoreTokens())
1436 int index
= facenames
.Index(tk
.GetNextToken(), false);
1437 if (index
!= wxNOT_FOUND
)
1439 facename
= facenames
[index
];
1446 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1447 bool hasEncoding
= HasParam(wxT("encoding"));
1450 wxString encoding
= GetParamValue(wxT("encoding"));
1451 wxFontMapper mapper
;
1452 if (!encoding
.empty())
1453 enc
= mapper
.CharsetToEncoding(encoding
);
1454 if (enc
== wxFONTENCODING_SYSTEM
)
1455 enc
= wxFONTENCODING_DEFAULT
;
1458 // is this font based on a system font?
1459 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1463 if (hasSize
&& isize
!= -1)
1464 font
.SetPointSize(isize
);
1465 else if (HasParam(wxT("relativesize")))
1466 font
.SetPointSize(int(font
.GetPointSize() *
1467 GetFloat(wxT("relativesize"))));
1470 font
.SetStyle(istyle
);
1472 font
.SetWeight(iweight
);
1474 font
.SetUnderlined(underlined
);
1476 font
.SetFamily(ifamily
);
1478 font
.SetFaceName(facename
);
1480 font
.SetDefaultEncoding(enc
);
1482 else // not based on system font
1484 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1485 ifamily
, istyle
, iweight
,
1486 underlined
, facename
, enc
);
1494 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1496 //FIXME : add cursor
1498 if (HasParam(wxT("exstyle")))
1499 // Have to OR it with existing style, since
1500 // some implementations (e.g. wxGTK) use the extra style
1502 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1503 if (HasParam(wxT("bg")))
1504 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1505 if (HasParam(wxT("fg")))
1506 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1507 if (GetBool(wxT("enabled"), 1) == 0)
1509 if (GetBool(wxT("focused"), 0) == 1)
1511 if (GetBool(wxT("hidden"), 0) == 1)
1514 if (HasParam(wxT("tooltip")))
1515 wnd
->SetToolTip(GetText(wxT("tooltip")));
1517 if (HasParam(wxT("font")))
1518 wnd
->SetFont(GetFont());
1519 if (HasParam(wxT("help")))
1520 wnd
->SetHelpText(GetText(wxT("help")));
1524 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1526 wxXmlNode
*n
= m_node
->GetChildren();
1530 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1531 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1533 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1534 this_hnd_only
? this : NULL
);
1541 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1544 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1545 wxXmlNode
*n
= root
->GetChildren();
1549 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1551 CreateResource(n
, parent
, NULL
);
1563 // --------------- XRCID implementation -----------------------------
1565 #define XRCID_TABLE_SIZE 1024
1575 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1577 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1581 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1582 index
%= XRCID_TABLE_SIZE
;
1584 XRCID_record
*oldrec
= NULL
;
1585 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1587 if (wxStrcmp(rec
->key
, str_id
) == 0)
1594 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1595 &XRCID_Records
[index
] : &oldrec
->next
;
1596 *rec_var
= new XRCID_record
;
1597 (*rec_var
)->key
= wxStrdup(str_id
);
1598 (*rec_var
)->next
= NULL
;
1601 if (value_if_not_found
!= wxID_NONE
)
1602 (*rec_var
)->id
= value_if_not_found
;
1605 int asint
= wxStrtol(str_id
, &end
, 10);
1606 if (*str_id
&& *end
== 0)
1608 // if str_id was integer, keep it verbosely:
1609 (*rec_var
)->id
= asint
;
1613 (*rec_var
)->id
= wxWindow::NewControlId();
1617 return (*rec_var
)->id
;
1620 static void AddStdXRCID_Records();
1623 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
1625 static bool s_stdIDsAdded
= false;
1627 if ( !s_stdIDsAdded
)
1629 s_stdIDsAdded
= true;
1630 AddStdXRCID_Records();
1633 return XRCID_Lookup(str_id
, value_if_not_found
);
1637 static void CleanXRCID_Record(XRCID_record
*rec
)
1641 CleanXRCID_Record(rec
->next
);
1647 static void CleanXRCID_Records()
1649 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1651 CleanXRCID_Record(XRCID_Records
[i
]);
1652 XRCID_Records
[i
] = NULL
;
1656 static void AddStdXRCID_Records()
1658 #define stdID(id) XRCID_Lookup(#id, id)
1662 stdID(wxID_SEPARATOR
);
1675 stdID(wxID_PRINT_SETUP
);
1676 stdID(wxID_PAGE_SETUP
);
1677 stdID(wxID_PREVIEW
);
1679 stdID(wxID_HELP_CONTENTS
);
1680 stdID(wxID_HELP_COMMANDS
);
1681 stdID(wxID_HELP_PROCEDURES
);
1682 stdID(wxID_HELP_CONTEXT
);
1683 stdID(wxID_CLOSE_ALL
);
1684 stdID(wxID_PREFERENCES
);
1690 stdID(wxID_DUPLICATE
);
1691 stdID(wxID_SELECTALL
);
1693 stdID(wxID_REPLACE
);
1694 stdID(wxID_REPLACE_ALL
);
1695 stdID(wxID_PROPERTIES
);
1696 stdID(wxID_VIEW_DETAILS
);
1697 stdID(wxID_VIEW_LARGEICONS
);
1698 stdID(wxID_VIEW_SMALLICONS
);
1699 stdID(wxID_VIEW_LIST
);
1700 stdID(wxID_VIEW_SORTDATE
);
1701 stdID(wxID_VIEW_SORTNAME
);
1702 stdID(wxID_VIEW_SORTSIZE
);
1703 stdID(wxID_VIEW_SORTTYPE
);
1719 stdID(wxID_FORWARD
);
1720 stdID(wxID_BACKWARD
);
1721 stdID(wxID_DEFAULT
);
1725 stdID(wxID_CONTEXT_HELP
);
1726 stdID(wxID_YESTOALL
);
1727 stdID(wxID_NOTOALL
);
1736 stdID(wxID_REFRESH
);
1741 stdID(wxID_JUSTIFY_CENTER
);
1742 stdID(wxID_JUSTIFY_FILL
);
1743 stdID(wxID_JUSTIFY_RIGHT
);
1744 stdID(wxID_JUSTIFY_LEFT
);
1745 stdID(wxID_UNDERLINE
);
1747 stdID(wxID_UNINDENT
);
1748 stdID(wxID_ZOOM_100
);
1749 stdID(wxID_ZOOM_FIT
);
1750 stdID(wxID_ZOOM_IN
);
1751 stdID(wxID_ZOOM_OUT
);
1752 stdID(wxID_UNDELETE
);
1753 stdID(wxID_REVERT_TO_SAVED
);
1754 stdID(wxID_SYSTEM_MENU
);
1755 stdID(wxID_CLOSE_FRAME
);
1756 stdID(wxID_MOVE_FRAME
);
1757 stdID(wxID_RESIZE_FRAME
);
1758 stdID(wxID_MAXIMIZE_FRAME
);
1759 stdID(wxID_ICONIZE_FRAME
);
1760 stdID(wxID_RESTORE_FRAME
);
1769 // --------------- module and globals -----------------------------
1771 class wxXmlResourceModule
: public wxModule
1773 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1775 wxXmlResourceModule() {}
1778 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1783 delete wxXmlResource::Set(NULL
);
1784 if(wxXmlResource::ms_subclassFactories
)
1786 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
1787 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
1791 wxDELETE(wxXmlResource::ms_subclassFactories
);
1793 CleanXRCID_Records();
1797 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1800 // When wxXml is loaded dynamically after the application is already running
1801 // then the built-in module system won't pick this one up. Add it manually.
1802 void wxXmlInitResourceModule()
1804 wxModule
* module = new wxXmlResourceModule
;
1806 wxModule::RegisterModule(module);