From fe599e475720b63f87fa8885dfd4a6062a3e26b4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 3 Feb 2024 19:55:35 +0100 Subject: [PATCH] Add wxXmlResourceHandler::GetNodeAttribute() Add another helper function that can be used in XRC handlers without using wxXmlNode directly. --- include/wx/xrc/xmlres.h | 5 +++++ include/wx/xrc/xmlreshandler.h | 10 ++++++++++ interface/wx/xrc/xmlres.h | 11 +++++++++++ src/xrc/xmlres.cpp | 8 ++++++++ 4 files changed, 34 insertions(+) diff --git a/include/wx/xrc/xmlres.h b/include/wx/xrc/xmlres.h index 2ededfc555..f6ccad05a1 100644 --- a/include/wx/xrc/xmlres.h +++ b/include/wx/xrc/xmlres.h @@ -511,6 +511,11 @@ public: // Returns the name of the node, e.g. "object" or "sizeritem". wxString GetNodeName(const wxXmlNode *node) const override; + // Returns the value of the given attribute under the node. + wxString GetNodeAttribute(const wxXmlNode *node, + const wxString& attrName, + const wxString& defaultValue) const override; + // Gets node content from wxXML_ENTITY_NODE // The problem is, content is represented as // wxXML_ENTITY_NODE name="tag", content="" diff --git a/include/wx/xrc/xmlreshandler.h b/include/wx/xrc/xmlreshandler.h index d78f474c36..8943f7921a 100644 --- a/include/wx/xrc/xmlreshandler.h +++ b/include/wx/xrc/xmlreshandler.h @@ -62,6 +62,9 @@ public: virtual bool IsOfClass(wxXmlNode *node, const wxString& classname) const = 0; virtual bool IsObjectNode(const wxXmlNode *node) const = 0; virtual wxString GetNodeName(const wxXmlNode *node) const = 0; + virtual wxString GetNodeAttribute(const wxXmlNode *node, + const wxString& attrName, + const wxString& defaultValue) const = 0; virtual wxString GetNodeContent(const wxXmlNode *node) const = 0; virtual wxXmlNode *GetNodeParent(const wxXmlNode *node) const = 0; virtual wxXmlNode *GetNodeNext(const wxXmlNode *node) const = 0; @@ -247,6 +250,13 @@ protected: return GetImpl()->GetNodeName(node); } + wxString GetNodeAttribute(const wxXmlNode *node, + const wxString& attrName, + const wxString& defaultValue = {}) const + { + return GetImpl()->GetNodeAttribute(node, attrName, defaultValue); + } + wxString GetNodeContent(const wxXmlNode *node) const { return GetImpl()->GetNodeContent(node); diff --git a/interface/wx/xrc/xmlres.h b/interface/wx/xrc/xmlres.h index b1a620f1aa..bc6c5e59be 100644 --- a/interface/wx/xrc/xmlres.h +++ b/interface/wx/xrc/xmlres.h @@ -770,6 +770,17 @@ protected: */ wxString GetNodeName(wxXmlNode* node) const; + /** + Gets the node attribute value. + + If @a node is @NULL or the attribute is not present, returns @a defaultValue. + + @since 3.3.0 + */ + wxString GetNodeAttribute(const wxXmlNode *node, + const wxString& attrName, + const wxString& defaultValue = {}) const; + /** Gets node content from wxXML_ENTITY_NODE. */ diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index e3d4311727..be4aa4a3a1 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -2265,6 +2265,14 @@ wxString wxXmlResourceHandlerImpl::GetNodeName(const wxXmlNode *node) const return node ? node->GetName() : wxString{}; } +wxString +wxXmlResourceHandlerImpl::GetNodeAttribute(const wxXmlNode *node, + const wxString& attrName, + const wxString& defaultValue) const +{ + return node ? node->GetAttribute(attrName, defaultValue) : defaultValue; +} + wxString wxXmlResourceHandlerImpl::GetNodeContent(const wxXmlNode *node) const { return node ? node->GetNodeContent() : wxString{};