wxAnimationCtrl cleanup
[wxWidgets.git] / src / gtk / animate.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/animate.cpp
3 // Purpose: wxAnimation and wxAnimationCtrl
4 // Author: Francesco Montorsi
5 // Modified By:
6 // Created: 24/09/2006
7 // Id: $Id$
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_ANIMATIONCTRL
16
17 #include "wx/animate.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/image.h"
21 #include "wx/log.h"
22 #include "wx/stream.h"
23 #endif
24
25 #include <gtk/gtk.h>
26
27
28 // ============================================================================
29 // implementation
30 // ============================================================================
31
32 void gdk_pixbuf_area_updated(GdkPixbufLoader *loader,
33 gint x,
34 gint y,
35 gint width,
36 gint height,
37 wxAnimation *anim)
38 {
39 if (anim && anim->GetPixbuf() == NULL)
40 {
41 // we need to set the pixbuf only if this is the first time this signal
42 // has been called!
43 anim->SetPixbuf(gdk_pixbuf_loader_get_animation(loader));
44 }
45 }
46
47
48 //-----------------------------------------------------------------------------
49 // wxAnimation
50 //-----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxAnimation, wxAnimationBase)
53
54 wxAnimation::wxAnimation(const wxAnimation& that)
55 : base_type(that)
56 {
57 m_pixbuf = that.m_pixbuf;
58 if (m_pixbuf)
59 g_object_ref(m_pixbuf);
60 }
61
62 wxAnimation& wxAnimation::operator=(const wxAnimation& that)
63 {
64 if (this != &that)
65 {
66 base_type::operator=(that);
67 UnRef();
68 m_pixbuf = that.m_pixbuf;
69 if (m_pixbuf)
70 g_object_ref(m_pixbuf);
71 }
72 return *this;
73 }
74
75 bool wxAnimation::LoadFile(const wxString &name, wxAnimationType WXUNUSED(type))
76 {
77 UnRef();
78 m_pixbuf = gdk_pixbuf_animation_new_from_file(
79 wxConvFileName->cWX2MB(name), NULL);
80 return IsOk();
81 }
82
83 bool wxAnimation::Load(wxInputStream &stream, wxAnimationType type)
84 {
85 UnRef();
86
87 char anim_type[12];
88 switch (type)
89 {
90 case wxANIMATION_TYPE_GIF:
91 strcpy(anim_type, "gif");
92 break;
93
94 case wxANIMATION_TYPE_ANI:
95 strcpy(anim_type, "ani");
96 break;
97
98 default:
99 anim_type[0] = '\0';
100 break;
101 }
102
103 // create a GdkPixbufLoader
104 GError *error = NULL;
105 GdkPixbufLoader *loader;
106 if (type != wxANIMATION_TYPE_INVALID && type != wxANIMATION_TYPE_ANY)
107 loader = gdk_pixbuf_loader_new_with_type(anim_type, &error);
108 else
109 loader = gdk_pixbuf_loader_new();
110
111 if (!loader)
112 {
113 wxLogDebug(wxT("Could not create the loader for '%s' animation type"), anim_type);
114 return false;
115 }
116
117 // connect to loader signals
118 g_signal_connect(loader, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated), this);
119
120 //m_bLoadComplete = false;
121 guchar buf[2048];
122 while (stream.IsOk())
123 {
124 // read a chunk of data
125 stream.Read(buf, sizeof(buf));
126
127 // fetch all data into the loader
128 if (!gdk_pixbuf_loader_write(loader, buf, stream.LastRead(), &error))
129 {
130 gdk_pixbuf_loader_close(loader, &error);
131 wxLogDebug(wxT("Could not write to the loader"));
132 return false;
133 }
134 }
135
136 // load complete
137 if (!gdk_pixbuf_loader_close(loader, &error))
138 {
139 wxLogDebug(wxT("Could not close the loader"));
140 return false;
141 }
142 //m_bLoadComplete = true;
143
144 // wait until we get the last area_updated signal
145 return true;
146 }
147
148 wxImage wxAnimation::GetFrame(size_t i) const
149 {
150 return wxNullImage;
151 }
152
153 wxSize wxAnimation::GetSize() const
154 {
155 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf),
156 gdk_pixbuf_animation_get_height(m_pixbuf));
157 }
158
159 void wxAnimation::UnRef()
160 {
161 if (m_pixbuf)
162 g_object_unref(m_pixbuf);
163 m_pixbuf = NULL;
164 }
165
166 void wxAnimation::SetPixbuf(GdkPixbufAnimation* p)
167 {
168 UnRef();
169 m_pixbuf = p;
170 if (m_pixbuf)
171 g_object_ref(m_pixbuf);
172 }
173
174 //-----------------------------------------------------------------------------
175 // wxAnimationCtrl
176 //-----------------------------------------------------------------------------
177
178 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl, wxAnimationCtrlBase)
179 BEGIN_EVENT_TABLE(wxAnimationCtrl, wxAnimationCtrlBase)
180 EVT_TIMER(wxID_ANY, wxAnimationCtrl::OnTimer)
181 END_EVENT_TABLE()
182
183 wxAnimationCtrl::wxAnimationCtrl()
184 {
185 m_anim = NULL;
186 m_iter = NULL;
187 m_bPlaying = false;
188 }
189
190 bool wxAnimationCtrl::Create( wxWindow *parent, wxWindowID id,
191 const wxAnimation& anim,
192 const wxPoint& pos,
193 const wxSize& size,
194 long style,
195 const wxString& name)
196 {
197 m_needParent = true;
198 m_acceptsFocus = true;
199
200 if (!PreCreation( parent, pos, size ) ||
201 !base_type::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
202 wxDefaultValidator, name))
203 {
204 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
205 return false;
206 }
207
208 SetWindowStyle(style);
209
210 m_widget = gtk_image_new();
211 gtk_widget_show( GTK_WIDGET(m_widget) );
212
213 m_parent->DoAddChild( this );
214
215 PostCreation(size);
216 SetBestSize(size);
217
218 m_anim = NULL;
219 m_iter = NULL;
220 m_bPlaying = false;
221 if (anim != wxNullAnimation)
222 SetAnimation(anim);
223
224 // init the timer used for animation
225 m_timer.SetOwner(this);
226
227 return true;
228 }
229
230 wxAnimationCtrl::~wxAnimationCtrl()
231 {
232 ResetAnim();
233 ResetIter();
234 }
235
236 bool wxAnimationCtrl::LoadFile(const wxString &filename, wxAnimationType type)
237 {
238 wxAnimation anim;
239 if (!anim.LoadFile(filename, type))
240 return false;
241
242 SetAnimation(anim);
243 return true;
244 }
245
246 void wxAnimationCtrl::SetAnimation(const wxAnimation &anim)
247 {
248 if (IsPlaying())
249 Stop();
250
251 ResetAnim();
252 ResetIter();
253
254 // copy underlying GdkPixbuf object
255 m_anim = anim.GetPixbuf();
256
257 // m_anim may be null in case wxNullAnimation has been passed
258 if (m_anim)
259 {
260 // add a reference to the GdkPixbufAnimation
261 g_object_ref(m_anim);
262
263 if (!this->HasFlag(wxAC_NO_AUTORESIZE))
264 FitToAnimation();
265
266 // display first frame
267 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget),
268 gdk_pixbuf_animation_get_static_image(m_anim));
269 }
270 else
271 {
272 // we need to clear the control to the background colour
273 ClearToBackgroundColour();
274 }
275 }
276
277 void wxAnimationCtrl::FitToAnimation()
278 {
279 if (!m_anim)
280 return;
281
282 int w = gdk_pixbuf_animation_get_width(m_anim),
283 h = gdk_pixbuf_animation_get_height(m_anim);
284
285 // update our size to fit animation
286 //if (w > 0 && h > 0)
287 // gtk_widget_set_size_request(m_widget, w, h);
288 SetSize(w, h);
289 }
290
291 void wxAnimationCtrl::ResetAnim()
292 {
293 if (m_anim)
294 g_object_unref(m_anim);
295 m_anim = NULL;
296 }
297
298 void wxAnimationCtrl::ResetIter()
299 {
300 if (m_iter)
301 g_object_unref(m_iter);
302 m_iter = NULL;
303 }
304
305 bool wxAnimationCtrl::Play()
306 {
307 if (m_anim == NULL)
308 return false;
309
310 // init the iterator and start a one-shot timer
311 ResetIter();
312 m_iter = gdk_pixbuf_animation_get_iter (m_anim, NULL);
313 m_bPlaying = true;
314
315 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
316 // that the timer should not start
317 int n = gdk_pixbuf_animation_iter_get_delay_time(m_iter);
318 if (n >= 0)
319 m_timer.Start(n, true);
320
321 return true;
322 }
323
324 void wxAnimationCtrl::Stop()
325 {
326 // leave current frame displayed until Play() is called again
327 if (IsPlaying())
328 m_timer.Stop();
329 m_bPlaying = false;
330 }
331
332 bool wxAnimationCtrl::IsPlaying() const
333 {
334 // NB: we cannot just return m_timer.IsRunning() as this would not
335 // be safe as e.g. if we are displaying a frame forever,
336 // then we are "officially" still playing the animation, but
337 // the timer is not running anymore...
338 return m_bPlaying;
339 }
340
341 wxSize wxAnimationCtrl::DoGetBestSize() const
342 {
343 if (m_anim && !this->HasFlag(wxAC_NO_AUTORESIZE))
344 {
345 return wxSize(gdk_pixbuf_animation_get_width(m_anim),
346 gdk_pixbuf_animation_get_height(m_anim));
347 }
348
349 return wxSize(100,100);
350 }
351
352 void wxAnimationCtrl::ClearToBackgroundColour()
353 {
354 wxSize sz = GetClientSize();
355 GdkPixbuf *newpix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8,
356 sz.GetWidth(), sz.GetHeight());
357 if (!newpix)
358 return;
359
360 wxColour clr = GetBackgroundColour();
361 guint32 col = (clr.Red() << 24) | (clr.Green() << 16) | (clr.Blue() << 8);
362 gdk_pixbuf_fill(newpix, col);
363
364 wxLogDebug(wxT("Clearing to background %s"), clr.GetAsString().c_str());
365
366 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget), newpix);
367 g_object_unref(newpix);
368 }
369
370 bool wxAnimationCtrl::SetBackgroundColour( const wxColour &colour )
371 {
372 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
373 // it won't show the background colour unlike the user would expect.
374 // Thus we clear the GtkImage contents to the background colour...
375 if (!wxControl::SetBackgroundColour(colour))
376 return false;
377 ClearToBackgroundColour();
378 return true;
379 }
380
381
382 //-----------------------------------------------------------------------------
383 // wxAnimationCtrl - event handlers
384 //-----------------------------------------------------------------------------
385
386 void wxAnimationCtrl::OnTimer(wxTimerEvent &ev)
387 {
388 wxASSERT(m_iter != NULL);
389
390 // gdk_pixbuf_animation_iter_advance() will automatically restart
391 // the animation, if necessary and we have no way to know !!
392 if (gdk_pixbuf_animation_iter_advance(m_iter, NULL))
393 {
394 // start a new one-shot timer
395 int n = gdk_pixbuf_animation_iter_get_delay_time(m_iter);
396 if (n >= 0)
397 m_timer.Start(n, true);
398
399 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget),
400 gdk_pixbuf_animation_iter_get_pixbuf(m_iter));
401 }
402 else
403 {
404 // no need to update the m_widget yet
405 m_timer.Start(10, true);
406 }
407 }
408
409 #endif // wxUSE_ANIMATIONCTRL