- Make the month calendar child window auto-size instead of using a
[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         if (infoPtr->lpIndex) {
191             HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
192             infoPtr->lpIndex = NULL;
193         }
194         if (infoPtr->hic) {
195             fnIC.fnICClose(infoPtr->hic);
196             infoPtr->hic = 0;
197         }
198         if (infoPtr->inbih) {
199             HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
200             infoPtr->inbih = NULL;
201         }
202         if (infoPtr->outbih) {
203             HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
204             infoPtr->outbih = NULL;
205         }
206         if( infoPtr->indata )
207         {
208         HeapFree(GetProcessHeap(), 0, infoPtr->indata);
209             infoPtr->indata = NULL;
210         }
211         if( infoPtr->outdata )
212         {
213         HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
214             infoPtr->outdata = NULL;
215         }
216         if( infoPtr->hbmPrevFrame )
217         {
218             DeleteObject(infoPtr->hbmPrevFrame);
219             infoPtr->hbmPrevFrame = 0;
220         }
221         infoPtr->indata = infoPtr->outdata = NULL;
222         infoPtr->hwndSelf = 0;
223         infoPtr->hMMio = 0;
224
225         memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
226         memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
227         infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
228     }
229     infoPtr->transparentColor = ANIMATE_COLOR_NONE;
230 }
231
232 static void ANIMATE_TransparentBlt(ANIMATE_INFO* infoPtr, HDC hdcDest, HDC hdcSource)
233 {
234     HDC hdcMask;
235     HBITMAP hbmMask;
236     HBITMAP hbmOld;
237
238     /* create a transparency mask */
239     hdcMask = CreateCompatibleDC(hdcDest);
240     hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
241     hbmOld = SelectObject(hdcMask, hbmMask);
242
243     SetBkColor(hdcSource,infoPtr->transparentColor);
244     BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
245
246     /* mask the source bitmap */
247     SetBkColor(hdcSource, RGB(0,0,0));
248     SetTextColor(hdcSource, RGB(255,255,255));
249     BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
250
251     /* mask the destination bitmap */
252     SetBkColor(hdcDest, RGB(255,255,255));
253     SetTextColor(hdcDest, RGB(0,0,0));
254     BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
255
256     /* combine source and destination */
257     BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
258
259     SelectObject(hdcMask, hbmOld);
260     DeleteObject(hbmMask);
261     DeleteDC(hdcMask);
262 }
263
264 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
265 {
266     void* pBitmapData = NULL;
267     LPBITMAPINFO pBitmapInfo = NULL;
268
269     HDC hdcMem;
270     HBITMAP hbmOld;
271
272     int nOffsetX = 0;
273     int nOffsetY = 0;
274
275     int nWidth;
276     int nHeight;
277
278     if (!hDC || !infoPtr->inbih)
279         return TRUE;
280
281     if (infoPtr->hic )
282     {
283         pBitmapData = infoPtr->outdata;
284         pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
285
286         nWidth = infoPtr->outbih->biWidth;
287         nHeight = infoPtr->outbih->biHeight;
288     } else
289     {
290         pBitmapData = infoPtr->indata;
291         pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
292
293         nWidth = infoPtr->inbih->biWidth;
294         nHeight = infoPtr->inbih->biHeight;
295     }
296
297     if(!infoPtr->hbmPrevFrame)
298     {
299         infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
300     }
301
302     SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
303
304     hdcMem = CreateCompatibleDC(hDC);
305     hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
306
307     /*
308      * we need to get the transparent color even without ACS_TRANSPARENT,
309      * because the style can be changed later on and the color should always
310      * be obtained in the first frame
311      */
312     if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
313     {
314         infoPtr->transparentColor = GetPixel(hdcMem,0,0);
315     }
316
317     if(GetWindowLongA(infoPtr->hwndSelf, GWL_STYLE) & ACS_TRANSPARENT)
318     {
319         HDC hdcFinal = CreateCompatibleDC(hDC);
320         HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
321         HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
322         RECT rect;
323
324         rect.left = 0;
325         rect.top = 0;
326         rect.right = nWidth;
327         rect.bottom = nHeight;
328
329         if(!infoPtr->hbrushBG)
330             infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
331
332         FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
333         ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
334
335         SelectObject(hdcFinal, hbmOld2);
336         SelectObject(hdcMem, hbmFinal);
337         DeleteDC(hdcFinal);
338         DeleteObject(infoPtr->hbmPrevFrame);
339         infoPtr->hbmPrevFrame = hbmFinal;
340          }
341
342     if (GetWindowLongA(infoPtr->hwndSelf, GWL_STYLE) & ACS_CENTER)
343     {
344        RECT rect;
345
346        GetWindowRect(infoPtr->hwndSelf, &rect);
347        nOffsetX = ((rect.right - rect.left) - nWidth)/2;
348        nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
349     }
350     BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
351
352     SelectObject(hdcMem, hbmOld);
353     DeleteDC(hdcMem);
354     return TRUE;
355 }
356
357 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
358 {
359     HDC         hDC;
360
361     TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
362
363     EnterCriticalSection(&infoPtr->cs);
364
365     mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
366     mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
367
368     if (infoPtr->hic &&
369         fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
370                      infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
371         LeaveCriticalSection(&infoPtr->cs);
372         WARN("Decompression error\n");
373         return FALSE;
374     }
375
376     if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
377         ANIMATE_PaintFrame(infoPtr, hDC);
378         ReleaseDC(infoPtr->hwndSelf, hDC);
379     }
380
381     if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
382         infoPtr->currFrame = infoPtr->nFromFrame;
383         if (infoPtr->nLoop != -1) {
384             if (--infoPtr->nLoop == 0) {
385                 ANIMATE_DoStop(infoPtr);
386             }
387         }
388     }
389     LeaveCriticalSection(&infoPtr->cs);
390
391     return TRUE;
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(HWND hWnd, WPARAM wParam, LPARAM lParam)
416 {
417     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
418
419     /* nothing opened */
420     if (!infoPtr->hMMio)
421         return FALSE;
422
423     if (infoPtr->hThread || infoPtr->uTimer) {
424         TRACE("Already playing\n");
425         return TRUE;
426     }
427
428     infoPtr->nFromFrame = (INT)LOWORD(lParam);
429     infoPtr->nToFrame   = (INT)HIWORD(lParam);
430     infoPtr->nLoop      = (INT)wParam;
431
432     if (infoPtr->nToFrame == 0xFFFF)
433         infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
434
435     TRACE("(repeat=%d from=%d to=%d);\n",
436           infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
437
438     if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
439         infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
440         return FALSE;
441
442     infoPtr->currFrame = infoPtr->nFromFrame;
443
444     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
445         TRACE("Using a timer\n");
446         /* create a timer to display AVI */
447         infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
448     } else {
449         if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
450         {
451             infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
452                                                      WM_CTLCOLORSTATIC, 0, (LPARAM)hWnd);
453         }
454
455         TRACE("Using an animation thread\n");
456         infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
457         infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr, 0, &infoPtr->threadId);
458         if(!infoPtr->hThread)
459         {
460            ERR("Could not create animation thread!\n");
461            return FALSE;
462         }
463
464     }
465
466     ANIMATE_Notify(infoPtr, ACN_START);
467
468     return TRUE;
469 }
470
471
472 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
473 {
474     MMCKINFO            ckMainRIFF;
475     MMCKINFO            mmckHead;
476     MMCKINFO            mmckList;
477     MMCKINFO            mmckInfo;
478     DWORD               numFrame;
479     DWORD               insize;
480
481     if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
482         WARN("Can't find 'RIFF' chunk\n");
483         return FALSE;
484     }
485
486     if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
487         (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
488         WARN("Can't find 'AVI ' chunk\n");
489         return FALSE;
490     }
491
492     mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
493     if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
494         WARN("Can't find 'hdrl' list\n");
495         return FALSE;
496     }
497
498     mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
499     if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
500         WARN("Can't find 'avih' chunk\n");
501         return FALSE;
502     }
503
504     mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
505
506     TRACE("mah.dwMicroSecPerFrame=%ld\n",       infoPtr->mah.dwMicroSecPerFrame);
507     TRACE("mah.dwMaxBytesPerSec=%ld\n",         infoPtr->mah.dwMaxBytesPerSec);
508     TRACE("mah.dwPaddingGranularity=%ld\n",     infoPtr->mah.dwPaddingGranularity);
509     TRACE("mah.dwFlags=%ld\n",                  infoPtr->mah.dwFlags);
510     TRACE("mah.dwTotalFrames=%ld\n",            infoPtr->mah.dwTotalFrames);
511     TRACE("mah.dwInitialFrames=%ld\n",          infoPtr->mah.dwInitialFrames);
512     TRACE("mah.dwStreams=%ld\n",                infoPtr->mah.dwStreams);
513     TRACE("mah.dwSuggestedBufferSize=%ld\n",    infoPtr->mah.dwSuggestedBufferSize);
514     TRACE("mah.dwWidth=%ld\n",                  infoPtr->mah.dwWidth);
515     TRACE("mah.dwHeight=%ld\n",                 infoPtr->mah.dwHeight);
516
517     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
518
519     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
520     if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
521         WARN("Can't find 'strl' list\n");
522         return FALSE;
523     }
524
525     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
526     if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
527         WARN("Can't find 'strh' chunk\n");
528         return FALSE;
529     }
530
531     mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
532
533     TRACE("ash.fccType='%c%c%c%c'\n",           LOBYTE(LOWORD(infoPtr->ash.fccType)),
534                                                 HIBYTE(LOWORD(infoPtr->ash.fccType)),
535                                                 LOBYTE(HIWORD(infoPtr->ash.fccType)),
536                                                 HIBYTE(HIWORD(infoPtr->ash.fccType)));
537     TRACE("ash.fccHandler='%c%c%c%c'\n",        LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
538                                                 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
539                                                 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
540                                                 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
541     TRACE("ash.dwFlags=%ld\n",                  infoPtr->ash.dwFlags);
542     TRACE("ash.wPriority=%d\n",                 infoPtr->ash.wPriority);
543     TRACE("ash.wLanguage=%d\n",                 infoPtr->ash.wLanguage);
544     TRACE("ash.dwInitialFrames=%ld\n",          infoPtr->ash.dwInitialFrames);
545     TRACE("ash.dwScale=%ld\n",                  infoPtr->ash.dwScale);
546     TRACE("ash.dwRate=%ld\n",                   infoPtr->ash.dwRate);
547     TRACE("ash.dwStart=%ld\n",                  infoPtr->ash.dwStart);
548     TRACE("ash.dwLength=%ld\n",                 infoPtr->ash.dwLength);
549     TRACE("ash.dwSuggestedBufferSize=%ld\n",    infoPtr->ash.dwSuggestedBufferSize);
550     TRACE("ash.dwQuality=%ld\n",                infoPtr->ash.dwQuality);
551     TRACE("ash.dwSampleSize=%ld\n",             infoPtr->ash.dwSampleSize);
552     TRACE("ash.rcFrame=(%d,%d,%d,%d)\n",        infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
553           infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
554
555     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
556
557     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
558     if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
559         WARN("Can't find 'strh' chunk\n");
560         return FALSE;
561     }
562
563     infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
564     if (!infoPtr->inbih) {
565         WARN("Can't alloc input BIH\n");
566         return FALSE;
567     }
568
569     mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
570
571     TRACE("bih.biSize=%ld\n",           infoPtr->inbih->biSize);
572     TRACE("bih.biWidth=%ld\n",          infoPtr->inbih->biWidth);
573     TRACE("bih.biHeight=%ld\n",         infoPtr->inbih->biHeight);
574     TRACE("bih.biPlanes=%d\n",          infoPtr->inbih->biPlanes);
575     TRACE("bih.biBitCount=%d\n",        infoPtr->inbih->biBitCount);
576     TRACE("bih.biCompression=%ld\n",    infoPtr->inbih->biCompression);
577     TRACE("bih.biSizeImage=%ld\n",      infoPtr->inbih->biSizeImage);
578     TRACE("bih.biXPelsPerMeter=%ld\n",  infoPtr->inbih->biXPelsPerMeter);
579     TRACE("bih.biYPelsPerMeter=%ld\n",  infoPtr->inbih->biYPelsPerMeter);
580     TRACE("bih.biClrUsed=%ld\n",        infoPtr->inbih->biClrUsed);
581     TRACE("bih.biClrImportant=%ld\n",   infoPtr->inbih->biClrImportant);
582
583     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
584
585     mmioAscend(infoPtr->hMMio, &mmckList, 0);
586
587 #if 0
588     /* an AVI has 0 or 1 video stream, and to be animated should not contain
589      * an audio stream, so only one strl is allowed
590      */
591     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
592     if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
593         WARN("There should be a single 'strl' list\n");
594         return FALSE;
595     }
596 #endif
597
598     mmioAscend(infoPtr->hMMio, &mmckHead, 0);
599
600     /* no need to read optional JUNK chunk */
601
602     mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
603     if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
604         WARN("Can't find 'movi' list\n");
605         return FALSE;
606     }
607
608     /* FIXME: should handle the 'rec ' LIST when present */
609
610     infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
611                                  infoPtr->mah.dwTotalFrames * sizeof(DWORD));
612     if (!infoPtr->lpIndex) {
613         WARN("Can't alloc index array\n");
614         return FALSE;
615     }
616
617     numFrame = insize = 0;
618     while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
619            numFrame < infoPtr->mah.dwTotalFrames) {
620         infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
621         if (insize < mmckInfo.cksize)
622             insize = mmckInfo.cksize;
623         numFrame++;
624         mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
625     }
626     if (numFrame != infoPtr->mah.dwTotalFrames) {
627         WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
628         return FALSE;
629     }
630     if (insize > infoPtr->ash.dwSuggestedBufferSize) {
631         WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
632         infoPtr->ash.dwSuggestedBufferSize = insize;
633     }
634
635     infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
636     if (!infoPtr->indata) {
637         WARN("Can't alloc input buffer\n");
638         return FALSE;
639     }
640
641     return TRUE;
642 }
643
644
645 static BOOL    ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
646 {
647     DWORD       outSize;
648
649     /* check uncompressed AVI */
650     if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
651        (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
652        (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
653     {
654         infoPtr->hic = 0;
655         return TRUE;
656     }
657
658     /* try to get a decompressor for that type */
659     infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
660     if (!infoPtr->hic) {
661         WARN("Can't load codec for the file\n");
662         return FALSE;
663     }
664
665     outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
666                             (DWORD)infoPtr->inbih, 0L);
667
668     infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
669     if (!infoPtr->outbih) {
670         WARN("Can't alloc output BIH\n");
671         return FALSE;
672     }
673
674     if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
675                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize) {
676         WARN("Can't get output BIH\n");
677         return FALSE;
678     }
679
680     infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
681     if (!infoPtr->outdata) {
682         WARN("Can't alloc output buffer\n");
683         return FALSE;
684     }
685
686     if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
687                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
688         WARN("Can't begin decompression\n");
689         return FALSE;
690     }
691
692     return TRUE;
693 }
694
695 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
696 {
697     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
698     HINSTANCE hInstance = (HINSTANCE)wParam;
699
700     ANIMATE_Free(infoPtr);
701     infoPtr->hwndSelf = hWnd;
702
703     if (!lParam) {
704         TRACE("Closing avi!\n");
705         /* installer of thebat! v1.62 requires FALSE here */
706         return (infoPtr->hMMio != 0);
707     }
708
709     if (!hInstance)
710        hInstance = (HINSTANCE)GetWindowLongPtrW(hWnd, GWLP_HINSTANCE);
711
712     if (HIWORD(lParam)) {
713         TRACE("(\"%s\");\n", (LPSTR)lParam);
714
715         if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
716             TRACE("No AVI resource found!\n");
717             if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
718                 WARN("No AVI file found!\n");
719                 return FALSE;
720             }
721         }
722     } else {
723         TRACE("(%u);\n", (WORD)LOWORD(lParam));
724
725         if (!ANIMATE_LoadResA(infoPtr, hInstance,
726                               MAKEINTRESOURCEA((INT)lParam))) {
727             WARN("No AVI resource found!\n");
728             return FALSE;
729         }
730     }
731
732     if (!ANIMATE_GetAviInfo(infoPtr)) {
733         WARN("Can't get AVI information\n");
734         ANIMATE_Free(infoPtr);
735         return FALSE;
736     }
737
738     if (!ANIMATE_GetAviCodec(infoPtr)) {
739         WARN("Can't get AVI Codec\n");
740         ANIMATE_Free(infoPtr);
741         return FALSE;
742     }
743
744     if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
745         SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
746                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
747     }
748
749     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
750         return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
751     }
752
753     return TRUE;
754 }
755
756
757 /* << ANIMATE_Open32W >> */
758
759 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
760 {
761     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
762
763     /* nothing opened */
764     if (!infoPtr->hMMio)
765         return FALSE;
766
767     ANIMATE_DoStop(infoPtr);
768     return TRUE;
769 }
770
771
772 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
773 {
774     ANIMATE_INFO*       infoPtr;
775
776     if (!fnIC.hModule) /* FIXME: not thread safe */
777     {
778         /* since there's a circular dep between msvfw32 and comctl32, we could either:
779          * - fix the build chain to allow this circular dep
780          * - handle it by hand
781          * AJ wants the latter :-(
782          */
783         fnIC.hModule = LoadLibraryA("msvfw32.dll");
784         if (!fnIC.hModule) return FALSE;
785
786         fnIC.fnICOpen        = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
787         fnIC.fnICClose       = (void*)GetProcAddress(fnIC.hModule, "ICClose");
788         fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
789         fnIC.fnICDecompress  = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
790     }
791
792     /* allocate memory for info structure */
793     infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
794     if (!infoPtr) {
795         ERR("could not allocate info memory!\n");
796         return 0;
797     }
798
799     /* store crossref hWnd <-> info structure */
800     SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
801     infoPtr->hwndSelf = hWnd;
802     infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
803     infoPtr->transparentColor = ANIMATE_COLOR_NONE;
804     infoPtr->hbmPrevFrame = 0;
805
806     TRACE("Animate style=0x%08lx, parent=%p\n", GetWindowLongA(hWnd, GWL_STYLE), infoPtr->hwndNotify);
807
808     InitializeCriticalSection(&infoPtr->cs);
809
810     return TRUE;
811 }
812
813
814 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
815 {
816     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
817
818
819     /* free avi data */
820     ANIMATE_Free(infoPtr);
821
822     /* free animate info data */
823     Free(infoPtr);
824     SetWindowLongPtrW(hWnd, 0, 0);
825
826     return 0;
827 }
828
829
830 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
831 {
832     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
833     RECT rect;
834     HBRUSH hBrush = 0;
835
836     if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
837     {
838         hBrush = (HBRUSH)SendMessageA(infoPtr->hwndNotify,WM_CTLCOLORSTATIC,
839                                       wParam, (LPARAM)hWnd);
840     }
841
842     GetClientRect(hWnd, &rect);
843     FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
844
845     return TRUE;
846 }
847
848 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
849 {
850     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
851         InvalidateRect(hWnd, NULL, TRUE);
852     }
853     return TRUE;
854 }
855
856 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
857 {
858     TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
859     if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
860         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
861     switch (uMsg)
862     {
863     case ACM_OPENA:
864         return ANIMATE_OpenA(hWnd, wParam, lParam);
865
866     case ACM_OPENW:
867         FIXME("ACM_OPENW: stub!\n");
868         /* return ANIMATE_Open32W(hWnd, wParam, lParam); */
869         return 0;
870
871     case ACM_PLAY:
872         return ANIMATE_Play(hWnd, wParam, lParam);
873
874     case ACM_STOP:
875         return ANIMATE_Stop(hWnd, wParam, lParam);
876
877     case WM_NCCREATE:
878         return ANIMATE_Create(hWnd, wParam, lParam);
879
880     case WM_NCHITTEST:
881         return HTTRANSPARENT;
882
883     case WM_DESTROY:
884         return ANIMATE_Destroy(hWnd, wParam, lParam);
885
886     case WM_ERASEBKGND:
887         return ANIMATE_EraseBackground(hWnd, wParam, lParam);
888
889     /*  case WM_STYLECHANGED: FIXME shall we do something ?? */
890
891     case WM_TIMER:
892         if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
893         {
894             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
895             infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
896                                                      WM_CTLCOLORSTATIC,
897                                                      wParam, (LPARAM)hWnd);
898         }
899         return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
900
901     case WM_PAINT:
902         {
903             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
904
905             /* the animation isn't playing, or has not decompressed
906              * (and displayed) the first frame yet, don't paint
907              */
908             if ((!infoPtr->uTimer && !infoPtr->hThread) ||
909                 !infoPtr->hbmPrevFrame)
910             {
911                 /* default paint handling */
912                 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
913             }
914
915             if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
916                 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
917                                                          WM_CTLCOLORSTATIC,
918                                                          wParam, (LPARAM)hWnd);
919
920             if (wParam)
921             {
922                 EnterCriticalSection(&infoPtr->cs);
923                 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
924                 LeaveCriticalSection(&infoPtr->cs);
925             }
926             else
927             {
928                 PAINTSTRUCT ps;
929                 HDC hDC = BeginPaint(hWnd, &ps);
930
931                 EnterCriticalSection(&infoPtr->cs);
932                 ANIMATE_PaintFrame(infoPtr, hDC);
933                 LeaveCriticalSection(&infoPtr->cs);
934
935                 EndPaint(hWnd, &ps);
936             }
937         }
938         break;
939
940     case WM_SIZE:
941         return ANIMATE_Size(hWnd, wParam, lParam);
942
943     default:
944         if ((uMsg >= WM_USER) && (uMsg < WM_APP))
945             ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
946
947         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
948     }
949     return 0;
950 }
951
952 void ANIMATE_Register(void)
953 {
954     WNDCLASSA wndClass;
955
956     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
957     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
958     wndClass.lpfnWndProc   = ANIMATE_WindowProc;
959     wndClass.cbClsExtra    = 0;
960     wndClass.cbWndExtra    = sizeof(ANIMATE_INFO *);
961     wndClass.hCursor       = LoadCursorA(0, (LPSTR)IDC_ARROW);
962     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
963     wndClass.lpszClassName = ANIMATE_CLASSA;
964
965     RegisterClassA(&wndClass);
966 }
967
968
969 void ANIMATE_Unregister(void)
970 {
971     UnregisterClassA(ANIMATE_CLASSA, NULL);
972 }