]>
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"
25 #include "wx/wfstream.h"
30 // ============================================================================
32 // ============================================================================
34 void gdk_pixbuf_area_updated(GdkPixbufLoader
*loader
,
38 gint
WXUNUSED(height
),
41 if (anim
&& anim
->GetPixbuf() == NULL
)
43 // we need to set the pixbuf only if this is the first time this signal
45 anim
->SetPixbuf(gdk_pixbuf_loader_get_animation(loader
));
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxAnimation
, wxAnimationBase
)
56 wxAnimation::wxAnimation(const wxAnimation
& that
)
59 m_pixbuf
= that
.m_pixbuf
;
61 g_object_ref(m_pixbuf
);
64 wxAnimation::wxAnimation(GdkPixbufAnimation
*p
)
68 g_object_ref(m_pixbuf
);
71 wxAnimation
& wxAnimation::operator=(const wxAnimation
& that
)
75 base_type::operator=(that
);
77 m_pixbuf
= that
.m_pixbuf
;
79 g_object_ref(m_pixbuf
);
84 bool wxAnimation::LoadFile(const wxString
&name
, wxAnimationType
WXUNUSED(type
))
87 m_pixbuf
= gdk_pixbuf_animation_new_from_file(name
.fn_str(), NULL
);
91 bool wxAnimation::Load(wxInputStream
&stream
, wxAnimationType type
)
98 case wxANIMATION_TYPE_GIF
:
99 strcpy(anim_type
, "gif");
102 case wxANIMATION_TYPE_ANI
:
103 strcpy(anim_type
, "ani");
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
);
117 loader
= gdk_pixbuf_loader_new();
120 error
!= NULL
) // even if the loader was allocated, an error could have happened
122 wxLogDebug(wxT("Could not create the loader for '%s' animation type: %s"),
123 anim_type
, error
->message
);
127 // connect to loader signals
128 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
131 bool data_written
= false;
132 while (stream
.IsOk())
134 // read a chunk of data
135 if (!stream
.Read(buf
, sizeof(buf
)))
137 // gdk_pixbuf_loader_close wants the GError == NULL
138 gdk_pixbuf_loader_close(loader
, NULL
);
142 // fetch all data into the loader
143 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
145 wxLogDebug(wxT("Could not write to the loader: %s"), error
->message
);
147 // gdk_pixbuf_loader_close wants the GError == NULL
148 gdk_pixbuf_loader_close(loader
, NULL
);
157 wxLogDebug("Could not read data from the stream...");
162 if (!gdk_pixbuf_loader_close(loader
, &error
))
164 wxLogDebug(wxT("Could not close the loader: %s"), error
->message
);
168 // wait until we get the last area_updated signal
172 wxImage
wxAnimation::GetFrame(unsigned int WXUNUSED(frame
)) const
177 wxSize
wxAnimation::GetSize() const
179 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf
),
180 gdk_pixbuf_animation_get_height(m_pixbuf
));
183 void wxAnimation::UnRef()
186 g_object_unref(m_pixbuf
);
190 void wxAnimation::SetPixbuf(GdkPixbufAnimation
* p
)
195 g_object_ref(m_pixbuf
);
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
202 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
203 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
204 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
207 void wxAnimationCtrl::Init()
214 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
215 const wxAnimation
& anim
,
219 const wxString
& name
)
221 if (!PreCreation( parent
, pos
, size
) ||
222 !base_type::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
223 wxDefaultValidator
, name
))
225 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
229 SetWindowStyle(style
);
231 m_widget
= gtk_image_new();
232 g_object_ref(m_widget
);
233 gtk_widget_show(m_widget
);
235 m_parent
->DoAddChild( this );
238 SetInitialSize(size
);
243 // init the timer used for animation
244 m_timer
.SetOwner(this);
249 wxAnimationCtrl::~wxAnimationCtrl()
255 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
257 wxFileInputStream
fis(filename
);
260 return Load(fis
, type
);
263 bool wxAnimationCtrl::Load(wxInputStream
& stream
, wxAnimationType type
)
266 if ( !anim
.Load(stream
, type
) || !anim
.IsOk() )
273 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
281 // copy underlying GdkPixbuf object
282 m_anim
= anim
.GetPixbuf();
284 // m_anim may be null in case wxNullAnimation has been passed
287 // add a reference to the GdkPixbufAnimation
288 g_object_ref(m_anim
);
290 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
294 DisplayStaticImage();
297 void wxAnimationCtrl::FitToAnimation()
302 int w
= gdk_pixbuf_animation_get_width(m_anim
),
303 h
= gdk_pixbuf_animation_get_height(m_anim
);
305 // update our size to fit animation
309 void wxAnimationCtrl::ResetAnim()
312 g_object_unref(m_anim
);
316 void wxAnimationCtrl::ResetIter()
319 g_object_unref(m_iter
);
323 bool wxAnimationCtrl::Play()
328 // init the iterator and start a one-shot timer
330 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
333 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
334 // that the timer should not start
335 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
337 m_timer
.Start(n
, true);
342 void wxAnimationCtrl::Stop()
344 // leave current frame displayed until Play() is called again
350 DisplayStaticImage();
353 void wxAnimationCtrl::DisplayStaticImage()
355 wxASSERT(!IsPlaying());
357 // m_bmpStaticReal will be updated only if necessary...
360 if (m_bmpStaticReal
.IsOk())
362 // show inactive bitmap
363 GdkBitmap
*mask
= NULL
;
364 if (m_bmpStaticReal
.GetMask())
365 mask
= m_bmpStaticReal
.GetMask()->GetBitmap();
367 if (m_bmpStaticReal
.HasPixbuf())
369 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
370 m_bmpStaticReal
.GetPixbuf());
374 gtk_image_set_from_pixmap(GTK_IMAGE(m_widget
),
375 m_bmpStaticReal
.GetPixmap(), mask
);
382 // even if not clearly documented, gdk_pixbuf_animation_get_static_image()
383 // always returns the first frame of the animation
384 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
385 gdk_pixbuf_animation_get_static_image(m_anim
));
389 ClearToBackgroundColour();
394 bool wxAnimationCtrl::IsPlaying() const
396 // NB: we cannot just return m_timer.IsRunning() as this would not
397 // be safe as e.g. if we are displaying a frame forever,
398 // then we are "officially" still playing the animation, but
399 // the timer is not running anymore...
403 wxSize
wxAnimationCtrl::DoGetBestSize() const
405 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
407 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
408 gdk_pixbuf_animation_get_height(m_anim
));
411 return wxSize(100,100);
414 void wxAnimationCtrl::ClearToBackgroundColour()
416 wxSize sz
= GetClientSize();
417 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
418 sz
.GetWidth(), sz
.GetHeight());
422 wxColour clr
= GetBackgroundColour();
423 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
424 gdk_pixbuf_fill(newpix
, col
);
426 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
427 g_object_unref(newpix
);
430 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
432 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
433 // it won't show the background colour unlike the user would expect.
434 // Thus we clear the GtkImage contents to the background colour...
435 if (!wxControl::SetBackgroundColour(colour
))
438 // if not playing the change must take place immediately but
439 // remember that the inactive bitmap has higher priority over the background
440 // colour; DisplayStaticImage() will handle that
442 DisplayStaticImage();
448 //-----------------------------------------------------------------------------
449 // wxAnimationCtrl - event handlers
450 //-----------------------------------------------------------------------------
452 void wxAnimationCtrl::OnTimer(wxTimerEvent
& WXUNUSED(ev
))
454 wxASSERT(m_iter
!= NULL
);
456 // gdk_pixbuf_animation_iter_advance() will automatically restart
457 // the animation, if necessary and we have no way to know !!
458 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
460 // start a new one-shot timer
461 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
463 m_timer
.Start(n
, true);
465 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
466 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
470 // no need to update the m_widget yet
471 m_timer
.Start(10, true);
475 #endif // wxUSE_ANIMATIONCTRL