Added the processing for determination of SFGAO_HASSUBFOLDER flag in
[wine/testsucceed.git] / dlls / comctl32 / animate.c
blob1a15609f7c77833b44f0883c62dfd96eb0fe05cd
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #define COM_NO_WINDOWS_H
37 #include <stdarg.h>
38 #include <string.h>
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "commctrl.h"
45 #include "vfw.h"
46 #include "mmsystem.h"
47 #include "comctl32.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(animate);
52 static struct {
53 HMODULE hModule;
54 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
55 LRESULT (WINAPI *fnICClose)(HIC);
56 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD, DWORD);
57 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
58 } fnIC;
60 typedef struct
62 /* reference to input stream (file or resource) */
63 HGLOBAL hRes;
64 HMMIO hMMio; /* handle to mmio stream */
65 HWND hwndSelf;
66 HWND hwndNotify;
67 DWORD dwStyle;
68 /* information on the loaded AVI file */
69 MainAVIHeader mah;
70 AVIStreamHeader ash;
71 LPBITMAPINFOHEADER inbih;
72 LPDWORD lpIndex;
73 /* data for the decompressor */
74 HIC hic;
75 LPBITMAPINFOHEADER outbih;
76 LPVOID indata;
77 LPVOID outdata;
78 /* data for the background mechanism */
79 CRITICAL_SECTION cs;
80 HANDLE hStopEvent;
81 HANDLE hThread;
82 DWORD threadId;
83 UINT uTimer;
84 /* data for playing the file */
85 int nFromFrame;
86 int nToFrame;
87 int nLoop;
88 int currFrame;
89 /* tranparency info*/
90 COLORREF transparentColor;
91 HBRUSH hbrushBG;
92 HBITMAP hbmPrevFrame;
93 } ANIMATE_INFO;
95 #define ANIMATE_COLOR_NONE 0xffffffff
97 static void ANIMATE_Notify(ANIMATE_INFO *infoPtr, UINT notif)
99 SendMessageW(infoPtr->hwndNotify, WM_COMMAND,
100 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
101 (LPARAM)infoPtr->hwndSelf);
104 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPWSTR lpName)
106 static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
107 HRSRC hrsrc;
108 MMIOINFO mminfo;
109 LPVOID lpAvi;
111 hrsrc = FindResourceW(hInst, lpName, aviW);
112 if (!hrsrc)
113 return FALSE;
115 infoPtr->hRes = LoadResource(hInst, hrsrc);
116 if (!infoPtr->hRes)
117 return FALSE;
119 lpAvi = LockResource(infoPtr->hRes);
120 if (!lpAvi)
121 return FALSE;
123 memset(&mminfo, 0, sizeof(mminfo));
124 mminfo.fccIOProc = FOURCC_MEM;
125 mminfo.pchBuffer = (LPSTR)lpAvi;
126 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
127 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
128 if (!infoPtr->hMMio)
130 FreeResource(infoPtr->hRes);
131 return FALSE;
134 return TRUE;
138 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
140 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
142 return (BOOL)infoPtr->hMMio;
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 *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* pBitmapData = NULL;
255 LPBITMAPINFO pBitmapInfo = NULL;
257 HDC hdcMem;
258 HBITMAP hbmOld;
260 int nOffsetX = 0;
261 int nOffsetY = 0;
263 int nWidth;
264 int nHeight;
266 if (!hDC || !infoPtr->inbih)
267 return TRUE;
269 if (infoPtr->hic )
271 pBitmapData = infoPtr->outdata;
272 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
274 nWidth = infoPtr->outbih->biWidth;
275 nHeight = infoPtr->outbih->biHeight;
277 else
279 pBitmapData = infoPtr->indata;
280 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
282 nWidth = infoPtr->inbih->biWidth;
283 nHeight = infoPtr->inbih->biHeight;
286 if(!infoPtr->hbmPrevFrame)
288 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
291 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
293 hdcMem = CreateCompatibleDC(hDC);
294 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
297 * we need to get the transparent color even without ACS_TRANSPARENT,
298 * because the style can be changed later on and the color should always
299 * be obtained in the first frame
301 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
303 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
306 if(infoPtr->dwStyle & ACS_TRANSPARENT)
308 HDC hdcFinal = CreateCompatibleDC(hDC);
309 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
310 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
311 RECT rect;
313 rect.left = 0;
314 rect.top = 0;
315 rect.right = nWidth;
316 rect.bottom = nHeight;
318 if(!infoPtr->hbrushBG)
319 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
321 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
322 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
324 SelectObject(hdcFinal, hbmOld2);
325 SelectObject(hdcMem, hbmFinal);
326 DeleteDC(hdcFinal);
327 DeleteObject(infoPtr->hbmPrevFrame);
328 infoPtr->hbmPrevFrame = hbmFinal;
331 if (infoPtr->dwStyle & ACS_CENTER)
333 RECT rect;
335 GetWindowRect(infoPtr->hwndSelf, &rect);
336 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
337 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
339 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
341 SelectObject(hdcMem, hbmOld);
342 DeleteDC(hdcMem);
343 return TRUE;
346 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr)
348 HDC hDC;
350 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
352 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
353 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
355 if (infoPtr->hic &&
356 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
357 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
358 WARN("Decompression error\n");
359 return FALSE;
362 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0) {
363 ANIMATE_PaintFrame(infoPtr, hDC);
364 ReleaseDC(infoPtr->hwndSelf, hDC);
367 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
368 infoPtr->currFrame = infoPtr->nFromFrame;
369 if (infoPtr->nLoop != -1) {
370 if (--infoPtr->nLoop == 0) {
371 ANIMATE_DoStop(infoPtr);
376 return TRUE;
379 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
381 /* FIXME: we should pass the hDC instead of 0 to WM_CTLCOLORSTATIC */
382 if (infoPtr->dwStyle & ACS_TRANSPARENT)
383 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
384 WM_CTLCOLORSTATIC,
385 0, (LPARAM)infoPtr->hwndSelf);
386 EnterCriticalSection(&infoPtr->cs);
387 ANIMATE_DrawFrame(infoPtr);
388 LeaveCriticalSection(&infoPtr->cs);
390 return 0;
393 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
395 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)ptr_;
396 HANDLE event;
397 DWORD timeout;
399 while(1)
401 EnterCriticalSection(&infoPtr->cs);
402 ANIMATE_DrawFrame(infoPtr);
403 timeout = infoPtr->mah.dwMicroSecPerFrame;
404 event = infoPtr->hStopEvent;
405 LeaveCriticalSection(&infoPtr->cs);
407 /* time is in microseconds, we should convert it to milliseconds */
408 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
409 break;
411 return TRUE;
414 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
416 /* nothing opened */
417 if (!infoPtr->hMMio)
418 return FALSE;
420 if (infoPtr->hThread || infoPtr->uTimer) {
421 TRACE("Already playing\n");
422 return TRUE;
425 infoPtr->nFromFrame = wFrom;
426 infoPtr->nToFrame = wTo;
427 infoPtr->nLoop = cRepeat;
429 if (infoPtr->nToFrame == 0xFFFF)
430 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
432 TRACE("(repeat=%d from=%d to=%d);\n",
433 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
435 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
436 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
437 return FALSE;
439 infoPtr->currFrame = infoPtr->nFromFrame;
441 if (infoPtr->dwStyle & ACS_TIMER)
443 TRACE("Using a timer\n");
444 /* create a timer to display AVI */
445 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
446 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
448 else
450 if(infoPtr->dwStyle & ACS_TRANSPARENT)
451 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify,
452 WM_CTLCOLORSTATIC, 0,
453 (LPARAM)infoPtr->hwndSelf);
455 TRACE("Using an animation thread\n");
456 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
457 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
458 (LPVOID)infoPtr, 0, &infoPtr->threadId);
459 if(!infoPtr->hThread) return FALSE;
463 ANIMATE_Notify(infoPtr, ACN_START);
465 return TRUE;
469 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
471 MMCKINFO ckMainRIFF;
472 MMCKINFO mmckHead;
473 MMCKINFO mmckList;
474 MMCKINFO mmckInfo;
475 DWORD numFrame;
476 DWORD insize;
478 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
479 WARN("Can't find 'RIFF' chunk\n");
480 return FALSE;
483 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
484 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
485 WARN("Can't find 'AVI ' chunk\n");
486 return FALSE;
489 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
490 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
491 WARN("Can't find 'hdrl' list\n");
492 return FALSE;
495 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
496 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
497 WARN("Can't find 'avih' chunk\n");
498 return FALSE;
501 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
503 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
504 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
505 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
506 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
507 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
508 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
509 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
510 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
511 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
512 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
514 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
516 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
517 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
518 WARN("Can't find 'strl' list\n");
519 return FALSE;
522 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
523 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
524 WARN("Can't find 'strh' chunk\n");
525 return FALSE;
528 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
530 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
531 HIBYTE(LOWORD(infoPtr->ash.fccType)),
532 LOBYTE(HIWORD(infoPtr->ash.fccType)),
533 HIBYTE(HIWORD(infoPtr->ash.fccType)));
534 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
535 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
536 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
537 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
538 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
539 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
540 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
541 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
542 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
543 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
544 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
545 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
546 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
547 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
548 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
549 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
550 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
552 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
554 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
555 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
556 WARN("Can't find 'strh' chunk\n");
557 return FALSE;
560 infoPtr->inbih = Alloc(mmckInfo.cksize);
561 if (!infoPtr->inbih) {
562 WARN("Can't alloc input BIH\n");
563 return FALSE;
566 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
568 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
569 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
570 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
571 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
572 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
573 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
574 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
575 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
576 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
577 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
578 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
580 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
582 mmioAscend(infoPtr->hMMio, &mmckList, 0);
584 #if 0
585 /* an AVI has 0 or 1 video stream, and to be animated should not contain
586 * an audio stream, so only one strl is allowed
588 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
589 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
590 WARN("There should be a single 'strl' list\n");
591 return FALSE;
593 #endif
595 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
597 /* no need to read optional JUNK chunk */
599 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
600 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
601 WARN("Can't find 'movi' list\n");
602 return FALSE;
605 /* FIXME: should handle the 'rec ' LIST when present */
607 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
608 if (!infoPtr->lpIndex)
609 return FALSE;
611 numFrame = insize = 0;
612 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
613 numFrame < infoPtr->mah.dwTotalFrames) {
614 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
615 if (insize < mmckInfo.cksize)
616 insize = mmckInfo.cksize;
617 numFrame++;
618 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
620 if (numFrame != infoPtr->mah.dwTotalFrames) {
621 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
622 return FALSE;
624 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
625 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
626 infoPtr->ash.dwSuggestedBufferSize = insize;
629 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
630 if (!infoPtr->indata)
631 return FALSE;
633 return TRUE;
637 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
639 DWORD outSize;
641 /* check uncompressed AVI */
642 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
643 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
644 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
646 infoPtr->hic = 0;
647 return TRUE;
650 /* try to get a decompressor for that type */
651 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
652 if (!infoPtr->hic) {
653 WARN("Can't load codec for the file\n");
654 return FALSE;
657 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
658 (DWORD)infoPtr->inbih, 0L);
660 infoPtr->outbih = Alloc(outSize);
661 if (!infoPtr->outbih)
662 return FALSE;
664 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
665 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != outSize)
667 WARN("Can't get output BIH\n");
668 return FALSE;
671 infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
672 if (!infoPtr->outdata)
673 return FALSE;
675 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
676 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
677 WARN("Can't begin decompression\n");
678 return FALSE;
681 return TRUE;
685 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
687 ANIMATE_Free(infoPtr);
689 if (!lpszName)
691 TRACE("Closing avi!\n");
692 /* installer of thebat! v1.62 requires FALSE here */
693 return (infoPtr->hMMio != 0);
696 if (!hInstance)
697 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
699 if (HIWORD(lpszName))
701 TRACE("(\"%s\");\n", debugstr_w(lpszName));
703 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
705 TRACE("No AVI resource found!\n");
706 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
708 WARN("No AVI file found!\n");
709 return FALSE;
713 else
715 TRACE("(%u);\n", (WORD)(DWORD)lpszName);
717 if (!ANIMATE_LoadResW(infoPtr, hInstance, MAKEINTRESOURCEW((INT)lpszName)))
719 WARN("No AVI resource found!\n");
720 return FALSE;
724 if (!ANIMATE_GetAviInfo(infoPtr))
726 WARN("Can't get AVI information\n");
727 ANIMATE_Free(infoPtr);
728 return FALSE;
731 if (!ANIMATE_GetAviCodec(infoPtr))
733 WARN("Can't get AVI Codec\n");
734 ANIMATE_Free(infoPtr);
735 return FALSE;
738 if (!(infoPtr->dwStyle & ACS_CENTER))
739 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
740 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
742 if (infoPtr->dwStyle & ACS_AUTOPLAY)
743 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
745 return TRUE;
749 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
751 LPWSTR lpwszName;
752 LRESULT result;
753 INT len;
755 if (!HIWORD(lpszName))
756 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
758 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
759 lpwszName = Alloc(len * sizeof(WCHAR));
760 if (!lpwszName) return FALSE;
761 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
763 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
764 Free (lpwszName);
765 return result;
769 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
771 /* nothing opened */
772 if (!infoPtr->hMMio)
773 return FALSE;
775 ANIMATE_DoStop(infoPtr);
776 return TRUE;
780 static BOOL ANIMATE_Create(HWND hWnd, LPCREATESTRUCTW lpcs)
782 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
783 ANIMATE_INFO *infoPtr;
785 if (!fnIC.hModule)
787 fnIC.hModule = LoadLibraryW(msvfw32W);
788 if (!fnIC.hModule) return FALSE;
790 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
791 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
792 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
793 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
796 /* allocate memory for info structure */
797 infoPtr = (ANIMATE_INFO *)Alloc(sizeof(ANIMATE_INFO));
798 if (!infoPtr) return FALSE;
800 /* store crossref hWnd <-> info structure */
801 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
802 infoPtr->hwndSelf = hWnd;
803 infoPtr->hwndNotify = lpcs->hwndParent;
804 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
805 infoPtr->hbmPrevFrame = 0;
806 infoPtr->dwStyle = lpcs->style;
808 TRACE("Animate style=0x%08lx, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
810 InitializeCriticalSection(&infoPtr->cs);
812 return TRUE;
816 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
818 /* free avi data */
819 ANIMATE_Free(infoPtr);
821 /* free animate info data */
822 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
824 DeleteCriticalSection(&infoPtr->cs);
825 Free(infoPtr);
827 return 0;
831 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO *infoPtr, HDC hdc)
833 RECT rect;
834 HBRUSH hBrush = 0;
836 if(infoPtr->dwStyle & ACS_TRANSPARENT)
838 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
839 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
842 GetClientRect(infoPtr->hwndSelf, &rect);
843 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
845 return TRUE;
849 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, LPSTYLESTRUCT lpss)
851 TRACE("(styletype=%x, styleOld=0x%08lx, styleNew=0x%08lx)\n",
852 wStyleType, lpss->styleOld, lpss->styleNew);
854 if (wStyleType != GWL_STYLE) return 0;
856 infoPtr->dwStyle = lpss->styleNew;
858 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
859 return 0;
863 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
865 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
867 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
868 if (!infoPtr && (uMsg != WM_NCCREATE))
869 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
870 switch (uMsg)
872 case ACM_OPENA:
873 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
875 case ACM_OPENW:
876 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
878 case ACM_PLAY:
879 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
881 case ACM_STOP:
882 return ANIMATE_Stop(infoPtr);
884 case WM_CLOSE:
885 ANIMATE_Free(infoPtr);
886 return 0;
888 case WM_NCCREATE:
889 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
891 case WM_NCHITTEST:
892 return HTTRANSPARENT;
894 case WM_DESTROY:
895 return ANIMATE_Destroy(infoPtr);
897 case WM_ERASEBKGND:
898 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
900 case WM_STYLECHANGED:
901 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
903 case WM_TIMER:
904 return ANIMATE_Timer(infoPtr);
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=%08x 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);