aboutsummaryrefslogtreecommitdiff
path: root/ports/duilib/fix-encoding.patch
blob: 82d90664e9bed5363ceef832b038f5276c77ec95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
diff --git a/DuiLib/Control/UIGifAnim-patch.cpp b/DuiLib/Control/UIGifAnim-patch.cpp
new file mode 100644
index 0000000..61aa32f
--- /dev/null
+++ b/DuiLib/Control/UIGifAnim-patch.cpp
@@ -0,0 +1,368 @@
+#include "StdAfx.h"
+#include "UIGifAnim.h"
+
+///////////////////////////////////////////////////////////////////////////////////////
+DECLARE_HANDLE(HZIP);	// An HZIP identifies a zip file that has been opened
+typedef DWORD ZRESULT;
+typedef struct
+{ 
+	int index;                 // index of this file within the zip
+	char name[MAX_PATH];       // filename within the zip
+	DWORD attr;                // attributes, as in GetFileAttributes.
+	FILETIME atime,ctime,mtime;// access, create, modify filetimes
+	long comp_size;            // sizes of item, compressed and uncompressed. These
+	long unc_size;             // may be -1 if not yet known (e.g. being streamed in)
+} ZIPENTRY;
+typedef struct
+{ 
+	int index;                 // index of this file within the zip
+	TCHAR name[MAX_PATH];      // filename within the zip
+	DWORD attr;                // attributes, as in GetFileAttributes.
+	FILETIME atime,ctime,mtime;// access, create, modify filetimes
+	long comp_size;            // sizes of item, compressed and uncompressed. These
+	long unc_size;             // may be -1 if not yet known (e.g. being streamed in)
+} ZIPENTRYW;
+#define OpenZip OpenZipU
+#define CloseZip(hz) CloseZipU(hz)
+extern HZIP OpenZipU(void *z,unsigned int len,DWORD flags);
+extern ZRESULT CloseZipU(HZIP hz);
+#ifdef _UNICODE
+#define ZIPENTRY ZIPENTRYW
+#define GetZipItem GetZipItemW
+#define FindZipItem FindZipItemW
+#else
+#define GetZipItem GetZipItemA
+#define FindZipItem FindZipItemA
+#endif
+extern ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze);
+extern ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *ze);
+extern ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
+extern ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *ze);
+extern ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags);
+///////////////////////////////////////////////////////////////////////////////////////
+
+namespace DuiLib
+{
+
+	CGifAnimUI::CGifAnimUI(void)
+	{
+		m_pGifImage			=	NULL;
+		m_pPropertyItem		=	NULL;
+		m_nFrameCount		=	0;	
+		m_nFramePosition	=	0;	
+		m_bIsAutoPlay		=	true;
+		m_bIsAutoSize		=	false;
+		m_bIsPlaying		=	false;
+		m_pStream				=	NULL;
+	}
+
+
+	CGifAnimUI::~CGifAnimUI(void)
+	{
+		DeleteGif();
+		m_pManager->KillTimer( this, EVENT_TIEM_ID );
+
+	}
+
+	LPCTSTR CGifAnimUI::GetClass() const
+	{
+		return DUI_CTR_GIFANIM;
+	}
+
+	LPVOID CGifAnimUI::GetInterface( LPCTSTR pstrName )
+	{
+		if( _tcscmp(pstrName, DUI_CTR_GIFANIM) == 0 ) return static_cast<CGifAnimUI*>(this);
+		return CControlUI::GetInterface(pstrName);
+	}
+
+	void CGifAnimUI::DoInit()
+	{
+		InitGifImage();
+	}
+
+	bool CGifAnimUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
+	{
+		if ( NULL == m_pGifImage )
+		{		
+			InitGifImage();
+		}
+		DrawFrame( hDC );
+        return true;
+	}
+
+	void CGifAnimUI::DoEvent( TEventUI& event )
+	{
+		if( event.Type == UIEVENT_TIMER )
+			OnTimer( (UINT_PTR)event.wParam );
+	}
+
+	void CGifAnimUI::SetVisible(bool bVisible /* = true */)
+	{
+		CControlUI::SetVisible(bVisible);
+		if (bVisible)
+			PlayGif();
+		else
+			StopGif();
+	}
+
+	void CGifAnimUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
+	{
+		if( _tcscmp(pstrName, _T("bkimage")) == 0 ) SetBkImage(pstrValue);
+		else if( _tcscmp(pstrName, _T("autoplay")) == 0 ) {
+			SetAutoPlay(_tcscmp(pstrValue, _T("true")) == 0);
+		}
+		else if( _tcscmp(pstrName, _T("autosize")) == 0 ) {
+			SetAutoSize(_tcscmp(pstrValue, _T("true")) == 0);
+		}
+		else
+			CControlUI::SetAttribute(pstrName, pstrValue);
+	}
+
+	void CGifAnimUI::SetBkImage(LPCTSTR pStrImage)
+	{
+		if( m_sBkImage == pStrImage || NULL == pStrImage) return;
+
+		m_sBkImage = pStrImage;
+
+		StopGif();
+		DeleteGif();
+
+		Invalidate();
+
+	}
+
+	LPCTSTR CGifAnimUI::GetBkImage()
+	{
+		return m_sBkImage.GetData();
+	}
+
+	void CGifAnimUI::SetAutoPlay(bool bIsAuto)
+	{
+		m_bIsAutoPlay = bIsAuto;
+	}
+
+	bool CGifAnimUI::IsAutoPlay() const
+	{
+		return m_bIsAutoPlay;
+	}
+
+	void CGifAnimUI::SetAutoSize(bool bIsAuto)
+	{
+		m_bIsAutoSize = bIsAuto;
+	}
+
+	bool CGifAnimUI::IsAutoSize() const
+	{
+		return m_bIsAutoSize;
+	}
+
+	void CGifAnimUI::PlayGif()
+	{
+		if (m_bIsPlaying || m_pGifImage == NULL)
+		{
+			return;
+		}
+
+		long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
+		if ( lPause == 0 ) lPause = 100;
+		m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
+
+		m_bIsPlaying = true;
+	}
+
+	void CGifAnimUI::PauseGif()
+	{
+		if (!m_bIsPlaying || m_pGifImage == NULL)
+		{
+			return;
+		}
+
+		m_pManager->KillTimer(this, EVENT_TIEM_ID);
+		this->Invalidate();
+		m_bIsPlaying = false;
+	}
+
+	void CGifAnimUI::StopGif()
+	{
+		if (!m_bIsPlaying)
+		{
+			return;
+		}
+
+		m_pManager->KillTimer(this, EVENT_TIEM_ID);
+		m_nFramePosition = 0;
+		this->Invalidate();
+		m_bIsPlaying = false;
+	}
+
+	void CGifAnimUI::InitGifImage()
+	{
+		m_pGifImage = LoadGifFromFile(GetBkImage());
+		if ( NULL == m_pGifImage ) return;
+		UINT nCount	= 0;
+		nCount	=	m_pGifImage->GetFrameDimensionsCount();
+		GUID* pDimensionIDs	=	new GUID[ nCount ];
+		m_pGifImage->GetFrameDimensionsList( pDimensionIDs, nCount );
+		m_nFrameCount	=	m_pGifImage->GetFrameCount( &pDimensionIDs[0] );
+		int nSize		=	m_pGifImage->GetPropertyItemSize( PropertyTagFrameDelay );
+		m_pPropertyItem	=	(Gdiplus::PropertyItem*) malloc( nSize );
+		m_pGifImage->GetPropertyItem( PropertyTagFrameDelay, nSize, m_pPropertyItem );
+		delete[] pDimensionIDs;
+		pDimensionIDs = NULL;
+
+		if (m_bIsAutoSize)
+		{
+			SetFixedWidth(m_pGifImage->GetWidth());
+			SetFixedHeight(m_pGifImage->GetHeight());
+		}
+		if (m_bIsAutoPlay && nSize > 0)
+		{
+			PlayGif();
+		}
+	}
+
+	void CGifAnimUI::DeleteGif()
+	{
+		if (m_pStream != NULL )
+		{
+			m_pStream->Release();
+			m_pStream = NULL;
+		}
+		if ( m_pGifImage != NULL )
+		{
+			delete m_pGifImage;
+			m_pGifImage = NULL;
+		}
+
+		if ( m_pPropertyItem != NULL )
+		{
+			free( m_pPropertyItem );
+			m_pPropertyItem = NULL;
+		}
+		m_nFrameCount		=	0;	
+		m_nFramePosition	=	0;	
+	}
+
+	void CGifAnimUI::OnTimer( UINT_PTR idEvent )
+	{
+		if ( idEvent != EVENT_TIEM_ID )
+			return;
+		m_pManager->KillTimer( this, EVENT_TIEM_ID );
+		this->Invalidate();
+
+		m_nFramePosition = (++m_nFramePosition) % m_nFrameCount;
+
+		long lPause = ((long*) m_pPropertyItem->value)[m_nFramePosition] * 10;
+		if ( lPause == 0 ) lPause = 100;
+		m_pManager->SetTimer( this, EVENT_TIEM_ID, lPause );
+	}
+
+	void CGifAnimUI::DrawFrame( HDC hDC )
+	{
+		if ( NULL == hDC || NULL == m_pGifImage ) return;
+		GUID pageGuid = Gdiplus::FrameDimensionTime;
+		Gdiplus::Graphics graphics( hDC );
+		graphics.DrawImage( m_pGifImage, m_rcItem.left, m_rcItem.top, m_rcItem.right-m_rcItem.left, m_rcItem.bottom-m_rcItem.top );
+		m_pGifImage->SelectActiveFrame( &pageGuid, m_nFramePosition );
+	}
+
+	Gdiplus::Image* CGifAnimUI::LoadGifFromFile(LPCTSTR pstrGifPath)
+	{
+		LPBYTE pData = NULL;
+		DWORD dwSize = 0;
+
+		do 
+		{
+			CDuiString sFile = CPaintManagerUI::GetResourcePath();
+			if( CPaintManagerUI::GetResourceZip().IsEmpty() ) {
+				sFile += pstrGifPath;
+				HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
+					FILE_ATTRIBUTE_NORMAL, NULL);
+				if( hFile == INVALID_HANDLE_VALUE ) break;
+				dwSize = ::GetFileSize(hFile, NULL);
+				if( dwSize == 0 ) break;
+
+				DWORD dwRead = 0;
+				pData = new BYTE[ dwSize ];
+				::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
+				::CloseHandle( hFile );
+
+				if( dwRead != dwSize ) {
+					delete[] pData;
+					pData = NULL;
+					break;
+				}
+			}
+			else {
+				sFile += CPaintManagerUI::GetResourceZip();
+				HZIP hz = NULL;
+				if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
+				else hz = OpenZip((void*)sFile.GetData(), 0, 2);
+				if( hz == NULL ) break;
+				ZIPENTRY ze; 
+				int i; 
+				if( FindZipItem(hz, pstrGifPath, true, &i, &ze) != 0 ) break;
+				dwSize = ze.unc_size;
+				if( dwSize == 0 ) break;
+				pData = new BYTE[ dwSize ];
+				int res = UnzipItem(hz, i, pData, dwSize, 3);
+				if( res != 0x00000000 && res != 0x00000600) {
+					delete[] pData;
+					pData = NULL;
+					if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
+					break;
+				}
+				if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
+			}
+
+		} while (0);
+
+		while (!pData)
+		{
+			HANDLE hFile = ::CreateFile(pstrGifPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
+				FILE_ATTRIBUTE_NORMAL, NULL);
+			if( hFile == INVALID_HANDLE_VALUE ) break;
+			dwSize = ::GetFileSize(hFile, NULL);
+			if( dwSize == 0 ) break;
+
+			DWORD dwRead = 0;
+			pData = new BYTE[ dwSize ];
+			::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
+			::CloseHandle( hFile );
+
+			if( dwRead != dwSize ) {
+				delete[] pData;
+				pData = NULL;
+			}
+			break;
+		}
+		if (!pData)
+		{
+			return NULL;
+		}
+
+		Gdiplus::Image* pImage = LoadGifFromMemory(pData, dwSize);
+		delete[] pData;
+		return pImage;
+	}
+
+	Gdiplus::Image* CGifAnimUI::LoadGifFromMemory( LPVOID pBuf,size_t dwSize )
+	{
+		HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
+		BYTE* pMem = (BYTE*)::GlobalLock(hMem);
+
+		memcpy(pMem, pBuf, dwSize);
+		::GlobalUnlock(hMem);
+
+		::CreateStreamOnHGlobal(hMem, TRUE, &m_pStream);
+		Gdiplus::Image *pImg = Gdiplus::Image::FromStream(m_pStream);
+		if(!pImg || pImg->GetLastStatus() != Gdiplus::Ok)
+		{
+			m_pStream->Release();
+			m_pStream = NULL;
+			return 0;
+		}
+		return pImg;
+	}
+
+}