Remove bFirstPain funky optimization, it is causing too much grief.
[wine/wine64.git] / dlls / comctl32 / animate.c
blob066dfc7cda7266932d4b1a782a1f230771454eb0
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
5 * Copyright 1998, 1999 Eric Kohl
6 * 1999 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * NOTES
23 * I will only improve this control once in a while.
24 * Eric <ekohl@abo.rhein-zeitung.de>
26 * TODO:
27 * - check for the 'rec ' list in some AVI files
28 * - concurrent access to infoPtr
31 #define COM_NO_WINDOWS_H
32 #include <string.h>
33 #include "winbase.h"
34 #include "commctrl.h"
35 #include "vfw.h"
36 #include "mmsystem.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(animate);
41 static struct {
42 HMODULE hModule;
43 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
44 LRESULT (WINAPI *fnICClose)(HIC);
45 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD, DWORD);
46 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
47 } fnIC;
49 typedef struct
51 /* reference to input stream (file or resource) */
52 HGLOBAL hRes;
53 HMMIO hMMio; /* handle to mmio stream */
54 HWND hWnd;
55 /* information on the loaded AVI file */
56 MainAVIHeader mah;
57 AVIStreamHeader ash;
58 LPBITMAPINFOHEADER inbih;
59 LPDWORD lpIndex;
60 /* data for the decompressor */
61 HIC hic;
62 LPBITMAPINFOHEADER outbih;
63 LPVOID indata;
64 LPVOID outdata;
65 /* data for the background mechanism */
66 CRITICAL_SECTION cs;
67 HANDLE hThread;
68 UINT uTimer;
69 /* data for playing the file */
70 int nFromFrame;
71 int nToFrame;
72 int nLoop;
73 int currFrame;
74 /* tranparency info*/
75 COLORREF transparentColor;
76 HBRUSH hbrushBG;
77 HBITMAP hbmPrevFrame;
78 } ANIMATE_INFO;
80 #define ANIMATE_GetInfoPtr(hWnd) ((ANIMATE_INFO *)GetWindowLongA(hWnd, 0))
81 #define ANIMATE_COLOR_NONE 0xffffffff
83 static void ANIMATE_Notify(ANIMATE_INFO* infoPtr, UINT notif)
85 SendMessageA(GetParent(infoPtr->hWnd), WM_COMMAND,
86 MAKEWPARAM(GetDlgCtrlID(infoPtr->hWnd), notif),
87 (LPARAM)infoPtr->hWnd);
90 static BOOL ANIMATE_LoadResA(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPSTR lpName)
92 HRSRC hrsrc;
93 MMIOINFO mminfo;
94 LPVOID lpAvi;
96 hrsrc = FindResourceA(hInst, lpName, "AVI");
97 if (!hrsrc)
98 return FALSE;
100 infoPtr->hRes = LoadResource(hInst, hrsrc);
101 if (!infoPtr->hRes)
102 return FALSE;
104 lpAvi = LockResource(infoPtr->hRes);
105 if (!lpAvi)
106 return FALSE;
108 memset(&mminfo, 0, sizeof(mminfo));
109 mminfo.fccIOProc = FOURCC_MEM;
110 mminfo.pchBuffer = (LPSTR)lpAvi;
111 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
112 infoPtr->hMMio = mmioOpenA(NULL, &mminfo, MMIO_READ);
113 if (!infoPtr->hMMio) {
114 GlobalFree((HGLOBAL)lpAvi);
115 return FALSE;
118 return TRUE;
122 static BOOL ANIMATE_LoadFileA(ANIMATE_INFO *infoPtr, LPSTR lpName)
124 infoPtr->hMMio = mmioOpenA((LPSTR)lpName, NULL,
125 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
127 if (!infoPtr->hMMio)
128 return FALSE;
130 return TRUE;
134 static LRESULT ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
136 EnterCriticalSection(&infoPtr->cs);
138 /* should stop playing */
139 if (infoPtr->hThread)
141 if (!TerminateThread(infoPtr->hThread,0))
142 WARN("could not destroy animation thread!\n");
143 infoPtr->hThread = 0;
145 if (infoPtr->uTimer) {
146 KillTimer(infoPtr->hWnd, infoPtr->uTimer);
147 infoPtr->uTimer = 0;
150 LeaveCriticalSection(&infoPtr->cs);
152 ANIMATE_Notify(infoPtr, ACN_STOP);
154 return TRUE;
158 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
160 if (infoPtr->hMMio) {
161 ANIMATE_DoStop(infoPtr);
162 mmioClose(infoPtr->hMMio, 0);
163 if (infoPtr->hRes) {
164 FreeResource(infoPtr->hRes);
165 infoPtr->hRes = 0;
167 if (infoPtr->lpIndex) {
168 HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
169 infoPtr->lpIndex = NULL;
171 if (infoPtr->hic) {
172 fnIC.fnICClose(infoPtr->hic);
173 infoPtr->hic = 0;
175 if (infoPtr->inbih) {
176 HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
177 infoPtr->inbih = NULL;
179 if (infoPtr->outbih) {
180 HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
181 infoPtr->outbih = NULL;
183 if( infoPtr->indata )
185 HeapFree(GetProcessHeap(), 0, infoPtr->indata);
186 infoPtr->indata = NULL;
188 if( infoPtr->outdata )
190 HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
191 infoPtr->outdata = NULL;
193 if( infoPtr->hbmPrevFrame )
195 DeleteObject(infoPtr->hbmPrevFrame);
196 infoPtr->hbmPrevFrame = 0;
198 infoPtr->indata = infoPtr->outdata = NULL;
199 infoPtr->hWnd = 0;
200 infoPtr->hMMio = 0;
202 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
203 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
204 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
206 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
209 static void ANIMATE_TransparentBlt(ANIMATE_INFO* infoPtr, HDC hdcDest, HDC hdcSource)
211 HDC hdcMask;
212 HBITMAP hbmMask;
213 HBITMAP hbmOld;
215 /* create a transparency mask */
216 hdcMask = CreateCompatibleDC(hdcDest);
217 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
218 hbmOld = SelectObject(hdcMask, hbmMask);
220 SetBkColor(hdcSource,infoPtr->transparentColor);
221 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
223 /* mask the source bitmap */
224 SetBkColor(hdcSource, RGB(0,0,0));
225 SetTextColor(hdcSource, RGB(255,255,255));
226 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
228 /* mask the destination bitmap */
229 SetBkColor(hdcDest, RGB(255,255,255));
230 SetTextColor(hdcDest, RGB(0,0,0));
231 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
233 /* combine source and destination */
234 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
236 SelectObject(hdcMask, hbmOld);
237 DeleteObject(hbmMask);
238 DeleteDC(hdcMask);
241 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
243 void* pBitmapData = NULL;
244 LPBITMAPINFO pBitmapInfo = NULL;
246 HDC hdcMem;
247 HBITMAP hbmOld;
249 int nOffsetX = 0;
250 int nOffsetY = 0;
252 int nWidth;
253 int nHeight;
255 if (!hDC || !infoPtr->inbih)
256 return TRUE;
258 if (infoPtr->hic )
260 pBitmapData = infoPtr->outdata;
261 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
263 nWidth = infoPtr->outbih->biWidth;
264 nHeight = infoPtr->outbih->biHeight;
265 } else
267 pBitmapData = infoPtr->indata;
268 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
270 nWidth = infoPtr->inbih->biWidth;
271 nHeight = infoPtr->inbih->biHeight;
274 if(!infoPtr->hbmPrevFrame)
276 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
279 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
281 hdcMem = CreateCompatibleDC(hDC);
282 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
285 * we need to get the transparent color even without ACS_TRANSPARENT,
286 * because the style can be changed later on and the color should always
287 * be obtained in the first frame
289 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
291 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
294 if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT)
296 HDC hdcFinal = CreateCompatibleDC(hDC);
297 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
298 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
299 RECT rect;
301 rect.left = 0;
302 rect.top = 0;
303 rect.right = nWidth;
304 rect.bottom = nHeight;
306 if(!infoPtr->hbrushBG)
307 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
309 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
310 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
312 SelectObject(hdcFinal, hbmOld2);
313 SelectObject(hdcMem, hbmFinal);
314 DeleteDC(hdcFinal);
315 DeleteObject(infoPtr->hbmPrevFrame);
316 infoPtr->hbmPrevFrame = hbmFinal;
319 if (GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_CENTER)
321 RECT rect;
323 GetWindowRect(infoPtr->hWnd, &rect);
324 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
325 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
327 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
329 SelectObject(hdcMem, hbmOld);
330 DeleteDC(hdcMem);
331 return TRUE;
334 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
336 HDC hDC;
338 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
340 EnterCriticalSection(&infoPtr->cs);
342 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
343 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
345 if (infoPtr->hic &&
346 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
347 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
348 LeaveCriticalSection(&infoPtr->cs);
349 WARN("Decompression error\n");
350 return FALSE;
353 if ((hDC = GetDC(infoPtr->hWnd)) != 0) {
354 ANIMATE_PaintFrame(infoPtr, hDC);
355 ReleaseDC(infoPtr->hWnd, hDC);
358 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
359 infoPtr->currFrame = infoPtr->nFromFrame;
360 if (infoPtr->nLoop != -1) {
361 if (--infoPtr->nLoop == 0) {
362 ANIMATE_DoStop(infoPtr);
366 LeaveCriticalSection(&infoPtr->cs);
368 return TRUE;
371 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
373 ANIMATE_INFO* infoPtr = (ANIMATE_INFO*)ptr_;
374 HDC hDC;
376 if(!infoPtr)
378 WARN("animation structure undefined!\n");
379 return FALSE;
382 while(1)
384 if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT)
386 hDC = GetDC(infoPtr->hWnd);
387 /* sometimes the animation window will be destroyed in between
388 * by the main program, so a ReleaseDC() error msg is possible */
389 infoPtr->hbrushBG = (HBRUSH)SendMessageA(GetParent(infoPtr->hWnd),
390 WM_CTLCOLORSTATIC, (WPARAM)hDC,
391 (LPARAM)infoPtr->hWnd);
392 ReleaseDC(infoPtr->hWnd,hDC);
395 EnterCriticalSection(&infoPtr->cs);
396 ANIMATE_DrawFrame(infoPtr);
397 LeaveCriticalSection(&infoPtr->cs);
399 /* time is in microseconds, we should convert it to milliseconds */
400 Sleep((infoPtr->mah.dwMicroSecPerFrame+500)/1000);
402 return TRUE;
405 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
407 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
409 /* nothing opened */
410 if (!infoPtr->hMMio)
411 return FALSE;
413 if (infoPtr->hThread || infoPtr->uTimer) {
414 FIXME("Already playing ? what should I do ??\n");
415 ANIMATE_DoStop(infoPtr);
418 infoPtr->nFromFrame = (INT)LOWORD(lParam);
419 infoPtr->nToFrame = (INT)HIWORD(lParam);
420 infoPtr->nLoop = (INT)wParam;
422 if (infoPtr->nToFrame == 0xFFFF)
423 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
425 TRACE("(repeat=%d from=%d to=%d);\n",
426 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
428 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
429 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
430 return FALSE;
432 infoPtr->currFrame = infoPtr->nFromFrame;
434 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
435 TRACE("Using a timer\n");
436 /* create a timer to display AVI */
437 infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
438 } else {
439 DWORD threadID;
441 TRACE("Using an animation thread\n");
442 infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr, 0, &threadID);
443 if(!infoPtr->hThread)
445 ERR("Could not create animation thread!\n");
446 return FALSE;
451 ANIMATE_Notify(infoPtr, ACN_START);
453 return TRUE;
457 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
459 MMCKINFO ckMainRIFF;
460 MMCKINFO mmckHead;
461 MMCKINFO mmckList;
462 MMCKINFO mmckInfo;
463 DWORD numFrame;
464 DWORD insize;
466 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
467 WARN("Can't find 'RIFF' chunk\n");
468 return FALSE;
471 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
472 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
473 WARN("Can't find 'AVI ' chunk\n");
474 return FALSE;
477 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
478 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
479 WARN("Can't find 'hdrl' list\n");
480 return FALSE;
483 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
484 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
485 WARN("Can't find 'avih' chunk\n");
486 return FALSE;
489 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
491 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
492 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
493 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
494 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
495 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
496 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
497 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
498 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
499 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
500 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
502 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
504 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
505 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
506 WARN("Can't find 'strl' list\n");
507 return FALSE;
510 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
511 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
512 WARN("Can't find 'strh' chunk\n");
513 return FALSE;
516 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
518 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
519 HIBYTE(LOWORD(infoPtr->ash.fccType)),
520 LOBYTE(HIWORD(infoPtr->ash.fccType)),
521 HIBYTE(HIWORD(infoPtr->ash.fccType)));
522 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
523 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
524 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
525 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
526 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
527 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
528 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
529 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
530 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
531 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
532 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
533 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
534 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
535 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
536 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
537 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
538 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
540 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
542 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
543 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
544 WARN("Can't find 'strh' chunk\n");
545 return FALSE;
548 infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
549 if (!infoPtr->inbih) {
550 WARN("Can't alloc input BIH\n");
551 return FALSE;
554 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
556 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
557 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
558 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
559 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
560 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
561 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
562 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
563 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
564 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
565 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
566 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
568 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
570 mmioAscend(infoPtr->hMMio, &mmckList, 0);
572 #if 0
573 /* an AVI has 0 or 1 video stream, and to be animated should not contain
574 * an audio stream, so only one strl is allowed
576 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
577 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
578 WARN("There should be a single 'strl' list\n");
579 return FALSE;
581 #endif
583 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
585 /* no need to read optional JUNK chunk */
587 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
588 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
589 WARN("Can't find 'movi' list\n");
590 return FALSE;
593 /* FIXME: should handle the 'rec ' LIST when present */
595 infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
596 infoPtr->mah.dwTotalFrames * sizeof(DWORD));
597 if (!infoPtr->lpIndex) {
598 WARN("Can't alloc index array\n");
599 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 WARN("Can't alloc input buffer\n");
623 return FALSE;
626 return TRUE;
630 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
632 DWORD outSize;
634 /* check uncompressed AVI */
635 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
636 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
637 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
639 infoPtr->hic = 0;
640 return TRUE;
643 /* try to get a decompressor for that type */
644 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
645 if (!infoPtr->hic) {
646 WARN("Can't load codec for the file\n");
647 return FALSE;
650 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
651 (DWORD)infoPtr->inbih, 0L);
653 infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
654 if (!infoPtr->outbih) {
655 WARN("Can't alloc output BIH\n");
656 return FALSE;
659 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
660 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize) {
661 WARN("Can't get output BIH\n");
662 return FALSE;
665 infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
666 if (!infoPtr->outdata) {
667 WARN("Can't alloc output buffer\n");
668 return FALSE;
671 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
672 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
673 WARN("Can't begin decompression\n");
674 return FALSE;
677 return TRUE;
680 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
682 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
683 HINSTANCE hInstance = (HINSTANCE)wParam;
685 ANIMATE_Free(infoPtr);
686 infoPtr->hWnd = hWnd;
688 if (!lParam) {
689 TRACE("Closing avi!\n");
690 /* installer of thebat! v1.62 requires FALSE here */
691 return (infoPtr->hMMio != 0);
694 if (!hInstance)
695 hInstance = (HINSTANCE)GetWindowLongA(hWnd, GWL_HINSTANCE);
697 if (HIWORD(lParam)) {
698 TRACE("(\"%s\");\n", (LPSTR)lParam);
700 if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
701 TRACE("No AVI resource found!\n");
702 if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
703 WARN("No AVI file found!\n");
704 return FALSE;
707 } else {
708 TRACE("(%u);\n", (WORD)LOWORD(lParam));
710 if (!ANIMATE_LoadResA(infoPtr, hInstance,
711 MAKEINTRESOURCEA((INT)lParam))) {
712 WARN("No AVI resource found!\n");
713 return FALSE;
717 if (!ANIMATE_GetAviInfo(infoPtr)) {
718 WARN("Can't get AVI information\n");
719 ANIMATE_Free(infoPtr);
720 return FALSE;
723 if (!ANIMATE_GetAviCodec(infoPtr)) {
724 WARN("Can't get AVI Codec\n");
725 ANIMATE_Free(infoPtr);
726 return FALSE;
729 if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
730 SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
731 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
734 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
735 return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
738 return TRUE;
742 /* << ANIMATE_Open32W >> */
744 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
746 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
748 /* nothing opened */
749 if (!infoPtr->hMMio)
750 return FALSE;
752 ANIMATE_DoStop(infoPtr);
753 return TRUE;
757 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
759 ANIMATE_INFO* infoPtr;
761 if (!fnIC.hModule) /* FIXME: not thread safe */
763 /* since there's a circular dep between msvfw32 and comctl32, we could either:
764 * - fix the build chain to allow this circular dep
765 * - handle it by hand
766 * AJ wants the latter :-(
768 fnIC.hModule = LoadLibraryA("msvfw32.dll");
769 if (!fnIC.hModule) return FALSE;
771 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
772 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
773 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
774 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
777 /* allocate memory for info structure */
778 infoPtr = (ANIMATE_INFO *)COMCTL32_Alloc(sizeof(ANIMATE_INFO));
779 if (!infoPtr) {
780 ERR("could not allocate info memory!\n");
781 return 0;
784 TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)GetParent(hWnd));
786 /* store crossref hWnd <-> info structure */
787 SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
788 infoPtr->hWnd = hWnd;
789 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
790 infoPtr->hbmPrevFrame = 0;
792 InitializeCriticalSection(&infoPtr->cs);
794 return 0;
798 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
800 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
803 /* free avi data */
804 ANIMATE_Free(infoPtr);
806 /* free animate info data */
807 COMCTL32_Free(infoPtr);
808 SetWindowLongA(hWnd, 0, 0);
810 return 0;
814 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
816 RECT rect;
817 HBRUSH hBrush = 0;
819 if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
821 hBrush = (HBRUSH)SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,
822 wParam, (LPARAM)hWnd);
825 GetClientRect(hWnd, &rect);
826 FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
828 return TRUE;
831 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
833 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
834 InvalidateRect(hWnd, NULL, TRUE);
836 return TRUE;
839 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
841 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
842 if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
843 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
844 switch (uMsg)
846 case ACM_OPENA:
847 return ANIMATE_OpenA(hWnd, wParam, lParam);
849 /* case ACM_OPEN32W: FIXME!! */
850 /* return ANIMATE_Open32W(hWnd, wParam, lParam); */
852 case ACM_PLAY:
853 return ANIMATE_Play(hWnd, wParam, lParam);
855 case ACM_STOP:
856 return ANIMATE_Stop(hWnd, wParam, lParam);
858 case WM_NCCREATE:
859 ANIMATE_Create(hWnd, wParam, lParam);
860 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
862 case WM_NCHITTEST:
863 return HTTRANSPARENT;
865 case WM_DESTROY:
866 ANIMATE_Destroy(hWnd, wParam, lParam);
867 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
869 case WM_ERASEBKGND:
870 ANIMATE_EraseBackground(hWnd, wParam, lParam);
871 break;
873 /* case WM_STYLECHANGED: FIXME shall we do something ?? */
875 case WM_TIMER:
876 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
878 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
879 infoPtr->hbrushBG = (HBRUSH)SendMessageA(GetParent(hWnd),
880 WM_CTLCOLORSTATIC,
881 wParam, (LPARAM)hWnd);
883 return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
885 case WM_CLOSE:
886 ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
887 return TRUE;
889 case WM_PAINT:
891 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
893 /* the animation isn't playing, don't paint */
894 if(!infoPtr->uTimer && !infoPtr->hThread)
895 /* default paint handling */
896 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
898 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
899 infoPtr->hbrushBG = (HBRUSH)SendMessageA(GetParent(hWnd),
900 WM_CTLCOLORSTATIC,
901 wParam, (LPARAM)hWnd);
903 if (wParam)
905 EnterCriticalSection(&infoPtr->cs);
906 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
907 LeaveCriticalSection(&infoPtr->cs);
909 else
911 PAINTSTRUCT ps;
912 HDC hDC = BeginPaint(hWnd, &ps);
914 EnterCriticalSection(&infoPtr->cs);
915 ANIMATE_PaintFrame(infoPtr, hDC);
916 LeaveCriticalSection(&infoPtr->cs);
918 EndPaint(hWnd, &ps);
921 break;
923 case WM_SIZE:
924 ANIMATE_Size(hWnd, wParam, lParam);
925 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
927 default:
928 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
929 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
931 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
933 return 0;
936 void ANIMATE_Register(void)
938 WNDCLASSA wndClass;
940 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
941 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
942 wndClass.lpfnWndProc = (WNDPROC)ANIMATE_WindowProc;
943 wndClass.cbClsExtra = 0;
944 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
945 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
946 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
947 wndClass.lpszClassName = ANIMATE_CLASSA;
949 RegisterClassA(&wndClass);
953 void ANIMATE_Unregister(void)
955 UnregisterClassA(ANIMATE_CLASSA, NULL);