Don't return overflow if no class buffer was specified.
[wine/dcerpc.git] / dlls / comctl32 / animate.c
blobdbcc02334c744ffe696df45b855562232da864da
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
5 * Copyright 1998, 1999 Eric Kohl
6 * 1999 Eric Pouech
8 * NOTES
9 * I will only improve this control once in a while.
10 * Eric <ekohl@abo.rhein-zeitung.de>
12 * TODO:
13 * - check for the 'rec ' list in some AVI files
14 * - concurrent access to infoPtr
17 #include <string.h>
18 #include "winbase.h"
19 #include "commctrl.h"
20 #include "vfw.h"
21 #include "mmsystem.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(animate);
26 static struct {
27 HMODULE hModule;
28 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
29 LRESULT (WINAPI *fnICClose)(HIC);
30 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD, DWORD);
31 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
32 } fnIC;
34 typedef struct
36 /* reference to input stream (file or resource) */
37 HGLOBAL hRes;
38 HMMIO hMMio; /* handle to mmio stream */
39 HWND hWnd;
40 /* information on the loaded AVI file */
41 MainAVIHeader mah;
42 AVIStreamHeader ash;
43 LPBITMAPINFOHEADER inbih;
44 LPDWORD lpIndex;
45 /* data for the decompressor */
46 HIC hic;
47 LPBITMAPINFOHEADER outbih;
48 LPVOID indata;
49 LPVOID outdata;
50 /* data for the background mechanism */
51 CRITICAL_SECTION cs;
52 HANDLE hThread;
53 UINT uTimer;
54 /* data for playing the file */
55 int nFromFrame;
56 int nToFrame;
57 int nLoop;
58 int currFrame;
59 /* tranparency info*/
60 COLORREF transparentColor;
61 HBRUSH hbrushBG;
62 HBITMAP hbmPrevFrame;
63 } ANIMATE_INFO;
65 #define ANIMATE_GetInfoPtr(hWnd) ((ANIMATE_INFO *)GetWindowLongA(hWnd, 0))
66 #define ANIMATE_COLOR_NONE 0xffffffff
68 static void ANIMATE_Notify(ANIMATE_INFO* infoPtr, UINT notif)
70 SendMessageA(GetParent(infoPtr->hWnd), WM_COMMAND,
71 MAKEWPARAM(GetDlgCtrlID(infoPtr->hWnd), notif),
72 (LPARAM)infoPtr->hWnd);
75 static BOOL ANIMATE_LoadResA(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPSTR lpName)
77 HRSRC hrsrc;
78 MMIOINFO mminfo;
79 LPVOID lpAvi;
81 hrsrc = FindResourceA(hInst, lpName, "AVI");
82 if (!hrsrc)
83 return FALSE;
85 infoPtr->hRes = LoadResource(hInst, hrsrc);
86 if (!infoPtr->hRes)
87 return FALSE;
89 lpAvi = LockResource(infoPtr->hRes);
90 if (!lpAvi)
91 return FALSE;
93 memset(&mminfo, 0, sizeof(mminfo));
94 mminfo.fccIOProc = FOURCC_MEM;
95 mminfo.pchBuffer = (LPSTR)lpAvi;
96 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
97 infoPtr->hMMio = mmioOpenA(NULL, &mminfo, MMIO_READ);
98 if (!infoPtr->hMMio) {
99 GlobalFree((HGLOBAL)lpAvi);
100 return FALSE;
103 return TRUE;
107 static BOOL ANIMATE_LoadFileA(ANIMATE_INFO *infoPtr, LPSTR lpName)
109 infoPtr->hMMio = mmioOpenA((LPSTR)lpName, NULL,
110 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
112 if (!infoPtr->hMMio)
113 return FALSE;
115 return TRUE;
119 static LRESULT ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
121 EnterCriticalSection(&infoPtr->cs);
123 /* should stop playing */
124 if (infoPtr->hThread)
126 if (!TerminateThread(infoPtr->hThread,0))
127 WARN("could not destroy animation thread!\n");
128 infoPtr->hThread = 0;
130 if (infoPtr->uTimer) {
131 KillTimer(infoPtr->hWnd, infoPtr->uTimer);
132 infoPtr->uTimer = 0;
135 LeaveCriticalSection(&infoPtr->cs);
137 ANIMATE_Notify(infoPtr, ACN_STOP);
139 return TRUE;
143 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
145 if (infoPtr->hMMio) {
146 ANIMATE_DoStop(infoPtr);
147 mmioClose(infoPtr->hMMio, 0);
148 if (infoPtr->hRes) {
149 FreeResource(infoPtr->hRes);
150 infoPtr->hRes = 0;
152 if (infoPtr->lpIndex) {
153 HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
154 infoPtr->lpIndex = NULL;
156 if (infoPtr->hic) {
157 fnIC.fnICClose(infoPtr->hic);
158 infoPtr->hic = 0;
160 if (infoPtr->inbih) {
161 HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
162 infoPtr->inbih = NULL;
164 if (infoPtr->outbih) {
165 HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
166 infoPtr->outbih = NULL;
168 if( infoPtr->indata )
170 HeapFree(GetProcessHeap(), 0, infoPtr->indata);
171 infoPtr->indata = NULL;
173 if( infoPtr->outdata )
175 HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
176 infoPtr->outdata = NULL;
178 if( infoPtr->hbmPrevFrame )
180 DeleteObject(infoPtr->hbmPrevFrame);
181 infoPtr->hbmPrevFrame = 0;
183 infoPtr->indata = infoPtr->outdata = NULL;
184 infoPtr->hWnd = 0;
185 infoPtr->hMMio = 0;
187 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
188 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
189 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
191 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
194 static void ANIMATE_TransparentBlt(ANIMATE_INFO* infoPtr, HDC hdcDest, HDC hdcSource)
196 HDC hdcMask;
197 HBITMAP hbmMask;
198 HBITMAP hbmOld;
200 /* create a transparency mask */
201 hdcMask = CreateCompatibleDC(hdcDest);
202 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
203 hbmOld = SelectObject(hdcMask, hbmMask);
205 SetBkColor(hdcSource,infoPtr->transparentColor);
206 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
208 /* mask the source bitmap */
209 SetBkColor(hdcSource, RGB(0,0,0));
210 SetTextColor(hdcSource, RGB(255,255,255));
211 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
213 /* mask the destination bitmap */
214 SetBkColor(hdcDest, RGB(255,255,255));
215 SetTextColor(hdcDest, RGB(0,0,0));
216 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
218 /* combine source and destination */
219 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
221 SelectObject(hdcMask, hbmOld);
222 DeleteObject(hbmMask);
223 DeleteDC(hdcMask);
226 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
228 void* pBitmapData = NULL;
229 LPBITMAPINFO pBitmapInfo = NULL;
231 HDC hdcMem;
232 HBITMAP hbmOld;
234 int nOffsetX = 0;
235 int nOffsetY = 0;
237 int nWidth;
238 int nHeight;
240 if (!hDC || !infoPtr->inbih)
241 return TRUE;
243 if (infoPtr->hic )
245 pBitmapData = infoPtr->outdata;
246 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
248 nWidth = infoPtr->outbih->biWidth;
249 nHeight = infoPtr->outbih->biHeight;
250 } else
252 pBitmapData = infoPtr->indata;
253 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
255 nWidth = infoPtr->inbih->biWidth;
256 nHeight = infoPtr->inbih->biHeight;
259 if(!infoPtr->hbmPrevFrame)
261 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
264 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
266 hdcMem = CreateCompatibleDC(hDC);
267 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
270 * we need to get the transparent color even without ACS_TRANSPARENT,
271 * because the style can be changed later on and the color should always
272 * be obtained in the first frame
274 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
276 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
279 if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT)
281 HDC hdcFinal = CreateCompatibleDC(hDC);
282 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
283 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
284 RECT rect;
286 rect.left = 0;
287 rect.top = 0;
288 rect.right = nWidth;
289 rect.bottom = nHeight;
291 if(!infoPtr->hbrushBG)
292 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
294 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
295 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
297 SelectObject(hdcFinal, hbmOld2);
298 SelectObject(hdcMem, hbmFinal);
299 DeleteDC(hdcFinal);
300 DeleteObject(infoPtr->hbmPrevFrame);
301 infoPtr->hbmPrevFrame = hbmFinal;
304 if (GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_CENTER)
306 RECT rect;
308 GetWindowRect(infoPtr->hWnd, &rect);
309 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
310 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
312 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
314 SelectObject(hdcMem, hbmOld);
315 DeleteDC(hdcMem);
316 return TRUE;
319 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
321 HDC hDC;
323 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
325 EnterCriticalSection(&infoPtr->cs);
327 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
328 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
330 if (infoPtr->hic &&
331 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
332 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
333 LeaveCriticalSection(&infoPtr->cs);
334 WARN("Decompression error\n");
335 return FALSE;
338 if ((hDC = GetDC(infoPtr->hWnd)) != 0) {
339 ANIMATE_PaintFrame(infoPtr, hDC);
340 ReleaseDC(infoPtr->hWnd, hDC);
343 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
344 infoPtr->currFrame = infoPtr->nFromFrame;
345 if (infoPtr->nLoop != -1) {
346 if (--infoPtr->nLoop == 0) {
347 ANIMATE_DoStop(infoPtr);
351 LeaveCriticalSection(&infoPtr->cs);
353 return TRUE;
356 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
358 ANIMATE_INFO* infoPtr = (ANIMATE_INFO*)ptr_;
359 HDC hDC;
361 if(!infoPtr)
363 WARN("animation structure undefined!\n");
364 return FALSE;
367 while(1)
369 if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT)
371 hDC = GetDC(infoPtr->hWnd);
372 /* sometimes the animation window will be destroyed in between
373 * by the main program, so a ReleaseDC() error msg is possible */
374 infoPtr->hbrushBG = SendMessageA(GetParent(infoPtr->hWnd),WM_CTLCOLORSTATIC,hDC, infoPtr->hWnd);
375 ReleaseDC(infoPtr->hWnd,hDC);
378 EnterCriticalSection(&infoPtr->cs);
379 ANIMATE_DrawFrame(infoPtr);
380 LeaveCriticalSection(&infoPtr->cs);
382 /* time is in microseconds, we should convert it to milliseconds */
383 Sleep((infoPtr->mah.dwMicroSecPerFrame+500)/1000);
385 return TRUE;
388 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
390 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
392 /* nothing opened */
393 if (!infoPtr->hMMio)
394 return FALSE;
396 if (infoPtr->hThread || infoPtr->uTimer) {
397 FIXME("Already playing ? what should I do ??\n");
398 ANIMATE_DoStop(infoPtr);
401 infoPtr->nFromFrame = (INT)LOWORD(lParam);
402 infoPtr->nToFrame = (INT)HIWORD(lParam);
403 infoPtr->nLoop = (INT)wParam;
405 if (infoPtr->nToFrame == 0xFFFF)
406 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
408 TRACE("(repeat=%d from=%d to=%d);\n",
409 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
411 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
412 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
413 return FALSE;
415 infoPtr->currFrame = infoPtr->nFromFrame;
417 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
418 TRACE("Using a timer\n");
419 /* create a timer to display AVI */
420 infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
421 } else {
422 DWORD threadID;
424 TRACE("Using an animation thread\n");
425 infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr,0,0 &threadID);
426 if(!infoPtr->hThread)
428 ERR("Could not create animation thread!\n");
429 return FALSE;
434 ANIMATE_Notify(infoPtr, ACN_START);
436 return TRUE;
440 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
442 MMCKINFO ckMainRIFF;
443 MMCKINFO mmckHead;
444 MMCKINFO mmckList;
445 MMCKINFO mmckInfo;
446 DWORD numFrame;
447 DWORD insize;
449 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
450 WARN("Can't find 'RIFF' chunk\n");
451 return FALSE;
454 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
455 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
456 WARN("Can't find 'AVI ' chunk\n");
457 return FALSE;
460 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
461 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
462 WARN("Can't find 'hdrl' list\n");
463 return FALSE;
466 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
467 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
468 WARN("Can't find 'avih' chunk\n");
469 return FALSE;
472 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
474 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
475 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
476 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
477 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
478 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
479 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
480 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
481 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
482 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
483 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
485 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
487 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
488 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
489 WARN("Can't find 'strl' list\n");
490 return FALSE;
493 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
494 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
495 WARN("Can't find 'strh' chunk\n");
496 return FALSE;
499 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
501 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
502 HIBYTE(LOWORD(infoPtr->ash.fccType)),
503 LOBYTE(HIWORD(infoPtr->ash.fccType)),
504 HIBYTE(HIWORD(infoPtr->ash.fccType)));
505 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
506 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
507 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
508 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
509 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
510 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
511 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
512 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
513 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
514 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
515 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
516 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
517 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
518 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
519 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
520 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
521 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
523 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
525 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
526 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
527 WARN("Can't find 'strh' chunk\n");
528 return FALSE;
531 infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
532 if (!infoPtr->inbih) {
533 WARN("Can't alloc input BIH\n");
534 return FALSE;
537 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
539 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
540 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
541 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
542 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
543 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
544 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
545 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
546 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
547 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
548 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
549 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
551 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
553 mmioAscend(infoPtr->hMMio, &mmckList, 0);
555 #if 0
556 /* an AVI has 0 or 1 video stream, and to be animated should not contain
557 * an audio stream, so only one strl is allowed
559 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
560 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
561 WARN("There should be a single 'strl' list\n");
562 return FALSE;
564 #endif
566 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
568 /* no need to read optional JUNK chunk */
570 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
571 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
572 WARN("Can't find 'movi' list\n");
573 return FALSE;
576 /* FIXME: should handle the 'rec ' LIST when present */
578 infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
579 infoPtr->mah.dwTotalFrames * sizeof(DWORD));
580 if (!infoPtr->lpIndex) {
581 WARN("Can't alloc index array\n");
582 return FALSE;
585 numFrame = insize = 0;
586 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
587 numFrame < infoPtr->mah.dwTotalFrames) {
588 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
589 if (insize < mmckInfo.cksize)
590 insize = mmckInfo.cksize;
591 numFrame++;
592 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
594 if (numFrame != infoPtr->mah.dwTotalFrames) {
595 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
596 return FALSE;
598 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
599 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
600 infoPtr->ash.dwSuggestedBufferSize = insize;
603 infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
604 if (!infoPtr->indata) {
605 WARN("Can't alloc input buffer\n");
606 return FALSE;
609 return TRUE;
613 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
615 DWORD outSize;
617 /* check uncompressed AVI */
618 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
619 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
620 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
622 infoPtr->hic = 0;
623 return TRUE;
626 /* try to get a decompressor for that type */
627 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
628 if (!infoPtr->hic) {
629 WARN("Can't load codec for the file\n");
630 return FALSE;
633 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
634 (DWORD)infoPtr->inbih, 0L);
636 infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
637 if (!infoPtr->outbih) {
638 WARN("Can't alloc output BIH\n");
639 return FALSE;
642 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
643 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
644 WARN("Can't get output BIH\n");
645 return FALSE;
648 infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
649 if (!infoPtr->outdata) {
650 WARN("Can't alloc output buffer\n");
651 return FALSE;
654 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
655 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
656 WARN("Can't begin decompression\n");
657 return FALSE;
660 return TRUE;
663 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
665 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
666 HINSTANCE hInstance = (HINSTANCE)wParam;
668 ANIMATE_Free(infoPtr);
669 infoPtr->hWnd = hWnd;
671 if (!lParam) {
672 TRACE("Closing avi!\n");
673 return TRUE;
676 if (!hInstance)
677 hInstance = GetWindowLongA(hWnd, GWL_HINSTANCE);
679 if (HIWORD(lParam)) {
680 TRACE("(\"%s\");\n", (LPSTR)lParam);
682 if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
683 TRACE("No AVI resource found!\n");
684 if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
685 WARN("No AVI file found!\n");
686 return FALSE;
689 } else {
690 TRACE("(%u);\n", (WORD)LOWORD(lParam));
692 if (!ANIMATE_LoadResA(infoPtr, hInstance,
693 MAKEINTRESOURCEA((INT)lParam))) {
694 WARN("No AVI resource found!\n");
695 return FALSE;
699 if (!ANIMATE_GetAviInfo(infoPtr)) {
700 WARN("Can't get AVI information\n");
701 ANIMATE_Free(infoPtr);
702 return FALSE;
705 if (!ANIMATE_GetAviCodec(infoPtr)) {
706 WARN("Can't get AVI Codec\n");
707 ANIMATE_Free(infoPtr);
708 return FALSE;
711 if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
712 SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
713 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
716 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
717 return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
720 return TRUE;
724 /* << ANIMATE_Open32W >> */
726 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
728 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
730 /* nothing opened */
731 if (!infoPtr->hMMio)
732 return FALSE;
734 ANIMATE_DoStop(infoPtr);
735 return TRUE;
739 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
741 ANIMATE_INFO* infoPtr;
743 if (!fnIC.hModule) /* FIXME: not thread safe */
745 /* since there's a circular dep between msvfw32 and comctl32, we could either:
746 * - fix the build chain to allow this circular dep
747 * - handle it by hand
748 * AJ wants the latter :-(
750 fnIC.hModule = LoadLibraryA("msvfw32.dll");
751 if (!fnIC.hModule) return FALSE;
753 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
754 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
755 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
756 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
759 /* allocate memory for info structure */
760 infoPtr = (ANIMATE_INFO *)COMCTL32_Alloc(sizeof(ANIMATE_INFO));
761 if (!infoPtr) {
762 ERR("could not allocate info memory!\n");
763 return 0;
766 TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)GetParent(hWnd));
768 /* store crossref hWnd <-> info structure */
769 SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
770 infoPtr->hWnd = hWnd;
771 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
772 infoPtr->hbmPrevFrame = 0;
774 InitializeCriticalSection(&infoPtr->cs);
776 return 0;
780 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
782 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
785 /* free avi data */
786 ANIMATE_Free(infoPtr);
788 /* free animate info data */
789 COMCTL32_Free(infoPtr);
790 SetWindowLongA(hWnd, 0, 0);
792 return 0;
796 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
798 RECT rect;
799 HBRUSH hBrush = 0;
801 if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
803 hBrush = SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,(HDC)wParam, hWnd);
806 GetClientRect(hWnd, &rect);
807 FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
809 return TRUE;
812 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
814 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
815 InvalidateRect(hWnd, NULL, TRUE);
817 return TRUE;
820 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
822 TRACE("hwnd=%x msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
823 if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
824 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
825 switch (uMsg)
827 case ACM_OPENA:
828 return ANIMATE_OpenA(hWnd, wParam, lParam);
830 /* case ACM_OPEN32W: FIXME!! */
831 /* return ANIMATE_Open32W(hWnd, wParam, lParam); */
833 case ACM_PLAY:
834 return ANIMATE_Play(hWnd, wParam, lParam);
836 case ACM_STOP:
837 return ANIMATE_Stop(hWnd, wParam, lParam);
839 case WM_NCCREATE:
840 ANIMATE_Create(hWnd, wParam, lParam);
841 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
843 case WM_NCHITTEST:
844 return HTTRANSPARENT;
846 case WM_DESTROY:
847 ANIMATE_Destroy(hWnd, wParam, lParam);
848 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
850 case WM_ERASEBKGND:
851 ANIMATE_EraseBackground(hWnd, wParam, lParam);
852 break;
854 /* case WM_STYLECHANGED: FIXME shall we do something ?? */
856 case WM_TIMER:
857 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
859 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
860 infoPtr->hbrushBG = SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,(HDC)wParam, hWnd);
862 return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
864 case WM_CLOSE:
865 ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
866 return TRUE;
868 case WM_PAINT:
870 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
872 /* the animation isn't playing, don't paint */
873 if(!infoPtr->uTimer && !infoPtr->hThread)
874 /* default paint handling */
875 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
877 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
878 infoPtr->hbrushBG = SendMessageA(GetParent(hWnd), WM_CTLCOLORSTATIC,
879 (HDC)wParam, hWnd);
881 if (wParam)
883 EnterCriticalSection(&infoPtr->cs);
884 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
885 LeaveCriticalSection(&infoPtr->cs);
887 else
889 PAINTSTRUCT ps;
890 HDC hDC = BeginPaint(hWnd, &ps);
892 EnterCriticalSection(&infoPtr->cs);
893 ANIMATE_PaintFrame(infoPtr, hDC);
894 LeaveCriticalSection(&infoPtr->cs);
896 EndPaint(hWnd, &ps);
899 break;
901 case WM_SIZE:
902 ANIMATE_Size(hWnd, wParam, lParam);
903 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
905 default:
906 if (uMsg >= WM_USER)
907 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
909 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
911 return 0;
914 void ANIMATE_Register(void)
916 WNDCLASSA wndClass;
918 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
919 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
920 wndClass.lpfnWndProc = (WNDPROC)ANIMATE_WindowProc;
921 wndClass.cbClsExtra = 0;
922 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
923 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
924 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
925 wndClass.lpszClassName = ANIMATE_CLASSA;
927 RegisterClassA(&wndClass);
931 void ANIMATE_Unregister(void)
933 UnregisterClassA(ANIMATE_CLASSA, (HINSTANCE)NULL);