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.
This commit is contained in:
Vadim Zeitlin 2020-04-05 16:55:44 +02:00
parent d0371d75f7
commit fc3669b551
2 changed files with 80 additions and 53 deletions

View File

@ -59,7 +59,8 @@ enum
ID_SET_NULL_ANIMATION, ID_SET_NULL_ANIMATION,
ID_SET_INACTIVE_BITMAP, ID_SET_INACTIVE_BITMAP,
ID_SET_NO_AUTO_RESIZE, ID_SET_NO_AUTO_RESIZE,
ID_SET_BGCOLOR ID_SET_BGCOLOR,
ID_USE_GENERIC
}; };
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) 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_INACTIVE_BITMAP, MyFrame::OnSetInactiveBitmap)
EVT_MENU(ID_SET_NO_AUTO_RESIZE, MyFrame::OnSetNoAutoResize) EVT_MENU(ID_SET_NO_AUTO_RESIZE, MyFrame::OnSetNoAutoResize)
EVT_MENU(ID_SET_BGCOLOR, MyFrame::OnSetBgColor) 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_STOP, MyFrame::OnStop)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
@ -140,6 +144,12 @@ MyFrame::MyFrame(wxWindow *parent,
play_menu->Append(ID_SET_BGCOLOR, "Set background colour...", play_menu->Append(ID_SET_BGCOLOR, "Set background colour...",
"Sets the background colour of the control"); "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; wxMenu *help_menu = new wxMenu;
help_menu->Append(wxID_ABOUT); help_menu->Append(wxID_ABOUT);
@ -213,25 +223,7 @@ void MyFrame::OnSetNoAutoResize(wxCommandEvent& event)
if (style != m_animationCtrl->GetWindowStyle()) if (style != m_animationCtrl->GetWindowStyle())
{ {
// save status of the control before destroying it RecreateAnimation(style);
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();
} }
} }
@ -244,6 +236,58 @@ void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event))
m_animationCtrl->SetBackgroundColour(clr); 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)) void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{ {
Close(); Close();
@ -272,15 +316,14 @@ void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
{ {
wxString filename(dialog.GetPath()); wxString filename(dialog.GetPath());
// enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl wxAnimation temp
#if 0 #ifdef wxHAS_NATIVE_ANIMATIONCTRL
if (m_animationCtrl->LoadFile(filename)) (GetMenuBar()->IsChecked(ID_USE_GENERIC)
m_animationCtrl->Play(); ? wxANIMATION_IMPL_TYPE_GENERIC
else : wxANIMATION_IMPL_TYPE_NATIVE)
wxMessageBox("Sorry, this animation is not a valid format for wxAnimation."); #endif // wxHAS_NATIVE_ANIMATIONCTRL
#else ;
#if 0
wxAnimation temp;
if (!temp.LoadFile(filename)) if (!temp.LoadFile(filename))
{ {
wxLogError("Sorry, this animation is not a valid format for wxAnimation."); 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->SetAnimation(temp);
m_animationCtrl->Play(); 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(); GetSizer()->Layout();
} }

View File

@ -36,17 +36,20 @@ public:
void OnSetBgColor(wxCommandEvent& event); void OnSetBgColor(wxCommandEvent& event);
void OnStop(wxCommandEvent& event); void OnStop(wxCommandEvent& event);
#ifdef wxHAS_NATIVE_ANIMATIONCTRL
void OnUseGeneric(wxCommandEvent& event);
#endif // wxHAS_NATIVE_ANIMATIONCTRL
void OnUpdateUI(wxUpdateUIEvent& event); void OnUpdateUI(wxUpdateUIEvent& event);
#if wxUSE_FILEDLG #if wxUSE_FILEDLG
void OnOpen(wxCommandEvent& event); void OnOpen(wxCommandEvent& event);
#endif // wxUSE_FILEDLG #endif // wxUSE_FILEDLG
wxAnimationCtrl* GetAnimationCtrl() const { return m_animationCtrl; }
protected:
wxAnimationCtrl* m_animationCtrl;
private: private:
void RecreateAnimation(long style);
wxAnimationCtrlBase* m_animationCtrl;
wxDECLARE_EVENT_TABLE(); wxDECLARE_EVENT_TABLE();
}; };