comctl32: Change some overly exciting tracing messages.
[wine.git] / dlls / comctl32 / animate.c
blob12c4f65c4aa120593fff32b29b487a5b95faf15c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
5 * Copyright 1998, 1999 Eric Kohl
6 * Copyright 1999 Eric Pouech
7 * Copyright 2005 Dimitrie O. Paun
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * NOTES
25 * This code was audited for completeness against the documented features
26 * of Comctl32.dll version 6.0 on Mar. 15, 2005, by Dimitrie O. Paun.
28 * Unless otherwise noted, we believe this code to be complete, as per
29 * the specification mentioned above.
30 * If you discover missing features, or bugs, please note them below.
32 * TODO:
33 * - check for the 'rec ' list in some AVI files
36 #include <stdarg.h>
37 #include <string.h>
38 #include "windef.h"
39 #include "winbase.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42 #include "winnls.h"
43 #include "commctrl.h"
44 #include "vfw.h"
45 #include "mmsystem.h"
46 #include "comctl32.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(animate);
51 static struct {
52 HMODULE hModule;
53 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
54 LRESULT (WINAPI *fnICClose)(HIC);
55 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD_PTR, DWORD_PTR);
56 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
57 } fnIC;
59 typedef struct
61 /* reference to input stream (file or resource) */
62 HGLOBAL hRes;
63 HMMIO hMMio; /* handle to mmio stream */
64 HWND hwndSelf;
65 HWND hwndNotify;
66 DWORD dwStyle;
67 /* information on the loaded AVI file */
68 MainAVIHeader mah;
69 AVIStreamHeader ash;
70 LPBITMAPINFOHEADER inbih;
71 LPDWORD lpIndex;
72 /* data for the decompressor */
73 HIC hic;
74 LPBITMAPINFOHEADER outbih;
75 LPVOID indata;
76 LPVOID outdata;
77 /* data for the background mechanism */
78 CRITICAL_SECTION cs;
79 HANDLE hStopEvent;
80 HANDLE hThread;
81 DWORD threadId;
82 UINT uTimer;
83 /* data for playing the file */
84 int nFromFrame;
85 int nToFrame;
86 int nLoop;
87 int currFrame;
88 /* transparency info*/
89 COLORREF transparentColor;
90 HBRUSH hbrushBG;
91 HBITMAP hbmPrevFrame;
92 } ANIMATE_INFO;
94 #define ANIMATE_COLOR_NONE 0xffffffff
96 static void ANIMATE_Notify(const ANIMATE_INFO *infoPtr, UINT notif)
98 PostMessageW(infoPtr->hwndNotify, WM_COMMAND,
99 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
100 (LPARAM)infoPtr->hwndSelf);
103 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPCWSTR lpName)
105 static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
106 HRSRC hrsrc;
107 MMIOINFO mminfo;
108 LPVOID lpAvi;
110 hrsrc = FindResourceW(hInst, lpName, aviW);
111 if (!hrsrc)
112 return FALSE;
114 infoPtr->hRes = LoadResource(hInst, hrsrc);
115 if (!infoPtr->hRes)
116 return FALSE;
118 lpAvi = LockResource(infoPtr->hRes);
119 if (!lpAvi)
120 return FALSE;
122 memset(&mminfo, 0, sizeof(mminfo));
123 mminfo.fccIOProc = FOURCC_MEM;
124 mminfo.pchBuffer = lpAvi;
125 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
126 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
127 if (!infoPtr->hMMio)
129 FreeResource(infoPtr->hRes);
130 return FALSE;
133 return TRUE;
137 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
139 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
141 if(!infoPtr->hMMio) return FALSE;
142 return TRUE;
146 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
148 BOOL stopped = FALSE;
150 EnterCriticalSection(&infoPtr->cs);
152 /* should stop playing */
153 if (infoPtr->hThread)
155 HANDLE handle = infoPtr->hThread;
157 TRACE("stopping animation thread\n");
158 infoPtr->hThread = 0;
159 SetEvent( infoPtr->hStopEvent );
161 if (infoPtr->threadId != GetCurrentThreadId())
163 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
164 WaitForSingleObject( handle, INFINITE );
165 TRACE("animation thread stopped\n");
166 EnterCriticalSection(&infoPtr->cs);
169 CloseHandle( handle );
170 CloseHandle( infoPtr->hStopEvent );
171 infoPtr->hStopEvent = 0;
172 stopped = TRUE;
174 if (infoPtr->uTimer) {
175 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
176 infoPtr->uTimer = 0;
177 stopped = TRUE;
180 LeaveCriticalSection(&infoPtr->cs);
182 if (stopped)
183 ANIMATE_Notify(infoPtr, ACN_STOP);
185 return TRUE;
189 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
191 if (infoPtr->hMMio) {
192 ANIMATE_DoStop(infoPtr);
193 mmioClose(infoPtr->hMMio, 0);
194 if (infoPtr->hRes) {
195 FreeResource(infoPtr->hRes);
196 infoPtr->hRes = 0;
198 Free (infoPtr->lpIndex);
199 infoPtr->lpIndex = NULL;
200 if (infoPtr->hic) {
201 fnIC.fnICClose(infoPtr->hic);
202 infoPtr->hic = 0;
204 Free (infoPtr->inbih);
205 infoPtr->inbih = NULL;
206 Free (infoPtr->outbih);
207 infoPtr->outbih = NULL;
208 Free (infoPtr->indata);
209 infoPtr->indata = NULL;
210 Free (infoPtr->outdata);
211 infoPtr->outdata = NULL;
212 if( infoPtr->hbmPrevFrame )
214 DeleteObject(infoPtr->hbmPrevFrame);
215 infoPtr->hbmPrevFrame = 0;
218 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
219 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
220 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
222 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
225 static void ANIMATE_TransparentBlt(ANIMATE_INFO const *infoPtr, HDC hdcDest, HDC hdcSource)
227 HDC hdcMask;
228 HBITMAP hbmMask;
229 HBITMAP hbmOld;
231 /* create a transparency mask */
232 hdcMask = CreateCompatibleDC(hdcDest);
233 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
234 hbmOld = SelectObject(hdcMask, hbmMask);
236 SetBkColor(hdcSource,infoPtr->transparentColor);
237 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
239 /* mask the source bitmap */
240 SetBkColor(hdcSource, RGB(0,0,0));
241 SetTextColor(hdcSource, RGB(255,255,255));
242 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
244 /* mask the destination bitmap */
245 SetBkColor(hdcDest, RGB(255,255,255));
246 SetTextColor(hdcDest, RGB(0,0,0));
247 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
249 /* combine source and destination */
250 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
252 SelectObject(hdcMask, hbmOld);
253 DeleteObject(hbmMask);
254 DeleteDC(hdcMask);
257 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
259 void const *pBitmapData;
260 BITMAPINFO const *pBitmapInfo;
261 HDC hdcMem;
262 HBITMAP hbmOld;
263 int nOffsetX = 0;
264 int nOffsetY = 0;
265 int nWidth;
266 int nHeight;
268 if (!hDC || !infoPtr->inbih)
269 return TRUE;
271 if (infoPtr->hic )
273 pBitmapData = infoPtr->outdata;
274 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
276 nWidth = infoPtr->outbih->biWidth;
277 nHeight = infoPtr->outbih->biHeight;
279 else
281 pBitmapData = infoPtr->indata;
282 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
284 nWidth = infoPtr->inbih->biWidth;
285 nHeight = infoPtr->inbih->biHeight;
288 if(!infoPtr->hbmPrevFrame)
290 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
293 hdcMem = CreateCompatibleDC(hDC);
294 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
296 SetDIBits(hdcMem, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, pBitmapInfo, DIB_RGB_COLORS);
299 * we need to get the transparent color even without ACS_TRANSPARENT,
300 * because the style can be changed later on and the color should always
301 * be obtained in the first frame
303 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
305 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
308 if(infoPtr->dwStyle & ACS_TRANSPARENT)
310 HDC hdcFinal = CreateCompatibleDC(hDC);
311 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
312 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
313 RECT rect;
315 SetRect(&rect, 0, 0, nWidth, nHeight);
317 if(!infoPtr->hbrushBG)
318 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
320 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
321 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
323 SelectObject(hdcFinal, hbmOld2);
324 SelectObject(hdcMem, hbmFinal);
325 DeleteDC(hdcFinal);
326 DeleteObject(infoPtr->hbmPrevFrame);
327 infoPtr->hbmPrevFrame = hbmFinal;
330 if (infoPtr->dwStyle & ACS_CENTER)
332 RECT rect;
334 GetWindowRect(infoPtr->hwndSelf, &rect);
335 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
336 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
338 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
340 SelectObject(hdcMem, hbmOld);
341 DeleteDC(hdcMem);
342 return TRUE;
345 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr, HDC hDC)
347 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
349 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
350 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
352 if (infoPtr->hic &&
353 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
354 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
355 WARN("Decompression error\n");
356 return FALSE;
359 ANIMATE_PaintFrame(infoPtr, hDC);
361 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
362 infoPtr->currFrame = infoPtr->nFromFrame;
363 if (infoPtr->nLoop != -1) {
364 if (--infoPtr->nLoop == 0) {
365 ANIMATE_DoStop(infoPtr);
370 return TRUE;
373 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
375 HDC hDC;
377 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
379 EnterCriticalSection(&infoPtr->cs);
380 ANIMATE_DrawFrame(infoPtr, hDC);
381 LeaveCriticalSection(&infoPtr->cs);
383 ReleaseDC(infoPtr->hwndSelf, hDC);
386 return 0;
389 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
391 ANIMATE_INFO *infoPtr = ptr_;
392 HANDLE event;
393 DWORD timeout;
395 while(1)
397 HDC hDC = GetDC(infoPtr->hwndSelf);
399 EnterCriticalSection(&infoPtr->cs);
400 ANIMATE_DrawFrame(infoPtr, hDC);
401 timeout = infoPtr->mah.dwMicroSecPerFrame;
402 event = infoPtr->hStopEvent;
403 LeaveCriticalSection(&infoPtr->cs);
405 ReleaseDC(infoPtr->hwndSelf, hDC);
407 /* time is in microseconds, we should convert it to milliseconds */
408 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
409 break;
411 return TRUE;
414 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
416 /* nothing opened */
417 if (!infoPtr->hMMio)
418 return FALSE;
420 if (infoPtr->hThread || infoPtr->uTimer) {
421 TRACE("Already playing\n");
422 return TRUE;
425 infoPtr->nFromFrame = wFrom;
426 infoPtr->nToFrame = wTo;
427 infoPtr->nLoop = cRepeat;
429 if (infoPtr->nToFrame == 0xFFFF)
430 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
432 TRACE("(repeat=%d from=%d to=%d);\n",
433 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
435 if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
436 (SHORT)infoPtr->nFromFrame < 0)
437 infoPtr->nFromFrame = 0;
439 if (infoPtr->nFromFrame > infoPtr->nToFrame ||
440 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
441 return FALSE;
443 infoPtr->currFrame = infoPtr->nFromFrame;
445 /* seek - doesn't need to start a thread or set a timer and neither
446 * does it send a notification */
447 if (infoPtr->nFromFrame == infoPtr->nToFrame)
449 HDC hDC;
451 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
453 ANIMATE_DrawFrame(infoPtr, hDC);
455 ReleaseDC(infoPtr->hwndSelf, hDC);
457 return TRUE;
460 if (infoPtr->dwStyle & ACS_TIMER)
462 TRACE("Using a timer\n");
463 /* create a timer to display AVI */
464 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
465 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
467 else
469 TRACE("Using an animation thread\n");
470 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
471 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
472 infoPtr, 0, &infoPtr->threadId);
473 if(!infoPtr->hThread) return FALSE;
477 ANIMATE_Notify(infoPtr, ACN_START);
479 return TRUE;
483 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
485 MMCKINFO ckMainRIFF;
486 MMCKINFO mmckHead;
487 MMCKINFO mmckList;
488 MMCKINFO mmckInfo;
489 DWORD numFrame;
490 DWORD insize;
492 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
493 WARN("Can't find 'RIFF' chunk\n");
494 return FALSE;
497 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
498 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
499 WARN("Can't find 'AVI ' chunk\n");
500 return FALSE;
503 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
504 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
505 WARN("Can't find 'hdrl' list\n");
506 return FALSE;
509 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
510 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
511 WARN("Can't find 'avih' chunk\n");
512 return FALSE;
515 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
517 TRACE("mah.dwMicroSecPerFrame=%d\n", infoPtr->mah.dwMicroSecPerFrame);
518 TRACE("mah.dwMaxBytesPerSec=%d\n", infoPtr->mah.dwMaxBytesPerSec);
519 TRACE("mah.dwPaddingGranularity=%d\n", infoPtr->mah.dwPaddingGranularity);
520 TRACE("mah.dwFlags=%d\n", infoPtr->mah.dwFlags);
521 TRACE("mah.dwTotalFrames=%d\n", infoPtr->mah.dwTotalFrames);
522 TRACE("mah.dwInitialFrames=%d\n", infoPtr->mah.dwInitialFrames);
523 TRACE("mah.dwStreams=%d\n", infoPtr->mah.dwStreams);
524 TRACE("mah.dwSuggestedBufferSize=%d\n", infoPtr->mah.dwSuggestedBufferSize);
525 TRACE("mah.dwWidth=%d\n", infoPtr->mah.dwWidth);
526 TRACE("mah.dwHeight=%d\n", infoPtr->mah.dwHeight);
528 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
530 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
531 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
532 WARN("Can't find 'strl' list\n");
533 return FALSE;
536 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
537 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
538 WARN("Can't find 'strh' chunk\n");
539 return FALSE;
542 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
544 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
545 HIBYTE(LOWORD(infoPtr->ash.fccType)),
546 LOBYTE(HIWORD(infoPtr->ash.fccType)),
547 HIBYTE(HIWORD(infoPtr->ash.fccType)));
548 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
549 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
550 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
551 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
552 TRACE("ash.dwFlags=%d\n", infoPtr->ash.dwFlags);
553 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
554 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
555 TRACE("ash.dwInitialFrames=%d\n", infoPtr->ash.dwInitialFrames);
556 TRACE("ash.dwScale=%d\n", infoPtr->ash.dwScale);
557 TRACE("ash.dwRate=%d\n", infoPtr->ash.dwRate);
558 TRACE("ash.dwStart=%d\n", infoPtr->ash.dwStart);
559 TRACE("ash.dwLength=%d\n", infoPtr->ash.dwLength);
560 TRACE("ash.dwSuggestedBufferSize=%d\n", infoPtr->ash.dwSuggestedBufferSize);
561 TRACE("ash.dwQuality=%d\n", infoPtr->ash.dwQuality);
562 TRACE("ash.dwSampleSize=%d\n", infoPtr->ash.dwSampleSize);
563 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
564 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
566 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
568 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
569 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
570 WARN("Can't find 'strh' chunk\n");
571 return FALSE;
574 infoPtr->inbih = Alloc(mmckInfo.cksize);
575 if (!infoPtr->inbih) {
576 WARN("Can't alloc input BIH\n");
577 return FALSE;
580 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
582 TRACE("bih.biSize=%d\n", infoPtr->inbih->biSize);
583 TRACE("bih.biWidth=%d\n", infoPtr->inbih->biWidth);
584 TRACE("bih.biHeight=%d\n", infoPtr->inbih->biHeight);
585 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
586 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
587 TRACE("bih.biCompression=%d\n", infoPtr->inbih->biCompression);
588 TRACE("bih.biSizeImage=%d\n", infoPtr->inbih->biSizeImage);
589 TRACE("bih.biXPelsPerMeter=%d\n", infoPtr->inbih->biXPelsPerMeter);
590 TRACE("bih.biYPelsPerMeter=%d\n", infoPtr->inbih->biYPelsPerMeter);
591 TRACE("bih.biClrUsed=%d\n", infoPtr->inbih->biClrUsed);
592 TRACE("bih.biClrImportant=%d\n", infoPtr->inbih->biClrImportant);
594 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
596 mmioAscend(infoPtr->hMMio, &mmckList, 0);
598 #if 0
599 /* an AVI has 0 or 1 video stream, and to be animated should not contain
600 * an audio stream, so only one strl is allowed
602 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
603 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
604 WARN("There should be a single 'strl' list\n");
605 return FALSE;
607 #endif
609 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
611 /* no need to read optional JUNK chunk */
613 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
614 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
615 WARN("Can't find 'movi' list\n");
616 return FALSE;
619 /* FIXME: should handle the 'rec ' LIST when present */
621 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
622 if (!infoPtr->lpIndex)
623 return FALSE;
625 numFrame = insize = 0;
626 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
627 numFrame < infoPtr->mah.dwTotalFrames) {
628 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
629 if (insize < mmckInfo.cksize)
630 insize = mmckInfo.cksize;
631 numFrame++;
632 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
634 if (numFrame != infoPtr->mah.dwTotalFrames) {
635 WARN("Found %d frames (/%d)\n", numFrame, infoPtr->mah.dwTotalFrames);
636 return FALSE;
638 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
639 WARN("insize=%d suggestedSize=%d\n", insize, infoPtr->ash.dwSuggestedBufferSize);
640 infoPtr->ash.dwSuggestedBufferSize = insize;
643 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
644 if (!infoPtr->indata)
645 return FALSE;
647 return TRUE;
651 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
653 DWORD outSize;
655 /* check uncompressed AVI */
656 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
657 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
658 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
660 infoPtr->hic = 0;
661 return TRUE;
664 /* try to get a decompressor for that type */
665 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
666 if (!infoPtr->hic) {
667 WARN("Can't load codec for the file\n");
668 return FALSE;
671 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
672 (DWORD_PTR)infoPtr->inbih, 0L);
674 infoPtr->outbih = Alloc(outSize);
675 if (!infoPtr->outbih)
676 return FALSE;
678 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
679 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
681 WARN("Can't get output BIH\n");
682 return FALSE;
685 infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
686 if (!infoPtr->outdata)
687 return FALSE;
689 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
690 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
691 WARN("Can't begin decompression\n");
692 return FALSE;
695 return TRUE;
699 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
701 HDC hdc;
703 ANIMATE_Free(infoPtr);
705 if (!lpszName)
707 TRACE("Closing avi.\n");
708 /* installer of thebat! v1.62 requires FALSE here */
709 return (infoPtr->hMMio != 0);
712 if (!hInstance)
713 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
715 TRACE("(%s)\n", debugstr_w(lpszName));
717 if (!IS_INTRESOURCE(lpszName))
719 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
721 TRACE("No AVI resource found.\n");
722 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
724 WARN("No AVI file found.\n");
725 return FALSE;
729 else
731 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
733 WARN("No AVI resource found.\n");
734 return FALSE;
738 if (!ANIMATE_GetAviInfo(infoPtr))
740 WARN("Can't get AVI information\n");
741 ANIMATE_Free(infoPtr);
742 return FALSE;
745 if (!ANIMATE_GetAviCodec(infoPtr))
747 WARN("Can't get AVI Codec\n");
748 ANIMATE_Free(infoPtr);
749 return FALSE;
752 hdc = GetDC(infoPtr->hwndSelf);
753 /* native looks at the top left pixel of the first frame here too. */
754 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
755 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
756 ReleaseDC(infoPtr->hwndSelf, hdc);
758 if (!(infoPtr->dwStyle & ACS_CENTER))
759 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
760 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
762 if (infoPtr->dwStyle & ACS_AUTOPLAY)
763 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
765 return TRUE;
769 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
771 LPWSTR lpwszName;
772 LRESULT result;
773 INT len;
775 if (IS_INTRESOURCE(lpszName))
776 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
778 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
779 lpwszName = Alloc(len * sizeof(WCHAR));
780 if (!lpwszName) return FALSE;
781 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
783 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
784 Free (lpwszName);
785 return result;
789 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
791 /* nothing opened */
792 if (!infoPtr->hMMio)
793 return FALSE;
795 ANIMATE_DoStop(infoPtr);
796 return TRUE;
800 static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
802 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
803 ANIMATE_INFO *infoPtr;
805 if (!fnIC.hModule)
807 fnIC.hModule = LoadLibraryW(msvfw32W);
808 if (!fnIC.hModule) return FALSE;
810 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
811 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
812 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
813 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
816 /* allocate memory for info structure */
817 infoPtr = Alloc(sizeof(ANIMATE_INFO));
818 if (!infoPtr) return FALSE;
820 /* store crossref hWnd <-> info structure */
821 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
822 infoPtr->hwndSelf = hWnd;
823 infoPtr->hwndNotify = lpcs->hwndParent;
824 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
825 infoPtr->hbmPrevFrame = 0;
826 infoPtr->dwStyle = lpcs->style;
828 TRACE("Animate style=0x%08x, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
830 InitializeCriticalSection(&infoPtr->cs);
831 infoPtr->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ANIMATE_INFO*->cs");
833 return TRUE;
837 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
839 /* free avi data */
840 ANIMATE_Free(infoPtr);
842 /* free animate info data */
843 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
845 infoPtr->cs.DebugInfo->Spare[0] = 0;
846 DeleteCriticalSection(&infoPtr->cs);
847 Free(infoPtr);
849 return 0;
853 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
855 RECT rect;
856 HBRUSH hBrush;
858 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
859 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
860 GetClientRect(infoPtr->hwndSelf, &rect);
861 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
863 return TRUE;
867 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
869 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
870 wStyleType, lpss->styleOld, lpss->styleNew);
872 if (wStyleType != GWL_STYLE) return 0;
874 infoPtr->dwStyle = lpss->styleNew;
876 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
877 return 0;
881 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
883 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
885 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hWnd, uMsg, wParam, lParam);
886 if (!infoPtr && (uMsg != WM_NCCREATE))
887 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
888 switch (uMsg)
890 case ACM_OPENA:
891 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
893 case ACM_OPENW:
894 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
896 case ACM_PLAY:
897 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
899 case ACM_STOP:
900 return ANIMATE_Stop(infoPtr);
902 case WM_CLOSE:
903 ANIMATE_Free(infoPtr);
904 return 0;
906 case WM_NCCREATE:
907 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
909 case WM_NCHITTEST:
910 return HTTRANSPARENT;
912 case WM_DESTROY:
913 return ANIMATE_Destroy(infoPtr);
915 case WM_ERASEBKGND:
916 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
918 case WM_STYLECHANGED:
919 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
921 case WM_TIMER:
922 return ANIMATE_Timer(infoPtr);
924 case WM_PRINTCLIENT:
925 case WM_PAINT:
927 /* the animation has not decompressed
928 * (and displayed) the first frame yet, don't paint
930 if (!infoPtr->hbmPrevFrame)
932 /* default paint handling */
933 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
936 if (wParam)
938 EnterCriticalSection(&infoPtr->cs);
939 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
940 LeaveCriticalSection(&infoPtr->cs);
942 else
944 PAINTSTRUCT ps;
945 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
947 EnterCriticalSection(&infoPtr->cs);
948 ANIMATE_PaintFrame(infoPtr, hDC);
949 LeaveCriticalSection(&infoPtr->cs);
951 EndPaint(infoPtr->hwndSelf, &ps);
954 break;
956 case WM_SIZE:
957 if (infoPtr->dwStyle & ACS_CENTER)
958 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
959 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
961 default:
962 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
963 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
965 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
967 return 0;
970 void ANIMATE_Register(void)
972 WNDCLASSW wndClass;
974 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
975 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
976 wndClass.lpfnWndProc = ANIMATE_WindowProc;
977 wndClass.cbClsExtra = 0;
978 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
979 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
980 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
981 wndClass.lpszClassName = ANIMATE_CLASSW;
983 RegisterClassW(&wndClass);
987 void ANIMATE_Unregister(void)
989 UnregisterClassW(ANIMATE_CLASSW, NULL);