]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/animate.cpp
use Pango to draw rotated text if possible (this supports text background and should...
[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 {
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
128 guchar buf[2048];
129 while (stream.IsOk())
130 {
131 // read a chunk of data
132 stream.Read(buf, sizeof(buf));
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 }
149
150 // wait until we get the last area_updated signal
151 return true;
152 }
153
154 wxImage wxAnimation::GetFrame(unsigned int WXUNUSED(frame)) const
155 {
156 return wxNullImage;
157 }
158
159 wxSize wxAnimation::GetSize() const
160 {
161 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf),
162 gdk_pixbuf_animation_get_height(m_pixbuf));
163 }
164
165 void wxAnimation::UnRef()
166 {
167 if (m_pixbuf)
168 g_object_unref(m_pixbuf);
169 m_pixbuf = NULL;
170 }
171
172 void wxAnimation::SetPixbuf(GdkPixbufAnimation* p)
173 {
174 UnRef();
175 m_pixbuf = p;
176 if (m_pixbuf)
177 g_object_ref(m_pixbuf);
178 }
179
180 //-----------------------------------------------------------------------------
181 // wxAnimationCtrl
182 //-----------------------------------------------------------------------------
183
184 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl, wxAnimationCtrlBase)
185 BEGIN_EVENT_TABLE(wxAnimationCtrl, wxAnimationCtrlBase)
186 EVT_TIMER(wxID_ANY, wxAnimationCtrl::OnTimer)
187 END_EVENT_TABLE()
188
189 void wxAnimationCtrl::Init()
190 {
191 m_anim = NULL;
192 m_iter = NULL;
193 m_bPlaying = false;
194 }
195
196 bool 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 {
203 if (!PreCreation( parent, pos, size ) ||
204 !base_type::CreateBase(parent, id, pos, size, style & wxWINDOW_STYLE_MASK,
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();
214 gtk_widget_show(m_widget);
215
216 m_parent->DoAddChild( this );
217
218 PostCreation(size);
219 SetInitialSize(size);
220
221 if (anim.IsOk())
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 wxFileInputStream fis(filename);
239 return Load(fis, type);
240 }
241
242 bool wxAnimationCtrl::Load(wxInputStream& stream, wxAnimationType type)
243 {
244 wxAnimation anim;
245 if ( !anim.Load(stream, type) || !anim.IsOk() )
246 return false;
247
248 SetAnimation(anim);
249 return true;
250 }
251
252 void 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();
271 }
272
273 DisplayStaticImage();
274 }
275
276 void 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
285 SetSize(w, h);
286 }
287
288 void wxAnimationCtrl::ResetAnim()
289 {
290 if (m_anim)
291 g_object_unref(m_anim);
292 m_anim = NULL;
293 }
294
295 void wxAnimationCtrl::ResetIter()
296 {
297 if (m_iter)
298 g_object_unref(m_iter);
299 m_iter = NULL;
300 }
301
302 bool 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
321 void wxAnimationCtrl::Stop()
322 {
323 // leave current frame displayed until Play() is called again
324 if (IsPlaying())
325 m_timer.Stop();
326 m_bPlaying = false;
327
328 ResetIter();
329 DisplayStaticImage();
330 }
331
332 void wxAnimationCtrl::DisplayStaticImage()
333 {
334 wxASSERT(!IsPlaying());
335
336 // m_bmpStaticReal will be updated only if necessary...
337 UpdateStaticImage();
338
339 if (m_bmpStaticReal.IsOk())
340 {
341 // show inactive bitmap
342 GdkBitmap *mask = (GdkBitmap *) NULL;
343 if (m_bmpStaticReal.GetMask())
344 mask = m_bmpStaticReal.GetMask()->GetBitmap();
345
346 if (m_bmpStaticReal.HasPixbuf())
347 {
348 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget),
349 m_bmpStaticReal.GetPixbuf());
350 }
351 else
352 {
353 gtk_image_set_from_pixmap(GTK_IMAGE(m_widget),
354 m_bmpStaticReal.GetPixmap(), mask);
355 }
356 }
357 else
358 {
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 }
370 }
371 }
372
373 bool 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
382 wxSize 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
393 void 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
405 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget), newpix);
406 g_object_unref(newpix);
407 }
408
409 bool 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;
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
423 return true;
424 }
425
426
427 //-----------------------------------------------------------------------------
428 // wxAnimationCtrl - event handlers
429 //-----------------------------------------------------------------------------
430
431 void wxAnimationCtrl::OnTimer(wxTimerEvent& WXUNUSED(ev))
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