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 // Create the main frame window
95 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, _T("Animation Demo"),
96 wxDefaultPosition
, wxSize(500, 400),
97 wxDEFAULT_FRAME_STYLE
);
100 frame
->SetIcon(wxICON(sample
));
103 wxMenu
*file_menu
= new wxMenu
;
106 file_menu
->Append(wxID_OPEN
, _T("&Open Animation...\tCtrl+O"), _T("Loads an animation"));
107 #endif // wxUSE_FILEDLG
108 file_menu
->Append(wxID_EXIT
);
110 wxMenu
*play_menu
= new wxMenu
;
111 play_menu
->Append(ID_PLAY
, _T("Play\tCtrl+P"), _T("Play the animation"));
112 play_menu
->Append(wxID_STOP
, _T("Stop\tCtrl+P"), _T("Stop the animation"));
113 play_menu
->AppendSeparator();
114 play_menu
->Append(ID_SET_NULL_ANIMATION
, _T("Set null animation"),
115 _T("Sets the empty animation in the control"));
116 play_menu
->AppendCheckItem(ID_SET_INACTIVE_BITMAP
, _T("Set inactive bitmap"),
117 _T("Sets an inactive bitmap for the control"));
118 play_menu
->AppendCheckItem(ID_SET_NO_AUTO_RESIZE
, _T("Set no autoresize"),
119 _T("Tells the control not to resize automatically"));
120 play_menu
->Append(ID_SET_BGCOLOR
, _T("Set background colour..."),
121 _T("Sets the background colour of the control"));
123 wxMenu
*help_menu
= new wxMenu
;
124 help_menu
->Append(wxID_ABOUT
);
126 wxMenuBar
*menu_bar
= new wxMenuBar
;
128 menu_bar
->Append(file_menu
, _T("&File"));
129 menu_bar
->Append(play_menu
, _T("&Animation"));
130 menu_bar
->Append(help_menu
, _T("&Help"));
132 // Associate the menu bar with the frame
133 frame
->SetMenuBar(menu_bar
);
136 frame
->CreateStatusBar();
137 #endif // wxUSE_STATUSBAR
146 // ---------------------------------------------------------------------------
148 // ---------------------------------------------------------------------------
150 #include "wx/wfstream.h"
152 // Define my frame constructor
153 MyFrame::MyFrame(wxWindow
*parent
,
155 const wxString
& title
,
159 : wxFrame(parent
, id
, title
, pos
, size
,
160 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
162 // use a wxBoxSizer otherwise wxFrame will automatically
163 // resize the m_animationCtrl to fill its client area on
165 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
166 sz
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxAnimationCtrl:")),
167 wxSizerFlags().Centre().Border());
169 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
);
170 if (m_animationCtrl
->LoadFile(wxT("throbber.gif")))
171 m_animationCtrl
->Play();
173 sz
->Add(m_animationCtrl
, wxSizerFlags().Centre().Border());
181 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
183 if (!m_animationCtrl
->Play())
184 wxLogError(wxT("Invalid animation"));
187 void MyFrame::OnStop(wxCommandEvent
& WXUNUSED(event
))
189 m_animationCtrl
->Stop();
192 void MyFrame::OnSetNullAnimation(wxCommandEvent
& WXUNUSED(event
))
194 m_animationCtrl
->SetAnimation(wxNullAnimation
);
197 void MyFrame::OnSetInactiveBitmap(wxCommandEvent
& event
)
199 if (event
.IsChecked())
201 // set a dummy bitmap as the inactive bitmap
202 wxBitmap bmp
= wxArtProvider::GetBitmap(wxART_MISSING_IMAGE
);
203 m_animationCtrl
->SetInactiveBitmap(bmp
);
206 m_animationCtrl
->SetInactiveBitmap(wxNullBitmap
);
209 void MyFrame::OnSetNoAutoResize(wxCommandEvent
& event
)
211 // recreate the control with the new flag if necessary
212 long style
= wxAC_DEFAULT_STYLE
|
213 (event
.IsChecked() ? wxAC_NO_AUTORESIZE
: 0);
215 if (style
!= m_animationCtrl
->GetWindowStyle())
217 // save status of the control before destroying it
218 wxAnimation curr
= m_animationCtrl
->GetAnimation();
219 wxBitmap inactive
= m_animationCtrl
->GetInactiveBitmap();
220 wxColour bg
= m_animationCtrl
->GetBackgroundColour();
223 wxAnimationCtrl
*old
= m_animationCtrl
;
224 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
, curr
,
225 wxDefaultPosition
, wxDefaultSize
,
228 GetSizer()->Replace(old
, m_animationCtrl
);
231 // load old status in new control
232 m_animationCtrl
->SetInactiveBitmap(inactive
);
233 m_animationCtrl
->SetBackgroundColour(bg
);
235 GetSizer()->Layout();
239 void MyFrame::OnSetBgColor(wxCommandEvent
& WXUNUSED(event
))
241 wxColour clr
= wxGetColourFromUser(this, m_animationCtrl
->GetBackgroundColour(),
242 wxT("Choose the background colour"));
245 m_animationCtrl
->SetBackgroundColour(clr
);
248 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
253 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
255 wxAboutDialogInfo info
;
256 info
.SetName(_("wxAnimationCtrl and wxAnimation sample"));
257 info
.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
258 info
.SetCopyright(_T("(C) 2006 Julian Smart"));
260 info
.AddDeveloper(_T("Julian Smart"));
261 info
.AddDeveloper(_T("Guillermo Rodriguez Garcia"));
262 info
.AddDeveloper(_T("Francesco Montorsi"));
268 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
270 wxFileDialog
dialog(this, _T("Please choose an animation"),
271 wxEmptyString
, wxEmptyString
, wxT("*.gif;*.ani"), wxFD_OPEN
);
272 if (dialog
.ShowModal() == wxID_OK
)
274 wxString
filename(dialog
.GetPath());
276 // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
278 if (m_animationCtrl
->LoadFile(filename
))
279 m_animationCtrl
->Play();
281 wxMessageBox(_T("Sorry, this animation is not a valid format for wxAnimation."));
285 if (!temp
.LoadFile(filename
))
287 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
291 m_animationCtrl
->SetAnimation(temp
);
292 m_animationCtrl
->Play();
294 wxFileInputStream
stream(filename
);
297 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
302 if (!temp
.Load(stream
))
304 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
308 m_animationCtrl
->SetAnimation(temp
);
309 m_animationCtrl
->Play();
313 GetSizer()->Layout();
316 #endif // wxUSE_FILEDLG
318 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& WXUNUSED(event
) )
320 GetMenuBar()->FindItem(wxID_STOP
)->Enable(m_animationCtrl
->IsPlaying());
321 GetMenuBar()->FindItem(ID_PLAY
)->Enable(!m_animationCtrl
->IsPlaying());
322 GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE
)->Enable(!m_animationCtrl
->IsPlaying());