]> git.saurik.com Git - wxWidgets.git/blob - samples/animate/anitest.cpp
added tests for background colour, static bitmap &c (part of patch 1598005)
[wxWidgets.git] / samples / animate / anitest.cpp
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__
32 #include "sample.xpm"
33 #endif
34
35 #include "wx/aboutdlg.h"
36 #include "wx/artprov.h"
37 #include "wx/colordlg.h"
38 #include "anitest.h"
39
40 #if !wxUSE_ANIMATIONCTRL
41 #error Cannot compile this sample if wxAnimationCtrl is not enabled
42 #endif
43
44
45 IMPLEMENT_APP(MyApp)
46
47 // ---------------------------------------------------------------------------
48 // global variables
49 // ---------------------------------------------------------------------------
50
51 // ---------------------------------------------------------------------------
52 // event tables
53 // ---------------------------------------------------------------------------
54
55 enum
56 {
57 ID_PLAY = 1,
58 ID_SET_NULL_ANIMATION,
59 ID_SET_INACTIVE_BITMAP,
60 ID_SET_NO_AUTO_RESIZE,
61 ID_SET_BGCOLOR
62 };
63
64 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
65 EVT_MENU(ID_PLAY, MyFrame::OnPlay)
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
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 {
93 // Create the main frame window
94
95 MyFrame* frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("Animation Demo"),
96 wxDefaultPosition, wxSize(500, 400),
97 wxDEFAULT_FRAME_STYLE);
98
99 // Give it an icon
100 frame->SetIcon(wxICON(sample));
101
102 // Make a menubar
103 wxMenu *file_menu = new wxMenu;
104
105 #if wxUSE_FILEDLG
106 file_menu->Append(wxID_OPEN, _T("&Open Animation...\tCtrl+O"), _T("Loads an animation"));
107 #endif // wxUSE_FILEDLG
108 file_menu->Append(wxID_EXIT);
109
110 wxMenu *play_menu = new wxMenu;
111 play_menu->Append(ID_PLAY, _T("Play\tCtrl+P"), _T("Play the animation"));
112 play_menu->Append(wxID_STOP, _T("Stop\tCtrl+P"), _T("Stop the animation"));
113 play_menu->AppendSeparator();
114 play_menu->Append(ID_SET_NULL_ANIMATION, _T("Set null animation"),
115 _T("Sets the empty animation in the control"));
116 play_menu->AppendCheckItem(ID_SET_INACTIVE_BITMAP, _T("Set inactive bitmap"),
117 _T("Sets an inactive bitmap for the control"));
118 play_menu->AppendCheckItem(ID_SET_NO_AUTO_RESIZE, _T("Set no autoresize"),
119 _T("Tells the control not to resize automatically"));
120 play_menu->Append(ID_SET_BGCOLOR, _T("Set background colour..."),
121 _T("Sets the background colour of the control"));
122
123 wxMenu *help_menu = new wxMenu;
124 help_menu->Append(wxID_ABOUT);
125
126 wxMenuBar *menu_bar = new wxMenuBar;
127
128 menu_bar->Append(file_menu, _T("&File"));
129 menu_bar->Append(play_menu, _T("&Animation"));
130 menu_bar->Append(help_menu, _T("&Help"));
131
132 // Associate the menu bar with the frame
133 frame->SetMenuBar(menu_bar);
134
135 #if wxUSE_STATUSBAR
136 frame->CreateStatusBar();
137 #endif // wxUSE_STATUSBAR
138
139 frame->Show(true);
140
141 SetTopWindow(frame);
142
143 return true;
144 }
145
146 // ---------------------------------------------------------------------------
147 // MyFrame
148 // ---------------------------------------------------------------------------
149
150 #include "wx/wfstream.h"
151
152 // Define my frame constructor
153 MyFrame::MyFrame(wxWindow *parent,
154 const wxWindowID id,
155 const wxString& title,
156 const wxPoint& pos,
157 const wxSize& size,
158 const long style)
159 : wxFrame(parent, id, title, pos, size,
160 style | wxNO_FULL_REPAINT_ON_RESIZE)
161 {
162 // use a wxBoxSizer otherwise wxFrame will automatically
163 // resize the m_animationCtrl to fill its client area on
164 // user resizes
165 wxSizer *sz = new wxBoxSizer(wxVERTICAL);
166 sz->Add(new wxStaticText(this, wxID_ANY, wxT("wxAnimationCtrl:")),
167 wxSizerFlags().Centre().Border());
168
169 m_animationCtrl = new wxAnimationCtrl(this, wxID_ANY);
170 if (m_animationCtrl->LoadFile(wxT("throbber.gif")))
171 m_animationCtrl->Play();
172
173 sz->Add(m_animationCtrl, wxSizerFlags().Centre().Border());
174 SetSizer(sz);
175 }
176
177 MyFrame::~MyFrame()
178 {
179 }
180
181 void MyFrame::OnPlay(wxCommandEvent& WXUNUSED(event))
182 {
183 if (!m_animationCtrl->Play())
184 wxLogError(wxT("Invalid animation"));
185 }
186
187 void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
188 {
189 m_animationCtrl->Stop();
190 }
191
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
248 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
249 {
250 Close();
251 }
252
253 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
254 {
255 wxAboutDialogInfo info;
256 info.SetName(_("wxAnimationCtrl and wxAnimation sample"));
257 info.SetDescription(_("This sample program demonstrates the usage of wxAnimationCtrl"));
258 info.SetCopyright(_T("(C) 2006 Julian Smart"));
259
260 info.AddDeveloper(_T("Julian Smart"));
261 info.AddDeveloper(_T("Guillermo Rodriguez Garcia"));
262 info.AddDeveloper(_T("Francesco Montorsi"));
263
264 wxAboutBox(info);
265 }
266
267 #if wxUSE_FILEDLG
268 void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(event))
269 {
270 wxFileDialog dialog(this, _T("Please choose an animation"),
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
281 wxMessageBox(_T("Sorry, this animation is not a valid format for wxAnimation."));
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);
295 if (!stream.Ok())
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
312
313 GetSizer()->Layout();
314 }
315 }
316 #endif // wxUSE_FILEDLG
317
318 void MyFrame::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event) )
319 {
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());
323 }
324