]>
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"
26 #include "wx/gtk/private.h"
31 // ============================================================================
33 // ============================================================================
35 void gdk_pixbuf_area_updated(GdkPixbufLoader
*loader
,
39 gint
WXUNUSED(height
),
42 if (anim
&& anim
->GetPixbuf() == NULL
)
44 // we need to set the pixbuf only if this is the first time this signal
46 anim
->SetPixbuf(gdk_pixbuf_loader_get_animation(loader
));
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxAnimation
, wxAnimationBase
)
57 wxAnimation::wxAnimation(const wxAnimation
& that
)
60 m_pixbuf
= that
.m_pixbuf
;
62 g_object_ref(m_pixbuf
);
65 wxAnimation::wxAnimation(GdkPixbufAnimation
*p
)
69 g_object_ref(m_pixbuf
);
72 wxAnimation
& wxAnimation::operator=(const wxAnimation
& that
)
76 base_type::operator=(that
);
78 m_pixbuf
= that
.m_pixbuf
;
80 g_object_ref(m_pixbuf
);
85 bool wxAnimation::LoadFile(const wxString
&name
, wxAnimationType
WXUNUSED(type
))
88 m_pixbuf
= gdk_pixbuf_animation_new_from_file(wxGTK_CONV_FN(name
), NULL
);
92 bool wxAnimation::Load(wxInputStream
&stream
, wxAnimationType type
)
99 case wxANIMATION_TYPE_GIF
:
100 strcpy(anim_type
, "gif");
103 case wxANIMATION_TYPE_ANI
:
104 strcpy(anim_type
, "ani");
112 // create a GdkPixbufLoader
113 GError
*error
= NULL
;
114 GdkPixbufLoader
*loader
;
115 if (type
!= wxANIMATION_TYPE_INVALID
&& type
!= wxANIMATION_TYPE_ANY
)
116 loader
= gdk_pixbuf_loader_new_with_type(anim_type
, &error
);
118 loader
= gdk_pixbuf_loader_new();
121 error
!= NULL
) // even if the loader was allocated, an error could have happened
123 wxLogDebug(wxT("Could not create the loader for '%s' animation type: %s"),
124 anim_type
, error
->message
);
128 // connect to loader signals
129 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
132 bool data_written
= false;
133 while (stream
.IsOk())
135 // read a chunk of data
136 if (!stream
.Read(buf
, sizeof(buf
)) &&
137 stream
.GetLastError() != wxSTREAM_EOF
) // EOF is OK for now
139 // gdk_pixbuf_loader_close wants the GError == NULL
140 gdk_pixbuf_loader_close(loader
, NULL
);
144 // fetch all data into the loader
145 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
147 wxLogDebug(wxT("Could not write to the loader: %s"), error
->message
);
149 // gdk_pixbuf_loader_close wants the GError == NULL
150 gdk_pixbuf_loader_close(loader
, NULL
);
159 wxLogDebug("Could not read data from the stream...");
163 // load complete: gdk_pixbuf_loader_close will now check if the data we
164 // wrote inside the pixbuf loader does make sense and will give an error
165 // if it doesn't (because of a truncated file, corrupted data or whatelse)
166 if (!gdk_pixbuf_loader_close(loader
, &error
))
168 wxLogDebug(wxT("Could not close the loader: %s"), error
->message
);
172 // wait until we get the last area_updated signal
176 wxImage
wxAnimation::GetFrame(unsigned int WXUNUSED(frame
)) const
181 wxSize
wxAnimation::GetSize() const
183 return wxSize(gdk_pixbuf_animation_get_width(m_pixbuf
),
184 gdk_pixbuf_animation_get_height(m_pixbuf
));
187 void wxAnimation::UnRef()
190 g_object_unref(m_pixbuf
);
194 void wxAnimation::SetPixbuf(GdkPixbufAnimation
* p
)
199 g_object_ref(m_pixbuf
);
202 //-----------------------------------------------------------------------------
204 //-----------------------------------------------------------------------------
206 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
207 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
208 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
211 void wxAnimationCtrl::Init()
218 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
219 const wxAnimation
& anim
,
223 const wxString
& name
)
225 if (!PreCreation( parent
, pos
, size
) ||
226 !base_type::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
227 wxDefaultValidator
, name
))
229 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
233 SetWindowStyle(style
);
235 m_widget
= gtk_image_new();
236 g_object_ref(m_widget
);
238 m_parent
->DoAddChild( this );
241 SetInitialSize(size
);
246 // init the timer used for animation
247 m_timer
.SetOwner(this);
252 wxAnimationCtrl::~wxAnimationCtrl()
258 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
260 wxFileInputStream
fis(filename
);
263 return Load(fis
, type
);
266 bool wxAnimationCtrl::Load(wxInputStream
& stream
, wxAnimationType type
)
269 if ( !anim
.Load(stream
, type
) || !anim
.IsOk() )
276 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
284 // copy underlying GdkPixbuf object
285 m_anim
= anim
.GetPixbuf();
287 // m_anim may be null in case wxNullAnimation has been passed
290 // add a reference to the GdkPixbufAnimation
291 g_object_ref(m_anim
);
293 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
297 DisplayStaticImage();
300 void wxAnimationCtrl::FitToAnimation()
305 int w
= gdk_pixbuf_animation_get_width(m_anim
),
306 h
= gdk_pixbuf_animation_get_height(m_anim
);
308 // update our size to fit animation
312 void wxAnimationCtrl::ResetAnim()
315 g_object_unref(m_anim
);
319 void wxAnimationCtrl::ResetIter()
322 g_object_unref(m_iter
);
326 bool wxAnimationCtrl::Play()
331 // init the iterator and start a one-shot timer
333 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
336 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
337 // that the timer should not start
338 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
340 m_timer
.Start(n
, true);
345 void wxAnimationCtrl::Stop()
347 // leave current frame displayed until Play() is called again
353 DisplayStaticImage();
356 void wxAnimationCtrl::DisplayStaticImage()
358 wxASSERT(!IsPlaying());
360 // m_bmpStaticReal will be updated only if necessary...
363 if (m_bmpStaticReal
.IsOk())
365 // show inactive bitmap
366 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
367 m_bmpStaticReal
.GetPixbuf());
373 // even if not clearly documented, gdk_pixbuf_animation_get_static_image()
374 // always returns the first frame of the animation
375 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
376 gdk_pixbuf_animation_get_static_image(m_anim
));
380 ClearToBackgroundColour();
385 bool wxAnimationCtrl::IsPlaying() const
387 // NB: we cannot just return m_timer.IsRunning() as this would not
388 // be safe as e.g. if we are displaying a frame forever,
389 // then we are "officially" still playing the animation, but
390 // the timer is not running anymore...
394 wxSize
wxAnimationCtrl::DoGetBestSize() const
396 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
398 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
399 gdk_pixbuf_animation_get_height(m_anim
));
402 return wxSize(100,100);
405 void wxAnimationCtrl::ClearToBackgroundColour()
407 wxSize sz
= GetClientSize();
408 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
409 sz
.GetWidth(), sz
.GetHeight());
413 wxColour clr
= GetBackgroundColour();
414 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
415 gdk_pixbuf_fill(newpix
, col
);
417 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
418 g_object_unref(newpix
);
421 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
423 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
424 // it won't show the background colour unlike the user would expect.
425 // Thus we clear the GtkImage contents to the background colour...
426 if (!wxControl::SetBackgroundColour(colour
))
429 // if not playing the change must take place immediately but
430 // remember that the inactive bitmap has higher priority over the background
431 // colour; DisplayStaticImage() will handle that
433 DisplayStaticImage();
439 //-----------------------------------------------------------------------------
440 // wxAnimationCtrl - event handlers
441 //-----------------------------------------------------------------------------
443 void wxAnimationCtrl::OnTimer(wxTimerEvent
& WXUNUSED(ev
))
445 wxASSERT(m_iter
!= NULL
);
447 // gdk_pixbuf_animation_iter_advance() will automatically restart
448 // the animation, if necessary and we have no way to know !!
449 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
451 // start a new one-shot timer
452 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
454 m_timer
.Start(n
, true);
456 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
457 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
461 // no need to update the m_widget yet
462 m_timer
.Start(10, true);
466 #endif // wxUSE_ANIMATIONCTRL