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