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