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