]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/animate.cpp
No real changes, just update copyright year in wxInfoMessageBox().
[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 && !defined(__WXUNIVERSAL__)
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 "wx/wfstream.h"
26
27 #include <gtk/gtk.h>
28
29
30 // ============================================================================
31 // implementation
32 // ============================================================================
33
34 void gdk_pixbuf_area_updated(GdkPixbufLoader *loader,
35 gint WXUNUSED(x),
36 gint WXUNUSED(y),
37 gint WXUNUSED(width),
38 gint WXUNUSED(height),
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
54 IMPLEMENT_DYNAMIC_CLASS(wxAnimation, wxAnimationBase)
55
56 wxAnimation::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
64 wxAnimation::wxAnimation(GdkPixbufAnimation *p)
65 {
66 m_pixbuf = p;
67 if ( m_pixbuf )
68 g_object_ref(m_pixbuf);
69 }
70
71 wxAnimation& 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
84 bool wxAnimation::LoadFile(const wxString &name, wxAnimationType WXUNUSED(type))
85 {
86 UnRef();
87 m_pixbuf = gdk_pixbuf_animation_new_from_file(name.fn_str(), NULL);
88 return IsOk();
89 }
90
91 bool 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:
107 anim_type[0] = '\0';
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 error != NULL) // even if the loader was allocated, an error could have happened
121 {
122 wxLogDebug(wxT("Could not create the loader for '%s' animation type: %s"),
123 anim_type, error->message);
124 return false;
125 }
126
127 // connect to loader signals
128 g_signal_connect(loader, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated), this);
129
130 guchar buf[2048];
131 bool data_written = false;
132 while (stream.IsOk())
133 {
134 // read a chunk of data
135 if (!stream.Read(buf, sizeof(buf)) &&
136 stream.GetLastError() != wxSTREAM_EOF) // EOF is OK for now
137 {
138 // gdk_pixbuf_loader_close wants the GError == NULL
139 gdk_pixbuf_loader_close(loader, NULL);
140 return false;
141 }
142
143 // fetch all data into the loader
144 if (!gdk_pixbuf_loader_write(loader, buf, stream.LastRead(), &error))
145 {
146 wxLogDebug(wxT("Could not write to the loader: %s"), error->message);
147
148 // gdk_pixbuf_loader_close wants the GError == NULL
149 gdk_pixbuf_loader_close(loader, NULL);
150 return false;
151 }
152
153 data_written = true;
154 }
155
156 if (!data_written)
157 {
158 wxLogDebug("Could not read data from the stream...");
159 return false;
160 }
161
162 // load complete: gdk_pixbuf_loader_close will now check if the data we
163 // wrote inside the pixbuf loader does make sense and will give an error
164 // if it doesn't (because of a truncated file, corrupted data or whatelse)
165 if (!gdk_pixbuf_loader_close(loader, &error))
166 {
167 wxLogDebug(wxT("Could not close the loader: %s"), error->message);
168 return false;
169 }
170
171 // wait until we get the last area_updated signal
172 return data_written;
173 }
174
175 wxImage wxAnimation::GetFrame(unsigned int WXUNUSED(frame)) const
176 {
177 return wxNullImage;
178 }
179
180 wxSize wxAnimation::GetSize() const
181 {
182 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf),
183 gdk_pixbuf_animation_get_height(m_pixbuf));
184 }
185
186 void wxAnimation::UnRef()
187 {
188 if (m_pixbuf)
189 g_object_unref(m_pixbuf);
190 m_pixbuf = NULL;
191 }
192
193 void wxAnimation::SetPixbuf(GdkPixbufAnimation* p)
194 {
195 UnRef();
196 m_pixbuf = p;
197 if (m_pixbuf)
198 g_object_ref(m_pixbuf);
199 }
200
201 //-----------------------------------------------------------------------------
202 // wxAnimationCtrl
203 //-----------------------------------------------------------------------------
204
205 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl, wxAnimationCtrlBase)
206 BEGIN_EVENT_TABLE(wxAnimationCtrl, wxAnimationCtrlBase)
207 EVT_TIMER(wxID_ANY, wxAnimationCtrl::OnTimer)
208 END_EVENT_TABLE()
209
210 void wxAnimationCtrl::Init()
211 {
212 m_anim = NULL;
213 m_iter = NULL;
214 m_bPlaying = false;
215 }
216
217 bool wxAnimationCtrl::Create( wxWindow *parent, wxWindowID id,
218 const wxAnimation& anim,
219 const wxPoint& pos,
220 const wxSize& size,
221 long style,
222 const wxString& name)
223 {
224 if (!PreCreation( parent, pos, size ) ||
225 !base_type::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
226 wxDefaultValidator, name))
227 {
228 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
229 return false;
230 }
231
232 SetWindowStyle(style);
233
234 m_widget = gtk_image_new();
235 g_object_ref(m_widget);
236 gtk_widget_show(m_widget);
237
238 m_parent->DoAddChild( this );
239
240 PostCreation(size);
241 SetInitialSize(size);
242
243 if (anim.IsOk())
244 SetAnimation(anim);
245
246 // init the timer used for animation
247 m_timer.SetOwner(this);
248
249 return true;
250 }
251
252 wxAnimationCtrl::~wxAnimationCtrl()
253 {
254 ResetAnim();
255 ResetIter();
256 }
257
258 bool wxAnimationCtrl::LoadFile(const wxString &filename, wxAnimationType type)
259 {
260 wxFileInputStream fis(filename);
261 if (!fis.IsOk())
262 return false;
263 return Load(fis, type);
264 }
265
266 bool wxAnimationCtrl::Load(wxInputStream& stream, wxAnimationType type)
267 {
268 wxAnimation anim;
269 if ( !anim.Load(stream, type) || !anim.IsOk() )
270 return false;
271
272 SetAnimation(anim);
273 return true;
274 }
275
276 void wxAnimationCtrl::SetAnimation(const wxAnimation &anim)
277 {
278 if (IsPlaying())
279 Stop();
280
281 ResetAnim();
282 ResetIter();
283
284 // copy underlying GdkPixbuf object
285 m_anim = anim.GetPixbuf();
286
287 // m_anim may be null in case wxNullAnimation has been passed
288 if (m_anim)
289 {
290 // add a reference to the GdkPixbufAnimation
291 g_object_ref(m_anim);
292
293 if (!this->HasFlag(wxAC_NO_AUTORESIZE))
294 FitToAnimation();
295 }
296
297 DisplayStaticImage();
298 }
299
300 void wxAnimationCtrl::FitToAnimation()
301 {
302 if (!m_anim)
303 return;
304
305 int w = gdk_pixbuf_animation_get_width(m_anim),
306 h = gdk_pixbuf_animation_get_height(m_anim);
307
308 // update our size to fit animation
309 SetSize(w, h);
310 }
311
312 void wxAnimationCtrl::ResetAnim()
313 {
314 if (m_anim)
315 g_object_unref(m_anim);
316 m_anim = NULL;
317 }
318
319 void wxAnimationCtrl::ResetIter()
320 {
321 if (m_iter)
322 g_object_unref(m_iter);
323 m_iter = NULL;
324 }
325
326 bool wxAnimationCtrl::Play()
327 {
328 if (m_anim == NULL)
329 return false;
330
331 // init the iterator and start a one-shot timer
332 ResetIter();
333 m_iter = gdk_pixbuf_animation_get_iter (m_anim, NULL);
334 m_bPlaying = true;
335
336 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
337 // that the timer should not start
338 int n = gdk_pixbuf_animation_iter_get_delay_time(m_iter);
339 if (n >= 0)
340 m_timer.Start(n, true);
341
342 return true;
343 }
344
345 void wxAnimationCtrl::Stop()
346 {
347 // leave current frame displayed until Play() is called again
348 if (IsPlaying())
349 m_timer.Stop();
350 m_bPlaying = false;
351
352 ResetIter();
353 DisplayStaticImage();
354 }
355
356 void wxAnimationCtrl::DisplayStaticImage()
357 {
358 wxASSERT(!IsPlaying());
359
360 // m_bmpStaticReal will be updated only if necessary...
361 UpdateStaticImage();
362
363 if (m_bmpStaticReal.IsOk())
364 {
365 // show inactive bitmap
366 GdkBitmap *mask = NULL;
367 if (m_bmpStaticReal.GetMask())
368 mask = m_bmpStaticReal.GetMask()->GetBitmap();
369
370 if (m_bmpStaticReal.HasPixbuf())
371 {
372 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget),
373 m_bmpStaticReal.GetPixbuf());
374 }
375 else
376 {
377 gtk_image_set_from_pixmap(GTK_IMAGE(m_widget),
378 m_bmpStaticReal.GetPixmap(), mask);
379 }
380 }
381 else
382 {
383 if (m_anim)
384 {
385 // even if not clearly documented, gdk_pixbuf_animation_get_static_image()
386 // always returns the first frame of the animation
387 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget),
388 gdk_pixbuf_animation_get_static_image(m_anim));
389 }
390 else
391 {
392 ClearToBackgroundColour();
393 }
394 }
395 }
396
397 bool wxAnimationCtrl::IsPlaying() const
398 {
399 // NB: we cannot just return m_timer.IsRunning() as this would not
400 // be safe as e.g. if we are displaying a frame forever,
401 // then we are "officially" still playing the animation, but
402 // the timer is not running anymore...
403 return m_bPlaying;
404 }
405
406 wxSize wxAnimationCtrl::DoGetBestSize() const
407 {
408 if (m_anim && !this->HasFlag(wxAC_NO_AUTORESIZE))
409 {
410 return wxSize(gdk_pixbuf_animation_get_width(m_anim),
411 gdk_pixbuf_animation_get_height(m_anim));
412 }
413
414 return wxSize(100,100);
415 }
416
417 void wxAnimationCtrl::ClearToBackgroundColour()
418 {
419 wxSize sz = GetClientSize();
420 GdkPixbuf *newpix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8,
421 sz.GetWidth(), sz.GetHeight());
422 if (!newpix)
423 return;
424
425 wxColour clr = GetBackgroundColour();
426 guint32 col = (clr.Red() << 24) | (clr.Green() << 16) | (clr.Blue() << 8);
427 gdk_pixbuf_fill(newpix, col);
428
429 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget), newpix);
430 g_object_unref(newpix);
431 }
432
433 bool wxAnimationCtrl::SetBackgroundColour( const wxColour &colour )
434 {
435 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
436 // it won't show the background colour unlike the user would expect.
437 // Thus we clear the GtkImage contents to the background colour...
438 if (!wxControl::SetBackgroundColour(colour))
439 return false;
440
441 // if not playing the change must take place immediately but
442 // remember that the inactive bitmap has higher priority over the background
443 // colour; DisplayStaticImage() will handle that
444 if ( !IsPlaying() )
445 DisplayStaticImage();
446
447 return true;
448 }
449
450
451 //-----------------------------------------------------------------------------
452 // wxAnimationCtrl - event handlers
453 //-----------------------------------------------------------------------------
454
455 void wxAnimationCtrl::OnTimer(wxTimerEvent& WXUNUSED(ev))
456 {
457 wxASSERT(m_iter != NULL);
458
459 // gdk_pixbuf_animation_iter_advance() will automatically restart
460 // the animation, if necessary and we have no way to know !!
461 if (gdk_pixbuf_animation_iter_advance(m_iter, NULL))
462 {
463 // start a new one-shot timer
464 int n = gdk_pixbuf_animation_iter_get_delay_time(m_iter);
465 if (n >= 0)
466 m_timer.Start(n, true);
467
468 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget),
469 gdk_pixbuf_animation_iter_get_pixbuf(m_iter));
470 }
471 else
472 {
473 // no need to update the m_widget yet
474 m_timer.Start(10, true);
475 }
476 }
477
478 #endif // wxUSE_ANIMATIONCTRL