Removed all non-standard common control headers from the include
[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  *   - implement some missing flags (ACS_TRANSPARENT and ACS_CENTER)
15  *   - protection between service thread and wndproc messages handling 
16  *     concurrent access to infoPtr
17  */
18
19
20 #include "winbase.h"
21 #include "commctrl.h"
22 #include "vfw.h"
23 #include "mmsystem.h"
24 #include "services.h"
25 #include "debugtools.h"
26
27 DEFAULT_DEBUG_CHANNEL(animate);
28
29 typedef struct
30 {
31    /* pointer to msvideo functions. it's easier to put them here.
32     * to be correct, they should be defined on a per process basis, but
33     * this would required a per process storage. We're using a per object
34     * storage instead, which is not efficient on memory usage, but
35     * will lead to less bugs in the future
36     */
37    HIC          WINAPI  (*fnICOpen)(DWORD, DWORD, UINT);
38    LRESULT      WINAPI  (*fnICClose)(HIC);
39    LRESULT      WINAPI  (*fnICSendMessage)(HIC, UINT, DWORD, DWORD);
40    DWORD        WINAPIV (*fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
41
42     HMMIO WINAPI (*fnmmioOpenA)(LPSTR,MMIOINFO*,DWORD);
43     MMRESULT WINAPI (*fnmmioClose)(HMMIO,UINT);
44     UINT  WINAPI (*fnmmioAscend)(HMMIO,MMCKINFO*,UINT);
45     UINT  WINAPI (*fnmmioDescend)(HMMIO,MMCKINFO*,const MMCKINFO*,UINT);
46     LONG  WINAPI (*fnmmioSeek)(HMMIO,LONG,INT);
47     LONG  WINAPI (*fnmmioRead)(HMMIO,HPSTR,LONG);
48
49    /* reference to input stream (file or resource) */
50    HGLOBAL              hRes;
51    HMMIO                        hMMio;  /* handle to mmio stream */
52    HWND                 hWnd;
53    /* information on the loaded AVI file */
54    MainAVIHeader        mah;
55    AVIStreamHeader      ash;
56    LPBITMAPINFOHEADER   inbih;
57    LPDWORD              lpIndex;
58    /* data for the decompressor */
59    HIC                  hic;
60    LPBITMAPINFOHEADER   outbih;
61    LPVOID               indata;
62    LPVOID               outdata;
63    /* data for the background mechanism */
64    CRITICAL_SECTION     cs;
65    HANDLE               hService;
66    UINT                 uTimer;
67    /* data for playing the file */
68    int                  nFromFrame;
69    int                  nToFrame;
70    int                  nLoop;
71    int                  currFrame;
72 } ANIMATE_INFO;
73
74 #define ANIMATE_GetInfoPtr(hWnd) ((ANIMATE_INFO *)GetWindowLongA(hWnd, 0))
75
76 HMODULE hModWinmm;
77
78 static void ANIMATE_Notify(ANIMATE_INFO* infoPtr, UINT notif)
79 {
80     SendMessageA(GetParent(infoPtr->hWnd), WM_COMMAND, 
81                  MAKEWPARAM(GetDlgCtrlID(infoPtr->hWnd), notif), 
82                  (LPARAM)infoPtr->hWnd);
83 }
84
85 static BOOL ANIMATE_LoadResA(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPSTR lpName)
86 {
87     HRSRC       hrsrc;
88     MMIOINFO    mminfo;
89     LPVOID      lpAvi;
90     
91     hrsrc = FindResourceA(hInst, lpName, "AVI");
92     if (!hrsrc)
93         return FALSE;
94     
95     infoPtr->hRes = LoadResource(hInst, hrsrc);
96     if (!infoPtr->hRes)
97         return FALSE;
98     
99     lpAvi = LockResource(infoPtr->hRes);
100     if (!lpAvi)
101         return FALSE;
102     
103     memset(&mminfo, 0, sizeof(mminfo));
104     mminfo.fccIOProc = FOURCC_MEM;
105     mminfo.pchBuffer = (LPSTR)lpAvi;
106     mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
107     infoPtr->hMMio = infoPtr->fnmmioOpenA(NULL, &mminfo, MMIO_READ);
108     
109     if (!infoPtr->hMMio) {
110         GlobalFree((HGLOBAL)lpAvi);
111         return FALSE;
112     }
113     
114     return TRUE;
115 }
116
117
118 static BOOL ANIMATE_LoadFileA(ANIMATE_INFO *infoPtr, LPSTR lpName)
119 {
120     infoPtr->hMMio = infoPtr->fnmmioOpenA((LPSTR)lpName, NULL,
121                                MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
122     
123     if (!infoPtr->hMMio)
124         return FALSE;
125     
126     return TRUE;
127 }
128
129
130 static LRESULT ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
131 {
132     EnterCriticalSection(&infoPtr->cs);
133
134     /* should stop playing */
135     if (infoPtr->hService) {
136         SERVICE_Delete(infoPtr->hService);
137         infoPtr->hService = 0;
138     }
139     if (infoPtr->uTimer) {
140         KillTimer(infoPtr->hWnd, infoPtr->uTimer);
141         infoPtr->uTimer = 0;
142     }
143
144     LeaveCriticalSection(&infoPtr->cs);
145
146     ANIMATE_Notify(infoPtr, ACN_STOP);
147
148     return TRUE;
149 }
150
151
152 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
153 {
154     if (infoPtr->hMMio) {
155         ANIMATE_DoStop(infoPtr);
156         infoPtr->fnmmioClose(infoPtr->hMMio, 0);
157         if (infoPtr->hRes) {
158             FreeResource(infoPtr->hRes);
159             infoPtr->hRes = 0;
160         }
161         if (infoPtr->lpIndex) {
162             HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
163             infoPtr->lpIndex = NULL;
164         }
165         if (infoPtr->hic) {
166             (infoPtr->fnICClose)(infoPtr->hic);
167             infoPtr->hic = 0;
168         }
169         if (infoPtr->inbih) {
170             HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
171             infoPtr->inbih = NULL;
172         }
173         if (infoPtr->outbih) {
174             HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
175             infoPtr->outbih = NULL;
176         }
177         HeapFree(GetProcessHeap(), 0, infoPtr->indata);
178         HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
179         infoPtr->indata = infoPtr->outdata = NULL;
180         infoPtr->hWnd = 0;
181         infoPtr->hMMio = 0;
182         memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
183         memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
184         infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
185     }
186 }
187
188
189 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
190 {
191     if (!hDC || !infoPtr->inbih)
192         return TRUE;
193     if (infoPtr->hic)
194         StretchDIBits(hDC, 0, 0, infoPtr->outbih->biWidth, infoPtr->outbih->biHeight, 
195                       0, 0, infoPtr->outbih->biWidth, infoPtr->outbih->biHeight, 
196                       infoPtr->outdata, (LPBITMAPINFO)infoPtr->outbih, DIB_RGB_COLORS, 
197                       SRCCOPY);
198     else
199         StretchDIBits(hDC, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 
200                       0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 
201                       infoPtr->indata, (LPBITMAPINFO)infoPtr->inbih, DIB_RGB_COLORS, 
202                       SRCCOPY);
203
204     return TRUE;
205 }
206
207 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
208 {
209     HDC         hDC;
210
211     TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
212
213     EnterCriticalSection(&infoPtr->cs);
214
215     infoPtr->fnmmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
216     infoPtr->fnmmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
217     
218     if (infoPtr->hic &&
219         (infoPtr->fnICDecompress)(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata, 
220                                   infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
221         LeaveCriticalSection(&infoPtr->cs);
222         WARN("Decompression error\n");
223         return FALSE;
224     }
225
226     if ((hDC = GetDC(infoPtr->hWnd)) != 0) {
227         ANIMATE_PaintFrame(infoPtr, hDC);
228         ReleaseDC(infoPtr->hWnd, hDC);
229     }
230
231     if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
232         infoPtr->currFrame = infoPtr->nFromFrame;
233         if (infoPtr->nLoop != -1) {
234             if (--infoPtr->nLoop == 0) {
235                 ANIMATE_DoStop(infoPtr);
236             }
237         }
238     }
239     LeaveCriticalSection(&infoPtr->cs);
240
241     return TRUE;
242 }
243
244 static void CALLBACK ANIMATE_ServiceCallback(ULONG_PTR ptr_)
245 {
246     ANIMATE_INFO*       infoPtr = (ANIMATE_INFO*)ptr_;
247
248     EnterCriticalSection(&infoPtr->cs);
249     ANIMATE_DrawFrame(infoPtr);
250     LeaveCriticalSection(&infoPtr->cs);
251 }
252
253 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
254 {
255     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
256
257     /* nothing opened */
258     if (!infoPtr->hMMio)
259         return FALSE;
260
261     if (infoPtr->hService || infoPtr->uTimer) {
262         FIXME("Already playing ? what should I do ??\n");
263         ANIMATE_DoStop(infoPtr);
264     }
265
266     infoPtr->nFromFrame = (INT)LOWORD(lParam);
267     infoPtr->nToFrame   = (INT)HIWORD(lParam);
268     infoPtr->nLoop      = (INT)wParam;
269
270     if (infoPtr->nToFrame == 0xFFFF)
271         infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
272
273     TRACE("(repeat=%d from=%d to=%d);\n", 
274           infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
275
276     if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
277         infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
278         return FALSE;
279
280     infoPtr->currFrame = infoPtr->nFromFrame;
281
282     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
283         TRACE("Using a timer\n");
284         /* create a timer to display AVI */
285         infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
286     } else {
287         TRACE("Using the service thread\n");
288         /* time is in Âµs */
289         infoPtr->hService = SERVICE_AddTimer(infoPtr->mah.dwMicroSecPerFrame / 1000, 
290                                              ANIMATE_ServiceCallback, (DWORD)infoPtr);
291     }
292         
293     ANIMATE_Notify(infoPtr, ACN_START);
294
295     return TRUE;
296 }
297
298
299 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
300 {
301     MMCKINFO            ckMainRIFF;
302     MMCKINFO            mmckHead;
303     MMCKINFO            mmckList;
304     MMCKINFO            mmckInfo;
305     DWORD               numFrame;
306     DWORD               insize;
307
308     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
309         WARN("Can't find 'RIFF' chunk\n");
310         return FALSE;
311     }
312
313     if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
314         (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
315         WARN("Can't find 'AVI ' chunk\n");
316         return FALSE;
317     }
318
319     mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
320     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
321         WARN("Can't find 'hdrl' list\n");
322         return FALSE;
323     }
324
325     mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
326     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
327         WARN("Can't find 'avih' chunk\n");
328         return FALSE;
329     }
330
331     infoPtr->fnmmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
332     TRACE("mah.dwMicroSecPerFrame=%ld\n",       infoPtr->mah.dwMicroSecPerFrame);
333     TRACE("mah.dwMaxBytesPerSec=%ld\n",         infoPtr->mah.dwMaxBytesPerSec);
334     TRACE("mah.dwPaddingGranularity=%ld\n",     infoPtr->mah.dwPaddingGranularity);
335     TRACE("mah.dwFlags=%ld\n",                  infoPtr->mah.dwFlags);
336     TRACE("mah.dwTotalFrames=%ld\n",            infoPtr->mah.dwTotalFrames);
337     TRACE("mah.dwInitialFrames=%ld\n",          infoPtr->mah.dwInitialFrames);
338     TRACE("mah.dwStreams=%ld\n",                infoPtr->mah.dwStreams);
339     TRACE("mah.dwSuggestedBufferSize=%ld\n",    infoPtr->mah.dwSuggestedBufferSize);
340     TRACE("mah.dwWidth=%ld\n",                  infoPtr->mah.dwWidth);
341     TRACE("mah.dwHeight=%ld\n",                 infoPtr->mah.dwHeight);
342     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
343
344     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
345     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
346         WARN("Can't find 'strl' list\n");
347         return FALSE;
348     }
349
350     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
351     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
352         WARN("Can't find 'strh' chunk\n");
353         return FALSE;
354     }
355
356     infoPtr->fnmmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
357     TRACE("ash.fccType='%c%c%c%c'\n",           LOBYTE(LOWORD(infoPtr->ash.fccType)), 
358                                                 HIBYTE(LOWORD(infoPtr->ash.fccType)), 
359                                                 LOBYTE(HIWORD(infoPtr->ash.fccType)), 
360                                                 HIBYTE(HIWORD(infoPtr->ash.fccType)));
361     TRACE("ash.fccHandler='%c%c%c%c'\n",        LOBYTE(LOWORD(infoPtr->ash.fccHandler)), 
362                                                 HIBYTE(LOWORD(infoPtr->ash.fccHandler)), 
363                                                 LOBYTE(HIWORD(infoPtr->ash.fccHandler)), 
364                                                 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
365     TRACE("ash.dwFlags=%ld\n",                  infoPtr->ash.dwFlags);
366     TRACE("ash.wPriority=%d\n",                 infoPtr->ash.wPriority);
367     TRACE("ash.wLanguage=%d\n",                 infoPtr->ash.wLanguage);
368     TRACE("ash.dwInitialFrames=%ld\n",          infoPtr->ash.dwInitialFrames);
369     TRACE("ash.dwScale=%ld\n",                  infoPtr->ash.dwScale);
370     TRACE("ash.dwRate=%ld\n",                   infoPtr->ash.dwRate);
371     TRACE("ash.dwStart=%ld\n",                  infoPtr->ash.dwStart);
372     TRACE("ash.dwLength=%ld\n",                 infoPtr->ash.dwLength);
373     TRACE("ash.dwSuggestedBufferSize=%ld\n",    infoPtr->ash.dwSuggestedBufferSize);
374     TRACE("ash.dwQuality=%ld\n",                infoPtr->ash.dwQuality);
375     TRACE("ash.dwSampleSize=%ld\n",             infoPtr->ash.dwSampleSize);
376     TRACE("ash.rcFrame=(%d,%d,%d,%d)\n",        infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left, 
377           infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
378     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
379
380     mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
381     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
382         WARN("Can't find 'strh' chunk\n");
383         return FALSE;
384     }
385
386     infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
387     if (!infoPtr->inbih) {
388         WARN("Can't alloc input BIH\n");
389         return FALSE;
390     }
391
392     infoPtr->fnmmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
393     TRACE("bih.biSize=%ld\n",           infoPtr->inbih->biSize);
394     TRACE("bih.biWidth=%ld\n",          infoPtr->inbih->biWidth);
395     TRACE("bih.biHeight=%ld\n",         infoPtr->inbih->biHeight);
396     TRACE("bih.biPlanes=%d\n",          infoPtr->inbih->biPlanes);
397     TRACE("bih.biBitCount=%d\n",        infoPtr->inbih->biBitCount);
398     TRACE("bih.biCompression=%ld\n",    infoPtr->inbih->biCompression);
399     TRACE("bih.biSizeImage=%ld\n",      infoPtr->inbih->biSizeImage);
400     TRACE("bih.biXPelsPerMeter=%ld\n",  infoPtr->inbih->biXPelsPerMeter);
401     TRACE("bih.biYPelsPerMeter=%ld\n",  infoPtr->inbih->biYPelsPerMeter);
402     TRACE("bih.biClrUsed=%ld\n",        infoPtr->inbih->biClrUsed);
403     TRACE("bih.biClrImportant=%ld\n",   infoPtr->inbih->biClrImportant);
404     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
405
406     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckList, 0);
407     
408 #if 0
409     /* an AVI has 0 or 1 video stream, and to be animated should not contain
410      * an audio stream, so only one strl is allowed 
411      */
412     mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
413     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
414         WARN("There should be a single 'strl' list\n");
415         return FALSE;
416     }
417 #endif
418
419     infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckHead, 0);
420
421     /* no need to read optional JUNK chunk */
422
423     mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
424     if (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
425         WARN("Can't find 'movi' list\n");
426         return FALSE;
427     }
428
429     /* FIXME: should handle the 'rec ' LIST when present */
430
431     infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
432                                  infoPtr->mah.dwTotalFrames * sizeof(DWORD));
433     if (!infoPtr->lpIndex) {
434         WARN("Can't alloc index array\n");
435         return FALSE;
436     }
437
438     numFrame = insize = 0;
439     while (infoPtr->fnmmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 && 
440            numFrame < infoPtr->mah.dwTotalFrames) {
441         infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
442         if (insize < mmckInfo.cksize)
443             insize = mmckInfo.cksize;
444         numFrame++;
445         infoPtr->fnmmioAscend(infoPtr->hMMio, &mmckInfo, 0);
446     }
447     if (numFrame != infoPtr->mah.dwTotalFrames) {
448         WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
449         return FALSE;
450     }
451     if (insize > infoPtr->ash.dwSuggestedBufferSize) {
452         WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
453         infoPtr->ash.dwSuggestedBufferSize = insize;
454     }
455
456     infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
457     if (!infoPtr->indata) {
458         WARN("Can't alloc input buffer\n");
459         return FALSE;
460     }
461
462     return TRUE;
463 }
464
465
466 static BOOL    ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
467 {
468     DWORD       outSize;
469
470     /* check uncompressed AVI */
471     if (infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ') ||
472         infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) {
473         infoPtr->hic = 0;
474         return TRUE;
475     }
476
477     /* try to get a decompressor for that type */
478     infoPtr->hic = (infoPtr->fnICOpen)(ICTYPE_VIDEO, 
479                                        infoPtr->ash.fccHandler, 
480                                        ICMODE_DECOMPRESS);
481     if (!infoPtr->hic) {
482         WARN("Can't load codec for the file\n");
483         return FALSE;
484     }
485     
486     outSize = (infoPtr->fnICSendMessage)(infoPtr->hic, 
487                                          ICM_DECOMPRESS_GET_FORMAT, 
488                                          (DWORD)infoPtr->inbih, 0L);
489     infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
490     if (!infoPtr->outbih) {
491         WARN("Can't alloc output BIH\n");
492         return FALSE;
493     }
494
495     if ((infoPtr->fnICSendMessage)(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT, 
496                                    (DWORD)infoPtr->inbih, 
497                                    (DWORD)infoPtr->outbih) != ICERR_OK) {
498         WARN("Can't get output BIH\n");
499         return FALSE;
500     }
501
502     infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
503     if (!infoPtr->outdata) {
504         WARN("Can't alloc output buffer\n");
505         return FALSE;
506     }
507
508     if ((infoPtr->fnICSendMessage)(infoPtr->hic, ICM_DECOMPRESS_BEGIN, 
509                                    (DWORD)infoPtr->inbih, 
510                                    (DWORD)infoPtr->outbih) != ICERR_OK) {
511         WARN("Can't begin decompression\n");
512         return FALSE;
513     }
514
515     return TRUE;
516 }
517
518 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
519 {
520     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
521     HINSTANCE hInstance = (HINSTANCE)wParam;
522
523     ANIMATE_Free(infoPtr);
524
525     if (!lParam) {
526         TRACE("Closing avi!\n");
527         return TRUE;
528     }
529     
530     if (!hInstance)
531        hInstance = GetWindowLongA(hWnd, GWL_HINSTANCE);
532
533     if (HIWORD(lParam)) {
534         TRACE("(\"%s\");\n", (LPSTR)lParam);
535
536         if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
537             TRACE("No AVI resource found!\n");
538             if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
539                 WARN("No AVI file found!\n");
540                 return FALSE;
541             }
542         }
543     } else {
544         TRACE("(%u);\n", (WORD)LOWORD(lParam));
545
546         if (!ANIMATE_LoadResA(infoPtr, hInstance,
547                               MAKEINTRESOURCEA((INT)lParam))) {
548             WARN("No AVI resource found!\n");
549             return FALSE;
550         }
551     }
552
553     if (!ANIMATE_GetAviInfo(infoPtr)) {
554         WARN("Can't get AVI information\n");
555         ANIMATE_Free(infoPtr);
556         return FALSE;
557     }
558
559     if (!ANIMATE_GetAviCodec(infoPtr)) {
560         WARN("Can't get AVI Codec\n");
561         ANIMATE_Free(infoPtr);
562         return FALSE;
563     }
564
565     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
566         FIXME("ACS_CENTER: NIY\n");
567     } else {
568         /*      MoveWindow(hWnd, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight, FALSE);*/
569         SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
570                      SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
571     }
572
573     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT) {
574         FIXME("ACS_TRANSPARENT: NIY\n");
575     }
576
577     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
578         return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
579     }
580
581     return TRUE;
582 }
583
584
585 /* << ANIMATE_Open32W >> */
586
587 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
588 {
589     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
590
591     /* nothing opened */
592     if (!infoPtr->hMMio)
593         return FALSE;
594
595     ANIMATE_DoStop(infoPtr);
596     return TRUE;
597 }
598
599
600 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
601 {
602     ANIMATE_INFO*       infoPtr;
603     HMODULE             hModule = LoadLibraryA("msvfw32.dll");
604
605     if (!hModule)
606         return FALSE;
607
608     /* allocate memory for info structure */
609     infoPtr = (ANIMATE_INFO *)COMCTL32_Alloc(sizeof(ANIMATE_INFO));
610     if (!infoPtr) {
611         ERR("could not allocate info memory!\n");
612         return 0;
613     }
614
615     /* Temporary hack until we get dllglue up and running */
616     infoPtr->fnICOpen        = (void*)GetProcAddress(hModule, "ICOpen");
617     infoPtr->fnICClose       = (void*)GetProcAddress(hModule, "ICClose");
618     infoPtr->fnICSendMessage = (void*)GetProcAddress(hModule, "ICSendMessage");
619     infoPtr->fnICDecompress  = (void*)GetProcAddress(hModule, "ICDecompress");
620
621     TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)GetParent(hWnd));
622
623     /* store crossref hWnd <-> info structure */
624     SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
625     infoPtr->hWnd = hWnd;
626
627     hModWinmm = LoadLibraryA("WINMM");
628
629     infoPtr->fnmmioOpenA = (void*)GetProcAddress(hModWinmm, "mmioOpenA");
630     infoPtr->fnmmioClose = (void*)GetProcAddress(hModWinmm, "mmioClose");
631     infoPtr->fnmmioAscend = (void*)GetProcAddress(hModWinmm, "mmioAscend");
632     infoPtr->fnmmioDescend = (void*)GetProcAddress(hModWinmm, "mmioDescend");
633     infoPtr->fnmmioSeek = (void*)GetProcAddress(hModWinmm, "mmioSeek");
634     infoPtr->fnmmioRead = (void*)GetProcAddress(hModWinmm, "mmioRead");
635
636     InitializeCriticalSection(&infoPtr->cs);
637     
638     return 0;
639 }
640
641
642 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
643 {
644     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
645
646
647     /* free avi data */
648     ANIMATE_Free(infoPtr);
649
650     /* free animate info data */
651     COMCTL32_Free(infoPtr);
652     SetWindowLongA(hWnd, 0, 0);
653
654     FreeLibrary(hModWinmm);
655     return 0;
656 }
657
658
659 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
660 {
661     RECT rect;
662
663     GetClientRect(hWnd, &rect);
664 #if 0
665     HBRUSH hBrush = CreateSolidBrush(infoPtr->clrBk);
666
667     FillRect((HDC)wParam, &rect, hBrush);
668     DeleteObject(hBrush);
669 #else
670     FillRect((HDC)wParam, &rect, GetSysColorBrush(COLOR_WINDOW));
671 #endif
672     return TRUE;
673 }
674
675 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
676 {
677     ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
678
679     if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
680         FIXME("NIY\n");
681         if (infoPtr->hMMio) {
682             /* centers the animation in the control, invalidates the control
683              */
684         }
685         InvalidateRect(hWnd, NULL, TRUE);
686     }
687     return TRUE;
688 }
689
690 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
691 {
692     TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
693     if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
694         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
695     switch (uMsg)
696     {
697     case ACM_OPENA:
698         return ANIMATE_OpenA(hWnd, wParam, lParam);
699         
700         /*      case ACM_OPEN32W: FIXME!! */
701         /*          return ANIMATE_Open32W(hWnd, wParam, lParam); */
702         
703     case ACM_PLAY:
704         return ANIMATE_Play(hWnd, wParam, lParam);
705         
706     case ACM_STOP:
707         return ANIMATE_Stop(hWnd, wParam, lParam);
708         
709     case WM_NCCREATE:
710         ANIMATE_Create(hWnd, wParam, lParam);
711         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
712         
713     case WM_NCHITTEST:
714         return HTTRANSPARENT;
715
716     case WM_DESTROY:
717         ANIMATE_Destroy(hWnd, wParam, lParam);
718         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
719         
720     case WM_ERASEBKGND:
721         ANIMATE_EraseBackground(hWnd, wParam, lParam);
722         break;
723
724     /*  case WM_STYLECHANGED: FIXME shall we do something ?? */
725
726     case WM_TIMER:
727         return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
728         
729     case WM_CLOSE:
730         ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
731         return TRUE;
732
733     case WM_PAINT:
734         if (wParam) {
735             ANIMATE_PaintFrame(ANIMATE_GetInfoPtr(hWnd), (HDC)wParam);
736         } else {
737             PAINTSTRUCT ps;
738             HDC hDC = BeginPaint(hWnd, &ps);
739             ANIMATE_PaintFrame(ANIMATE_GetInfoPtr(hWnd), hDC);
740             EndPaint(hWnd, &ps);
741         }
742         break;
743
744     case WM_SIZE:
745         ANIMATE_Size(hWnd, wParam, lParam);
746         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
747
748     default:
749         if (uMsg >= WM_USER)
750             ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
751         
752         return DefWindowProcA(hWnd, uMsg, wParam, lParam);
753     }
754     return 0;
755 }
756
757
758 void ANIMATE_Register(void)
759 {
760     WNDCLASSA wndClass;
761
762     ZeroMemory(&wndClass, sizeof(WNDCLASSA));
763     wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
764     wndClass.lpfnWndProc   = (WNDPROC)ANIMATE_WindowProc;
765     wndClass.cbClsExtra    = 0;
766     wndClass.cbWndExtra    = sizeof(ANIMATE_INFO *);
767     wndClass.hCursor       = LoadCursorA(0, IDC_ARROWA);
768     wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
769     wndClass.lpszClassName = ANIMATE_CLASSA;
770  
771     RegisterClassA(&wndClass);
772 }
773
774
775 void ANIMATE_Unregister(void)
776 {
777     UnregisterClassA(ANIMATE_CLASSA, (HINSTANCE)NULL);
778 }
779