]>
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 void wxAnimationCtrl::Init()
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 );
218 if (anim
!= wxNullAnimation
)
221 // init the timer used for animation
222 m_timer
.SetOwner(this);
227 wxAnimationCtrl::~wxAnimationCtrl()
233 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
236 if (!anim
.LoadFile(filename
, type
))
243 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
251 // copy underlying GdkPixbuf object
252 m_anim
= anim
.GetPixbuf();
254 // m_anim may be null in case wxNullAnimation has been passed
257 // add a reference to the GdkPixbufAnimation
258 g_object_ref(m_anim
);
260 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
263 // display first frame
264 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
265 gdk_pixbuf_animation_get_static_image(m_anim
));
269 // we need to clear the control to the background colour
270 ClearToBackgroundColour();
274 void wxAnimationCtrl::FitToAnimation()
279 int w
= gdk_pixbuf_animation_get_width(m_anim
),
280 h
= gdk_pixbuf_animation_get_height(m_anim
);
282 // update our size to fit animation
283 //if (w > 0 && h > 0)
284 // gtk_widget_set_size_request(m_widget, w, h);
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 bool wxAnimationCtrl::IsPlaying() const
331 // NB: we cannot just return m_timer.IsRunning() as this would not
332 // be safe as e.g. if we are displaying a frame forever,
333 // then we are "officially" still playing the animation, but
334 // the timer is not running anymore...
338 wxSize
wxAnimationCtrl::DoGetBestSize() const
340 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
342 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
343 gdk_pixbuf_animation_get_height(m_anim
));
346 return wxSize(100,100);
349 void wxAnimationCtrl::ClearToBackgroundColour()
351 wxSize sz
= GetClientSize();
352 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
353 sz
.GetWidth(), sz
.GetHeight());
357 wxColour clr
= GetBackgroundColour();
358 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
359 gdk_pixbuf_fill(newpix
, col
);
361 wxLogDebug(wxT("Clearing to background %s"), clr
.GetAsString().c_str());
363 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
364 g_object_unref(newpix
);
367 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
369 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
370 // it won't show the background colour unlike the user would expect.
371 // Thus we clear the GtkImage contents to the background colour...
372 if (!wxControl::SetBackgroundColour(colour
))
374 ClearToBackgroundColour();
379 //-----------------------------------------------------------------------------
380 // wxAnimationCtrl - event handlers
381 //-----------------------------------------------------------------------------
383 void wxAnimationCtrl::OnTimer(wxTimerEvent
&ev
)
385 wxASSERT(m_iter
!= NULL
);
387 // gdk_pixbuf_animation_iter_advance() will automatically restart
388 // the animation, if necessary and we have no way to know !!
389 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
391 // start a new one-shot timer
392 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
394 m_timer
.Start(n
, true);
396 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
397 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
401 // no need to update the m_widget yet
402 m_timer
.Start(10, true);
406 #endif // wxUSE_ANIMATIONCTRL