Revert changes to the painting code, the WM_CTLCOLORSTATIC message
[wine/wine64.git] / dlls / comctl32 / animate.c
blobe420cb6939b182d74463b937e15badf4028c500c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
5 * Copyright 1998, 1999 Eric Kohl
6 * Copyright 1999 Eric Pouech
7 * Copyright 2005 Dimitrie O. Paun
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.
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.
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
23 * NOTES
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.
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.
32 * TODO:
33 * - check for the 'rec ' list in some AVI files
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"
50 WINE_DEFAULT_DEBUG_CHANNEL(animate);
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, DWORD);
57 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
58 } fnIC;
60 typedef struct
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;
95 #define ANIMATE_COLOR_NONE 0xffffffff
97 static void ANIMATE_Notify(ANIMATE_INFO *infoPtr, UINT notif)
99 SendMessageW(infoPtr->hwndNotify, WM_COMMAND,
100 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
101 (LPARAM)infoPtr->hwndSelf);
104 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPWSTR lpName)
106 static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
107 HRSRC hrsrc;
108 MMIOINFO mminfo;
109 LPVOID lpAvi;
111 hrsrc = FindResourceW(hInst, lpName, aviW);
112 if (!hrsrc)
113 return FALSE;
115 infoPtr->hRes = LoadResource(hInst, hrsrc);
116 if (!infoPtr->hRes)
117 return FALSE;
119 lpAvi = LockResource(infoPtr->hRes);
120 if (!lpAvi)
121 return FALSE;
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)
130 FreeResource(infoPtr->hRes);
131 return FALSE;
134 return TRUE;
138 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
140 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
142 return (BOOL)infoPtr->hMMio;
146 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
148 EnterCriticalSection(&infoPtr->cs);
150 /* should stop playing */
151 if (infoPtr->hThread)
153 HANDLE handle = infoPtr->hThread;
155 TRACE("stopping animation thread\n");
156 infoPtr->hThread = 0;
157 SetEvent( infoPtr->hStopEvent );
159 if (infoPtr->threadId != GetCurrentThreadId())
161 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
162 WaitForSingleObject( handle, INFINITE );
163 TRACE("animation thread stopped\n");
164 EnterCriticalSection(&infoPtr->cs);
167 CloseHandle( handle );
168 CloseHandle( infoPtr->hStopEvent );
169 infoPtr->hStopEvent = 0;
171 if (infoPtr->uTimer) {
172 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
173 infoPtr->uTimer = 0;
176 LeaveCriticalSection(&infoPtr->cs);
178 ANIMATE_Notify(infoPtr, ACN_STOP);
180 return TRUE;
184 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
186 if (infoPtr->hMMio) {
187 ANIMATE_DoStop(infoPtr);
188 mmioClose(infoPtr->hMMio, 0);
189 if (infoPtr->hRes) {
190 FreeResource(infoPtr->hRes);
191 infoPtr->hRes = 0;
193 HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
194 infoPtr->lpIndex = NULL;
195 if (infoPtr->hic) {
196 fnIC.fnICClose(infoPtr->hic);
197 infoPtr->hic = 0;
199 HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
200 infoPtr->inbih = NULL;
201 HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
202 infoPtr->outbih = NULL;
203 HeapFree(GetProcessHeap(), 0, infoPtr->indata);
204 infoPtr->indata = NULL;
205 HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
206 infoPtr->outdata = NULL;
207 if( infoPtr->hbmPrevFrame )
209 DeleteObject(infoPtr->hbmPrevFrame);
210 infoPtr->hbmPrevFrame = 0;
213 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
214 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
215 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
217 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
220 static void ANIMATE_TransparentBlt(ANIMATE_INFO *infoPtr, HDC hdcDest, HDC hdcSource)
222 HDC hdcMask;
223 HBITMAP hbmMask;
224 HBITMAP hbmOld;
226 /* create a transparency mask */
227 hdcMask = CreateCompatibleDC(hdcDest);
228 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
229 hbmOld = SelectObject(hdcMask, hbmMask);
231 SetBkColor(hdcSource,infoPtr->transparentColor);
232 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
234 /* mask the source bitmap */
235 SetBkColor(hdcSource, RGB(0,0,0));
236 SetTextColor(hdcSource, RGB(255,255,255));
237 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
239 /* mask the destination bitmap */
240 SetBkColor(hdcDest, RGB(255,255,255));
241 SetTextColor(hdcDest, RGB(0,0,0));
242 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
244 /* combine source and destination */
245 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
247 SelectObject(hdcMask, hbmOld);
248 DeleteObject(hbmMask);
249 DeleteDC(hdcMask);
252 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
254 void* pBitmapData = NULL;
255 LPBITMAPINFO pBitmapInfo = NULL;
257 HDC hdcMem;
258 HBITMAP hbmOld;
260 int nOffsetX = 0;
261 int nOffsetY = 0;
263 int nWidth;
264 int nHeight;
266 if (!hDC || !infoPtr->inbih)
267 return TRUE;
269 if (infoPtr->hic )
271 pBitmapData = infoPtr->outdata;
272 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
274 nWidth = infoPtr->outbih->biWidth;
275 nHeight = infoPtr->outbih->biHeight;
277 else
279 pBitmapData = infoPtr->indata;
280 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
282 nWidth = infoPtr->inbih->biWidth;
283 nHeight = infoPtr->inbih->biHeight;
286 if(!infoPtr->hbmPrevFrame)
288 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
291 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
293 hdcMem = CreateCompatibleDC(hDC);
294 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
297 * we need to get the transparent color even without ACS_TRANSPARENT,
298 * because the style can be changed later on and the color should always
299 * be obtained in the first frame
301 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
303 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
306 if(infoPtr->dwStyle & ACS_TRANSPARENT)
308 HDC hdcFinal = CreateCompatibleDC(hDC);
309 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
310 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
311 RECT rect;
313 rect.left = 0;
314 rect.top = 0;
315 rect.right = nWidth;
316 rect.bottom = nHeight;
318 if(!infoPtr->hbrushBG)
319 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
321 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
322 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
324 SelectObject(hdcFinal, hbmOld2);
325 SelectObject(hdcMem, hbmFinal);
326 DeleteDC(hdcFinal);
327 DeleteObject(infoPtr->hbmPrevFrame);
328 infoPtr->hbmPrevFrame = hbmFinal;
331 if (infoPtr->dwStyle & ACS_CENTER)
333 RECT rect;
335 GetWindowRect(infoPtr->hwndSelf, &rect);
336 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
337 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
339 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
341 SelectObject(hdcMem, hbmOld);
342 DeleteDC(hdcMem);
343 return TRUE;
346 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
348 HDC hDC;
350 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
352 EnterCriticalSection(&infoPtr->cs);
354 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
355 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
357 if (infoPtr->hic &&
358 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
359 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
360 LeaveCriticalSection(&infoPtr->cs);
361 WARN("Decompression error\n");
362 return FALSE;
365 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
366 ANIMATE_PaintFrame(infoPtr, hDC);
367 ReleaseDC(infoPtr->hwndSelf, hDC);
370 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
371 infoPtr->currFrame = infoPtr->nFromFrame;
372 if (infoPtr->nLoop != -1) {
373 if (--infoPtr->nLoop == 0) {
374 ANIMATE_DoStop(infoPtr);
378 LeaveCriticalSection(&infoPtr->cs);
380 return TRUE;
383 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
385 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)ptr_;
386 HANDLE event;
387 DWORD timeout;
389 while(1)
391 EnterCriticalSection(&infoPtr->cs);
392 ANIMATE_DrawFrame(infoPtr);
393 timeout = infoPtr->mah.dwMicroSecPerFrame;
394 event = infoPtr->hStopEvent;
395 LeaveCriticalSection(&infoPtr->cs);
397 /* time is in microseconds, we should convert it to milliseconds */
398 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
399 break;
401 return TRUE;
404 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
406 /* nothing opened */
407 if (!infoPtr->hMMio)
408 return FALSE;
410 if (infoPtr->hThread || infoPtr->uTimer) {
411 TRACE("Already playing\n");
412 return TRUE;
415 infoPtr->nFromFrame = wFrom;
416 infoPtr->nToFrame = wTo;
417 infoPtr->nLoop = cRepeat;
419 if (infoPtr->nToFrame == 0xFFFF)
420 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
422 TRACE("(repeat=%d from=%d to=%d);\n",
423 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
425 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
426 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
427 return FALSE;
429 infoPtr->currFrame = infoPtr->nFromFrame;
431 if (infoPtr->dwStyle & ACS_TIMER)
433 TRACE("Using a timer\n");
434 /* create a timer to display AVI */
435 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
436 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
438 else
440 if(infoPtr->dwStyle & ACS_TRANSPARENT)
441 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
442 WM_CTLCOLORSTATIC, 0,
443 (LPARAM)infoPtr->hwndSelf);
445 TRACE("Using an animation thread\n");
446 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
447 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
448 (LPVOID)infoPtr, 0, &infoPtr->threadId);
449 if(!infoPtr->hThread) return FALSE;
453 ANIMATE_Notify(infoPtr, ACN_START);
455 return TRUE;
459 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
461 MMCKINFO ckMainRIFF;
462 MMCKINFO mmckHead;
463 MMCKINFO mmckList;
464 MMCKINFO mmckInfo;
465 DWORD numFrame;
466 DWORD insize;
468 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
469 WARN("Can't find 'RIFF' chunk\n");
470 return FALSE;
473 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
474 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
475 WARN("Can't find 'AVI ' chunk\n");
476 return FALSE;
479 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
480 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
481 WARN("Can't find 'hdrl' list\n");
482 return FALSE;
485 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
486 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
487 WARN("Can't find 'avih' chunk\n");
488 return FALSE;
491 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
493 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
494 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
495 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
496 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
497 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
498 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
499 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
500 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
501 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
502 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
504 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
506 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
507 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
508 WARN("Can't find 'strl' list\n");
509 return FALSE;
512 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
513 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
514 WARN("Can't find 'strh' chunk\n");
515 return FALSE;
518 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
520 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
521 HIBYTE(LOWORD(infoPtr->ash.fccType)),
522 LOBYTE(HIWORD(infoPtr->ash.fccType)),
523 HIBYTE(HIWORD(infoPtr->ash.fccType)));
524 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
525 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
526 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
527 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
528 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
529 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
530 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
531 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
532 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
533 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
534 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
535 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
536 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
537 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
538 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
539 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
540 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
542 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
544 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
545 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
546 WARN("Can't find 'strh' chunk\n");
547 return FALSE;
550 infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
551 if (!infoPtr->inbih) {
552 WARN("Can't alloc input BIH\n");
553 return FALSE;
556 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
558 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
559 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
560 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
561 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
562 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
563 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
564 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
565 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
566 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
567 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
568 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
570 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
572 mmioAscend(infoPtr->hMMio, &mmckList, 0);
574 #if 0
575 /* an AVI has 0 or 1 video stream, and to be animated should not contain
576 * an audio stream, so only one strl is allowed
578 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
579 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
580 WARN("There should be a single 'strl' list\n");
581 return FALSE;
583 #endif
585 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
587 /* no need to read optional JUNK chunk */
589 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
590 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
591 WARN("Can't find 'movi' list\n");
592 return FALSE;
595 /* FIXME: should handle the 'rec ' LIST when present */
597 infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
598 infoPtr->mah.dwTotalFrames * sizeof(DWORD));
599 if (!infoPtr->lpIndex)
600 return FALSE;
602 numFrame = insize = 0;
603 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
604 numFrame < infoPtr->mah.dwTotalFrames) {
605 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
606 if (insize < mmckInfo.cksize)
607 insize = mmckInfo.cksize;
608 numFrame++;
609 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
611 if (numFrame != infoPtr->mah.dwTotalFrames) {
612 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
613 return FALSE;
615 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
616 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
617 infoPtr->ash.dwSuggestedBufferSize = insize;
620 infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
621 if (!infoPtr->indata)
622 return FALSE;
624 return TRUE;
628 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
630 DWORD outSize;
632 /* check uncompressed AVI */
633 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
634 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
635 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
637 infoPtr->hic = 0;
638 return TRUE;
641 /* try to get a decompressor for that type */
642 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
643 if (!infoPtr->hic) {
644 WARN("Can't load codec for the file\n");
645 return FALSE;
648 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
649 (DWORD)infoPtr->inbih, 0L);
651 infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
652 if (!infoPtr->outbih)
653 return FALSE;
655 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
656 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize)
658 WARN("Can't get output BIH\n");
659 return FALSE;
662 infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
663 if (!infoPtr->outdata)
664 return FALSE;
666 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
667 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
668 WARN("Can't begin decompression\n");
669 return FALSE;
672 return TRUE;
676 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
678 ANIMATE_Free(infoPtr);
680 if (!lpszName)
682 TRACE("Closing avi!\n");
683 /* installer of thebat! v1.62 requires FALSE here */
684 return (infoPtr->hMMio != 0);
687 if (!hInstance)
688 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
690 if (HIWORD(lpszName))
692 TRACE("(\"%s\");\n", debugstr_w(lpszName));
694 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
696 TRACE("No AVI resource found!\n");
697 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
699 WARN("No AVI file found!\n");
700 return FALSE;
704 else
706 TRACE("(%u);\n", (WORD)(DWORD)lpszName);
708 if (!ANIMATE_LoadResW(infoPtr, hInstance, MAKEINTRESOURCEW((INT)lpszName)))
710 WARN("No AVI resource found!\n");
711 return FALSE;
715 if (!ANIMATE_GetAviInfo(infoPtr))
717 WARN("Can't get AVI information\n");
718 ANIMATE_Free(infoPtr);
719 return FALSE;
722 if (!ANIMATE_GetAviCodec(infoPtr))
724 WARN("Can't get AVI Codec\n");
725 ANIMATE_Free(infoPtr);
726 return FALSE;
729 if (!(infoPtr->dwStyle & ACS_CENTER))
730 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
731 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
733 if (infoPtr->dwStyle & ACS_AUTOPLAY)
734 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
736 return TRUE;
740 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
742 LPWSTR lpwszName;
743 LRESULT result;
744 INT len;
746 if (!HIWORD(lpszName))
747 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
749 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
750 lpwszName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
751 if (!lpwszName) return FALSE;
752 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
754 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
755 HeapFree(GetProcessHeap(), 0, lpwszName);
756 return result;
760 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
762 /* nothing opened */
763 if (!infoPtr->hMMio)
764 return FALSE;
766 ANIMATE_DoStop(infoPtr);
767 return TRUE;
771 static BOOL ANIMATE_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
773 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
774 ANIMATE_INFO *infoPtr;
776 if (!fnIC.hModule)
778 fnIC.hModule = LoadLibraryW(msvfw32W);
779 if (!fnIC.hModule) return FALSE;
781 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
782 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
783 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
784 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
787 /* allocate memory for info structure */
788 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
789 if (!infoPtr) return FALSE;
791 /* store crossref hWnd <-> info structure */
792 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
793 infoPtr->hwndSelf = hWnd;
794 infoPtr->hwndNotify = lpcs->hwndParent;
795 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
796 infoPtr->hbmPrevFrame = 0;
797 infoPtr->dwStyle = lpcs->style;
799 TRACE("Animate style=0x%08lx, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
801 InitializeCriticalSection(&infoPtr->cs);
803 return TRUE;
807 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
809 /* free avi data */
810 ANIMATE_Free(infoPtr);
812 /* free animate info data */
813 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
814 Free(infoPtr);
816 return 0;
820 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO *infoPtr, HDC hdc)
822 RECT rect;
823 HBRUSH hBrush = 0;
825 if(infoPtr->dwStyle & ACS_TRANSPARENT)
827 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
828 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
831 GetClientRect(infoPtr->hwndSelf, &rect);
832 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
834 return TRUE;
838 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, LPSTYLESTRUCT lpss)
840 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
841 wStyleType, lpss->styleOld, lpss->styleNew);
843 if (wStyleType != GWL_STYLE) return 0;
845 infoPtr->dwStyle = lpss->styleNew;
847 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
848 return 0;
852 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
854 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
856 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
857 if (!infoPtr && (uMsg != WM_NCCREATE))
858 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
859 switch (uMsg)
861 case ACM_OPENA:
862 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
864 case ACM_OPENW:
865 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
867 case ACM_PLAY:
868 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
870 case ACM_STOP:
871 return ANIMATE_Stop(infoPtr);
873 case WM_CLOSE:
874 ANIMATE_Free(infoPtr);
875 return 0;
877 case WM_NCCREATE:
878 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
880 case WM_NCHITTEST:
881 return HTTRANSPARENT;
883 case WM_DESTROY:
884 return ANIMATE_Destroy(infoPtr);
886 case WM_ERASEBKGND:
887 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
889 case WM_STYLECHANGED:
890 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
892 case WM_TIMER:
893 if (infoPtr->dwStyle & ACS_TRANSPARENT)
894 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
895 WM_CTLCOLORSTATIC,
896 wParam, (LPARAM)infoPtr->hwndSelf);
897 return ANIMATE_DrawFrame(infoPtr);
899 case WM_PAINT:
901 /* the animation isn't playing, or has not decompressed
902 * (and displayed) the first frame yet, don't paint
904 if ((!infoPtr->uTimer && !infoPtr->hThread) ||
905 !infoPtr->hbmPrevFrame)
907 /* default paint handling */
908 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
911 if (infoPtr->dwStyle & ACS_TRANSPARENT)
912 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
913 WM_CTLCOLORSTATIC,
914 wParam, (LPARAM)infoPtr->hwndSelf);
916 if (wParam)
918 EnterCriticalSection(&infoPtr->cs);
919 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
920 LeaveCriticalSection(&infoPtr->cs);
922 else
924 PAINTSTRUCT ps;
925 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
927 EnterCriticalSection(&infoPtr->cs);
928 ANIMATE_PaintFrame(infoPtr, hDC);
929 LeaveCriticalSection(&infoPtr->cs);
931 EndPaint(infoPtr->hwndSelf, &ps);
934 break;
936 case WM_SIZE:
937 if (infoPtr->dwStyle & ACS_CENTER)
938 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
939 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
941 default:
942 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
943 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
945 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
947 return 0;
950 void ANIMATE_Register(void)
952 WNDCLASSW wndClass;
954 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
955 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
956 wndClass.lpfnWndProc = ANIMATE_WindowProc;
957 wndClass.cbClsExtra = 0;
958 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
959 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
960 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
961 wndClass.lpszClassName = ANIMATE_CLASSW;
963 RegisterClassW(&wndClass);
967 void ANIMATE_Unregister(void)
969 UnregisterClassW(ANIMATE_CLASSW, NULL);