reshuffling of dlls
[reactos.git] / reactos / dll / win32 / comctl32 / animate.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
4 *
5 * Copyright 1998, 1999 Eric Kohl
6 * Copyright 1999 Eric Pouech
7 * Copyright 2005 Dimitrie O. Paun
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * NOTES
24 *
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Mar. 15, 2005, by Dimitrie O. Paun.
27 *
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
31 *
32 * TODO:
33 * - check for the 'rec ' list in some AVI files
34 */
35
36 #define COM_NO_WINDOWS_H
37 #include <stdarg.h>
38 #include <string.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "vfw.h"
46 #include "mmsystem.h"
47 #include "comctl32.h"
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(animate);
51
52 static struct {
53 HMODULE hModule;
54 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
55 LRESULT (WINAPI *fnICClose)(HIC);
56 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD_PTR, DWORD_PTR);
57 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
58 } fnIC;
59
60 typedef struct
61 {
62 /* reference to input stream (file or resource) */
63 HGLOBAL hRes;
64 HMMIO hMMio; /* handle to mmio stream */
65 HWND hwndSelf;
66 HWND hwndNotify;
67 DWORD dwStyle;
68 /* information on the loaded AVI file */
69 MainAVIHeader mah;
70 AVIStreamHeader ash;
71 LPBITMAPINFOHEADER inbih;
72 LPDWORD lpIndex;
73 /* data for the decompressor */
74 HIC hic;
75 LPBITMAPINFOHEADER outbih;
76 LPVOID indata;
77 LPVOID outdata;
78 /* data for the background mechanism */
79 CRITICAL_SECTION cs;
80 HANDLE hStopEvent;
81 HANDLE hThread;
82 DWORD threadId;
83 UINT uTimer;
84 /* data for playing the file */
85 int nFromFrame;
86 int nToFrame;
87 int nLoop;
88 int currFrame;
89 /* tranparency info*/
90 COLORREF transparentColor;
91 HBRUSH hbrushBG;
92 HBITMAP hbmPrevFrame;
93 } ANIMATE_INFO;
94
95 #define ANIMATE_COLOR_NONE 0xffffffff
96
97 static void ANIMATE_Notify(ANIMATE_INFO *infoPtr, UINT notif)
98 {
99 SendMessageW(infoPtr->hwndNotify, WM_COMMAND,
100 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
101 (LPARAM)infoPtr->hwndSelf);
102 }
103
104 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPWSTR lpName)
105 {
106 static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
107 HRSRC hrsrc;
108 MMIOINFO mminfo;
109 LPVOID lpAvi;
110
111 hrsrc = FindResourceW(hInst, lpName, aviW);
112 if (!hrsrc)
113 return FALSE;
114
115 infoPtr->hRes = LoadResource(hInst, hrsrc);
116 if (!infoPtr->hRes)
117 return FALSE;
118
119 lpAvi = LockResource(infoPtr->hRes);
120 if (!lpAvi)
121 return FALSE;
122
123 memset(&mminfo, 0, sizeof(mminfo));
124 mminfo.fccIOProc = FOURCC_MEM;
125 mminfo.pchBuffer = (LPSTR)lpAvi;
126 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
127 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
128 if (!infoPtr->hMMio)
129 {
130 FreeResource(infoPtr->hRes);
131 return FALSE;
132 }
133
134 return TRUE;
135 }
136
137
138 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
139 {
140 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
141
142 if(!infoPtr->hMMio) return FALSE;
143 return TRUE;
144 }
145
146
147 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
148 {
149 EnterCriticalSection(&infoPtr->cs);
150
151 /* should stop playing */
152 if (infoPtr->hThread)
153 {
154 HANDLE handle = infoPtr->hThread;
155
156 TRACE("stopping animation thread\n");
157 infoPtr->hThread = 0;
158 SetEvent( infoPtr->hStopEvent );
159
160 if (infoPtr->threadId != GetCurrentThreadId())
161 {
162 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
163 WaitForSingleObject( handle, INFINITE );
164 TRACE("animation thread stopped\n");
165 EnterCriticalSection(&infoPtr->cs);
166 }
167
168 CloseHandle( handle );
169 CloseHandle( infoPtr->hStopEvent );
170 infoPtr->hStopEvent = 0;
171 }
172 if (infoPtr->uTimer) {
173 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
174 infoPtr->uTimer = 0;
175 }
176
177 LeaveCriticalSection(&infoPtr->cs);
178
179 ANIMATE_Notify(infoPtr, ACN_STOP);
180
181 return TRUE;
182 }
183
184
185 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
186 {
187 if (infoPtr->hMMio) {
188 ANIMATE_DoStop(infoPtr);
189 mmioClose(infoPtr->hMMio, 0);
190 if (infoPtr->hRes) {
191 FreeResource(infoPtr->hRes);
192 infoPtr->hRes = 0;
193 }
194 Free (infoPtr->lpIndex);
195 infoPtr->lpIndex = NULL;
196 if (infoPtr->hic) {
197 fnIC.fnICClose(infoPtr->hic);
198 infoPtr->hic = 0;
199 }
200 Free (infoPtr->inbih);
201 infoPtr->inbih = NULL;
202 Free (infoPtr->outbih);
203 infoPtr->outbih = NULL;
204 Free (infoPtr->indata);
205 infoPtr->indata = NULL;
206 Free (infoPtr->outdata);
207 infoPtr->outdata = NULL;
208 if( infoPtr->hbmPrevFrame )
209 {
210 DeleteObject(infoPtr->hbmPrevFrame);
211 infoPtr->hbmPrevFrame = 0;
212 }
213
214 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
215 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
216 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
217 }
218 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
219 }
220
221 static void ANIMATE_TransparentBlt(ANIMATE_INFO *infoPtr, HDC hdcDest, HDC hdcSource)
222 {
223 HDC hdcMask;
224 HBITMAP hbmMask;
225 HBITMAP hbmOld;
226
227 /* create a transparency mask */
228 hdcMask = CreateCompatibleDC(hdcDest);
229 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
230 hbmOld = SelectObject(hdcMask, hbmMask);
231
232 SetBkColor(hdcSource,infoPtr->transparentColor);
233 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
234
235 /* mask the source bitmap */
236 SetBkColor(hdcSource, RGB(0,0,0));
237 SetTextColor(hdcSource, RGB(255,255,255));
238 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
239
240 /* mask the destination bitmap */
241 SetBkColor(hdcDest, RGB(255,255,255));
242 SetTextColor(hdcDest, RGB(0,0,0));
243 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
244
245 /* combine source and destination */
246 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
247
248 SelectObject(hdcMask, hbmOld);
249 DeleteObject(hbmMask);
250 DeleteDC(hdcMask);
251 }
252
253 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
254 {
255 void *pBitmapData;
256 LPBITMAPINFO pBitmapInfo;
257 HDC hdcMem;
258 HBITMAP hbmOld;
259 int nOffsetX = 0;
260 int nOffsetY = 0;
261 int nWidth;
262 int nHeight;
263
264 if (!hDC || !infoPtr->inbih)
265 return TRUE;
266
267 if (infoPtr->hic )
268 {
269 pBitmapData = infoPtr->outdata;
270 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
271
272 nWidth = infoPtr->outbih->biWidth;
273 nHeight = infoPtr->outbih->biHeight;
274 }
275 else
276 {
277 pBitmapData = infoPtr->indata;
278 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
279
280 nWidth = infoPtr->inbih->biWidth;
281 nHeight = infoPtr->inbih->biHeight;
282 }
283
284 if(!infoPtr->hbmPrevFrame)
285 {
286 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
287 }
288
289 hdcMem = CreateCompatibleDC(hDC);
290 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
291
292 SetDIBits(hdcMem, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, pBitmapInfo, DIB_RGB_COLORS);
293
294 /*
295 * we need to get the transparent color even without ACS_TRANSPARENT,
296 * because the style can be changed later on and the color should always
297 * be obtained in the first frame
298 */
299 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
300 {
301 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
302 }
303
304 if(infoPtr->dwStyle & ACS_TRANSPARENT)
305 {
306 HDC hdcFinal = CreateCompatibleDC(hDC);
307 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
308 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
309 RECT rect;
310
311 rect.left = 0;
312 rect.top = 0;
313 rect.right = nWidth;
314 rect.bottom = nHeight;
315
316 if(!infoPtr->hbrushBG)
317 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
318
319 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
320 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
321
322 SelectObject(hdcFinal, hbmOld2);
323 SelectObject(hdcMem, hbmFinal);
324 DeleteDC(hdcFinal);
325 DeleteObject(infoPtr->hbmPrevFrame);
326 infoPtr->hbmPrevFrame = hbmFinal;
327 }
328
329 if (infoPtr->dwStyle & ACS_CENTER)
330 {
331 RECT rect;
332
333 GetWindowRect(infoPtr->hwndSelf, &rect);
334 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
335 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
336 }
337 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
338
339 SelectObject(hdcMem, hbmOld);
340 DeleteDC(hdcMem);
341 return TRUE;
342 }
343
344 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
345 {
346 HDC hDC;
347
348 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
349
350 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
351 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
352
353 if (infoPtr->hic &&
354 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
355 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
356 WARN("Decompression error\n");
357 return FALSE;
358 }
359
360 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
361 ANIMATE_PaintFrame(infoPtr, hDC);
362 ReleaseDC(infoPtr->hwndSelf, hDC);
363 }
364
365 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
366 infoPtr->currFrame = infoPtr->nFromFrame;
367 if (infoPtr->nLoop != -1) {
368 if (--infoPtr->nLoop == 0) {
369 ANIMATE_DoStop(infoPtr);
370 }
371 }
372 }
373
374 return TRUE;
375 }
376
377 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
378 {
379 /* FIXME: we should pass the hDC instead of 0 to WM_CTLCOLORSTATIC */
380 if (infoPtr->dwStyle & ACS_TRANSPARENT)
381 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
382 WM_CTLCOLORSTATIC,
383 0, (LPARAM)infoPtr->hwndSelf);
384 EnterCriticalSection(&infoPtr->cs);
385 ANIMATE_DrawFrame(infoPtr);
386 LeaveCriticalSection(&infoPtr->cs);
387
388 return 0;
389 }
390
391 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
392 {
393 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)ptr_;
394 HANDLE event;
395 DWORD timeout;
396
397 while(1)
398 {
399 EnterCriticalSection(&infoPtr->cs);
400 ANIMATE_DrawFrame(infoPtr);
401 timeout = infoPtr->mah.dwMicroSecPerFrame;
402 event = infoPtr->hStopEvent;
403 LeaveCriticalSection(&infoPtr->cs);
404
405 /* time is in microseconds, we should convert it to milliseconds */
406 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
407 break;
408 }
409 return TRUE;
410 }
411
412 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
413 {
414 /* nothing opened */
415 if (!infoPtr->hMMio)
416 return FALSE;
417
418 if (infoPtr->hThread || infoPtr->uTimer) {
419 TRACE("Already playing\n");
420 return TRUE;
421 }
422
423 infoPtr->nFromFrame = wFrom;
424 infoPtr->nToFrame = wTo;
425 infoPtr->nLoop = cRepeat;
426
427 if (infoPtr->nToFrame == 0xFFFF)
428 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
429
430 TRACE("(repeat=%d from=%d to=%d);\n",
431 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
432
433 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
434 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
435 return FALSE;
436
437 infoPtr->currFrame = infoPtr->nFromFrame;
438
439 if (infoPtr->dwStyle & ACS_TIMER)
440 {
441 TRACE("Using a timer\n");
442 /* create a timer to display AVI */
443 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
444 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
445 }
446 else
447 {
448 if(infoPtr->dwStyle & ACS_TRANSPARENT)
449 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
450 WM_CTLCOLORSTATIC, 0,
451 (LPARAM)infoPtr->hwndSelf);
452
453 TRACE("Using an animation thread\n");
454 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
455 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
456 (LPVOID)infoPtr, 0, &infoPtr->threadId);
457 if(!infoPtr->hThread) return FALSE;
458
459 }
460
461 ANIMATE_Notify(infoPtr, ACN_START);
462
463 return TRUE;
464 }
465
466
467 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
468 {
469 MMCKINFO ckMainRIFF;
470 MMCKINFO mmckHead;
471 MMCKINFO mmckList;
472 MMCKINFO mmckInfo;
473 DWORD numFrame;
474 DWORD insize;
475
476 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
477 WARN("Can't find 'RIFF' chunk\n");
478 return FALSE;
479 }
480
481 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
482 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
483 WARN("Can't find 'AVI ' chunk\n");
484 return FALSE;
485 }
486
487 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
488 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
489 WARN("Can't find 'hdrl' list\n");
490 return FALSE;
491 }
492
493 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
494 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
495 WARN("Can't find 'avih' chunk\n");
496 return FALSE;
497 }
498
499 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
500
501 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
502 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
503 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
504 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
505 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
506 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
507 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
508 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
509 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
510 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
511
512 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
513
514 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
515 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
516 WARN("Can't find 'strl' list\n");
517 return FALSE;
518 }
519
520 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
521 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
522 WARN("Can't find 'strh' chunk\n");
523 return FALSE;
524 }
525
526 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
527
528 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
529 HIBYTE(LOWORD(infoPtr->ash.fccType)),
530 LOBYTE(HIWORD(infoPtr->ash.fccType)),
531 HIBYTE(HIWORD(infoPtr->ash.fccType)));
532 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
533 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
534 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
535 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
536 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
537 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
538 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
539 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
540 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
541 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
542 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
543 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
544 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
545 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
546 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
547 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
548 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
549
550 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
551
552 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
553 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
554 WARN("Can't find 'strh' chunk\n");
555 return FALSE;
556 }
557
558 infoPtr->inbih = Alloc(mmckInfo.cksize);
559 if (!infoPtr->inbih) {
560 WARN("Can't alloc input BIH\n");
561 return FALSE;
562 }
563
564 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
565
566 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
567 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
568 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
569 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
570 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
571 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
572 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
573 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
574 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
575 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
576 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
577
578 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
579
580 mmioAscend(infoPtr->hMMio, &mmckList, 0);
581
582 #if 0
583 /* an AVI has 0 or 1 video stream, and to be animated should not contain
584 * an audio stream, so only one strl is allowed
585 */
586 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
587 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
588 WARN("There should be a single 'strl' list\n");
589 return FALSE;
590 }
591 #endif
592
593 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
594
595 /* no need to read optional JUNK chunk */
596
597 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
598 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
599 WARN("Can't find 'movi' list\n");
600 return FALSE;
601 }
602
603 /* FIXME: should handle the 'rec ' LIST when present */
604
605 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
606 if (!infoPtr->lpIndex)
607 return FALSE;
608
609 numFrame = insize = 0;
610 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
611 numFrame < infoPtr->mah.dwTotalFrames) {
612 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
613 if (insize < mmckInfo.cksize)
614 insize = mmckInfo.cksize;
615 numFrame++;
616 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
617 }
618 if (numFrame != infoPtr->mah.dwTotalFrames) {
619 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
620 return FALSE;
621 }
622 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
623 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
624 infoPtr->ash.dwSuggestedBufferSize = insize;
625 }
626
627 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
628 if (!infoPtr->indata)
629 return FALSE;
630
631 return TRUE;
632 }
633
634
635 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
636 {
637 DWORD outSize;
638
639 /* check uncompressed AVI */
640 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
641 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
642 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
643 {
644 infoPtr->hic = 0;
645 return TRUE;
646 }
647
648 /* try to get a decompressor for that type */
649 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
650 if (!infoPtr->hic) {
651 WARN("Can't load codec for the file\n");
652 return FALSE;
653 }
654
655 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
656 (DWORD_PTR)infoPtr->inbih, 0L);
657
658 infoPtr->outbih = Alloc(outSize);
659 if (!infoPtr->outbih)
660 return FALSE;
661
662 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
663 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != outSize)
664 {
665 WARN("Can't get output BIH\n");
666 return FALSE;
667 }
668
669 infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
670 if (!infoPtr->outdata)
671 return FALSE;
672
673 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
674 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
675 WARN("Can't begin decompression\n");
676 return FALSE;
677 }
678
679 return TRUE;
680 }
681
682
683 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
684 {
685 ANIMATE_Free(infoPtr);
686
687 if (!lpszName)
688 {
689 TRACE("Closing avi!\n");
690 /* installer of thebat! v1.62 requires FALSE here */
691 return (infoPtr->hMMio != 0);
692 }
693
694 if (!hInstance)
695 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
696
697 TRACE("(%s)\n", debugstr_w(lpszName));
698
699 if (HIWORD(lpszName))
700 {
701 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
702 {
703 TRACE("No AVI resource found!\n");
704 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
705 {
706 WARN("No AVI file found!\n");
707 return FALSE;
708 }
709 }
710 }
711 else
712 {
713 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
714 {
715 WARN("No AVI resource found!\n");
716 return FALSE;
717 }
718 }
719
720 if (!ANIMATE_GetAviInfo(infoPtr))
721 {
722 WARN("Can't get AVI information\n");
723 ANIMATE_Free(infoPtr);
724 return FALSE;
725 }
726
727 if (!ANIMATE_GetAviCodec(infoPtr))
728 {
729 WARN("Can't get AVI Codec\n");
730 ANIMATE_Free(infoPtr);
731 return FALSE;
732 }
733
734 if (!(infoPtr->dwStyle & ACS_CENTER))
735 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
736 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
737
738 if (infoPtr->dwStyle & ACS_AUTOPLAY)
739 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
740
741 return TRUE;
742 }
743
744
745 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
746 {
747 LPWSTR lpwszName;
748 LRESULT result;
749 INT len;
750
751 if (!HIWORD(lpszName))
752 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
753
754 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
755 lpwszName = Alloc(len * sizeof(WCHAR));
756 if (!lpwszName) return FALSE;
757 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
758
759 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
760 Free (lpwszName);
761 return result;
762 }
763
764
765 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
766 {
767 /* nothing opened */
768 if (!infoPtr->hMMio)
769 return FALSE;
770
771 ANIMATE_DoStop(infoPtr);
772 return TRUE;
773 }
774
775
776 static BOOL ANIMATE_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
777 {
778 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
779 ANIMATE_INFO *infoPtr;
780
781 if (!fnIC.hModule)
782 {
783 fnIC.hModule = LoadLibraryW(msvfw32W);
784 if (!fnIC.hModule) return FALSE;
785
786 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
787 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
788 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
789 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
790 }
791
792 /* allocate memory for info structure */
793 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
794 if (!infoPtr) return FALSE;
795
796 /* store crossref hWnd <-> info structure */
797 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
798 infoPtr->hwndSelf = hWnd;
799 infoPtr->hwndNotify = lpcs->hwndParent;
800 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
801 infoPtr->hbmPrevFrame = 0;
802 infoPtr->dwStyle = lpcs->style;
803
804 TRACE("Animate style=0x%08lx, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
805
806 InitializeCriticalSection(&infoPtr->cs);
807
808 return TRUE;
809 }
810
811
812 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
813 {
814 /* free avi data */
815 ANIMATE_Free(infoPtr);
816
817 /* free animate info data */
818 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
819
820 DeleteCriticalSection(&infoPtr->cs);
821 Free(infoPtr);
822
823 return 0;
824 }
825
826
827 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO *infoPtr, HDC hdc)
828 {
829 RECT rect;
830 HBRUSH hBrush = 0;
831
832 if(infoPtr->dwStyle & ACS_TRANSPARENT)
833 {
834 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
835 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
836 }
837
838 GetClientRect(infoPtr->hwndSelf, &rect);
839 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
840
841 return TRUE;
842 }
843
844
845 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, LPSTYLESTRUCT lpss)
846 {
847 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
848 wStyleType, lpss->styleOld, lpss->styleNew);
849
850 if (wStyleType != GWL_STYLE) return 0;
851
852 infoPtr->dwStyle = lpss->styleNew;
853
854 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
855 return 0;
856 }
857
858
859 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
860 {
861 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
862
863 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
864 if (!infoPtr && (uMsg != WM_NCCREATE))
865 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
866 switch (uMsg)
867 {
868 case ACM_OPENA:
869 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
870
871 case ACM_OPENW:
872 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
873
874 case ACM_PLAY:
875 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
876
877 case ACM_STOP:
878 return ANIMATE_Stop(infoPtr);
879
880 case WM_CLOSE:
881 ANIMATE_Free(infoPtr);
882 return 0;
883
884 case WM_NCCREATE:
885 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
886
887 case WM_NCHITTEST:
888 return HTTRANSPARENT;
889
890 case WM_DESTROY:
891 return ANIMATE_Destroy(infoPtr);
892
893 case WM_ERASEBKGND:
894 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
895
896 case WM_STYLECHANGED:
897 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
898
899 case WM_TIMER:
900 return ANIMATE_Timer(infoPtr);
901
902 case WM_PRINTCLIENT:
903 case WM_PAINT:
904 {
905 /* the animation isn't playing, or has not decompressed
906 * (and displayed) the first frame yet, don't paint
907 */
908 if ((!infoPtr->uTimer && !infoPtr->hThread) ||
909 !infoPtr->hbmPrevFrame)
910 {
911 /* default paint handling */
912 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
913 }
914
915 if (infoPtr->dwStyle & ACS_TRANSPARENT)
916 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
917 WM_CTLCOLORSTATIC,
918 wParam, (LPARAM)infoPtr->hwndSelf);
919
920 if (wParam)
921 {
922 EnterCriticalSection(&infoPtr->cs);
923 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
924 LeaveCriticalSection(&infoPtr->cs);
925 }
926 else
927 {
928 PAINTSTRUCT ps;
929 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
930
931 EnterCriticalSection(&infoPtr->cs);
932 ANIMATE_PaintFrame(infoPtr, hDC);
933 LeaveCriticalSection(&infoPtr->cs);
934
935 EndPaint(infoPtr->hwndSelf, &ps);
936 }
937 }
938 break;
939
940 case WM_SIZE:
941 if (infoPtr->dwStyle & ACS_CENTER)
942 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
943 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
944
945 default:
946 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
947 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
948
949 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
950 }
951 return 0;
952 }
953
954 void ANIMATE_Register(void)
955 {
956 WNDCLASSW wndClass;
957
958 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
959 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
960 wndClass.lpfnWndProc = ANIMATE_WindowProc;
961 wndClass.cbClsExtra = 0;
962 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
963 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
964 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
965 wndClass.lpszClassName = ANIMATE_CLASSW;
966
967 RegisterClassW(&wndClass);
968 }
969
970
971 void ANIMATE_Unregister(void)
972 {
973 UnregisterClassW(ANIMATE_CLASSW, NULL);
974 }