From fc3669b5516c2ced2eec6156b8e3c1ad6d55024d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 5 Apr 2020 16:55:44 +0200 Subject: [PATCH] Add possibility to use generic version to the animation sample Add menu item to switch to the generic version when using the sample on a platform where a native version is available (i.e. wxGTK) in order to allow testing it there easily. --- samples/animate/anitest.cpp | 120 +++++++++++++++++++++--------------- samples/animate/anitest.h | 13 ++-- 2 files changed, 80 insertions(+), 53 deletions(-) diff --git a/samples/animate/anitest.cpp b/samples/animate/anitest.cpp index edd1f669e2..fb4d948de3 100644 --- a/samples/animate/anitest.cpp +++ b/samples/animate/anitest.cpp @@ -59,7 +59,8 @@ enum ID_SET_NULL_ANIMATION, ID_SET_INACTIVE_BITMAP, ID_SET_NO_AUTO_RESIZE, - ID_SET_BGCOLOR + ID_SET_BGCOLOR, + ID_USE_GENERIC }; wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) @@ -68,6 +69,9 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(ID_SET_INACTIVE_BITMAP, MyFrame::OnSetInactiveBitmap) EVT_MENU(ID_SET_NO_AUTO_RESIZE, MyFrame::OnSetNoAutoResize) EVT_MENU(ID_SET_BGCOLOR, MyFrame::OnSetBgColor) +#ifdef wxHAS_NATIVE_ANIMATIONCTRL + EVT_MENU(ID_USE_GENERIC, MyFrame::OnUseGeneric) +#endif // wxHAS_NATIVE_ANIMATIONCTRL EVT_MENU(wxID_STOP, MyFrame::OnStop) EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) @@ -140,6 +144,12 @@ MyFrame::MyFrame(wxWindow *parent, play_menu->Append(ID_SET_BGCOLOR, "Set background colour...", "Sets the background colour of the control"); +#ifdef wxHAS_NATIVE_ANIMATIONCTRL + play_menu->AppendSeparator(); + play_menu->AppendCheckItem(ID_USE_GENERIC, "Use &generic animation\tCtrl+G", + "Selects whether native or generic version is used"); +#endif // wxHAS_NATIVE_ANIMATIONCTRL + wxMenu *help_menu = new wxMenu; help_menu->Append(wxID_ABOUT); @@ -213,25 +223,7 @@ void MyFrame::OnSetNoAutoResize(wxCommandEvent& event) if (style != m_animationCtrl->GetWindowStyle()) { - // save status of the control before destroying it - wxAnimation curr = m_animationCtrl->GetAnimation(); - wxBitmap inactive = m_animationCtrl->GetInactiveBitmap(); - wxColour bg = m_animationCtrl->GetBackgroundColour(); - - // destroy & rebuild - wxAnimationCtrl *old = m_animationCtrl; - m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, curr, - wxDefaultPosition, wxDefaultSize, - style); - - GetSizer()->Replace(old, m_animationCtrl); - delete old; - - // load old status in new control - m_animationCtrl->SetInactiveBitmap(inactive); - m_animationCtrl->SetBackgroundColour(bg); - - GetSizer()->Layout(); + RecreateAnimation(style); } } @@ -244,6 +236,58 @@ void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event)) m_animationCtrl->SetBackgroundColour(clr); } +void MyFrame::RecreateAnimation(long style) +{ + // save status of the control before destroying it + + // We can't reuse the existing animation if we're switching from native to + // generic control or vice versa (as indicated by the absence of change in + // the style, which is the only other reason we can get called). We could + // save the file name we loaded it from and recreate it, of course, but for + // now, for simplicity, just start without any animation in this case. + wxAnimation curr; +#ifdef wxHAS_NATIVE_ANIMATIONCTRL + if ( style != m_animationCtrl->GetWindowStyle() ) + curr = m_animationCtrl->GetAnimation(); +#endif // wxHAS_NATIVE_ANIMATIONCTRL + + wxBitmap inactive = m_animationCtrl->GetInactiveBitmap(); + wxColour bg = m_animationCtrl->GetBackgroundColour(); + + // destroy & rebuild + wxAnimationCtrlBase *old = m_animationCtrl; + +#ifdef wxHAS_NATIVE_ANIMATIONCTRL + if ( GetMenuBar()->IsChecked(ID_USE_GENERIC) ) + m_animationCtrl = new wxGenericAnimationCtrl(this, wxID_ANY, curr, + wxDefaultPosition, + wxDefaultSize, + style); + else +#endif // wxHAS_NATIVE_ANIMATIONCTRL + m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, curr, + wxDefaultPosition, wxDefaultSize, + style); + + GetSizer()->Replace(old, m_animationCtrl); + delete old; + + // load old status in new control + m_animationCtrl->SetInactiveBitmap(inactive); + m_animationCtrl->SetBackgroundColour(bg); + + GetSizer()->Layout(); +} + +#ifdef wxHAS_NATIVE_ANIMATIONCTRL + +void MyFrame::OnUseGeneric(wxCommandEvent& WXUNUSED(event)) +{ + RecreateAnimation(m_animationCtrl->GetWindowStyle()); +} + +#endif // wxHAS_NATIVE_ANIMATIONCTRL + void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(); @@ -272,15 +316,14 @@ void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) { wxString filename(dialog.GetPath()); - // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl -#if 0 - if (m_animationCtrl->LoadFile(filename)) - m_animationCtrl->Play(); - else - wxMessageBox("Sorry, this animation is not a valid format for wxAnimation."); -#else - #if 0 - wxAnimation temp; + wxAnimation temp +#ifdef wxHAS_NATIVE_ANIMATIONCTRL + (GetMenuBar()->IsChecked(ID_USE_GENERIC) + ? wxANIMATION_IMPL_TYPE_GENERIC + : wxANIMATION_IMPL_TYPE_NATIVE) +#endif // wxHAS_NATIVE_ANIMATIONCTRL + ; + if (!temp.LoadFile(filename)) { wxLogError("Sorry, this animation is not a valid format for wxAnimation."); @@ -289,25 +332,6 @@ void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) m_animationCtrl->SetAnimation(temp); m_animationCtrl->Play(); - #else - wxFileInputStream stream(filename); - if (!stream.IsOk()) - { - wxLogError("Sorry, this animation is not a valid format for wxAnimation."); - return; - } - - wxAnimation temp; - if (!temp.Load(stream)) - { - wxLogError("Sorry, this animation is not a valid format for wxAnimation."); - return; - } - - m_animationCtrl->SetAnimation(temp); - m_animationCtrl->Play(); - #endif -#endif GetSizer()->Layout(); } diff --git a/samples/animate/anitest.h b/samples/animate/anitest.h index 330d10acd5..505dfdb305 100644 --- a/samples/animate/anitest.h +++ b/samples/animate/anitest.h @@ -36,17 +36,20 @@ public: void OnSetBgColor(wxCommandEvent& event); void OnStop(wxCommandEvent& event); +#ifdef wxHAS_NATIVE_ANIMATIONCTRL + void OnUseGeneric(wxCommandEvent& event); +#endif // wxHAS_NATIVE_ANIMATIONCTRL + void OnUpdateUI(wxUpdateUIEvent& event); #if wxUSE_FILEDLG void OnOpen(wxCommandEvent& event); #endif // wxUSE_FILEDLG - wxAnimationCtrl* GetAnimationCtrl() const { return m_animationCtrl; } - -protected: - wxAnimationCtrl* m_animationCtrl; - private: + void RecreateAnimation(long style); + + wxAnimationCtrlBase* m_animationCtrl; + wxDECLARE_EVENT_TABLE(); };