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_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,13 +223,48 @@ void MyFrame::OnSetNoAutoResize(wxCommandEvent& event)
if (style != m_animationCtrl->GetWindowStyle())
{
RecreateAnimation(style);
}
}
void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event))
{
wxColour clr = wxGetColourFromUser(this, m_animationCtrl->GetBackgroundColour(),
"Choose the background colour");
if (clr.IsOk())
m_animationCtrl->SetBackgroundColour(clr);
}
void MyFrame::RecreateAnimation(long style)
{
// save status of the control before destroying it
wxAnimation curr = m_animationCtrl->GetAnimation();
// 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
wxAnimationCtrl *old = m_animationCtrl;
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);
@ -232,18 +277,17 @@ void MyFrame::OnSetNoAutoResize(wxCommandEvent& event)
m_animationCtrl->SetBackgroundColour(bg);
GetSizer()->Layout();
}
}
void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event))
#ifdef wxHAS_NATIVE_ANIMATIONCTRL
void MyFrame::OnUseGeneric(wxCommandEvent& WXUNUSED(event))
{
wxColour clr = wxGetColourFromUser(this, m_animationCtrl->GetBackgroundColour(),
"Choose the background colour");
if (clr.IsOk())
m_animationCtrl->SetBackgroundColour(clr);
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();
}

View File

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