]>
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: %s"),
122 anim_type
, error
->message
);
126 // connect to loader signals
127 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
130 bool data_written
= false;
131 while (stream
.IsOk())
133 // read a chunk of data
134 stream
.Read(buf
, sizeof(buf
));
136 // fetch all data into the loader
137 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
139 wxLogDebug(wxT("Could not write to the loader: %s"), error
->message
);
141 // gdk_pixbuf_loader_close wants the GError == NULL; reset it:
143 gdk_pixbuf_loader_close(loader
, &error
);
152 wxLogDebug("Could not read data from the stream...");
157 if (!gdk_pixbuf_loader_close(loader
, &error
))
159 wxLogDebug(wxT("Could not close the loader: %s"), error
->message
);
163 // wait until we get the last area_updated signal
167 wxImage
wxAnimation::GetFrame(unsigned int WXUNUSED(frame
)) const
172 wxSize
wxAnimation::GetSize() const
174 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf
),
175 gdk_pixbuf_animation_get_height(m_pixbuf
));
178 void wxAnimation::UnRef()
181 g_object_unref(m_pixbuf
);
185 void wxAnimation::SetPixbuf(GdkPixbufAnimation
* p
)
190 g_object_ref(m_pixbuf
);
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
197 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
198 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
199 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
202 void wxAnimationCtrl::Init()
209 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
210 const wxAnimation
& anim
,
214 const wxString
& name
)
216 if (!PreCreation( parent
, pos
, size
) ||
217 !base_type::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
218 wxDefaultValidator
, name
))
220 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
224 SetWindowStyle(style
);
226 m_widget
= gtk_image_new();
227 g_object_ref(m_widget
);
228 gtk_widget_show(m_widget
);
230 m_parent
->DoAddChild( this );
233 SetInitialSize(size
);
238 // init the timer used for animation
239 m_timer
.SetOwner(this);
244 wxAnimationCtrl::~wxAnimationCtrl()
250 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
252 wxFileInputStream
fis(filename
);
255 return Load(fis
, type
);
258 bool wxAnimationCtrl::Load(wxInputStream
& stream
, wxAnimationType type
)
261 if ( !anim
.Load(stream
, type
) || !anim
.IsOk() )
268 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
276 // copy underlying GdkPixbuf object
277 m_anim
= anim
.GetPixbuf();
279 // m_anim may be null in case wxNullAnimation has been passed
282 // add a reference to the GdkPixbufAnimation
283 g_object_ref(m_anim
);
285 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
289 DisplayStaticImage();
292 void wxAnimationCtrl::FitToAnimation()
297 int w
= gdk_pixbuf_animation_get_width(m_anim
),
298 h
= gdk_pixbuf_animation_get_height(m_anim
);
300 // update our size to fit animation
304 void wxAnimationCtrl::ResetAnim()
307 g_object_unref(m_anim
);
311 void wxAnimationCtrl::ResetIter()
314 g_object_unref(m_iter
);
318 bool wxAnimationCtrl::Play()
323 // init the iterator and start a one-shot timer
325 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
328 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
329 // that the timer should not start
330 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
332 m_timer
.Start(n
, true);
337 void wxAnimationCtrl::Stop()
339 // leave current frame displayed until Play() is called again
345 DisplayStaticImage();
348 void wxAnimationCtrl::DisplayStaticImage()
350 wxASSERT(!IsPlaying());
352 // m_bmpStaticReal will be updated only if necessary...
355 if (m_bmpStaticReal
.IsOk())
357 // show inactive bitmap
358 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
359 if (m_bmpStaticReal
.GetMask())
360 mask
= m_bmpStaticReal
.GetMask()->GetBitmap();
362 if (m_bmpStaticReal
.HasPixbuf())
364 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
365 m_bmpStaticReal
.GetPixbuf());
369 gtk_image_set_from_pixmap(GTK_IMAGE(m_widget
),
370 m_bmpStaticReal
.GetPixmap(), mask
);
377 // even if not clearly documented, gdk_pixbuf_animation_get_static_image()
378 // always returns the first frame of the animation
379 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
380 gdk_pixbuf_animation_get_static_image(m_anim
));
384 ClearToBackgroundColour();
389 bool wxAnimationCtrl::IsPlaying() const
391 // NB: we cannot just return m_timer.IsRunning() as this would not
392 // be safe as e.g. if we are displaying a frame forever,
393 // then we are "officially" still playing the animation, but
394 // the timer is not running anymore...
398 wxSize
wxAnimationCtrl::DoGetBestSize() const
400 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
402 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
403 gdk_pixbuf_animation_get_height(m_anim
));
406 return wxSize(100,100);
409 void wxAnimationCtrl::ClearToBackgroundColour()
411 wxSize sz
= GetClientSize();
412 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
413 sz
.GetWidth(), sz
.GetHeight());
417 wxColour clr
= GetBackgroundColour();
418 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
419 gdk_pixbuf_fill(newpix
, col
);
421 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
422 g_object_unref(newpix
);
425 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
427 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
428 // it won't show the background colour unlike the user would expect.
429 // Thus we clear the GtkImage contents to the background colour...
430 if (!wxControl::SetBackgroundColour(colour
))
433 // if not playing the change must take place immediately but
434 // remember that the inactive bitmap has higher priority over the background
435 // colour; DisplayStaticImage() will handle that
437 DisplayStaticImage();
443 //-----------------------------------------------------------------------------
444 // wxAnimationCtrl - event handlers
445 //-----------------------------------------------------------------------------
447 void wxAnimationCtrl::OnTimer(wxTimerEvent
& WXUNUSED(ev
))
449 wxASSERT(m_iter
!= NULL
);
451 // gdk_pixbuf_animation_iter_advance() will automatically restart
452 // the animation, if necessary and we have no way to know !!
453 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
455 // start a new one-shot timer
456 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
458 m_timer
.Start(n
, true);
460 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
461 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
465 // no need to update the m_widget yet
466 m_timer
.Start(10, true);
470 #endif // wxUSE_ANIMATIONCTRL