Upstream tarball 9882
[amule.git] / src / MuleGifCtrl.cpp
blob5286194922953fa69538aa3b8ab358b049d8e293
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wx/mstream.h>
27 #include <wx/gifdecod.h>
28 #include <wx/dcbuffer.h>
31 #include "MuleGifCtrl.h"
32 #include "Types.h"
35 BEGIN_EVENT_TABLE(MuleGifCtrl, wxControl)
36 EVT_TIMER(GIFTIMERID, MuleGifCtrl::OnTimer)
37 EVT_PAINT(MuleGifCtrl::OnPaint)
38 EVT_ERASE_BACKGROUND(MuleGifCtrl::OnErase)
39 END_EVENT_TABLE()
41 class MuleGIFDecoder : public wxGIFDecoder
43 public:
44 MuleGIFDecoder()
46 m_nframe = 0;
49 ~MuleGIFDecoder() { }
51 void GoFirstFrame() { m_nframe = 0; }
52 void GoNextFrame() { m_nframe < GetFrameCount() - 1 ? m_nframe++ : m_nframe = 0; }
53 void GoLastFrame() { m_nframe = GetFrameCount() - 1; }
55 void ConvertToImage(wxImage* image) { wxGIFDecoder::ConvertToImage(m_nframe, image); }
57 long GetDelay() { return wxGIFDecoder::GetDelay(m_nframe); }
59 private:
60 uint32_t m_nframe;
63 MuleGifCtrl::MuleGifCtrl(
64 wxWindow *parent,
65 wxWindowID id,
66 const wxPoint& pos,
67 const wxSize& size,
68 long style,
69 const wxValidator& validator,
70 const wxString& name)
72 wxControl(parent, id, pos, size, style, validator, name),
73 m_decoder(NULL),
74 m_timer(this, GIFTIMERID)
79 MuleGifCtrl::~MuleGifCtrl()
81 m_timer.Stop();
82 if (m_decoder) {
83 delete m_decoder;
84 m_decoder = NULL;
89 bool MuleGifCtrl::LoadData(const char* data, int size)
91 if (m_decoder) {
92 m_timer.Stop();
93 delete m_decoder;
94 m_decoder = NULL;
97 wxMemoryInputStream stream(data, size);
98 m_decoder = new MuleGIFDecoder();
99 if ( m_decoder->LoadGIF(stream) != wxGIF_OK ) {
100 delete m_decoder;
101 m_decoder = NULL;
102 return false;
105 m_decoder->GoFirstFrame();
106 wxImage frame;
107 m_decoder->ConvertToImage( &frame );
108 m_frame = wxBitmap(frame);
110 return true;
114 void MuleGifCtrl::Start()
116 if (m_decoder && m_decoder->IsAnimation()) {
117 m_timer.Stop();
118 m_decoder->GoLastFrame();
119 #if wxCHECK_VERSION(2, 9, 0)
120 wxTimerEvent evt(m_timer);
121 #else
122 wxTimerEvent evt;
123 #endif
124 OnTimer(evt);
129 void MuleGifCtrl::Stop()
131 m_timer.Stop();
135 wxSize MuleGifCtrl::GetBestSize()
137 return m_decoder->GetAnimationSize();
141 void MuleGifCtrl::OnTimer(wxTimerEvent& WXUNUSED(event))
143 if (m_decoder) {
144 if (m_decoder->IsAnimation()) {
145 m_decoder->GoNextFrame();
148 wxImage frame;
149 m_decoder->ConvertToImage(&frame);
150 m_frame = wxBitmap(frame);
152 Refresh();
154 if (m_decoder->IsAnimation()) {
155 m_timer.Start(m_decoder->GetDelay(), true);
161 void MuleGifCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
163 wxBufferedPaintDC dc(this);
165 wxSize clientsize = GetClientSize();
166 wxSize gifsize = m_decoder->GetAnimationSize();
167 int x = (clientsize.GetWidth()-gifsize.GetWidth())/2;
168 int y = (clientsize.GetHeight()-gifsize.GetHeight())/2;
170 dc.SetBackground(*(wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID)));
171 dc.Clear();
172 dc.DrawBitmap(m_frame, x, y, true);
176 // File_checked_for_headers