win32u: Move NtUserTranslateMessage implementation from user32.
[wine.git] / dlls / comctl32 / animate.c
blobfff963a6baaa416673b6aecdf6135927657b7e45
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 * TODO:
24 * - check for the 'rec ' list in some AVI files
27 #include <stdarg.h>
28 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "winnls.h"
34 #include "commctrl.h"
35 #include "vfw.h"
36 #include "mmsystem.h"
37 #include "comctl32.h"
38 #include "wine/debug.h"
39 #include "wine/heap.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(animate);
43 static struct {
44 HMODULE hModule;
45 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
46 LRESULT (WINAPI *fnICClose)(HIC);
47 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD_PTR, DWORD_PTR);
48 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
49 } fnIC;
51 typedef struct
53 /* reference to input stream (file or resource) */
54 HGLOBAL hRes;
55 HMMIO hMMio; /* handle to mmio stream */
56 HWND hwndSelf;
57 HWND hwndNotify;
58 DWORD dwStyle;
59 /* information on the loaded AVI file */
60 MainAVIHeader mah;
61 AVIStreamHeader ash;
62 LPBITMAPINFOHEADER inbih;
63 LPDWORD lpIndex;
64 /* data for the decompressor */
65 HIC hic;
66 LPBITMAPINFOHEADER outbih;
67 LPVOID indata;
68 LPVOID outdata;
69 /* data for the background mechanism */
70 CRITICAL_SECTION cs;
71 HANDLE hStopEvent;
72 HANDLE hThread;
73 DWORD threadId;
74 UINT uTimer;
75 /* data for playing the file */
76 int nFromFrame;
77 int nToFrame;
78 int nLoop;
79 int currFrame;
80 /* transparency info*/
81 COLORREF transparentColor;
82 HBRUSH hbrushBG;
83 HBITMAP hbmPrevFrame;
84 } ANIMATE_INFO;
86 #define ANIMATE_COLOR_NONE 0xffffffff
88 static void ANIMATE_Notify(const ANIMATE_INFO *infoPtr, UINT notif)
90 PostMessageW(infoPtr->hwndNotify, WM_COMMAND,
91 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
92 (LPARAM)infoPtr->hwndSelf);
95 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPCWSTR lpName)
97 HRSRC hrsrc;
98 MMIOINFO mminfo;
99 LPVOID lpAvi;
101 hrsrc = FindResourceW(hInst, lpName, L"AVI");
102 if (!hrsrc)
103 return FALSE;
105 infoPtr->hRes = LoadResource(hInst, hrsrc);
106 if (!infoPtr->hRes)
107 return FALSE;
109 lpAvi = LockResource(infoPtr->hRes);
110 if (!lpAvi)
111 return FALSE;
113 memset(&mminfo, 0, sizeof(mminfo));
114 mminfo.fccIOProc = FOURCC_MEM;
115 mminfo.pchBuffer = lpAvi;
116 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
117 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
118 if (!infoPtr->hMMio)
120 FreeResource(infoPtr->hRes);
121 return FALSE;
124 return TRUE;
128 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
130 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
132 if(!infoPtr->hMMio) return FALSE;
133 return TRUE;
137 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
139 BOOL stopped = FALSE;
141 EnterCriticalSection(&infoPtr->cs);
143 /* should stop playing */
144 if (infoPtr->hThread)
146 HANDLE handle = infoPtr->hThread;
148 TRACE("stopping animation thread\n");
149 infoPtr->hThread = 0;
150 SetEvent( infoPtr->hStopEvent );
152 if (infoPtr->threadId != GetCurrentThreadId())
154 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
155 WaitForSingleObject( handle, INFINITE );
156 TRACE("animation thread stopped\n");
157 EnterCriticalSection(&infoPtr->cs);
160 CloseHandle( handle );
161 CloseHandle( infoPtr->hStopEvent );
162 infoPtr->hStopEvent = 0;
163 stopped = TRUE;
165 if (infoPtr->uTimer) {
166 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
167 infoPtr->uTimer = 0;
168 stopped = TRUE;
171 LeaveCriticalSection(&infoPtr->cs);
173 if (stopped)
174 ANIMATE_Notify(infoPtr, ACN_STOP);
176 return TRUE;
180 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
182 if (infoPtr->hMMio)
184 ANIMATE_DoStop(infoPtr);
185 mmioClose(infoPtr->hMMio, 0);
186 if (infoPtr->hRes)
188 FreeResource(infoPtr->hRes);
189 infoPtr->hRes = 0;
191 heap_free (infoPtr->lpIndex);
192 infoPtr->lpIndex = NULL;
193 if (infoPtr->hic)
195 fnIC.fnICClose(infoPtr->hic);
196 infoPtr->hic = 0;
198 heap_free (infoPtr->inbih);
199 infoPtr->inbih = NULL;
200 heap_free (infoPtr->outbih);
201 infoPtr->outbih = NULL;
202 heap_free (infoPtr->indata);
203 infoPtr->indata = NULL;
204 heap_free (infoPtr->outdata);
205 infoPtr->outdata = NULL;
206 if (infoPtr->hbmPrevFrame)
208 DeleteObject(infoPtr->hbmPrevFrame);
209 infoPtr->hbmPrevFrame = 0;
212 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
213 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
214 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
216 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
219 static void ANIMATE_TransparentBlt(ANIMATE_INFO const *infoPtr, HDC hdcDest, HDC hdcSource)
221 HDC hdcMask;
222 HBITMAP hbmMask;
223 HBITMAP hbmOld;
225 /* create a transparency mask */
226 hdcMask = CreateCompatibleDC(hdcDest);
227 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
228 hbmOld = SelectObject(hdcMask, hbmMask);
230 SetBkColor(hdcSource,infoPtr->transparentColor);
231 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
233 /* mask the source bitmap */
234 SetBkColor(hdcSource, RGB(0,0,0));
235 SetTextColor(hdcSource, RGB(255,255,255));
236 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
238 /* mask the destination bitmap */
239 SetBkColor(hdcDest, RGB(255,255,255));
240 SetTextColor(hdcDest, RGB(0,0,0));
241 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
243 /* combine source and destination */
244 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
246 SelectObject(hdcMask, hbmOld);
247 DeleteObject(hbmMask);
248 DeleteDC(hdcMask);
251 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
253 void const *pBitmapData;
254 BITMAPINFO const *pBitmapInfo;
255 HDC hdcMem;
256 HBITMAP hbmOld;
257 int nOffsetX = 0;
258 int nOffsetY = 0;
259 int nWidth;
260 int nHeight;
262 if (!hDC || !infoPtr->inbih)
263 return TRUE;
265 if (infoPtr->hic )
267 pBitmapData = infoPtr->outdata;
268 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
270 nWidth = infoPtr->outbih->biWidth;
271 nHeight = infoPtr->outbih->biHeight;
273 else
275 pBitmapData = infoPtr->indata;
276 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
278 nWidth = infoPtr->inbih->biWidth;
279 nHeight = infoPtr->inbih->biHeight;
282 if(!infoPtr->hbmPrevFrame)
284 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
287 hdcMem = CreateCompatibleDC(hDC);
288 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
290 SetDIBits(hdcMem, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, pBitmapInfo, DIB_RGB_COLORS);
293 * we need to get the transparent color even without ACS_TRANSPARENT,
294 * because the style can be changed later on and the color should always
295 * be obtained in the first frame
297 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
299 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
302 if(infoPtr->dwStyle & ACS_TRANSPARENT)
304 HDC hdcFinal = CreateCompatibleDC(hDC);
305 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
306 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
307 RECT rect;
309 SetRect(&rect, 0, 0, nWidth, nHeight);
311 if(!infoPtr->hbrushBG)
312 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
314 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
315 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
317 SelectObject(hdcFinal, hbmOld2);
318 SelectObject(hdcMem, hbmFinal);
319 DeleteDC(hdcFinal);
320 DeleteObject(infoPtr->hbmPrevFrame);
321 infoPtr->hbmPrevFrame = hbmFinal;
324 if (infoPtr->dwStyle & ACS_CENTER)
326 RECT rect;
328 GetWindowRect(infoPtr->hwndSelf, &rect);
329 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
330 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
332 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
334 SelectObject(hdcMem, hbmOld);
335 DeleteDC(hdcMem);
336 return TRUE;
339 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr, HDC hDC)
341 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
343 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
344 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
346 if (infoPtr->hic &&
347 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
348 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
349 WARN("Decompression error\n");
350 return FALSE;
353 ANIMATE_PaintFrame(infoPtr, hDC);
355 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
356 infoPtr->currFrame = infoPtr->nFromFrame;
357 if (infoPtr->nLoop != -1) {
358 if (--infoPtr->nLoop == 0) {
359 ANIMATE_DoStop(infoPtr);
364 return TRUE;
367 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
369 HDC hDC;
371 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
373 EnterCriticalSection(&infoPtr->cs);
374 ANIMATE_DrawFrame(infoPtr, hDC);
375 LeaveCriticalSection(&infoPtr->cs);
377 ReleaseDC(infoPtr->hwndSelf, hDC);
380 return 0;
383 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
385 ANIMATE_INFO *infoPtr = ptr_;
386 HANDLE event;
387 DWORD timeout;
389 while(1)
391 HDC hDC = GetDC(infoPtr->hwndSelf);
393 EnterCriticalSection(&infoPtr->cs);
394 ANIMATE_DrawFrame(infoPtr, hDC);
395 timeout = infoPtr->mah.dwMicroSecPerFrame;
396 event = infoPtr->hStopEvent;
397 LeaveCriticalSection(&infoPtr->cs);
399 ReleaseDC(infoPtr->hwndSelf, hDC);
401 /* time is in microseconds, we should convert it to milliseconds */
402 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
403 break;
405 return TRUE;
408 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
410 /* nothing opened */
411 if (!infoPtr->hMMio)
412 return FALSE;
414 if (infoPtr->hThread || infoPtr->uTimer) {
415 TRACE("Already playing\n");
416 return TRUE;
419 infoPtr->nFromFrame = wFrom;
420 infoPtr->nToFrame = wTo;
421 infoPtr->nLoop = cRepeat;
423 if (infoPtr->nToFrame == 0xFFFF)
424 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
426 TRACE("(repeat=%d from=%d to=%d);\n",
427 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
429 if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
430 (SHORT)infoPtr->nFromFrame < 0)
431 infoPtr->nFromFrame = 0;
433 if (infoPtr->nFromFrame > infoPtr->nToFrame ||
434 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
435 return FALSE;
437 infoPtr->currFrame = infoPtr->nFromFrame;
439 /* seek - doesn't need to start a thread or set a timer and neither
440 * does it send a notification */
441 if (infoPtr->nFromFrame == infoPtr->nToFrame)
443 HDC hDC;
445 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
447 ANIMATE_DrawFrame(infoPtr, hDC);
449 ReleaseDC(infoPtr->hwndSelf, hDC);
451 return TRUE;
454 if (infoPtr->dwStyle & ACS_TIMER)
456 TRACE("Using a timer\n");
457 /* create a timer to display AVI */
458 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
459 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
461 else
463 TRACE("Using an animation thread\n");
464 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
465 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
466 infoPtr, 0, &infoPtr->threadId);
467 if(!infoPtr->hThread) return FALSE;
471 ANIMATE_Notify(infoPtr, ACN_START);
473 return TRUE;
477 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
479 MMCKINFO ckMainRIFF;
480 MMCKINFO mmckHead;
481 MMCKINFO mmckList;
482 MMCKINFO mmckInfo;
483 DWORD numFrame;
484 DWORD insize;
486 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
487 WARN("Can't find 'RIFF' chunk\n");
488 return FALSE;
491 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
492 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
493 WARN("Can't find 'AVI ' chunk\n");
494 return FALSE;
497 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
498 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
499 WARN("Can't find 'hdrl' list\n");
500 return FALSE;
503 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
504 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
505 WARN("Can't find 'avih' chunk\n");
506 return FALSE;
509 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
511 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
512 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
513 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
514 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
515 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
516 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
517 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
518 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
519 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
520 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
522 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
524 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
525 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
526 WARN("Can't find 'strl' list\n");
527 return FALSE;
530 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
531 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
532 WARN("Can't find 'strh' chunk\n");
533 return FALSE;
536 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
538 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
539 HIBYTE(LOWORD(infoPtr->ash.fccType)),
540 LOBYTE(HIWORD(infoPtr->ash.fccType)),
541 HIBYTE(HIWORD(infoPtr->ash.fccType)));
542 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
543 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
544 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
545 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
546 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
547 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
548 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
549 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
550 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
551 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
552 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
553 TRACE("ash.dwLength=%lu\n", infoPtr->ash.dwLength);
554 TRACE("ash.dwSuggestedBufferSize=%lu\n", infoPtr->ash.dwSuggestedBufferSize);
555 TRACE("ash.dwQuality=%lu\n", infoPtr->ash.dwQuality);
556 TRACE("ash.dwSampleSize=%lu\n", infoPtr->ash.dwSampleSize);
557 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
558 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
560 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
562 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
563 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
564 WARN("Can't find 'strh' chunk\n");
565 return FALSE;
568 infoPtr->inbih = heap_alloc_zero(mmckInfo.cksize);
569 if (!infoPtr->inbih) {
570 WARN("Can't alloc input BIH\n");
571 return FALSE;
574 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
576 TRACE("bih.biSize=%lu\n", infoPtr->inbih->biSize);
577 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
578 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
579 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
580 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
581 TRACE("bih.biCompression=%lu\n", infoPtr->inbih->biCompression);
582 TRACE("bih.biSizeImage=%lu\n", infoPtr->inbih->biSizeImage);
583 TRACE("bih.biXPelsPerMeter=%lu\n", infoPtr->inbih->biXPelsPerMeter);
584 TRACE("bih.biYPelsPerMeter=%lu\n", infoPtr->inbih->biYPelsPerMeter);
585 TRACE("bih.biClrUsed=%lu\n", infoPtr->inbih->biClrUsed);
586 TRACE("bih.biClrImportant=%lu\n", infoPtr->inbih->biClrImportant);
588 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
590 mmioAscend(infoPtr->hMMio, &mmckList, 0);
592 #if 0
593 /* an AVI has 0 or 1 video stream, and to be animated should not contain
594 * an audio stream, so only one strl is allowed
596 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
597 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
598 WARN("There should be a single 'strl' list\n");
599 return FALSE;
601 #endif
603 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
605 /* no need to read optional JUNK chunk */
607 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
608 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
609 WARN("Can't find 'movi' list\n");
610 return FALSE;
613 /* FIXME: should handle the 'rec ' LIST when present */
615 infoPtr->lpIndex = heap_alloc_zero(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
616 if (!infoPtr->lpIndex)
617 return FALSE;
619 numFrame = insize = 0;
620 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
621 numFrame < infoPtr->mah.dwTotalFrames) {
622 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
623 if (insize < mmckInfo.cksize)
624 insize = mmckInfo.cksize;
625 numFrame++;
626 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
628 if (numFrame != infoPtr->mah.dwTotalFrames)
630 WARN("Found %lu frames (/%lu)\n", numFrame, infoPtr->mah.dwTotalFrames);
631 return FALSE;
633 if (insize > infoPtr->ash.dwSuggestedBufferSize)
635 WARN("insize %lu suggestedSize %lu\n", insize, infoPtr->ash.dwSuggestedBufferSize);
636 infoPtr->ash.dwSuggestedBufferSize = insize;
639 infoPtr->indata = heap_alloc_zero(infoPtr->ash.dwSuggestedBufferSize);
640 if (!infoPtr->indata)
641 return FALSE;
643 return TRUE;
647 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
649 DWORD outSize;
651 /* check uncompressed AVI */
652 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
653 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
654 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
656 infoPtr->hic = 0;
657 return TRUE;
660 /* try to get a decompressor for that type */
661 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
662 if (!infoPtr->hic) {
663 WARN("Can't load codec for the file\n");
664 return FALSE;
667 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
668 (DWORD_PTR)infoPtr->inbih, 0L);
670 infoPtr->outbih = heap_alloc_zero(outSize);
671 if (!infoPtr->outbih)
672 return FALSE;
674 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
675 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
677 WARN("Can't get output BIH\n");
678 return FALSE;
681 infoPtr->outdata = heap_alloc_zero(infoPtr->outbih->biSizeImage);
682 if (!infoPtr->outdata)
683 return FALSE;
685 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
686 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
687 WARN("Can't begin decompression\n");
688 return FALSE;
691 return TRUE;
695 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
697 HDC hdc;
699 ANIMATE_Free(infoPtr);
701 if (!lpszName)
703 TRACE("Closing avi.\n");
704 /* installer of thebat! v1.62 requires FALSE here */
705 return (infoPtr->hMMio != 0);
708 if (!hInstance)
709 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
711 TRACE("(%s)\n", debugstr_w(lpszName));
713 if (!IS_INTRESOURCE(lpszName))
715 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
717 TRACE("No AVI resource found.\n");
718 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
720 WARN("No AVI file found.\n");
721 return FALSE;
725 else
727 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
729 WARN("No AVI resource found.\n");
730 return FALSE;
734 if (!ANIMATE_GetAviInfo(infoPtr))
736 WARN("Can't get AVI information\n");
737 ANIMATE_Free(infoPtr);
738 return FALSE;
741 if (!ANIMATE_GetAviCodec(infoPtr))
743 WARN("Can't get AVI Codec\n");
744 ANIMATE_Free(infoPtr);
745 return FALSE;
748 hdc = GetDC(infoPtr->hwndSelf);
749 /* native looks at the top left pixel of the first frame here too. */
750 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
751 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
752 ReleaseDC(infoPtr->hwndSelf, hdc);
754 if (!(infoPtr->dwStyle & ACS_CENTER))
755 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
756 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
758 if (infoPtr->dwStyle & ACS_AUTOPLAY)
759 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
761 return TRUE;
765 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
767 LPWSTR lpwszName;
768 LRESULT result;
769 INT len;
771 if (IS_INTRESOURCE(lpszName))
772 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
774 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
775 lpwszName = heap_alloc(len * sizeof(WCHAR));
776 if (!lpwszName) return FALSE;
777 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
779 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
780 heap_free (lpwszName);
781 return result;
785 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
787 /* nothing opened */
788 if (!infoPtr->hMMio)
789 return FALSE;
791 ANIMATE_DoStop(infoPtr);
792 return TRUE;
796 static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
798 ANIMATE_INFO *infoPtr;
800 if (!fnIC.hModule)
802 fnIC.hModule = LoadLibraryW(L"msvfw32.dll");
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 = heap_alloc_zero(sizeof(*infoPtr));
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 %#lx, 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 heap_free(infoPtr);
844 return 0;
848 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
850 RECT rect;
851 HBRUSH hBrush;
853 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
854 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
855 GetClientRect(infoPtr->hwndSelf, &rect);
856 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
858 return TRUE;
862 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
864 TRACE("%#Ix, styleOld %#lx, styleNew %#lx.\n", wStyleType, lpss->styleOld, lpss->styleNew);
866 if (wStyleType != GWL_STYLE) return 0;
868 infoPtr->dwStyle = lpss->styleNew;
870 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
871 return 0;
875 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
877 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
879 TRACE("hwnd %p, msg %x, wparam %#Ix, lparam %#Ix.\n", hWnd, uMsg, wParam, lParam);
881 if (!infoPtr && (uMsg != WM_NCCREATE))
882 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
883 switch (uMsg)
885 case ACM_OPENA:
886 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
888 case ACM_OPENW:
889 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
891 case ACM_PLAY:
892 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
894 case ACM_STOP:
895 return ANIMATE_Stop(infoPtr);
897 case WM_CLOSE:
898 ANIMATE_Free(infoPtr);
899 return 0;
901 case WM_NCCREATE:
902 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
904 case WM_NCHITTEST:
905 return HTTRANSPARENT;
907 case WM_DESTROY:
908 return ANIMATE_Destroy(infoPtr);
910 case WM_ERASEBKGND:
911 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
913 case WM_STYLECHANGED:
914 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
916 case WM_TIMER:
917 return ANIMATE_Timer(infoPtr);
919 case WM_PRINTCLIENT:
920 case WM_PAINT:
922 /* the animation has not decompressed
923 * (and displayed) the first frame yet, don't paint
925 if (!infoPtr->hbmPrevFrame)
927 /* default paint handling */
928 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
931 if (wParam)
933 EnterCriticalSection(&infoPtr->cs);
934 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
935 LeaveCriticalSection(&infoPtr->cs);
937 else
939 PAINTSTRUCT ps;
940 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
942 EnterCriticalSection(&infoPtr->cs);
943 ANIMATE_PaintFrame(infoPtr, hDC);
944 LeaveCriticalSection(&infoPtr->cs);
946 EndPaint(infoPtr->hwndSelf, &ps);
949 break;
951 case WM_SIZE:
952 if (infoPtr->dwStyle & ACS_CENTER)
953 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
954 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
956 default:
957 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
958 ERR("unknown msg %#x, wp %#Ix, lp %#Ix.\n", uMsg, wParam, lParam);
960 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
962 return 0;
965 void ANIMATE_Register(void)
967 WNDCLASSW wndClass;
969 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
970 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
971 wndClass.lpfnWndProc = ANIMATE_WindowProc;
972 wndClass.cbClsExtra = 0;
973 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
974 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
975 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
976 wndClass.lpszClassName = ANIMATE_CLASSW;
978 RegisterClassW(&wndClass);
982 void ANIMATE_Unregister(void)
984 UnregisterClassW(ANIMATE_CLASSW, NULL);