1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Animation classes
4 // Author: Julian Smart and Guillermo Rodriguez Garcia
8 // Copyright: (c) Julian Smart and Guillermo Rodriguez Garcia
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_ANIMATEH__
13 #define _WX_ANIMATEH__
16 #include <wx/string.h>
17 #include <wx/gdicmn.h>
20 #include <wx/bitmap.h>
21 #include <wx/colour.h>
22 #include <wx/control.h>
25 #ifdef WXMAKINGDLL_ANIMATE
26 #define WXDLLIMPEXP_ANIMATE WXEXPORT
27 #elif defined(WXUSINGDLL)
28 #define WXDLLIMPEXP_ANIMATE WXIMPORT
29 #else // not making nor using DLL
30 #define WXDLLIMPEXP_ANIMATE
33 class WXDLLIMPEXP_ANIMATE wxAnimationBase
;
34 class WXDLLIMPEXP_ANIMATE wxAnimationPlayer
;
35 class WXDLLEXPORT wxImage
;
37 enum wxAnimationDisposal
39 wxANIM_UNSPECIFIED
= -1,
40 wxANIM_DONOTREMOVE
= 0,
41 wxANIM_TOBACKGROUND
= 1,
45 class WXDLLIMPEXP_ANIMATE wxAnimationTimer
: public wxTimer
49 { m_player
= (wxAnimationPlayer
*) NULL
; }
51 virtual void Notify();
53 void SetPlayer(wxAnimationPlayer
* player
)
54 { m_player
= player
; }
57 wxAnimationPlayer
* m_player
;
61 * Create an object of this class, and either pass an wxXXXAnimation object in
62 * the constructor, or call SetAnimation. Then call Play(). The wxAnimation
63 * object is only destroyed in the destructor if destroyAnimation is TRUE in
67 class WXDLLIMPEXP_ANIMATE wxAnimationPlayer
: public wxObject
69 DECLARE_CLASS(wxAnimationPlayer
)
72 wxAnimationPlayer(wxAnimationBase
*animation
= (wxAnimationBase
*) NULL
, bool destroyAnimation
= false);
77 void SetAnimation(wxAnimationBase
* animation
, bool destroyAnimation
= false);
78 wxAnimationBase
* GetAnimation() const
79 { return m_animation
; }
81 void SetDestroyAnimation(bool destroyAnimation
)
82 { m_destroyAnimation
= destroyAnimation
; }
84 bool GetDestroyAnimation() const
85 { return m_destroyAnimation
; }
87 void SetCurrentFrame(int currentFrame
)
88 { m_currentFrame
= currentFrame
; }
90 int GetCurrentFrame() const
91 { return m_currentFrame
; }
93 void SetWindow(wxWindow
* window
)
94 { m_window
= window
; }
96 wxWindow
* GetWindow() const
99 void SetPosition(const wxPoint
& pos
)
100 { m_position
= pos
; }
102 wxPoint
GetPosition() const
103 { return m_position
; }
105 void SetLooped(bool looped
)
106 { m_looped
= looped
; }
108 bool GetLooped() const
111 bool HasAnimation() const
112 { return (m_animation
!= (wxAnimationBase
*) NULL
); }
114 bool IsPlaying() const
115 { return m_isPlaying
; }
117 // Specify whether the GIF's background colour is to be shown,
118 // or whether the window background should show through (the default)
119 void UseBackgroundColour(bool useBackground
)
120 { m_useBackgroundColour
= useBackground
; }
122 bool UsingBackgroundColour() const
123 { return m_useBackgroundColour
; }
125 // Set and use a user-specified background colour (valid for transparent
127 void SetCustomBackgroundColour(const wxColour
& col
, bool useCustomBackgroundColour
= true)
129 m_customBackgroundColour
= col
;
130 m_useCustomBackgroundColour
= useCustomBackgroundColour
;
133 bool UsingCustomBackgroundColour() const
134 { return m_useCustomBackgroundColour
; }
136 const wxColour
& GetCustomBackgroundColour() const
137 { return m_customBackgroundColour
; }
139 // Another refinement - suppose we're drawing the animation in a separate
140 // control or window. We may wish to use the background of the parent
141 // window as the background of our animation. This allows us to specify
142 // whether to grab from the parent or from this window.
143 void UseParentBackground(bool useParent
)
144 { m_useParentBackground
= useParent
; }
146 bool UsingParentBackground() const
147 { return m_useParentBackground
; }
152 virtual bool Play(wxWindow
& window
, const wxPoint
& pos
= wxPoint(0, 0), bool looped
= true);
154 // Build animation (list of wxImages). If not called before Play
155 // is called, Play will call this automatically.
156 virtual bool Build();
158 // Stop the animation
161 // Draw the current view of the animation into this DC.
162 // Call this from your OnPaint, for example.
163 virtual void Draw(wxDC
& dc
);
165 //// Accessing the current animation
167 virtual int GetFrameCount() const;
168 virtual wxImage
* GetFrame(int i
) const; // Creates a new wxImage
169 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const;
170 virtual wxRect
GetFrameRect(int i
) const; // Position and size of frame
171 virtual int GetDelay(int i
) const; // Delay for this frame
173 virtual wxSize
GetLogicalScreenSize() const;
174 virtual bool GetBackgroundColour(wxColour
& col
) const;
175 virtual bool GetTransparentColour(wxColour
& col
) const;
180 virtual bool PlayFrame(int frame
, wxWindow
& window
, const wxPoint
& pos
);
181 virtual bool PlayFrame();
182 virtual void DrawFrame(int frame
, wxDC
& dc
, const wxPoint
& pos
);
183 virtual void DrawBackground(wxDC
& dc
, const wxPoint
& pos
, const wxColour
& colour
);
185 // Clear the wxImage cache
186 virtual void ClearCache();
188 // Save the pertinent area of the window so we can restore
189 // it if drawing transparently
190 void SaveBackground(const wxRect
& rect
);
192 wxBitmap
& GetBackingStore()
193 { return m_backingStore
; }
197 wxAnimationBase
* m_animation
;
198 bool m_destroyAnimation
; // Destroy m_animation on deletion of this object
199 wxList m_frames
; // List of cached wxBitmap frames.
200 int m_currentFrame
; // Current frame
201 wxWindow
* m_window
; // Window to draw into
202 wxPoint m_position
; // Position to draw at
203 bool m_looped
; // Looped, or not
204 wxAnimationTimer m_timer
; // The timer
205 bool m_isPlaying
; // Is the animation playing?
206 wxBitmap m_savedBackground
; // Saved background of window portion
207 wxBitmap m_backingStore
; // The player draws into this
208 bool m_useBackgroundColour
; // Use colour or background
209 wxColour m_customBackgroundColour
; // Override animation background
210 bool m_useCustomBackgroundColour
;
211 bool m_useParentBackground
; // Grab background from parent?
215 * Base class for animations.
216 * A wxXXXAnimation only stores the animation, providing accessors to
217 * wxAnimationPlayer. Currently an animation is read-only, but we could
218 * extend the API for adding frames programmatically, and perhaps have a
219 * wxMemoryAnimation class that stores its frames in memory, and is able to
220 * save all files with suitable filenames. You could then use e.g. Ulead GIF
221 * Animator to load the image files into a GIF animation.
224 class WXDLLIMPEXP_ANIMATE wxAnimationBase
: public wxObject
226 DECLARE_ABSTRACT_CLASS(wxAnimationBase
)
229 wxAnimationBase() {};
230 ~wxAnimationBase() {};
232 //// Accessors. Should be overridden by each derived class.
234 virtual int GetFrameCount() const = 0;
235 virtual wxImage
* GetFrame(int i
) const = 0; // Creates a new wxImage
236 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const = 0;
237 virtual wxRect
GetFrameRect(int i
) const = 0; // Position and size of frame
238 virtual int GetDelay(int i
) const = 0; // Delay for this frame
240 virtual wxSize
GetLogicalScreenSize() const = 0;
241 virtual bool GetBackgroundColour(wxColour
& col
) const = 0;
242 virtual bool GetTransparentColour(wxColour
& col
) const = 0;
244 // Is the animation OK?
245 virtual bool IsValid() const = 0;
249 virtual bool LoadFile(const wxString
& WXUNUSED(filename
))
254 * This will be moved to a separate file in due course.
257 class WXDLLIMPEXP_ANIMATE wxGIFDecoder
;
259 class WXDLLIMPEXP_ANIMATE wxGIFAnimation
: public wxAnimationBase
261 DECLARE_CLASS(wxGIFAnimation
)
269 virtual int GetFrameCount() const;
270 virtual wxImage
* GetFrame(int i
) const;
271 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const;
272 virtual wxRect
GetFrameRect(int i
) const; // Position and size of frame
273 virtual int GetDelay(int i
) const; // Delay for this frame
275 virtual wxSize
GetLogicalScreenSize() const;
276 virtual bool GetBackgroundColour(wxColour
& col
) const;
277 virtual bool GetTransparentColour(wxColour
& col
) const;
279 virtual bool IsValid() const;
283 virtual bool LoadFile(const wxString
& filename
);
286 wxGIFDecoder
* m_decoder
;
290 * wxAnimationCtrlBase
291 * Abstract base class for format-specific animation controls.
292 * This class implements most of the functionality; all a derived
293 * class has to do is create the appropriate animation class on demand.
296 // Resize to animation size if this is set
297 #define wxAN_FIT_ANIMATION 0x0010
299 class WXDLLIMPEXP_ANIMATE wxAnimationCtrlBase
: public wxControl
302 wxAnimationCtrlBase() {}
303 wxAnimationCtrlBase(wxWindow
*parent
, wxWindowID id
,
304 const wxString
& filename
= wxEmptyString
,
305 const wxPoint
& pos
= wxDefaultPosition
,
306 const wxSize
& size
= wxDefaultSize
, long style
= wxAN_FIT_ANIMATION
|wxNO_BORDER
,
307 const wxString
& name
= wxT("animationControl"))
309 Create(parent
, id
, filename
, pos
, size
, style
, name
);
311 ~wxAnimationCtrlBase();
313 bool Create(wxWindow
*parent
, wxWindowID id
,
314 const wxString
& filename
= wxEmptyString
,
315 const wxPoint
& pos
= wxDefaultPosition
,
316 const wxSize
& size
= wxDefaultSize
,
317 long style
= wxAN_FIT_ANIMATION
| wxNO_BORDER
,
318 const wxString
& name
= wxT("animationControl"));
321 virtual bool LoadFile(const wxString
& filename
= wxEmptyString
);
322 virtual bool Play(bool looped
= true);
324 { m_animationPlayer
.Stop(); }
326 virtual void FitToAnimation();
329 virtual bool IsPlaying() const
330 { return m_animationPlayer
.IsPlaying(); }
332 virtual wxAnimationPlayer
& GetPlayer()
333 { return m_animationPlayer
; }
335 virtual wxAnimationBase
* GetAnimation()
336 { return m_animation
; }
338 const wxString
& GetFilename() const
339 { return m_filename
; }
341 void SetFilename(const wxString
& filename
)
342 { m_filename
= filename
; }
345 void OnPaint(wxPaintEvent
& event
);
348 virtual wxSize
DoGetBestSize() const;
350 // Override this in derived classes
351 virtual wxAnimationBase
* DoCreateAnimation(const wxString
& filename
) = 0;
353 wxAnimationPlayer m_animationPlayer
;
354 wxAnimationBase
* m_animation
;
358 DECLARE_ABSTRACT_CLASS(wxAnimationCtrlBase
)
359 DECLARE_EVENT_TABLE()
364 * Provides a GIF animation class when required.
367 class WXDLLIMPEXP_ANIMATE wxGIFAnimationCtrl
: public wxAnimationCtrlBase
370 wxGIFAnimationCtrl() {}
371 wxGIFAnimationCtrl(wxWindow
*parent
,
373 const wxString
& filename
= wxEmptyString
,
374 const wxPoint
& pos
= wxDefaultPosition
,
375 const wxSize
& size
= wxDefaultSize
,
376 long style
= wxAN_FIT_ANIMATION
| wxNO_BORDER
,
377 const wxString
& name
= wxT("animationControl"))
379 Create(parent
, id
, filename
, pos
, size
, style
, name
);
383 virtual wxAnimationBase
* DoCreateAnimation(const wxString
& filename
);
386 DECLARE_DYNAMIC_CLASS(wxGIFAnimationCtrl
)
389 #endif // _WX_ANIMATEH__