]> git.saurik.com Git - wxWidgets.git/blame - src/generic/animateg.cpp
minor typo fixes (part of patch 1598005)
[wxWidgets.git] / src / generic / animateg.cpp
CommitLineData
72045d57
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: animateg.cpp
3// Purpose: wxAnimation and wxAnimationCtrl
4// Author: Julian Smart and Guillermo Rodriguez Garcia
5// Modified by: Francesco Montorsi
6// Created: 13/8/99
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Guillermo Rodriguez Garcia
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif //__BORLANDC__
17
5f8047a6 18#if wxUSE_ANIMATIONCTRL && (!defined(__WXGTK20__) || defined(__WXUNIVERSAL__))
72045d57 19
c2f12218
PC
20#include "wx/animate.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
24 #include "wx/image.h"
25 #include "wx/dcmemory.h"
26 #include "wx/dcclient.h"
27 #include "wx/module.h"
28#endif
29
72045d57 30#include "wx/wfstream.h"
72045d57
VZ
31#include "wx/gifdecod.h"
32#include "wx/anidecod.h"
72045d57 33
c2f12218 34#include "wx/listimpl.cpp"
14c0d834 35WX_DEFINE_LIST(wxAnimationDecoderList)
72045d57
VZ
36
37wxAnimationDecoderList wxAnimation::sm_handlers;
38
39
72045d57
VZ
40// ----------------------------------------------------------------------------
41// wxAnimation
42// ----------------------------------------------------------------------------
43
44IMPLEMENT_DYNAMIC_CLASS(wxAnimation, wxAnimationBase)
45#define M_ANIMDATA wx_static_cast(wxAnimationDecoder*, m_refData)
46
47wxSize wxAnimation::GetSize() const
48{
49 wxCHECK_MSG( IsOk(), wxDefaultSize, wxT("invalid animation") );
50
51 return M_ANIMDATA->GetAnimationSize();
52}
53
54size_t wxAnimation::GetFrameCount() const
55{
56 wxCHECK_MSG( IsOk(), 0, wxT("invalid animation") );
57
58 return M_ANIMDATA->GetFrameCount();
59}
60
61wxImage wxAnimation::GetFrame(size_t i) const
62{
63 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid animation") );
64
65 wxImage ret;
66 if (!M_ANIMDATA->ConvertToImage(i, &ret))
67 return wxNullImage;
68 return ret;
69}
70
71int wxAnimation::GetDelay(size_t i) const
72{
73 wxCHECK_MSG( IsOk(), 0, wxT("invalid animation") );
74
75 return M_ANIMDATA->GetDelay(i);
76}
77
78wxPoint wxAnimation::GetFramePosition(size_t frame) const
79{
80 wxCHECK_MSG( IsOk(), wxDefaultPosition, wxT("invalid animation") );
81
82 return M_ANIMDATA->GetFramePosition(frame);
83}
84
05a98b6d
RR
85wxSize wxAnimation::GetFrameSize(size_t frame) const
86{
87 wxCHECK_MSG( IsOk(), wxDefaultSize, wxT("invalid animation") );
88
89 return M_ANIMDATA->GetFrameSize(frame);
90}
91
72045d57
VZ
92wxAnimationDisposal wxAnimation::GetDisposalMethod(size_t frame) const
93{
94 wxCHECK_MSG( IsOk(), wxANIM_UNSPECIFIED, wxT("invalid animation") );
95
96 return M_ANIMDATA->GetDisposalMethod(frame);
97}
98
05a98b6d
RR
99wxColour wxAnimation::GetTransparentColour(size_t frame) const
100{
101 wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid animation") );
102
103 return M_ANIMDATA->GetTransparentColour(frame);
104}
105
72045d57
VZ
106wxColour wxAnimation::GetBackgroundColour() const
107{
108 wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid animation") );
109
110 return M_ANIMDATA->GetBackgroundColour();
111}
112
113bool wxAnimation::LoadFile(const wxString& filename, wxAnimationType type)
114{
115 wxFileInputStream stream(filename);
9d9e3858 116 if ( !stream.IsOk() )
72045d57
VZ
117 return false;
118
119 return Load(stream, type);
120}
121
122bool wxAnimation::Load(wxInputStream &stream, wxAnimationType type)
123{
124 UnRef();
125
126 const wxAnimationDecoder *handler;
127 if ( type == wxANIMATION_TYPE_ANY )
128 {
9d9e3858 129 for ( wxAnimationDecoderList::compatibility_iterator node = sm_handlers.GetFirst();
72045d57
VZ
130 node; node = node->GetNext() )
131 {
132 handler=(const wxAnimationDecoder*)node->GetData();
133
134 if ( handler->CanRead(stream) )
135 {
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);
140 }
141
142 }
143
144 wxLogWarning( _("No handler found for animation type.") );
145 return false;
146 }
147
148 handler = FindHandler(type);
149
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();
153
154 if (handler == NULL)
155 {
156 wxLogWarning( _("No animation handler for type %ld defined."), type );
157
158 return false;
159 }
160
161 if (stream.IsSeekable() && !M_ANIMDATA->CanRead(stream))
162 {
163 wxLogError(_("Animation file is not of type %ld."), type);
164 return false;
165 }
166 else
167 return M_ANIMDATA->Load(stream);
168}
169
170
171// ----------------------------------------------------------------------------
172// animation decoders
173// ----------------------------------------------------------------------------
174
175void wxAnimation::AddHandler( wxAnimationDecoder *handler )
176{
177 // Check for an existing handler of the type being added.
178 if (FindHandler( handler->GetType() ) == 0)
179 {
180 sm_handlers.Append( handler );
181 }
182 else
183 {
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.
189
190 wxLogDebug( _T("Adding duplicate animation handler for '%d' type"),
191 handler->GetType() );
192 delete handler;
193 }
194}
195
196void wxAnimation::InsertHandler( wxAnimationDecoder *handler )
197{
198 // Check for an existing handler of the type being added.
199 if (FindHandler( handler->GetType() ) == 0)
200 {
201 sm_handlers.Insert( handler );
202 }
203 else
204 {
205 // see AddHandler for additional comments.
206 wxLogDebug( _T("Inserting duplicate animation handler for '%d' type"),
207 handler->GetType() );
208 delete handler;
209 }
210}
211
212const wxAnimationDecoder *wxAnimation::FindHandler( wxAnimationType animType )
213{
214 wxAnimationDecoderList::compatibility_iterator node = sm_handlers.GetFirst();
215 while (node)
216 {
217 const wxAnimationDecoder *handler = (const wxAnimationDecoder *)node->GetData();
218 if (handler->GetType() == animType) return handler;
219 node = node->GetNext();
220 }
221 return 0;
222}
223
224void wxAnimation::InitStandardHandlers()
225{
d18868bb 226#if wxUSE_GIF
72045d57 227 AddHandler(new wxGIFDecoder);
d18868bb
WS
228#endif // wxUSE_GIF
229#if wxUSE_ICO_CUR
72045d57 230 AddHandler(new wxANIDecoder);
d18868bb 231#endif // wxUSE_ICO_CUR
72045d57
VZ
232}
233
234void wxAnimation::CleanUpHandlers()
235{
236 wxAnimationDecoderList::compatibility_iterator node = sm_handlers.GetFirst();
237 while (node)
238 {
239 wxAnimationDecoder *handler = (wxAnimationDecoder *)node->GetData();
240 wxAnimationDecoderList::compatibility_iterator next = node->GetNext();
241 delete handler;
242 node = next;
243 }
244
245 sm_handlers.Clear();
246}
247
248
249// A module to allow wxAnimation initialization/cleanup
250// without calling these functions from app.cpp or from
251// the user's application.
252
253class wxAnimationModule: public wxModule
254{
255DECLARE_DYNAMIC_CLASS(wxAnimationModule)
256public:
257 wxAnimationModule() {}
258 bool OnInit() { wxAnimation::InitStandardHandlers(); return true; };
259 void OnExit() { wxAnimation::CleanUpHandlers(); };
260};
261
262IMPLEMENT_DYNAMIC_CLASS(wxAnimationModule, wxModule)
263
264
72045d57
VZ
265// ----------------------------------------------------------------------------
266// wxAnimationCtrl
267// ----------------------------------------------------------------------------
268
269IMPLEMENT_CLASS(wxAnimationCtrl, wxAnimationCtrlBase)
270BEGIN_EVENT_TABLE(wxAnimationCtrl, wxAnimationCtrlBase)
271 EVT_PAINT(wxAnimationCtrl::OnPaint)
272 EVT_SIZE(wxAnimationCtrl::OnSize)
273 EVT_TIMER(wxID_ANY, wxAnimationCtrl::OnTimer)
274END_EVENT_TABLE()
275
05a98b6d 276void wxAnimationCtrl::Init()
c2f12218
PC
277{
278 m_currentFrame = 0;
279 m_looped = false;
280 m_isPlaying = false;
05a98b6d
RR
281
282 // use the window background colour by default to be consistent
283 // with the GTK+ native version
284 m_useWinBackgroundColour = true;
c2f12218
PC
285}
286
72045d57
VZ
287bool wxAnimationCtrl::Create(wxWindow *parent, wxWindowID id,
288 const wxAnimation& animation, const wxPoint& pos,
289 const wxSize& size, long style, const wxString& name)
290{
72045d57
VZ
291 m_timer.SetOwner(this);
292
c2f12218 293 if (!base_type::Create(parent, id, pos, size, style, wxDefaultValidator, name))
72045d57
VZ
294 return false;
295
296 // by default we get the same background colour of our parent
297 SetBackgroundColour(parent->GetBackgroundColour());
4683dc1c
VZ
298
299 SetAnimation(animation);
300
72045d57
VZ
301 return true;
302}
303
304wxAnimationCtrl::~wxAnimationCtrl()
305{
306 Stop();
307}
308
309bool wxAnimationCtrl::LoadFile(const wxString& filename, wxAnimationType type)
310{
311 wxAnimation anim;
9d9e3858 312 if (!anim.LoadFile(filename, type) ||
72045d57
VZ
313 !anim.IsOk())
314 return false;
315
316 SetAnimation(anim);
317 return true;
318}
319
320wxSize wxAnimationCtrl::DoGetBestSize() const
321{
322 if (m_animation.IsOk() && !this->HasFlag(wxAC_NO_AUTORESIZE))
323 return m_animation.GetSize();
324
325 return wxSize(100, 100);
326}
327
328void wxAnimationCtrl::SetAnimation(const wxAnimation& animation)
329{
330 if (IsPlaying())
331 Stop();
332
333 m_animation = animation;
334
335 if (m_animation.GetBackgroundColour() == wxNullColour)
336 SetUseWindowBackgroundColour();
337 if (!this->HasFlag(wxAC_NO_AUTORESIZE))
338 FitToAnimation();
339
8e458bb5 340 // reset frame counter
72045d57 341 m_currentFrame = 0;
72045d57 342
8e458bb5
RR
343 UpdateBackingStoreWithStaticImage();
344}
72045d57 345
8e458bb5
RR
346void wxAnimationCtrl::SetInactiveBitmap(const wxBitmap &bmp)
347{
580ca0a4 348 m_bmpStatic = bmp;
8e458bb5
RR
349
350 // if not playing, update the backing store now
351 if (!IsPlaying())
352 UpdateBackingStoreWithStaticImage();
72045d57
VZ
353}
354
355void wxAnimationCtrl::FitToAnimation()
356{
357 SetSize(m_animation.GetSize());
358}
359
360
361// ----------------------------------------------------------------------------
362// wxAnimationCtrl - stop/play methods
363// ----------------------------------------------------------------------------
364
365void wxAnimationCtrl::Stop()
366{
72045d57
VZ
367 m_timer.Stop();
368 m_isPlaying = false;
8e458bb5
RR
369
370 UpdateBackingStoreWithStaticImage();
72045d57
VZ
371}
372
373bool wxAnimationCtrl::Play(bool looped)
374{
375 if (!m_animation.IsOk())
376 return false;
377
72045d57
VZ
378 m_looped = looped;
379 m_currentFrame = 0;
72045d57 380
8434297d
RD
381 if (!RebuildBackingStoreUpToFrame(0))
382 return false;
05a98b6d
RR
383
384 m_isPlaying = true;
72045d57 385
8e458bb5
RR
386 // do a ClearBackground() to avoid that e.g. the custom static bitmap which
387 // was eventually shown previously remains partially drawn
388 ClearBackground();
389
72045d57
VZ
390 // DrawCurrentFrame() will use our updated backing store
391 wxClientDC clientDC(this);
392 DrawCurrentFrame(clientDC);
393
394 // start the timer
395 int delay = m_animation.GetDelay(0);
396 if (delay == 0)
397 delay = 1; // 0 is invalid timeout for wxTimer.
398 m_timer.Start(delay);
399
400 return true;
401}
402
403
404
405// ----------------------------------------------------------------------------
406// wxAnimationCtrl - rendering methods
407// ----------------------------------------------------------------------------
408
05a98b6d 409bool wxAnimationCtrl::RebuildBackingStoreUpToFrame(size_t frame)
72045d57
VZ
410{
411 // if we've not created the backing store yet or it's too
412 // small, then recreate it
413 wxSize sz = m_animation.GetSize(),
414 winsz = GetClientSize();
415 int w = wxMin(sz.GetWidth(), winsz.GetWidth());
416 int h = wxMin(sz.GetHeight(), winsz.GetHeight());
417
9d9e3858 418 if ( !m_backingStore.IsOk() ||
6003de2f
VZ
419 m_backingStore.GetWidth() < w || m_backingStore.GetHeight() < h )
420 {
05a98b6d
RR
421 if (!m_backingStore.Create(w, h))
422 return false;
6003de2f 423 }
72045d57
VZ
424
425 wxMemoryDC dc;
426 dc.SelectObject(m_backingStore);
427
428 // Draw the background
429 DisposeToBackground(dc);
430
431 // Draw all intermediate frames that haven't been removed from the animation
432 for (size_t i = 0; i < frame; i++)
433 {
434 if (m_animation.GetDisposalMethod(i) == wxANIM_DONOTREMOVE ||
435 m_animation.GetDisposalMethod(i) == wxANIM_UNSPECIFIED)
436 {
437 DrawFrame(dc, i);
438 }
05a98b6d
RR
439 else if (m_animation.GetDisposalMethod(i) == wxANIM_TOBACKGROUND)
440 DisposeToBackground(dc, m_animation.GetFramePosition(i),
441 m_animation.GetFrameSize(i));
72045d57
VZ
442 }
443
444 // finally draw this frame
445 DrawFrame(dc, frame);
446 dc.SelectObject(wxNullBitmap);
05a98b6d
RR
447
448 return true;
72045d57
VZ
449}
450
451void wxAnimationCtrl::IncrementalUpdateBackingStore()
452{
453 wxMemoryDC dc;
454 dc.SelectObject(m_backingStore);
455
456 // OPTIMIZATION:
457 // since wxAnimationCtrl can only play animations forward, without skipping
458 // frames, we can be sure that m_backingStore contains the m_currentFrame-1
459 // frame and thus we just need to dispose the m_currentFrame-1 frame and
460 // render the m_currentFrame-th one.
461
462 if (m_currentFrame == 0)
463 {
464 // before drawing the first frame always dispose to bg colour
465 DisposeToBackground(dc);
466 }
467 else
468 {
469 switch (m_animation.GetDisposalMethod(m_currentFrame-1))
470 {
471 case wxANIM_TOBACKGROUND:
05a98b6d
RR
472 DisposeToBackground(dc, m_animation.GetFramePosition(m_currentFrame-1),
473 m_animation.GetFrameSize(m_currentFrame-1));
72045d57
VZ
474 break;
475
476 case wxANIM_TOPREVIOUS:
477 // this disposal should never be used too often.
478 // E.g. GIF specification explicitely say to keep the usage of this
9d9e3858
VZ
479 // disposal limited to the minimum.
480 // In fact it may require a lot of time to restore
72045d57
VZ
481 if (m_currentFrame == 1)
482 {
483 // if 0-th frame disposal is to restore to previous frame,
484 // the best we can do is to restore to background
485 DisposeToBackground(dc);
486 }
487 else
05a98b6d
RR
488 if (!RebuildBackingStoreUpToFrame(m_currentFrame-2))
489 Stop();
72045d57
VZ
490 break;
491
492 case wxANIM_DONOTREMOVE:
493 case wxANIM_UNSPECIFIED:
494 break;
495 }
496 }
497
498 // now just draw the current frame on the top of the backing store
499 DrawFrame(dc, m_currentFrame);
500 dc.SelectObject(wxNullBitmap);
501}
502
8e458bb5
RR
503void wxAnimationCtrl::UpdateBackingStoreWithStaticImage()
504{
505 wxASSERT(!IsPlaying());
506
507 if (m_bmpStatic.IsOk())
508 {
509 // copy the inactive bitmap in the backing store
510 m_backingStore = m_bmpStatic;
511 }
512 else
513 {
514 // put in the backing store the first frame of the animation
515 if (!m_animation.IsOk() ||
516 !RebuildBackingStoreUpToFrame(0))
517 {
518 m_animation = wxNullAnimation;
519 DisposeToBackground();
520 }
521 }
522
523 Refresh();
524}
525
72045d57
VZ
526void wxAnimationCtrl::DrawFrame(wxDC &dc, size_t frame)
527{
528 // PERFORMANCE NOTE:
529 // this draw stuff is not as fast as possible: the wxAnimationDecoder
530 // needs first to convert from its internal format to wxImage RGB24;
531 // the wxImage is then converted as a wxBitmap and finally blitted.
532 // If wxAnimationDecoder had a function to convert directly from its
533 // internal format to a port-specific wxBitmap, it would be somewhat faster.
534 wxBitmap bmp(m_animation.GetFrame(frame));
535 dc.DrawBitmap(bmp, m_animation.GetFramePosition(frame),
536 true /* use mask */);
537}
538
539void wxAnimationCtrl::DrawCurrentFrame(wxDC& dc)
540{
9d9e3858 541 wxASSERT( m_backingStore.IsOk() );
72045d57
VZ
542
543 // m_backingStore always contains the current frame
8e458bb5
RR
544 dc.DrawBitmap(m_backingStore, 0, 0, true /* use mask in case it's present */);
545}
546
547void wxAnimationCtrl::DisposeToBackground()
548{
549 // clear the backing store
550 wxMemoryDC dc;
551 dc.SelectObject(m_backingStore);
552 DisposeToBackground(dc);
72045d57
VZ
553}
554
555void wxAnimationCtrl::DisposeToBackground(wxDC& dc)
87d2b3f1
CE
556{
557 wxColour col = IsUsingWindowBackgroundColour()
9d9e3858 558 ? GetBackgroundColour()
05a98b6d 559 : m_animation.GetBackgroundColour();
87d2b3f1 560 wxBrush brush(col);
72045d57
VZ
561 dc.SetBackground(brush);
562 dc.Clear();
563}
564
05a98b6d
RR
565void wxAnimationCtrl::DisposeToBackground(wxDC& dc, const wxPoint &pos, const wxSize &sz)
566{
567 wxColour col = IsUsingWindowBackgroundColour()
568 ? GetBackgroundColour()
569 : m_animation.GetBackgroundColour();
570 wxBrush brush(col);
571 dc.SetBrush(brush); // SetBrush and not SetBackground !!
572 dc.SetPen(*wxTRANSPARENT_PEN);
573 dc.DrawRectangle(pos, sz);
574}
575
72045d57
VZ
576// ----------------------------------------------------------------------------
577// wxAnimationCtrl - event handlers
578// ----------------------------------------------------------------------------
579
580void wxAnimationCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
581{
582 // VERY IMPORTANT: the wxPaintDC *must* be created in any case
583 wxPaintDC dc(this);
584
9d9e3858 585 if ( m_backingStore.IsOk() )
72045d57 586 DrawCurrentFrame(dc);
8e458bb5
RR
587 else
588 {
589 // m_animation is not valid and thus we don't have a valid backing store...
590 // clear then our area to the background colour
591 DisposeToBackground(dc);
592 }
72045d57
VZ
593}
594
595void wxAnimationCtrl::OnTimer(wxTimerEvent &WXUNUSED(event))
596{
597 m_currentFrame++;
598 if (m_currentFrame == m_animation.GetFrameCount())
599 {
600 // Should a non-looped animation display the last frame?
601 if (!m_looped)
602 {
8e458bb5 603 Stop();
72045d57
VZ
604 return;
605 }
606 else
607 m_currentFrame = 0; // let's restart
608 }
609
610 IncrementalUpdateBackingStore();
611
612 wxClientDC dc(this);
613 DrawCurrentFrame(dc);
614
7444cb50
VZ
615#ifdef __WXMAC__
616 // without this, the animation currently doesn't redraw under Mac
617 Refresh();
618#endif // __WXMAC__
619
72045d57
VZ
620 // Set the timer for the next frame
621 int delay = m_animation.GetDelay(m_currentFrame);
622 if (delay == 0)
623 delay = 1; // 0 is invalid timeout for wxTimer.
624 m_timer.Start(delay);
625}
626
627void wxAnimationCtrl::OnSize(wxSizeEvent &WXUNUSED(event))
628{
629 // NB: resizing an animation control may take a lot of time
630 // for big animations as the backing store must be
580ca0a4
VZ
631 // extended and rebuilt. Try to avoid it e.g. using
632 // a null proportion value for your wxAnimationCtrls
633 // when using them inside sizers.
72045d57 634 if (m_animation.IsOk())
580ca0a4
VZ
635 {
636 // be careful to change the backing store *only* if we are
637 // playing the animation as otherwise we may be displaying
638 // the inactive bitmap and overwriting the backing store
639 // with the last played frame is wrong in this case
640 if (IsPlaying())
641 {
05a98b6d
RR
642 if (!RebuildBackingStoreUpToFrame(m_currentFrame))
643 Stop(); // in case we are playing
580ca0a4
VZ
644 }
645 }
72045d57
VZ
646}
647
648#endif // wxUSE_ANIMATIONCTRL
649