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