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