2006-10-09 13:51:07 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: anitest.cpp
|
|
|
|
// Purpose: Animation sample
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Modified by: Francesco Montorsi
|
|
|
|
// Created: 02/07/2001
|
|
|
|
// Copyright: (c) Julian Smart
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
// declarations
|
|
|
|
// ===========================================================================
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2012-03-03 19:28:58 -05:00
|
|
|
#ifndef wxHAS_IMAGES_IN_RESOURCES
|
2010-06-20 13:42:33 -04:00
|
|
|
#include "../sample.xpm"
|
2006-10-09 13:51:07 -04:00
|
|
|
#endif
|
|
|
|
|
2006-10-29 13:04:09 -05:00
|
|
|
#include "wx/aboutdlg.h"
|
2006-11-17 13:15:44 -05:00
|
|
|
#include "wx/artprov.h"
|
|
|
|
#include "wx/colordlg.h"
|
2009-02-21 10:40:15 -05:00
|
|
|
#include "wx/wfstream.h"
|
|
|
|
|
2006-10-09 13:51:07 -04:00
|
|
|
#include "anitest.h"
|
|
|
|
|
2006-10-29 13:04:09 -05:00
|
|
|
#if !wxUSE_ANIMATIONCTRL
|
|
|
|
#error Cannot compile this sample if wxAnimationCtrl is not enabled
|
|
|
|
#endif
|
|
|
|
|
2020-04-05 19:09:36 -04:00
|
|
|
#ifdef wxHAS_NATIVE_ANIMATIONCTRL
|
|
|
|
#include "wx/generic/animate.h"
|
|
|
|
#endif
|
2006-10-29 13:04:09 -05:00
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|
2006-10-09 13:51:07 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// global variables
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// event tables
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2006-11-17 13:15:44 -05:00
|
|
|
ID_PLAY = 1,
|
|
|
|
ID_SET_NULL_ANIMATION,
|
|
|
|
ID_SET_INACTIVE_BITMAP,
|
|
|
|
ID_SET_NO_AUTO_RESIZE,
|
2020-04-05 10:55:44 -04:00
|
|
|
ID_SET_BGCOLOR,
|
|
|
|
ID_USE_GENERIC
|
2006-10-09 13:51:07 -04:00
|
|
|
};
|
|
|
|
|
2014-03-30 03:07:55 -04:00
|
|
|
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
2006-10-09 14:57:07 -04:00
|
|
|
EVT_MENU(ID_PLAY, MyFrame::OnPlay)
|
2006-11-17 13:15:44 -05:00
|
|
|
EVT_MENU(ID_SET_NULL_ANIMATION, MyFrame::OnSetNullAnimation)
|
|
|
|
EVT_MENU(ID_SET_INACTIVE_BITMAP, MyFrame::OnSetInactiveBitmap)
|
|
|
|
EVT_MENU(ID_SET_NO_AUTO_RESIZE, MyFrame::OnSetNoAutoResize)
|
|
|
|
EVT_MENU(ID_SET_BGCOLOR, MyFrame::OnSetBgColor)
|
2020-04-05 10:55:44 -04:00
|
|
|
#ifdef wxHAS_NATIVE_ANIMATIONCTRL
|
|
|
|
EVT_MENU(ID_USE_GENERIC, MyFrame::OnUseGeneric)
|
|
|
|
#endif // wxHAS_NATIVE_ANIMATIONCTRL
|
2006-11-17 13:15:44 -05:00
|
|
|
|
2006-10-09 13:51:07 -04:00
|
|
|
EVT_MENU(wxID_STOP, MyFrame::OnStop)
|
|
|
|
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
|
|
|
|
EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
|
|
|
|
#if wxUSE_FILEDLG
|
|
|
|
EVT_MENU(wxID_OPEN, MyFrame::OnOpen)
|
|
|
|
#endif // wxUSE_FILEDLG
|
|
|
|
|
|
|
|
EVT_SIZE(MyFrame::OnSize)
|
|
|
|
EVT_UPDATE_UI(wxID_ANY, MyFrame::OnUpdateUI)
|
2014-03-30 03:07:55 -04:00
|
|
|
wxEND_EVENT_TABLE()
|
2006-10-09 13:51:07 -04:00
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
// implementation
|
|
|
|
// ===========================================================================
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// MyApp
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Initialise this in OnInit, not statically
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2007-02-03 19:34:18 -05:00
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2006-10-09 13:51:07 -04:00
|
|
|
// Create the main frame window
|
|
|
|
|
2006-10-10 03:19:26 -04:00
|
|
|
MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, "Animation Demo",
|
2006-11-17 13:15:44 -05:00
|
|
|
wxDefaultPosition, wxSize(500, 400),
|
|
|
|
wxDEFAULT_FRAME_STYLE);
|
2009-02-15 19:27:52 -05:00
|
|
|
frame->Show(true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2006-10-09 13:51:07 -04:00
|
|
|
|
2009-02-15 19:27:52 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// MyFrame
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Define my frame constructor
|
|
|
|
MyFrame::MyFrame(wxWindow *parent,
|
|
|
|
const wxWindowID id,
|
|
|
|
const wxString& title,
|
|
|
|
const wxPoint& pos,
|
|
|
|
const wxSize& size,
|
|
|
|
const long style)
|
2019-10-10 16:29:12 -04:00
|
|
|
: wxFrame(parent, id, title, pos, size, style)
|
2009-02-15 19:27:52 -05:00
|
|
|
{
|
|
|
|
SetIcon(wxICON(sample));
|
2006-10-09 13:51:07 -04:00
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
|
|
|
#if wxUSE_FILEDLG
|
|
|
|
file_menu->Append(wxID_OPEN, "&Open Animation...\tCtrl+O", "Loads an animation");
|
|
|
|
#endif // wxUSE_FILEDLG
|
|
|
|
file_menu->Append(wxID_EXIT);
|
|
|
|
|
|
|
|
wxMenu *play_menu = new wxMenu;
|
2006-10-09 14:57:07 -04:00
|
|
|
play_menu->Append(ID_PLAY, "Play\tCtrl+P", "Play the animation");
|
2009-12-08 11:35:54 -05:00
|
|
|
play_menu->Append(wxID_STOP, "Stop\tCtrl+S", "Stop the animation");
|
2006-11-17 13:15:44 -05:00
|
|
|
play_menu->AppendSeparator();
|
|
|
|
play_menu->Append(ID_SET_NULL_ANIMATION, "Set null animation",
|
|
|
|
"Sets the empty animation in the control");
|
|
|
|
play_menu->AppendCheckItem(ID_SET_INACTIVE_BITMAP, "Set inactive bitmap",
|
|
|
|
"Sets an inactive bitmap for the control");
|
|
|
|
play_menu->AppendCheckItem(ID_SET_NO_AUTO_RESIZE, "Set no autoresize",
|
|
|
|
"Tells the control not to resize automatically");
|
|
|
|
play_menu->Append(ID_SET_BGCOLOR, "Set background colour...",
|
|
|
|
"Sets the background colour of the control");
|
2006-10-09 13:51:07 -04:00
|
|
|
|
2020-04-05 10:55:44 -04:00
|
|
|
#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
|
|
|
|
|
2006-10-09 13:51:07 -04:00
|
|
|
wxMenu *help_menu = new wxMenu;
|
|
|
|
help_menu->Append(wxID_ABOUT);
|
|
|
|
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
|
|
|
|
menu_bar->Append(file_menu, "&File");
|
|
|
|
menu_bar->Append(play_menu, "&Animation");
|
|
|
|
menu_bar->Append(help_menu, "&Help");
|
|
|
|
|
2009-02-15 19:27:52 -05:00
|
|
|
// Associate the menu bar with this frame
|
|
|
|
SetMenuBar(menu_bar);
|
2006-10-09 13:51:07 -04:00
|
|
|
|
|
|
|
#if wxUSE_STATUSBAR
|
2009-02-15 19:27:52 -05:00
|
|
|
CreateStatusBar();
|
2006-10-09 13:51:07 -04:00
|
|
|
#endif // wxUSE_STATUSBAR
|
|
|
|
|
2006-11-17 13:15:44 -05:00
|
|
|
// use a wxBoxSizer otherwise wxFrame will automatically
|
|
|
|
// resize the m_animationCtrl to fill its client area on
|
|
|
|
// user resizes
|
|
|
|
wxSizer *sz = new wxBoxSizer(wxVERTICAL);
|
|
|
|
sz->Add(new wxStaticText(this, wxID_ANY, "wxAnimationCtrl:"),
|
|
|
|
wxSizerFlags().Centre().Border());
|
|
|
|
|
|
|
|
m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY);
|
2006-10-09 13:51:07 -04:00
|
|
|
if (m_animationCtrl->LoadFile("throbber.gif"))
|
|
|
|
m_animationCtrl->Play();
|
2006-11-17 13:15:44 -05:00
|
|
|
|
|
|
|
sz->Add(m_animationCtrl, wxSizerFlags().Centre().Border());
|
|
|
|
SetSizer(sz);
|
2006-10-09 13:51:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::~MyFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
if (!m_animationCtrl->Play())
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2006-10-09 13:51:07 -04:00
|
|
|
wxLogError("Invalid animation");
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2006-10-09 13:51:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
m_animationCtrl->Stop();
|
|
|
|
}
|
|
|
|
|
2006-11-17 13:15:44 -05:00
|
|
|
void MyFrame::OnSetNullAnimation(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
m_animationCtrl->SetAnimation(wxNullAnimation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnSetInactiveBitmap(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
if (event.IsChecked())
|
|
|
|
{
|
|
|
|
// set a dummy bitmap as the inactive bitmap
|
|
|
|
wxBitmap bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE);
|
|
|
|
m_animationCtrl->SetInactiveBitmap(bmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_animationCtrl->SetInactiveBitmap(wxNullBitmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnSetNoAutoResize(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
// recreate the control with the new flag if necessary
|
|
|
|
long style = wxAC_DEFAULT_STYLE |
|
|
|
|
(event.IsChecked() ? wxAC_NO_AUTORESIZE : 0);
|
|
|
|
|
|
|
|
if (style != m_animationCtrl->GetWindowStyle())
|
|
|
|
{
|
2020-04-05 10:55:44 -04:00
|
|
|
RecreateAnimation(style);
|
2006-11-17 13:15:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxColour clr = wxGetColourFromUser(this, m_animationCtrl->GetBackgroundColour(),
|
|
|
|
"Choose the background colour");
|
|
|
|
|
|
|
|
if (clr.IsOk())
|
|
|
|
m_animationCtrl->SetBackgroundColour(clr);
|
|
|
|
}
|
|
|
|
|
2020-04-05 10:55:44 -04:00
|
|
|
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
|
|
|
|
|
2006-10-09 13:51:07 -04:00
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
|
|
|
|
{
|
2006-10-29 13:04:09 -05:00
|
|
|
wxAboutDialogInfo info;
|
|
|
|
info.SetName(_("wxAnimationCtrl and wxAnimation sample"));
|
|
|
|
info.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
|
|
|
|
info.SetCopyright("(C) 2006 Julian Smart");
|
2006-10-09 13:51:07 -04:00
|
|
|
|
2006-10-29 13:04:09 -05:00
|
|
|
info.AddDeveloper("Julian Smart");
|
|
|
|
info.AddDeveloper("Guillermo Rodriguez Garcia");
|
|
|
|
info.AddDeveloper("Francesco Montorsi");
|
2006-10-09 13:51:07 -04:00
|
|
|
|
2017-03-20 18:27:57 -04:00
|
|
|
wxAboutBox(info, this);
|
2006-10-09 13:51:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if wxUSE_FILEDLG
|
|
|
|
void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxFileDialog dialog(this, "Please choose an animation",
|
|
|
|
wxEmptyString, wxEmptyString, "*.gif;*.ani", wxFD_OPEN);
|
|
|
|
if (dialog.ShowModal() == wxID_OK)
|
|
|
|
{
|
|
|
|
wxString filename(dialog.GetPath());
|
|
|
|
|
2020-04-05 19:00:15 -04:00
|
|
|
wxAnimation temp(m_animationCtrl->CreateAnimation());
|
2020-04-05 10:55:44 -04:00
|
|
|
if (!temp.LoadFile(filename))
|
2006-10-09 13:51:07 -04:00
|
|
|
{
|
|
|
|
wxLogError("Sorry, this animation is not a valid format for wxAnimation.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_animationCtrl->SetAnimation(temp);
|
|
|
|
m_animationCtrl->Play();
|
2006-11-17 13:15:44 -05:00
|
|
|
|
|
|
|
GetSizer()->Layout();
|
2006-10-09 13:51:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // wxUSE_FILEDLG
|
|
|
|
|
|
|
|
void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) )
|
|
|
|
{
|
|
|
|
GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying());
|
2006-10-09 14:57:07 -04:00
|
|
|
GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying());
|
2006-11-17 13:15:44 -05:00
|
|
|
GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE)->Enable(!m_animationCtrl->IsPlaying());
|
2006-10-09 13:51:07 -04:00
|
|
|
}
|
|
|
|
|