Updated.
[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             infoPtr->hbrushBG = SendMessageA(GetParent(infoPtr->hWnd),WM_CTLCOLORSTATIC,hDC, infoPtr->hWnd);
365             ReleaseDC(infoPtr->hWnd,hDC);
366         }
367         
368     EnterCriticalSection(&infoPtr->cs);
369     ANIMATE_DrawFrame(infoPtr);
370     LeaveCriticalSection(&infoPtr->cs);
371     
372         /* time is in microseconds, we should convert it to milliseconds */
373         Sleep((infoPtr->mah.dwMicroSecPerFrame+500)/1000);
374 }
375     return TRUE;
376 }
377
378 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
379 {
380     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
381
382     /* nothing opened */
383     if (!infoPtr->hMMio)
384         return FALSE;
385
386     if (infoPtr->hThread || infoPtr->uTimer) {
387         FIXME("Already playing ? what should I do ??\n");
388         ANIMATE_DoStop(infoPtr);
389     }
390
391     infoPtr->nFromFrame = (INT)LOWORD(lParam);
392     infoPtr->nToFrame   = (INT)HIWORD(lParam);
393     infoPtr->nLoop      = (INT)wParam;
394
395     if (infoPtr->nToFrame == 0xFFFF)
396         infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
397
398     TRACE("(repeat=%d from=%d to=%d);\n", 
399           infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
400
401     if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
402         infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
403         return FALSE;
404
405     infoPtr->currFrame = infoPtr->nFromFrame;
406
407     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
408         TRACE("Using a timer\n");
409         /* create a timer to display AVI */
410         infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
411     } else {
412         DWORD threadID;
413
414         TRACE("Using an animation thread\n");
415         infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr,0,0 &threadID);
416         if(!infoPtr->hThread)
417         {
418            ERR("Could not create animation thread!\n");
419            return FALSE;
420     }
421         
422     }
423         
424     ANIMATE_Notify(infoPtr, ACN_START);
425
426     return TRUE;
427 }
428
429
430 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
431 {
432     MMCKINFO            ckMainRIFF;
433     MMCKINFO            mmckHead;
434     MMCKINFO            mmckList;
435     MMCKINFO            mmckInfo;
436     DWORD               numFrame;
437     DWORD               insize;
438
439     if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
440         WARN("Can't find 'RIFF' chunk\n");
441         return FALSE;
442     }
443
444     if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
445         (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
446         WARN("Can't find 'AVI ' chunk\n");
447         return FALSE;
448     }
449
450     mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
451     if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
452         WARN("Can't find 'hdrl' list\n");
453         return FALSE;
454     }
455
456     mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
457     if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
458         WARN("Can't find 'avih' chunk\n");
459         return FALSE;
460     }
461
462     mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
463
464     TRACE("mah.dwMicroSecPerFrame=%ld\n",       infoPtr->mah.dwMicroSecPerFrame);
465     TRACE("mah.dwMaxBytesPerSec=%ld\n",         infoPtr->mah.dwMaxBytesPerSec);
466     TRACE("mah.dwPaddingGranularity=%ld\n",     infoPtr->mah.dwPaddingGranularity);
467     TRACE("mah.dwFlags=%ld\n",                  infoPtr->mah.dwFlags);
468     TRACE("mah.dwTotalFrames=%ld\n",            infoPtr->mah.dwTotalFrames);
469     TRACE("mah.dwInitialFrames=%ld\n",          infoPtr->mah.dwInitialFrames);
470     TRACE("mah.dwStreams=%ld\n",                infoPtr->mah.dwStreams);
471     TRACE("mah.dwSuggestedBufferSize=%ld\n",    infoPtr->mah.dwSuggestedBufferSize);
472     TRACE("mah.dwWidth=%ld\n",                  infoPtr->mah.dwWidth);
473     TRACE("mah.dwHeight=%ld\n",                 infoPtr->mah.dwHeight);
474
475     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
476
477     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
478     if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
479         WARN("Can't find 'strl' list\n");
480         return FALSE;
481     }
482
483     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
484     if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
485         WARN("Can't find 'strh' chunk\n");
486         return FALSE;
487     }
488
489     mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
490
491     TRACE("ash.fccType='%c%c%c%c'\n",           LOBYTE(LOWORD(infoPtr->ash.fccType)), 
492                                                 HIBYTE(LOWORD(infoPtr->ash.fccType)), 
493                                                 LOBYTE(HIWORD(infoPtr->ash.fccType)), 
494                                                 HIBYTE(HIWORD(infoPtr->ash.fccType)));
495     TRACE("ash.fccHandler='%c%c%c%c'\n",        LOBYTE(LOWORD(infoPtr->ash.fccHandler)), 
496                                                 HIBYTE(LOWORD(infoPtr->ash.fccHandler)), 
497                                                 LOBYTE(HIWORD(infoPtr->ash.fccHandler)), 
498                                                 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
499     TRACE("ash.dwFlags=%ld\n",                  infoPtr->ash.dwFlags);
500     TRACE("ash.wPriority=%d\n",                 infoPtr->ash.wPriority);
501     TRACE("ash.wLanguage=%d\n",                 infoPtr->ash.wLanguage);
502     TRACE("ash.dwInitialFrames=%ld\n",          infoPtr->ash.dwInitialFrames);
503     TRACE("ash.dwScale=%ld\n",                  infoPtr->ash.dwScale);
504     TRACE("ash.dwRate=%ld\n",                   infoPtr->ash.dwRate);
505     TRACE("ash.dwStart=%ld\n",                  infoPtr->ash.dwStart);
506     TRACE("ash.dwLength=%ld\n",                 infoPtr->ash.dwLength);
507     TRACE("ash.dwSuggestedBufferSize=%ld\n",    infoPtr->ash.dwSuggestedBufferSize);
508     TRACE("ash.dwQuality=%ld\n",                infoPtr->ash.dwQuality);
509     TRACE("ash.dwSampleSize=%ld\n",             infoPtr->ash.dwSampleSize);
510     TRACE("ash.rcFrame=(%d,%d,%d,%d)\n",        infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left, 
511           infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
512
513     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
514
515     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
516     if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
517         WARN("Can't find 'strh' chunk\n");
518         return FALSE;
519     }
520
521     infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
522     if (!infoPtr->inbih) {
523         WARN("Can't alloc input BIH\n");
524         return FALSE;
525     }
526
527     mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
528
529     TRACE("bih.biSize=%ld\n",           infoPtr->inbih->biSize);
530     TRACE("bih.biWidth=%ld\n",          infoPtr->inbih->biWidth);
531     TRACE("bih.biHeight=%ld\n",         infoPtr->inbih->biHeight);
532     TRACE("bih.biPlanes=%d\n",          infoPtr->inbih->biPlanes);
533     TRACE("bih.biBitCount=%d\n",        infoPtr->inbih->biBitCount);
534     TRACE("bih.biCompression=%ld\n",    infoPtr->inbih->biCompression);
535     TRACE("bih.biSizeImage=%ld\n",      infoPtr->inbih->biSizeImage);
536     TRACE("bih.biXPelsPerMeter=%ld\n",  infoPtr->inbih->biXPelsPerMeter);
537     TRACE("bih.biYPelsPerMeter=%ld\n",  infoPtr->inbih->biYPelsPerMeter);
538     TRACE("bih.biClrUsed=%ld\n",        infoPtr->inbih->biClrUsed);
539     TRACE("bih.biClrImportant=%ld\n",   infoPtr->inbih->biClrImportant);
540
541     mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
542
543     mmioAscend(infoPtr->hMMio, &mmckList, 0);
544     
545 #if 0
546     /* an AVI has 0 or 1 video stream, and to be animated should not contain
547      * an audio stream, so only one strl is allowed 
548      */
549     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
550     if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
551         WARN("There should be a single 'strl' list\n");
552         return FALSE;
553     }
554 #endif
555
556     mmioAscend(infoPtr->hMMio, &mmckHead, 0);
557
558     /* no need to read optional JUNK chunk */
559
560     mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
561     if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
562         WARN("Can't find 'movi' list\n");
563         return FALSE;
564     }
565
566     /* FIXME: should handle the 'rec ' LIST when present */
567
568     infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
569                                  infoPtr->mah.dwTotalFrames * sizeof(DWORD));
570     if (!infoPtr->lpIndex) {
571         WARN("Can't alloc index array\n");
572         return FALSE;
573     }
574
575     numFrame = insize = 0;
576     while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 && 
577            numFrame < infoPtr->mah.dwTotalFrames) {
578         infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
579         if (insize < mmckInfo.cksize)
580             insize = mmckInfo.cksize;
581         numFrame++;
582         mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
583     }
584     if (numFrame != infoPtr->mah.dwTotalFrames) {
585         WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
586         return FALSE;
587     }
588     if (insize > infoPtr->ash.dwSuggestedBufferSize) {
589         WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
590         infoPtr->ash.dwSuggestedBufferSize = insize;
591     }
592
593     infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
594     if (!infoPtr->indata) {
595         WARN("Can't alloc input buffer\n");
596         return FALSE;
597     }
598
599     return TRUE;
600 }
601
602
603 static BOOL    ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
604 {
605     DWORD       outSize;
606
607     /* check uncompressed AVI */
608     if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
609        (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')))
610     {
611         infoPtr->hic = 0;             
612         return TRUE;
613     }
614
615     /* try to get a decompressor for that type */
616     infoPtr->hic = ICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
617     if (!infoPtr->hic) {
618         WARN("Can't load codec for the file\n");
619         return FALSE;
620     }
621     
622     outSize = ICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT, 
623                             (DWORD)infoPtr->inbih, 0L);
624
625     infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
626     if (!infoPtr->outbih) {
627         WARN("Can't alloc output BIH\n");
628         return FALSE;
629     }
630
631     if (ICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT, 
632                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
633         WARN("Can't get output BIH\n");
634         return FALSE;
635     }
636
637     infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
638     if (!infoPtr->outdata) {
639         WARN("Can't alloc output buffer\n");
640         return FALSE;
641     }
642
643     if (ICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN, 
644                       (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
645         WARN("Can't begin decompression\n");
646         return FALSE;
647     }
648
649     return TRUE;
650 }
651
652 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
653 {
654     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
655     HINSTANCE hInstance = (HINSTANCE)wParam;
656
657     ANIMATE_Free(infoPtr);
658
659     if (!lParam) {
660         TRACE("Closing avi!\n");
661         return TRUE;
662     }
663     
664     if (!hInstance)
665        hInstance = GetWindowLongA(hWnd, GWL_HINSTANCE);
666
667     if (HIWORD(lParam)) {
668         TRACE("(\"%s\");\n", (LPSTR)lParam);
669
670         if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
671             TRACE("No AVI resource found!\n");
672             if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
673                 WARN("No AVI file found!\n");
674                 return FALSE;
675             }
676         }
677     } else {
678         TRACE("(%u);\n", (WORD)LOWORD(lParam));
679
680         if (!ANIMATE_LoadResA(infoPtr, hInstance,
681                               MAKEINTRESOURCEA((INT)lParam))) {
682             WARN("No AVI resource found!\n");
683             return FALSE;
684         }
685     }
686
687     if (!ANIMATE_GetAviInfo(infoPtr)) {
688         WARN("Can't get AVI information\n");
689         ANIMATE_Free(infoPtr);
690         return FALSE;
691     }
692
693     if (!ANIMATE_GetAviCodec(infoPtr)) {
694         WARN("Can't get AVI Codec\n");
695         ANIMATE_Free(infoPtr);
696         return FALSE;
697     }
698
699     if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
700         SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
701                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
702     }
703
704     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
705         return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
706     }
707
708     return TRUE;
709 }
710
711
712 /* << ANIMATE_Open32W >> */
713
714 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
715 {
716     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
717
718     /* nothing opened */
719     if (!infoPtr->hMMio)
720         return FALSE;
721
722     ANIMATE_DoStop(infoPtr);
723     return TRUE;
724 }
725
726
727 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
728 {
729     ANIMATE_INFO*       infoPtr;
730
731     /* allocate memory for info structure */
732     infoPtr = (ANIMATE_INFO *)COMCTL32_Alloc(sizeof(ANIMATE_INFO));
733     if (!infoPtr) {
734         ERR("could not allocate info memory!\n");
735         return 0;
736     }
737
738     TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)GetParent(hWnd));
739
740     /* store crossref hWnd <-> info structure */
741     SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
742     infoPtr->hWnd = hWnd;
743     infoPtr->transparentColor = ANIMATE_COLOR_NONE;
744     infoPtr->hbmPrevFrame = 0;
745
746     InitializeCriticalSection(&infoPtr->cs);
747     
748     return 0;
749 }
750
751
752 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
753 {
754     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
755
756
757     /* free avi data */
758     ANIMATE_Free(infoPtr);
759
760     /* free animate info data */
761     COMCTL32_Free(infoPtr);
762     SetWindowLongA(hWnd, 0, 0);
763
764     return 0;
765 }
766
767
768 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
769 {
770     RECT rect;
771     HBRUSH hBrush = 0;       
772
773     if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT) 
774     {
775         hBrush = SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,(HDC)wParam, hWnd);    
776     }
777
778     GetClientRect(hWnd, &rect);
779     FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
780
781     return TRUE;
782 }
783
784 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
785 {
786     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
787         InvalidateRect(hWnd, NULL, TRUE);
788     }
789     return TRUE;
790 }
791
792 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
793 {
794     TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
795     if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
796         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
797     switch (uMsg)
798     {
799     case ACM_OPENA:
800         return ANIMATE_OpenA(hWnd, wParam, lParam);
801         
802         /*      case ACM_OPEN32W: FIXME!! */
803         /*          return ANIMATE_Open32W(hWnd, wParam, lParam); */
804         
805     case ACM_PLAY:
806         return ANIMATE_Play(hWnd, wParam, lParam);
807         
808     case ACM_STOP:
809         return ANIMATE_Stop(hWnd, wParam, lParam);
810         
811     case WM_NCCREATE:
812         ANIMATE_Create(hWnd, wParam, lParam);
813         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
814         
815     case WM_NCHITTEST:
816         return HTTRANSPARENT;
817
818     case WM_DESTROY:
819         ANIMATE_Destroy(hWnd, wParam, lParam);
820         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
821         
822     case WM_ERASEBKGND:
823         ANIMATE_EraseBackground(hWnd, wParam, lParam);
824         break;
825
826     /*  case WM_STYLECHANGED: FIXME shall we do something ?? */
827
828     case WM_TIMER:
829         if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
830         {
831             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
832             infoPtr->hbrushBG = SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,(HDC)wParam, hWnd);
833         }
834         return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
835         
836     case WM_CLOSE:
837         ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
838         return TRUE;
839
840     case WM_PAINT:
841         {
842             ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
843                 
844             /* the animation isn't playing, don't paint */
845             if(!infoPtr->uTimer && !infoPtr->hThread)
846                 /* default paint handling */
847                 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
848             
849             if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
850                 infoPtr->hbrushBG = SendMessageA(GetParent(hWnd), WM_CTLCOLORSTATIC,
851                                                  (HDC)wParam, hWnd);
852     
853             if (wParam)
854             {
855                 EnterCriticalSection(&infoPtr->cs);
856                 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
857                 LeaveCriticalSection(&infoPtr->cs);
858             }
859             else
860             {
861                 PAINTSTRUCT ps;
862                 HDC hDC = BeginPaint(hWnd, &ps);
863
864                 EnterCriticalSection(&infoPtr->cs);
865                 ANIMATE_PaintFrame(infoPtr, hDC);
866                 LeaveCriticalSection(&infoPtr->cs);
867     
868                 EndPaint(hWnd, &ps);
869             }
870         }
871         break;
872
873     case WM_SIZE:
874         ANIMATE_Size(hWnd, wParam, lParam);
875         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
876
877     default:
878         if (uMsg >= WM_USER)
879             ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
880         
881         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
882     }
883     return 0;
884 }
885
886
887 void ANIMATE_Register(void)
888 {
889     WNDCLASSA wndClass;
890
891     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
892     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
893     wndClass.lpfnWndProc   = (WNDPROC)ANIMATE_WindowProc;
894     wndClass.cbClsExtra    = 0;
895     wndClass.cbWndExtra    = sizeof(ANIMATE_INFO *);
896     wndClass.hCursor       = LoadCursorA(0, IDC_ARROWA);
897     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
898     wndClass.lpszClassName = ANIMATE_CLASSA;
899  
900     RegisterClassA(&wndClass);
901 }
902
903
904 void ANIMATE_Unregister(void)
905 {
906     UnregisterClassA(ANIMATE_CLASSA, (HINSTANCE)NULL);
907 }
908