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