save old text color during a call of DrawCaptionTempW
[wine/kumbayo.git] / dlls / comctl32 / animate.c
blob958bf69a4e0e9c1086e85a92cfb3a557e7bd4782
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 /* tranparency 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 EnterCriticalSection(&infoPtr->cs);
150 /* should stop playing */
151 if (infoPtr->hThread)
153 HANDLE handle = infoPtr->hThread;
155 TRACE("stopping animation thread\n");
156 infoPtr->hThread = 0;
157 SetEvent( infoPtr->hStopEvent );
159 if (infoPtr->threadId != GetCurrentThreadId())
161 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
162 WaitForSingleObject( handle, INFINITE );
163 TRACE("animation thread stopped\n");
164 EnterCriticalSection(&infoPtr->cs);
167 CloseHandle( handle );
168 CloseHandle( infoPtr->hStopEvent );
169 infoPtr->hStopEvent = 0;
171 if (infoPtr->uTimer) {
172 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
173 infoPtr->uTimer = 0;
176 LeaveCriticalSection(&infoPtr->cs);
178 ANIMATE_Notify(infoPtr, ACN_STOP);
180 return TRUE;
184 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
186 if (infoPtr->hMMio) {
187 ANIMATE_DoStop(infoPtr);
188 mmioClose(infoPtr->hMMio, 0);
189 if (infoPtr->hRes) {
190 FreeResource(infoPtr->hRes);
191 infoPtr->hRes = 0;
193 Free (infoPtr->lpIndex);
194 infoPtr->lpIndex = NULL;
195 if (infoPtr->hic) {
196 fnIC.fnICClose(infoPtr->hic);
197 infoPtr->hic = 0;
199 Free (infoPtr->inbih);
200 infoPtr->inbih = NULL;
201 Free (infoPtr->outbih);
202 infoPtr->outbih = NULL;
203 Free (infoPtr->indata);
204 infoPtr->indata = NULL;
205 Free (infoPtr->outdata);
206 infoPtr->outdata = NULL;
207 if( infoPtr->hbmPrevFrame )
209 DeleteObject(infoPtr->hbmPrevFrame);
210 infoPtr->hbmPrevFrame = 0;
213 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
214 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
215 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
217 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
220 static void ANIMATE_TransparentBlt(ANIMATE_INFO const *infoPtr, HDC hdcDest, HDC hdcSource)
222 HDC hdcMask;
223 HBITMAP hbmMask;
224 HBITMAP hbmOld;
226 /* create a transparency mask */
227 hdcMask = CreateCompatibleDC(hdcDest);
228 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
229 hbmOld = SelectObject(hdcMask, hbmMask);
231 SetBkColor(hdcSource,infoPtr->transparentColor);
232 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
234 /* mask the source bitmap */
235 SetBkColor(hdcSource, RGB(0,0,0));
236 SetTextColor(hdcSource, RGB(255,255,255));
237 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
239 /* mask the destination bitmap */
240 SetBkColor(hdcDest, RGB(255,255,255));
241 SetTextColor(hdcDest, RGB(0,0,0));
242 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
244 /* combine source and destination */
245 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
247 SelectObject(hdcMask, hbmOld);
248 DeleteObject(hbmMask);
249 DeleteDC(hdcMask);
252 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
254 void const *pBitmapData;
255 BITMAPINFO const *pBitmapInfo;
256 HDC hdcMem;
257 HBITMAP hbmOld;
258 int nOffsetX = 0;
259 int nOffsetY = 0;
260 int nWidth;
261 int nHeight;
263 if (!hDC || !infoPtr->inbih)
264 return TRUE;
266 if (infoPtr->hic )
268 pBitmapData = infoPtr->outdata;
269 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
271 nWidth = infoPtr->outbih->biWidth;
272 nHeight = infoPtr->outbih->biHeight;
274 else
276 pBitmapData = infoPtr->indata;
277 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
279 nWidth = infoPtr->inbih->biWidth;
280 nHeight = infoPtr->inbih->biHeight;
283 if(!infoPtr->hbmPrevFrame)
285 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
288 hdcMem = CreateCompatibleDC(hDC);
289 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
291 SetDIBits(hdcMem, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, pBitmapInfo, DIB_RGB_COLORS);
294 * we need to get the transparent color even without ACS_TRANSPARENT,
295 * because the style can be changed later on and the color should always
296 * be obtained in the first frame
298 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
300 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
303 if(infoPtr->dwStyle & ACS_TRANSPARENT)
305 HDC hdcFinal = CreateCompatibleDC(hDC);
306 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
307 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
308 RECT rect;
310 rect.left = 0;
311 rect.top = 0;
312 rect.right = nWidth;
313 rect.bottom = nHeight;
315 if(!infoPtr->hbrushBG)
316 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
318 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
319 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
321 SelectObject(hdcFinal, hbmOld2);
322 SelectObject(hdcMem, hbmFinal);
323 DeleteDC(hdcFinal);
324 DeleteObject(infoPtr->hbmPrevFrame);
325 infoPtr->hbmPrevFrame = hbmFinal;
328 if (infoPtr->dwStyle & ACS_CENTER)
330 RECT rect;
332 GetWindowRect(infoPtr->hwndSelf, &rect);
333 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
334 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
336 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
338 SelectObject(hdcMem, hbmOld);
339 DeleteDC(hdcMem);
340 return TRUE;
343 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
345 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 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
360 ANIMATE_PaintFrame(infoPtr, hDC);
361 ReleaseDC(infoPtr->hwndSelf, hDC);
364 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
365 infoPtr->currFrame = infoPtr->nFromFrame;
366 if (infoPtr->nLoop != -1) {
367 if (--infoPtr->nLoop == 0) {
368 ANIMATE_DoStop(infoPtr);
373 return TRUE;
376 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
378 /* FIXME: we should pass the hDC instead of 0 to WM_CTLCOLORSTATIC */
379 if (infoPtr->dwStyle & ACS_TRANSPARENT)
380 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
381 WM_CTLCOLORSTATIC,
382 0, (LPARAM)infoPtr->hwndSelf);
383 EnterCriticalSection(&infoPtr->cs);
384 ANIMATE_DrawFrame(infoPtr);
385 LeaveCriticalSection(&infoPtr->cs);
387 return 0;
390 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
392 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)ptr_;
393 HANDLE event;
394 DWORD timeout;
396 RENAME_CURRENT_THREAD("comctl32 animation thread");
398 while(1)
400 EnterCriticalSection(&infoPtr->cs);
401 ANIMATE_DrawFrame(infoPtr);
402 timeout = infoPtr->mah.dwMicroSecPerFrame;
403 event = infoPtr->hStopEvent;
404 LeaveCriticalSection(&infoPtr->cs);
406 /* time is in microseconds, we should convert it to milliseconds */
407 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
408 break;
410 return TRUE;
413 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
415 /* nothing opened */
416 if (!infoPtr->hMMio)
417 return FALSE;
419 if (infoPtr->hThread || infoPtr->uTimer) {
420 TRACE("Already playing\n");
421 return TRUE;
424 infoPtr->nFromFrame = wFrom;
425 infoPtr->nToFrame = wTo;
426 infoPtr->nLoop = cRepeat;
428 if (infoPtr->nToFrame == 0xFFFF)
429 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
431 TRACE("(repeat=%d from=%d to=%d);\n",
432 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
434 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
435 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
436 return FALSE;
438 infoPtr->currFrame = infoPtr->nFromFrame;
440 if (infoPtr->dwStyle & ACS_TIMER)
442 TRACE("Using a timer\n");
443 /* create a timer to display AVI */
444 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
445 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
447 else
449 if(infoPtr->dwStyle & ACS_TRANSPARENT)
450 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
451 WM_CTLCOLORSTATIC, 0,
452 (LPARAM)infoPtr->hwndSelf);
454 TRACE("Using an animation thread\n");
455 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
456 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
457 (LPVOID)infoPtr, 0, &infoPtr->threadId);
458 if(!infoPtr->hThread) return FALSE;
462 ANIMATE_Notify(infoPtr, ACN_START);
464 return TRUE;
468 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
470 MMCKINFO ckMainRIFF;
471 MMCKINFO mmckHead;
472 MMCKINFO mmckList;
473 MMCKINFO mmckInfo;
474 DWORD numFrame;
475 DWORD insize;
477 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
478 WARN("Can't find 'RIFF' chunk\n");
479 return FALSE;
482 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
483 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
484 WARN("Can't find 'AVI ' chunk\n");
485 return FALSE;
488 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
489 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
490 WARN("Can't find 'hdrl' list\n");
491 return FALSE;
494 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
495 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
496 WARN("Can't find 'avih' chunk\n");
497 return FALSE;
500 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
502 TRACE("mah.dwMicroSecPerFrame=%d\n", infoPtr->mah.dwMicroSecPerFrame);
503 TRACE("mah.dwMaxBytesPerSec=%d\n", infoPtr->mah.dwMaxBytesPerSec);
504 TRACE("mah.dwPaddingGranularity=%d\n", infoPtr->mah.dwPaddingGranularity);
505 TRACE("mah.dwFlags=%d\n", infoPtr->mah.dwFlags);
506 TRACE("mah.dwTotalFrames=%d\n", infoPtr->mah.dwTotalFrames);
507 TRACE("mah.dwInitialFrames=%d\n", infoPtr->mah.dwInitialFrames);
508 TRACE("mah.dwStreams=%d\n", infoPtr->mah.dwStreams);
509 TRACE("mah.dwSuggestedBufferSize=%d\n", infoPtr->mah.dwSuggestedBufferSize);
510 TRACE("mah.dwWidth=%d\n", infoPtr->mah.dwWidth);
511 TRACE("mah.dwHeight=%d\n", infoPtr->mah.dwHeight);
513 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
515 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
516 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
517 WARN("Can't find 'strl' list\n");
518 return FALSE;
521 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
522 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
523 WARN("Can't find 'strh' chunk\n");
524 return FALSE;
527 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
529 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
530 HIBYTE(LOWORD(infoPtr->ash.fccType)),
531 LOBYTE(HIWORD(infoPtr->ash.fccType)),
532 HIBYTE(HIWORD(infoPtr->ash.fccType)));
533 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
534 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
535 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
536 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
537 TRACE("ash.dwFlags=%d\n", infoPtr->ash.dwFlags);
538 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
539 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
540 TRACE("ash.dwInitialFrames=%d\n", infoPtr->ash.dwInitialFrames);
541 TRACE("ash.dwScale=%d\n", infoPtr->ash.dwScale);
542 TRACE("ash.dwRate=%d\n", infoPtr->ash.dwRate);
543 TRACE("ash.dwStart=%d\n", infoPtr->ash.dwStart);
544 TRACE("ash.dwLength=%d\n", infoPtr->ash.dwLength);
545 TRACE("ash.dwSuggestedBufferSize=%d\n", infoPtr->ash.dwSuggestedBufferSize);
546 TRACE("ash.dwQuality=%d\n", infoPtr->ash.dwQuality);
547 TRACE("ash.dwSampleSize=%d\n", infoPtr->ash.dwSampleSize);
548 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
549 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
551 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
553 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
554 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
555 WARN("Can't find 'strh' chunk\n");
556 return FALSE;
559 infoPtr->inbih = Alloc(mmckInfo.cksize);
560 if (!infoPtr->inbih) {
561 WARN("Can't alloc input BIH\n");
562 return FALSE;
565 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
567 TRACE("bih.biSize=%d\n", infoPtr->inbih->biSize);
568 TRACE("bih.biWidth=%d\n", infoPtr->inbih->biWidth);
569 TRACE("bih.biHeight=%d\n", infoPtr->inbih->biHeight);
570 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
571 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
572 TRACE("bih.biCompression=%d\n", infoPtr->inbih->biCompression);
573 TRACE("bih.biSizeImage=%d\n", infoPtr->inbih->biSizeImage);
574 TRACE("bih.biXPelsPerMeter=%d\n", infoPtr->inbih->biXPelsPerMeter);
575 TRACE("bih.biYPelsPerMeter=%d\n", infoPtr->inbih->biYPelsPerMeter);
576 TRACE("bih.biClrUsed=%d\n", infoPtr->inbih->biClrUsed);
577 TRACE("bih.biClrImportant=%d\n", infoPtr->inbih->biClrImportant);
579 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
581 mmioAscend(infoPtr->hMMio, &mmckList, 0);
583 #if 0
584 /* an AVI has 0 or 1 video stream, and to be animated should not contain
585 * an audio stream, so only one strl is allowed
587 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
588 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
589 WARN("There should be a single 'strl' list\n");
590 return FALSE;
592 #endif
594 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
596 /* no need to read optional JUNK chunk */
598 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
599 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
600 WARN("Can't find 'movi' list\n");
601 return FALSE;
604 /* FIXME: should handle the 'rec ' LIST when present */
606 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
607 if (!infoPtr->lpIndex)
608 return FALSE;
610 numFrame = insize = 0;
611 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
612 numFrame < infoPtr->mah.dwTotalFrames) {
613 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
614 if (insize < mmckInfo.cksize)
615 insize = mmckInfo.cksize;
616 numFrame++;
617 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
619 if (numFrame != infoPtr->mah.dwTotalFrames) {
620 WARN("Found %d frames (/%d)\n", numFrame, infoPtr->mah.dwTotalFrames);
621 return FALSE;
623 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
624 WARN("insize=%d suggestedSize=%d\n", insize, infoPtr->ash.dwSuggestedBufferSize);
625 infoPtr->ash.dwSuggestedBufferSize = insize;
628 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
629 if (!infoPtr->indata)
630 return FALSE;
632 return TRUE;
636 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
638 DWORD outSize;
640 /* check uncompressed AVI */
641 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
642 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
643 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
645 infoPtr->hic = 0;
646 return TRUE;
649 /* try to get a decompressor for that type */
650 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
651 if (!infoPtr->hic) {
652 WARN("Can't load codec for the file\n");
653 return FALSE;
656 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
657 (DWORD_PTR)infoPtr->inbih, 0L);
659 infoPtr->outbih = Alloc(outSize);
660 if (!infoPtr->outbih)
661 return FALSE;
663 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
664 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
666 WARN("Can't get output BIH\n");
667 return FALSE;
670 infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
671 if (!infoPtr->outdata)
672 return FALSE;
674 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
675 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
676 WARN("Can't begin decompression\n");
677 return FALSE;
680 return TRUE;
684 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
686 ANIMATE_Free(infoPtr);
688 if (!lpszName)
690 TRACE("Closing avi!\n");
691 /* installer of thebat! v1.62 requires FALSE here */
692 return (infoPtr->hMMio != 0);
695 if (!hInstance)
696 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
698 TRACE("(%s)\n", debugstr_w(lpszName));
700 if (HIWORD(lpszName))
702 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
704 TRACE("No AVI resource found!\n");
705 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
707 WARN("No AVI file found!\n");
708 return FALSE;
712 else
714 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
716 WARN("No AVI resource found!\n");
717 return FALSE;
721 if (!ANIMATE_GetAviInfo(infoPtr))
723 WARN("Can't get AVI information\n");
724 ANIMATE_Free(infoPtr);
725 return FALSE;
728 if (!ANIMATE_GetAviCodec(infoPtr))
730 WARN("Can't get AVI Codec\n");
731 ANIMATE_Free(infoPtr);
732 return FALSE;
735 if (!(infoPtr->dwStyle & ACS_CENTER))
736 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
737 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
739 if (infoPtr->dwStyle & ACS_AUTOPLAY)
740 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
742 return TRUE;
746 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
748 LPWSTR lpwszName;
749 LRESULT result;
750 INT len;
752 if (!HIWORD(lpszName))
753 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
755 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
756 lpwszName = Alloc(len * sizeof(WCHAR));
757 if (!lpwszName) return FALSE;
758 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
760 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
761 Free (lpwszName);
762 return result;
766 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
768 /* nothing opened */
769 if (!infoPtr->hMMio)
770 return FALSE;
772 ANIMATE_DoStop(infoPtr);
773 return TRUE;
777 static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
779 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
780 ANIMATE_INFO *infoPtr;
782 if (!fnIC.hModule)
784 fnIC.hModule = LoadLibraryW(msvfw32W);
785 if (!fnIC.hModule) return FALSE;
787 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
788 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
789 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
790 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
793 /* allocate memory for info structure */
794 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
795 if (!infoPtr) return FALSE;
797 /* store crossref hWnd <-> info structure */
798 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
799 infoPtr->hwndSelf = hWnd;
800 infoPtr->hwndNotify = lpcs->hwndParent;
801 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
802 infoPtr->hbmPrevFrame = 0;
803 infoPtr->dwStyle = lpcs->style;
805 TRACE("Animate style=0x%08x, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
807 InitializeCriticalSection(&infoPtr->cs);
808 infoPtr->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ANIMATE_INFO*->cs");
810 return TRUE;
814 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
816 /* free avi data */
817 ANIMATE_Free(infoPtr);
819 /* free animate info data */
820 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
822 infoPtr->cs.DebugInfo->Spare[0] = 0;
823 DeleteCriticalSection(&infoPtr->cs);
824 Free(infoPtr);
826 return 0;
830 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
832 RECT rect;
833 HBRUSH hBrush = 0;
835 if(infoPtr->dwStyle & ACS_TRANSPARENT)
837 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
838 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
841 GetClientRect(infoPtr->hwndSelf, &rect);
842 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
844 return TRUE;
848 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
850 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
851 wStyleType, lpss->styleOld, lpss->styleNew);
853 if (wStyleType != GWL_STYLE) return 0;
855 infoPtr->dwStyle = lpss->styleNew;
857 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
858 return 0;
862 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
864 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
866 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hWnd, uMsg, wParam, lParam);
867 if (!infoPtr && (uMsg != WM_NCCREATE))
868 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
869 switch (uMsg)
871 case ACM_OPENA:
872 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
874 case ACM_OPENW:
875 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
877 case ACM_PLAY:
878 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
880 case ACM_STOP:
881 return ANIMATE_Stop(infoPtr);
883 case WM_CLOSE:
884 ANIMATE_Free(infoPtr);
885 return 0;
887 case WM_NCCREATE:
888 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
890 case WM_NCHITTEST:
891 return HTTRANSPARENT;
893 case WM_DESTROY:
894 return ANIMATE_Destroy(infoPtr);
896 case WM_ERASEBKGND:
897 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
899 case WM_STYLECHANGED:
900 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
902 case WM_TIMER:
903 return ANIMATE_Timer(infoPtr);
905 case WM_PRINTCLIENT:
906 case WM_PAINT:
908 /* the animation isn't playing, or has not decompressed
909 * (and displayed) the first frame yet, don't paint
911 if ((!infoPtr->uTimer && !infoPtr->hThread) ||
912 !infoPtr->hbmPrevFrame)
914 /* default paint handling */
915 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
918 if (infoPtr->dwStyle & ACS_TRANSPARENT)
919 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
920 WM_CTLCOLORSTATIC,
921 wParam, (LPARAM)infoPtr->hwndSelf);
923 if (wParam)
925 EnterCriticalSection(&infoPtr->cs);
926 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
927 LeaveCriticalSection(&infoPtr->cs);
929 else
931 PAINTSTRUCT ps;
932 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
934 EnterCriticalSection(&infoPtr->cs);
935 ANIMATE_PaintFrame(infoPtr, hDC);
936 LeaveCriticalSection(&infoPtr->cs);
938 EndPaint(infoPtr->hwndSelf, &ps);
941 break;
943 case WM_SIZE:
944 if (infoPtr->dwStyle & ACS_CENTER)
945 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
946 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
948 default:
949 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
950 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
952 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
954 return 0;
957 void ANIMATE_Register(void)
959 WNDCLASSW wndClass;
961 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
962 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
963 wndClass.lpfnWndProc = ANIMATE_WindowProc;
964 wndClass.cbClsExtra = 0;
965 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
966 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
967 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
968 wndClass.lpszClassName = ANIMATE_CLASSW;
970 RegisterClassW(&wndClass);
974 void ANIMATE_Unregister(void)
976 UnregisterClassW(ANIMATE_CLASSW, NULL);