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