]>
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
,
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(
86 wxConvFileName
->cWX2MB(name
), NULL
);
90 bool wxAnimation::Load(wxInputStream
&stream
, wxAnimationType type
)
97 case wxANIMATION_TYPE_GIF
:
98 strcpy(anim_type
, "gif");
101 case wxANIMATION_TYPE_ANI
:
102 strcpy(anim_type
, "ani");
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
);
116 loader
= gdk_pixbuf_loader_new();
120 wxLogDebug(wxT("Could not create the loader for '%s' animation type"), anim_type
);
124 // connect to loader signals
125 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
128 while (stream
.IsOk())
130 // read a chunk of data
131 stream
.Read(buf
, sizeof(buf
));
133 // fetch all data into the loader
134 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
136 gdk_pixbuf_loader_close(loader
, &error
);
137 wxLogDebug(wxT("Could not write to the loader"));
143 if (!gdk_pixbuf_loader_close(loader
, &error
))
145 wxLogDebug(wxT("Could not close the loader"));
149 // wait until we get the last area_updated signal
153 wxImage
wxAnimation::GetFrame(size_t i
) const
158 wxSize
wxAnimation::GetSize() const
160 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf
),
161 gdk_pixbuf_animation_get_height(m_pixbuf
));
164 void wxAnimation::UnRef()
167 g_object_unref(m_pixbuf
);
171 void wxAnimation::SetPixbuf(GdkPixbufAnimation
* p
)
176 g_object_ref(m_pixbuf
);
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
184 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
185 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
188 void wxAnimationCtrl::Init()
195 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
196 const wxAnimation
& anim
,
200 const wxString
& name
)
203 m_acceptsFocus
= true;
205 if (!PreCreation( parent
, pos
, size
) ||
206 !base_type::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
207 wxDefaultValidator
, name
))
209 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
213 SetWindowStyle(style
);
215 m_widget
= gtk_image_new();
216 gtk_widget_show( GTK_WIDGET(m_widget
) );
218 m_parent
->DoAddChild( this );
221 SetInitialSize(size
);
226 // init the timer used for animation
227 m_timer
.SetOwner(this);
232 wxAnimationCtrl::~wxAnimationCtrl()
238 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
241 if (!anim
.LoadFile(filename
, type
))
248 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
256 // copy underlying GdkPixbuf object
257 m_anim
= anim
.GetPixbuf();
259 // m_anim may be null in case wxNullAnimation has been passed
262 // add a reference to the GdkPixbufAnimation
263 g_object_ref(m_anim
);
265 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
269 DisplayStaticImage();
272 void wxAnimationCtrl::FitToAnimation()
277 int w
= gdk_pixbuf_animation_get_width(m_anim
),
278 h
= gdk_pixbuf_animation_get_height(m_anim
);
280 // update our size to fit animation
284 void wxAnimationCtrl::ResetAnim()
287 g_object_unref(m_anim
);
291 void wxAnimationCtrl::ResetIter()
294 g_object_unref(m_iter
);
298 bool wxAnimationCtrl::Play()
303 // init the iterator and start a one-shot timer
305 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
308 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
309 // that the timer should not start
310 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
312 m_timer
.Start(n
, true);
317 void wxAnimationCtrl::Stop()
319 // leave current frame displayed until Play() is called again
325 DisplayStaticImage();
328 void wxAnimationCtrl::SetInactiveBitmap(const wxBitmap
&bmp
)
332 // update the pixbuf associated with m_widget now...
334 DisplayStaticImage();
337 void wxAnimationCtrl::DisplayStaticImage()
339 wxASSERT(!IsPlaying());
341 if (m_bmpStatic
.IsOk())
343 // show inactive bitmap
344 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
345 if (m_bmpStatic
.GetMask())
346 mask
= m_bmpStatic
.GetMask()->GetBitmap();
348 if (m_bmpStatic
.HasPixbuf())
350 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
351 m_bmpStatic
.GetPixbuf());
355 gtk_image_set_from_pixmap(GTK_IMAGE(m_widget
),
356 m_bmpStatic
.GetPixmap(), mask
);
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
));
370 ClearToBackgroundColour();
375 bool wxAnimationCtrl::IsPlaying() const
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...
384 wxSize
wxAnimationCtrl::DoGetBestSize() const
386 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
388 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
389 gdk_pixbuf_animation_get_height(m_anim
));
392 return wxSize(100,100);
395 void wxAnimationCtrl::ClearToBackgroundColour()
397 wxSize sz
= GetClientSize();
398 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
399 sz
.GetWidth(), sz
.GetHeight());
403 wxColour clr
= GetBackgroundColour();
404 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
405 gdk_pixbuf_fill(newpix
, col
);
407 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
408 g_object_unref(newpix
);
411 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
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
))
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
423 DisplayStaticImage();
429 //-----------------------------------------------------------------------------
430 // wxAnimationCtrl - event handlers
431 //-----------------------------------------------------------------------------
433 void wxAnimationCtrl::OnTimer(wxTimerEvent
&ev
)
435 wxASSERT(m_iter
!= NULL
);
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
))
441 // start a new one-shot timer
442 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
444 m_timer
.Start(n
, true);
446 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
447 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
451 // no need to update the m_widget yet
452 m_timer
.Start(10, true);
456 #endif // wxUSE_ANIMATIONCTRL