]> git.saurik.com Git - wxWidgets.git/blame - samples/animate/anitest.cpp
Don't use printf() with non-literal string as format.
[wxWidgets.git] / samples / animate / anitest.cpp
CommitLineData
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
e7092398 31#ifndef wxHAS_IMAGES_IN_RESOURCES
3cb332c1 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
47IMPLEMENT_APP(MyApp)
48
49// ---------------------------------------------------------------------------
50// global variables
51// ---------------------------------------------------------------------------
52
53// ---------------------------------------------------------------------------
54// event tables
55// ---------------------------------------------------------------------------
56
57enum
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
66BEGIN_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)
82END_EVENT_TABLE()
83
84// ===========================================================================
85// implementation
86// ===========================================================================
87
88// ---------------------------------------------------------------------------
89// MyApp
90// ---------------------------------------------------------------------------
91
92// Initialise this in OnInit, not statically
93bool 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
faf224da
FM
105 return true;
106}
72045d57 107
faf224da
FM
108// ---------------------------------------------------------------------------
109// MyFrame
110// ---------------------------------------------------------------------------
111
faf224da
FM
112// Define my frame constructor
113MyFrame::MyFrame(wxWindow *parent,
114 const wxWindowID id,
115 const wxString& title,
116 const wxPoint& pos,
117 const wxSize& size,
118 const long style)
119 : wxFrame(parent, id, title, pos, size,
120 style | wxNO_FULL_REPAINT_ON_RESIZE)
121{
122 SetIcon(wxICON(sample));
72045d57
VZ
123
124 // Make a menubar
125 wxMenu *file_menu = new wxMenu;
126
127#if wxUSE_FILEDLG
9a83f860 128 file_menu->Append(wxID_OPEN, wxT("&Open Animation...\tCtrl+O"), wxT("Loads an animation"));
72045d57
VZ
129#endif // wxUSE_FILEDLG
130 file_menu->Append(wxID_EXIT);
131
132 wxMenu *play_menu = new wxMenu;
9a83f860 133 play_menu->Append(ID_PLAY, wxT("Play\tCtrl+P"), wxT("Play the animation"));
efe2190f 134 play_menu->Append(wxID_STOP, wxT("Stop\tCtrl+S"), wxT("Stop the animation"));
cf46511d 135 play_menu->AppendSeparator();
9a83f860
VZ
136 play_menu->Append(ID_SET_NULL_ANIMATION, wxT("Set null animation"),
137 wxT("Sets the empty animation in the control"));
138 play_menu->AppendCheckItem(ID_SET_INACTIVE_BITMAP, wxT("Set inactive bitmap"),
139 wxT("Sets an inactive bitmap for the control"));
140 play_menu->AppendCheckItem(ID_SET_NO_AUTO_RESIZE, wxT("Set no autoresize"),
141 wxT("Tells the control not to resize automatically"));
142 play_menu->Append(ID_SET_BGCOLOR, wxT("Set background colour..."),
143 wxT("Sets the background colour of the control"));
72045d57
VZ
144
145 wxMenu *help_menu = new wxMenu;
146 help_menu->Append(wxID_ABOUT);
147
148 wxMenuBar *menu_bar = new wxMenuBar;
149
9a83f860
VZ
150 menu_bar->Append(file_menu, wxT("&File"));
151 menu_bar->Append(play_menu, wxT("&Animation"));
152 menu_bar->Append(help_menu, wxT("&Help"));
72045d57 153
faf224da
FM
154 // Associate the menu bar with this frame
155 SetMenuBar(menu_bar);
72045d57
VZ
156
157#if wxUSE_STATUSBAR
faf224da 158 CreateStatusBar();
72045d57
VZ
159#endif // wxUSE_STATUSBAR
160
cf46511d
VZ
161 // use a wxBoxSizer otherwise wxFrame will automatically
162 // resize the m_animationCtrl to fill its client area on
163 // user resizes
164 wxSizer *sz = new wxBoxSizer(wxVERTICAL);
165 sz->Add(new wxStaticText(this, wxID_ANY, wxT("wxAnimationCtrl:")),
166 wxSizerFlags().Centre().Border());
167
168 m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY);
72045d57
VZ
169 if (m_animationCtrl->LoadFile(wxT("throbber.gif")))
170 m_animationCtrl->Play();
cf46511d
VZ
171
172 sz->Add(m_animationCtrl, wxSizerFlags().Centre().Border());
173 SetSizer(sz);
72045d57
VZ
174}
175
176MyFrame::~MyFrame()
177{
178}
179
180void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
181{
182 if (!m_animationCtrl->Play())
43b2d5e7 183 {
72045d57 184 wxLogError(wxT("Invalid animation"));
43b2d5e7 185 }
72045d57
VZ
186}
187
188void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
189{
190 m_animationCtrl->Stop();
191}
192
cf46511d
VZ
193void MyFrame::OnSetNullAnimation(wxCommandEvent& WXUNUSED(event))
194{
195 m_animationCtrl->SetAnimation(wxNullAnimation);
196}
197
198void MyFrame::OnSetInactiveBitmap(wxCommandEvent& event)
199{
200 if (event.IsChecked())
201 {
202 // set a dummy bitmap as the inactive bitmap
203 wxBitmap bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE);
204 m_animationCtrl->SetInactiveBitmap(bmp);
205 }
206 else
207 m_animationCtrl->SetInactiveBitmap(wxNullBitmap);
208}
209
210void MyFrame::OnSetNoAutoResize(wxCommandEvent& event)
211{
212 // recreate the control with the new flag if necessary
213 long style = wxAC_DEFAULT_STYLE |
214 (event.IsChecked() ? wxAC_NO_AUTORESIZE : 0);
215
216 if (style != m_animationCtrl->GetWindowStyle())
217 {
218 // save status of the control before destroying it
219 wxAnimation curr = m_animationCtrl->GetAnimation();
220 wxBitmap inactive = m_animationCtrl->GetInactiveBitmap();
221 wxColour bg = m_animationCtrl->GetBackgroundColour();
222
223 // destroy & rebuild
224 wxAnimationCtrl *old = m_animationCtrl;
225 m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY, curr,
226 wxDefaultPosition, wxDefaultSize,
227 style);
228
229 GetSizer()->Replace(old, m_animationCtrl);
230 delete old;
231
232 // load old status in new control
233 m_animationCtrl->SetInactiveBitmap(inactive);
234 m_animationCtrl->SetBackgroundColour(bg);
235
236 GetSizer()->Layout();
237 }
238}
239
240void MyFrame::OnSetBgColor(wxCommandEvent& WXUNUSED(event))
241{
242 wxColour clr = wxGetColourFromUser(this, m_animationCtrl->GetBackgroundColour(),
243 wxT("Choose the background colour"));
244
245 if (clr.IsOk())
246 m_animationCtrl->SetBackgroundColour(clr);
247}
248
72045d57
VZ
249void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
250{
251 Close();
252}
253
254void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
255{
68aef14d
VZ
256 wxAboutDialogInfo info;
257 info.SetName(_("wxAnimationCtrl and wxAnimation sample"));
258 info.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
9a83f860 259 info.SetCopyright(wxT("(C) 2006 Julian Smart"));
72045d57 260
9a83f860
VZ
261 info.AddDeveloper(wxT("Julian Smart"));
262 info.AddDeveloper(wxT("Guillermo Rodriguez Garcia"));
263 info.AddDeveloper(wxT("Francesco Montorsi"));
72045d57 264
68aef14d 265 wxAboutBox(info);
72045d57
VZ
266}
267
268#if wxUSE_FILEDLG
269void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
270{
9a83f860 271 wxFileDialog dialog(this, wxT("Please choose an animation"),
72045d57
VZ
272 wxEmptyString, wxEmptyString, wxT("*.gif;*.ani"), wxFD_OPEN);
273 if (dialog.ShowModal() == wxID_OK)
274 {
275 wxString filename(dialog.GetPath());
276
277 // enable one of the two chunk of codes to test different parts of wxAnimation/wxAnimationCtrl
278#if 0
279 if (m_animationCtrl->LoadFile(filename))
280 m_animationCtrl->Play();
281 else
9a83f860 282 wxMessageBox(wxT("Sorry, this animation is not a valid format for wxAnimation."));
72045d57
VZ
283#else
284 #if 0
285 wxAnimation temp;
286 if (!temp.LoadFile(filename))
287 {
288 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
289 return;
290 }
291
292 m_animationCtrl->SetAnimation(temp);
293 m_animationCtrl->Play();
294 #else
295 wxFileInputStream stream(filename);
a1b806b9 296 if (!stream.IsOk())
72045d57
VZ
297 {
298 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
299 return;
300 }
301
302 wxAnimation temp;
303 if (!temp.Load(stream))
304 {
305 wxLogError(wxT("Sorry, this animation is not a valid format for wxAnimation."));
306 return;
307 }
308
309 m_animationCtrl->SetAnimation(temp);
310 m_animationCtrl->Play();
311 #endif
312#endif
cf46511d
VZ
313
314 GetSizer()->Layout();
72045d57
VZ
315 }
316}
317#endif // wxUSE_FILEDLG
318
319void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) )
320{
321 GetMenuBar()->FindItem(wxID_STOP)->Enable(m_animationCtrl->IsPlaying());
c3d6e0aa 322 GetMenuBar()->FindItem(ID_PLAY)->Enable(!m_animationCtrl->IsPlaying());
cf46511d 323 GetMenuBar()->FindItem(ID_SET_NO_AUTO_RESIZE)->Enable(!m_animationCtrl->IsPlaying());
72045d57
VZ
324}
325