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"
38 #include "wx/wfstream.h"
39 #include "wx/filesys.h"
40 #include "wx/filename.h"
41 #include "wx/tokenzr.h"
42 #include "wx/fontenum.h"
43 #include "wx/fontmap.h"
44 #include "wx/artprov.h"
46 #include "wx/xml/xml.h"
48 #include "wx/arrimpl.cpp"
49 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
)
52 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
54 /*static*/ wxXmlResource
*wxXmlResource::Get()
57 ms_instance
= new wxXmlResource
;
61 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
63 wxXmlResource
*old
= ms_instance
;
68 wxXmlResource::wxXmlResource(int flags
, const wxString
& domain
)
73 if (! domain
.empty() )
77 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
82 if (! domain
.empty() )
87 wxXmlResource::~wxXmlResource()
94 void wxXmlResource::SetDomain(const wxChar
* domain
)
99 if (domain
&& wxStrlen(domain
))
100 m_domain
= wxStrdup(domain
);
105 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
107 wxString
fnd(filename
);
109 // NB: as Load() and Unload() accept both filenames and URLs (should
110 // probably be changed to filenames only, but embedded resources
111 // currently rely on its ability to handle URLs - FIXME) we need to
112 // determine whether found name is filename and not URL and this is the
113 // fastest/simplest way to do it
114 if (wxFileName::FileExists(fnd
))
116 // Make the name absolute filename, because the app may
117 // change working directory later:
122 fnd
= fn
.GetFullPath();
125 fnd
= wxFileSystem::FileNameToURL(fnd
);
135 bool wxXmlResource::IsArchive(const wxString
& filename
)
137 const wxString fnd
= filename
.Lower();
139 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
142 #endif // wxUSE_FILESYSTEM
144 bool wxXmlResource::Load(const wxString
& filemask
)
147 wxXmlResourceDataRecord
*drec
;
148 bool iswild
= wxIsWild(filemask
);
153 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
154 # define wxXmlFindNext fsys.FindNext()
156 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
157 # define wxXmlFindNext wxFindNextFile()
160 fnd
= wxXmlFindFirst
;
165 fnd
= ConvertFileNameToURL(fnd
);
168 if ( IsArchive(fnd
) )
170 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
172 else // a single resource URL
173 #endif // wxUSE_FILESYSTEM
175 drec
= new wxXmlResourceDataRecord
;
185 # undef wxXmlFindFirst
186 # undef wxXmlFindNext
187 return rt
&& UpdateResources();
190 bool wxXmlResource::Unload(const wxString
& filename
)
192 wxASSERT_MSG( !wxIsWild(filename
),
193 _T("wildcards not supported by wxXmlResource::Unload()") );
195 wxString fnd
= ConvertFileNameToURL(filename
);
197 const bool isArchive
= IsArchive(fnd
);
200 #endif // wxUSE_FILESYSTEM
202 bool unloaded
= false;
203 const size_t count
= m_data
.GetCount();
204 for ( size_t i
= 0; i
< count
; i
++ )
209 if ( m_data
[i
].File
.StartsWith(fnd
) )
211 // don't break from the loop, we can have other matching files
213 else // a single resource URL
214 #endif // wxUSE_FILESYSTEM
216 if ( m_data
[i
].File
== fnd
)
221 // no sense in continuing, there is only one file with this URL
231 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
233 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
235 m_handlers
.Append(handler
);
236 handler
->SetParentResource(this);
239 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
241 m_handlers
.Insert(handler
);
242 handler
->SetParentResource(this);
247 void wxXmlResource::ClearHandlers()
249 WX_CLEAR_LIST(wxList
, m_handlers
);
253 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
255 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
260 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
262 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
268 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
270 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
275 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
277 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
280 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
282 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
287 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
289 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
292 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
294 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
297 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
299 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
302 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
304 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
307 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
309 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
310 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
313 if (bmp
) { rt
= *bmp
; delete bmp
; }
317 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
319 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
320 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
323 if (icon
) { rt
= *icon
; delete icon
; }
328 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
330 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
333 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
335 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
339 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
340 wxWindow
*control
, wxWindow
*parent
)
343 parent
= control
->GetParent();
344 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
347 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
350 return control
->Reparent(container
);
354 static void ProcessPlatformProperty(wxXmlNode
*node
)
359 wxXmlNode
*c
= node
->GetChildren();
363 if (!c
->GetPropVal(wxT("platform"), &s
))
367 wxStringTokenizer
tkn(s
, wxT(" |"));
369 while (tkn
.HasMoreTokens())
371 s
= tkn
.GetNextToken();
373 if (s
== wxT("win")) isok
= true;
375 #if defined(__MAC__) || defined(__APPLE__)
376 if (s
== wxT("mac")) isok
= true;
377 #elif defined(__UNIX__)
378 if (s
== wxT("unix")) isok
= true;
381 if (s
== wxT("os2")) isok
= true;
391 ProcessPlatformProperty(c
);
396 wxXmlNode
*c2
= c
->GetNext();
397 node
->RemoveChild(c
);
406 bool wxXmlResource::UpdateResources()
410 # if wxUSE_FILESYSTEM
411 wxFSFile
*file
= NULL
;
416 wxString
encoding(wxT("UTF-8"));
417 #if !wxUSE_UNICODE && wxUSE_INTL
418 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
420 // In case we are not using wxLocale to translate strings, convert the
421 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
422 // is on, because it could break wxGetTranslation lookup.
423 encoding
= wxLocale::GetSystemEncodingName();
427 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
429 modif
= (m_data
[i
].Doc
== NULL
);
431 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
433 # if wxUSE_FILESYSTEM
434 file
= fsys
.OpenFile(m_data
[i
].File
);
436 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
437 # else // wxUSE_DATETIME
439 # endif // wxUSE_DATETIME
442 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
447 # else // wxUSE_FILESYSTEM
449 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
450 # else // wxUSE_DATETIME
452 # endif // wxUSE_DATETIME
453 # endif // wxUSE_FILESYSTEM
458 wxLogTrace(_T("xrc"),
459 _T("opening file '%s'"), m_data
[i
].File
.c_str());
461 wxInputStream
*stream
= NULL
;
463 # if wxUSE_FILESYSTEM
464 file
= fsys
.OpenFile(m_data
[i
].File
);
466 stream
= file
->GetStream();
468 stream
= new wxFileInputStream(m_data
[i
].File
);
473 delete m_data
[i
].Doc
;
474 m_data
[i
].Doc
= new wxXmlDocument
;
476 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
478 wxLogError(_("Cannot load resources from file '%s'."),
479 m_data
[i
].File
.c_str());
480 wxDELETE(m_data
[i
].Doc
);
483 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
485 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
486 wxDELETE(m_data
[i
].Doc
);
493 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
494 wxT("version"), wxT("0.0.0.0"));
495 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
496 &v1
, &v2
, &v3
, &v4
) == 4)
497 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
502 if (m_version
!= version
)
504 wxLogError(_("Resource files must have same version number!"));
508 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
511 m_data
[i
].Time
= file
->GetModificationTime();
512 #else // wxUSE_FILESYSTEM
513 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
514 #endif // wxUSE_FILESYSTEM
515 #endif // wxUSE_DATETIME
518 # if wxUSE_FILESYSTEM
531 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
532 const wxString
& name
,
533 const wxString
& classname
,
539 // first search for match at the top-level nodes (as this is
540 // where the resource is most commonly looked for):
541 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
543 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
544 (node
->GetName() == wxT("object") ||
545 node
->GetName() == wxT("object_ref")) &&
546 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
548 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
549 if (!classname
|| cls
== classname
)
551 // object_ref may not have 'class' property:
552 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
554 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
557 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
559 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
568 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
570 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
571 (node
->GetName() == wxT("object") ||
572 node
->GetName() == wxT("object_ref")) )
574 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
583 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
584 const wxString
& classname
,
587 UpdateResources(); //ensure everything is up-to-date
590 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
592 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
595 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
596 name
, classname
, recursive
);
600 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
606 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
607 name
.c_str(), classname
.c_str());
611 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
614 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
616 wxXmlProperty
*dprop
;
617 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
620 if ( dprop
->GetName() == prop
->GetName() )
622 dprop
->SetValue(prop
->GetValue());
628 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
631 // Merge child nodes:
632 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
634 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
637 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
639 if ( dnode
->GetName() == node
->GetName() &&
640 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
641 dnode
->GetType() == node
->GetType() )
643 MergeNodes(*dnode
, *node
);
650 static const wxChar
*AT_END
= wxT("end");
651 wxString insert_pos
= node
->GetPropVal(wxT("insert_at"), AT_END
);
652 if ( insert_pos
== AT_END
)
654 dest
.AddChild(new wxXmlNode(*node
));
656 else if ( insert_pos
== wxT("begin") )
658 dest
.InsertChild(new wxXmlNode(*node
), dest
.GetChildren());
663 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().length() )
664 dest
.SetContent(with
.GetContent());
667 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
669 wxXmlResourceHandler
*handlerToUse
)
671 if (node
== NULL
) return NULL
;
673 // handling of referenced resource
674 if ( node
->GetName() == wxT("object_ref") )
676 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
677 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
681 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
686 wxXmlNode
copy(*refNode
);
687 MergeNodes(copy
, *node
);
689 return CreateResFromNode(©
, parent
, instance
);
692 wxXmlResourceHandler
*handler
;
696 if (handlerToUse
->CanHandle(node
))
698 return handlerToUse
->CreateResource(node
, parent
, instance
);
701 else if (node
->GetName() == wxT("object"))
703 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
706 handler
= (wxXmlResourceHandler
*)ND
->GetData();
707 if (handler
->CanHandle(node
))
709 return handler
->CreateResource(node
, parent
, instance
);
715 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
716 node
->GetName().c_str(),
717 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
722 #include "wx/listimpl.cpp"
723 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
724 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
)
726 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
728 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
730 if (!ms_subclassFactories
)
732 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
734 ms_subclassFactories
->Append(factory
);
737 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
740 ~wxXmlSubclassFactoryCXX() {}
742 wxObject
*Create(const wxString
& className
)
744 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
747 return classInfo
->CreateObject();
756 wxXmlResourceHandler::wxXmlResourceHandler()
757 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
758 m_parentAsWindow(NULL
)
763 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
765 wxXmlNode
*myNode
= m_node
;
766 wxString myClass
= m_class
;
767 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
768 wxWindow
*myParentAW
= m_parentAsWindow
;
770 m_instance
= instance
;
771 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
772 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
774 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
775 if (!subclass
.empty())
777 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
780 m_instance
= i
->GetData()->Create(subclass
);
787 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
788 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
789 subclass
.c_str(), name
.c_str());
795 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
797 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
799 wxObject
*returned
= DoCreateResource();
803 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
804 m_instance
= myInstance
;
810 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
812 m_styleNames
.Add(name
);
813 m_styleValues
.Add(value
);
818 void wxXmlResourceHandler::AddWindowStyles()
820 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
822 // the border styles all have the old and new names, recognize both for now
823 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
824 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
825 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
826 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
827 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
828 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
830 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
831 XRC_ADD_STYLE(wxWANTS_CHARS
);
832 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
833 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
834 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
835 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
836 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
837 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
842 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
844 return (GetParamNode(param
) != NULL
);
848 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
850 wxString s
= GetParamValue(param
);
852 if (!s
) return defaults
;
854 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
858 while (tkn
.HasMoreTokens())
860 fl
= tkn
.GetNextToken();
861 index
= m_styleNames
.Index(fl
);
862 if (index
!= wxNOT_FOUND
)
863 style
|= m_styleValues
[index
];
865 wxLogError(_("Unknown style flag ") + fl
);
872 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
874 wxXmlNode
*parNode
= GetParamNode(param
);
875 wxString
str1(GetNodeContent(parNode
));
880 // VS: First version of XRC resources used $ instead of & (which is
881 // illegal in XML), but later I realized that '_' fits this purpose
882 // much better (because &File means "File with F underlined").
883 if (m_resource
->CompareVersion(2,3,0,1) < 0)
888 for (dt
= str1
.c_str(); *dt
; dt
++)
890 // Remap amp_char to &, map double amp_char to amp_char (for things
891 // like "&File..." -- this is illegal in XML, so we use "_File..."):
894 if ( *(++dt
) == amp_char
)
897 str2
<< wxT('&') << *dt
;
899 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
900 else if (*dt
== wxT('\\'))
916 // "\\" wasn't translated to "\" prior to 2.5.3.0:
917 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
922 // else fall-through to default: branch below
925 str2
<< wxT('\\') << *dt
;
931 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
933 if (translate
&& parNode
&&
934 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
936 return wxGetTranslation(str2
, m_resource
->GetDomain());
943 // The string is internally stored as UTF-8, we have to convert
944 // it into system's default encoding so that it can be displayed:
945 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
950 // If wxXRC_USE_LOCALE is not set, then the string is already in
951 // system's default encoding in ANSI build, so we don't have to
952 // do anything special here.
958 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
961 wxString str1
= GetParamValue(param
);
963 if (!str1
.ToLong(&value
))
969 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
972 wxString str1
= GetParamValue(param
);
975 const char *prevlocale
= setlocale(LC_NUMERIC
, "C");
978 if (!str1
.ToDouble(&value
))
982 setlocale(LC_NUMERIC
, prevlocale
);
985 return wx_truncate_cast(float, value
);
989 int wxXmlResourceHandler::GetID()
991 return wxXmlResource::GetXRCID(GetName());
996 wxString
wxXmlResourceHandler::GetName()
998 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
1003 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
1005 wxString v
= GetParamValue(param
);
1007 if (!v
) return defaultv
;
1009 return (v
== wxT("1"));
1013 static wxColour
GetSystemColour(const wxString
& name
)
1017 #define SYSCLR(clr) \
1018 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1019 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1020 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1021 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1022 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1023 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1024 SYSCLR(wxSYS_COLOUR_MENU
)
1025 SYSCLR(wxSYS_COLOUR_WINDOW
)
1026 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1027 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1028 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1029 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1030 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1031 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1032 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1033 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1034 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1035 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1036 SYSCLR(wxSYS_COLOUR_3DFACE
)
1037 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1038 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1039 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1040 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1041 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1042 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1043 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1044 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1045 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1046 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1047 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1048 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1049 SYSCLR(wxSYS_COLOUR_INFOBK
)
1050 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1051 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1052 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1053 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1054 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1055 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1059 return wxNullColour
;
1062 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1064 wxString v
= GetParamValue(param
);
1071 // wxString -> wxColour conversion
1074 // the colour doesn't use #RRGGBB format, check if it is symbolic
1076 clr
= GetSystemColour(v
);
1080 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
1081 v
.c_str(), param
.c_str());
1082 return wxNullColour
;
1090 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1091 const wxArtClient
& defaultArtClient
,
1094 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1095 wxXmlNode
*bmpNode
= GetParamNode(param
);
1098 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
1101 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
1103 scl
= defaultArtClient
;
1105 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1108 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1110 if ( stockArt
.Ok() )
1115 /* ...or load the bitmap from file: */
1116 wxString name
= GetParamValue(param
);
1117 if (name
.empty()) return wxNullBitmap
;
1118 #if wxUSE_FILESYSTEM
1119 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1122 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1124 return wxNullBitmap
;
1126 wxImage
img(*(fsfile
->GetStream()));
1134 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1136 return wxNullBitmap
;
1138 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1139 return wxBitmap(img
);
1142 #if wxUSE_ANIMATIONCTRL
1143 wxAnimation
wxXmlResourceHandler::GetAnimation(const wxString
& param
)
1147 /* load the animation from file: */
1148 wxString name
= GetParamValue(param
);
1149 if (name
.empty()) return wxNullAnimation
;
1150 #if wxUSE_FILESYSTEM
1151 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1154 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1156 return wxNullAnimation
;
1158 ani
.Load(*(fsfile
->GetStream()));
1166 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1168 return wxNullAnimation
;
1173 #endif // wxUSE_ANIMATIONCTRL
1177 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1178 const wxArtClient
& defaultArtClient
,
1182 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1188 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1190 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1192 wxXmlNode
*n
= m_node
->GetChildren();
1196 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1205 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1207 return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
;
1212 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1214 wxXmlNode
*n
= node
;
1215 if (n
== NULL
) return wxEmptyString
;
1216 n
= n
->GetChildren();
1220 if (n
->GetType() == wxXML_TEXT_NODE
||
1221 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1222 return n
->GetContent();
1225 return wxEmptyString
;
1230 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1233 return GetNodeContent(m_node
);
1235 return GetNodeContent(GetParamNode(param
));
1240 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1241 wxWindow
*windowToUse
)
1243 wxString s
= GetParamValue(param
);
1244 if (s
.empty()) s
= wxT("-1,-1");
1248 is_dlg
= s
[s
.length()-1] == wxT('d');
1249 if (is_dlg
) s
.RemoveLast();
1251 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1252 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1254 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1255 return wxDefaultSize
;
1262 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1264 else if (m_parentAsWindow
)
1266 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1270 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1271 return wxDefaultSize
;
1275 return wxSize(sx
, sy
);
1280 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1282 wxSize sz
= GetSize(param
);
1283 return wxPoint(sz
.x
, sz
.y
);
1288 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1290 wxWindow
*windowToUse
)
1292 wxString s
= GetParamValue(param
);
1293 if (s
.empty()) return defaultv
;
1297 is_dlg
= s
[s
.length()-1] == wxT('d');
1298 if (is_dlg
) s
.RemoveLast();
1302 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1310 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1312 else if (m_parentAsWindow
)
1314 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1318 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1327 // Get system font index using indexname
1328 static wxFont
GetSystemFont(const wxString
& name
)
1332 #define SYSFNT(fnt) \
1333 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1334 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1335 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1336 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1337 SYSFNT(wxSYS_SYSTEM_FONT
)
1338 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1339 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1340 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1341 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1348 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1350 wxXmlNode
*font_node
= GetParamNode(param
);
1351 if (font_node
== NULL
)
1353 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1357 wxXmlNode
*oldnode
= m_node
;
1364 bool hasSize
= HasParam(wxT("size"));
1366 isize
= GetLong(wxT("size"), -1);
1369 int istyle
= wxNORMAL
;
1370 bool hasStyle
= HasParam(wxT("style"));
1373 wxString style
= GetParamValue(wxT("style"));
1374 if (style
== wxT("italic"))
1376 else if (style
== wxT("slant"))
1381 int iweight
= wxNORMAL
;
1382 bool hasWeight
= HasParam(wxT("weight"));
1385 wxString weight
= GetParamValue(wxT("weight"));
1386 if (weight
== wxT("bold"))
1388 else if (weight
== wxT("light"))
1393 bool hasUnderlined
= HasParam(wxT("underlined"));
1394 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1396 // family and facename
1397 int ifamily
= wxDEFAULT
;
1398 bool hasFamily
= HasParam(wxT("family"));
1401 wxString family
= GetParamValue(wxT("family"));
1402 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1403 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1404 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1405 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1406 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1407 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1412 bool hasFacename
= HasParam(wxT("face"));
1415 wxString faces
= GetParamValue(wxT("face"));
1416 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1417 wxStringTokenizer
tk(faces
, wxT(","));
1418 while (tk
.HasMoreTokens())
1420 int index
= facenames
.Index(tk
.GetNextToken(), false);
1421 if (index
!= wxNOT_FOUND
)
1423 facename
= facenames
[index
];
1430 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1431 bool hasEncoding
= HasParam(wxT("encoding"));
1434 wxString encoding
= GetParamValue(wxT("encoding"));
1435 wxFontMapper mapper
;
1436 if (!encoding
.empty())
1437 enc
= mapper
.CharsetToEncoding(encoding
);
1438 if (enc
== wxFONTENCODING_SYSTEM
)
1439 enc
= wxFONTENCODING_DEFAULT
;
1442 // is this font based on a system font?
1443 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1447 if (hasSize
&& isize
!= -1)
1448 font
.SetPointSize(isize
);
1449 else if (HasParam(wxT("relativesize")))
1450 font
.SetPointSize(int(font
.GetPointSize() *
1451 GetFloat(wxT("relativesize"))));
1454 font
.SetStyle(istyle
);
1456 font
.SetWeight(iweight
);
1458 font
.SetUnderlined(underlined
);
1460 font
.SetFamily(ifamily
);
1462 font
.SetFaceName(facename
);
1464 font
.SetDefaultEncoding(enc
);
1466 else // not based on system font
1468 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1469 ifamily
, istyle
, iweight
,
1470 underlined
, facename
, enc
);
1478 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1480 //FIXME : add cursor
1482 if (HasParam(wxT("exstyle")))
1483 // Have to OR it with existing style, since
1484 // some implementations (e.g. wxGTK) use the extra style
1486 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1487 if (HasParam(wxT("bg")))
1488 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1489 if (HasParam(wxT("fg")))
1490 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1491 if (GetBool(wxT("enabled"), 1) == 0)
1493 if (GetBool(wxT("focused"), 0) == 1)
1495 if (GetBool(wxT("hidden"), 0) == 1)
1498 if (HasParam(wxT("tooltip")))
1499 wnd
->SetToolTip(GetText(wxT("tooltip")));
1501 if (HasParam(wxT("font")))
1502 wnd
->SetFont(GetFont());
1503 if (HasParam(wxT("help")))
1504 wnd
->SetHelpText(GetText(wxT("help")));
1508 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1510 wxXmlNode
*n
= m_node
->GetChildren();
1514 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1515 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1517 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1518 this_hnd_only
? this : NULL
);
1525 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1528 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1529 wxXmlNode
*n
= root
->GetChildren();
1533 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1535 CreateResource(n
, parent
, NULL
);
1547 // --------------- XRCID implementation -----------------------------
1549 #define XRCID_TABLE_SIZE 1024
1559 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1561 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= wxID_NONE
)
1565 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1566 index
%= XRCID_TABLE_SIZE
;
1568 XRCID_record
*oldrec
= NULL
;
1569 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1571 if (wxStrcmp(rec
->key
, str_id
) == 0)
1578 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1579 &XRCID_Records
[index
] : &oldrec
->next
;
1580 *rec_var
= new XRCID_record
;
1581 (*rec_var
)->key
= wxStrdup(str_id
);
1582 (*rec_var
)->next
= NULL
;
1585 if (value_if_not_found
!= wxID_NONE
)
1586 (*rec_var
)->id
= value_if_not_found
;
1589 int asint
= wxStrtol(str_id
, &end
, 10);
1590 if (*str_id
&& *end
== 0)
1592 // if str_id was integer, keep it verbosely:
1593 (*rec_var
)->id
= asint
;
1597 (*rec_var
)->id
= wxWindow::NewControlId();
1601 return (*rec_var
)->id
;
1604 static void AddStdXRCID_Records();
1607 int wxXmlResource::GetXRCID(const wxChar
*str_id
, int value_if_not_found
)
1609 static bool s_stdIDsAdded
= false;
1611 if ( !s_stdIDsAdded
)
1613 s_stdIDsAdded
= true;
1614 AddStdXRCID_Records();
1617 return XRCID_Lookup(str_id
, value_if_not_found
);
1621 static void CleanXRCID_Record(XRCID_record
*rec
)
1625 CleanXRCID_Record(rec
->next
);
1631 static void CleanXRCID_Records()
1633 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1635 CleanXRCID_Record(XRCID_Records
[i
]);
1636 XRCID_Records
[i
] = NULL
;
1640 static void AddStdXRCID_Records()
1642 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1646 stdID(wxID_SEPARATOR
);
1659 stdID(wxID_PRINT_SETUP
);
1660 stdID(wxID_PAGE_SETUP
);
1661 stdID(wxID_PREVIEW
);
1663 stdID(wxID_HELP_CONTENTS
);
1664 stdID(wxID_HELP_COMMANDS
);
1665 stdID(wxID_HELP_PROCEDURES
);
1666 stdID(wxID_HELP_CONTEXT
);
1667 stdID(wxID_CLOSE_ALL
);
1668 stdID(wxID_PREFERENCES
);
1674 stdID(wxID_DUPLICATE
);
1675 stdID(wxID_SELECTALL
);
1677 stdID(wxID_REPLACE
);
1678 stdID(wxID_REPLACE_ALL
);
1679 stdID(wxID_PROPERTIES
);
1680 stdID(wxID_VIEW_DETAILS
);
1681 stdID(wxID_VIEW_LARGEICONS
);
1682 stdID(wxID_VIEW_SMALLICONS
);
1683 stdID(wxID_VIEW_LIST
);
1684 stdID(wxID_VIEW_SORTDATE
);
1685 stdID(wxID_VIEW_SORTNAME
);
1686 stdID(wxID_VIEW_SORTSIZE
);
1687 stdID(wxID_VIEW_SORTTYPE
);
1703 stdID(wxID_FORWARD
);
1704 stdID(wxID_BACKWARD
);
1705 stdID(wxID_DEFAULT
);
1709 stdID(wxID_CONTEXT_HELP
);
1710 stdID(wxID_YESTOALL
);
1711 stdID(wxID_NOTOALL
);
1720 stdID(wxID_REFRESH
);
1725 stdID(wxID_JUSTIFY_CENTER
);
1726 stdID(wxID_JUSTIFY_FILL
);
1727 stdID(wxID_JUSTIFY_RIGHT
);
1728 stdID(wxID_JUSTIFY_LEFT
);
1729 stdID(wxID_UNDERLINE
);
1731 stdID(wxID_UNINDENT
);
1732 stdID(wxID_ZOOM_100
);
1733 stdID(wxID_ZOOM_FIT
);
1734 stdID(wxID_ZOOM_IN
);
1735 stdID(wxID_ZOOM_OUT
);
1736 stdID(wxID_UNDELETE
);
1737 stdID(wxID_REVERT_TO_SAVED
);
1738 stdID(wxID_SYSTEM_MENU
);
1739 stdID(wxID_CLOSE_FRAME
);
1740 stdID(wxID_MOVE_FRAME
);
1741 stdID(wxID_RESIZE_FRAME
);
1742 stdID(wxID_MAXIMIZE_FRAME
);
1743 stdID(wxID_ICONIZE_FRAME
);
1744 stdID(wxID_RESTORE_FRAME
);
1753 // --------------- module and globals -----------------------------
1755 class wxXmlResourceModule
: public wxModule
1757 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1759 wxXmlResourceModule() {}
1762 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1767 delete wxXmlResource::Set(NULL
);
1768 if(wxXmlResource::ms_subclassFactories
)
1769 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1770 wxDELETE(wxXmlResource::ms_subclassFactories
);
1771 CleanXRCID_Records();
1775 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1778 // When wxXml is loaded dynamically after the application is already running
1779 // then the built-in module system won't pick this one up. Add it manually.
1780 void wxXmlInitResourceModule()
1782 wxModule
* module = new wxXmlResourceModule
;
1784 wxModule::RegisterModule(module);