]>
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
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::operator=(const wxAnimation
& that
)
66 base_type::operator=(that
);
68 m_pixbuf
= that
.m_pixbuf
;
70 g_object_ref(m_pixbuf
);
75 bool wxAnimation::LoadFile(const wxString
&name
, wxAnimationType
WXUNUSED(type
))
78 m_pixbuf
= gdk_pixbuf_animation_new_from_file(
79 wxConvFileName
->cWX2MB(name
), NULL
);
83 bool wxAnimation::Load(wxInputStream
&stream
, wxAnimationType type
)
90 case wxANIMATION_TYPE_GIF
:
91 strcpy(anim_type
, "gif");
94 case wxANIMATION_TYPE_ANI
:
95 strcpy(anim_type
, "ani");
103 // create a GdkPixbufLoader
104 GError
*error
= NULL
;
105 GdkPixbufLoader
*loader
;
106 if (type
!= wxANIMATION_TYPE_INVALID
&& type
!= wxANIMATION_TYPE_ANY
)
107 loader
= gdk_pixbuf_loader_new_with_type(anim_type
, &error
);
109 loader
= gdk_pixbuf_loader_new();
113 wxLogDebug(wxT("Could not create the loader for '%s' animation type"), anim_type
);
117 // connect to loader signals
118 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
120 //m_bLoadComplete = false;
122 while (stream
.IsOk())
124 // read a chunk of data
125 stream
.Read(buf
, sizeof(buf
));
127 // fetch all data into the loader
128 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
130 gdk_pixbuf_loader_close(loader
, &error
);
131 wxLogDebug(wxT("Could not write to the loader"));
137 if (!gdk_pixbuf_loader_close(loader
, &error
))
139 wxLogDebug(wxT("Could not close the loader"));
142 //m_bLoadComplete = true;
144 // wait until we get the last area_updated signal
148 wxImage
wxAnimation::GetFrame(size_t i
) const
153 wxSize
wxAnimation::GetSize() const
155 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf
),
156 gdk_pixbuf_animation_get_height(m_pixbuf
));
159 void wxAnimation::UnRef()
162 g_object_unref(m_pixbuf
);
166 void wxAnimation::SetPixbuf(GdkPixbufAnimation
* p
)
171 g_object_ref(m_pixbuf
);
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
178 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
179 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
180 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
183 wxAnimationCtrl::wxAnimationCtrl()
190 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
191 const wxAnimation
& anim
,
195 const wxString
& name
)
198 m_acceptsFocus
= true;
200 if (!PreCreation( parent
, pos
, size
) ||
201 !base_type::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
202 wxDefaultValidator
, name
))
204 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
208 SetWindowStyle(style
);
210 m_widget
= gtk_image_new();
211 gtk_widget_show( GTK_WIDGET(m_widget
) );
213 m_parent
->DoAddChild( this );
221 if (anim
!= wxNullAnimation
)
224 // init the timer used for animation
225 m_timer
.SetOwner(this);
230 wxAnimationCtrl::~wxAnimationCtrl()
236 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
239 if (!anim
.LoadFile(filename
, type
))
246 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
254 // copy underlying GdkPixbuf object
255 m_anim
= anim
.GetPixbuf();
257 // m_anim may be null in case wxNullAnimation has been passed
260 // add a reference to the GdkPixbufAnimation
261 g_object_ref(m_anim
);
263 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
266 // display first frame
267 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
268 gdk_pixbuf_animation_get_static_image(m_anim
));
272 // we need to clear the control to the background colour
273 ClearToBackgroundColour();
277 void wxAnimationCtrl::FitToAnimation()
282 int w
= gdk_pixbuf_animation_get_width(m_anim
),
283 h
= gdk_pixbuf_animation_get_height(m_anim
);
285 // update our size to fit animation
286 //if (w > 0 && h > 0)
287 // gtk_widget_set_size_request(m_widget, w, h);
291 void wxAnimationCtrl::ResetAnim()
294 g_object_unref(m_anim
);
298 void wxAnimationCtrl::ResetIter()
301 g_object_unref(m_iter
);
305 bool wxAnimationCtrl::Play()
310 // init the iterator and start a one-shot timer
312 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
315 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
316 // that the timer should not start
317 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
319 m_timer
.Start(n
, true);
324 void wxAnimationCtrl::Stop()
326 // leave current frame displayed until Play() is called again
332 bool wxAnimationCtrl::IsPlaying() const
334 // NB: we cannot just return m_timer.IsRunning() as this would not
335 // be safe as e.g. if we are displaying a frame forever,
336 // then we are "officially" still playing the animation, but
337 // the timer is not running anymore...
341 wxSize
wxAnimationCtrl::DoGetBestSize() const
343 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
345 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
346 gdk_pixbuf_animation_get_height(m_anim
));
349 return wxSize(100,100);
352 void wxAnimationCtrl::ClearToBackgroundColour()
354 wxSize sz
= GetClientSize();
355 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
356 sz
.GetWidth(), sz
.GetHeight());
360 wxColour clr
= GetBackgroundColour();
361 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
362 gdk_pixbuf_fill(newpix
, col
);
364 wxLogDebug(wxT("Clearing to background %s"), clr
.GetAsString().c_str());
366 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
367 g_object_unref(newpix
);
370 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
372 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
373 // it won't show the background colour unlike the user would expect.
374 // Thus we clear the GtkImage contents to the background colour...
375 if (!wxControl::SetBackgroundColour(colour
))
377 ClearToBackgroundColour();
382 //-----------------------------------------------------------------------------
383 // wxAnimationCtrl - event handlers
384 //-----------------------------------------------------------------------------
386 void wxAnimationCtrl::OnTimer(wxTimerEvent
&ev
)
388 wxASSERT(m_iter
!= NULL
);
390 // gdk_pixbuf_animation_iter_advance() will automatically restart
391 // the animation, if necessary and we have no way to know !!
392 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
394 // start a new one-shot timer
395 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
397 m_timer
.Start(n
, true);
399 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
400 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
404 // no need to update the m_widget yet
405 m_timer
.Start(10, true);
409 #endif // wxUSE_ANIMATIONCTRL