]>
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" | |
72045d57 VZ |
38 | #include "anitest.h" |
39 | ||
68aef14d VZ |
40 | #if !wxUSE_ANIMATIONCTRL |
41 | #error Cannot compile this sample if wxAnimationCtrl is not enabled | |
42 | #endif | |
43 | ||
44 | ||
72045d57 VZ |
45 | IMPLEMENT_APP(MyApp) |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | // global variables | |
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | // event tables | |
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | enum | |
56 | { | |
cf46511d VZ |
57 | ID_PLAY = 1, |
58 | ID_SET_NULL_ANIMATION, | |
59 | ID_SET_INACTIVE_BITMAP, | |
60 | ID_SET_NO_AUTO_RESIZE, | |
61 | ID_SET_BGCOLOR | |
72045d57 VZ |
62 | }; |
63 | ||
64 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
c3d6e0aa | 65 | EVT_MENU(ID_PLAY, MyFrame::OnPlay) |
cf46511d VZ |
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) | |
70 | ||
72045d57 VZ |
71 | EVT_MENU(wxID_STOP, MyFrame::OnStop) |
72 | EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) | |
73 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
74 | #if wxUSE_FILEDLG | |
75 | EVT_MENU(wxID_OPEN, MyFrame::OnOpen) | |
76 | #endif // wxUSE_FILEDLG | |
77 | ||
78 | EVT_SIZE(MyFrame::OnSize) | |
79 | EVT_UPDATE_UI(wxID_ANY, MyFrame::OnUpdateUI) | |
80 | END_EVENT_TABLE() | |
81 | ||
82 | // =========================================================================== | |
83 | // implementation | |
84 | // =========================================================================== | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | // MyApp | |
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | // Initialise this in OnInit, not statically | |
91 | bool MyApp::OnInit() | |
92 | { | |
45e6e6f8 VZ |
93 | if ( !wxApp::OnInit() ) |
94 | return false; | |
95 | ||
72045d57 VZ |
96 | // Create the main frame window |
97 | ||
3f4a2351 | 98 | MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("Animation Demo"), |
cf46511d VZ |
99 | wxDefaultPosition, wxSize(500, 400), |
100 | wxDEFAULT_FRAME_STYLE); | |
72045d57 VZ |
101 | |
102 | // Give it an icon | |
73565531 | 103 | frame->SetIcon(wxICON(sample)); |
72045d57 VZ |
104 | |
105 | // Make a menubar | |
106 | wxMenu *file_menu = new wxMenu; | |
107 | ||
108 | #if wxUSE_FILEDLG | |
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); | |
112 | ||
113 | wxMenu *play_menu = new wxMenu; | |
c3d6e0aa | 114 | play_menu->Append(ID_PLAY, _T("Play\tCtrl+P"), _T("Play the animation")); |
72045d57 | 115 | play_menu->Append(wxID_STOP, _T("Stop\tCtrl+P"), _T("Stop the animation")); |
cf46511d VZ |
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")); | |
72045d57 VZ |
125 | |
126 | wxMenu *help_menu = new wxMenu; | |
127 | help_menu->Append(wxID_ABOUT); | |
128 | ||
129 | wxMenuBar *menu_bar = new wxMenuBar; | |
130 | ||
131 | menu_bar->Append(file_menu, _T("&File")); | |
132 | menu_bar->Append(play_menu, _T("&Animation")); | |
133 | menu_bar->Append(help_menu, _T("&Help")); | |
134 | ||
135 | // Associate the menu bar with the frame | |
136 | frame->SetMenuBar(menu_bar); | |
137 | ||
138 | #if wxUSE_STATUSBAR | |
139 | frame->CreateStatusBar(); | |
140 | #endif // wxUSE_STATUSBAR | |
141 | ||
142 | frame->Show(true); | |
143 | ||
144 | SetTopWindow(frame); | |
145 | ||
146 | return true; | |
147 | } | |
148 | ||
149 | // --------------------------------------------------------------------------- | |
150 | // MyFrame | |
151 | // --------------------------------------------------------------------------- | |
152 | ||
153 | #include "wx/wfstream.h" | |
154 | ||
155 | // Define my frame constructor | |
156 | MyFrame::MyFrame(wxWindow *parent, | |
157 | const wxWindowID id, | |
158 | const wxString& title, | |
159 | const wxPoint& pos, | |
160 | const wxSize& size, | |
161 | const long style) | |
162 | : wxFrame(parent, id, title, pos, size, | |
163 | style | wxNO_FULL_REPAINT_ON_RESIZE) | |
164 | { | |
cf46511d VZ |
165 | // use a wxBoxSizer otherwise wxFrame will automatically |
166 | // resize the m_animationCtrl to fill its client area on | |
167 | // user resizes | |
168 | wxSizer *sz = new wxBoxSizer(wxVERTICAL); | |
169 | sz->Add(new wxStaticText(this, wxID_ANY, wxT("wxAnimationCtrl:")), | |
170 | wxSizerFlags().Centre().Border()); | |
171 | ||
172 | m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY); | |
72045d57 VZ |
173 | if (m_animationCtrl->LoadFile(wxT("throbber.gif"))) |
174 | m_animationCtrl->Play(); | |
cf46511d VZ |
175 | |
176 | sz->Add(m_animationCtrl, wxSizerFlags().Centre().Border()); | |
177 | SetSizer(sz); | |
72045d57 VZ |
178 | } |
179 | ||
180 | MyFrame::~MyFrame() | |
181 | { | |
182 | } | |
183 | ||
184 | void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event)) | |
185 | { | |
186 | if (!m_animationCtrl->Play()) | |
187 | wxLogError(wxT("Invalid animation")); | |
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")); | |
261 | info.SetCopyright(_T("(C) 2006 Julian Smart")); | |
72045d57 | 262 | |
68aef14d VZ |
263 | info.AddDeveloper(_T("Julian Smart")); |
264 | info.AddDeveloper(_T("Guillermo Rodriguez Garcia")); | |
265 | info.AddDeveloper(_T("Francesco Montorsi")); | |
72045d57 | 266 | |
68aef14d | 267 | wxAboutBox(info); |
72045d57 VZ |
268 | } |
269 | ||
270 | #if wxUSE_FILEDLG | |
271 | void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event)) | |
272 | { | |
273 | wxFileDialog dialog(this, _T("Please choose an animation"), | |
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 | |
284 | wxMessageBox(_T("Sorry, this animation is not a valid format for wxAnimation.")); | |
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 |