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__
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "animate.h"
20 #include <wx/string.h>
21 #include <wx/gdicmn.h>
24 #include <wx/bitmap.h>
25 #include <wx/colour.h>
26 #include <wx/control.h>
28 #ifdef WXMAKINGDLL_ANIMATE
29 #define WXDLLIMPEXP_ANIMATE WXEXPORT
30 #elif defined(WXUSINGDLL)
31 #define WXDLLIMPEXP_ANIMATE WXIMPORT
32 #else // not making nor using DLL
33 #define WXDLLIMPEXP_ANIMATE
36 class WXDLLIMPEXP_ANIMATE wxAnimationBase
;
37 class WXDLLIMPEXP_ANIMATE wxAnimationPlayer
;
38 class WXDLLEXPORT wxImage
;
40 enum wxAnimationDisposal
42 wxANIM_UNSPECIFIED
= -1,
43 wxANIM_DONOTREMOVE
= 0,
44 wxANIM_TOBACKGROUND
= 1,
48 class WXDLLIMPEXP_ANIMATE wxAnimationTimer
: public wxTimer
51 wxAnimationTimer() { m_player
= (wxAnimationPlayer
*) NULL
; }
53 virtual void Notify();
54 void SetPlayer(wxAnimationPlayer
* player
) { 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
);
76 void SetAnimation(wxAnimationBase
* animation
, bool destroyAnimation
= FALSE
);
77 wxAnimationBase
* GetAnimation() const { return m_animation
; }
79 void SetDestroyAnimation(bool destroyAnimation
) { m_destroyAnimation
= destroyAnimation
; };
80 bool GetDestroyAnimation() const { return m_destroyAnimation
; }
82 void SetCurrentFrame(int currentFrame
) { m_currentFrame
= currentFrame
; };
83 int GetCurrentFrame() const { return m_currentFrame
; }
85 void SetWindow(wxWindow
* window
) { m_window
= window
; };
86 wxWindow
* GetWindow() const { return m_window
; }
88 void SetPosition(const wxPoint
& pos
) { m_position
= pos
; };
89 wxPoint
GetPosition() const { return m_position
; }
91 void SetLooped(bool looped
) { m_looped
= looped
; };
92 bool GetLooped() const { return m_looped
; }
94 bool HasAnimation() const { return (m_animation
!= (wxAnimationBase
*) NULL
); }
96 bool IsPlaying() const { return m_isPlaying
; }
98 // Specify whether the GIF's background colour is to be shown,
99 // or whether the window background should show through (the default)
100 void UseBackgroundColour(bool useBackground
) { m_useBackgroundColour
= useBackground
; }
101 bool UsingBackgroundColour() const { return m_useBackgroundColour
; }
103 // Set and use a user-specified background colour (valid for transparent
105 void SetCustomBackgroundColour(const wxColour
& col
, bool useCustomBackgroundColour
= TRUE
)
106 { m_customBackgroundColour
= col
; m_useCustomBackgroundColour
= useCustomBackgroundColour
; }
108 bool UsingCustomBackgroundColour() const { return m_useCustomBackgroundColour
; }
109 const wxColour
& GetCustomBackgroundColour() const { return m_customBackgroundColour
; }
111 // Another refinement - suppose we're drawing the animation in a separate
112 // control or window. We may wish to use the background of the parent
113 // window as the background of our animation. This allows us to specify
114 // whether to grab from the parent or from this window.
115 void UseParentBackground(bool useParent
) { m_useParentBackground
= useParent
; }
116 bool UsingParentBackground() const { return m_useParentBackground
; }
121 virtual bool Play(wxWindow
& window
, const wxPoint
& pos
= wxPoint(0, 0), bool looped
= TRUE
);
123 // Build animation (list of wxImages). If not called before Play
124 // is called, Play will call this automatically.
125 virtual bool Build();
127 // Stop the animation
130 // Draw the current view of the animation into this DC.
131 // Call this from your OnPaint, for example.
132 virtual void Draw(wxDC
& dc
);
134 //// Accessing the current animation
136 virtual int GetFrameCount() const;
137 virtual wxImage
* GetFrame(int i
) const; // Creates a new wxImage
138 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const;
139 virtual wxRect
GetFrameRect(int i
) const; // Position and size of frame
140 virtual int GetDelay(int i
) const; // Delay for this frame
142 virtual wxSize
GetLogicalScreenSize() const;
143 virtual bool GetBackgroundColour(wxColour
& col
) const ;
144 virtual bool GetTransparentColour(wxColour
& col
) const ;
149 virtual bool PlayFrame(int frame
, wxWindow
& window
, const wxPoint
& pos
);
150 virtual bool PlayFrame();
151 virtual void DrawFrame(int frame
, wxDC
& dc
, const wxPoint
& pos
);
152 virtual void DrawBackground(wxDC
& dc
, const wxPoint
& pos
, const wxColour
& colour
);
154 // Clear the wxImage cache
155 virtual void ClearCache();
157 // Save the pertinent area of the window so we can restore
158 // it if drawing transparently
159 void SaveBackground(const wxRect
& rect
);
161 wxBitmap
& GetBackingStore() { return m_backingStore
; }
165 wxAnimationBase
* m_animation
;
166 bool m_destroyAnimation
; // Destroy m_animation on deletion of this object
167 wxList m_frames
; // List of cached wxBitmap frames.
168 int m_currentFrame
; // Current frame
169 wxWindow
* m_window
; // Window to draw into
170 wxPoint m_position
; // Position to draw at
171 bool m_looped
; // Looped, or not
172 wxAnimationTimer m_timer
; // The timer
173 bool m_isPlaying
; // Is the animation playing?
174 wxBitmap m_savedBackground
; // Saved background of window portion
175 wxBitmap m_backingStore
; // The player draws into this
176 bool m_useBackgroundColour
; // Use colour or background
177 wxColour m_customBackgroundColour
; // Override animation background
178 bool m_useCustomBackgroundColour
;
179 bool m_useParentBackground
; // Grab background from parent?
183 * Base class for animations.
184 * A wxXXXAnimation only stores the animation, providing accessors to
185 * wxAnimationPlayer. Currently an animation is read-only, but we could
186 * extend the API for adding frames programmatically, and perhaps have a
187 * wxMemoryAnimation class that stores its frames in memory, and is able to
188 * save all files with suitable filenames. You could then use e.g. Ulead GIF
189 * Animator to load the image files into a GIF animation.
192 class WXDLLIMPEXP_ANIMATE wxAnimationBase
: public wxObject
194 DECLARE_ABSTRACT_CLASS(wxAnimationBase
)
197 wxAnimationBase() {};
198 ~wxAnimationBase() {};
200 //// Accessors. Should be overridden by each derived class.
202 virtual int GetFrameCount() const = 0;
203 virtual wxImage
* GetFrame(int i
) const = 0; // Creates a new wxImage
204 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const = 0;
205 virtual wxRect
GetFrameRect(int i
) const = 0; // Position and size of frame
206 virtual int GetDelay(int i
) const = 0; // Delay for this frame
208 virtual wxSize
GetLogicalScreenSize() const = 0;
209 virtual bool GetBackgroundColour(wxColour
& col
) const = 0;
210 virtual bool GetTransparentColour(wxColour
& col
) const = 0;
212 // Is the animation OK?
213 virtual bool IsValid() const = 0;
217 virtual bool LoadFile(const wxString
& filename
) { return FALSE
; }
221 * This will be moved to a separate file in due course.
224 class WXDLLIMPEXP_ANIMATE wxGIFDecoder
;
226 class WXDLLIMPEXP_ANIMATE wxGIFAnimation
: public wxAnimationBase
228 DECLARE_CLASS(wxGIFAnimation
)
236 virtual int GetFrameCount() const;
237 virtual wxImage
* GetFrame(int i
) const;
238 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const;
239 virtual wxRect
GetFrameRect(int i
) const; // Position and size of frame
240 virtual int GetDelay(int i
) const; // Delay for this frame
242 virtual wxSize
GetLogicalScreenSize() const ;
243 virtual bool GetBackgroundColour(wxColour
& col
) const ;
244 virtual bool GetTransparentColour(wxColour
& col
) const ;
246 virtual bool IsValid() const;
250 virtual bool LoadFile(const wxString
& filename
);
254 wxGIFDecoder
* m_decoder
;
258 * wxAnimationCtrlBase
259 * Abstract base class for format-specific animation controls.
260 * This class implements most of the functionality; all a derived
261 * class has to do is create the appropriate animation class on demand.
264 // Resize to animation size if this is set
265 #define wxAN_FIT_ANIMATION 0x0010
267 class WXDLLIMPEXP_ANIMATE wxAnimationCtrlBase
: public wxControl
270 wxAnimationCtrlBase() { }
271 wxAnimationCtrlBase(wxWindow
*parent
, wxWindowID id
,
272 const wxString
& filename
= wxEmptyString
,
273 const wxPoint
& pos
= wxDefaultPosition
,
274 const wxSize
& size
= wxDefaultSize
, long style
= wxAN_FIT_ANIMATION
|wxNO_BORDER
,
275 const wxString
& name
= wxT("animationControl"))
277 Create(parent
, id
, filename
, pos
, size
, style
, name
);
279 ~wxAnimationCtrlBase();
281 bool Create(wxWindow
*parent
, wxWindowID id
,
282 const wxString
& filename
= wxEmptyString
,
283 const wxPoint
& pos
= wxDefaultPosition
,
284 const wxSize
& size
= wxDefaultSize
, long style
= wxAN_FIT_ANIMATION
|wxNO_BORDER
,
285 const wxString
& name
= wxT("animationControl"));
288 virtual bool LoadFile(const wxString
& filename
= wxEmptyString
);
289 virtual bool Play(bool looped
= TRUE
) ;
290 virtual void Stop() { m_animationPlayer
.Stop(); }
291 virtual void FitToAnimation();
294 virtual bool IsPlaying() const { return m_animationPlayer
.IsPlaying(); }
295 virtual wxAnimationPlayer
& GetPlayer() { return m_animationPlayer
; }
296 virtual wxAnimationBase
* GetAnimation() { return m_animation
; }
298 const wxString
& GetFilename() const { return m_filename
; }
299 void SetFilename(const wxString
& filename
) { m_filename
= filename
; }
302 void OnPaint(wxPaintEvent
& event
);
305 virtual wxSize
DoGetBestSize() const;
307 // Override this in derived classes
308 virtual wxAnimationBase
* DoCreateAnimation(const wxString
& filename
) = 0;
310 wxAnimationPlayer m_animationPlayer
;
311 wxAnimationBase
* m_animation
;
315 DECLARE_ABSTRACT_CLASS(wxAnimationCtrlBase
)
316 DECLARE_EVENT_TABLE()
321 * Provides a GIF animation class when required.
324 class WXDLLIMPEXP_ANIMATE wxGIFAnimationCtrl
: public wxAnimationCtrlBase
327 wxGIFAnimationCtrl() { }
328 wxGIFAnimationCtrl(wxWindow
*parent
, wxWindowID id
,
329 const wxString
& filename
= wxEmptyString
,
330 const wxPoint
& pos
= wxDefaultPosition
,
331 const wxSize
& size
= wxDefaultSize
, long style
= wxAN_FIT_ANIMATION
|wxNO_BORDER
,
332 const wxString
& name
= wxT("animationControl"))
334 Create(parent
, id
, filename
, pos
, size
, style
, name
);
338 virtual wxAnimationBase
* DoCreateAnimation(const wxString
& filename
) ;
340 DECLARE_DYNAMIC_CLASS(wxGIFAnimationCtrl
)
343 #endif // _WX_ANIMATEH__