Use GetAnimation() in wxAnimationCtrlXmlHandler again

This function basically only exists in order to be used in this handler,
so extend it to allow doing it by adding wxAnimationCtrlBase argument to
it, allowing it to create wxAnimation object compatible with the given
control.

This reduces code duplication and, incidentally, makes GetAnimation()
more useful for any user-defined XRC handlers.
This commit is contained in:
Vadim Zeitlin
2020-06-09 18:05:00 +02:00
parent 2c4b0b4271
commit 8ff6fa6f54
4 changed files with 25 additions and 39 deletions

View File

@@ -591,8 +591,10 @@ public:
wxImageList *GetImageList(const wxString& param = wxT("imagelist")) wxOVERRIDE;
#if wxUSE_ANIMATIONCTRL
// Gets an animation.
wxAnimation* GetAnimation(const wxString& param = wxT("animation")) wxOVERRIDE;
// Gets an animation creating it using the provided control (so that it
// will be compatible with it) if any.
wxAnimation* GetAnimation(const wxString& param = wxT("animation"),
wxAnimationCtrlBase* ctrl = NULL) wxOVERRIDE;
#endif
// Gets a font.

View File

@@ -22,6 +22,7 @@
#include "wx/window.h"
class WXDLLIMPEXP_FWD_CORE wxAnimation;
class WXDLLIMPEXP_FWD_CORE wxAnimationCtrlBase;
class WXDLLIMPEXP_FWD_XML wxXmlNode;
class WXDLLIMPEXP_FWD_XML wxXmlResource;
@@ -100,7 +101,8 @@ public:
virtual wxImageList *GetImageList(const wxString& param = wxT("imagelist")) = 0;
#if wxUSE_ANIMATIONCTRL
virtual wxAnimation* GetAnimation(const wxString& param = wxT("animation")) = 0;
virtual wxAnimation* GetAnimation(const wxString& param = wxT("animation"),
wxAnimationCtrlBase* ctrl = NULL) = 0;
#endif
virtual wxFont GetFont(const wxString& param = wxT("font"), wxWindow* parent = NULL) = 0;
@@ -353,9 +355,10 @@ protected:
}
#if wxUSE_ANIMATIONCTRL
wxAnimation* GetAnimation(const wxString& param = wxT("animation"))
wxAnimation* GetAnimation(const wxString& param = wxT("animation"),
wxAnimationCtrlBase* ctrl = NULL)
{
return GetImpl()->GetAnimation(param);
return GetImpl()->GetAnimation(param, ctrl);
}
#endif

View File

@@ -542,8 +542,15 @@ protected:
/**
Creates an animation (see wxAnimation) from the filename specified in @a param.
It is recommended to provide @a ctrl argument to this function (which
is only available in wxWidgets 3.1.4 or later) to make sure that the
created animation is compatible with the specified control, otherwise a
wxAnimation object compatible with the default wxAnimationCtrl
implementation is created.
*/
wxAnimation* GetAnimation(const wxString& param = "animation");
wxAnimation* GetAnimation(const wxString& param = "animation",
wxAnimationCtrlBase* ctrl = NULL);
/**
Gets a bitmap.

View File

@@ -60,37 +60,9 @@ wxObject *wxAnimationCtrlXmlHandler::DoCreateResource()
if ( GetBool("hidden", 0) == 1 )
ctrl->Hide();
const wxString aniParam = "animation";
wxString path = GetFilePath(GetParamNode(aniParam));
// load the animation from file
if ( !path.empty() )
{
wxAnimation ani = ctrl->CreateAnimation();
#if wxUSE_FILESYSTEM
wxScopedPtr<wxFSFile> fsfile(
GetCurFileSystem().OpenFile(path, wxFS_READ | wxFS_SEEKABLE)
);
if (fsfile)
{
ani.Load(*fsfile->GetStream());
}
#else
ani.LoadFile(name);
#endif
if ( ani.IsOk() )
{
ctrl->SetAnimation(ani);
}
else
{
ReportParamError
(
aniParam,
wxString::Format("cannot create animation from \"%s\"", path)
);
}
}
wxScopedPtr<wxAnimation> animation(GetAnimation("animation", ctrl));
if ( animation )
ctrl->SetAnimation(*animation);
// if no inactive-bitmap has been provided, GetBitmap() will return wxNullBitmap
// which just tells wxAnimationCtrl to use the default for inactive status
@@ -107,14 +79,16 @@ bool wxAnimationCtrlXmlHandler::CanHandle(wxXmlNode *node)
IsOfClass(node, wxT("wxGenericAnimationCtrl"));
}
wxAnimation* wxXmlResourceHandlerImpl::GetAnimation(const wxString& param)
wxAnimation* wxXmlResourceHandlerImpl::GetAnimation(const wxString& param,
wxAnimationCtrlBase* ctrl)
{
wxString name = GetFilePath(GetParamNode(param));
if ( name.empty() )
return NULL;
// load the animation from file
wxScopedPtr<wxAnimation> ani(new wxAnimation);
wxScopedPtr<wxAnimation> ani(ctrl ? new wxAnimation(ctrl->CreateAnimation())
: new wxAnimation);
#if wxUSE_FILESYSTEM
wxFSFile * const
fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);