From f077169c865d45b4d3aa73e105ebe79def989a1e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 20 Feb 2022 01:16:57 +0000 Subject: [PATCH] Refactor wxBitmapButtonXmlHandler before using wxBitmapBundle This replaces 4 calls to GetBitmap() with a single one. Note that we now use SetBitmapPressed() and SetBitmapCurrent(), which take wxBitmapBundle, instead of deprecated SetBitmapSelected() and SetBitmapFocus(), which do not. We also search for the parameter node only once instead of doing it twice in a row if it was found, so the new code is slightly more efficient. --- include/wx/xrc/xh_bmpbt.h | 9 +++++++++ src/xrc/xh_bmpbt.cpp | 28 ++++++++++++++++------------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/wx/xrc/xh_bmpbt.h b/include/wx/xrc/xh_bmpbt.h index 4e016a1ed5..e215f90613 100644 --- a/include/wx/xrc/xh_bmpbt.h +++ b/include/wx/xrc/xh_bmpbt.h @@ -14,6 +14,8 @@ #if wxUSE_XRC && wxUSE_BMPBUTTON +#include "wx/bmpbuttn.h" + class WXDLLIMPEXP_XRC wxBitmapButtonXmlHandler : public wxXmlResourceHandler { wxDECLARE_DYNAMIC_CLASS(wxBitmapButtonXmlHandler); @@ -22,6 +24,13 @@ public: wxBitmapButtonXmlHandler(); virtual wxObject *DoCreateResource() wxOVERRIDE; virtual bool CanHandle(wxXmlNode *node) wxOVERRIDE; + +private: + typedef void (wxBitmapButton::*BitmapSetter)(const wxBitmapBundle&); + + void SetBitmapIfSpecified(wxBitmapButton* button, + BitmapSetter setter, + const char* paramName); }; #endif // wxUSE_XRC && wxUSE_BMPBUTTON diff --git a/src/xrc/xh_bmpbt.cpp b/src/xrc/xh_bmpbt.cpp index 822fb2b235..210c95dae5 100644 --- a/src/xrc/xh_bmpbt.cpp +++ b/src/xrc/xh_bmpbt.cpp @@ -15,10 +15,6 @@ #include "wx/xrc/xh_bmpbt.h" -#ifndef WX_PRECOMP - #include "wx/bmpbuttn.h" -#endif - wxIMPLEMENT_DYNAMIC_CLASS(wxBitmapButtonXmlHandler, wxXmlResourceHandler); wxBitmapButtonXmlHandler::wxBitmapButtonXmlHandler() @@ -33,6 +29,18 @@ wxBitmapButtonXmlHandler::wxBitmapButtonXmlHandler() AddWindowStyles(); } +// Function calls the given setter with the contents of the node with the given +// name, if present. +void +wxBitmapButtonXmlHandler::SetBitmapIfSpecified(wxBitmapButton* button, + BitmapSetter setter, + const char* paramName) +{ + wxXmlNode* const node = GetParamNode(paramName); + if ( node ) + (button->*setter)(GetBitmap(node)); +} + wxObject *wxBitmapButtonXmlHandler::DoCreateResource() { XRC_MAKE_INSTANCE(button, wxBitmapButton) @@ -58,14 +66,10 @@ wxObject *wxBitmapButtonXmlHandler::DoCreateResource() button->SetDefault(); SetupWindow(button); - if (GetParamNode(wxT("selected"))) - button->SetBitmapSelected(GetBitmap(wxT("selected"))); - if (GetParamNode(wxT("focus"))) - button->SetBitmapFocus(GetBitmap(wxT("focus"))); - if (GetParamNode(wxT("disabled"))) - button->SetBitmapDisabled(GetBitmap(wxT("disabled"))); - if (GetParamNode(wxT("hover"))) - button->SetBitmapHover(GetBitmap(wxT("hover"))); + SetBitmapIfSpecified(button, &wxBitmapButton::SetBitmapPressed, "selected"); + SetBitmapIfSpecified(button, &wxBitmapButton::SetBitmapFocus, "focus"); + SetBitmapIfSpecified(button, &wxBitmapButton::SetBitmapDisabled, "disabled"); + SetBitmapIfSpecified(button, &wxBitmapButton::SetBitmapCurrent, "hover"); return button; }