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/wfstream.h"
40 #include "wx/filesys.h"
41 #include "wx/filename.h"
42 #include "wx/tokenzr.h"
43 #include "wx/fontenum.h"
44 #include "wx/fontmap.h"
45 #include "wx/artprov.h"
47 #include "wx/xml/xml.h"
49 #include "wx/arrimpl.cpp"
50 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
)
53 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
55 /*static*/ wxXmlResource
*wxXmlResource::Get()
58 ms_instance
= new wxXmlResource
;
62 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
64 wxXmlResource
*old
= ms_instance
;
69 wxXmlResource::wxXmlResource(int flags
, const wxString
& domain
)
76 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
84 wxXmlResource::~wxXmlResource()
89 void wxXmlResource::SetDomain(const wxString
& domain
)
96 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
98 wxString
fnd(filename
);
100 // NB: as Load() and Unload() accept both filenames and URLs (should
101 // probably be changed to filenames only, but embedded resources
102 // currently rely on its ability to handle URLs - FIXME) we need to
103 // determine whether found name is filename and not URL and this is the
104 // fastest/simplest way to do it
105 if (wxFileName::FileExists(fnd
))
107 // Make the name absolute filename, because the app may
108 // change working directory later:
113 fnd
= fn
.GetFullPath();
116 fnd
= wxFileSystem::FileNameToURL(fnd
);
126 bool wxXmlResource::IsArchive(const wxString
& filename
)
128 const wxString fnd
= filename
.Lower();
130 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
133 #endif // wxUSE_FILESYSTEM
135 bool wxXmlResource::Load(const wxString
& filemask
)
138 wxXmlResourceDataRecord
*drec
;
139 bool iswild
= wxIsWild(filemask
);
144 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
145 # define wxXmlFindNext fsys.FindNext()
147 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
148 # define wxXmlFindNext wxFindNextFile()
151 fnd
= wxXmlFindFirst
;
156 fnd
= ConvertFileNameToURL(fnd
);
159 if ( IsArchive(fnd
) )
161 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
163 else // a single resource URL
164 #endif // wxUSE_FILESYSTEM
166 drec
= new wxXmlResourceDataRecord
;
176 # undef wxXmlFindFirst
177 # undef wxXmlFindNext
178 return rt
&& UpdateResources();
181 bool wxXmlResource::Unload(const wxString
& filename
)
183 wxASSERT_MSG( !wxIsWild(filename
),
184 _T("wildcards not supported by wxXmlResource::Unload()") );
186 wxString fnd
= ConvertFileNameToURL(filename
);
188 const bool isArchive
= IsArchive(fnd
);
191 #endif // wxUSE_FILESYSTEM
193 bool unloaded
= false;
194 const size_t count
= m_data
.GetCount();
195 for ( size_t i
= 0; i
< count
; i
++ )
200 if ( m_data
[i
].File
.StartsWith(fnd
) )
202 // don't break from the loop, we can have other matching files
204 else // a single resource URL
205 #endif // wxUSE_FILESYSTEM
207 if ( m_data
[i
].File
== fnd
)
212 // no sense in continuing, there is only one file with this URL
222 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
224 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
226 m_handlers
.Append(handler
);
227 handler
->SetParentResource(this);
230 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
232 m_handlers
.Insert(handler
);
233 handler
->SetParentResource(this);
238 void wxXmlResource::ClearHandlers()
240 WX_CLEAR_LIST(wxList
, m_handlers
);
244 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
246 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
251 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
253 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
259 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
261 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
266 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
268 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
271 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
273 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
278 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
280 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
283 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
285 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
288 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
290 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
293 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
295 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
298 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
300 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
301 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
304 if (bmp
) { rt
= *bmp
; delete bmp
; }
308 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
310 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
311 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
314 if (icon
) { rt
= *icon
; delete icon
; }
319 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
321 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
324 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
326 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
330 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
331 wxWindow
*control
, wxWindow
*parent
)
334 parent
= control
->GetParent();
335 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
338 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
341 return control
->Reparent(container
);
345 static void ProcessPlatformProperty(wxXmlNode
*node
)
350 wxXmlNode
*c
= node
->GetChildren();
354 if (!c
->GetPropVal(wxT("platform"), &s
))
358 wxStringTokenizer
tkn(s
, wxT(" |"));
360 while (tkn
.HasMoreTokens())
362 s
= tkn
.GetNextToken();
364 if (s
== wxT("win")) isok
= true;
366 #if defined(__MAC__) || defined(__APPLE__)
367 if (s
== wxT("mac")) isok
= true;
368 #elif defined(__UNIX__)
369 if (s
== wxT("unix")) isok
= true;
372 if (s
== wxT("os2")) isok
= true;
382 ProcessPlatformProperty(c
);
387 wxXmlNode
*c2
= c
->GetNext();
388 node
->RemoveChild(c
);
397 bool wxXmlResource::UpdateResources()
401 # if wxUSE_FILESYSTEM
402 wxFSFile
*file
= NULL
;
407 wxString
encoding(wxT("UTF-8"));
408 #if !wxUSE_UNICODE && wxUSE_INTL
409 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
411 // In case we are not using wxLocale to translate strings, convert the
412 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
413 // is on, because it could break wxGetTranslation lookup.
414 encoding
= wxLocale::GetSystemEncodingName();
418 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
420 modif
= (m_data
[i
].Doc
== NULL
);
422 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
424 # if wxUSE_FILESYSTEM
425 file
= fsys
.OpenFile(m_data
[i
].File
);
427 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
428 # else // wxUSE_DATETIME
430 # endif // wxUSE_DATETIME
433 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
438 # else // wxUSE_FILESYSTEM
440 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
441 # else // wxUSE_DATETIME
443 # endif // wxUSE_DATETIME
444 # endif // wxUSE_FILESYSTEM
449 wxLogTrace(_T("xrc"),
450 _T("opening file '%s'"), m_data
[i
].File
.c_str());
452 wxInputStream
*stream
= NULL
;
454 # if wxUSE_FILESYSTEM
455 file
= fsys
.OpenFile(m_data
[i
].File
);
457 stream
= file
->GetStream();
459 stream
= new wxFileInputStream(m_data
[i
].File
);
464 delete m_data
[i
].Doc
;
465 m_data
[i
].Doc
= new wxXmlDocument
;
467 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
469 wxLogError(_("Cannot load resources from file '%s'."),
470 m_data
[i
].File
.c_str());
471 wxDELETE(m_data
[i
].Doc
);
474 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
476 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
477 wxDELETE(m_data
[i
].Doc
);
484 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
485 wxT("version"), wxT("0.0.0.0"));
486 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
487 &v1
, &v2
, &v3
, &v4
) == 4)
488 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
493 if (m_version
!= version
)
495 wxLogError(_("Resource files must have same version number!"));
499 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
502 m_data
[i
].Time
= file
->GetModificationTime();
503 #else // wxUSE_FILESYSTEM
504 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
505 #endif // wxUSE_FILESYSTEM
506 #endif // wxUSE_DATETIME
509 # if wxUSE_FILESYSTEM
522 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
523 const wxString
& name
,
524 const wxString
& classname
,
530 // first search for match at the top-level nodes (as this is
531 // where the resource is most commonly looked for):
532 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
534 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
535 (node
->GetName() == wxT("object") ||
536 node
->GetName() == wxT("object_ref")) &&
537 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
539 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
540 if (!classname
|| cls
== classname
)
542 // object_ref may not have 'class' property:
543 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
545 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
548 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
550 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
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")) )
565 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
574 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
575 const wxString
& classname
,
578 UpdateResources(); //ensure everything is up-to-date
581 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
583 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
586 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
587 name
, classname
, recursive
);
591 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
597 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
598 name
.c_str(), classname
.c_str());
602 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
605 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
607 wxXmlProperty
*dprop
;
608 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
611 if ( dprop
->GetName() == prop
->GetName() )
613 dprop
->SetValue(prop
->GetValue());
619 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
622 // Merge child nodes:
623 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
625 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
628 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
630 if ( dnode
->GetName() == node
->GetName() &&
631 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
632 dnode
->GetType() == node
->GetType() )
634 MergeNodes(*dnode
, *node
);
641 static const wxChar
*AT_END
= wxT("end");
642 wxString insert_pos
= node
->GetPropVal(wxT("insert_at"), AT_END
);
643 if ( insert_pos
== AT_END
)
645 dest
.AddChild(new wxXmlNode(*node
));
647 else if ( insert_pos
== wxT("begin") )
649 dest
.InsertChild(new wxXmlNode(*node
), dest
.GetChildren());
654 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().length() )
655 dest
.SetContent(with
.GetContent());
658 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
660 wxXmlResourceHandler
*handlerToUse
)
662 if (node
== NULL
) return NULL
;
664 // handling of referenced resource
665 if ( node
->GetName() == wxT("object_ref") )
667 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
668 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
672 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
677 wxXmlNode
copy(*refNode
);
678 MergeNodes(copy
, *node
);
680 return CreateResFromNode(©
, parent
, instance
);
683 wxXmlResourceHandler
*handler
;
687 if (handlerToUse
->CanHandle(node
))
689 return handlerToUse
->CreateResource(node
, parent
, instance
);
692 else if (node
->GetName() == wxT("object"))
694 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
697 handler
= (wxXmlResourceHandler
*)ND
->GetData();
698 if (handler
->CanHandle(node
))
700 return handler
->CreateResource(node
, parent
, instance
);
706 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
707 node
->GetName().c_str(),
708 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
713 #include "wx/listimpl.cpp"
714 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
715 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
)
717 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
719 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
721 if (!ms_subclassFactories
)
723 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
725 ms_subclassFactories
->Append(factory
);
728 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
731 ~wxXmlSubclassFactoryCXX() {}
733 wxObject
*Create(const wxString
& className
)
735 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
738 return classInfo
->CreateObject();
747 wxXmlResourceHandler::wxXmlResourceHandler()
748 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
749 m_parentAsWindow(NULL
)
754 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
756 wxXmlNode
*myNode
= m_node
;
757 wxString myClass
= m_class
;
758 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
759 wxWindow
*myParentAW
= m_parentAsWindow
;
761 m_instance
= instance
;
762 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
763 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
765 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
766 if (!subclass
.empty())
768 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
771 m_instance
= i
->GetData()->Create(subclass
);
778 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
779 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
780 subclass
.c_str(), name
.c_str());
786 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
788 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
790 wxObject
*returned
= DoCreateResource();
794 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
795 m_instance
= myInstance
;
801 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
803 m_styleNames
.Add(name
);
804 m_styleValues
.Add(value
);
809 void wxXmlResourceHandler::AddWindowStyles()
811 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
813 // the border styles all have the old and new names, recognize both for now
814 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
815 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
816 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
817 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
818 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
819 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
821 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
822 XRC_ADD_STYLE(wxWANTS_CHARS
);
823 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
824 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
825 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
826 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
827 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
828 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
833 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
835 return (GetParamNode(param
) != NULL
);
839 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
841 wxString s
= GetParamValue(param
);
843 if (!s
) return defaults
;
845 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
849 while (tkn
.HasMoreTokens())
851 fl
= tkn
.GetNextToken();
852 index
= m_styleNames
.Index(fl
);
853 if (index
!= wxNOT_FOUND
)
854 style
|= m_styleValues
[index
];
856 wxLogError(_("Unknown style flag ") + fl
);
863 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
865 wxXmlNode
*parNode
= GetParamNode(param
);
866 wxString
str1(GetNodeContent(parNode
));
871 // VS: First version of XRC resources used $ instead of & (which is
872 // illegal in XML), but later I realized that '_' fits this purpose
873 // much better (because &File means "File with F underlined").
874 if (m_resource
->CompareVersion(2,3,0,1) < 0)
879 for (dt
= str1
.c_str(); *dt
; dt
++)
881 // Remap amp_char to &, map double amp_char to amp_char (for things
882 // like "&File..." -- this is illegal in XML, so we use "_File..."):
885 if ( *(++dt
) == amp_char
)
888 str2
<< wxT('&') << *dt
;
890 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
891 else if (*dt
== wxT('\\'))
907 // "\\" wasn't translated to "\" prior to 2.5.3.0:
908 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
913 // else fall-through to default: branch below
916 str2
<< wxT('\\') << *dt
;
922 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
924 if (translate
&& parNode
&&
925 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
927 return wxGetTranslation(str2
, m_resource
->GetDomain());
934 // The string is internally stored as UTF-8, we have to convert
935 // it into system's default encoding so that it can be displayed:
936 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
941 // If wxXRC_USE_LOCALE is not set, then the string is already in
942 // system's default encoding in ANSI build, so we don't have to
943 // do anything special here.
949 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
952 wxString str1
= GetParamValue(param
);
954 if (!str1
.ToLong(&value
))
960 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
963 wxString str1
= GetParamValue(param
);
966 const char *prevlocale
= setlocale(LC_NUMERIC
, "C");
969 if (!str1
.ToDouble(&value
))
973 setlocale(LC_NUMERIC
, prevlocale
);
976 return wx_truncate_cast(float, value
);
980 int wxXmlResourceHandler::GetID()
982 return wxXmlResource::GetXRCID(GetName());
987 wxString
wxXmlResourceHandler::GetName()
989 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
994 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
996 wxString v
= GetParamValue(param
);
998 if (!v
) return defaultv
;
1000 return (v
== wxT("1"));
1004 static wxColour
GetSystemColour(const wxString
& name
)
1008 #define SYSCLR(clr) \
1009 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1010 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1011 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1012 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1013 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1014 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1015 SYSCLR(wxSYS_COLOUR_MENU
)
1016 SYSCLR(wxSYS_COLOUR_WINDOW
)
1017 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1018 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1019 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1020 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1021 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1022 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1023 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1024 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1025 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1026 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1027 SYSCLR(wxSYS_COLOUR_3DFACE
)
1028 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1029 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1030 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1031 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1032 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1033 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1034 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1035 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1036 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1037 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1038 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1039 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1040 SYSCLR(wxSYS_COLOUR_INFOBK
)
1041 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1042 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1043 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1044 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1045 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1046 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1050 return wxNullColour
;
1053 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1055 wxString v
= GetParamValue(param
);
1062 // wxString -> wxColour conversion
1065 // the colour doesn't use #RRGGBB format, check if it is symbolic
1067 clr
= GetSystemColour(v
);
1071 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
1072 v
.c_str(), param
.c_str());
1073 return wxNullColour
;
1081 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1082 const wxArtClient
& defaultArtClient
,
1085 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1086 wxXmlNode
*bmpNode
= GetParamNode(param
);
1089 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
1092 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
1094 scl
= defaultArtClient
;
1096 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1099 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1101 if ( stockArt
.Ok() )
1106 /* ...or load the bitmap from file: */
1107 wxString name
= GetParamValue(param
);
1108 if (name
.empty()) return wxNullBitmap
;
1109 #if wxUSE_FILESYSTEM
1110 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1113 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1115 return wxNullBitmap
;
1117 wxImage
img(*(fsfile
->GetStream()));
1125 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1127 return wxNullBitmap
;
1129 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1130 return wxBitmap(img
);
1133 #if wxUSE_ANIMATIONCTRL
1134 wxAnimation
wxXmlResourceHandler::GetAnimation(const wxString
& param
)
1138 /* load the animation from file: */
1139 wxString name
= GetParamValue(param
);
1140 if (name
.empty()) return wxNullAnimation
;
1141 #if wxUSE_FILESYSTEM
1142 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1145 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1147 return wxNullAnimation
;
1149 ani
.Load(*(fsfile
->GetStream()));
1157 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1159 return wxNullAnimation
;
1164 #endif // wxUSE_ANIMATIONCTRL
1168 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1169 const wxArtClient
& defaultArtClient
,
1173 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1179 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1181 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1183 wxXmlNode
*n
= m_node
->GetChildren();
1187 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1196 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1198 return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
;
1203 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1205 wxXmlNode
*n
= node
;
1206 if (n
== NULL
) return wxEmptyString
;
1207 n
= n
->GetChildren();
1211 if (n
->GetType() == wxXML_TEXT_NODE
||
1212 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1213 return n
->GetContent();
1216 return wxEmptyString
;
1221 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1224 return GetNodeContent(m_node
);
1226 return GetNodeContent(GetParamNode(param
));
1231 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1232 wxWindow
*windowToUse
)
1234 wxString s
= GetParamValue(param
);
1235 if (s
.empty()) s
= wxT("-1,-1");
1239 is_dlg
= s
[s
.length()-1] == wxT('d');
1240 if (is_dlg
) s
.RemoveLast();
1242 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1243 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1245 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1246 return wxDefaultSize
;
1253 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1255 else if (m_parentAsWindow
)
1257 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1261 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1262 return wxDefaultSize
;
1266 return wxSize(sx
, sy
);
1271 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1273 wxSize sz
= GetSize(param
);
1274 return wxPoint(sz
.x
, sz
.y
);
1279 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1281 wxWindow
*windowToUse
)
1283 wxString s
= GetParamValue(param
);
1284 if (s
.empty()) return defaultv
;
1288 is_dlg
= s
[s
.length()-1] == wxT('d');
1289 if (is_dlg
) s
.RemoveLast();
1293 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1301 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1303 else if (m_parentAsWindow
)
1305 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1309 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1318 // Get system font index using indexname
1319 static wxFont
GetSystemFont(const wxString
& name
)
1323 #define SYSFNT(fnt) \
1324 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1325 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1326 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1327 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1328 SYSFNT(wxSYS_SYSTEM_FONT
)
1329 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1330 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1331 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1332 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1339 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1341 wxXmlNode
*font_node
= GetParamNode(param
);
1342 if (font_node
== NULL
)
1344 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1348 wxXmlNode
*oldnode
= m_node
;
1355 bool hasSize
= HasParam(wxT("size"));
1357 isize
= GetLong(wxT("size"), -1);
1360 int istyle
= wxNORMAL
;
1361 bool hasStyle
= HasParam(wxT("style"));
1364 wxString style
= GetParamValue(wxT("style"));
1365 if (style
== wxT("italic"))
1367 else if (style
== wxT("slant"))
1372 int iweight
= wxNORMAL
;
1373 bool hasWeight
= HasParam(wxT("weight"));
1376 wxString weight
= GetParamValue(wxT("weight"));
1377 if (weight
== wxT("bold"))
1379 else if (weight
== wxT("light"))
1384 bool hasUnderlined
= HasParam(wxT("underlined"));
1385 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1387 // family and facename
1388 int ifamily
= wxDEFAULT
;
1389 bool hasFamily
= HasParam(wxT("family"));
1392 wxString family
= GetParamValue(wxT("family"));
1393 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1394 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1395 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1396 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1397 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1398 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1403 bool hasFacename
= HasParam(wxT("face"));
1406 wxString faces
= GetParamValue(wxT("face"));
1407 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1408 wxStringTokenizer
tk(faces
, wxT(","));
1409 while (tk
.HasMoreTokens())
1411 int index
= facenames
.Index(tk
.GetNextToken(), false);
1412 if (index
!= wxNOT_FOUND
)
1414 facename
= facenames
[index
];
1421 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1422 bool hasEncoding
= HasParam(wxT("encoding"));
1425 wxString encoding
= GetParamValue(wxT("encoding"));
1426 wxFontMapper mapper
;
1427 if (!encoding
.empty())
1428 enc
= mapper
.CharsetToEncoding(encoding
);
1429 if (enc
== wxFONTENCODING_SYSTEM
)
1430 enc
= wxFONTENCODING_DEFAULT
;
1433 // is this font based on a system font?
1434 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1438 if (hasSize
&& isize
!= -1)
1439 font
.SetPointSize(isize
);
1440 else if (HasParam(wxT("relativesize")))
1441 font
.SetPointSize(int(font
.GetPointSize() *
1442 GetFloat(wxT("relativesize"))));
1445 font
.SetStyle(istyle
);
1447 font
.SetWeight(iweight
);
1449 font
.SetUnderlined(underlined
);
1451 font
.SetFamily(ifamily
);
1453 font
.SetFaceName(facename
);
1455 font
.SetDefaultEncoding(enc
);
1457 else // not based on system font
1459 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1460 ifamily
, istyle
, iweight
,
1461 underlined
, facename
, enc
);
1469 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1471 //FIXME : add cursor
1473 if (HasParam(wxT("exstyle")))
1474 // Have to OR it with existing style, since
1475 // some implementations (e.g. wxGTK) use the extra style
1477 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1478 if (HasParam(wxT("bg")))
1479 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1480 if (HasParam(wxT("fg")))
1481 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1482 if (GetBool(wxT("enabled"), 1) == 0)
1484 if (GetBool(wxT("focused"), 0) == 1)
1486 if (GetBool(wxT("hidden"), 0) == 1)
1489 if (HasParam(wxT("tooltip")))
1490 wnd
->SetToolTip(GetText(wxT("tooltip")));
1492 if (HasParam(wxT("font")))
1493 wnd
->SetFont(GetFont());
1494 if (HasParam(wxT("help")))
1495 wnd
->SetHelpText(GetText(wxT("help")));
1499 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1501 wxXmlNode
*n
= m_node
->GetChildren();
1505 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1506 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1508 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1509 this_hnd_only
? this : NULL
);
1516 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1519 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1520 wxXmlNode
*n
= root
->GetChildren();
1524 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1526 CreateResource(n
, parent
, NULL
);
1538 // --------------- XRCID implementation -----------------------------
1540 #define XRCID_TABLE_SIZE 1024
1550 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1552 static int XRCID_Lookup(const char *str_id
, int value_if_not_found
= wxID_NONE
)
1556 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
1557 index
%= XRCID_TABLE_SIZE
;
1559 XRCID_record
*oldrec
= NULL
;
1560 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1562 if (wxStrcmp(rec
->key
, str_id
) == 0)
1569 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1570 &XRCID_Records
[index
] : &oldrec
->next
;
1571 *rec_var
= new XRCID_record
;
1572 (*rec_var
)->key
= wxStrdup(str_id
);
1573 (*rec_var
)->next
= NULL
;
1576 if (value_if_not_found
!= wxID_NONE
)
1577 (*rec_var
)->id
= value_if_not_found
;
1580 int asint
= wxStrtol(str_id
, &end
, 10);
1581 if (*str_id
&& *end
== 0)
1583 // if str_id was integer, keep it verbosely:
1584 (*rec_var
)->id
= asint
;
1588 (*rec_var
)->id
= wxNewId();
1592 return (*rec_var
)->id
;
1595 static void AddStdXRCID_Records();
1598 int wxXmlResource::DoGetXRCID(const char *str_id
, int value_if_not_found
)
1600 static bool s_stdIDsAdded
= false;
1602 if ( !s_stdIDsAdded
)
1604 s_stdIDsAdded
= true;
1605 AddStdXRCID_Records();
1608 return XRCID_Lookup(str_id
, value_if_not_found
);
1612 static void CleanXRCID_Record(XRCID_record
*rec
)
1616 CleanXRCID_Record(rec
->next
);
1622 static void CleanXRCID_Records()
1624 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1626 CleanXRCID_Record(XRCID_Records
[i
]);
1627 XRCID_Records
[i
] = NULL
;
1631 static void AddStdXRCID_Records()
1633 #define stdID(id) XRCID_Lookup(#id, id)
1637 stdID(wxID_SEPARATOR
);
1650 stdID(wxID_PRINT_SETUP
);
1651 stdID(wxID_PAGE_SETUP
);
1652 stdID(wxID_PREVIEW
);
1654 stdID(wxID_HELP_CONTENTS
);
1655 stdID(wxID_HELP_COMMANDS
);
1656 stdID(wxID_HELP_PROCEDURES
);
1657 stdID(wxID_HELP_CONTEXT
);
1658 stdID(wxID_CLOSE_ALL
);
1659 stdID(wxID_PREFERENCES
);
1665 stdID(wxID_DUPLICATE
);
1666 stdID(wxID_SELECTALL
);
1668 stdID(wxID_REPLACE
);
1669 stdID(wxID_REPLACE_ALL
);
1670 stdID(wxID_PROPERTIES
);
1671 stdID(wxID_VIEW_DETAILS
);
1672 stdID(wxID_VIEW_LARGEICONS
);
1673 stdID(wxID_VIEW_SMALLICONS
);
1674 stdID(wxID_VIEW_LIST
);
1675 stdID(wxID_VIEW_SORTDATE
);
1676 stdID(wxID_VIEW_SORTNAME
);
1677 stdID(wxID_VIEW_SORTSIZE
);
1678 stdID(wxID_VIEW_SORTTYPE
);
1694 stdID(wxID_FORWARD
);
1695 stdID(wxID_BACKWARD
);
1696 stdID(wxID_DEFAULT
);
1700 stdID(wxID_CONTEXT_HELP
);
1701 stdID(wxID_YESTOALL
);
1702 stdID(wxID_NOTOALL
);
1711 stdID(wxID_REFRESH
);
1716 stdID(wxID_JUSTIFY_CENTER
);
1717 stdID(wxID_JUSTIFY_FILL
);
1718 stdID(wxID_JUSTIFY_RIGHT
);
1719 stdID(wxID_JUSTIFY_LEFT
);
1720 stdID(wxID_UNDERLINE
);
1722 stdID(wxID_UNINDENT
);
1723 stdID(wxID_ZOOM_100
);
1724 stdID(wxID_ZOOM_FIT
);
1725 stdID(wxID_ZOOM_IN
);
1726 stdID(wxID_ZOOM_OUT
);
1727 stdID(wxID_UNDELETE
);
1728 stdID(wxID_REVERT_TO_SAVED
);
1729 stdID(wxID_SYSTEM_MENU
);
1730 stdID(wxID_CLOSE_FRAME
);
1731 stdID(wxID_MOVE_FRAME
);
1732 stdID(wxID_RESIZE_FRAME
);
1733 stdID(wxID_MAXIMIZE_FRAME
);
1734 stdID(wxID_ICONIZE_FRAME
);
1735 stdID(wxID_RESTORE_FRAME
);
1744 // --------------- module and globals -----------------------------
1746 class wxXmlResourceModule
: public wxModule
1748 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1750 wxXmlResourceModule() {}
1753 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1758 delete wxXmlResource::Set(NULL
);
1759 if(wxXmlResource::ms_subclassFactories
)
1760 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1761 wxDELETE(wxXmlResource::ms_subclassFactories
);
1762 CleanXRCID_Records();
1766 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1769 // When wxXml is loaded dynamically after the application is already running
1770 // then the built-in module system won't pick this one up. Add it manually.
1771 void wxXmlInitResourceModule()
1773 wxModule
* module = new wxXmlResourceModule
;
1775 wxModule::RegisterModule(module);