]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/animate/animate.cpp
fixes for animated GIFs playing (patch 1097003)
[wxWidgets.git] / contrib / src / animate / animate.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: animate.cpp
3 // Purpose: Implementation of wxAnimation classes
4 // Author: Julian Smart and Guillermo Rodriguez Garcia
5 // Modified by:
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 #ifdef __GNUG__
13 #pragma implementation "animate.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif //__BORLANDC__
21
22 #include "wx/wfstream.h"
23 #include "wx/image.h"
24 #include "wx/gifdecod.h"
25 #include "wx/log.h"
26 #include "wx/dcmemory.h"
27 #include "wx/animate/animate.h"
28 #include "wx/dc.h"
29 #include "wx/dcclient.h"
30
31 /*
32 * wxAnimationPlayer
33 */
34
35 IMPLEMENT_CLASS(wxAnimationPlayer, wxObject)
36
37 wxAnimationPlayer::wxAnimationPlayer(wxAnimationBase *animation, bool destroyAnimation)
38 {
39 m_animation = animation;
40 m_destroyAnimation = destroyAnimation;
41 m_currentFrame = 0;
42 m_window = (wxWindow*) NULL;
43 m_position = wxPoint(0, 0);
44 m_looped = TRUE;
45 m_isPlaying = FALSE;
46 m_useBackgroundColour = FALSE;
47 m_customBackgroundColour = wxColour(0, 0, 0);
48 m_useCustomBackgroundColour = FALSE;
49 m_useParentBackground = FALSE;
50 m_timer.SetPlayer(this);
51 }
52
53 wxAnimationPlayer::~wxAnimationPlayer()
54 {
55 Stop();
56 ClearCache();
57
58 if (m_destroyAnimation)
59 delete m_animation;
60 }
61
62 void wxAnimationPlayer::SetAnimation(wxAnimationBase* animation, bool destroyAnimation)
63 {
64 ClearCache();
65 if (m_destroyAnimation)
66 delete m_animation;
67 m_animation = animation;
68 m_destroyAnimation = destroyAnimation;
69 }
70
71 // Play
72 bool wxAnimationPlayer::Play(wxWindow& window, const wxPoint& pos, bool WXUNUSED(looped))
73 {
74 m_window = & window;
75
76 if (!m_animation || !m_animation->IsValid())
77 return FALSE;
78
79 wxSize sz = GetLogicalScreenSize();
80 wxRect rect(pos, sz);
81 SaveBackground(rect);
82
83 if (m_frames.GetCount() == 0)
84 {
85 if (!Build())
86 {
87 wxLogWarning(_T("wxAnimationPlayer::Play: could not build the image cache."));
88 return FALSE;
89 }
90 }
91 m_currentFrame = 0;
92
93 // Create the backing store
94 m_backingStore.Create(sz.x, sz.y);
95
96 PlayFrame();
97
98 return TRUE;
99 }
100
101 // Build animation (list of wxImages). If not called before Play
102 // is called, Play will call this automatically.
103 bool wxAnimationPlayer::Build()
104 {
105 ClearCache();
106 if (m_animation)
107 {
108 int n = GetFrameCount();
109 int i;
110 for (i = 0; i < n; i++)
111 {
112 wxImage* image = GetFrame(i);
113 if (image)
114 {
115 // If the frame has transparency,
116 // set the colour so converting to a bitmap
117 // will create a mask
118 wxColour transparentColour;
119 if (GetTransparentColour(transparentColour))
120 image->SetMaskColour(transparentColour.Red(), transparentColour.Green(), transparentColour.Blue());
121
122 wxBitmap* bitmap = new wxBitmap(* image);
123 delete image;
124 if (bitmap)
125 m_frames.Append(bitmap);
126 else
127 return FALSE;
128 }
129 else
130 return FALSE;
131 }
132 return TRUE;
133 }
134 else
135 return FALSE;
136 }
137
138 // Stop the animation
139 void wxAnimationPlayer::Stop()
140 {
141 m_timer.Stop();
142 m_isPlaying = FALSE;
143 }
144
145 // Draw the current view of the animation into this DC.
146 // Call this from your OnPaint, for example.
147 void wxAnimationPlayer::Draw(wxDC& dc)
148 {
149 dc.DrawBitmap(m_backingStore, m_position.x, m_position.y);
150 }
151
152
153 int wxAnimationPlayer::GetFrameCount() const
154 {
155 if (m_animation)
156 return m_animation->GetFrameCount();
157 else
158 return 0;
159 }
160
161 wxImage* wxAnimationPlayer::GetFrame(int i) const
162 {
163 if (m_animation)
164 return m_animation->GetFrame(i);
165 else
166 return (wxImage*) NULL;
167 }
168
169 wxAnimationDisposal wxAnimationPlayer::GetDisposalMethod(int i) const
170 {
171 if (m_animation)
172 return m_animation->GetDisposalMethod(i);
173 else
174 return wxANIM_UNSPECIFIED;
175 }
176
177 wxRect wxAnimationPlayer::GetFrameRect(int i) const
178 {
179 if (m_animation)
180 return m_animation->GetFrameRect(i);
181 else
182 return wxRect(0, 0, 0, 0);
183 }
184
185 int wxAnimationPlayer::GetDelay(int i) const
186 {
187 if (m_animation)
188 return m_animation->GetDelay(i);
189 else
190 return 0;
191 }
192
193 wxSize wxAnimationPlayer::GetLogicalScreenSize() const
194 {
195 if (m_animation)
196 return m_animation->GetLogicalScreenSize();
197 else
198 return wxSize(0, 0);
199 }
200
201 bool wxAnimationPlayer::GetBackgroundColour(wxColour& col) const
202 {
203 if (m_animation)
204 return m_animation->GetBackgroundColour(col);
205 else
206 return FALSE;
207 }
208
209 bool wxAnimationPlayer::GetTransparentColour(wxColour& col) const
210 {
211 if (m_animation)
212 return m_animation->GetTransparentColour(col);
213 else
214 return FALSE;
215 }
216
217 // Play the frame
218 bool wxAnimationPlayer::PlayFrame(int frame, wxWindow& window, const wxPoint& WXUNUSED(pos))
219 {
220 wxMemoryDC dc;
221 dc.SelectObject(m_backingStore);
222
223 // Draw the background: colour or area beneath animation
224 wxColour col(255, 255, 255);
225
226 if (UsingBackgroundColour())
227 {
228 if (UsingCustomBackgroundColour())
229 col = GetCustomBackgroundColour();
230 else
231 {
232 GetBackgroundColour(col);
233 }
234
235 // Draw the background colour loaded from the animation
236 // (or set by the user)
237 DrawBackground(dc, wxPoint(0, 0), col);
238 }
239 else
240 {
241 // Draw background we saved
242 dc.DrawBitmap(m_savedBackground, 0, 0);
243 }
244
245 // Draw all intermediate frames that haven't been removed from the
246 // animation
247 int i;
248 for (i = 0; i < frame; i++)
249 {
250 if ((GetDisposalMethod(i) == wxANIM_DONOTREMOVE) || (GetDisposalMethod(i) == wxANIM_UNSPECIFIED))
251 {
252 DrawFrame(i, dc, wxPoint(0, 0));
253 }
254 }
255 DrawFrame(frame, dc, wxPoint(0, 0));
256
257 dc.SelectObject(wxNullBitmap);
258
259 // Draw from backing bitmap onto window
260 wxClientDC clientDC(& window);
261 Draw(clientDC);
262
263 return TRUE;
264 }
265
266 bool wxAnimationPlayer::PlayFrame()
267 {
268 m_isPlaying = TRUE;
269
270 PlayFrame(GetCurrentFrame(), * GetWindow(), GetPosition());
271
272 // Set the timer for the next frame
273 m_timer.Start(GetDelay(GetCurrentFrame()));
274
275 m_currentFrame ++;
276
277 if (m_currentFrame == GetFrameCount())
278 {
279 // Should a non-looped animation display the last frame?
280 if (!m_looped)
281 {
282 m_timer.Stop();
283 m_isPlaying = FALSE;
284 }
285 else
286 m_currentFrame = 0;
287 }
288
289 return TRUE;
290 }
291
292 // Clear the wxImage cache
293 void wxAnimationPlayer::ClearCache()
294 {
295 wxList::compatibility_iterator node = m_frames.GetFirst();
296 while (node)
297 {
298 wxList::compatibility_iterator next = node->GetNext();
299 wxBitmap* bitmap = (wxBitmap*) node->GetData();
300 delete bitmap;
301 m_frames.Erase(node);
302 node = next;
303 }
304 }
305
306 // Draw the background colour
307 void wxAnimationPlayer::DrawBackground(wxDC& dc, const wxPoint& pos, const wxColour& colour)
308 {
309 wxASSERT_MSG( (m_animation != NULL), _T("Animation not present in wxAnimationPlayer"));
310 wxASSERT_MSG( (m_frames.GetCount() != 0), _T("Animation cache not present in wxAnimationPlayer"));
311
312 // Optimization: if the first frame fills the whole area, and is non-transparent,
313 // don't bother drawing the background
314
315 wxBitmap* firstBitmap = (wxBitmap*) m_frames.GetFirst()->GetData() ;
316 wxSize screenSize = GetLogicalScreenSize();
317 if (!firstBitmap->GetMask() && (firstBitmap->GetWidth() == screenSize.x) && (firstBitmap->GetHeight() == screenSize.y))
318 {
319 return;
320 }
321
322 wxBrush brush(colour, wxSOLID);
323 wxPen pen(colour, 1, wxSOLID);
324 dc.SetBrush(brush);
325 dc.SetPen(pen);
326 dc.SetLogicalFunction(wxCOPY);
327
328 dc.DrawRectangle(pos.x, pos.y, screenSize.x, screenSize.y);
329 }
330
331 // Save the pertinent area of the window so we can restore
332 // it if drawing transparently
333 void wxAnimationPlayer::SaveBackground(const wxRect& rect)
334 {
335 wxASSERT( GetWindow() );
336
337 if (!GetWindow())
338 return;
339
340 m_savedBackground.Create(rect.width, rect.height);
341
342 wxMemoryDC memDC;
343 memDC.SelectObject(m_savedBackground);
344
345 if (m_useParentBackground && GetWindow()->GetParent())
346 {
347 wxWindow* parent = GetWindow()->GetParent();
348 wxClientDC dc(parent);
349
350 // Translate the point to coordinates in the
351 // parent's client area, going via screen coordinates
352 wxPoint pt(rect.x, rect.y);
353 wxPoint screenPt = GetWindow()->ClientToScreen(pt);
354 wxPoint parentPt = parent->ScreenToClient(screenPt);
355
356 memDC.Blit(0, 0, rect.width, rect.height, & dc, parentPt.x, parentPt.y);
357 }
358 else
359 {
360 wxClientDC dc(GetWindow());
361
362 memDC.Blit(0, 0, rect.width, rect.height, & dc, rect.x, rect.y);
363 }
364 memDC.SelectObject(wxNullBitmap);
365 }
366
367 // Draw this frame
368 void wxAnimationPlayer::DrawFrame(int frame, wxDC& dc, const wxPoint& pos)
369 {
370 wxASSERT_MSG( (m_animation != NULL), _T("Animation not present in wxAnimationPlayer"));
371 wxASSERT_MSG( (m_frames.GetCount() != 0), _T("Animation cache not present in wxAnimationPlayer"));
372 wxASSERT_MSG( !!m_frames.Item(frame), _T("Image not present in wxAnimationPlayer::DrawFrame"));
373
374 wxBitmap* bitmap = (wxBitmap*) m_frames.Item(frame)->GetData() ;
375
376 wxRect rect = GetFrameRect(frame);
377
378 dc.DrawBitmap(* bitmap, pos.x + rect.x, pos.y + rect.y, (bitmap->GetMask() != NULL));
379 }
380
381 void wxAnimationTimer::Notify()
382 {
383 m_player->PlayFrame();
384 }
385
386 /*
387 * wxAnimationBase
388 */
389
390 IMPLEMENT_ABSTRACT_CLASS(wxAnimationBase, wxObject)
391
392 /*
393 * wxGIFAnimation
394 */
395
396 IMPLEMENT_CLASS(wxGIFAnimation, wxAnimationBase)
397
398 wxGIFAnimation::wxGIFAnimation()
399 {
400 m_decoder = (wxGIFDecoder*) NULL;
401 }
402
403 wxGIFAnimation::~wxGIFAnimation()
404 {
405 delete m_decoder;
406 }
407
408 int wxGIFAnimation::GetFrameCount() const
409 {
410 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
411
412 return m_decoder->GetNumberOfFrames();
413 }
414
415 wxImage* wxGIFAnimation::GetFrame(int i) const
416 {
417 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
418
419 m_decoder->GoFrame(i + 1);
420
421 wxImage* image = new wxImage;
422 m_decoder->ConvertToImage(image);
423 return image;
424 }
425
426 wxAnimationDisposal wxGIFAnimation::GetDisposalMethod(int i) const
427 {
428 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
429
430 m_decoder->GoFrame(i + 1);
431
432 int disposalMethod = m_decoder->GetDisposalMethod();
433 return (wxAnimationDisposal) disposalMethod;
434 }
435
436 wxRect wxGIFAnimation::GetFrameRect(int i) const
437 {
438 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
439
440 m_decoder->GoFrame(i + 1);
441
442 wxRect rect(m_decoder->GetLeft(), m_decoder->GetTop(), m_decoder->GetWidth(), m_decoder->GetHeight());
443 return rect;
444 }
445
446 int wxGIFAnimation::GetDelay(int i) const
447 {
448 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
449
450 m_decoder->GoFrame(i + 1);
451 return m_decoder->GetDelay();
452 }
453
454 wxSize wxGIFAnimation::GetLogicalScreenSize() const
455 {
456 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
457
458 return wxSize(m_decoder->GetLogicalScreenWidth(), m_decoder->GetLogicalScreenHeight());
459 }
460
461 bool wxGIFAnimation::GetBackgroundColour(wxColour& col) const
462 {
463 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
464
465 int i = m_decoder->GetBackgroundColour();
466 if (i == -1)
467 return FALSE;
468 else
469 {
470 unsigned char* pal = m_decoder->GetPalette();
471
472 if (pal)
473 {
474 col = wxColour(pal[3*i + 0], pal[3*i + 1], pal[3*i + 2]);
475 return TRUE;
476 }
477 else
478 return FALSE;
479 }
480 }
481
482 bool wxGIFAnimation::GetTransparentColour(wxColour& col) const
483 {
484 wxASSERT_MSG( (m_decoder != (wxGIFDecoder*) NULL), _T("m_decoder must be non-NULL"));
485
486 int i = m_decoder->GetTransparentColour();
487 if (i == -1)
488 return FALSE;
489 else
490 {
491 unsigned char* pal = m_decoder->GetPalette();
492
493 if (pal)
494 {
495 col = wxColour(pal[3*i + 0], pal[3*i + 1], pal[3*i + 2]);
496 return TRUE;
497 }
498 else
499 return FALSE;
500 }
501 }
502
503 bool wxGIFAnimation::IsValid() const
504 {
505 return ((m_decoder != NULL) && (m_decoder->IsAnimation()));
506 }
507
508 bool wxGIFAnimation::LoadFile(const wxString& filename)
509 {
510 if (m_decoder)
511 delete m_decoder;
512 m_decoder = NULL;
513
514 if (wxFileExists(filename))
515 {
516 wxFileInputStream stream(filename);
517 m_decoder = new wxGIFDecoder(& stream, TRUE);
518
519 if (m_decoder->ReadGIF() != wxGIF_OK)
520 {
521 delete m_decoder;
522 m_decoder = NULL;
523 return FALSE;
524 }
525
526 if (!m_decoder->IsAnimation())
527 {
528 delete m_decoder;
529 m_decoder = NULL;
530
531 return FALSE;
532 }
533 else
534 return TRUE;
535 }
536 else
537 return FALSE;
538 }
539
540 /*
541 * wxAnimationCtrlBase
542 * Abstract base class for format-specific animation controls.
543 * This class implements most of the functionality; all a derived
544 * class has to do is create the appropriate animation class on demand.
545 */
546
547 IMPLEMENT_ABSTRACT_CLASS(wxAnimationCtrlBase, wxControl)
548
549 BEGIN_EVENT_TABLE(wxAnimationCtrlBase, wxControl)
550 EVT_PAINT(wxAnimationCtrlBase::OnPaint)
551 END_EVENT_TABLE()
552
553 bool wxAnimationCtrlBase::Create(wxWindow *parent, wxWindowID id,
554 const wxString& filename, const wxPoint& pos,
555 const wxSize& size, long style, const wxString& name)
556 {
557 m_animation = NULL;
558 m_filename = filename;
559
560 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
561 return FALSE;
562
563 SetBackgroundColour(parent->GetBackgroundColour());
564
565 m_animationPlayer.SetCustomBackgroundColour(GetBackgroundColour());
566
567 // Want to give the impression of transparency by painting
568 // the parent background
569 // if (parent)
570 // m_animationPlayer.UseParentBackground(TRUE);
571 m_animationPlayer.SetWindow(this);
572 m_animationPlayer.SetPosition(wxPoint(0, 0));
573 m_animationPlayer.SetDestroyAnimation(FALSE);
574
575 return TRUE;
576 }
577
578 wxAnimationCtrlBase::~wxAnimationCtrlBase()
579 {
580 if (m_animationPlayer.IsPlaying())
581 m_animationPlayer.Stop();
582 m_animationPlayer.SetAnimation(NULL, FALSE);
583 delete m_animation;
584 }
585
586 bool wxAnimationCtrlBase::LoadFile(const wxString& filename)
587 {
588 if (m_animationPlayer.IsPlaying())
589 m_animationPlayer.Stop();
590
591 wxString filename1(filename);
592
593 if (filename1.IsEmpty())
594 filename1 = m_filename;
595
596 if (filename1.IsEmpty())
597 return FALSE;
598
599 if (m_animation)
600 {
601 delete m_animation;
602 m_animation = NULL;
603 }
604
605 m_animation = DoCreateAnimation(filename1);
606 if (!m_animation)
607 return FALSE;
608
609 if (!m_animation->LoadFile(filename) || !m_animation->IsValid())
610 {
611 delete m_animation;
612 m_animation = NULL;
613 return FALSE;
614 }
615 m_animationPlayer.SetAnimation(m_animation, FALSE);
616
617 if (GetWindowStyle() & wxAN_FIT_ANIMATION)
618 FitToAnimation();
619
620 return TRUE;
621 }
622
623 bool wxAnimationCtrlBase::Play(bool looped)
624 {
625 return m_animationPlayer.Play(*this, wxPoint(0, 0), looped);
626 }
627
628 wxSize wxAnimationCtrlBase::DoGetBestSize() const
629 {
630 if (m_animationPlayer.HasAnimation() && (GetWindowStyle() & wxAN_FIT_ANIMATION))
631 {
632 return m_animationPlayer.GetLogicalScreenSize();
633 }
634 else
635 {
636 return GetSize();
637 }
638 }
639
640 void wxAnimationCtrlBase::FitToAnimation()
641 {
642 if (!m_animationPlayer.HasAnimation())
643 return;
644
645 wxSize sz = m_animationPlayer.GetLogicalScreenSize();
646 SetClientSize(sz);
647 }
648
649 void wxAnimationCtrlBase::OnPaint(wxPaintEvent& WXUNUSED(event))
650 {
651 wxPaintDC dc(this);
652
653 if (GetPlayer().IsPlaying())
654 {
655 GetPlayer().Draw(dc);
656 }
657 }
658
659 /*
660 * wxGIFAnimationCtrl
661 * Provides a GIF animation class when required.
662 */
663
664 IMPLEMENT_ABSTRACT_CLASS(wxGIFAnimationCtrl, wxAnimationCtrlBase)
665
666 wxAnimationBase* wxGIFAnimationCtrl::DoCreateAnimation(const wxString& WXUNUSED(filename))
667 {
668 return new wxGIFAnimation;
669 }