dmime: Remove DECLSPEC_HIDDEN usage.
[wine.git] / dlls / comctl32 / animate.c
blobf4d848fef9170ae40a7ad617045594a5132fc526
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"
40 WINE_DEFAULT_DEBUG_CHANNEL(animate);
42 static struct {
43 HMODULE hModule;
44 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
45 LRESULT (WINAPI *fnICClose)(HIC);
46 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD_PTR, DWORD_PTR);
47 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
48 } fnIC;
50 typedef struct
52 /* reference to input stream (file or resource) */
53 HGLOBAL hRes;
54 HMMIO hMMio; /* handle to mmio stream */
55 HWND hwndSelf;
56 HWND hwndNotify;
57 DWORD dwStyle;
58 /* information on the loaded AVI file */
59 MainAVIHeader mah;
60 AVIStreamHeader ash;
61 LPBITMAPINFOHEADER inbih;
62 LPDWORD lpIndex;
63 /* data for the decompressor */
64 HIC hic;
65 LPBITMAPINFOHEADER outbih;
66 LPVOID indata;
67 LPVOID outdata;
68 /* data for the background mechanism */
69 CRITICAL_SECTION cs;
70 HANDLE hStopEvent;
71 HANDLE hThread;
72 DWORD threadId;
73 UINT uTimer;
74 /* data for playing the file */
75 int nFromFrame;
76 int nToFrame;
77 int nLoop;
78 int currFrame;
79 /* transparency info*/
80 COLORREF transparentColor;
81 HBRUSH hbrushBG;
82 HBITMAP hbmPrevFrame;
83 } ANIMATE_INFO;
85 #define ANIMATE_COLOR_NONE 0xffffffff
87 static void ANIMATE_Notify(const ANIMATE_INFO *infoPtr, UINT notif)
89 PostMessageW(infoPtr->hwndNotify, WM_COMMAND,
90 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
91 (LPARAM)infoPtr->hwndSelf);
94 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPCWSTR lpName)
96 HRSRC hrsrc;
97 MMIOINFO mminfo;
98 LPVOID lpAvi;
100 hrsrc = FindResourceW(hInst, lpName, L"AVI");
101 if (!hrsrc)
102 return FALSE;
104 infoPtr->hRes = LoadResource(hInst, hrsrc);
105 if (!infoPtr->hRes)
106 return FALSE;
108 lpAvi = LockResource(infoPtr->hRes);
109 if (!lpAvi)
110 return FALSE;
112 memset(&mminfo, 0, sizeof(mminfo));
113 mminfo.fccIOProc = FOURCC_MEM;
114 mminfo.pchBuffer = lpAvi;
115 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
116 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
117 if (!infoPtr->hMMio)
119 FreeResource(infoPtr->hRes);
120 return FALSE;
123 return TRUE;
127 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
129 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
131 if(!infoPtr->hMMio) return FALSE;
132 return TRUE;
136 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
138 BOOL stopped = FALSE;
140 EnterCriticalSection(&infoPtr->cs);
142 /* should stop playing */
143 if (infoPtr->hThread)
145 HANDLE handle = infoPtr->hThread;
147 TRACE("stopping animation thread\n");
148 infoPtr->hThread = 0;
149 SetEvent( infoPtr->hStopEvent );
151 if (infoPtr->threadId != GetCurrentThreadId())
153 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
154 WaitForSingleObject( handle, INFINITE );
155 TRACE("animation thread stopped\n");
156 EnterCriticalSection(&infoPtr->cs);
159 CloseHandle( handle );
160 CloseHandle( infoPtr->hStopEvent );
161 infoPtr->hStopEvent = 0;
162 stopped = TRUE;
164 if (infoPtr->uTimer) {
165 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
166 infoPtr->uTimer = 0;
167 stopped = TRUE;
170 LeaveCriticalSection(&infoPtr->cs);
172 if (stopped)
173 ANIMATE_Notify(infoPtr, ACN_STOP);
175 return TRUE;
179 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
181 if (infoPtr->hMMio)
183 ANIMATE_DoStop(infoPtr);
184 mmioClose(infoPtr->hMMio, 0);
185 if (infoPtr->hRes)
187 FreeResource(infoPtr->hRes);
188 infoPtr->hRes = 0;
190 Free (infoPtr->lpIndex);
191 infoPtr->lpIndex = NULL;
192 if (infoPtr->hic)
194 fnIC.fnICClose(infoPtr->hic);
195 infoPtr->hic = 0;
197 Free (infoPtr->inbih);
198 infoPtr->inbih = NULL;
199 Free (infoPtr->outbih);
200 infoPtr->outbih = NULL;
201 Free (infoPtr->indata);
202 infoPtr->indata = NULL;
203 Free (infoPtr->outdata);
204 infoPtr->outdata = NULL;
205 if (infoPtr->hbmPrevFrame)
207 DeleteObject(infoPtr->hbmPrevFrame);
208 infoPtr->hbmPrevFrame = 0;
211 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
212 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
213 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
215 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
218 static void ANIMATE_TransparentBlt(ANIMATE_INFO const *infoPtr, HDC hdcDest, HDC hdcSource)
220 HDC hdcMask;
221 HBITMAP hbmMask;
222 HBITMAP hbmOld;
224 /* create a transparency mask */
225 hdcMask = CreateCompatibleDC(hdcDest);
226 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
227 hbmOld = SelectObject(hdcMask, hbmMask);
229 SetBkColor(hdcSource,infoPtr->transparentColor);
230 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
232 /* mask the source bitmap */
233 SetBkColor(hdcSource, RGB(0,0,0));
234 SetTextColor(hdcSource, RGB(255,255,255));
235 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
237 /* mask the destination bitmap */
238 SetBkColor(hdcDest, RGB(255,255,255));
239 SetTextColor(hdcDest, RGB(0,0,0));
240 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
242 /* combine source and destination */
243 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
245 SelectObject(hdcMask, hbmOld);
246 DeleteObject(hbmMask);
247 DeleteDC(hdcMask);
250 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
252 void const *pBitmapData;
253 BITMAPINFO const *pBitmapInfo;
254 HDC hdcMem;
255 HBITMAP hbmOld;
256 int nOffsetX = 0;
257 int nOffsetY = 0;
258 int nWidth;
259 int nHeight;
261 if (!hDC || !infoPtr->inbih)
262 return TRUE;
264 if (infoPtr->hic )
266 pBitmapData = infoPtr->outdata;
267 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
269 nWidth = infoPtr->outbih->biWidth;
270 nHeight = infoPtr->outbih->biHeight;
272 else
274 pBitmapData = infoPtr->indata;
275 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
277 nWidth = infoPtr->inbih->biWidth;
278 nHeight = infoPtr->inbih->biHeight;
281 if(!infoPtr->hbmPrevFrame)
283 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
286 hdcMem = CreateCompatibleDC(hDC);
287 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
289 SetDIBits(hdcMem, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, pBitmapInfo, DIB_RGB_COLORS);
292 * we need to get the transparent color even without ACS_TRANSPARENT,
293 * because the style can be changed later on and the color should always
294 * be obtained in the first frame
296 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
298 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
301 if(infoPtr->dwStyle & ACS_TRANSPARENT)
303 HDC hdcFinal = CreateCompatibleDC(hDC);
304 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
305 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
306 RECT rect;
308 SetRect(&rect, 0, 0, nWidth, nHeight);
310 if(!infoPtr->hbrushBG)
311 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
313 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
314 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
316 SelectObject(hdcFinal, hbmOld2);
317 SelectObject(hdcMem, hbmFinal);
318 DeleteDC(hdcFinal);
319 DeleteObject(infoPtr->hbmPrevFrame);
320 infoPtr->hbmPrevFrame = hbmFinal;
323 if (infoPtr->dwStyle & ACS_CENTER)
325 RECT rect;
327 GetWindowRect(infoPtr->hwndSelf, &rect);
328 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
329 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
331 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
333 SelectObject(hdcMem, hbmOld);
334 DeleteDC(hdcMem);
335 return TRUE;
338 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr, HDC hDC)
340 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
342 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
343 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
345 if (infoPtr->hic &&
346 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
347 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
348 WARN("Decompression error\n");
349 return FALSE;
352 ANIMATE_PaintFrame(infoPtr, hDC);
354 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
355 infoPtr->currFrame = infoPtr->nFromFrame;
356 if (infoPtr->nLoop != -1) {
357 if (--infoPtr->nLoop == 0) {
358 ANIMATE_DoStop(infoPtr);
363 return TRUE;
366 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
368 HDC hDC;
370 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
372 EnterCriticalSection(&infoPtr->cs);
373 ANIMATE_DrawFrame(infoPtr, hDC);
374 LeaveCriticalSection(&infoPtr->cs);
376 ReleaseDC(infoPtr->hwndSelf, hDC);
379 return 0;
382 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
384 ANIMATE_INFO *infoPtr = ptr_;
385 HANDLE event;
386 DWORD timeout;
388 while(1)
390 HDC hDC = GetDC(infoPtr->hwndSelf);
392 EnterCriticalSection(&infoPtr->cs);
393 ANIMATE_DrawFrame(infoPtr, hDC);
394 timeout = infoPtr->mah.dwMicroSecPerFrame;
395 event = infoPtr->hStopEvent;
396 LeaveCriticalSection(&infoPtr->cs);
398 ReleaseDC(infoPtr->hwndSelf, hDC);
400 /* time is in microseconds, we should convert it to milliseconds */
401 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
402 break;
404 return TRUE;
407 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
409 /* nothing opened */
410 if (!infoPtr->hMMio)
411 return FALSE;
413 if (infoPtr->hThread || infoPtr->uTimer) {
414 TRACE("Already playing\n");
415 return TRUE;
418 infoPtr->nFromFrame = wFrom;
419 infoPtr->nToFrame = wTo;
420 infoPtr->nLoop = cRepeat;
422 if (infoPtr->nToFrame == 0xFFFF)
423 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
425 TRACE("(repeat=%d from=%d to=%d);\n",
426 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
428 if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
429 (SHORT)infoPtr->nFromFrame < 0)
430 infoPtr->nFromFrame = 0;
432 if (infoPtr->nFromFrame > infoPtr->nToFrame ||
433 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
434 return FALSE;
436 infoPtr->currFrame = infoPtr->nFromFrame;
438 /* seek - doesn't need to start a thread or set a timer and neither
439 * does it send a notification */
440 if (infoPtr->nFromFrame == infoPtr->nToFrame)
442 HDC hDC;
444 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
446 ANIMATE_DrawFrame(infoPtr, hDC);
448 ReleaseDC(infoPtr->hwndSelf, hDC);
450 return TRUE;
453 if (infoPtr->dwStyle & ACS_TIMER)
455 TRACE("Using a timer\n");
456 /* create a timer to display AVI */
457 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
458 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
460 else
462 TRACE("Using an animation thread\n");
463 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
464 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
465 infoPtr, 0, &infoPtr->threadId);
466 if(!infoPtr->hThread) return FALSE;
470 ANIMATE_Notify(infoPtr, ACN_START);
472 return TRUE;
476 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
478 MMCKINFO ckMainRIFF;
479 MMCKINFO mmckHead;
480 MMCKINFO mmckList;
481 MMCKINFO mmckInfo;
482 DWORD numFrame;
483 DWORD insize;
485 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
486 WARN("Can't find 'RIFF' chunk\n");
487 return FALSE;
490 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
491 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
492 WARN("Can't find 'AVI ' chunk\n");
493 return FALSE;
496 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
497 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
498 WARN("Can't find 'hdrl' list\n");
499 return FALSE;
502 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
503 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
504 WARN("Can't find 'avih' chunk\n");
505 return FALSE;
508 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
510 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
511 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
512 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
513 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
514 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
515 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
516 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
517 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
518 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
519 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
521 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
523 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
524 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
525 WARN("Can't find 'strl' list\n");
526 return FALSE;
529 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
530 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
531 WARN("Can't find 'strh' chunk\n");
532 return FALSE;
535 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
537 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
538 HIBYTE(LOWORD(infoPtr->ash.fccType)),
539 LOBYTE(HIWORD(infoPtr->ash.fccType)),
540 HIBYTE(HIWORD(infoPtr->ash.fccType)));
541 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
542 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
543 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
544 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
545 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
546 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
547 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
548 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
549 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
550 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
551 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
552 TRACE("ash.dwLength=%lu\n", infoPtr->ash.dwLength);
553 TRACE("ash.dwSuggestedBufferSize=%lu\n", infoPtr->ash.dwSuggestedBufferSize);
554 TRACE("ash.dwQuality=%lu\n", infoPtr->ash.dwQuality);
555 TRACE("ash.dwSampleSize=%lu\n", infoPtr->ash.dwSampleSize);
556 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
557 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
559 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
561 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
562 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
563 WARN("Can't find 'strh' chunk\n");
564 return FALSE;
567 infoPtr->inbih = Alloc(mmckInfo.cksize);
568 if (!infoPtr->inbih) {
569 WARN("Can't alloc input BIH\n");
570 return FALSE;
573 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
575 TRACE("bih.biSize=%lu\n", infoPtr->inbih->biSize);
576 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
577 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
578 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
579 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
580 TRACE("bih.biCompression=%lu\n", infoPtr->inbih->biCompression);
581 TRACE("bih.biSizeImage=%lu\n", infoPtr->inbih->biSizeImage);
582 TRACE("bih.biXPelsPerMeter=%lu\n", infoPtr->inbih->biXPelsPerMeter);
583 TRACE("bih.biYPelsPerMeter=%lu\n", infoPtr->inbih->biYPelsPerMeter);
584 TRACE("bih.biClrUsed=%lu\n", infoPtr->inbih->biClrUsed);
585 TRACE("bih.biClrImportant=%lu\n", infoPtr->inbih->biClrImportant);
587 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
589 mmioAscend(infoPtr->hMMio, &mmckList, 0);
591 #if 0
592 /* an AVI has 0 or 1 video stream, and to be animated should not contain
593 * an audio stream, so only one strl is allowed
595 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
596 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
597 WARN("There should be a single 'strl' list\n");
598 return FALSE;
600 #endif
602 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
604 /* no need to read optional JUNK chunk */
606 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
607 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
608 WARN("Can't find 'movi' list\n");
609 return FALSE;
612 /* FIXME: should handle the 'rec ' LIST when present */
614 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
615 if (!infoPtr->lpIndex)
616 return FALSE;
618 numFrame = insize = 0;
619 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
620 numFrame < infoPtr->mah.dwTotalFrames) {
621 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
622 if (insize < mmckInfo.cksize)
623 insize = mmckInfo.cksize;
624 numFrame++;
625 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
627 if (numFrame != infoPtr->mah.dwTotalFrames)
629 WARN("Found %lu frames (/%lu)\n", numFrame, infoPtr->mah.dwTotalFrames);
630 return FALSE;
632 if (insize > infoPtr->ash.dwSuggestedBufferSize)
634 WARN("insize %lu suggestedSize %lu\n", insize, infoPtr->ash.dwSuggestedBufferSize);
635 infoPtr->ash.dwSuggestedBufferSize = insize;
638 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
639 if (!infoPtr->indata)
640 return FALSE;
642 return TRUE;
646 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
648 DWORD outSize;
650 /* check uncompressed AVI */
651 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
652 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
653 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
655 infoPtr->hic = 0;
656 return TRUE;
659 /* try to get a decompressor for that type */
660 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
661 if (!infoPtr->hic) {
662 WARN("Can't load codec for the file\n");
663 return FALSE;
666 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
667 (DWORD_PTR)infoPtr->inbih, 0L);
669 if (!(infoPtr->outbih = Alloc(outSize)))
670 return FALSE;
672 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
673 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
675 WARN("Can't get output BIH\n");
676 return FALSE;
679 if (!(infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage)))
680 return FALSE;
682 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
683 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
684 WARN("Can't begin decompression\n");
685 return FALSE;
688 return TRUE;
692 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
694 HDC hdc;
696 ANIMATE_Free(infoPtr);
698 if (!lpszName)
700 TRACE("Closing avi.\n");
701 /* installer of thebat! v1.62 requires FALSE here */
702 return (infoPtr->hMMio != 0);
705 if (!hInstance)
706 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
708 TRACE("(%s)\n", debugstr_w(lpszName));
710 if (!IS_INTRESOURCE(lpszName))
712 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
714 TRACE("No AVI resource found.\n");
715 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
717 WARN("No AVI file found.\n");
718 return FALSE;
722 else
724 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
726 WARN("No AVI resource found.\n");
727 return FALSE;
731 if (!ANIMATE_GetAviInfo(infoPtr))
733 WARN("Can't get AVI information\n");
734 ANIMATE_Free(infoPtr);
735 return FALSE;
738 if (!ANIMATE_GetAviCodec(infoPtr))
740 WARN("Can't get AVI Codec\n");
741 ANIMATE_Free(infoPtr);
742 return FALSE;
745 hdc = GetDC(infoPtr->hwndSelf);
746 /* native looks at the top left pixel of the first frame here too. */
747 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
748 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
749 ReleaseDC(infoPtr->hwndSelf, hdc);
751 if (!(infoPtr->dwStyle & ACS_CENTER))
752 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
753 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
755 if (infoPtr->dwStyle & ACS_AUTOPLAY)
756 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
758 return TRUE;
762 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
764 LPWSTR lpwszName;
765 LRESULT result;
766 INT len;
768 if (IS_INTRESOURCE(lpszName))
769 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
771 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
772 lpwszName = Alloc(len * sizeof(WCHAR));
773 if (!lpwszName) return FALSE;
774 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
776 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
777 Free (lpwszName);
778 return result;
782 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
784 /* nothing opened */
785 if (!infoPtr->hMMio)
786 return FALSE;
788 ANIMATE_DoStop(infoPtr);
789 return TRUE;
793 static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
795 ANIMATE_INFO *infoPtr;
797 if (!fnIC.hModule)
799 fnIC.hModule = LoadLibraryW(L"msvfw32.dll");
800 if (!fnIC.hModule) return FALSE;
802 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
803 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
804 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
805 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
808 /* allocate memory for info structure */
809 infoPtr = Alloc(sizeof(*infoPtr));
810 if (!infoPtr) return FALSE;
812 /* store crossref hWnd <-> info structure */
813 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
814 infoPtr->hwndSelf = hWnd;
815 infoPtr->hwndNotify = lpcs->hwndParent;
816 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
817 infoPtr->hbmPrevFrame = 0;
818 infoPtr->dwStyle = lpcs->style;
820 TRACE("Animate style %#lx, parent %p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
822 InitializeCriticalSection(&infoPtr->cs);
823 infoPtr->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ANIMATE_INFO*->cs");
825 return TRUE;
829 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
831 /* free avi data */
832 ANIMATE_Free(infoPtr);
834 /* free animate info data */
835 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
837 infoPtr->cs.DebugInfo->Spare[0] = 0;
838 DeleteCriticalSection(&infoPtr->cs);
839 Free(infoPtr);
841 return 0;
845 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
847 RECT rect;
848 HBRUSH hBrush;
850 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
851 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
852 GetClientRect(infoPtr->hwndSelf, &rect);
853 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
855 return TRUE;
859 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
861 TRACE("%#Ix, styleOld %#lx, styleNew %#lx.\n", wStyleType, lpss->styleOld, lpss->styleNew);
863 if (wStyleType != GWL_STYLE) return 0;
865 infoPtr->dwStyle = lpss->styleNew;
866 return 0;
870 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
872 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
874 TRACE("hwnd %p, msg %x, wparam %#Ix, lparam %#Ix.\n", hWnd, uMsg, wParam, lParam);
876 if (!infoPtr && (uMsg != WM_NCCREATE))
877 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
878 switch (uMsg)
880 case ACM_OPENA:
881 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
883 case ACM_OPENW:
884 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
886 case ACM_PLAY:
887 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
889 case ACM_STOP:
890 return ANIMATE_Stop(infoPtr);
892 case WM_CLOSE:
893 ANIMATE_Free(infoPtr);
894 return 0;
896 case WM_NCCREATE:
897 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
899 case WM_NCHITTEST:
900 return HTTRANSPARENT;
902 case WM_DESTROY:
903 return ANIMATE_Destroy(infoPtr);
905 case WM_ERASEBKGND:
906 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
908 case WM_STYLECHANGED:
909 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
911 case WM_TIMER:
912 return ANIMATE_Timer(infoPtr);
914 case WM_PRINTCLIENT:
915 case WM_PAINT:
917 /* the animation has not decompressed
918 * (and displayed) the first frame yet, don't paint
920 if (!infoPtr->hbmPrevFrame)
922 /* default paint handling */
923 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
926 if (wParam)
928 EnterCriticalSection(&infoPtr->cs);
929 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
930 LeaveCriticalSection(&infoPtr->cs);
932 else
934 PAINTSTRUCT ps;
935 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
937 EnterCriticalSection(&infoPtr->cs);
938 ANIMATE_PaintFrame(infoPtr, hDC);
939 LeaveCriticalSection(&infoPtr->cs);
941 EndPaint(infoPtr->hwndSelf, &ps);
944 break;
946 case WM_SIZE:
947 if (infoPtr->dwStyle & ACS_CENTER)
948 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
949 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
951 default:
952 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
953 ERR("unknown msg %#x, wp %#Ix, lp %#Ix.\n", uMsg, wParam, lParam);
955 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
957 return 0;
960 void ANIMATE_Register(void)
962 WNDCLASSW wndClass;
964 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
965 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
966 wndClass.lpfnWndProc = ANIMATE_WindowProc;
967 wndClass.cbClsExtra = 0;
968 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
969 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
970 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
971 wndClass.lpszClassName = ANIMATE_CLASSW;
973 RegisterClassW(&wndClass);
977 void ANIMATE_Unregister(void)
979 UnregisterClassW(ANIMATE_CLASSW, NULL);