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