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