Update GTK animation classes for the new pattern

This commit is contained in:
Robin Dunn
2020-04-01 14:16:15 -07:00
parent f641601ea9
commit 6b6a930e9c
3 changed files with 85 additions and 64 deletions

View File

@@ -23,16 +23,21 @@ typedef struct _GdkPixbufAnimationIter GdkPixbufAnimationIter;
// refcounted so that assignment is very fast // refcounted so that assignment is very fast
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class WXDLLIMPEXP_ADV wxAnimation : public wxAnimationBase class WXDLLIMPEXP_ADV wxAnimationGTKImpl : public wxAnimationImpl
{ {
public: public:
wxAnimation(const wxString &name, wxAnimationType type = wxANIMATION_TYPE_ANY) wxAnimationGTKImpl()
: m_pixbuf(NULL) { LoadFile(name, type); } : m_pixbuf(NULL) {}
wxAnimation(GdkPixbufAnimation *p = NULL); // wxAnimation(const wxString &name, wxAnimationType type = wxANIMATION_TYPE_ANY)
wxAnimation(const wxAnimation&); // : m_pixbuf(NULL) { LoadFile(name, type); }
~wxAnimation() { UnRef(); } // wxAnimation(GdkPixbufAnimation *p = NULL);
// wxAnimation(const wxAnimation&);
~wxAnimationGTKImpl() { UnRef(); }
wxAnimation& operator= (const wxAnimation&); // wxAnimation& operator= (const wxAnimation&);
virtual wxAnimationImplType GetImplType() wxOVERRIDE
{ return wxANIMATION_IMPL_TYPE_NATIVE; }
virtual bool IsOk() const wxOVERRIDE virtual bool IsOk() const wxOVERRIDE
{ return m_pixbuf != NULL; } { return m_pixbuf != NULL; }
@@ -52,16 +57,6 @@ public:
{ return 0; } { return 0; }
virtual wxImage GetFrame(unsigned int WXUNUSED(frame)) const wxOVERRIDE virtual wxImage GetFrame(unsigned int WXUNUSED(frame)) const wxOVERRIDE
{ return wxNullImage; } { return wxNullImage; }
virtual wxPoint GetFramePosition(unsigned int WXUNUSED(frame)) const wxOVERRIDE
{ return wxDefaultPosition; }
virtual wxSize GetFrameSize(unsigned int WXUNUSED(frame)) const wxOVERRIDE
{ return wxDefaultSize; }
virtual wxAnimationDisposal GetDisposalMethod(unsigned int WXUNUSED(frame)) const wxOVERRIDE
{ return wxANIM_UNSPECIFIED; }
virtual wxColour GetTransparentColour(unsigned int WXUNUSED(frame)) const wxOVERRIDE
{ return wxNullColour; }
virtual wxColour GetBackgroundColour() const wxOVERRIDE
{ return wxNullColour; }
// Implementation // Implementation
@@ -77,8 +72,8 @@ protected:
private: private:
void UnRef(); void UnRef();
typedef wxAnimationBase base_type; typedef wxAnimationImpl base_type;
wxDECLARE_DYNAMIC_CLASS(wxAnimation); wxDECLARE_DYNAMIC_CLASS(wxAnimationGTKImpl);
}; };
@@ -125,8 +120,6 @@ public: // public API
virtual bool Load(wxInputStream& stream, wxAnimationType type = wxANIMATION_TYPE_ANY) wxOVERRIDE; virtual bool Load(wxInputStream& stream, wxAnimationType type = wxANIMATION_TYPE_ANY) wxOVERRIDE;
void SetAnimation(const wxAnimation &anim); void SetAnimation(const wxAnimation &anim);
wxAnimation GetAnimation() const
{ return wxAnimation(m_anim) ; }
virtual bool Play() wxOVERRIDE; virtual bool Play() wxOVERRIDE;
virtual void Stop() wxOVERRIDE; virtual void Stop() wxOVERRIDE;
@@ -135,6 +128,9 @@ public: // public API
bool SetBackgroundColour( const wxColour &colour ) wxOVERRIDE; bool SetBackgroundColour( const wxColour &colour ) wxOVERRIDE;
static wxAnimationImpl* CreateAnimationImpl(wxAnimationImplType implType);
protected: protected:
virtual void DisplayStaticImage() wxOVERRIDE; virtual void DisplayStaticImage() wxOVERRIDE;
@@ -145,6 +141,11 @@ protected:
void ResetAnim(); void ResetAnim();
void ResetIter(); void ResetIter();
// Helpers to safely access methods in the wxAnimationGTKImpl that are
// specific to the gtk implementation
GdkPixbufAnimation *animation_GetPixbuf() const;
void animation_SetPixbuf(GdkPixbufAnimation* p);
protected: // internal vars protected: // internal vars
GdkPixbufAnimation *m_anim; GdkPixbufAnimation *m_anim;

View File

@@ -301,7 +301,8 @@ bool wxGenericAnimationCtrl::Create(wxWindow *parent, wxWindowID id,
wxGenericAnimationCtrl::~wxGenericAnimationCtrl() wxGenericAnimationCtrl::~wxGenericAnimationCtrl()
{ {
Stop(); if (IsPlaying())
Stop();
} }
bool wxGenericAnimationCtrl::LoadFile(const wxString& filename, wxAnimationType type) bool wxGenericAnimationCtrl::LoadFile(const wxString& filename, wxAnimationType type)

View File

@@ -31,12 +31,12 @@
extern "C" { extern "C" {
static static
void gdk_pixbuf_area_updated(GdkPixbufLoader *loader, void gdk_pixbuf_area_updated(GdkPixbufLoader *loader,
gint WXUNUSED(x), gint WXUNUSED(x),
gint WXUNUSED(y), gint WXUNUSED(y),
gint WXUNUSED(width), gint WXUNUSED(width),
gint WXUNUSED(height), gint WXUNUSED(height),
wxAnimation *anim) wxAnimationGTKImpl *anim)
{ {
if (anim && anim->GetPixbuf() == NULL) if (anim && anim->GetPixbuf() == NULL)
{ {
@@ -48,46 +48,19 @@ void gdk_pixbuf_area_updated(GdkPixbufLoader *loader,
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxAnimation // wxAnimationGTKImpl
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
wxIMPLEMENT_DYNAMIC_CLASS(wxAnimationGTKImpl, wxAnimationImpl);
wxAnimation::wxAnimation(const wxAnimation& that) bool wxAnimationGTKImpl::LoadFile(const wxString &name, wxAnimationType WXUNUSED(type))
: base_type(that)
{
m_pixbuf = that.m_pixbuf;
if (m_pixbuf)
g_object_ref(m_pixbuf);
}
wxAnimation::wxAnimation(GdkPixbufAnimation *p)
{
m_pixbuf = p;
if ( m_pixbuf )
g_object_ref(m_pixbuf);
}
wxAnimation& wxAnimation::operator=(const wxAnimation& that)
{
if (this != &that)
{
base_type::operator=(that);
UnRef();
m_pixbuf = that.m_pixbuf;
if (m_pixbuf)
g_object_ref(m_pixbuf);
}
return *this;
}
bool wxAnimation::LoadFile(const wxString &name, wxAnimationType WXUNUSED(type))
{ {
UnRef(); UnRef();
m_pixbuf = gdk_pixbuf_animation_new_from_file(wxGTK_CONV_FN(name), NULL); m_pixbuf = gdk_pixbuf_animation_new_from_file(wxGTK_CONV_FN(name), NULL);
return IsOk(); return IsOk();
} }
bool wxAnimation::Load(wxInputStream &stream, wxAnimationType type) bool wxAnimationGTKImpl::Load(wxInputStream &stream, wxAnimationType type)
{ {
UnRef(); UnRef();
@@ -174,20 +147,21 @@ bool wxAnimation::Load(wxInputStream &stream, wxAnimationType type)
return data_written; return data_written;
} }
wxSize wxAnimation::GetSize() const wxSize wxAnimationGTKImpl::GetSize() const
{ {
return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf), return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf),
gdk_pixbuf_animation_get_height(m_pixbuf)); gdk_pixbuf_animation_get_height(m_pixbuf));
} }
void wxAnimation::UnRef() void wxAnimationGTKImpl::UnRef()
{ {
base_type::UnRef();
if (m_pixbuf) if (m_pixbuf)
g_object_unref(m_pixbuf); g_object_unref(m_pixbuf);
m_pixbuf = NULL; m_pixbuf = NULL;
} }
void wxAnimation::SetPixbuf(GdkPixbufAnimation* p) void wxAnimationGTKImpl::SetPixbuf(GdkPixbufAnimation* p)
{ {
UnRef(); UnRef();
m_pixbuf = p; m_pixbuf = p;
@@ -247,6 +221,8 @@ bool wxAnimationCtrl::Create( wxWindow *parent, wxWindowID id,
wxAnimationCtrl::~wxAnimationCtrl() wxAnimationCtrl::~wxAnimationCtrl()
{ {
if (IsPlaying())
Stop();
ResetAnim(); ResetAnim();
ResetIter(); ResetIter();
} }
@@ -261,7 +237,7 @@ bool wxAnimationCtrl::LoadFile(const wxString &filename, wxAnimationType type)
bool wxAnimationCtrl::Load(wxInputStream& stream, wxAnimationType type) bool wxAnimationCtrl::Load(wxInputStream& stream, wxAnimationType type)
{ {
wxAnimation anim; wxAnimation anim(wxANIMATION_IMPL_TYPE_NATIVE);
if ( !anim.Load(stream, type) || !anim.IsOk() ) if ( !anim.Load(stream, type) || !anim.IsOk() )
return false; return false;
@@ -277,8 +253,19 @@ void wxAnimationCtrl::SetAnimation(const wxAnimation &anim)
ResetAnim(); ResetAnim();
ResetIter(); ResetIter();
m_animation = anim;
if (!m_animation.IsOk())
{
m_anim = NULL;
DisplayStaticImage();
return;
}
wxCHECK_RET(anim.GetImpl()->GetImplType() == wxANIMATION_IMPL_TYPE_NATIVE,
wxT("incorrect animation implementation type provided") );
// copy underlying GdkPixbuf object // copy underlying GdkPixbuf object
m_anim = anim.GetPixbuf(); m_anim = animation_GetPixbuf();
// m_anim may be null in case wxNullAnimation has been passed // m_anim may be null in case wxNullAnimation has been passed
if (m_anim) if (m_anim)
@@ -459,4 +446,36 @@ void wxAnimationCtrl::OnTimer(wxTimerEvent& WXUNUSED(ev))
} }
} }
// static
wxAnimationImpl* wxAnimationCtrl::CreateAnimationImpl(wxAnimationImplType implType)
{
switch (implType) {
case wxANIMATION_IMPL_TYPE_GENERIC:
return new wxAnimationGenericImpl();
case wxANIMATION_IMPL_TYPE_NATIVE:
return new wxAnimationGTKImpl();
default:
return NULL;
}
}
// helpers to safely access wxAnimationGenericImpl methods
#define ANIMATION (static_cast<wxAnimationGTKImpl*>(m_animation.GetImpl()))
GdkPixbufAnimation* wxAnimationCtrl::animation_GetPixbuf() const
{
wxCHECK_MSG( m_animation.IsOk(), NULL, wxT("invalid animation") );
return ANIMATION->GetPixbuf();
}
void wxAnimationCtrl::animation_SetPixbuf(GdkPixbufAnimation* p)
{
wxCHECK_RET( m_animation.IsOk(), wxT("invalid animation") );
ANIMATION->SetPixbuf(p);
}
#endif // wxUSE_ANIMATIONCTRL #endif // wxUSE_ANIMATIONCTRL