From daa85ee3af64b629eb52b8b9d21fa4b837b5cb78 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Sat, 29 Dec 2001 16:13:43 +0000 Subject: [PATCH] implemented subclassing in XRC git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13225 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/include/wx/xrc/xmlres.h | 37 ++++++++++++++++++------- contrib/src/xrc/xmlres.cpp | 48 ++++++++++++++++++++++----------- include/wx/xrc/xmlres.h | 37 ++++++++++++++++++------- src/xrc/xmlres.cpp | 48 ++++++++++++++++++++++----------- 4 files changed, 122 insertions(+), 48 deletions(-) diff --git a/contrib/include/wx/xrc/xmlres.h b/contrib/include/wx/xrc/xmlres.h index 7843e4f161..62d3093b4c 100644 --- a/contrib/include/wx/xrc/xmlres.h +++ b/contrib/include/wx/xrc/xmlres.h @@ -80,19 +80,26 @@ WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords); WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords); #endif +enum wxXmlResourceFlags +{ + wxXRC_USE_LOCALE = 1, + wxXRC_NO_SUBCLASSING = 2 +}; // This class holds XML resources from one or more .xml files // (or derived forms, either binary or zipped -- see manual for // details). - class WXXMLDLLEXPORT wxXmlResource : public wxObject { public: - // Ctor. If use_locale is TRUE, translatable strings are - // translated via _(). You can disable it by passing use_locale=FALSE - // (for example if you provide resource file for each locale) - wxXmlResource(bool use_locale = TRUE); - wxXmlResource(const wxString& filemask, bool use_locale = TRUE); + // Ctor. + // Flags: wxXRC_USE_LOCALE + // translatable strings will be translated via _() + // wxXRC_NO_SUBCLASSING + // subclass property of object nodes will be ignored + // (useful for previews in XRC editors) + wxXmlResource(int flags = wxXRC_USE_LOCALE); + wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); ~wxXmlResource(); // Loads resources from XML files that match given filemask. @@ -176,12 +183,12 @@ protected: // Creates resource from info in given node: wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL); - bool GetUseLocale() { return m_useLocale; } + int GetFlags() { return m_flags; } private: long m_version; - bool m_useLocale; + int m_flags; wxList m_handlers; wxXmlResourceDataRecords m_data; #if wxUSE_FILESYSTEM @@ -348,9 +355,21 @@ protected: #endif }; -#define ADD_STYLE(style) AddStyle(wxT(#style), style) + +// Programmer-friendly macros for writing XRC handlers: + +#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style) +#define ADD_STYLE XRC_ADD_STYLE /* deprecated, don't use!! */ + +#define XRC_MAKE_INSTANCE(variable, classname) \ + classname *variable = NULL; \ + if (m_instance) \ + variable = wxStaticCast(m_instance, classname); \ + if (!variable) \ + variable = new classname; +// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!! void wxXmlInitResourceModule(); #endif // _WX_XMLRES_H_ diff --git a/contrib/src/xrc/xmlres.cpp b/contrib/src/xrc/xmlres.cpp index 93a0b7f561..500bf81b69 100644 --- a/contrib/src/xrc/xmlres.cpp +++ b/contrib/src/xrc/xmlres.cpp @@ -40,16 +40,16 @@ WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords); -wxXmlResource::wxXmlResource(bool use_locale) +wxXmlResource::wxXmlResource(int flags) { m_handlers.DeleteContents(TRUE); - m_useLocale = use_locale; + m_flags = flags; m_version = -1; } -wxXmlResource::wxXmlResource(const wxString& filemask, bool use_locale) +wxXmlResource::wxXmlResource(const wxString& filemask, int flags) { - m_useLocale = use_locale; + m_flags = flags; m_version = -1; m_handlers.DeleteContents(TRUE); Load(filemask); @@ -421,10 +421,28 @@ wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent wxObject *myParent = m_parent, *myInstance = m_instance; wxWindow *myParentAW = m_parentAsWindow, *myInstanceAW = m_instanceAsWindow; + m_instance = instance; + if (!m_instance && node->HasProp(wxT("subclass")) && + !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING)) + { + wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString); + wxClassInfo* classInfo = wxClassInfo::FindClass(subclass); + + if (classInfo) + m_instance = classInfo->CreateObject(); + + if (!m_instance) + { + wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"), + subclass.c_str(), node->GetPropVal(wxT("name"), wxEmptyString).c_str()); + } + + m_instance = classInfo->CreateObject(); + } + m_node = node; m_class = node->GetPropVal(wxT("class"), wxEmptyString); m_parent = parent; - m_instance = instance; m_parentAsWindow = wxDynamicCast(m_parent, wxWindow); m_instanceAsWindow = wxDynamicCast(m_instance, wxWindow); @@ -449,15 +467,15 @@ void wxXmlResourceHandler::AddStyle(const wxString& name, int value) void wxXmlResourceHandler::AddWindowStyles() { - ADD_STYLE(wxSIMPLE_BORDER); - ADD_STYLE(wxSUNKEN_BORDER); - ADD_STYLE(wxDOUBLE_BORDER); - ADD_STYLE(wxRAISED_BORDER); - ADD_STYLE(wxSTATIC_BORDER); - ADD_STYLE(wxNO_BORDER); - ADD_STYLE(wxTRANSPARENT_WINDOW); - ADD_STYLE(wxWANTS_CHARS); - ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE); + XRC_ADD_STYLE(wxSIMPLE_BORDER); + XRC_ADD_STYLE(wxSUNKEN_BORDER); + XRC_ADD_STYLE(wxDOUBLE_BORDER); + XRC_ADD_STYLE(wxRAISED_BORDER); + XRC_ADD_STYLE(wxSTATIC_BORDER); + XRC_ADD_STYLE(wxNO_BORDER); + XRC_ADD_STYLE(wxTRANSPARENT_WINDOW); + XRC_ADD_STYLE(wxWANTS_CHARS); + XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE); } @@ -499,7 +517,7 @@ wxString wxXmlResourceHandler::GetText(const wxString& param) const wxChar *dt; wxChar amp_char; - if (m_resource->GetUseLocale()) + if (m_resource->GetFlags() & wxXRC_USE_LOCALE) str1 = wxGetTranslation(GetParamValue(param)); else str1 = GetParamValue(param); diff --git a/include/wx/xrc/xmlres.h b/include/wx/xrc/xmlres.h index 7843e4f161..62d3093b4c 100644 --- a/include/wx/xrc/xmlres.h +++ b/include/wx/xrc/xmlres.h @@ -80,19 +80,26 @@ WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords); WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords); #endif +enum wxXmlResourceFlags +{ + wxXRC_USE_LOCALE = 1, + wxXRC_NO_SUBCLASSING = 2 +}; // This class holds XML resources from one or more .xml files // (or derived forms, either binary or zipped -- see manual for // details). - class WXXMLDLLEXPORT wxXmlResource : public wxObject { public: - // Ctor. If use_locale is TRUE, translatable strings are - // translated via _(). You can disable it by passing use_locale=FALSE - // (for example if you provide resource file for each locale) - wxXmlResource(bool use_locale = TRUE); - wxXmlResource(const wxString& filemask, bool use_locale = TRUE); + // Ctor. + // Flags: wxXRC_USE_LOCALE + // translatable strings will be translated via _() + // wxXRC_NO_SUBCLASSING + // subclass property of object nodes will be ignored + // (useful for previews in XRC editors) + wxXmlResource(int flags = wxXRC_USE_LOCALE); + wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); ~wxXmlResource(); // Loads resources from XML files that match given filemask. @@ -176,12 +183,12 @@ protected: // Creates resource from info in given node: wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL); - bool GetUseLocale() { return m_useLocale; } + int GetFlags() { return m_flags; } private: long m_version; - bool m_useLocale; + int m_flags; wxList m_handlers; wxXmlResourceDataRecords m_data; #if wxUSE_FILESYSTEM @@ -348,9 +355,21 @@ protected: #endif }; -#define ADD_STYLE(style) AddStyle(wxT(#style), style) + +// Programmer-friendly macros for writing XRC handlers: + +#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style) +#define ADD_STYLE XRC_ADD_STYLE /* deprecated, don't use!! */ + +#define XRC_MAKE_INSTANCE(variable, classname) \ + classname *variable = NULL; \ + if (m_instance) \ + variable = wxStaticCast(m_instance, classname); \ + if (!variable) \ + variable = new classname; +// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!! void wxXmlInitResourceModule(); #endif // _WX_XMLRES_H_ diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index 93a0b7f561..500bf81b69 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -40,16 +40,16 @@ WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords); -wxXmlResource::wxXmlResource(bool use_locale) +wxXmlResource::wxXmlResource(int flags) { m_handlers.DeleteContents(TRUE); - m_useLocale = use_locale; + m_flags = flags; m_version = -1; } -wxXmlResource::wxXmlResource(const wxString& filemask, bool use_locale) +wxXmlResource::wxXmlResource(const wxString& filemask, int flags) { - m_useLocale = use_locale; + m_flags = flags; m_version = -1; m_handlers.DeleteContents(TRUE); Load(filemask); @@ -421,10 +421,28 @@ wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent wxObject *myParent = m_parent, *myInstance = m_instance; wxWindow *myParentAW = m_parentAsWindow, *myInstanceAW = m_instanceAsWindow; + m_instance = instance; + if (!m_instance && node->HasProp(wxT("subclass")) && + !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING)) + { + wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString); + wxClassInfo* classInfo = wxClassInfo::FindClass(subclass); + + if (classInfo) + m_instance = classInfo->CreateObject(); + + if (!m_instance) + { + wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"), + subclass.c_str(), node->GetPropVal(wxT("name"), wxEmptyString).c_str()); + } + + m_instance = classInfo->CreateObject(); + } + m_node = node; m_class = node->GetPropVal(wxT("class"), wxEmptyString); m_parent = parent; - m_instance = instance; m_parentAsWindow = wxDynamicCast(m_parent, wxWindow); m_instanceAsWindow = wxDynamicCast(m_instance, wxWindow); @@ -449,15 +467,15 @@ void wxXmlResourceHandler::AddStyle(const wxString& name, int value) void wxXmlResourceHandler::AddWindowStyles() { - ADD_STYLE(wxSIMPLE_BORDER); - ADD_STYLE(wxSUNKEN_BORDER); - ADD_STYLE(wxDOUBLE_BORDER); - ADD_STYLE(wxRAISED_BORDER); - ADD_STYLE(wxSTATIC_BORDER); - ADD_STYLE(wxNO_BORDER); - ADD_STYLE(wxTRANSPARENT_WINDOW); - ADD_STYLE(wxWANTS_CHARS); - ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE); + XRC_ADD_STYLE(wxSIMPLE_BORDER); + XRC_ADD_STYLE(wxSUNKEN_BORDER); + XRC_ADD_STYLE(wxDOUBLE_BORDER); + XRC_ADD_STYLE(wxRAISED_BORDER); + XRC_ADD_STYLE(wxSTATIC_BORDER); + XRC_ADD_STYLE(wxNO_BORDER); + XRC_ADD_STYLE(wxTRANSPARENT_WINDOW); + XRC_ADD_STYLE(wxWANTS_CHARS); + XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE); } @@ -499,7 +517,7 @@ wxString wxXmlResourceHandler::GetText(const wxString& param) const wxChar *dt; wxChar amp_char; - if (m_resource->GetUseLocale()) + if (m_resource->GetFlags() & wxXRC_USE_LOCALE) str1 = wxGetTranslation(GetParamValue(param)); else str1 = GetParamValue(param); -- 2.45.2