1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Animation sample
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
35 #include "wx/aboutdlg.h"
38 #if !wxUSE_ANIMATIONCTRL
39 #error Cannot compile this sample if wxAnimationCtrl is not enabled
45 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
58 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
59 EVT_MENU(ID_PLAY
, MyFrame::OnPlay
)
60 EVT_MENU(wxID_STOP
, MyFrame::OnStop
)
61 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
62 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
64 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
65 #endif // wxUSE_FILEDLG
67 EVT_SIZE(MyFrame::OnSize
)
68 EVT_UPDATE_UI(wxID_ANY
, MyFrame::OnUpdateUI
)
71 // ===========================================================================
73 // ===========================================================================
75 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
79 // Initialise this in OnInit, not statically
82 // Create the main frame window
84 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, _T("Animation Demo"),
85 wxDefaultPosition
, wxSize(500, 400),
86 wxDEFAULT_FRAME_STYLE
);
89 frame
->SetIcon(wxICON(sample
));
92 wxMenu
*file_menu
= new wxMenu
;
95 file_menu
->Append(wxID_OPEN
, _T("&Open Animation...\tCtrl+O"), _T("Loads an animation"));
96 #endif // wxUSE_FILEDLG
97 file_menu
->Append(wxID_EXIT
);
99 wxMenu
*play_menu
= new wxMenu
;
100 play_menu
->Append(ID_PLAY
, _T("Play\tCtrl+P"), _T("Play the animation"));
101 play_menu
->Append(wxID_STOP
, _T("Stop\tCtrl+P"), _T("Stop the animation"));
103 wxMenu
*help_menu
= new wxMenu
;
104 help_menu
->Append(wxID_ABOUT
);
106 wxMenuBar
*menu_bar
= new wxMenuBar
;
108 menu_bar
->Append(file_menu
, _T("&File"));
109 menu_bar
->Append(play_menu
, _T("&Animation"));
110 menu_bar
->Append(help_menu
, _T("&Help"));
112 // Associate the menu bar with the frame
113 frame
->SetMenuBar(menu_bar
);
116 frame
->CreateStatusBar();
117 #endif // wxUSE_STATUSBAR
126 // ---------------------------------------------------------------------------
128 // ---------------------------------------------------------------------------
130 #include "wx/wfstream.h"
132 // Define my frame constructor
133 MyFrame::MyFrame(wxWindow
*parent
,
135 const wxString
& title
,
139 : wxFrame(parent
, id
, title
, pos
, size
,
140 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
142 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
, wxNullAnimation
,
143 wxPoint(0,0),wxSize(100,100));
144 if (m_animationCtrl
->LoadFile(wxT("throbber.gif")))
145 m_animationCtrl
->Play();
152 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
154 if (!m_animationCtrl
->Play())
155 wxLogError(wxT("Invalid animation"));
158 void MyFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))
160 m_animationCtrl
->Stop();
163 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
168 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
170 wxAboutDialogInfo info
;
171 info
.SetName(_("wxAnimationCtrl and wxAnimation sample"));
172 info
.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
173 info
.SetCopyright(_T("(C) 2006 Julian Smart"));
175 info
.AddDeveloper(_T("Julian Smart"));
176 info
.AddDeveloper(_T("Guillermo Rodriguez Garcia"));
177 info
.AddDeveloper(_T("Francesco Montorsi"));
183 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
185 wxFileDialog
dialog(this, _T("Please choose an animation"),
186 wxEmptyString
, wxEmptyString
, wxT("*.gif;*.ani"), wxFD_OPEN
);
187 if (dialog
.ShowModal() == wxID_OK
)
189 wxString
filename(dialog
.GetPath());
191 // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
193 if (m_animationCtrl
->LoadFile(filename
))
194 m_animationCtrl
->Play();
196 wxMessageBox(_T("Sorry, this animation is not a valid format for wxAnimation."));
200 if (!temp
.LoadFile(filename
))
202 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
206 m_animationCtrl
->SetAnimation(temp
);
207 m_animationCtrl
->Play();
209 wxFileInputStream
stream(filename
);
212 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
217 if (!temp
.Load(stream
))
219 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
223 m_animationCtrl
->SetAnimation(temp
);
224 m_animationCtrl
->Play();
229 #endif // wxUSE_FILEDLG
231 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& WXUNUSED(event
) )
233 GetMenuBar()->FindItem(wxID_STOP
)->Enable(m_animationCtrl
->IsPlaying());
234 GetMenuBar()->FindItem(ID_PLAY
)->Enable(!m_animationCtrl
->IsPlaying());