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