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