]>
Commit | Line | Data |
---|---|---|
72045d57 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: anitest.cpp | |
3 | // Purpose: Animation sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: Francesco Montorsi | |
6 | // Created: 02/07/2001 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #ifndef WX_PRECOMP | |
28 | #include "wx/wx.h" | |
29 | #endif | |
30 | ||
31 | #ifndef __WXMSW__ | |
3652fd78 | 32 | #include "sample.xpm" |
72045d57 VZ |
33 | #endif |
34 | ||
68aef14d | 35 | #include "wx/aboutdlg.h" |
cf46511d VZ |
36 | #include "wx/artprov.h" |
37 | #include "wx/colordlg.h" | |
8b93348e FM |
38 | #include "wx/wfstream.h" |
39 | ||
72045d57 VZ |
40 | #include "anitest.h" |
41 | ||
68aef14d VZ |
42 | #if !wxUSE_ANIMATIONCTRL |
43 | #error Cannot compile this sample if wxAnimationCtrl is not enabled | |
44 | #endif | |
45 | ||
46 | ||
72045d57 VZ |
47 | IMPLEMENT_APP(MyApp) |
48 | ||
49 | // --------------------------------------------------------------------------- | |
50 | // global variables | |
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | // event tables | |
55 | // --------------------------------------------------------------------------- | |
56 | ||
57 | enum | |
58 | { | |
cf46511d VZ |
59 | ID_PLAY = 1, |
60 | ID_SET_NULL_ANIMATION, | |
61 | ID_SET_INACTIVE_BITMAP, | |
62 | ID_SET_NO_AUTO_RESIZE, | |
63 | ID_SET_BGCOLOR | |
72045d57 VZ |
64 | }; |
65 | ||
66 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
c3d6e0aa | 67 | EVT_MENU(ID_PLAY, MyFrame::OnPlay) |
cf46511d VZ |
68 | EVT_MENU(ID_SET_NULL_ANIMATION, MyFrame::OnSetNullAnimation) |
69 | EVT_MENU(ID_SET_INACTIVE_BITMAP, MyFrame::OnSetInactiveBitmap) | |
70 | EVT_MENU(ID_SET_NO_AUTO_RESIZE, MyFrame::OnSetNoAutoResize) | |
71 | EVT_MENU(ID_SET_BGCOLOR, MyFrame::OnSetBgColor) | |
72 | ||
72045d57 VZ |
73 | EVT_MENU(wxID_STOP, MyFrame::OnStop) |
74 | EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) | |
75 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
76 | #if wxUSE_FILEDLG | |
77 | EVT_MENU(wxID_OPEN, MyFrame::OnOpen) | |
78 | #endif // wxUSE_FILEDLG | |
79 | ||
80 | EVT_SIZE(MyFrame::OnSize) | |
81 | EVT_UPDATE_UI(wxID_ANY, MyFrame::OnUpdateUI) | |
82 | END_EVENT_TABLE() | |
83 | ||
84 | // =========================================================================== | |
85 | // implementation | |
86 | // =========================================================================== | |
87 | ||
88 | // --------------------------------------------------------------------------- | |
89 | // MyApp | |
90 | // --------------------------------------------------------------------------- | |
91 | ||
92 | // Initialise this in OnInit, not statically | |
93 | bool MyApp::OnInit() | |
94 | { | |
45e6e6f8 VZ |
95 | if ( !wxApp::OnInit() ) |
96 | return false; | |
97 | ||
72045d57 VZ |
98 | // Create the main frame window |
99 | ||
9a83f860 | 100 | MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, wxT("Animation Demo"), |
cf46511d VZ |
101 | wxDefaultPosition, wxSize(500, 400), |
102 | wxDEFAULT_FRAME_STYLE); | |
faf224da FM |
103 | frame->Show(true); |
104 | ||
105 | SetTopWindow(frame); | |
106 | ||
107 | return true; | |
108 | } | |
72045d57 | 109 | |
faf224da FM |
110 | // --------------------------------------------------------------------------- |
111 | // MyFrame | |
112 | // --------------------------------------------------------------------------- | |
113 | ||
faf224da FM |
114 | // Define my frame constructor |
115 | MyFrame::MyFrame(wxWindow *parent, | |
116 | const wxWindowID id, | |
117 | const wxString& title, | |
118 | const wxPoint& pos, | |
119 | const wxSize& size, | |
120 | const long style) | |
121 | : wxFrame(parent, id, title, pos, size, | |
122 | style | wxNO_FULL_REPAINT_ON_RESIZE) | |
123 | { | |
124 | SetIcon(wxICON(sample)); | |
72045d57 VZ |
125 | |
126 | // Make a menubar | |
127 | wxMenu *file_menu = new wxMenu; | |
128 | ||
129 | #if wxUSE_FILEDLG | |
9a83f860 | 130 | file_menu->Append(wxID_OPEN, wxT("&Open Animation...\tCtrl+O"), wxT("Loads an animation")); |
72045d57 VZ |
131 | #endif // wxUSE_FILEDLG |
132 | file_menu->Append(wxID_EXIT); | |
133 | ||
134 | wxMenu *play_menu = new wxMenu; | |
9a83f860 | 135 | play_menu->Append(ID_PLAY, wxT("Play\tCtrl+P"), wxT("Play the animation")); |
efe2190f | 136 | play_menu->Append(wxID_STOP, wxT("Stop\tCtrl+S"), wxT("Stop the animation")); |
cf46511d | 137 | play_menu->AppendSeparator(); |
9a83f860 VZ |
138 | play_menu->Append(ID_SET_NULL_ANIMATION, wxT("Set null animation"), |
139 | wxT("Sets the empty animation in the control")); | |
140 | play_menu->AppendCheckItem(ID_SET_INACTIVE_BITMAP, wxT("Set inactive bitmap"), | |
141 | wxT("Sets an inactive bitmap for the control")); | |
142 | play_menu->AppendCheckItem(ID_SET_NO_AUTO_RESIZE, wxT("Set no autoresize"), | |
143 | wxT("Tells the control not to resize automatically")); | |
144 | play_menu->Append(ID_SET_BGCOLOR, wxT("Set background colour..."), | |
145 | wxT("Sets the background colour of the control")); | |
72045d57 VZ |
146 | |
147 | wxMenu *help_menu = new wxMenu; | |
148 | help_menu->Append(wxID_ABOUT); | |
149 | ||
150 | wxMenuBar *menu_bar = new wxMenuBar; | |
151 | ||
9a83f860 VZ |
152 | menu_bar->Append(file_menu, wxT("&File")); |
153 | menu_bar->Append(play_menu, wxT("&Animation")); | |
154 | menu_bar->Append(help_menu, wxT("&Help")); | |
72045d57 | 155 | |
faf224da FM |
156 | // Associate the menu bar with this frame |
157 | SetMenuBar(menu_bar); | |
72045d57 VZ |
158 | |
159 | #if wxUSE_STATUSBAR | |
faf224da | 160 | CreateStatusBar(); |
72045d57 VZ |
161 | #endif // wxUSE_STATUSBAR |
162 | ||
cf46511d VZ |
163 | // use a wxBoxSizer otherwise wxFrame will automatically |
164 | // resize the m_animationCtrl to fill its client area on | |
165 | // user resizes | |
166 | wxSizer *sz = new wxBoxSizer(wxVERTICAL); | |
167 | sz->Add(new wxStaticText(this, wxID_ANY, wxT("wxAnimationCtrl:")), | |
168 | wxSizerFlags().Centre().Border()); | |
169 | ||
170 | m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY); | |
72045d57 VZ |
171 | if (m_animationCtrl->LoadFile(wxT("throbber.gif"))) |
172 | m_animationCtrl->Play(); | |
cf46511d VZ |
173 | |
174 | sz->Add(m_animationCtrl, wxSizerFlags().Centre().Border()); | |
175 | SetSizer(sz); | |
72045d57 VZ |
176 | } |
177 | ||
178 | MyFrame::~MyFrame() | |
179 | { | |
180 | } | |
181 | ||
182 | void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event)) | |
183 | { | |
184 | if (!m_animationCtrl->Play()) | |
43b2d5e7 | 185 | { |
72045d57 | 186 | wxLogError(wxT("Invalid animation")); |
43b2d5e7 | 187 | } |
72045d57 VZ |
188 | } |
189 | ||
190 | void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event)) | |
191 | { | |
192 | m_animationCtrl->Stop(); | |
193 | } | |
194 | ||
cf46511d VZ |
195 | void MyFrame::OnSetNullAnimation(wxCommandEvent& WXUNUSED(event)) |
196 | { | |
197 | m_animationCtrl->SetAnimation(wxNullAnimation); | |
198 | } | |
199 | ||
200 | void MyFrame::OnSetInactiveBitmap(wxCommandEvent& event) | |
201 | { | |
202 | if (event.IsChecked()) | |
203 | { | |
204 | // set a dummy bitmap as the inactive bitmap | |
205 | wxBitmap bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE); | |
206 | m_animationCtrl->SetInactiveBitmap(bmp); | |
207 | } | |
208 | else | |
209 | m_animationCtrl->SetInactiveBitmap(wxNullBitmap); | |
210 | } | |
211 | ||
212 | void MyFrame::OnSetNoAutoResize(wxCommandEvent& event) | |
213 | { | |
214 | // recreate the control with the new flag if necessary | |
215 | long style = wxAC_DEFAULT_STYLE | | |
216 | (event.IsChecked() ? wxAC_NO_AUTORESIZE : 0); | |
217 | ||
218 | if (style != m_animationCtrl->GetWindowStyle()) | |
219 | { | |
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(); | |
224 | ||
225 | // destroy & rebuild | |
226 | wxAnimationCtrl *old = m_animationCtrl; | |
227 | m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, curr, | |
228 | wxDefaultPosition, wxDefaultSize, | |
229 | style); | |
230 | ||
231 | GetSizer()->Replace(old, m_animationCtrl); | |
232 | delete old; | |
233 | ||
234 | // load old status in new control | |
235 | m_animationCtrl->SetInactiveBitmap(inactive); | |
236 | m_animationCtrl->SetBackgroundColour(bg); | |
237 | ||
238 | GetSizer()->Layout(); | |
239 | } | |
240 | } | |
241 | ||
242 | void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event)) | |
243 | { | |
244 | wxColour clr = wxGetColourFromUser(this, m_animationCtrl->GetBackgroundColour(), | |
245 | wxT("Choose the background colour")); | |
246 | ||
247 | if (clr.IsOk()) | |
248 | m_animationCtrl->SetBackgroundColour(clr); | |
249 | } | |
250 | ||
72045d57 VZ |
251 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
252 | { | |
253 | Close(); | |
254 | } | |
255 | ||
256 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
257 | { | |
68aef14d VZ |
258 | wxAboutDialogInfo info; |
259 | info.SetName(_("wxAnimationCtrl and wxAnimation sample")); | |
260 | info.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl")); | |
9a83f860 | 261 | info.SetCopyright(wxT("(C) 2006 Julian Smart")); |
72045d57 | 262 | |
9a83f860 VZ |
263 | info.AddDeveloper(wxT("Julian Smart")); |
264 | info.AddDeveloper(wxT("Guillermo Rodriguez Garcia")); | |
265 | info.AddDeveloper(wxT("Francesco Montorsi")); | |
72045d57 | 266 | |
68aef14d | 267 | wxAboutBox(info); |
72045d57 VZ |
268 | } |
269 | ||
270 | #if wxUSE_FILEDLG | |
271 | void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) | |
272 | { | |
9a83f860 | 273 | wxFileDialog dialog(this, wxT("Please choose an animation"), |
72045d57 VZ |
274 | wxEmptyString, wxEmptyString, wxT("*.gif;*.ani"), wxFD_OPEN); |
275 | if (dialog.ShowModal() == wxID_OK) | |
276 | { | |
277 | wxString filename(dialog.GetPath()); | |
278 | ||
279 | // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl | |
280 | #if 0 | |
281 | if (m_animationCtrl->LoadFile(filename)) | |
282 | m_animationCtrl->Play(); | |
283 | else | |
9a83f860 | 284 | wxMessageBox(wxT("Sorry, this animation is not a valid format for wxAnimation.")); |
72045d57 VZ |
285 | #else |
286 | #if 0 | |
287 | wxAnimation temp; | |
288 | if (!temp.LoadFile(filename)) | |
289 | { | |
290 | wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation.")); | |
291 | return; | |
292 | } | |
293 | ||
294 | m_animationCtrl->SetAnimation(temp); | |
295 | m_animationCtrl->Play(); | |
296 | #else | |
297 | wxFileInputStream stream(filename); | |
298 | if (!stream.Ok()) | |
299 | { | |
300 | wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation.")); | |
301 | return; | |
302 | } | |
303 | ||
304 | wxAnimation temp; | |
305 | if (!temp.Load(stream)) | |
306 | { | |
307 | wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation.")); | |
308 | return; | |
309 | } | |
310 | ||
311 | m_animationCtrl->SetAnimation(temp); | |
312 | m_animationCtrl->Play(); | |
313 | #endif | |
314 | #endif | |
cf46511d VZ |
315 | |
316 | GetSizer()->Layout(); | |
72045d57 VZ |
317 | } |
318 | } | |
319 | #endif // wxUSE_FILEDLG | |
320 | ||
321 | void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) ) | |
322 | { | |
323 | GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying()); | |
c3d6e0aa | 324 | GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying()); |
cf46511d | 325 | GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE)->Enable(!m_animationCtrl->IsPlaying()); |
72045d57 VZ |
326 | } |
327 |