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()
114 void wxXmlResource::SetDomain(const wxString
& domain
)
121 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
123 wxString
fnd(filename
);
125 // NB: as Load() and Unload() accept both filenames and URLs (should
126 // probably be changed to filenames only, but embedded resources
127 // currently rely on its ability to handle URLs - FIXME) we need to
128 // determine whether found name is filename and not URL and this is the
129 // fastest/simplest way to do it
130 if (wxFileName::FileExists(fnd
))
132 // Make the name absolute filename, because the app may
133 // change working directory later:
138 fnd
= fn
.GetFullPath();
141 fnd
= wxFileSystem::FileNameToURL(fnd
);
151 bool wxXmlResource::IsArchive(const wxString
& filename
)
153 const wxString fnd
= filename
.Lower();
155 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
158 #endif // wxUSE_FILESYSTEM
160 bool wxXmlResource::Load(const wxString
& filemask
)
163 bool iswild
= wxIsWild(filemask
);
168 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
169 # define wxXmlFindNext fsys.FindNext()
171 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
172 # define wxXmlFindNext wxFindNextFile()
175 fnd
= wxXmlFindFirst
;
180 fnd
= ConvertFileNameToURL(fnd
);
183 if ( IsArchive(fnd
) )
185 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
187 else // a single resource URL
188 #endif // wxUSE_FILESYSTEM
190 wxXmlResourceDataRecord drec
;
192 Data().push_back(drec
);
200 # undef wxXmlFindFirst
201 # undef wxXmlFindNext
202 return rt
&& UpdateResources();
205 bool wxXmlResource::Unload(const wxString
& filename
)
207 wxASSERT_MSG( !wxIsWild(filename
),
208 _T("wildcards not supported by wxXmlResource::Unload()") );
210 wxString fnd
= ConvertFileNameToURL(filename
);
212 const bool isArchive
= IsArchive(fnd
);
215 #endif // wxUSE_FILESYSTEM
217 bool unloaded
= false;
218 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
219 i
!= Data().end(); ++i
)
224 if ( i
->File
.StartsWith(fnd
) )
226 // don't break from the loop, we can have other matching files
228 else // a single resource URL
229 #endif // wxUSE_FILESYSTEM
231 if ( i
->File
== fnd
)
236 // no sense in continuing, there is only one file with this URL
246 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
248 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
250 m_handlers
.push_back(handler
);
251 handler
->SetParentResource(this);
254 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
256 m_handlers
.insert(m_handlers
.begin(), handler
);
257 handler
->SetParentResource(this);
262 void wxXmlResource::ClearHandlers()
264 for ( wxVector
<wxXmlResourceHandler
*>::iterator i
= m_handlers
.begin();
265 i
!= m_handlers
.end(); ++i
)
271 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
273 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
278 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
280 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
286 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
288 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
293 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
295 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
298 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
300 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
305 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
307 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
310 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
312 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
315 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
317 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
320 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
322 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
325 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
327 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
328 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
331 if (bmp
) { rt
= *bmp
; delete bmp
; }
335 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
337 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
338 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
341 if (icon
) { rt
= *icon
; delete icon
; }
346 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
348 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
351 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
353 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
357 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
358 wxWindow
*control
, wxWindow
*parent
)
361 parent
= control
->GetParent();
362 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
365 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
368 return control
->Reparent(container
);
372 static void ProcessPlatformProperty(wxXmlNode
*node
)
377 wxXmlNode
*c
= node
->GetChildren();
381 if (!c
->GetAttribute(wxT("platform"), &s
))
385 wxStringTokenizer
tkn(s
, wxT(" |"));
387 while (tkn
.HasMoreTokens())
389 s
= tkn
.GetNextToken();
391 if (s
== wxT("win")) isok
= true;
393 #if defined(__MAC__) || defined(__APPLE__)
394 if (s
== wxT("mac")) isok
= true;
395 #elif defined(__UNIX__)
396 if (s
== wxT("unix")) isok
= true;
399 if (s
== wxT("os2")) isok
= true;
409 ProcessPlatformProperty(c
);
414 wxXmlNode
*c2
= c
->GetNext();
415 node
->RemoveChild(c
);
424 bool wxXmlResource::UpdateResources()
428 # if wxUSE_FILESYSTEM
429 wxFSFile
*file
= NULL
;
434 wxString
encoding(wxT("UTF-8"));
435 #if !wxUSE_UNICODE && wxUSE_INTL
436 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
438 // In case we are not using wxLocale to translate strings, convert the
439 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
440 // is on, because it could break wxGetTranslation lookup.
441 encoding
= wxLocale::GetSystemEncodingName();
445 for ( wxXmlResourceDataRecords::iterator i
= Data().begin();
446 i
!= Data().end(); ++i
)
448 modif
= (i
->Doc
== NULL
);
450 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
452 # if wxUSE_FILESYSTEM
453 file
= fsys
.OpenFile(i
->File
);
455 modif
= file
&& file
->GetModificationTime() > i
->Time
;
456 # else // wxUSE_DATETIME
458 # endif // wxUSE_DATETIME
461 wxLogError(_("Cannot open file '%s'."), i
->File
.c_str());
466 # else // wxUSE_FILESYSTEM
468 modif
= wxDateTime(wxFileModificationTime(i
->File
)) > i
->Time
;
469 # else // wxUSE_DATETIME
471 # endif // wxUSE_DATETIME
472 # endif // wxUSE_FILESYSTEM
477 wxLogTrace(_T("xrc"),
478 _T("opening file '%s'"), i
->File
.c_str());
480 wxInputStream
*stream
= NULL
;
482 # if wxUSE_FILESYSTEM
483 file
= fsys
.OpenFile(i
->File
);
485 stream
= file
->GetStream();
487 stream
= new wxFileInputStream(i
->File
);
493 i
->Doc
= new wxXmlDocument
;
495 if (!stream
|| !i
->Doc
->Load(*stream
, encoding
))
497 wxLogError(_("Cannot load resources from file '%s'."),
502 else if (i
->Doc
->GetRoot()->GetName() != wxT("resource"))
504 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), i
->File
.c_str());
512 wxString verstr
= i
->Doc
->GetRoot()->GetAttribute(
513 wxT("version"), wxT("0.0.0.0"));
514 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
515 &v1
, &v2
, &v3
, &v4
) == 4)
516 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
521 if (m_version
!= version
)
523 wxLogError(_("Resource files must have same version number!"));
527 ProcessPlatformProperty(i
->Doc
->GetRoot());
530 i
->Time
= file
->GetModificationTime();
531 #else // wxUSE_FILESYSTEM
532 i
->Time
= wxDateTime(wxFileModificationTime(i
->File
));
533 #endif // wxUSE_FILESYSTEM
534 #endif // wxUSE_DATETIME
537 # if wxUSE_FILESYSTEM
550 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
551 const wxString
& name
,
552 const wxString
& classname
,
558 // first search for match at the top-level nodes (as this is
559 // where the resource is most commonly looked for):
560 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
562 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
563 (node
->GetName() == wxT("object") ||
564 node
->GetName() == wxT("object_ref")) &&
565 node
->GetAttribute(wxT("name"), &dummy
) && dummy
== name
)
567 wxString
cls(node
->GetAttribute(wxT("class"), wxEmptyString
));
568 if (!classname
|| cls
== classname
)
570 // object_ref may not have 'class' attribute:
571 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
573 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
576 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
578 refNode
->GetAttribute(wxT("class"), wxEmptyString
) == classname
)
587 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
589 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
590 (node
->GetName() == wxT("object") ||
591 node
->GetName() == wxT("object_ref")) )
593 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
602 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
603 const wxString
& classname
,
606 UpdateResources(); //ensure everything is up-to-date
609 for ( wxXmlResourceDataRecords::const_iterator f
= Data().begin();
610 f
!= Data().end(); ++f
)
612 if ( f
->Doc
== NULL
|| f
->Doc
->GetRoot() == NULL
)
615 wxXmlNode
* found
= DoFindResource(f
->Doc
->GetRoot(),
616 name
, classname
, recursive
);
620 m_curFileSystem
.ChangePathTo(f
->File
);
626 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
627 name
.c_str(), classname
.c_str());
631 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
634 for ( wxXmlAttribute
*attr
= with
.GetAttributes();
635 attr
; attr
= attr
->GetNext() )
637 wxXmlAttribute
*dattr
;
638 for (dattr
= dest
.GetAttributes(); dattr
; dattr
= dattr
->GetNext())
641 if ( dattr
->GetName() == attr
->GetName() )
643 dattr
->SetValue(attr
->GetValue());
649 dest
.AddAttribute(attr
->GetName(), attr
->GetValue());
652 // Merge child nodes:
653 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
655 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
658 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
660 if ( dnode
->GetName() == node
->GetName() &&
661 dnode
->GetAttribute(wxT("name"), wxEmptyString
) == name
&&
662 dnode
->GetType() == node
->GetType() )
664 MergeNodes(*dnode
, *node
);
671 static const wxChar
*AT_END
= wxT("end");
672 wxString insert_pos
= node
->GetAttribute(wxT("insert_at"), AT_END
);
673 if ( insert_pos
== AT_END
)
675 dest
.AddChild(new wxXmlNode(*node
));
677 else if ( insert_pos
== wxT("begin") )
679 dest
.InsertChild(new wxXmlNode(*node
), dest
.GetChildren());
684 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().length() )
685 dest
.SetContent(with
.GetContent());
688 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
690 wxXmlResourceHandler
*handlerToUse
)
692 if (node
== NULL
) return NULL
;
694 // handling of referenced resource
695 if ( node
->GetName() == wxT("object_ref") )
697 wxString refName
= node
->GetAttribute(wxT("ref"), wxEmptyString
);
698 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
702 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
707 wxXmlNode
copy(*refNode
);
708 MergeNodes(copy
, *node
);
710 return CreateResFromNode(©
, parent
, instance
);
715 if (handlerToUse
->CanHandle(node
))
717 return handlerToUse
->CreateResource(node
, parent
, instance
);
720 else if (node
->GetName() == wxT("object"))
722 for ( wxVector
<wxXmlResourceHandler
*>::iterator h
= m_handlers
.begin();
723 h
!= m_handlers
.end(); ++h
)
725 wxXmlResourceHandler
*handler
= *h
;
726 if (handler
->CanHandle(node
))
727 return handler
->CreateResource(node
, parent
, instance
);
731 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
732 node
->GetName().c_str(),
733 node
->GetAttribute(wxT("class"), wxEmptyString
).c_str());
738 class wxXmlSubclassFactories
: public wxVector
<wxXmlSubclassFactory
*>
740 // this is a class so that it can be forward-declared
743 wxXmlSubclassFactories
*wxXmlResource::ms_subclassFactories
= NULL
;
745 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
747 if (!ms_subclassFactories
)
749 ms_subclassFactories
= new wxXmlSubclassFactories
;
751 ms_subclassFactories
->push_back(factory
);
754 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
757 ~wxXmlSubclassFactoryCXX() {}
759 wxObject
*Create(const wxString
& className
)
761 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
764 return classInfo
->CreateObject();
773 wxXmlResourceHandler::wxXmlResourceHandler()
774 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
775 m_parentAsWindow(NULL
)
780 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
782 wxXmlNode
*myNode
= m_node
;
783 wxString myClass
= m_class
;
784 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
785 wxWindow
*myParentAW
= m_parentAsWindow
;
787 m_instance
= instance
;
788 if (!m_instance
&& node
->HasAttribute(wxT("subclass")) &&
789 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
791 wxString subclass
= node
->GetAttribute(wxT("subclass"), wxEmptyString
);
792 if (!subclass
.empty())
794 for (wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
795 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
797 m_instance
= (*i
)->Create(subclass
);
804 wxString name
= node
->GetAttribute(wxT("name"), wxEmptyString
);
805 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
806 subclass
.c_str(), name
.c_str());
812 m_class
= node
->GetAttribute(wxT("class"), wxEmptyString
);
814 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
816 wxObject
*returned
= DoCreateResource();
820 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
821 m_instance
= myInstance
;
827 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
829 m_styleNames
.Add(name
);
830 m_styleValues
.Add(value
);
835 void wxXmlResourceHandler::AddWindowStyles()
837 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
839 // the border styles all have the old and new names, recognize both for now
840 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
841 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
842 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
843 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
844 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
845 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
847 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
848 XRC_ADD_STYLE(wxWANTS_CHARS
);
849 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
850 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
851 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
852 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
853 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
854 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
859 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
861 return (GetParamNode(param
) != NULL
);
865 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
867 wxString s
= GetParamValue(param
);
869 if (!s
) return defaults
;
871 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
875 while (tkn
.HasMoreTokens())
877 fl
= tkn
.GetNextToken();
878 index
= m_styleNames
.Index(fl
);
879 if (index
!= wxNOT_FOUND
)
880 style
|= m_styleValues
[index
];
882 wxLogError(_("Unknown style flag ") + fl
);
889 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
891 wxXmlNode
*parNode
= GetParamNode(param
);
892 wxString
str1(GetNodeContent(parNode
));
897 // VS: First version of XRC resources used $ instead of & (which is
898 // illegal in XML), but later I realized that '_' fits this purpose
899 // much better (because &File means "File with F underlined").
900 if (m_resource
->CompareVersion(2,3,0,1) < 0)
905 for (dt
= str1
.c_str(); *dt
; dt
++)
907 // Remap amp_char to &, map double amp_char to amp_char (for things
908 // like "&File..." -- this is illegal in XML, so we use "_File..."):
911 if ( *(++dt
) == amp_char
)
914 str2
<< wxT('&') << *dt
;
916 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
917 else if (*dt
== wxT('\\'))
933 // "\\" wasn't translated to "\" prior to 2.5.3.0:
934 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
939 // else fall-through to default: branch below
942 str2
<< wxT('\\') << *dt
;
948 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
950 if (translate
&& parNode
&&
951 parNode
->GetAttribute(wxT("translate"), wxEmptyString
) != wxT("0"))
953 return wxGetTranslation(str2
, m_resource
->GetDomain());
960 // The string is internally stored as UTF-8, we have to convert
961 // it into system's default encoding so that it can be displayed:
962 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
967 // If wxXRC_USE_LOCALE is not set, then the string is already in
968 // system's default encoding in ANSI build, so we don't have to
969 // do anything special here.
975 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
978 wxString str1
= GetParamValue(param
);
980 if (!str1
.ToLong(&value
))
986 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
988 wxString str
= GetParamValue(param
);
990 // strings in XRC always use C locale but wxString::ToDouble() uses the
991 // current one, so transform the string to it supposing that the only
992 // difference between them is the decimal separator
994 // TODO: use wxString::ToCDouble() when we have it
995 str
.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT
,
996 wxLOCALE_CAT_NUMBER
));
999 if (!str
.ToDouble(&value
))
1002 return wx_truncate_cast(float, value
);
1006 int wxXmlResourceHandler::GetID()
1008 return wxXmlResource::GetXRCID(GetName());
1013 wxString
wxXmlResourceHandler::GetName()
1015 return m_node
->GetAttribute(wxT("name"), wxT("-1"));
1020 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1022 wxString v
= GetParamValue(param
);
1024 if (!v
) return defaultv
;
1026 return (v
== wxT("1"));
1030 static wxColour
GetSystemColour(const wxString
& name
)
1034 #define SYSCLR(clr) \
1035 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1036 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1037 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1038 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1039 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1040 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1041 SYSCLR(wxSYS_COLOUR_MENU
)
1042 SYSCLR(wxSYS_COLOUR_WINDOW
)
1043 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1044 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1045 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1046 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1047 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1048 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1049 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1050 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1051 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1052 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1053 SYSCLR(wxSYS_COLOUR_3DFACE
)
1054 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1055 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1056 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1057 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1058 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1059 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1060 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1061 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1062 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1063 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1064 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1065 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1066 SYSCLR(wxSYS_COLOUR_INFOBK
)
1067 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1068 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1069 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1070 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1071 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1072 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1076 return wxNullColour
;
1079 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1081 wxString v
= GetParamValue(param
);
1088 // wxString -> wxColour conversion
1091 // the colour doesn't use #RRGGBB format, check if it is symbolic
1093 clr
= GetSystemColour(v
);
1097 wxLogError(_("XRC resource: Incorrect colour specification '%s' for attribute '%s'."),
1098 v
.c_str(), param
.c_str());
1099 return wxNullColour
;
1107 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1108 const wxArtClient
& defaultArtClient
,
1111 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1112 wxXmlNode
*bmpNode
= GetParamNode(param
);
1115 wxString sid
= bmpNode
->GetAttribute(wxT("stock_id"), wxEmptyString
);
1118 wxString scl
= bmpNode
->GetAttribute(wxT("stock_client"), wxEmptyString
);
1120 scl
= defaultArtClient
;
1122 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1125 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1127 if ( stockArt
.Ok() )
1132 /* ...or load the bitmap from file: */
1133 wxString name
= GetParamValue(param
);
1134 if (name
.empty()) return wxNullBitmap
;
1135 #if wxUSE_FILESYSTEM
1136 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1139 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1141 return wxNullBitmap
;
1143 wxImage
img(*(fsfile
->GetStream()));
1151 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1153 return wxNullBitmap
;
1155 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1156 return wxBitmap(img
);
1159 #if wxUSE_ANIMATIONCTRL
1160 wxAnimation
wxXmlResourceHandler::GetAnimation(const wxString
& param
)
1164 /* load the animation from file: */
1165 wxString name
= GetParamValue(param
);
1166 if (name
.empty()) return wxNullAnimation
;
1167 #if wxUSE_FILESYSTEM
1168 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1171 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1173 return wxNullAnimation
;
1175 ani
.Load(*(fsfile
->GetStream()));
1183 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1185 return wxNullAnimation
;
1190 #endif // wxUSE_ANIMATIONCTRL
1194 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1195 const wxArtClient
& defaultArtClient
,
1199 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1205 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1207 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1209 wxXmlNode
*n
= m_node
->GetChildren();
1213 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1222 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1224 return node
->GetAttribute(wxT("class"), wxEmptyString
) == classname
;
1229 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1231 wxXmlNode
*n
= node
;
1232 if (n
== NULL
) return wxEmptyString
;
1233 n
= n
->GetChildren();
1237 if (n
->GetType() == wxXML_TEXT_NODE
||
1238 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1239 return n
->GetContent();
1242 return wxEmptyString
;
1247 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1250 return GetNodeContent(m_node
);
1252 return GetNodeContent(GetParamNode(param
));
1257 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1258 wxWindow
*windowToUse
)
1260 wxString s
= GetParamValue(param
);
1261 if (s
.empty()) s
= wxT("-1,-1");
1265 is_dlg
= s
[s
.length()-1] == wxT('d');
1266 if (is_dlg
) s
.RemoveLast();
1268 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1269 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1271 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1272 return wxDefaultSize
;
1279 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1281 else if (m_parentAsWindow
)
1283 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1287 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1288 return wxDefaultSize
;
1292 return wxSize(sx
, sy
);
1297 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1299 wxSize sz
= GetSize(param
);
1300 return wxPoint(sz
.x
, sz
.y
);
1305 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1307 wxWindow
*windowToUse
)
1309 wxString s
= GetParamValue(param
);
1310 if (s
.empty()) return defaultv
;
1314 is_dlg
= s
[s
.length()-1] == wxT('d');
1315 if (is_dlg
) s
.RemoveLast();
1319 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1327 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1329 else if (m_parentAsWindow
)
1331 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1335 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1344 // Get system font index using indexname
1345 static wxFont
GetSystemFont(const wxString
& name
)
1349 #define SYSFNT(fnt) \
1350 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1351 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1352 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1353 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1354 SYSFNT(wxSYS_SYSTEM_FONT
)
1355 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1356 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1357 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1358 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1365 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1367 wxXmlNode
*font_node
= GetParamNode(param
);
1368 if (font_node
== NULL
)
1370 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1374 wxXmlNode
*oldnode
= m_node
;
1381 bool hasSize
= HasParam(wxT("size"));
1383 isize
= GetLong(wxT("size"), -1);
1386 int istyle
= wxNORMAL
;
1387 bool hasStyle
= HasParam(wxT("style"));
1390 wxString style
= GetParamValue(wxT("style"));
1391 if (style
== wxT("italic"))
1393 else if (style
== wxT("slant"))
1398 int iweight
= wxNORMAL
;
1399 bool hasWeight
= HasParam(wxT("weight"));
1402 wxString weight
= GetParamValue(wxT("weight"));
1403 if (weight
== wxT("bold"))
1405 else if (weight
== wxT("light"))
1410 bool hasUnderlined
= HasParam(wxT("underlined"));
1411 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1413 // family and facename
1414 int ifamily
= wxDEFAULT
;
1415 bool hasFamily
= HasParam(wxT("family"));
1418 wxString family
= GetParamValue(wxT("family"));
1419 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1420 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1421 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1422 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1423 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1424 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1429 bool hasFacename
= HasParam(wxT("face"));
1432 wxString faces
= GetParamValue(wxT("face"));
1433 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1434 wxStringTokenizer
tk(faces
, wxT(","));
1435 while (tk
.HasMoreTokens())
1437 int index
= facenames
.Index(tk
.GetNextToken(), false);
1438 if (index
!= wxNOT_FOUND
)
1440 facename
= facenames
[index
];
1447 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1448 bool hasEncoding
= HasParam(wxT("encoding"));
1451 wxString encoding
= GetParamValue(wxT("encoding"));
1452 wxFontMapper mapper
;
1453 if (!encoding
.empty())
1454 enc
= mapper
.CharsetToEncoding(encoding
);
1455 if (enc
== wxFONTENCODING_SYSTEM
)
1456 enc
= wxFONTENCODING_DEFAULT
;
1459 // is this font based on a system font?
1460 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1464 if (hasSize
&& isize
!= -1)
1465 font
.SetPointSize(isize
);
1466 else if (HasParam(wxT("relativesize")))
1467 font
.SetPointSize(int(font
.GetPointSize() *
1468 GetFloat(wxT("relativesize"))));
1471 font
.SetStyle(istyle
);
1473 font
.SetWeight(iweight
);
1475 font
.SetUnderlined(underlined
);
1477 font
.SetFamily(ifamily
);
1479 font
.SetFaceName(facename
);
1481 font
.SetDefaultEncoding(enc
);
1483 else // not based on system font
1485 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1486 ifamily
, istyle
, iweight
,
1487 underlined
, facename
, enc
);
1495 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1497 //FIXME : add cursor
1499 if (HasParam(wxT("exstyle")))
1500 // Have to OR it with existing style, since
1501 // some implementations (e.g. wxGTK) use the extra style
1503 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1504 if (HasParam(wxT("bg")))
1505 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1506 if (HasParam(wxT("fg")))
1507 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1508 if (GetBool(wxT("enabled"), 1) == 0)
1510 if (GetBool(wxT("focused"), 0) == 1)
1512 if (GetBool(wxT("hidden"), 0) == 1)
1515 if (HasParam(wxT("tooltip")))
1516 wnd
->SetToolTip(GetText(wxT("tooltip")));
1518 if (HasParam(wxT("font")))
1519 wnd
->SetFont(GetFont());
1520 if (HasParam(wxT("help")))
1521 wnd
->SetHelpText(GetText(wxT("help")));
1525 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1527 wxXmlNode
*n
= m_node
->GetChildren();
1531 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1532 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1534 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1535 this_hnd_only
? this : NULL
);
1542 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1545 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1546 wxXmlNode
*n
= root
->GetChildren();
1550 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1552 CreateResource(n
, parent
, NULL
);
1564 // --------------- XRCID implementation -----------------------------
1566 #define XRCID_TABLE_SIZE 1024
1576 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1578 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1582 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1583 index
%= XRCID_TABLE_SIZE
;
1585 XRCID_record
*oldrec
= NULL
;
1586 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1588 if (wxStrcmp(rec
->key
, str_id
) == 0)
1595 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1596 &XRCID_Records
[index
] : &oldrec
->next
;
1597 *rec_var
= new XRCID_record
;
1598 (*rec_var
)->key
= wxStrdup(str_id
);
1599 (*rec_var
)->next
= NULL
;
1602 if (value_if_not_found
!= wxID_NONE
)
1603 (*rec_var
)->id
= value_if_not_found
;
1606 int asint
= wxStrtol(str_id
, &end
, 10);
1607 if (*str_id
&& *end
== 0)
1609 // if str_id was integer, keep it verbosely:
1610 (*rec_var
)->id
= asint
;
1614 (*rec_var
)->id
= wxWindowBase::NewControlId();
1618 return (*rec_var
)->id
;
1621 static void AddStdXRCID_Records();
1624 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
1626 static bool s_stdIDsAdded
= false;
1628 if ( !s_stdIDsAdded
)
1630 s_stdIDsAdded
= true;
1631 AddStdXRCID_Records();
1634 return XRCID_Lookup(str_id
, value_if_not_found
);
1638 static void CleanXRCID_Record(XRCID_record
*rec
)
1642 CleanXRCID_Record(rec
->next
);
1644 // if we had generated the value of this id automatically, release it
1645 // now that we don't need it any longer
1646 if ( wxWindow::IsAutoGeneratedId(rec
->id
) )
1647 wxWindow::ReleaseControlId(rec
->id
);
1654 static void CleanXRCID_Records()
1656 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1658 CleanXRCID_Record(XRCID_Records
[i
]);
1659 XRCID_Records
[i
] = NULL
;
1663 static void AddStdXRCID_Records()
1665 #define stdID(id) XRCID_Lookup(#id, id)
1669 stdID(wxID_SEPARATOR
);
1682 stdID(wxID_PRINT_SETUP
);
1683 stdID(wxID_PAGE_SETUP
);
1684 stdID(wxID_PREVIEW
);
1686 stdID(wxID_HELP_CONTENTS
);
1687 stdID(wxID_HELP_COMMANDS
);
1688 stdID(wxID_HELP_PROCEDURES
);
1689 stdID(wxID_HELP_CONTEXT
);
1690 stdID(wxID_CLOSE_ALL
);
1691 stdID(wxID_PREFERENCES
);
1697 stdID(wxID_DUPLICATE
);
1698 stdID(wxID_SELECTALL
);
1700 stdID(wxID_REPLACE
);
1701 stdID(wxID_REPLACE_ALL
);
1702 stdID(wxID_PROPERTIES
);
1703 stdID(wxID_VIEW_DETAILS
);
1704 stdID(wxID_VIEW_LARGEICONS
);
1705 stdID(wxID_VIEW_SMALLICONS
);
1706 stdID(wxID_VIEW_LIST
);
1707 stdID(wxID_VIEW_SORTDATE
);
1708 stdID(wxID_VIEW_SORTNAME
);
1709 stdID(wxID_VIEW_SORTSIZE
);
1710 stdID(wxID_VIEW_SORTTYPE
);
1726 stdID(wxID_FORWARD
);
1727 stdID(wxID_BACKWARD
);
1728 stdID(wxID_DEFAULT
);
1732 stdID(wxID_CONTEXT_HELP
);
1733 stdID(wxID_YESTOALL
);
1734 stdID(wxID_NOTOALL
);
1743 stdID(wxID_REFRESH
);
1748 stdID(wxID_JUSTIFY_CENTER
);
1749 stdID(wxID_JUSTIFY_FILL
);
1750 stdID(wxID_JUSTIFY_RIGHT
);
1751 stdID(wxID_JUSTIFY_LEFT
);
1752 stdID(wxID_UNDERLINE
);
1754 stdID(wxID_UNINDENT
);
1755 stdID(wxID_ZOOM_100
);
1756 stdID(wxID_ZOOM_FIT
);
1757 stdID(wxID_ZOOM_IN
);
1758 stdID(wxID_ZOOM_OUT
);
1759 stdID(wxID_UNDELETE
);
1760 stdID(wxID_REVERT_TO_SAVED
);
1761 stdID(wxID_SYSTEM_MENU
);
1762 stdID(wxID_CLOSE_FRAME
);
1763 stdID(wxID_MOVE_FRAME
);
1764 stdID(wxID_RESIZE_FRAME
);
1765 stdID(wxID_MAXIMIZE_FRAME
);
1766 stdID(wxID_ICONIZE_FRAME
);
1767 stdID(wxID_RESTORE_FRAME
);
1776 // --------------- module and globals -----------------------------
1778 class wxXmlResourceModule
: public wxModule
1780 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1782 wxXmlResourceModule() {}
1785 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1790 delete wxXmlResource::Set(NULL
);
1791 if(wxXmlResource::ms_subclassFactories
)
1793 for ( wxXmlSubclassFactories::iterator i
= wxXmlResource::ms_subclassFactories
->begin();
1794 i
!= wxXmlResource::ms_subclassFactories
->end(); ++i
)
1798 wxDELETE(wxXmlResource::ms_subclassFactories
);
1800 CleanXRCID_Records();
1804 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1807 // When wxXml is loaded dynamically after the application is already running
1808 // then the built-in module system won't pick this one up. Add it manually.
1809 void wxXmlInitResourceModule()
1811 wxModule
* module = new wxXmlResourceModule
;
1813 wxModule::RegisterModule(module);