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>
24 #ifdef WXMAKINGDLL_ANIMATE
25 #define WXDLLIMPEXP_ANIMATE WXEXPORT
26 #elif defined(WXUSINGDLL)
27 #define WXDLLIMPEXP_ANIMATE WXIMPORT
28 #else // not making nor using DLL
29 #define WXDLLIMPEXP_ANIMATE
32 class WXDLLIMPEXP_ANIMATE wxAnimationBase
;
33 class WXDLLIMPEXP_ANIMATE wxAnimationPlayer
;
34 class WXDLLEXPORT wxImage
;
36 enum wxAnimationDisposal
38 wxANIM_UNSPECIFIED
= -1,
39 wxANIM_DONOTREMOVE
= 0,
40 wxANIM_TOBACKGROUND
= 1,
44 class WXDLLIMPEXP_ANIMATE wxAnimationTimer
: public wxTimer
47 wxAnimationTimer() { m_player
= (wxAnimationPlayer
*) NULL
; }
49 virtual void Notify();
50 void SetPlayer(wxAnimationPlayer
* player
) { m_player
= player
; }
53 wxAnimationPlayer
* m_player
;
57 * Create an object of this class, and either pass an wxXXXAnimation object in
58 * the constructor, or call SetAnimation. Then call Play(). The wxAnimation
59 * object is only destroyed in the destructor if destroyAnimation is TRUE in
63 class WXDLLIMPEXP_ANIMATE wxAnimationPlayer
: public wxObject
65 DECLARE_CLASS(wxAnimationPlayer
)
68 wxAnimationPlayer(wxAnimationBase
*animation
= (wxAnimationBase
*) NULL
, bool destroyAnimation
= FALSE
);
72 void SetAnimation(wxAnimationBase
* animation
, bool destroyAnimation
= FALSE
);
73 wxAnimationBase
* GetAnimation() const { return m_animation
; }
75 void SetDestroyAnimation(bool destroyAnimation
) { m_destroyAnimation
= destroyAnimation
; };
76 bool GetDestroyAnimation() const { return m_destroyAnimation
; }
78 void SetCurrentFrame(int currentFrame
) { m_currentFrame
= currentFrame
; };
79 int GetCurrentFrame() const { return m_currentFrame
; }
81 void SetWindow(wxWindow
* window
) { m_window
= window
; };
82 wxWindow
* GetWindow() const { return m_window
; }
84 void SetPosition(const wxPoint
& pos
) { m_position
= pos
; };
85 wxPoint
GetPosition() const { return m_position
; }
87 void SetLooped(bool looped
) { m_looped
= looped
; };
88 bool GetLooped() const { return m_looped
; }
90 bool HasAnimation() const { return (m_animation
!= (wxAnimationBase
*) NULL
); }
92 bool IsPlaying() const { return m_isPlaying
; }
94 // Specify whether the GIF's background colour is to be shown,
95 // or whether the window background should show through (the default)
96 void UseBackgroundColour(bool useBackground
) { m_useBackgroundColour
= useBackground
; }
97 bool UsingBackgroundColour() const { return m_useBackgroundColour
; }
99 // Set and use a user-specified background colour (valid for transparent
101 void SetCustomBackgroundColour(const wxColour
& col
, bool useCustomBackgroundColour
= TRUE
)
102 { m_customBackgroundColour
= col
; m_useCustomBackgroundColour
= useCustomBackgroundColour
; }
104 bool UsingCustomBackgroundColour() const { return m_useCustomBackgroundColour
; }
105 const wxColour
& GetCustomBackgroundColour() const { return m_customBackgroundColour
; }
107 // Another refinement - suppose we're drawing the animation in a separate
108 // control or window. We may wish to use the background of the parent
109 // window as the background of our animation. This allows us to specify
110 // whether to grab from the parent or from this window.
111 void UseParentBackground(bool useParent
) { m_useParentBackground
= useParent
; }
112 bool UsingParentBackground() const { return m_useParentBackground
; }
117 virtual bool Play(wxWindow
& window
, const wxPoint
& pos
= wxPoint(0, 0), bool looped
= TRUE
);
119 // Build animation (list of wxImages). If not called before Play
120 // is called, Play will call this automatically.
121 virtual bool Build();
123 // Stop the animation
126 // Draw the current view of the animation into this DC.
127 // Call this from your OnPaint, for example.
128 virtual void Draw(wxDC
& dc
);
130 //// Accessing the current animation
132 virtual int GetFrameCount() const;
133 virtual wxImage
* GetFrame(int i
) const; // Creates a new wxImage
134 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const;
135 virtual wxRect
GetFrameRect(int i
) const; // Position and size of frame
136 virtual int GetDelay(int i
) const; // Delay for this frame
138 virtual wxSize
GetLogicalScreenSize() const;
139 virtual bool GetBackgroundColour(wxColour
& col
) const ;
140 virtual bool GetTransparentColour(wxColour
& col
) const ;
145 virtual bool PlayFrame(int frame
, wxWindow
& window
, const wxPoint
& pos
);
146 virtual bool PlayFrame();
147 virtual void DrawFrame(int frame
, wxDC
& dc
, const wxPoint
& pos
);
148 virtual void DrawBackground(wxDC
& dc
, const wxPoint
& pos
, const wxColour
& colour
);
150 // Clear the wxImage cache
151 virtual void ClearCache();
153 // Save the pertinent area of the window so we can restore
154 // it if drawing transparently
155 void SaveBackground(const wxRect
& rect
);
157 wxBitmap
& GetBackingStore() { return m_backingStore
; }
161 wxAnimationBase
* m_animation
;
162 bool m_destroyAnimation
; // Destroy m_animation on deletion of this object
163 wxList m_frames
; // List of cached wxBitmap frames.
164 int m_currentFrame
; // Current frame
165 wxWindow
* m_window
; // Window to draw into
166 wxPoint m_position
; // Position to draw at
167 bool m_looped
; // Looped, or not
168 wxAnimationTimer m_timer
; // The timer
169 bool m_isPlaying
; // Is the animation playing?
170 wxBitmap m_savedBackground
; // Saved background of window portion
171 wxBitmap m_backingStore
; // The player draws into this
172 bool m_useBackgroundColour
; // Use colour or background
173 wxColour m_customBackgroundColour
; // Override animation background
174 bool m_useCustomBackgroundColour
;
175 bool m_useParentBackground
; // Grab background from parent?
179 * Base class for animations.
180 * A wxXXXAnimation only stores the animation, providing accessors to
181 * wxAnimationPlayer. Currently an animation is read-only, but we could
182 * extend the API for adding frames programmatically, and perhaps have a
183 * wxMemoryAnimation class that stores its frames in memory, and is able to
184 * save all files with suitable filenames. You could then use e.g. Ulead GIF
185 * Animator to load the image files into a GIF animation.
188 class WXDLLIMPEXP_ANIMATE wxAnimationBase
: public wxObject
190 DECLARE_ABSTRACT_CLASS(wxAnimationBase
)
193 wxAnimationBase() {};
194 ~wxAnimationBase() {};
196 //// Accessors. Should be overridden by each derived class.
198 virtual int GetFrameCount() const = 0;
199 virtual wxImage
* GetFrame(int i
) const = 0; // Creates a new wxImage
200 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const = 0;
201 virtual wxRect
GetFrameRect(int i
) const = 0; // Position and size of frame
202 virtual int GetDelay(int i
) const = 0; // Delay for this frame
204 virtual wxSize
GetLogicalScreenSize() const = 0;
205 virtual bool GetBackgroundColour(wxColour
& col
) const = 0;
206 virtual bool GetTransparentColour(wxColour
& col
) const = 0;
208 // Is the animation OK?
209 virtual bool IsValid() const = 0;
213 virtual bool LoadFile(const wxString
& WXUNUSED(filename
)) { return FALSE
; }
217 * This will be moved to a separate file in due course.
220 class WXDLLIMPEXP_ANIMATE wxGIFDecoder
;
222 class WXDLLIMPEXP_ANIMATE wxGIFAnimation
: public wxAnimationBase
224 DECLARE_CLASS(wxGIFAnimation
)
232 virtual int GetFrameCount() const;
233 virtual wxImage
* GetFrame(int i
) const;
234 virtual wxAnimationDisposal
GetDisposalMethod(int i
) const;
235 virtual wxRect
GetFrameRect(int i
) const; // Position and size of frame
236 virtual int GetDelay(int i
) const; // Delay for this frame
238 virtual wxSize
GetLogicalScreenSize() const ;
239 virtual bool GetBackgroundColour(wxColour
& col
) const ;
240 virtual bool GetTransparentColour(wxColour
& col
) const ;
242 virtual bool IsValid() const;
246 virtual bool LoadFile(const wxString
& filename
);
250 wxGIFDecoder
* m_decoder
;
254 * wxAnimationCtrlBase
255 * Abstract base class for format-specific animation controls.
256 * This class implements most of the functionality; all a derived
257 * class has to do is create the appropriate animation class on demand.
260 // Resize to animation size if this is set
261 #define wxAN_FIT_ANIMATION 0x0010
263 class WXDLLIMPEXP_ANIMATE wxAnimationCtrlBase
: public wxControl
266 wxAnimationCtrlBase() { }
267 wxAnimationCtrlBase(wxWindow
*parent
, wxWindowID id
,
268 const wxString
& filename
= wxEmptyString
,
269 const wxPoint
& pos
= wxDefaultPosition
,
270 const wxSize
& size
= wxDefaultSize
, long style
= wxAN_FIT_ANIMATION
|wxNO_BORDER
,
271 const wxString
& name
= wxT("animationControl"))
273 Create(parent
, id
, filename
, pos
, size
, style
, name
);
275 ~wxAnimationCtrlBase();
277 bool Create(wxWindow
*parent
, wxWindowID id
,
278 const wxString
& filename
= wxEmptyString
,
279 const wxPoint
& pos
= wxDefaultPosition
,
280 const wxSize
& size
= wxDefaultSize
, long style
= wxAN_FIT_ANIMATION
|wxNO_BORDER
,
281 const wxString
& name
= wxT("animationControl"));
284 virtual bool LoadFile(const wxString
& filename
= wxEmptyString
);
285 virtual bool Play(bool looped
= TRUE
) ;
286 virtual void Stop() { m_animationPlayer
.Stop(); }
287 virtual void FitToAnimation();
290 virtual bool IsPlaying() const { return m_animationPlayer
.IsPlaying(); }
291 virtual wxAnimationPlayer
& GetPlayer() { return m_animationPlayer
; }
292 virtual wxAnimationBase
* GetAnimation() { return m_animation
; }
294 const wxString
& GetFilename() const { return m_filename
; }
295 void SetFilename(const wxString
& filename
) { m_filename
= filename
; }
298 void OnPaint(wxPaintEvent
& event
);
301 virtual wxSize
DoGetBestSize() const;
303 // Override this in derived classes
304 virtual wxAnimationBase
* DoCreateAnimation(const wxString
& filename
) = 0;
306 wxAnimationPlayer m_animationPlayer
;
307 wxAnimationBase
* m_animation
;
311 DECLARE_ABSTRACT_CLASS(wxAnimationCtrlBase
)
312 DECLARE_EVENT_TABLE()
317 * Provides a GIF animation class when required.
320 class WXDLLIMPEXP_ANIMATE wxGIFAnimationCtrl
: public wxAnimationCtrlBase
323 wxGIFAnimationCtrl() { }
324 wxGIFAnimationCtrl(wxWindow
*parent
, wxWindowID id
,
325 const wxString
& filename
= wxEmptyString
,
326 const wxPoint
& pos
= wxDefaultPosition
,
327 const wxSize
& size
= wxDefaultSize
, long style
= wxAN_FIT_ANIMATION
|wxNO_BORDER
,
328 const wxString
& name
= wxT("animationControl"))
330 Create(parent
, id
, filename
, pos
, size
, style
, name
);
334 virtual wxAnimationBase
* DoCreateAnimation(const wxString
& filename
) ;
336 DECLARE_DYNAMIC_CLASS(wxGIFAnimationCtrl
)
339 #endif // _WX_ANIMATEH__