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