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.
This commit is contained in:
Vadim Zeitlin
2022-02-20 01:16:57 +00:00
parent 38ad817163
commit f077169c86
2 changed files with 25 additions and 12 deletions

View File

@@ -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

View File

@@ -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;
}