advapi32: Fix the failing QueryServiceConfig2 test on platforms win2k3 and vista.
[wine/multimedia.git] / dlls / comctl32 / animate.c
blobee668aece53a6a96346e793c53a3535e2db37066
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 SendMessageW(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 = (LPSTR)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 rect.left = 0;
316 rect.top = 0;
317 rect.right = nWidth;
318 rect.bottom = nHeight;
320 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
321 WM_CTLCOLORSTATIC,
322 (WPARAM)hDC, (LPARAM)infoPtr->hwndSelf);
323 if(!infoPtr->hbrushBG)
324 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
326 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
327 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
329 SelectObject(hdcFinal, hbmOld2);
330 SelectObject(hdcMem, hbmFinal);
331 DeleteDC(hdcFinal);
332 DeleteObject(infoPtr->hbmPrevFrame);
333 infoPtr->hbmPrevFrame = hbmFinal;
336 if (infoPtr->dwStyle & ACS_CENTER)
338 RECT rect;
340 GetWindowRect(infoPtr->hwndSelf, &rect);
341 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
342 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
344 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
346 SelectObject(hdcMem, hbmOld);
347 DeleteDC(hdcMem);
348 return TRUE;
351 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
353 HDC hDC;
355 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
357 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
358 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
360 if (infoPtr->hic &&
361 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
362 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
363 WARN("Decompression error\n");
364 return FALSE;
367 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
368 ANIMATE_PaintFrame(infoPtr, hDC);
369 ReleaseDC(infoPtr->hwndSelf, hDC);
372 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
373 infoPtr->currFrame = infoPtr->nFromFrame;
374 if (infoPtr->nLoop != -1) {
375 if (--infoPtr->nLoop == 0) {
376 ANIMATE_DoStop(infoPtr);
381 return TRUE;
384 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
386 /* FIXME: we should pass the hDC instead of 0 to WM_CTLCOLORSTATIC */
387 if (infoPtr->dwStyle & ACS_TRANSPARENT)
388 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
389 WM_CTLCOLORSTATIC,
390 0, (LPARAM)infoPtr->hwndSelf);
391 EnterCriticalSection(&infoPtr->cs);
392 ANIMATE_DrawFrame(infoPtr);
393 LeaveCriticalSection(&infoPtr->cs);
395 return 0;
398 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
400 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)ptr_;
401 HANDLE event;
402 DWORD timeout;
404 while(1)
406 EnterCriticalSection(&infoPtr->cs);
407 ANIMATE_DrawFrame(infoPtr);
408 timeout = infoPtr->mah.dwMicroSecPerFrame;
409 event = infoPtr->hStopEvent;
410 LeaveCriticalSection(&infoPtr->cs);
412 /* time is in microseconds, we should convert it to milliseconds */
413 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
414 break;
416 return TRUE;
419 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
421 /* nothing opened */
422 if (!infoPtr->hMMio)
423 return FALSE;
425 if (infoPtr->hThread || infoPtr->uTimer) {
426 TRACE("Already playing\n");
427 return TRUE;
430 infoPtr->nFromFrame = wFrom;
431 infoPtr->nToFrame = wTo;
432 infoPtr->nLoop = cRepeat;
434 if (infoPtr->nToFrame == 0xFFFF)
435 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
437 TRACE("(repeat=%d from=%d to=%d);\n",
438 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
440 if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
441 (SHORT)infoPtr->nFromFrame < 0)
442 infoPtr->nFromFrame = 0;
444 if (infoPtr->nFromFrame > infoPtr->nToFrame ||
445 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
446 return FALSE;
448 infoPtr->currFrame = infoPtr->nFromFrame;
450 /* seek - doesn't need to start a thread or set a timer and neither
451 * does it send a notification */
452 if (infoPtr->nFromFrame == infoPtr->nToFrame)
454 ANIMATE_DrawFrame(infoPtr);
455 return TRUE;
458 if (infoPtr->dwStyle & ACS_TIMER)
460 TRACE("Using a timer\n");
461 /* create a timer to display AVI */
462 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
463 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
465 else
467 if(infoPtr->dwStyle & ACS_TRANSPARENT)
468 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
469 WM_CTLCOLORSTATIC, 0,
470 (LPARAM)infoPtr->hwndSelf);
472 TRACE("Using an animation thread\n");
473 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
474 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
475 (LPVOID)infoPtr, 0, &infoPtr->threadId);
476 if(!infoPtr->hThread) return FALSE;
480 ANIMATE_Notify(infoPtr, ACN_START);
482 return TRUE;
486 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
488 MMCKINFO ckMainRIFF;
489 MMCKINFO mmckHead;
490 MMCKINFO mmckList;
491 MMCKINFO mmckInfo;
492 DWORD numFrame;
493 DWORD insize;
495 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
496 WARN("Can't find 'RIFF' chunk\n");
497 return FALSE;
500 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
501 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
502 WARN("Can't find 'AVI ' chunk\n");
503 return FALSE;
506 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
507 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
508 WARN("Can't find 'hdrl' list\n");
509 return FALSE;
512 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
513 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
514 WARN("Can't find 'avih' chunk\n");
515 return FALSE;
518 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
520 TRACE("mah.dwMicroSecPerFrame=%d\n", infoPtr->mah.dwMicroSecPerFrame);
521 TRACE("mah.dwMaxBytesPerSec=%d\n", infoPtr->mah.dwMaxBytesPerSec);
522 TRACE("mah.dwPaddingGranularity=%d\n", infoPtr->mah.dwPaddingGranularity);
523 TRACE("mah.dwFlags=%d\n", infoPtr->mah.dwFlags);
524 TRACE("mah.dwTotalFrames=%d\n", infoPtr->mah.dwTotalFrames);
525 TRACE("mah.dwInitialFrames=%d\n", infoPtr->mah.dwInitialFrames);
526 TRACE("mah.dwStreams=%d\n", infoPtr->mah.dwStreams);
527 TRACE("mah.dwSuggestedBufferSize=%d\n", infoPtr->mah.dwSuggestedBufferSize);
528 TRACE("mah.dwWidth=%d\n", infoPtr->mah.dwWidth);
529 TRACE("mah.dwHeight=%d\n", infoPtr->mah.dwHeight);
531 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
533 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
534 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
535 WARN("Can't find 'strl' list\n");
536 return FALSE;
539 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
540 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
541 WARN("Can't find 'strh' chunk\n");
542 return FALSE;
545 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
547 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
548 HIBYTE(LOWORD(infoPtr->ash.fccType)),
549 LOBYTE(HIWORD(infoPtr->ash.fccType)),
550 HIBYTE(HIWORD(infoPtr->ash.fccType)));
551 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
552 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
553 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
554 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
555 TRACE("ash.dwFlags=%d\n", infoPtr->ash.dwFlags);
556 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
557 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
558 TRACE("ash.dwInitialFrames=%d\n", infoPtr->ash.dwInitialFrames);
559 TRACE("ash.dwScale=%d\n", infoPtr->ash.dwScale);
560 TRACE("ash.dwRate=%d\n", infoPtr->ash.dwRate);
561 TRACE("ash.dwStart=%d\n", infoPtr->ash.dwStart);
562 TRACE("ash.dwLength=%d\n", infoPtr->ash.dwLength);
563 TRACE("ash.dwSuggestedBufferSize=%d\n", infoPtr->ash.dwSuggestedBufferSize);
564 TRACE("ash.dwQuality=%d\n", infoPtr->ash.dwQuality);
565 TRACE("ash.dwSampleSize=%d\n", infoPtr->ash.dwSampleSize);
566 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
567 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
569 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
571 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
572 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
573 WARN("Can't find 'strh' chunk\n");
574 return FALSE;
577 infoPtr->inbih = Alloc(mmckInfo.cksize);
578 if (!infoPtr->inbih) {
579 WARN("Can't alloc input BIH\n");
580 return FALSE;
583 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
585 TRACE("bih.biSize=%d\n", infoPtr->inbih->biSize);
586 TRACE("bih.biWidth=%d\n", infoPtr->inbih->biWidth);
587 TRACE("bih.biHeight=%d\n", infoPtr->inbih->biHeight);
588 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
589 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
590 TRACE("bih.biCompression=%d\n", infoPtr->inbih->biCompression);
591 TRACE("bih.biSizeImage=%d\n", infoPtr->inbih->biSizeImage);
592 TRACE("bih.biXPelsPerMeter=%d\n", infoPtr->inbih->biXPelsPerMeter);
593 TRACE("bih.biYPelsPerMeter=%d\n", infoPtr->inbih->biYPelsPerMeter);
594 TRACE("bih.biClrUsed=%d\n", infoPtr->inbih->biClrUsed);
595 TRACE("bih.biClrImportant=%d\n", infoPtr->inbih->biClrImportant);
597 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
599 mmioAscend(infoPtr->hMMio, &mmckList, 0);
601 #if 0
602 /* an AVI has 0 or 1 video stream, and to be animated should not contain
603 * an audio stream, so only one strl is allowed
605 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
606 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
607 WARN("There should be a single 'strl' list\n");
608 return FALSE;
610 #endif
612 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
614 /* no need to read optional JUNK chunk */
616 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
617 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
618 WARN("Can't find 'movi' list\n");
619 return FALSE;
622 /* FIXME: should handle the 'rec ' LIST when present */
624 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
625 if (!infoPtr->lpIndex)
626 return FALSE;
628 numFrame = insize = 0;
629 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
630 numFrame < infoPtr->mah.dwTotalFrames) {
631 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
632 if (insize < mmckInfo.cksize)
633 insize = mmckInfo.cksize;
634 numFrame++;
635 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
637 if (numFrame != infoPtr->mah.dwTotalFrames) {
638 WARN("Found %d frames (/%d)\n", numFrame, infoPtr->mah.dwTotalFrames);
639 return FALSE;
641 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
642 WARN("insize=%d suggestedSize=%d\n", insize, infoPtr->ash.dwSuggestedBufferSize);
643 infoPtr->ash.dwSuggestedBufferSize = insize;
646 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
647 if (!infoPtr->indata)
648 return FALSE;
650 return TRUE;
654 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
656 DWORD outSize;
658 /* check uncompressed AVI */
659 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
660 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
661 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
663 infoPtr->hic = 0;
664 return TRUE;
667 /* try to get a decompressor for that type */
668 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
669 if (!infoPtr->hic) {
670 WARN("Can't load codec for the file\n");
671 return FALSE;
674 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
675 (DWORD_PTR)infoPtr->inbih, 0L);
677 infoPtr->outbih = Alloc(outSize);
678 if (!infoPtr->outbih)
679 return FALSE;
681 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
682 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
684 WARN("Can't get output BIH\n");
685 return FALSE;
688 infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
689 if (!infoPtr->outdata)
690 return FALSE;
692 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
693 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
694 WARN("Can't begin decompression\n");
695 return FALSE;
698 return TRUE;
702 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
704 ANIMATE_Free(infoPtr);
706 if (!lpszName)
708 TRACE("Closing avi!\n");
709 /* installer of thebat! v1.62 requires FALSE here */
710 return (infoPtr->hMMio != 0);
713 if (!hInstance)
714 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
716 TRACE("(%s)\n", debugstr_w(lpszName));
718 if (HIWORD(lpszName))
720 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
722 TRACE("No AVI resource found!\n");
723 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
725 WARN("No AVI file found!\n");
726 return FALSE;
730 else
732 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
734 WARN("No AVI resource found!\n");
735 return FALSE;
739 if (!ANIMATE_GetAviInfo(infoPtr))
741 WARN("Can't get AVI information\n");
742 ANIMATE_Free(infoPtr);
743 return FALSE;
746 if (!ANIMATE_GetAviCodec(infoPtr))
748 WARN("Can't get AVI Codec\n");
749 ANIMATE_Free(infoPtr);
750 return FALSE;
753 if (!(infoPtr->dwStyle & ACS_CENTER))
754 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
755 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
757 if (infoPtr->dwStyle & ACS_AUTOPLAY)
758 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
760 return TRUE;
764 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
766 LPWSTR lpwszName;
767 LRESULT result;
768 INT len;
770 if (!HIWORD(lpszName))
771 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
773 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
774 lpwszName = Alloc(len * sizeof(WCHAR));
775 if (!lpwszName) return FALSE;
776 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
778 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
779 Free (lpwszName);
780 return result;
784 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
786 /* nothing opened */
787 if (!infoPtr->hMMio)
788 return FALSE;
790 ANIMATE_DoStop(infoPtr);
791 return TRUE;
795 static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
797 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
798 ANIMATE_INFO *infoPtr;
800 if (!fnIC.hModule)
802 fnIC.hModule = LoadLibraryW(msvfw32W);
803 if (!fnIC.hModule) return FALSE;
805 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
806 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
807 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
808 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
811 /* allocate memory for info structure */
812 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
813 if (!infoPtr) return FALSE;
815 /* store crossref hWnd <-> info structure */
816 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
817 infoPtr->hwndSelf = hWnd;
818 infoPtr->hwndNotify = lpcs->hwndParent;
819 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
820 infoPtr->hbmPrevFrame = 0;
821 infoPtr->dwStyle = lpcs->style;
823 TRACE("Animate style=0x%08x, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
825 InitializeCriticalSection(&infoPtr->cs);
826 infoPtr->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ANIMATE_INFO*->cs");
828 return TRUE;
832 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
834 /* free avi data */
835 ANIMATE_Free(infoPtr);
837 /* free animate info data */
838 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
840 infoPtr->cs.DebugInfo->Spare[0] = 0;
841 DeleteCriticalSection(&infoPtr->cs);
842 Free(infoPtr);
844 return 0;
848 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
850 RECT rect;
851 HBRUSH hBrush = 0;
853 if(infoPtr->dwStyle & ACS_TRANSPARENT)
855 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
856 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
859 GetClientRect(infoPtr->hwndSelf, &rect);
860 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
862 return TRUE;
866 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
868 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
869 wStyleType, lpss->styleOld, lpss->styleNew);
871 if (wStyleType != GWL_STYLE) return 0;
873 infoPtr->dwStyle = lpss->styleNew;
875 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
876 return 0;
880 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
882 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
884 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hWnd, uMsg, wParam, lParam);
885 if (!infoPtr && (uMsg != WM_NCCREATE))
886 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
887 switch (uMsg)
889 case ACM_OPENA:
890 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
892 case ACM_OPENW:
893 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
895 case ACM_PLAY:
896 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
898 case ACM_STOP:
899 return ANIMATE_Stop(infoPtr);
901 case WM_CLOSE:
902 ANIMATE_Free(infoPtr);
903 return 0;
905 case WM_NCCREATE:
906 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
908 case WM_NCHITTEST:
909 return HTTRANSPARENT;
911 case WM_DESTROY:
912 return ANIMATE_Destroy(infoPtr);
914 case WM_ERASEBKGND:
915 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
917 case WM_STYLECHANGED:
918 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
920 case WM_TIMER:
921 return ANIMATE_Timer(infoPtr);
923 case WM_PRINTCLIENT:
924 case WM_PAINT:
926 /* the animation has not decompressed
927 * (and displayed) the first frame yet, don't paint
929 if (!infoPtr->hbmPrevFrame)
931 /* default paint handling */
932 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
935 if (wParam)
937 EnterCriticalSection(&infoPtr->cs);
938 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
939 LeaveCriticalSection(&infoPtr->cs);
941 else
943 PAINTSTRUCT ps;
944 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
946 EnterCriticalSection(&infoPtr->cs);
947 ANIMATE_PaintFrame(infoPtr, hDC);
948 LeaveCriticalSection(&infoPtr->cs);
950 EndPaint(infoPtr->hwndSelf, &ps);
953 break;
955 case WM_SIZE:
956 if (infoPtr->dwStyle & ACS_CENTER)
957 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
958 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
960 default:
961 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
962 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
964 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
966 return 0;
969 void ANIMATE_Register(void)
971 WNDCLASSW wndClass;
973 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
974 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
975 wndClass.lpfnWndProc = ANIMATE_WindowProc;
976 wndClass.cbClsExtra = 0;
977 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
978 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
979 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
980 wndClass.lpszClassName = ANIMATE_CLASSW;
982 RegisterClassW(&wndClass);
986 void ANIMATE_Unregister(void)
988 UnregisterClassW(ANIMATE_CLASSW, NULL);