]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/animate.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/animate.cpp
3 // Purpose: wxAnimation and wxAnimationCtrl
4 // Author: Francesco Montorsi
8 // Copyright: (c) Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if wxUSE_ANIMATIONCTRL && !defined(__WXUNIVERSAL__)
17 #include "wx/animate.h"
22 #include "wx/stream.h"
28 // ============================================================================
30 // ============================================================================
32 void gdk_pixbuf_area_updated(GdkPixbufLoader
*loader
,
36 gint
WXUNUSED(height
),
39 if (anim
&& anim
->GetPixbuf() == NULL
)
41 // we need to set the pixbuf only if this is the first time this signal
43 anim
->SetPixbuf(gdk_pixbuf_loader_get_animation(loader
));
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxAnimation
, wxAnimationBase
)
54 wxAnimation::wxAnimation(const wxAnimation
& that
)
57 m_pixbuf
= that
.m_pixbuf
;
59 g_object_ref(m_pixbuf
);
62 wxAnimation::wxAnimation(GdkPixbufAnimation
*p
)
66 g_object_ref(m_pixbuf
);
69 wxAnimation
& wxAnimation::operator=(const wxAnimation
& that
)
73 base_type::operator=(that
);
75 m_pixbuf
= that
.m_pixbuf
;
77 g_object_ref(m_pixbuf
);
82 bool wxAnimation::LoadFile(const wxString
&name
, wxAnimationType
WXUNUSED(type
))
85 m_pixbuf
= gdk_pixbuf_animation_new_from_file(name
.fn_str(), NULL
);
89 bool wxAnimation::Load(wxInputStream
&stream
, wxAnimationType type
)
96 case wxANIMATION_TYPE_GIF
:
97 strcpy(anim_type
, "gif");
100 case wxANIMATION_TYPE_ANI
:
101 strcpy(anim_type
, "ani");
109 // create a GdkPixbufLoader
110 GError
*error
= NULL
;
111 GdkPixbufLoader
*loader
;
112 if (type
!= wxANIMATION_TYPE_INVALID
&& type
!= wxANIMATION_TYPE_ANY
)
113 loader
= gdk_pixbuf_loader_new_with_type(anim_type
, &error
);
115 loader
= gdk_pixbuf_loader_new();
119 wxLogDebug(wxT("Could not create the loader for '%s' animation type"), anim_type
);
123 // connect to loader signals
124 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
127 while (stream
.IsOk())
129 // read a chunk of data
130 stream
.Read(buf
, sizeof(buf
));
132 // fetch all data into the loader
133 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
135 gdk_pixbuf_loader_close(loader
, &error
);
136 wxLogDebug(wxT("Could not write to the loader"));
142 if (!gdk_pixbuf_loader_close(loader
, &error
))
144 wxLogDebug(wxT("Could not close the loader"));
148 // wait until we get the last area_updated signal
152 wxImage
wxAnimation::GetFrame(unsigned int WXUNUSED(frame
)) const
157 wxSize
wxAnimation::GetSize() const
159 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf
),
160 gdk_pixbuf_animation_get_height(m_pixbuf
));
163 void wxAnimation::UnRef()
166 g_object_unref(m_pixbuf
);
170 void wxAnimation::SetPixbuf(GdkPixbufAnimation
* p
)
175 g_object_ref(m_pixbuf
);
178 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
182 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
183 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
184 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
187 void wxAnimationCtrl::Init()
194 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
195 const wxAnimation
& anim
,
199 const wxString
& name
)
201 if (!PreCreation( parent
, pos
, size
) ||
202 !base_type::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
203 wxDefaultValidator
, name
))
205 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
209 SetWindowStyle(style
);
211 m_widget
= gtk_image_new();
212 gtk_widget_show(m_widget
);
214 m_parent
->DoAddChild( this );
217 SetInitialSize(size
);
222 // init the timer used for animation
223 m_timer
.SetOwner(this);
228 wxAnimationCtrl::~wxAnimationCtrl()
234 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
237 if (!anim
.LoadFile(filename
, type
))
244 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
252 // copy underlying GdkPixbuf object
253 m_anim
= anim
.GetPixbuf();
255 // m_anim may be null in case wxNullAnimation has been passed
258 // add a reference to the GdkPixbufAnimation
259 g_object_ref(m_anim
);
261 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
265 DisplayStaticImage();
268 void wxAnimationCtrl::FitToAnimation()
273 int w
= gdk_pixbuf_animation_get_width(m_anim
),
274 h
= gdk_pixbuf_animation_get_height(m_anim
);
276 // update our size to fit animation
280 void wxAnimationCtrl::ResetAnim()
283 g_object_unref(m_anim
);
287 void wxAnimationCtrl::ResetIter()
290 g_object_unref(m_iter
);
294 bool wxAnimationCtrl::Play()
299 // init the iterator and start a one-shot timer
301 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
304 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
305 // that the timer should not start
306 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
308 m_timer
.Start(n
, true);
313 void wxAnimationCtrl::Stop()
315 // leave current frame displayed until Play() is called again
321 DisplayStaticImage();
324 void wxAnimationCtrl::DisplayStaticImage()
326 wxASSERT(!IsPlaying());
328 // m_bmpStaticReal will be updated only if necessary...
331 if (m_bmpStaticReal
.IsOk())
333 // show inactive bitmap
334 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
335 if (m_bmpStaticReal
.GetMask())
336 mask
= m_bmpStaticReal
.GetMask()->GetBitmap();
338 if (m_bmpStaticReal
.HasPixbuf())
340 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
341 m_bmpStaticReal
.GetPixbuf());
345 gtk_image_set_from_pixmap(GTK_IMAGE(m_widget
),
346 m_bmpStaticReal
.GetPixmap(), mask
);
353 // even if not clearly documented, gdk_pixbuf_animation_get_static_image()
354 // always returns the first frame of the animation
355 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
356 gdk_pixbuf_animation_get_static_image(m_anim
));
360 ClearToBackgroundColour();
365 bool wxAnimationCtrl::IsPlaying() const
367 // NB: we cannot just return m_timer.IsRunning() as this would not
368 // be safe as e.g. if we are displaying a frame forever,
369 // then we are "officially" still playing the animation, but
370 // the timer is not running anymore...
374 wxSize
wxAnimationCtrl::DoGetBestSize() const
376 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
378 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
379 gdk_pixbuf_animation_get_height(m_anim
));
382 return wxSize(100,100);
385 void wxAnimationCtrl::ClearToBackgroundColour()
387 wxSize sz
= GetClientSize();
388 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
389 sz
.GetWidth(), sz
.GetHeight());
393 wxColour clr
= GetBackgroundColour();
394 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
395 gdk_pixbuf_fill(newpix
, col
);
397 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
398 g_object_unref(newpix
);
401 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
403 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
404 // it won't show the background colour unlike the user would expect.
405 // Thus we clear the GtkImage contents to the background colour...
406 if (!wxControl::SetBackgroundColour(colour
))
409 // if not playing the change must take place immediately but
410 // remember that the inactive bitmap has higher priority over the background
411 // colour; DisplayStaticImage() will handle that
413 DisplayStaticImage();
419 //-----------------------------------------------------------------------------
420 // wxAnimationCtrl - event handlers
421 //-----------------------------------------------------------------------------
423 void wxAnimationCtrl::OnTimer(wxTimerEvent
& WXUNUSED(ev
))
425 wxASSERT(m_iter
!= NULL
);
427 // gdk_pixbuf_animation_iter_advance() will automatically restart
428 // the animation, if necessary and we have no way to know !!
429 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
431 // start a new one-shot timer
432 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
434 m_timer
.Start(n
, true);
436 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
437 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
441 // no need to update the m_widget yet
442 m_timer
.Start(10, true);
446 #endif // wxUSE_ANIMATIONCTRL