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"
36 #include "wx/artprov.h"
37 #include "wx/colordlg.h"
40 #if !wxUSE_ANIMATIONCTRL
41 #error Cannot compile this sample if wxAnimationCtrl is not enabled
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
58 ID_SET_NULL_ANIMATION
,
59 ID_SET_INACTIVE_BITMAP
,
60 ID_SET_NO_AUTO_RESIZE
,
64 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
65 EVT_MENU(ID_PLAY
, MyFrame::OnPlay
)
66 EVT_MENU(ID_SET_NULL_ANIMATION
, MyFrame::OnSetNullAnimation
)
67 EVT_MENU(ID_SET_INACTIVE_BITMAP
, MyFrame::OnSetInactiveBitmap
)
68 EVT_MENU(ID_SET_NO_AUTO_RESIZE
, MyFrame::OnSetNoAutoResize
)
69 EVT_MENU(ID_SET_BGCOLOR
, MyFrame::OnSetBgColor
)
71 EVT_MENU(wxID_STOP
, MyFrame::OnStop
)
72 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
73 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
75 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
76 #endif // wxUSE_FILEDLG
78 EVT_SIZE(MyFrame::OnSize
)
79 EVT_UPDATE_UI(wxID_ANY
, MyFrame::OnUpdateUI
)
82 // ===========================================================================
84 // ===========================================================================
86 // ---------------------------------------------------------------------------
88 // ---------------------------------------------------------------------------
90 // Initialise this in OnInit, not statically
93 if ( !wxApp::OnInit() )
96 // Create the main frame window
98 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, _T("Animation Demo"),
99 wxDefaultPosition
, wxSize(500, 400),
100 wxDEFAULT_FRAME_STYLE
);
103 frame
->SetIcon(wxICON(sample
));
106 wxMenu
*file_menu
= new wxMenu
;
109 file_menu
->Append(wxID_OPEN
, _T("&Open Animation...\tCtrl+O"), _T("Loads an animation"));
110 #endif // wxUSE_FILEDLG
111 file_menu
->Append(wxID_EXIT
);
113 wxMenu
*play_menu
= new wxMenu
;
114 play_menu
->Append(ID_PLAY
, _T("Play\tCtrl+P"), _T("Play the animation"));
115 play_menu
->Append(wxID_STOP
, _T("Stop\tCtrl+P"), _T("Stop the animation"));
116 play_menu
->AppendSeparator();
117 play_menu
->Append(ID_SET_NULL_ANIMATION
, _T("Set null animation"),
118 _T("Sets the empty animation in the control"));
119 play_menu
->AppendCheckItem(ID_SET_INACTIVE_BITMAP
, _T("Set inactive bitmap"),
120 _T("Sets an inactive bitmap for the control"));
121 play_menu
->AppendCheckItem(ID_SET_NO_AUTO_RESIZE
, _T("Set no autoresize"),
122 _T("Tells the control not to resize automatically"));
123 play_menu
->Append(ID_SET_BGCOLOR
, _T("Set background colour..."),
124 _T("Sets the background colour of the control"));
126 wxMenu
*help_menu
= new wxMenu
;
127 help_menu
->Append(wxID_ABOUT
);
129 wxMenuBar
*menu_bar
= new wxMenuBar
;
131 menu_bar
->Append(file_menu
, _T("&File"));
132 menu_bar
->Append(play_menu
, _T("&Animation"));
133 menu_bar
->Append(help_menu
, _T("&Help"));
135 // Associate the menu bar with the frame
136 frame
->SetMenuBar(menu_bar
);
139 frame
->CreateStatusBar();
140 #endif // wxUSE_STATUSBAR
149 // ---------------------------------------------------------------------------
151 // ---------------------------------------------------------------------------
153 #include "wx/wfstream.h"
155 // Define my frame constructor
156 MyFrame::MyFrame(wxWindow
*parent
,
158 const wxString
& title
,
162 : wxFrame(parent
, id
, title
, pos
, size
,
163 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
165 // use a wxBoxSizer otherwise wxFrame will automatically
166 // resize the m_animationCtrl to fill its client area on
168 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
169 sz
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxAnimationCtrl:")),
170 wxSizerFlags().Centre().Border());
172 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
);
173 if (m_animationCtrl
->LoadFile(wxT("throbber.gif")))
174 m_animationCtrl
->Play();
176 sz
->Add(m_animationCtrl
, wxSizerFlags().Centre().Border());
184 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
186 if (!m_animationCtrl
->Play())
187 wxLogError(wxT("Invalid animation"));
190 void MyFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))
192 m_animationCtrl
->Stop();
195 void MyFrame::OnSetNullAnimation(wxCommandEvent
& WXUNUSED(event
))
197 m_animationCtrl
->SetAnimation(wxNullAnimation
);
200 void MyFrame::OnSetInactiveBitmap(wxCommandEvent
& event
)
202 if (event
.IsChecked())
204 // set a dummy bitmap as the inactive bitmap
205 wxBitmap bmp
= wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
);
206 m_animationCtrl
->SetInactiveBitmap(bmp
);
209 m_animationCtrl
->SetInactiveBitmap(wxNullBitmap
);
212 void MyFrame::OnSetNoAutoResize(wxCommandEvent
& event
)
214 // recreate the control with the new flag if necessary
215 long style
= wxAC_DEFAULT_STYLE
|
216 (event
.IsChecked() ? wxAC_NO_AUTORESIZE
: 0);
218 if (style
!= m_animationCtrl
->GetWindowStyle())
220 // save status of the control before destroying it
221 wxAnimation curr
= m_animationCtrl
->GetAnimation();
222 wxBitmap inactive
= m_animationCtrl
->GetInactiveBitmap();
223 wxColour bg
= m_animationCtrl
->GetBackgroundColour();
226 wxAnimationCtrl
*old
= m_animationCtrl
;
227 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
, curr
,
228 wxDefaultPosition
, wxDefaultSize
,
231 GetSizer()->Replace(old
, m_animationCtrl
);
234 // load old status in new control
235 m_animationCtrl
->SetInactiveBitmap(inactive
);
236 m_animationCtrl
->SetBackgroundColour(bg
);
238 GetSizer()->Layout();
242 void MyFrame::OnSetBgColor(wxCommandEvent
& WXUNUSED(event
))
244 wxColour clr
= wxGetColourFromUser(this, m_animationCtrl
->GetBackgroundColour(),
245 wxT("Choose the background colour"));
248 m_animationCtrl
->SetBackgroundColour(clr
);
251 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
256 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
258 wxAboutDialogInfo info
;
259 info
.SetName(_("wxAnimationCtrl and wxAnimation sample"));
260 info
.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
261 info
.SetCopyright(_T("(C) 2006 Julian Smart"));
263 info
.AddDeveloper(_T("Julian Smart"));
264 info
.AddDeveloper(_T("Guillermo Rodriguez Garcia"));
265 info
.AddDeveloper(_T("Francesco Montorsi"));
271 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
273 wxFileDialog
dialog(this, _T("Please choose an animation"),
274 wxEmptyString
, wxEmptyString
, wxT("*.gif;*.ani"), wxFD_OPEN
);
275 if (dialog
.ShowModal() == wxID_OK
)
277 wxString
filename(dialog
.GetPath());
279 // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
281 if (m_animationCtrl
->LoadFile(filename
))
282 m_animationCtrl
->Play();
284 wxMessageBox(_T("Sorry, this animation is not a valid format for wxAnimation."));
288 if (!temp
.LoadFile(filename
))
290 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
294 m_animationCtrl
->SetAnimation(temp
);
295 m_animationCtrl
->Play();
297 wxFileInputStream
stream(filename
);
300 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
305 if (!temp
.Load(stream
))
307 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
311 m_animationCtrl
->SetAnimation(temp
);
312 m_animationCtrl
->Play();
316 GetSizer()->Layout();
319 #endif // wxUSE_FILEDLG
321 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& WXUNUSED(event
) )
323 GetMenuBar()->FindItem(wxID_STOP
)->Enable(m_animationCtrl
->IsPlaying());
324 GetMenuBar()->FindItem(ID_PLAY
)->Enable(!m_animationCtrl
->IsPlaying());
325 GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE
)->Enable(!m_animationCtrl
->IsPlaying());