]>
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();
121 wxLogDebug(wxT("Could not create the loader for '%s' animation type"), anim_type
);
125 // connect to loader signals
126 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
129 while (stream
.IsOk())
131 // read a chunk of data
132 stream
.Read(buf
, sizeof(buf
));
134 // fetch all data into the loader
135 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
137 gdk_pixbuf_loader_close(loader
, &error
);
138 wxLogDebug(wxT("Could not write to the loader"));
144 if (!gdk_pixbuf_loader_close(loader
, &error
))
146 wxLogDebug(wxT("Could not close the loader"));
150 // wait until we get the last area_updated signal
154 wxImage
wxAnimation::GetFrame(unsigned int WXUNUSED(frame
)) const
159 wxSize
wxAnimation::GetSize() const
161 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf
),
162 gdk_pixbuf_animation_get_height(m_pixbuf
));
165 void wxAnimation::UnRef()
168 g_object_unref(m_pixbuf
);
172 void wxAnimation::SetPixbuf(GdkPixbufAnimation
* p
)
177 g_object_ref(m_pixbuf
);
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
185 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
186 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
189 void wxAnimationCtrl::Init()
196 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
197 const wxAnimation
& anim
,
201 const wxString
& name
)
203 if (!PreCreation( parent
, pos
, size
) ||
204 !base_type::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
205 wxDefaultValidator
, name
))
207 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
211 SetWindowStyle(style
);
213 m_widget
= gtk_image_new();
214 gtk_widget_show(m_widget
);
216 m_parent
->DoAddChild( this );
219 SetInitialSize(size
);
224 // init the timer used for animation
225 m_timer
.SetOwner(this);
230 wxAnimationCtrl::~wxAnimationCtrl()
236 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
238 wxFileInputStream
fis(filename
);
239 return Load(fis
, type
);
242 bool wxAnimationCtrl::Load(wxInputStream
& stream
, wxAnimationType type
)
245 if ( !anim
.Load(stream
, type
) || !anim
.IsOk() )
252 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
260 // copy underlying GdkPixbuf object
261 m_anim
= anim
.GetPixbuf();
263 // m_anim may be null in case wxNullAnimation has been passed
266 // add a reference to the GdkPixbufAnimation
267 g_object_ref(m_anim
);
269 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
273 DisplayStaticImage();
276 void wxAnimationCtrl::FitToAnimation()
281 int w
= gdk_pixbuf_animation_get_width(m_anim
),
282 h
= gdk_pixbuf_animation_get_height(m_anim
);
284 // update our size to fit animation
288 void wxAnimationCtrl::ResetAnim()
291 g_object_unref(m_anim
);
295 void wxAnimationCtrl::ResetIter()
298 g_object_unref(m_iter
);
302 bool wxAnimationCtrl::Play()
307 // init the iterator and start a one-shot timer
309 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
312 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
313 // that the timer should not start
314 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
316 m_timer
.Start(n
, true);
321 void wxAnimationCtrl::Stop()
323 // leave current frame displayed until Play() is called again
329 DisplayStaticImage();
332 void wxAnimationCtrl::DisplayStaticImage()
334 wxASSERT(!IsPlaying());
336 // m_bmpStaticReal will be updated only if necessary...
339 if (m_bmpStaticReal
.IsOk())
341 // show inactive bitmap
342 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
343 if (m_bmpStaticReal
.GetMask())
344 mask
= m_bmpStaticReal
.GetMask()->GetBitmap();
346 if (m_bmpStaticReal
.HasPixbuf())
348 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
349 m_bmpStaticReal
.GetPixbuf());
353 gtk_image_set_from_pixmap(GTK_IMAGE(m_widget
),
354 m_bmpStaticReal
.GetPixmap(), mask
);
361 // even if not clearly documented, gdk_pixbuf_animation_get_static_image()
362 // always returns the first frame of the animation
363 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
364 gdk_pixbuf_animation_get_static_image(m_anim
));
368 ClearToBackgroundColour();
373 bool wxAnimationCtrl::IsPlaying() const
375 // NB: we cannot just return m_timer.IsRunning() as this would not
376 // be safe as e.g. if we are displaying a frame forever,
377 // then we are "officially" still playing the animation, but
378 // the timer is not running anymore...
382 wxSize
wxAnimationCtrl::DoGetBestSize() const
384 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
386 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
387 gdk_pixbuf_animation_get_height(m_anim
));
390 return wxSize(100,100);
393 void wxAnimationCtrl::ClearToBackgroundColour()
395 wxSize sz
= GetClientSize();
396 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
397 sz
.GetWidth(), sz
.GetHeight());
401 wxColour clr
= GetBackgroundColour();
402 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
403 gdk_pixbuf_fill(newpix
, col
);
405 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
406 g_object_unref(newpix
);
409 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
411 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
412 // it won't show the background colour unlike the user would expect.
413 // Thus we clear the GtkImage contents to the background colour...
414 if (!wxControl::SetBackgroundColour(colour
))
417 // if not playing the change must take place immediately but
418 // remember that the inactive bitmap has higher priority over the background
419 // colour; DisplayStaticImage() will handle that
421 DisplayStaticImage();
427 //-----------------------------------------------------------------------------
428 // wxAnimationCtrl - event handlers
429 //-----------------------------------------------------------------------------
431 void wxAnimationCtrl::OnTimer(wxTimerEvent
& WXUNUSED(ev
))
433 wxASSERT(m_iter
!= NULL
);
435 // gdk_pixbuf_animation_iter_advance() will automatically restart
436 // the animation, if necessary and we have no way to know !!
437 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
439 // start a new one-shot timer
440 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
442 m_timer
.Start(n
, true);
444 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
445 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
449 // no need to update the m_widget yet
450 m_timer
.Start(10, true);
454 #endif // wxUSE_ANIMATIONCTRL