]>
Commit | Line | Data |
---|---|---|
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 | ||
ff654490 | 18 | #if wxUSE_ANIMATIONCTRL |
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 | 35 | WX_DEFINE_LIST(wxAnimationDecoderList) |
72045d57 VZ |
36 | |
37 | wxAnimationDecoderList wxAnimation::sm_handlers; | |
38 | ||
39 | ||
72045d57 VZ |
40 | // ---------------------------------------------------------------------------- |
41 | // wxAnimation | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | IMPLEMENT_DYNAMIC_CLASS(wxAnimation, wxAnimationBase) | |
5c33522f | 45 | #define M_ANIMDATA static_cast<wxAnimationDecoder*>(m_refData) |
72045d57 VZ |
46 | |
47 | wxSize wxAnimation::GetSize() const | |
48 | { | |
49 | wxCHECK_MSG( IsOk(), wxDefaultSize, wxT("invalid animation") ); | |
50 | ||
51 | return M_ANIMDATA->GetAnimationSize(); | |
52 | } | |
53 | ||
870cf35c | 54 | unsigned int wxAnimation::GetFrameCount() const |
72045d57 VZ |
55 | { |
56 | wxCHECK_MSG( IsOk(), 0, wxT("invalid animation") ); | |
57 | ||
58 | return M_ANIMDATA->GetFrameCount(); | |
59 | } | |
60 | ||
870cf35c | 61 | wxImage wxAnimation::GetFrame(unsigned int i) const |
72045d57 VZ |
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 | ||
870cf35c | 71 | int wxAnimation::GetDelay(unsigned int i) const |
72045d57 VZ |
72 | { |
73 | wxCHECK_MSG( IsOk(), 0, wxT("invalid animation") ); | |
74 | ||
75 | return M_ANIMDATA->GetDelay(i); | |
76 | } | |
77 | ||
870cf35c | 78 | wxPoint wxAnimation::GetFramePosition(unsigned int frame) const |
72045d57 VZ |
79 | { |
80 | wxCHECK_MSG( IsOk(), wxDefaultPosition, wxT("invalid animation") ); | |
81 | ||
82 | return M_ANIMDATA->GetFramePosition(frame); | |
83 | } | |
84 | ||
870cf35c | 85 | wxSize wxAnimation::GetFrameSize(unsigned int frame) const |
05a98b6d RR |
86 | { |
87 | wxCHECK_MSG( IsOk(), wxDefaultSize, wxT("invalid animation") ); | |
88 | ||
89 | return M_ANIMDATA->GetFrameSize(frame); | |
90 | } | |
91 | ||
870cf35c | 92 | wxAnimationDisposal wxAnimation::GetDisposalMethod(unsigned int frame) const |
72045d57 VZ |
93 | { |
94 | wxCHECK_MSG( IsOk(), wxANIM_UNSPECIFIED, wxT("invalid animation") ); | |
95 | ||
96 | return M_ANIMDATA->GetDisposalMethod(frame); | |
97 | } | |
98 | ||
870cf35c | 99 | wxColour wxAnimation::GetTransparentColour(unsigned int frame) const |
05a98b6d RR |
100 | { |
101 | wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid animation") ); | |
102 | ||
103 | return M_ANIMDATA->GetTransparentColour(frame); | |
104 | } | |
105 | ||
72045d57 VZ |
106 | wxColour wxAnimation::GetBackgroundColour() const |
107 | { | |
108 | wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid animation") ); | |
109 | ||
110 | return M_ANIMDATA->GetBackgroundColour(); | |
111 | } | |
112 | ||
113 | bool 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 | ||
122 | bool 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 | ||
72045d57 VZ |
150 | if (handler == NULL) |
151 | { | |
152 | wxLogWarning( _("No animation handler for type %ld defined."), type ); | |
153 | ||
154 | return false; | |
155 | } | |
156 | ||
ba578767 VZ |
157 | |
158 | // do a copy of the handler from the static list which we will own | |
159 | // as our reference data | |
160 | m_refData = handler->Clone(); | |
161 | ||
72045d57 VZ |
162 | if (stream.IsSeekable() && !M_ANIMDATA->CanRead(stream)) |
163 | { | |
164 | wxLogError(_("Animation file is not of type %ld."), type); | |
165 | return false; | |
166 | } | |
167 | else | |
168 | return M_ANIMDATA->Load(stream); | |
169 | } | |
170 | ||
171 | ||
172 | // ---------------------------------------------------------------------------- | |
173 | // animation decoders | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
176 | void wxAnimation::AddHandler( wxAnimationDecoder *handler ) | |
177 | { | |
178 | // Check for an existing handler of the type being added. | |
179 | if (FindHandler( handler->GetType() ) == 0) | |
180 | { | |
181 | sm_handlers.Append( handler ); | |
182 | } | |
183 | else | |
184 | { | |
185 | // This is not documented behaviour, merely the simplest 'fix' | |
186 | // for preventing duplicate additions. If someone ever has | |
187 | // a good reason to add and remove duplicate handlers (and they | |
188 | // may) we should probably refcount the duplicates. | |
72045d57 VZ |
189 | |
190 | wxLogDebug( _T("Adding duplicate animation handler for '%d' type"), | |
191 | handler->GetType() ); | |
192 | delete handler; | |
193 | } | |
194 | } | |
195 | ||
196 | void 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 | ||
212 | const 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 | ||
224 | void 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 | ||
234 | void 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 | ||
253 | class wxAnimationModule: public wxModule | |
254 | { | |
255 | DECLARE_DYNAMIC_CLASS(wxAnimationModule) | |
256 | public: | |
257 | wxAnimationModule() {} | |
47b378bd VS |
258 | bool OnInit() { wxAnimation::InitStandardHandlers(); return true; } |
259 | void OnExit() { wxAnimation::CleanUpHandlers(); } | |
72045d57 VZ |
260 | }; |
261 | ||
262 | IMPLEMENT_DYNAMIC_CLASS(wxAnimationModule, wxModule) | |
263 | ||
264 | ||
72045d57 VZ |
265 | // ---------------------------------------------------------------------------- |
266 | // wxAnimationCtrl | |
267 | // ---------------------------------------------------------------------------- | |
268 | ||
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) | |
274 | END_EVENT_TABLE() | |
275 | ||
05a98b6d | 276 | void 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 |
287 | bool 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 | ||
304 | wxAnimationCtrl::~wxAnimationCtrl() | |
305 | { | |
306 | Stop(); | |
307 | } | |
308 | ||
309 | bool wxAnimationCtrl::LoadFile(const wxString& filename, wxAnimationType type) | |
462167a9 VZ |
310 | { |
311 | wxFileInputStream fis(filename); | |
312 | return Load(fis, type); | |
313 | } | |
314 | ||
315 | bool wxAnimationCtrl::Load(wxInputStream& stream, wxAnimationType type) | |
72045d57 VZ |
316 | { |
317 | wxAnimation anim; | |
462167a9 | 318 | if ( !anim.Load(stream, type) || !anim.IsOk() ) |
72045d57 VZ |
319 | return false; |
320 | ||
321 | SetAnimation(anim); | |
322 | return true; | |
323 | } | |
324 | ||
325 | wxSize wxAnimationCtrl::DoGetBestSize() const | |
326 | { | |
327 | if (m_animation.IsOk() && !this->HasFlag(wxAC_NO_AUTORESIZE)) | |
328 | return m_animation.GetSize(); | |
329 | ||
330 | return wxSize(100, 100); | |
331 | } | |
332 | ||
333 | void wxAnimationCtrl::SetAnimation(const wxAnimation& animation) | |
334 | { | |
335 | if (IsPlaying()) | |
336 | Stop(); | |
337 | ||
1afdfc9d | 338 | // set new animation even if it's wxNullAnimation |
72045d57 | 339 | m_animation = animation; |
1afdfc9d VZ |
340 | if (!m_animation.IsOk()) |
341 | { | |
1bd2ceb5 | 342 | DisplayStaticImage(); |
1afdfc9d VZ |
343 | return; |
344 | } | |
72045d57 VZ |
345 | |
346 | if (m_animation.GetBackgroundColour() == wxNullColour) | |
347 | SetUseWindowBackgroundColour(); | |
348 | if (!this->HasFlag(wxAC_NO_AUTORESIZE)) | |
349 | FitToAnimation(); | |
350 | ||
1bd2ceb5 | 351 | DisplayStaticImage(); |
8e458bb5 | 352 | } |
72045d57 | 353 | |
8e458bb5 RR |
354 | void wxAnimationCtrl::SetInactiveBitmap(const wxBitmap &bmp) |
355 | { | |
1afdfc9d VZ |
356 | // if the bitmap has an associated mask, we need to set our background to |
357 | // the colour of our parent otherwise when calling DrawCurrentFrame() | |
358 | // (which uses the bitmap's mask), our background colour would be used for | |
359 | // transparent areas - and that's not what we want (at least for | |
360 | // consistency with the GTK version) | |
361 | if ( bmp.GetMask() != NULL && GetParent() != NULL ) | |
362 | SetBackgroundColour(GetParent()->GetBackgroundColour()); | |
363 | ||
1bd2ceb5 | 364 | wxAnimationCtrlBase::SetInactiveBitmap(bmp); |
72045d57 VZ |
365 | } |
366 | ||
367 | void wxAnimationCtrl::FitToAnimation() | |
368 | { | |
369 | SetSize(m_animation.GetSize()); | |
370 | } | |
371 | ||
1afdfc9d VZ |
372 | bool wxAnimationCtrl::SetBackgroundColour(const wxColour& colour) |
373 | { | |
374 | if ( !wxWindow::SetBackgroundColour(colour) ) | |
375 | return false; | |
376 | ||
377 | // if not playing, then this change must be seen immediately (unless | |
378 | // there's an inactive bitmap set which has higher priority than bg colour) | |
379 | if ( !IsPlaying() ) | |
1bd2ceb5 | 380 | DisplayStaticImage(); |
1afdfc9d VZ |
381 | |
382 | return true; | |
383 | } | |
384 | ||
72045d57 VZ |
385 | |
386 | // ---------------------------------------------------------------------------- | |
387 | // wxAnimationCtrl - stop/play methods | |
388 | // ---------------------------------------------------------------------------- | |
389 | ||
390 | void wxAnimationCtrl::Stop() | |
391 | { | |
72045d57 VZ |
392 | m_timer.Stop(); |
393 | m_isPlaying = false; | |
8e458bb5 | 394 | |
1afdfc9d VZ |
395 | // reset frame counter |
396 | m_currentFrame = 0; | |
397 | ||
1bd2ceb5 | 398 | DisplayStaticImage(); |
72045d57 VZ |
399 | } |
400 | ||
401 | bool wxAnimationCtrl::Play(bool looped) | |
402 | { | |
403 | if (!m_animation.IsOk()) | |
404 | return false; | |
405 | ||
72045d57 VZ |
406 | m_looped = looped; |
407 | m_currentFrame = 0; | |
72045d57 | 408 | |
8434297d RD |
409 | if (!RebuildBackingStoreUpToFrame(0)) |
410 | return false; | |
05a98b6d RR |
411 | |
412 | m_isPlaying = true; | |
72045d57 | 413 | |
8e458bb5 RR |
414 | // do a ClearBackground() to avoid that e.g. the custom static bitmap which |
415 | // was eventually shown previously remains partially drawn | |
416 | ClearBackground(); | |
417 | ||
72045d57 VZ |
418 | // DrawCurrentFrame() will use our updated backing store |
419 | wxClientDC clientDC(this); | |
420 | DrawCurrentFrame(clientDC); | |
421 | ||
422 | // start the timer | |
423 | int delay = m_animation.GetDelay(0); | |
424 | if (delay == 0) | |
425 | delay = 1; // 0 is invalid timeout for wxTimer. | |
48271822 | 426 | m_timer.Start(delay, true); |
72045d57 VZ |
427 | |
428 | return true; | |
429 | } | |
430 | ||
431 | ||
432 | ||
433 | // ---------------------------------------------------------------------------- | |
434 | // wxAnimationCtrl - rendering methods | |
435 | // ---------------------------------------------------------------------------- | |
436 | ||
870cf35c | 437 | bool wxAnimationCtrl::RebuildBackingStoreUpToFrame(unsigned int frame) |
72045d57 VZ |
438 | { |
439 | // if we've not created the backing store yet or it's too | |
440 | // small, then recreate it | |
441 | wxSize sz = m_animation.GetSize(), | |
442 | winsz = GetClientSize(); | |
443 | int w = wxMin(sz.GetWidth(), winsz.GetWidth()); | |
444 | int h = wxMin(sz.GetHeight(), winsz.GetHeight()); | |
445 | ||
9d9e3858 | 446 | if ( !m_backingStore.IsOk() || |
6003de2f VZ |
447 | m_backingStore.GetWidth() < w || m_backingStore.GetHeight() < h ) |
448 | { | |
05a98b6d RR |
449 | if (!m_backingStore.Create(w, h)) |
450 | return false; | |
6003de2f | 451 | } |
72045d57 VZ |
452 | |
453 | wxMemoryDC dc; | |
454 | dc.SelectObject(m_backingStore); | |
455 | ||
456 | // Draw the background | |
457 | DisposeToBackground(dc); | |
458 | ||
459 | // Draw all intermediate frames that haven't been removed from the animation | |
870cf35c | 460 | for (unsigned int i = 0; i < frame; i++) |
72045d57 VZ |
461 | { |
462 | if (m_animation.GetDisposalMethod(i) == wxANIM_DONOTREMOVE || | |
463 | m_animation.GetDisposalMethod(i) == wxANIM_UNSPECIFIED) | |
464 | { | |
465 | DrawFrame(dc, i); | |
466 | } | |
05a98b6d RR |
467 | else if (m_animation.GetDisposalMethod(i) == wxANIM_TOBACKGROUND) |
468 | DisposeToBackground(dc, m_animation.GetFramePosition(i), | |
469 | m_animation.GetFrameSize(i)); | |
72045d57 VZ |
470 | } |
471 | ||
472 | // finally draw this frame | |
473 | DrawFrame(dc, frame); | |
474 | dc.SelectObject(wxNullBitmap); | |
05a98b6d RR |
475 | |
476 | return true; | |
72045d57 VZ |
477 | } |
478 | ||
479 | void wxAnimationCtrl::IncrementalUpdateBackingStore() | |
480 | { | |
481 | wxMemoryDC dc; | |
482 | dc.SelectObject(m_backingStore); | |
483 | ||
484 | // OPTIMIZATION: | |
485 | // since wxAnimationCtrl can only play animations forward, without skipping | |
486 | // frames, we can be sure that m_backingStore contains the m_currentFrame-1 | |
487 | // frame and thus we just need to dispose the m_currentFrame-1 frame and | |
488 | // render the m_currentFrame-th one. | |
489 | ||
490 | if (m_currentFrame == 0) | |
491 | { | |
492 | // before drawing the first frame always dispose to bg colour | |
493 | DisposeToBackground(dc); | |
494 | } | |
495 | else | |
496 | { | |
497 | switch (m_animation.GetDisposalMethod(m_currentFrame-1)) | |
498 | { | |
499 | case wxANIM_TOBACKGROUND: | |
05a98b6d RR |
500 | DisposeToBackground(dc, m_animation.GetFramePosition(m_currentFrame-1), |
501 | m_animation.GetFrameSize(m_currentFrame-1)); | |
72045d57 VZ |
502 | break; |
503 | ||
504 | case wxANIM_TOPREVIOUS: | |
505 | // this disposal should never be used too often. | |
506 | // E.g. GIF specification explicitely say to keep the usage of this | |
9d9e3858 VZ |
507 | // disposal limited to the minimum. |
508 | // In fact it may require a lot of time to restore | |
72045d57 VZ |
509 | if (m_currentFrame == 1) |
510 | { | |
511 | // if 0-th frame disposal is to restore to previous frame, | |
512 | // the best we can do is to restore to background | |
513 | DisposeToBackground(dc); | |
514 | } | |
515 | else | |
05a98b6d RR |
516 | if (!RebuildBackingStoreUpToFrame(m_currentFrame-2)) |
517 | Stop(); | |
72045d57 VZ |
518 | break; |
519 | ||
520 | case wxANIM_DONOTREMOVE: | |
521 | case wxANIM_UNSPECIFIED: | |
522 | break; | |
523 | } | |
524 | } | |
525 | ||
526 | // now just draw the current frame on the top of the backing store | |
527 | DrawFrame(dc, m_currentFrame); | |
528 | dc.SelectObject(wxNullBitmap); | |
529 | } | |
530 | ||
1bd2ceb5 | 531 | void wxAnimationCtrl::DisplayStaticImage() |
8e458bb5 RR |
532 | { |
533 | wxASSERT(!IsPlaying()); | |
534 | ||
1bd2ceb5 RR |
535 | // m_bmpStaticReal will be updated only if necessary... |
536 | UpdateStaticImage(); | |
537 | ||
538 | if (m_bmpStaticReal.IsOk()) | |
8e458bb5 RR |
539 | { |
540 | // copy the inactive bitmap in the backing store | |
1afdfc9d | 541 | // eventually using the mask if the static bitmap has one |
1bd2ceb5 | 542 | if ( m_bmpStaticReal.GetMask() ) |
1afdfc9d VZ |
543 | { |
544 | wxMemoryDC temp; | |
545 | temp.SelectObject(m_backingStore); | |
546 | DisposeToBackground(temp); | |
1bd2ceb5 | 547 | temp.DrawBitmap(m_bmpStaticReal, 0, 0, true /* use mask */); |
1afdfc9d VZ |
548 | } |
549 | else | |
1bd2ceb5 | 550 | m_backingStore = m_bmpStaticReal; |
8e458bb5 RR |
551 | } |
552 | else | |
553 | { | |
554 | // put in the backing store the first frame of the animation | |
555 | if (!m_animation.IsOk() || | |
556 | !RebuildBackingStoreUpToFrame(0)) | |
557 | { | |
558 | m_animation = wxNullAnimation; | |
559 | DisposeToBackground(); | |
560 | } | |
561 | } | |
562 | ||
563 | Refresh(); | |
564 | } | |
565 | ||
870cf35c | 566 | void wxAnimationCtrl::DrawFrame(wxDC &dc, unsigned int frame) |
72045d57 VZ |
567 | { |
568 | // PERFORMANCE NOTE: | |
569 | // this draw stuff is not as fast as possible: the wxAnimationDecoder | |
570 | // needs first to convert from its internal format to wxImage RGB24; | |
571 | // the wxImage is then converted as a wxBitmap and finally blitted. | |
572 | // If wxAnimationDecoder had a function to convert directly from its | |
573 | // internal format to a port-specific wxBitmap, it would be somewhat faster. | |
574 | wxBitmap bmp(m_animation.GetFrame(frame)); | |
575 | dc.DrawBitmap(bmp, m_animation.GetFramePosition(frame), | |
576 | true /* use mask */); | |
577 | } | |
578 | ||
579 | void wxAnimationCtrl::DrawCurrentFrame(wxDC& dc) | |
580 | { | |
9d9e3858 | 581 | wxASSERT( m_backingStore.IsOk() ); |
72045d57 VZ |
582 | |
583 | // m_backingStore always contains the current frame | |
8e458bb5 RR |
584 | dc.DrawBitmap(m_backingStore, 0, 0, true /* use mask in case it's present */); |
585 | } | |
586 | ||
587 | void wxAnimationCtrl::DisposeToBackground() | |
588 | { | |
589 | // clear the backing store | |
590 | wxMemoryDC dc; | |
591 | dc.SelectObject(m_backingStore); | |
988bbabb VZ |
592 | if ( dc.IsOk() ) |
593 | DisposeToBackground(dc); | |
72045d57 VZ |
594 | } |
595 | ||
596 | void wxAnimationCtrl::DisposeToBackground(wxDC& dc) | |
87d2b3f1 CE |
597 | { |
598 | wxColour col = IsUsingWindowBackgroundColour() | |
9d9e3858 | 599 | ? GetBackgroundColour() |
05a98b6d | 600 | : m_animation.GetBackgroundColour(); |
1afdfc9d | 601 | |
87d2b3f1 | 602 | wxBrush brush(col); |
72045d57 VZ |
603 | dc.SetBackground(brush); |
604 | dc.Clear(); | |
605 | } | |
606 | ||
05a98b6d RR |
607 | void wxAnimationCtrl::DisposeToBackground(wxDC& dc, const wxPoint &pos, const wxSize &sz) |
608 | { | |
609 | wxColour col = IsUsingWindowBackgroundColour() | |
610 | ? GetBackgroundColour() | |
611 | : m_animation.GetBackgroundColour(); | |
612 | wxBrush brush(col); | |
613 | dc.SetBrush(brush); // SetBrush and not SetBackground !! | |
614 | dc.SetPen(*wxTRANSPARENT_PEN); | |
615 | dc.DrawRectangle(pos, sz); | |
616 | } | |
617 | ||
72045d57 VZ |
618 | // ---------------------------------------------------------------------------- |
619 | // wxAnimationCtrl - event handlers | |
620 | // ---------------------------------------------------------------------------- | |
621 | ||
622 | void wxAnimationCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) | |
623 | { | |
624 | // VERY IMPORTANT: the wxPaintDC *must* be created in any case | |
625 | wxPaintDC dc(this); | |
626 | ||
9d9e3858 | 627 | if ( m_backingStore.IsOk() ) |
1afdfc9d VZ |
628 | { |
629 | // NOTE: we draw the bitmap explicitely ignoring the mask (if any); | |
630 | // i.e. we don't want to combine the backing store with the | |
631 | // possibly wrong preexisting contents of the window! | |
632 | dc.DrawBitmap(m_backingStore, 0, 0, false /* no mask */); | |
633 | } | |
8e458bb5 RR |
634 | else |
635 | { | |
636 | // m_animation is not valid and thus we don't have a valid backing store... | |
637 | // clear then our area to the background colour | |
638 | DisposeToBackground(dc); | |
639 | } | |
72045d57 VZ |
640 | } |
641 | ||
642 | void wxAnimationCtrl::OnTimer(wxTimerEvent &WXUNUSED(event)) | |
643 | { | |
644 | m_currentFrame++; | |
645 | if (m_currentFrame == m_animation.GetFrameCount()) | |
646 | { | |
647 | // Should a non-looped animation display the last frame? | |
648 | if (!m_looped) | |
649 | { | |
8e458bb5 | 650 | Stop(); |
72045d57 VZ |
651 | return; |
652 | } | |
653 | else | |
654 | m_currentFrame = 0; // let's restart | |
655 | } | |
656 | ||
657 | IncrementalUpdateBackingStore(); | |
658 | ||
659 | wxClientDC dc(this); | |
660 | DrawCurrentFrame(dc); | |
661 | ||
7444cb50 VZ |
662 | #ifdef __WXMAC__ |
663 | // without this, the animation currently doesn't redraw under Mac | |
664 | Refresh(); | |
665 | #endif // __WXMAC__ | |
666 | ||
72045d57 VZ |
667 | // Set the timer for the next frame |
668 | int delay = m_animation.GetDelay(m_currentFrame); | |
669 | if (delay == 0) | |
670 | delay = 1; // 0 is invalid timeout for wxTimer. | |
48271822 | 671 | m_timer.Start(delay, true); |
72045d57 VZ |
672 | } |
673 | ||
674 | void wxAnimationCtrl::OnSize(wxSizeEvent &WXUNUSED(event)) | |
675 | { | |
676 | // NB: resizing an animation control may take a lot of time | |
677 | // for big animations as the backing store must be | |
580ca0a4 VZ |
678 | // extended and rebuilt. Try to avoid it e.g. using |
679 | // a null proportion value for your wxAnimationCtrls | |
680 | // when using them inside sizers. | |
72045d57 | 681 | if (m_animation.IsOk()) |
580ca0a4 VZ |
682 | { |
683 | // be careful to change the backing store *only* if we are | |
684 | // playing the animation as otherwise we may be displaying | |
685 | // the inactive bitmap and overwriting the backing store | |
686 | // with the last played frame is wrong in this case | |
687 | if (IsPlaying()) | |
688 | { | |
1afdfc9d VZ |
689 | if (!RebuildBackingStoreUpToFrame(m_currentFrame)) |
690 | Stop(); // in case we are playing | |
580ca0a4 VZ |
691 | } |
692 | } | |
72045d57 VZ |
693 | } |
694 | ||
ff654490 | 695 | #endif // wxUSE_ANIMATIONCTRL |
72045d57 | 696 |