]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/animate.cpp
df942e3c1a79013ebff1925e26f3ba48f0b85e46
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 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
20 #if wxUSE_ANIMATIONCTRL
22 #include "wx/animate.h"
24 #include <gtk/gtkimage.h>
27 // ============================================================================
29 // ============================================================================
31 void gdk_pixbuf_area_updated(GdkPixbufLoader
*loader
,
38 if (anim
&& anim
->GetPixbuf() == NULL
)
40 // we need to set the pixbuf only if this is the first time this signal
42 anim
->SetPixbuf(gdk_pixbuf_loader_get_animation(loader
));
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 IMPLEMENT_DYNAMIC_CLASS(wxAnimation
, wxAnimationBase
)
53 bool wxAnimation::LoadFile(const wxString
&name
, wxAnimationType
WXUNUSED(type
))
56 m_pixbuf
= gdk_pixbuf_animation_new_from_file(name
.c_str(), NULL
);
60 bool wxAnimation::Load(wxInputStream
&stream
, wxAnimationType type
)
67 case wxANIMATION_TYPE_GIF
:
68 strcpy(anim_type
, "gif");
71 case wxANIMATION_TYPE_ANI
:
72 strcpy(anim_type
, "ani");
79 // create a GdkPixbufLoader
81 GdkPixbufLoader
*loader
;
82 if (type
!= wxANIMATION_TYPE_INVALID
&& type
!= wxANIMATION_TYPE_ANY
)
83 loader
= gdk_pixbuf_loader_new_with_type(anim_type
, &error
);
85 loader
= gdk_pixbuf_loader_new();
89 wxLogDebug(wxT("Could not create the loader for '%s' animation type"), anim_type
);
93 // connect to loader signals
94 g_signal_connect(loader
, "area-updated", G_CALLBACK(gdk_pixbuf_area_updated
), this);
96 //m_bLoadComplete = false;
100 // read a chunk of data
101 stream
.Read(buf
, 2048);
103 // fetch all data into the loader
104 if (!gdk_pixbuf_loader_write(loader
, buf
, stream
.LastRead(), &error
))
106 gdk_pixbuf_loader_close(loader
, &error
);
107 wxLogDebug(wxT("Could not write to the loader"));
113 if (!gdk_pixbuf_loader_close(loader
, &error
))
115 wxLogDebug(wxT("Could not close the loader"));
118 //m_bLoadComplete = true;
120 // wait until we get the last area_updated signal
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
130 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
131 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
134 bool wxAnimationCtrl::Create( wxWindow
*parent
, wxWindowID id
,
135 const wxAnimation
& anim
,
139 const wxString
& name
)
142 m_acceptsFocus
= true;
144 if (!PreCreation( parent
, pos
, size
) ||
145 !wxControl::CreateBase(parent
, id
, pos
, size
, style
& wxWINDOW_STYLE_MASK
,
146 wxDefaultValidator
, name
))
148 wxFAIL_MSG( wxT("wxAnimationCtrl creation failed") );
152 SetWindowStyle(style
);
154 m_widget
= gtk_image_new();
155 gtk_widget_show( GTK_WIDGET(m_widget
) );
157 m_parent
->DoAddChild( this );
165 if (anim
!= wxNullAnimation
)
168 // init the timer used for animation
169 m_timer
.SetOwner(this);
174 wxAnimationCtrl::~wxAnimationCtrl()
180 bool wxAnimationCtrl::LoadFile(const wxString
&filename
, wxAnimationType type
)
183 if (!anim
.LoadFile(filename
, type
))
190 void wxAnimationCtrl::SetAnimation(const wxAnimation
&anim
)
198 // copy underlying GdkPixbuf object
199 m_anim
= anim
.GetPixbuf();
201 // m_anim may be null in case wxNullAnimation has been passed
204 // add a reference to the GdkPixbufAnimation
205 g_object_ref(m_anim
);
207 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
210 // display first frame
211 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
212 gdk_pixbuf_animation_get_static_image(m_anim
));
216 // we need to clear the control to the background colour
217 ClearToBackgroundColour();
221 void wxAnimationCtrl::FitToAnimation()
226 int w
= gdk_pixbuf_animation_get_width(m_anim
),
227 h
= gdk_pixbuf_animation_get_height(m_anim
);
229 // update our size to fit animation
230 //if (w > 0 && h > 0)
231 // gtk_widget_set_size_request(m_widget, w, h);
235 bool wxAnimationCtrl::Play()
240 // init the iterator and start a one-shot timer
242 m_iter
= gdk_pixbuf_animation_get_iter (m_anim
, NULL
);
245 // gdk_pixbuf_animation_iter_get_delay_time() may return -1 which means
246 // that the timer should not start
247 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
249 m_timer
.Start(n
, true);
254 void wxAnimationCtrl::Stop()
256 // leave current frame displayed until Play() is called again
262 bool wxAnimationCtrl::IsPlaying() const
264 // NB: we cannot just return m_timer.IsRunning() as this would not
265 // be safe as e.g. if we are displaying a frame forever,
266 // then we are "officially" still playing the animation, but
267 // the timer is not running anymore...
271 wxSize
wxAnimationCtrl::DoGetBestSize() const
273 if (m_anim
&& !this->HasFlag(wxAC_NO_AUTORESIZE
))
275 return wxSize(gdk_pixbuf_animation_get_width(m_anim
),
276 gdk_pixbuf_animation_get_height(m_anim
));
279 return wxSize(100,100);
282 void wxAnimationCtrl::ClearToBackgroundColour()
284 wxSize sz
= GetClientSize();
285 GdkPixbuf
*newpix
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8,
286 sz
.GetWidth(), sz
.GetHeight());
290 wxColour clr
= GetBackgroundColour();
291 guint32 col
= (clr
.Red() << 24) | (clr
.Green() << 16) | (clr
.Blue() << 8);
292 gdk_pixbuf_fill(newpix
, col
);
294 wxLogDebug(wxT("Clearing to background %s"), clr
.GetAsString().c_str());
296 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
), newpix
);
297 g_object_unref(newpix
);
300 bool wxAnimationCtrl::SetBackgroundColour( const wxColour
&colour
)
302 // wxWindowGTK::SetBackgroundColour works but since our m_widget is a GtkImage
303 // it won't show the background colour unlike the user would expect.
304 // Thus we clear the GtkImage contents to the background colour...
305 if (!wxControl::SetBackgroundColour(colour
))
307 ClearToBackgroundColour();
312 //-----------------------------------------------------------------------------
313 // wxAnimationCtrl - event handlers
314 //-----------------------------------------------------------------------------
316 void wxAnimationCtrl::OnTimer(wxTimerEvent
&ev
)
318 wxASSERT(m_iter
!= NULL
);
320 // gdk_pixbuf_animation_iter_advance() will automatically restart
321 // the animation, if necessary and we have no way to know !!
322 if (gdk_pixbuf_animation_iter_advance(m_iter
, NULL
))
324 // start a new one-shot timer
325 int n
= gdk_pixbuf_animation_iter_get_delay_time(m_iter
);
327 m_timer
.Start(n
, true);
329 gtk_image_set_from_pixbuf(GTK_IMAGE(m_widget
),
330 gdk_pixbuf_animation_iter_get_pixbuf(m_iter
));
334 // no need to update the m_widget yet
335 m_timer
.Start(10, true);
339 #endif // wxUSE_ANIMATIONCTRL