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