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