- fix dirty flag on shelllink loading and saving
[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 *)GetWindowLongA(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         GlobalFree((HGLOBAL)lpAvi);
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         FIXME("Already playing ? what should I do ??\n");
418         ANIMATE_DoStop(infoPtr);
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             HDC hDC = GetDC(hWnd);
447             infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
448                                                      WM_CTLCOLORSTATIC, 0, (LPARAM)hWnd);
449             ReleaseDC(hWnd,hDC);
450         }
451
452         TRACE("Using an animation thread\n");
453         infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
454         infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr, 0, &threadID);
455         if(!infoPtr->hThread)
456         {
457            ERR("Could not create animation thread!\n");
458            return FALSE;
459         }
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 = HeapAlloc(GetProcessHeap(), 0, 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 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
608                                  infoPtr->mah.dwTotalFrames * sizeof(DWORD));
609     if (!infoPtr->lpIndex) {
610         WARN("Can't alloc index array\n");
611         return FALSE;
612     }
613
614     numFrame = insize = 0;
615     while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
616            numFrame < infoPtr->mah.dwTotalFrames) {
617         infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
618         if (insize < mmckInfo.cksize)
619             insize = mmckInfo.cksize;
620         numFrame++;
621         mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
622     }
623     if (numFrame != infoPtr->mah.dwTotalFrames) {
624         WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
625         return FALSE;
626     }
627     if (insize > infoPtr->ash.dwSuggestedBufferSize) {
628         WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
629         infoPtr->ash.dwSuggestedBufferSize = insize;
630     }
631
632     infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
633     if (!infoPtr->indata) {
634         WARN("Can't alloc input buffer\n");
635         return FALSE;
636     }
637
638     return TRUE;
639 }
640
641
642 static BOOL    ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
643 {
644     DWORD       outSize;
645
646     /* check uncompressed AVI */
647     if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
648        (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
649        (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
650     {
651         infoPtr->hic = 0;
652         return TRUE;
653     }
654
655     /* try to get a decompressor for that type */
656     infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
657     if (!infoPtr->hic) {
658         WARN("Can't load codec for the file\n");
659         return FALSE;
660     }
661
662     outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
663                             (DWORD)infoPtr->inbih, 0L);
664
665     infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
666     if (!infoPtr->outbih) {
667         WARN("Can't alloc output BIH\n");
668         return FALSE;
669     }
670
671     if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
672                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize) {
673         WARN("Can't get output BIH\n");
674         return FALSE;
675     }
676
677     infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
678     if (!infoPtr->outdata) {
679         WARN("Can't alloc output buffer\n");
680         return FALSE;
681     }
682
683     if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
684                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
685         WARN("Can't begin decompression\n");
686         return FALSE;
687     }
688
689     return TRUE;
690 }
691
692 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
693 {
694     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
695     HINSTANCE hInstance = (HINSTANCE)wParam;
696
697     ANIMATE_Free(infoPtr);
698     infoPtr->hwndSelf = hWnd;
699
700     if (!lParam) {
701         TRACE("Closing avi!\n");
702         /* installer of thebat! v1.62 requires FALSE here */
703         return (infoPtr->hMMio != 0);
704     }
705
706     if (!hInstance)
707        hInstance = (HINSTANCE)GetWindowLongA(hWnd, GWL_HINSTANCE);
708
709     if (HIWORD(lParam)) {
710         TRACE("(\"%s\");\n", (LPSTR)lParam);
711
712         if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
713             TRACE("No AVI resource found!\n");
714             if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
715                 WARN("No AVI file found!\n");
716                 return FALSE;
717             }
718         }
719     } else {
720         TRACE("(%u);\n", (WORD)LOWORD(lParam));
721
722         if (!ANIMATE_LoadResA(infoPtr, hInstance,
723                               MAKEINTRESOURCEA((INT)lParam))) {
724             WARN("No AVI resource found!\n");
725             return FALSE;
726         }
727     }
728
729     if (!ANIMATE_GetAviInfo(infoPtr)) {
730         WARN("Can't get AVI information\n");
731         ANIMATE_Free(infoPtr);
732         return FALSE;
733     }
734
735     if (!ANIMATE_GetAviCodec(infoPtr)) {
736         WARN("Can't get AVI Codec\n");
737         ANIMATE_Free(infoPtr);
738         return FALSE;
739     }
740
741     if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
742         SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
743                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
744     }
745
746     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
747         return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
748     }
749
750     return TRUE;
751 }
752
753
754 /* << ANIMATE_Open32W >> */
755
756 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
757 {
758     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
759
760     /* nothing opened */
761     if (!infoPtr->hMMio)
762         return FALSE;
763
764     ANIMATE_DoStop(infoPtr);
765     return TRUE;
766 }
767
768
769 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
770 {
771     ANIMATE_INFO*       infoPtr;
772
773     if (!fnIC.hModule) /* FIXME: not thread safe */
774     {
775         /* since there's a circular dep between msvfw32 and comctl32, we could either:
776          * - fix the build chain to allow this circular dep
777          * - handle it by hand
778          * AJ wants the latter :-(
779          */
780         fnIC.hModule = LoadLibraryA("msvfw32.dll");
781         if (!fnIC.hModule) return FALSE;
782
783         fnIC.fnICOpen        = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
784         fnIC.fnICClose       = (void*)GetProcAddress(fnIC.hModule, "ICClose");
785         fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
786         fnIC.fnICDecompress  = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
787     }
788
789     /* allocate memory for info structure */
790     infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
791     if (!infoPtr) {
792         ERR("could not allocate info memory!\n");
793         return 0;
794     }
795
796     /* store crossref hWnd <-> info structure */
797     SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
798     infoPtr->hwndSelf = hWnd;
799     infoPtr->hwndNotify = ((LPCREATESTRUCTA)lParam)->hwndParent;
800     infoPtr->transparentColor = ANIMATE_COLOR_NONE;
801     infoPtr->hbmPrevFrame = 0;
802
803     TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)infoPtr->hwndNotify);
804
805     InitializeCriticalSection(&infoPtr->cs);
806
807     return 0;
808 }
809
810
811 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
812 {
813     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
814
815
816     /* free avi data */
817     ANIMATE_Free(infoPtr);
818
819     /* free animate info data */
820     Free(infoPtr);
821     SetWindowLongA(hWnd, 0, 0);
822
823     return 0;
824 }
825
826
827 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
828 {
829     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
830     RECT rect;
831     HBRUSH hBrush = 0;
832
833     if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
834     {
835         hBrush = (HBRUSH)SendMessageA(infoPtr->hwndNotify,WM_CTLCOLORSTATIC,
836                                       wParam, (LPARAM)hWnd);
837     }
838
839     GetClientRect(hWnd, &rect);
840     FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
841
842     return TRUE;
843 }
844
845 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
846 {
847     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
848         InvalidateRect(hWnd, NULL, TRUE);
849     }
850     return TRUE;
851 }
852
853 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
854 {
855     TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
856     if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
857         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
858     switch (uMsg)
859     {
860     case ACM_OPENA:
861         return ANIMATE_OpenA(hWnd, wParam, lParam);
862
863         /*      case ACM_OPEN32W: FIXME!! */
864         /*          return ANIMATE_Open32W(hWnd, wParam, lParam); */
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         ANIMATE_Create(hWnd, wParam, lParam);
874         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
875
876     case WM_NCHITTEST:
877         return HTTRANSPARENT;
878
879     case WM_DESTROY:
880         ANIMATE_Destroy(hWnd, wParam, lParam);
881         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
882
883     case WM_ERASEBKGND:
884         ANIMATE_EraseBackground(hWnd, wParam, lParam);
885         break;
886
887     /*  case WM_STYLECHANGED: FIXME shall we do something ?? */
888
889     case WM_TIMER:
890         if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
891         {
892             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
893             infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
894                                                      WM_CTLCOLORSTATIC,
895                                                      wParam, (LPARAM)hWnd);
896         }
897         return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
898
899     case WM_CLOSE:
900         ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
901         return TRUE;
902
903     case WM_PAINT:
904         {
905             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
906
907             /* the animation isn't playing, or has not decompressed
908              * (and displayed) the first frame yet, don't paint
909              */
910             if ((!infoPtr->uTimer && !infoPtr->hThread) ||
911                 !infoPtr->hbmPrevFrame)
912             {
913                 /* default paint handling */
914                 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
915             }
916
917             if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
918                 infoPtr->hbrushBG = (HBRUSH)SendMessageA(infoPtr->hwndNotify,
919                                                          WM_CTLCOLORSTATIC,
920                                                          wParam, (LPARAM)hWnd);
921
922             if (wParam)
923             {
924                 EnterCriticalSection(&infoPtr->cs);
925                 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
926                 LeaveCriticalSection(&infoPtr->cs);
927             }
928             else
929             {
930                 PAINTSTRUCT ps;
931                 HDC hDC = BeginPaint(hWnd, &ps);
932
933                 EnterCriticalSection(&infoPtr->cs);
934                 ANIMATE_PaintFrame(infoPtr, hDC);
935                 LeaveCriticalSection(&infoPtr->cs);
936
937                 EndPaint(hWnd, &ps);
938             }
939         }
940         break;
941
942     case WM_SIZE:
943         ANIMATE_Size(hWnd, wParam, lParam);
944         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
945
946     default:
947         if ((uMsg >= WM_USER) && (uMsg < WM_APP))
948             ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
949
950         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
951     }
952     return 0;
953 }
954
955 void ANIMATE_Register(void)
956 {
957     WNDCLASSA wndClass;
958
959     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
960     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
961     wndClass.lpfnWndProc   = (WNDPROC)ANIMATE_WindowProc;
962     wndClass.cbClsExtra    = 0;
963     wndClass.cbWndExtra    = sizeof(ANIMATE_INFO *);
964     wndClass.hCursor       = LoadCursorA(0, (LPSTR)IDC_ARROW);
965     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
966     wndClass.lpszClassName = ANIMATE_CLASSA;
967
968     RegisterClassA(&wndClass);
969 }
970
971
972 void ANIMATE_Unregister(void)
973 {
974     UnregisterClassA(ANIMATE_CLASSA, NULL);
975 }