animate.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/animate.h
  3. // Purpose: wxAnimation and wxAnimationCtrl
  4. // Author: Julian Smart and Guillermo Rodriguez Garcia
  5. // Modified by: Francesco Montorsi
  6. // Created: 13/8/99
  7. // Copyright: (c) Julian Smart and Guillermo Rodriguez Garcia
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_GENERIC_ANIMATEH__
  11. #define _WX_GENERIC_ANIMATEH__
  12. #include "wx/bitmap.h"
  13. // ----------------------------------------------------------------------------
  14. // wxAnimation
  15. // ----------------------------------------------------------------------------
  16. WX_DECLARE_LIST_WITH_DECL(wxAnimationDecoder, wxAnimationDecoderList, class WXDLLIMPEXP_ADV);
  17. class WXDLLIMPEXP_ADV wxAnimation : public wxAnimationBase
  18. {
  19. public:
  20. wxAnimation() {}
  21. wxAnimation(const wxString &name, wxAnimationType type = wxANIMATION_TYPE_ANY)
  22. { LoadFile(name, type); }
  23. virtual bool IsOk() const
  24. { return m_refData != NULL; }
  25. virtual unsigned int GetFrameCount() const;
  26. virtual int GetDelay(unsigned int i) const;
  27. virtual wxImage GetFrame(unsigned int i) const;
  28. virtual wxSize GetSize() const;
  29. virtual bool LoadFile(const wxString& filename,
  30. wxAnimationType type = wxANIMATION_TYPE_ANY);
  31. virtual bool Load(wxInputStream& stream,
  32. wxAnimationType type = wxANIMATION_TYPE_ANY);
  33. // extended interface used by the generic implementation of wxAnimationCtrl
  34. wxPoint GetFramePosition(unsigned int frame) const;
  35. wxSize GetFrameSize(unsigned int frame) const;
  36. wxAnimationDisposal GetDisposalMethod(unsigned int frame) const;
  37. wxColour GetTransparentColour(unsigned int frame) const;
  38. wxColour GetBackgroundColour() const;
  39. protected:
  40. static wxAnimationDecoderList sm_handlers;
  41. public:
  42. static inline wxAnimationDecoderList& GetHandlers() { return sm_handlers; }
  43. static void AddHandler(wxAnimationDecoder *handler);
  44. static void InsertHandler(wxAnimationDecoder *handler);
  45. static const wxAnimationDecoder *FindHandler( wxAnimationType animType );
  46. static void CleanUpHandlers();
  47. static void InitStandardHandlers();
  48. DECLARE_DYNAMIC_CLASS(wxAnimation)
  49. };
  50. // ----------------------------------------------------------------------------
  51. // wxAnimationCtrl
  52. // ----------------------------------------------------------------------------
  53. class WXDLLIMPEXP_ADV wxAnimationCtrl: public wxAnimationCtrlBase
  54. {
  55. public:
  56. wxAnimationCtrl() { Init(); }
  57. wxAnimationCtrl(wxWindow *parent,
  58. wxWindowID id,
  59. const wxAnimation& anim = wxNullAnimation,
  60. const wxPoint& pos = wxDefaultPosition,
  61. const wxSize& size = wxDefaultSize,
  62. long style = wxAC_DEFAULT_STYLE,
  63. const wxString& name = wxAnimationCtrlNameStr)
  64. {
  65. Init();
  66. Create(parent, id, anim, pos, size, style, name);
  67. }
  68. void Init();
  69. bool Create(wxWindow *parent, wxWindowID id,
  70. const wxAnimation& anim = wxNullAnimation,
  71. const wxPoint& pos = wxDefaultPosition,
  72. const wxSize& size = wxDefaultSize,
  73. long style = wxAC_DEFAULT_STYLE,
  74. const wxString& name = wxAnimationCtrlNameStr);
  75. ~wxAnimationCtrl();
  76. public:
  77. virtual bool LoadFile(const wxString& filename, wxAnimationType type = wxANIMATION_TYPE_ANY);
  78. virtual bool Load(wxInputStream& stream, wxAnimationType type = wxANIMATION_TYPE_ANY);
  79. virtual void Stop();
  80. virtual bool Play()
  81. { return Play(true /* looped */); }
  82. virtual bool IsPlaying() const
  83. { return m_isPlaying; }
  84. void SetAnimation(const wxAnimation &animation);
  85. wxAnimation GetAnimation() const
  86. { return m_animation; }
  87. virtual void SetInactiveBitmap(const wxBitmap &bmp);
  88. // override base class method
  89. virtual bool SetBackgroundColour(const wxColour& col);
  90. public: // event handlers
  91. void OnPaint(wxPaintEvent& event);
  92. void OnTimer(wxTimerEvent& event);
  93. void OnSize(wxSizeEvent& event);
  94. public: // extended API specific to this implementation of wxAnimateCtrl
  95. // Specify whether the animation's background colour is to be shown (the default),
  96. // or whether the window background should show through
  97. void SetUseWindowBackgroundColour(bool useWinBackground = true)
  98. { m_useWinBackgroundColour = useWinBackground; }
  99. bool IsUsingWindowBackgroundColour() const
  100. { return m_useWinBackgroundColour; }
  101. // This overload of Play() lets you specify if the animation must loop or not
  102. bool Play(bool looped);
  103. // Draw the current frame of the animation into given DC.
  104. // This is fast as current frame is always cached.
  105. void DrawCurrentFrame(wxDC& dc);
  106. // Returns a wxBitmap with the current frame drawn in it
  107. wxBitmap& GetBackingStore()
  108. { return m_backingStore; }
  109. protected: // internal utilities
  110. // resize this control to fit m_animation
  111. void FitToAnimation();
  112. // Draw the background; use this when e.g. previous frame had wxANIM_TOBACKGROUND disposal.
  113. void DisposeToBackground();
  114. void DisposeToBackground(wxDC& dc);
  115. void DisposeToBackground(wxDC& dc, const wxPoint &pos, const wxSize &sz);
  116. void IncrementalUpdateBackingStore();
  117. bool RebuildBackingStoreUpToFrame(unsigned int);
  118. void DrawFrame(wxDC &dc, unsigned int);
  119. virtual void DisplayStaticImage();
  120. virtual wxSize DoGetBestSize() const;
  121. protected:
  122. unsigned int m_currentFrame; // Current frame
  123. bool m_looped; // Looped, or not
  124. wxTimer m_timer; // The timer
  125. wxAnimation m_animation; // The animation
  126. bool m_isPlaying; // Is the animation playing?
  127. bool m_useWinBackgroundColour; // Use animation bg colour or window bg colour?
  128. wxBitmap m_backingStore; // The frames are drawn here and then blitted
  129. // on the screen
  130. private:
  131. typedef wxAnimationCtrlBase base_type;
  132. DECLARE_DYNAMIC_CLASS(wxAnimationCtrl)
  133. DECLARE_EVENT_TABLE()
  134. };
  135. #endif // _WX_GENERIC_ANIMATEH__