1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Animation sample
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
30 #ifndef wxHAS_IMAGES_IN_RESOURCES
31 #include "../sample.xpm"
34 #include "wx/aboutdlg.h"
35 #include "wx/artprov.h"
36 #include "wx/colordlg.h"
37 #include "wx/wfstream.h"
41 #if !wxUSE_ANIMATIONCTRL
42 #error Cannot compile this sample if wxAnimationCtrl is not enabled
48 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
59 ID_SET_NULL_ANIMATION
,
60 ID_SET_INACTIVE_BITMAP
,
61 ID_SET_NO_AUTO_RESIZE
,
65 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
66 EVT_MENU(ID_PLAY
, MyFrame::OnPlay
)
67 EVT_MENU(ID_SET_NULL_ANIMATION
, MyFrame::OnSetNullAnimation
)
68 EVT_MENU(ID_SET_INACTIVE_BITMAP
, MyFrame::OnSetInactiveBitmap
)
69 EVT_MENU(ID_SET_NO_AUTO_RESIZE
, MyFrame::OnSetNoAutoResize
)
70 EVT_MENU(ID_SET_BGCOLOR
, MyFrame::OnSetBgColor
)
72 EVT_MENU(wxID_STOP
, MyFrame::OnStop
)
73 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
74 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
76 EVT_MENU(wxID_OPEN
, MyFrame::OnOpen
)
77 #endif // wxUSE_FILEDLG
79 EVT_SIZE(MyFrame::OnSize
)
80 EVT_UPDATE_UI(wxID_ANY
, MyFrame::OnUpdateUI
)
83 // ===========================================================================
85 // ===========================================================================
87 // ---------------------------------------------------------------------------
89 // ---------------------------------------------------------------------------
91 // Initialise this in OnInit, not statically
94 if ( !wxApp::OnInit() )
97 // Create the main frame window
99 MyFrame
* frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, wxT("Animation Demo"),
100 wxDefaultPosition
, wxSize(500, 400),
101 wxDEFAULT_FRAME_STYLE
);
107 // ---------------------------------------------------------------------------
109 // ---------------------------------------------------------------------------
111 // Define my frame constructor
112 MyFrame::MyFrame(wxWindow
*parent
,
114 const wxString
& title
,
118 : wxFrame(parent
, id
, title
, pos
, size
,
119 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
121 SetIcon(wxICON(sample
));
124 wxMenu
*file_menu
= new wxMenu
;
127 file_menu
->Append(wxID_OPEN
, wxT("&Open Animation...\tCtrl+O"), wxT("Loads an animation"));
128 #endif // wxUSE_FILEDLG
129 file_menu
->Append(wxID_EXIT
);
131 wxMenu
*play_menu
= new wxMenu
;
132 play_menu
->Append(ID_PLAY
, wxT("Play\tCtrl+P"), wxT("Play the animation"));
133 play_menu
->Append(wxID_STOP
, wxT("Stop\tCtrl+S"), wxT("Stop the animation"));
134 play_menu
->AppendSeparator();
135 play_menu
->Append(ID_SET_NULL_ANIMATION
, wxT("Set null animation"),
136 wxT("Sets the empty animation in the control"));
137 play_menu
->AppendCheckItem(ID_SET_INACTIVE_BITMAP
, wxT("Set inactive bitmap"),
138 wxT("Sets an inactive bitmap for the control"));
139 play_menu
->AppendCheckItem(ID_SET_NO_AUTO_RESIZE
, wxT("Set no autoresize"),
140 wxT("Tells the control not to resize automatically"));
141 play_menu
->Append(ID_SET_BGCOLOR
, wxT("Set background colour..."),
142 wxT("Sets the background colour of the control"));
144 wxMenu
*help_menu
= new wxMenu
;
145 help_menu
->Append(wxID_ABOUT
);
147 wxMenuBar
*menu_bar
= new wxMenuBar
;
149 menu_bar
->Append(file_menu
, wxT("&File"));
150 menu_bar
->Append(play_menu
, wxT("&Animation"));
151 menu_bar
->Append(help_menu
, wxT("&Help"));
153 // Associate the menu bar with this frame
154 SetMenuBar(menu_bar
);
158 #endif // wxUSE_STATUSBAR
160 // use a wxBoxSizer otherwise wxFrame will automatically
161 // resize the m_animationCtrl to fill its client area on
163 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
164 sz
->Add(new wxStaticText(this, wxID_ANY
, wxT("wxAnimationCtrl:")),
165 wxSizerFlags().Centre().Border());
167 m_animationCtrl
= new wxAnimationCtrl(this, wxID_ANY
);
168 if (m_animationCtrl
->LoadFile(wxT("throbber.gif")))
169 m_animationCtrl
->Play();
171 sz
->Add(m_animationCtrl
, wxSizerFlags().Centre().Border());
179 void MyFrame::OnPlay(wxCommandEvent
& WXUNUSED(event
))
181 if (!m_animationCtrl
->Play())
183 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(wxT("(C) 2006 Julian Smart"));
260 info
.AddDeveloper(wxT("Julian Smart"));
261 info
.AddDeveloper(wxT("Guillermo Rodriguez Garcia"));
262 info
.AddDeveloper(wxT("Francesco Montorsi"));
268 void MyFrame::OnOpen(wxCommandEvent
& WXUNUSED(event
))
270 wxFileDialog
dialog(this, wxT("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(wxT("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());