1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxAnimation and wxAnimationCtrl
4 // Author: Julian Smart and Guillermo Rodriguez Garcia
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) Julian Smart and Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #if wxUSE_ANIMATIONCTRL && (!defined(__WXGTK20__) || defined(__WXUNIVERSAL__))
20 #include "wx/animate.h"
25 #include "wx/dcmemory.h"
26 #include "wx/dcclient.h"
27 #include "wx/module.h"
30 #include "wx/wfstream.h"
31 #include "wx/gifdecod.h"
32 #include "wx/anidecod.h"
34 #include "wx/listimpl.cpp"
35 WX_DEFINE_LIST(wxAnimationDecoderList
)
37 wxAnimationDecoderList
wxAnimation::sm_handlers
;
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 IMPLEMENT_DYNAMIC_CLASS(wxAnimation
, wxAnimationBase
)
45 #define M_ANIMDATA wx_static_cast(wxAnimationDecoder*, m_refData)
47 wxSize
wxAnimation::GetSize() const
49 wxCHECK_MSG( IsOk(), wxDefaultSize
, wxT("invalid animation") );
51 return M_ANIMDATA
->GetAnimationSize();
54 size_t wxAnimation::GetFrameCount() const
56 wxCHECK_MSG( IsOk(), 0, wxT("invalid animation") );
58 return M_ANIMDATA
->GetFrameCount();
61 wxImage
wxAnimation::GetFrame(size_t i
) const
63 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid animation") );
66 if (!M_ANIMDATA
->ConvertToImage(i
, &ret
))
71 int wxAnimation::GetDelay(size_t i
) const
73 wxCHECK_MSG( IsOk(), 0, wxT("invalid animation") );
75 return M_ANIMDATA
->GetDelay(i
);
78 wxPoint
wxAnimation::GetFramePosition(size_t frame
) const
80 wxCHECK_MSG( IsOk(), wxDefaultPosition
, wxT("invalid animation") );
82 return M_ANIMDATA
->GetFramePosition(frame
);
85 wxSize
wxAnimation::GetFrameSize(size_t frame
) const
87 wxCHECK_MSG( IsOk(), wxDefaultSize
, wxT("invalid animation") );
89 return M_ANIMDATA
->GetFrameSize(frame
);
92 wxAnimationDisposal
wxAnimation::GetDisposalMethod(size_t frame
) const
94 wxCHECK_MSG( IsOk(), wxANIM_UNSPECIFIED
, wxT("invalid animation") );
96 return M_ANIMDATA
->GetDisposalMethod(frame
);
99 wxColour
wxAnimation::GetTransparentColour(size_t frame
) const
101 wxCHECK_MSG( IsOk(), wxNullColour
, wxT("invalid animation") );
103 return M_ANIMDATA
->GetTransparentColour(frame
);
106 wxColour
wxAnimation::GetBackgroundColour() const
108 wxCHECK_MSG( IsOk(), wxNullColour
, wxT("invalid animation") );
110 return M_ANIMDATA
->GetBackgroundColour();
113 bool wxAnimation::LoadFile(const wxString
& filename
, wxAnimationType type
)
115 wxFileInputStream
stream(filename
);
116 if ( !stream
.IsOk() )
119 return Load(stream
, type
);
122 bool wxAnimation::Load(wxInputStream
&stream
, wxAnimationType type
)
126 const wxAnimationDecoder
*handler
;
127 if ( type
== wxANIMATION_TYPE_ANY
)
129 for ( wxAnimationDecoderList::compatibility_iterator node
= sm_handlers
.GetFirst();
130 node
; node
= node
->GetNext() )
132 handler
=(const wxAnimationDecoder
*)node
->GetData();
134 if ( handler
->CanRead(stream
) )
136 // do a copy of the handler from the static list which we will own
137 // as our reference data
138 m_refData
= handler
->Clone();
139 return M_ANIMDATA
->Load(stream
);
144 wxLogWarning( _("No handler found for animation type.") );
148 handler
= FindHandler(type
);
150 // do a copy of the handler from the static list which we will own
151 // as our reference data
152 m_refData
= handler
->Clone();
156 wxLogWarning( _("No animation handler for type %ld defined."), type
);
161 if (stream
.IsSeekable() && !M_ANIMDATA
->CanRead(stream
))
163 wxLogError(_("Animation file is not of type %ld."), type
);
167 return M_ANIMDATA
->Load(stream
);
171 // ----------------------------------------------------------------------------
172 // animation decoders
173 // ----------------------------------------------------------------------------
175 void wxAnimation::AddHandler( wxAnimationDecoder
*handler
)
177 // Check for an existing handler of the type being added.
178 if (FindHandler( handler
->GetType() ) == 0)
180 sm_handlers
.Append( handler
);
184 // This is not documented behaviour, merely the simplest 'fix'
185 // for preventing duplicate additions. If someone ever has
186 // a good reason to add and remove duplicate handlers (and they
187 // may) we should probably refcount the duplicates.
188 // also an issue in InsertHandler below.
190 wxLogDebug( _T("Adding duplicate animation handler for '%d' type"),
191 handler
->GetType() );
196 void wxAnimation::InsertHandler( wxAnimationDecoder
*handler
)
198 // Check for an existing handler of the type being added.
199 if (FindHandler( handler
->GetType() ) == 0)
201 sm_handlers
.Insert( handler
);
205 // see AddHandler for additional comments.
206 wxLogDebug( _T("Inserting duplicate animation handler for '%d' type"),
207 handler
->GetType() );
212 const wxAnimationDecoder
*wxAnimation::FindHandler( wxAnimationType animType
)
214 wxAnimationDecoderList::compatibility_iterator node
= sm_handlers
.GetFirst();
217 const wxAnimationDecoder
*handler
= (const wxAnimationDecoder
*)node
->GetData();
218 if (handler
->GetType() == animType
) return handler
;
219 node
= node
->GetNext();
224 void wxAnimation::InitStandardHandlers()
227 AddHandler(new wxGIFDecoder
);
230 AddHandler(new wxANIDecoder
);
231 #endif // wxUSE_ICO_CUR
234 void wxAnimation::CleanUpHandlers()
236 wxAnimationDecoderList::compatibility_iterator node
= sm_handlers
.GetFirst();
239 wxAnimationDecoder
*handler
= (wxAnimationDecoder
*)node
->GetData();
240 wxAnimationDecoderList::compatibility_iterator next
= node
->GetNext();
249 // A module to allow wxAnimation initialization/cleanup
250 // without calling these functions from app.cpp or from
251 // the user's application.
253 class wxAnimationModule
: public wxModule
255 DECLARE_DYNAMIC_CLASS(wxAnimationModule
)
257 wxAnimationModule() {}
258 bool OnInit() { wxAnimation::InitStandardHandlers(); return true; };
259 void OnExit() { wxAnimation::CleanUpHandlers(); };
262 IMPLEMENT_DYNAMIC_CLASS(wxAnimationModule
, wxModule
)
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 IMPLEMENT_CLASS(wxAnimationCtrl
, wxAnimationCtrlBase
)
270 BEGIN_EVENT_TABLE(wxAnimationCtrl
, wxAnimationCtrlBase
)
271 EVT_PAINT(wxAnimationCtrl::OnPaint
)
272 EVT_SIZE(wxAnimationCtrl::OnSize
)
273 EVT_TIMER(wxID_ANY
, wxAnimationCtrl::OnTimer
)
276 void wxAnimationCtrl::Init()
282 // use the window background colour by default to be consistent
283 // with the GTK+ native version
284 m_useWinBackgroundColour
= true;
287 bool wxAnimationCtrl::Create(wxWindow
*parent
, wxWindowID id
,
288 const wxAnimation
& animation
, const wxPoint
& pos
,
289 const wxSize
& size
, long style
, const wxString
& name
)
291 m_animation
= animation
;
292 m_timer
.SetOwner(this);
294 if (!base_type::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
297 // by default we get the same background colour of our parent
298 SetBackgroundColour(parent
->GetBackgroundColour());
302 wxAnimationCtrl::~wxAnimationCtrl()
307 bool wxAnimationCtrl::LoadFile(const wxString
& filename
, wxAnimationType type
)
310 if (!anim
.LoadFile(filename
, type
) ||
318 wxSize
wxAnimationCtrl::DoGetBestSize() const
320 if (m_animation
.IsOk() && !this->HasFlag(wxAC_NO_AUTORESIZE
))
321 return m_animation
.GetSize();
323 return wxSize(100, 100);
326 void wxAnimationCtrl::SetAnimation(const wxAnimation
& animation
)
331 m_animation
= animation
;
333 if (m_animation
.GetBackgroundColour() == wxNullColour
)
334 SetUseWindowBackgroundColour();
335 if (!this->HasFlag(wxAC_NO_AUTORESIZE
))
338 // reset frame counter
341 UpdateBackingStoreWithStaticImage();
344 void wxAnimationCtrl::SetInactiveBitmap(const wxBitmap
&bmp
)
346 wxAnimationCtrlBase::SetInactiveBitmap(bmp
);
348 // if not playing, update the backing store now
350 UpdateBackingStoreWithStaticImage();
353 void wxAnimationCtrl::FitToAnimation()
355 SetSize(m_animation
.GetSize());
359 // ----------------------------------------------------------------------------
360 // wxAnimationCtrl - stop/play methods
361 // ----------------------------------------------------------------------------
363 void wxAnimationCtrl::Stop()
368 UpdateBackingStoreWithStaticImage();
371 bool wxAnimationCtrl::Play(bool looped
)
373 if (!m_animation
.IsOk())
376 int oldframe
= m_currentFrame
;
380 // small optimization: if the back store was already updated to the
381 // first frame, don't rebuild it
383 if (!RebuildBackingStoreUpToFrame(0))
388 // do a ClearBackground() to avoid that e.g. the custom static bitmap which
389 // was eventually shown previously remains partially drawn
392 // DrawCurrentFrame() will use our updated backing store
393 wxClientDC
clientDC(this);
394 DrawCurrentFrame(clientDC
);
397 int delay
= m_animation
.GetDelay(0);
399 delay
= 1; // 0 is invalid timeout for wxTimer.
400 m_timer
.Start(delay
);
407 // ----------------------------------------------------------------------------
408 // wxAnimationCtrl - rendering methods
409 // ----------------------------------------------------------------------------
411 bool wxAnimationCtrl::RebuildBackingStoreUpToFrame(size_t frame
)
413 // if we've not created the backing store yet or it's too
414 // small, then recreate it
415 wxSize sz
= m_animation
.GetSize(),
416 winsz
= GetClientSize();
417 int w
= wxMin(sz
.GetWidth(), winsz
.GetWidth());
418 int h
= wxMin(sz
.GetHeight(), winsz
.GetHeight());
420 if ( !m_backingStore
.IsOk() ||
421 m_backingStore
.GetWidth() < w
|| m_backingStore
.GetHeight() < h
)
423 if (!m_backingStore
.Create(w
, h
))
428 dc
.SelectObject(m_backingStore
);
430 // Draw the background
431 DisposeToBackground(dc
);
433 // Draw all intermediate frames that haven't been removed from the animation
434 for (size_t i
= 0; i
< frame
; i
++)
436 if (m_animation
.GetDisposalMethod(i
) == wxANIM_DONOTREMOVE
||
437 m_animation
.GetDisposalMethod(i
) == wxANIM_UNSPECIFIED
)
441 else if (m_animation
.GetDisposalMethod(i
) == wxANIM_TOBACKGROUND
)
442 DisposeToBackground(dc
, m_animation
.GetFramePosition(i
),
443 m_animation
.GetFrameSize(i
));
446 // finally draw this frame
447 DrawFrame(dc
, frame
);
448 dc
.SelectObject(wxNullBitmap
);
453 void wxAnimationCtrl::IncrementalUpdateBackingStore()
456 dc
.SelectObject(m_backingStore
);
459 // since wxAnimationCtrl can only play animations forward, without skipping
460 // frames, we can be sure that m_backingStore contains the m_currentFrame-1
461 // frame and thus we just need to dispose the m_currentFrame-1 frame and
462 // render the m_currentFrame-th one.
464 if (m_currentFrame
== 0)
466 // before drawing the first frame always dispose to bg colour
467 DisposeToBackground(dc
);
471 switch (m_animation
.GetDisposalMethod(m_currentFrame
-1))
473 case wxANIM_TOBACKGROUND
:
474 DisposeToBackground(dc
, m_animation
.GetFramePosition(m_currentFrame
-1),
475 m_animation
.GetFrameSize(m_currentFrame
-1));
478 case wxANIM_TOPREVIOUS
:
479 // this disposal should never be used too often.
480 // E.g. GIF specification explicitely say to keep the usage of this
481 // disposal limited to the minimum.
482 // In fact it may require a lot of time to restore
483 if (m_currentFrame
== 1)
485 // if 0-th frame disposal is to restore to previous frame,
486 // the best we can do is to restore to background
487 DisposeToBackground(dc
);
490 if (!RebuildBackingStoreUpToFrame(m_currentFrame
-2))
494 case wxANIM_DONOTREMOVE
:
495 case wxANIM_UNSPECIFIED
:
500 // now just draw the current frame on the top of the backing store
501 DrawFrame(dc
, m_currentFrame
);
502 dc
.SelectObject(wxNullBitmap
);
505 void wxAnimationCtrl::UpdateBackingStoreWithStaticImage()
507 wxASSERT(!IsPlaying());
509 if (m_bmpStatic
.IsOk())
511 // copy the inactive bitmap in the backing store
512 m_backingStore
= m_bmpStatic
;
516 // put in the backing store the first frame of the animation
517 if (!m_animation
.IsOk() ||
518 !RebuildBackingStoreUpToFrame(0))
520 m_animation
= wxNullAnimation
;
521 DisposeToBackground();
528 void wxAnimationCtrl::DrawFrame(wxDC
&dc
, size_t frame
)
531 // this draw stuff is not as fast as possible: the wxAnimationDecoder
532 // needs first to convert from its internal format to wxImage RGB24;
533 // the wxImage is then converted as a wxBitmap and finally blitted.
534 // If wxAnimationDecoder had a function to convert directly from its
535 // internal format to a port-specific wxBitmap, it would be somewhat faster.
536 wxBitmap
bmp(m_animation
.GetFrame(frame
));
537 dc
.DrawBitmap(bmp
, m_animation
.GetFramePosition(frame
),
538 true /* use mask */);
541 void wxAnimationCtrl::DrawCurrentFrame(wxDC
& dc
)
543 wxASSERT( m_backingStore
.IsOk() );
545 // m_backingStore always contains the current frame
546 dc
.DrawBitmap(m_backingStore
, 0, 0, true /* use mask in case it's present */);
549 void wxAnimationCtrl::DisposeToBackground()
551 // clear the backing store
553 dc
.SelectObject(m_backingStore
);
554 DisposeToBackground(dc
);
557 void wxAnimationCtrl::DisposeToBackground(wxDC
& dc
)
559 wxColour col
= IsUsingWindowBackgroundColour()
560 ? GetBackgroundColour()
561 : m_animation
.GetBackgroundColour();
563 dc
.SetBackground(brush
);
567 void wxAnimationCtrl::DisposeToBackground(wxDC
& dc
, const wxPoint
&pos
, const wxSize
&sz
)
569 wxColour col
= IsUsingWindowBackgroundColour()
570 ? GetBackgroundColour()
571 : m_animation
.GetBackgroundColour();
573 dc
.SetBrush(brush
); // SetBrush and not SetBackground !!
574 dc
.SetPen(*wxTRANSPARENT_PEN
);
575 dc
.DrawRectangle(pos
, sz
);
578 // ----------------------------------------------------------------------------
579 // wxAnimationCtrl - event handlers
580 // ----------------------------------------------------------------------------
582 void wxAnimationCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
584 // VERY IMPORTANT: the wxPaintDC *must* be created in any case
587 if ( m_backingStore
.IsOk() )
588 DrawCurrentFrame(dc
);
591 // m_animation is not valid and thus we don't have a valid backing store...
592 // clear then our area to the background colour
593 DisposeToBackground(dc
);
597 void wxAnimationCtrl::OnTimer(wxTimerEvent
&WXUNUSED(event
))
600 if (m_currentFrame
== m_animation
.GetFrameCount())
602 // Should a non-looped animation display the last frame?
609 m_currentFrame
= 0; // let's restart
612 IncrementalUpdateBackingStore();
615 DrawCurrentFrame(dc
);
618 // without this, the animation currently doesn't redraw under Mac
622 // Set the timer for the next frame
623 int delay
= m_animation
.GetDelay(m_currentFrame
);
625 delay
= 1; // 0 is invalid timeout for wxTimer.
626 m_timer
.Start(delay
);
629 void wxAnimationCtrl::OnSize(wxSizeEvent
&WXUNUSED(event
))
631 // NB: resizing an animation control may take a lot of time
632 // for big animations as the backing store must be
633 // extended and rebuilt. Try to avoid it!!
634 if (m_animation
.IsOk())
635 if (!RebuildBackingStoreUpToFrame(m_currentFrame
))
636 Stop(); // in case we are playing
639 #endif // wxUSE_ANIMATIONCTRL