rtworkq: Add RtwqJoinWorkQueue()/RtwqUnjoinWorkQueue() stubs.
[wine.git] / dlls / comctl32 / animate.c
blobf8ba159779a1982f3e3f5af4122737363146ecc9
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 static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
98 HRSRC hrsrc;
99 MMIOINFO mminfo;
100 LPVOID lpAvi;
102 hrsrc = FindResourceW(hInst, lpName, aviW);
103 if (!hrsrc)
104 return FALSE;
106 infoPtr->hRes = LoadResource(hInst, hrsrc);
107 if (!infoPtr->hRes)
108 return FALSE;
110 lpAvi = LockResource(infoPtr->hRes);
111 if (!lpAvi)
112 return FALSE;
114 memset(&mminfo, 0, sizeof(mminfo));
115 mminfo.fccIOProc = FOURCC_MEM;
116 mminfo.pchBuffer = lpAvi;
117 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
118 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
119 if (!infoPtr->hMMio)
121 FreeResource(infoPtr->hRes);
122 return FALSE;
125 return TRUE;
129 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
131 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
133 if(!infoPtr->hMMio) return FALSE;
134 return TRUE;
138 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
140 BOOL stopped = FALSE;
142 EnterCriticalSection(&infoPtr->cs);
144 /* should stop playing */
145 if (infoPtr->hThread)
147 HANDLE handle = infoPtr->hThread;
149 TRACE("stopping animation thread\n");
150 infoPtr->hThread = 0;
151 SetEvent( infoPtr->hStopEvent );
153 if (infoPtr->threadId != GetCurrentThreadId())
155 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
156 WaitForSingleObject( handle, INFINITE );
157 TRACE("animation thread stopped\n");
158 EnterCriticalSection(&infoPtr->cs);
161 CloseHandle( handle );
162 CloseHandle( infoPtr->hStopEvent );
163 infoPtr->hStopEvent = 0;
164 stopped = TRUE;
166 if (infoPtr->uTimer) {
167 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
168 infoPtr->uTimer = 0;
169 stopped = TRUE;
172 LeaveCriticalSection(&infoPtr->cs);
174 if (stopped)
175 ANIMATE_Notify(infoPtr, ACN_STOP);
177 return TRUE;
181 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
183 if (infoPtr->hMMio)
185 ANIMATE_DoStop(infoPtr);
186 mmioClose(infoPtr->hMMio, 0);
187 if (infoPtr->hRes)
189 FreeResource(infoPtr->hRes);
190 infoPtr->hRes = 0;
192 heap_free (infoPtr->lpIndex);
193 infoPtr->lpIndex = NULL;
194 if (infoPtr->hic)
196 fnIC.fnICClose(infoPtr->hic);
197 infoPtr->hic = 0;
199 heap_free (infoPtr->inbih);
200 infoPtr->inbih = NULL;
201 heap_free (infoPtr->outbih);
202 infoPtr->outbih = NULL;
203 heap_free (infoPtr->indata);
204 infoPtr->indata = NULL;
205 heap_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 SetRect(&rect, 0, 0, nWidth, nHeight);
312 if(!infoPtr->hbrushBG)
313 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
315 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
316 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
318 SelectObject(hdcFinal, hbmOld2);
319 SelectObject(hdcMem, hbmFinal);
320 DeleteDC(hdcFinal);
321 DeleteObject(infoPtr->hbmPrevFrame);
322 infoPtr->hbmPrevFrame = hbmFinal;
325 if (infoPtr->dwStyle & ACS_CENTER)
327 RECT rect;
329 GetWindowRect(infoPtr->hwndSelf, &rect);
330 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
331 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
333 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
335 SelectObject(hdcMem, hbmOld);
336 DeleteDC(hdcMem);
337 return TRUE;
340 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr, HDC hDC)
342 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
344 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
345 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
347 if (infoPtr->hic &&
348 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
349 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
350 WARN("Decompression error\n");
351 return FALSE;
354 ANIMATE_PaintFrame(infoPtr, hDC);
356 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
357 infoPtr->currFrame = infoPtr->nFromFrame;
358 if (infoPtr->nLoop != -1) {
359 if (--infoPtr->nLoop == 0) {
360 ANIMATE_DoStop(infoPtr);
365 return TRUE;
368 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
370 HDC hDC;
372 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
374 EnterCriticalSection(&infoPtr->cs);
375 ANIMATE_DrawFrame(infoPtr, hDC);
376 LeaveCriticalSection(&infoPtr->cs);
378 ReleaseDC(infoPtr->hwndSelf, hDC);
381 return 0;
384 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
386 ANIMATE_INFO *infoPtr = ptr_;
387 HANDLE event;
388 DWORD timeout;
390 while(1)
392 HDC hDC = GetDC(infoPtr->hwndSelf);
394 EnterCriticalSection(&infoPtr->cs);
395 ANIMATE_DrawFrame(infoPtr, hDC);
396 timeout = infoPtr->mah.dwMicroSecPerFrame;
397 event = infoPtr->hStopEvent;
398 LeaveCriticalSection(&infoPtr->cs);
400 ReleaseDC(infoPtr->hwndSelf, hDC);
402 /* time is in microseconds, we should convert it to milliseconds */
403 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
404 break;
406 return TRUE;
409 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
411 /* nothing opened */
412 if (!infoPtr->hMMio)
413 return FALSE;
415 if (infoPtr->hThread || infoPtr->uTimer) {
416 TRACE("Already playing\n");
417 return TRUE;
420 infoPtr->nFromFrame = wFrom;
421 infoPtr->nToFrame = wTo;
422 infoPtr->nLoop = cRepeat;
424 if (infoPtr->nToFrame == 0xFFFF)
425 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
427 TRACE("(repeat=%d from=%d to=%d);\n",
428 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
430 if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
431 (SHORT)infoPtr->nFromFrame < 0)
432 infoPtr->nFromFrame = 0;
434 if (infoPtr->nFromFrame > infoPtr->nToFrame ||
435 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
436 return FALSE;
438 infoPtr->currFrame = infoPtr->nFromFrame;
440 /* seek - doesn't need to start a thread or set a timer and neither
441 * does it send a notification */
442 if (infoPtr->nFromFrame == infoPtr->nToFrame)
444 HDC hDC;
446 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
448 ANIMATE_DrawFrame(infoPtr, hDC);
450 ReleaseDC(infoPtr->hwndSelf, hDC);
452 return TRUE;
455 if (infoPtr->dwStyle & ACS_TIMER)
457 TRACE("Using a timer\n");
458 /* create a timer to display AVI */
459 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
460 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
462 else
464 TRACE("Using an animation thread\n");
465 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
466 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
467 infoPtr, 0, &infoPtr->threadId);
468 if(!infoPtr->hThread) return FALSE;
472 ANIMATE_Notify(infoPtr, ACN_START);
474 return TRUE;
478 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
480 MMCKINFO ckMainRIFF;
481 MMCKINFO mmckHead;
482 MMCKINFO mmckList;
483 MMCKINFO mmckInfo;
484 DWORD numFrame;
485 DWORD insize;
487 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
488 WARN("Can't find 'RIFF' chunk\n");
489 return FALSE;
492 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
493 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
494 WARN("Can't find 'AVI ' chunk\n");
495 return FALSE;
498 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
499 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
500 WARN("Can't find 'hdrl' list\n");
501 return FALSE;
504 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
505 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
506 WARN("Can't find 'avih' chunk\n");
507 return FALSE;
510 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
512 TRACE("mah.dwMicroSecPerFrame=%d\n", infoPtr->mah.dwMicroSecPerFrame);
513 TRACE("mah.dwMaxBytesPerSec=%d\n", infoPtr->mah.dwMaxBytesPerSec);
514 TRACE("mah.dwPaddingGranularity=%d\n", infoPtr->mah.dwPaddingGranularity);
515 TRACE("mah.dwFlags=%d\n", infoPtr->mah.dwFlags);
516 TRACE("mah.dwTotalFrames=%d\n", infoPtr->mah.dwTotalFrames);
517 TRACE("mah.dwInitialFrames=%d\n", infoPtr->mah.dwInitialFrames);
518 TRACE("mah.dwStreams=%d\n", infoPtr->mah.dwStreams);
519 TRACE("mah.dwSuggestedBufferSize=%d\n", infoPtr->mah.dwSuggestedBufferSize);
520 TRACE("mah.dwWidth=%d\n", infoPtr->mah.dwWidth);
521 TRACE("mah.dwHeight=%d\n", infoPtr->mah.dwHeight);
523 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
525 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
526 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
527 WARN("Can't find 'strl' list\n");
528 return FALSE;
531 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
532 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
533 WARN("Can't find 'strh' chunk\n");
534 return FALSE;
537 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
539 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
540 HIBYTE(LOWORD(infoPtr->ash.fccType)),
541 LOBYTE(HIWORD(infoPtr->ash.fccType)),
542 HIBYTE(HIWORD(infoPtr->ash.fccType)));
543 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
544 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
545 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
546 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
547 TRACE("ash.dwFlags=%d\n", infoPtr->ash.dwFlags);
548 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
549 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
550 TRACE("ash.dwInitialFrames=%d\n", infoPtr->ash.dwInitialFrames);
551 TRACE("ash.dwScale=%d\n", infoPtr->ash.dwScale);
552 TRACE("ash.dwRate=%d\n", infoPtr->ash.dwRate);
553 TRACE("ash.dwStart=%d\n", infoPtr->ash.dwStart);
554 TRACE("ash.dwLength=%d\n", infoPtr->ash.dwLength);
555 TRACE("ash.dwSuggestedBufferSize=%d\n", infoPtr->ash.dwSuggestedBufferSize);
556 TRACE("ash.dwQuality=%d\n", infoPtr->ash.dwQuality);
557 TRACE("ash.dwSampleSize=%d\n", infoPtr->ash.dwSampleSize);
558 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
559 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
561 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
563 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
564 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
565 WARN("Can't find 'strh' chunk\n");
566 return FALSE;
569 infoPtr->inbih = heap_alloc_zero(mmckInfo.cksize);
570 if (!infoPtr->inbih) {
571 WARN("Can't alloc input BIH\n");
572 return FALSE;
575 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
577 TRACE("bih.biSize=%d\n", infoPtr->inbih->biSize);
578 TRACE("bih.biWidth=%d\n", infoPtr->inbih->biWidth);
579 TRACE("bih.biHeight=%d\n", infoPtr->inbih->biHeight);
580 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
581 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
582 TRACE("bih.biCompression=%d\n", infoPtr->inbih->biCompression);
583 TRACE("bih.biSizeImage=%d\n", infoPtr->inbih->biSizeImage);
584 TRACE("bih.biXPelsPerMeter=%d\n", infoPtr->inbih->biXPelsPerMeter);
585 TRACE("bih.biYPelsPerMeter=%d\n", infoPtr->inbih->biYPelsPerMeter);
586 TRACE("bih.biClrUsed=%d\n", infoPtr->inbih->biClrUsed);
587 TRACE("bih.biClrImportant=%d\n", infoPtr->inbih->biClrImportant);
589 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
591 mmioAscend(infoPtr->hMMio, &mmckList, 0);
593 #if 0
594 /* an AVI has 0 or 1 video stream, and to be animated should not contain
595 * an audio stream, so only one strl is allowed
597 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
598 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
599 WARN("There should be a single 'strl' list\n");
600 return FALSE;
602 #endif
604 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
606 /* no need to read optional JUNK chunk */
608 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
609 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
610 WARN("Can't find 'movi' list\n");
611 return FALSE;
614 /* FIXME: should handle the 'rec ' LIST when present */
616 infoPtr->lpIndex = heap_alloc_zero(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
617 if (!infoPtr->lpIndex)
618 return FALSE;
620 numFrame = insize = 0;
621 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
622 numFrame < infoPtr->mah.dwTotalFrames) {
623 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
624 if (insize < mmckInfo.cksize)
625 insize = mmckInfo.cksize;
626 numFrame++;
627 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
629 if (numFrame != infoPtr->mah.dwTotalFrames) {
630 WARN("Found %d frames (/%d)\n", numFrame, infoPtr->mah.dwTotalFrames);
631 return FALSE;
633 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
634 WARN("insize=%d suggestedSize=%d\n", insize, infoPtr->ash.dwSuggestedBufferSize);
635 infoPtr->ash.dwSuggestedBufferSize = insize;
638 infoPtr->indata = heap_alloc_zero(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 infoPtr->outbih = heap_alloc_zero(outSize);
670 if (!infoPtr->outbih)
671 return FALSE;
673 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
674 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
676 WARN("Can't get output BIH\n");
677 return FALSE;
680 infoPtr->outdata = heap_alloc_zero(infoPtr->outbih->biSizeImage);
681 if (!infoPtr->outdata)
682 return FALSE;
684 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
685 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
686 WARN("Can't begin decompression\n");
687 return FALSE;
690 return TRUE;
694 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
696 HDC hdc;
698 ANIMATE_Free(infoPtr);
700 if (!lpszName)
702 TRACE("Closing avi.\n");
703 /* installer of thebat! v1.62 requires FALSE here */
704 return (infoPtr->hMMio != 0);
707 if (!hInstance)
708 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
710 TRACE("(%s)\n", debugstr_w(lpszName));
712 if (!IS_INTRESOURCE(lpszName))
714 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
716 TRACE("No AVI resource found.\n");
717 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
719 WARN("No AVI file found.\n");
720 return FALSE;
724 else
726 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
728 WARN("No AVI resource found.\n");
729 return FALSE;
733 if (!ANIMATE_GetAviInfo(infoPtr))
735 WARN("Can't get AVI information\n");
736 ANIMATE_Free(infoPtr);
737 return FALSE;
740 if (!ANIMATE_GetAviCodec(infoPtr))
742 WARN("Can't get AVI Codec\n");
743 ANIMATE_Free(infoPtr);
744 return FALSE;
747 hdc = GetDC(infoPtr->hwndSelf);
748 /* native looks at the top left pixel of the first frame here too. */
749 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
750 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
751 ReleaseDC(infoPtr->hwndSelf, hdc);
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 (IS_INTRESOURCE(lpszName))
771 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
773 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
774 lpwszName = heap_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 heap_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 = 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=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 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("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
865 wStyleType, lpss->styleOld, lpss->styleNew);
867 if (wStyleType != GWL_STYLE) return 0;
869 infoPtr->dwStyle = lpss->styleNew;
871 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
872 return 0;
876 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
878 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
880 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\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 %04x wp=%08lx lp=%08lx\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);