1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 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.
14 //
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
23 //
24
25
26 #include <wx/mstream.h>
27 #include <wx/gifdecod.h>
28 #include <wx/dcbuffer.h>
29
30
31 #include "MuleGifCtrl.h"
32 #include "Types.h"
33
34
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()
40
41 class MuleGIFDecoder : public wxGIFDecoder
42 {
43 public:
MuleGIFDecoder()44 MuleGIFDecoder()
45 {
46 m_nframe = 0;
47 }
48
~MuleGIFDecoder()49 ~MuleGIFDecoder() { }
50
GoFirstFrame()51 void GoFirstFrame() { m_nframe = 0; }
GoNextFrame()52 void GoNextFrame() { (m_nframe < GetFrameCount() - 1) ? m_nframe++ : m_nframe = 0; }
GoLastFrame()53 void GoLastFrame() { m_nframe = GetFrameCount() - 1; }
54
ConvertToImage(wxImage * image)55 void ConvertToImage(wxImage* image) { wxGIFDecoder::ConvertToImage(m_nframe, image); }
56
GetDelay()57 long GetDelay() { return wxGIFDecoder::GetDelay(m_nframe); }
58
59 private:
60 uint32_t m_nframe;
61 };
62
MuleGifCtrl(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)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)
71 :
72 wxControl(parent, id, pos, size, style, validator, name),
73 m_decoder(NULL),
74 m_timer(this, GIFTIMERID)
75 {
76 }
77
78
~MuleGifCtrl()79 MuleGifCtrl::~MuleGifCtrl()
80 {
81 m_timer.Stop();
82 if (m_decoder) {
83 delete m_decoder;
84 m_decoder = NULL;
85 }
86 }
87
88
LoadData(const char * data,int size)89 bool MuleGifCtrl::LoadData(const char* data, int size)
90 {
91 if (m_decoder) {
92 m_timer.Stop();
93 delete m_decoder;
94 m_decoder = NULL;
95 }
96
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;
103 }
104
105 m_decoder->GoFirstFrame();
106 wxImage frame;
107 m_decoder->ConvertToImage( &frame );
108 m_frame = wxBitmap(frame);
109
110 return true;
111 }
112
113
Start()114 void MuleGifCtrl::Start()
115 {
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);
125 }
126 }
127
128
Stop()129 void MuleGifCtrl::Stop()
130 {
131 m_timer.Stop();
132 }
133
134
GetBestSize()135 wxSize MuleGifCtrl::GetBestSize()
136 {
137 return m_decoder->GetAnimationSize();
138 }
139
140
OnTimer(wxTimerEvent & WXUNUSED (event))141 void MuleGifCtrl::OnTimer(wxTimerEvent& WXUNUSED(event))
142 {
143 if (m_decoder) {
144 if (m_decoder->IsAnimation()) {
145 m_decoder->GoNextFrame();
146 }
147
148 wxImage frame;
149 m_decoder->ConvertToImage(&frame);
150 m_frame = wxBitmap(frame);
151
152 Refresh();
153
154 if (m_decoder->IsAnimation()) {
155 m_timer.Start(m_decoder->GetDelay(), true);
156 }
157 }
158 }
159
160
OnPaint(wxPaintEvent & WXUNUSED (event))161 void MuleGifCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
162 {
163 wxBufferedPaintDC dc(this);
164
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;
169
170 dc.SetBackground(*(wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID)));
171 dc.Clear();
172 dc.DrawBitmap(m_frame, x, y, true);
173 }
174
175
176 // File_checked_for_headers
177