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