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