- fix the "int format, HANDLE arg" type of warnings for comctl32
[wine/multimedia.git] / dlls / comctl32 / animate.c
blobcef888a6940480e8a9c54100c371c18a307b9ec0
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
5 * Copyright 1998, 1999 Eric Kohl
6 * 1999 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * NOTES
23 * I will only improve this control once in a while.
24 * Eric <ekohl@abo.rhein-zeitung.de>
26 * TODO:
27 * - check for the 'rec ' list in some AVI files
28 * - concurrent access to infoPtr
31 #include <string.h>
32 #include "winbase.h"
33 #include "commctrl.h"
34 #include "vfw.h"
35 #include "mmsystem.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(animate);
40 static struct {
41 HMODULE hModule;
42 HIC (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
43 LRESULT (WINAPI *fnICClose)(HIC);
44 LRESULT (WINAPI *fnICSendMessage)(HIC, UINT, DWORD, DWORD);
45 DWORD (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
46 } fnIC;
48 typedef struct
50 /* reference to input stream (file or resource) */
51 HGLOBAL hRes;
52 HMMIO hMMio; /* handle to mmio stream */
53 HWND hWnd;
54 /* information on the loaded AVI file */
55 MainAVIHeader mah;
56 AVIStreamHeader ash;
57 LPBITMAPINFOHEADER inbih;
58 LPDWORD lpIndex;
59 /* data for the decompressor */
60 HIC hic;
61 LPBITMAPINFOHEADER outbih;
62 LPVOID indata;
63 LPVOID outdata;
64 /* data for the background mechanism */
65 CRITICAL_SECTION cs;
66 HANDLE hThread;
67 UINT uTimer;
68 /* data for playing the file */
69 int nFromFrame;
70 int nToFrame;
71 int nLoop;
72 int currFrame;
73 /* tranparency info*/
74 COLORREF transparentColor;
75 HBRUSH hbrushBG;
76 HBITMAP hbmPrevFrame;
77 } ANIMATE_INFO;
79 #define ANIMATE_GetInfoPtr(hWnd) ((ANIMATE_INFO *)GetWindowLongA(hWnd, 0))
80 #define ANIMATE_COLOR_NONE 0xffffffff
82 static void ANIMATE_Notify(ANIMATE_INFO* infoPtr, UINT notif)
84 SendMessageA(GetParent(infoPtr->hWnd), WM_COMMAND,
85 MAKEWPARAM(GetDlgCtrlID(infoPtr->hWnd), notif),
86 (LPARAM)infoPtr->hWnd);
89 static BOOL ANIMATE_LoadResA(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPSTR lpName)
91 HRSRC hrsrc;
92 MMIOINFO mminfo;
93 LPVOID lpAvi;
95 hrsrc = FindResourceA(hInst, lpName, "AVI");
96 if (!hrsrc)
97 return FALSE;
99 infoPtr->hRes = LoadResource(hInst, hrsrc);
100 if (!infoPtr->hRes)
101 return FALSE;
103 lpAvi = LockResource(infoPtr->hRes);
104 if (!lpAvi)
105 return FALSE;
107 memset(&mminfo, 0, sizeof(mminfo));
108 mminfo.fccIOProc = FOURCC_MEM;
109 mminfo.pchBuffer = (LPSTR)lpAvi;
110 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
111 infoPtr->hMMio = mmioOpenA(NULL, &mminfo, MMIO_READ);
112 if (!infoPtr->hMMio) {
113 GlobalFree((HGLOBAL)lpAvi);
114 return FALSE;
117 return TRUE;
121 static BOOL ANIMATE_LoadFileA(ANIMATE_INFO *infoPtr, LPSTR lpName)
123 infoPtr->hMMio = mmioOpenA((LPSTR)lpName, NULL,
124 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
126 if (!infoPtr->hMMio)
127 return FALSE;
129 return TRUE;
133 static LRESULT ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
135 EnterCriticalSection(&infoPtr->cs);
137 /* should stop playing */
138 if (infoPtr->hThread)
140 if (!TerminateThread(infoPtr->hThread,0))
141 WARN("could not destroy animation thread!\n");
142 infoPtr->hThread = 0;
144 if (infoPtr->uTimer) {
145 KillTimer(infoPtr->hWnd, infoPtr->uTimer);
146 infoPtr->uTimer = 0;
149 LeaveCriticalSection(&infoPtr->cs);
151 ANIMATE_Notify(infoPtr, ACN_STOP);
153 return TRUE;
157 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
159 if (infoPtr->hMMio) {
160 ANIMATE_DoStop(infoPtr);
161 mmioClose(infoPtr->hMMio, 0);
162 if (infoPtr->hRes) {
163 FreeResource(infoPtr->hRes);
164 infoPtr->hRes = 0;
166 if (infoPtr->lpIndex) {
167 HeapFree(GetProcessHeap(), 0, infoPtr->lpIndex);
168 infoPtr->lpIndex = NULL;
170 if (infoPtr->hic) {
171 fnIC.fnICClose(infoPtr->hic);
172 infoPtr->hic = 0;
174 if (infoPtr->inbih) {
175 HeapFree(GetProcessHeap(), 0, infoPtr->inbih);
176 infoPtr->inbih = NULL;
178 if (infoPtr->outbih) {
179 HeapFree(GetProcessHeap(), 0, infoPtr->outbih);
180 infoPtr->outbih = NULL;
182 if( infoPtr->indata )
184 HeapFree(GetProcessHeap(), 0, infoPtr->indata);
185 infoPtr->indata = NULL;
187 if( infoPtr->outdata )
189 HeapFree(GetProcessHeap(), 0, infoPtr->outdata);
190 infoPtr->outdata = NULL;
192 if( infoPtr->hbmPrevFrame )
194 DeleteObject(infoPtr->hbmPrevFrame);
195 infoPtr->hbmPrevFrame = 0;
197 infoPtr->indata = infoPtr->outdata = NULL;
198 infoPtr->hWnd = 0;
199 infoPtr->hMMio = 0;
201 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
202 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
203 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
205 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
208 static void ANIMATE_TransparentBlt(ANIMATE_INFO* infoPtr, HDC hdcDest, HDC hdcSource)
210 HDC hdcMask;
211 HBITMAP hbmMask;
212 HBITMAP hbmOld;
214 /* create a transparency mask */
215 hdcMask = CreateCompatibleDC(hdcDest);
216 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
217 hbmOld = SelectObject(hdcMask, hbmMask);
219 SetBkColor(hdcSource,infoPtr->transparentColor);
220 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
222 /* mask the source bitmap */
223 SetBkColor(hdcSource, RGB(0,0,0));
224 SetTextColor(hdcSource, RGB(255,255,255));
225 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
227 /* mask the destination bitmap */
228 SetBkColor(hdcDest, RGB(255,255,255));
229 SetTextColor(hdcDest, RGB(0,0,0));
230 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
232 /* combine source and destination */
233 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
235 SelectObject(hdcMask, hbmOld);
236 DeleteObject(hbmMask);
237 DeleteDC(hdcMask);
240 static LRESULT ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
242 void* pBitmapData = NULL;
243 LPBITMAPINFO pBitmapInfo = NULL;
245 HDC hdcMem;
246 HBITMAP hbmOld;
248 int nOffsetX = 0;
249 int nOffsetY = 0;
251 int nWidth;
252 int nHeight;
254 if (!hDC || !infoPtr->inbih)
255 return TRUE;
257 if (infoPtr->hic )
259 pBitmapData = infoPtr->outdata;
260 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
262 nWidth = infoPtr->outbih->biWidth;
263 nHeight = infoPtr->outbih->biHeight;
264 } else
266 pBitmapData = infoPtr->indata;
267 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
269 nWidth = infoPtr->inbih->biWidth;
270 nHeight = infoPtr->inbih->biHeight;
273 if(!infoPtr->hbmPrevFrame)
275 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
278 SetDIBits(hDC, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, (LPBITMAPINFO)pBitmapInfo, DIB_RGB_COLORS);
280 hdcMem = CreateCompatibleDC(hDC);
281 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
284 * we need to get the transparent color even without ACS_TRANSPARENT,
285 * because the style can be changed later on and the color should always
286 * be obtained in the first frame
288 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
290 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
293 if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT)
295 HDC hdcFinal = CreateCompatibleDC(hDC);
296 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
297 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
298 RECT rect;
300 rect.left = 0;
301 rect.top = 0;
302 rect.right = nWidth;
303 rect.bottom = nHeight;
305 if(!infoPtr->hbrushBG)
306 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
308 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
309 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
311 SelectObject(hdcFinal, hbmOld2);
312 SelectObject(hdcMem, hbmFinal);
313 DeleteDC(hdcFinal);
314 DeleteObject(infoPtr->hbmPrevFrame);
315 infoPtr->hbmPrevFrame = hbmFinal;
318 if (GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_CENTER)
320 RECT rect;
322 GetWindowRect(infoPtr->hWnd, &rect);
323 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
324 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
326 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
328 SelectObject(hdcMem, hbmOld);
329 DeleteDC(hdcMem);
330 return TRUE;
333 static LRESULT ANIMATE_DrawFrame(ANIMATE_INFO* infoPtr)
335 HDC hDC;
337 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
339 EnterCriticalSection(&infoPtr->cs);
341 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
342 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
344 if (infoPtr->hic &&
345 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
346 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
347 LeaveCriticalSection(&infoPtr->cs);
348 WARN("Decompression error\n");
349 return FALSE;
352 if ((hDC = GetDC(infoPtr->hWnd)) != 0) {
353 ANIMATE_PaintFrame(infoPtr, hDC);
354 ReleaseDC(infoPtr->hWnd, hDC);
357 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
358 infoPtr->currFrame = infoPtr->nFromFrame;
359 if (infoPtr->nLoop != -1) {
360 if (--infoPtr->nLoop == 0) {
361 ANIMATE_DoStop(infoPtr);
365 LeaveCriticalSection(&infoPtr->cs);
367 return TRUE;
370 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
372 ANIMATE_INFO* infoPtr = (ANIMATE_INFO*)ptr_;
373 HDC hDC;
375 if(!infoPtr)
377 WARN("animation structure undefined!\n");
378 return FALSE;
381 while(1)
383 if(GetWindowLongA(infoPtr->hWnd, GWL_STYLE) & ACS_TRANSPARENT)
385 hDC = GetDC(infoPtr->hWnd);
386 /* sometimes the animation window will be destroyed in between
387 * by the main program, so a ReleaseDC() error msg is possible */
388 infoPtr->hbrushBG = (HBRUSH)SendMessageA(GetParent(infoPtr->hWnd),
389 WM_CTLCOLORSTATIC, (WPARAM)hDC,
390 (LPARAM)infoPtr->hWnd);
391 ReleaseDC(infoPtr->hWnd,hDC);
394 EnterCriticalSection(&infoPtr->cs);
395 ANIMATE_DrawFrame(infoPtr);
396 LeaveCriticalSection(&infoPtr->cs);
398 /* time is in microseconds, we should convert it to milliseconds */
399 Sleep((infoPtr->mah.dwMicroSecPerFrame+500)/1000);
401 return TRUE;
404 static LRESULT ANIMATE_Play(HWND hWnd, WPARAM wParam, LPARAM lParam)
406 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
408 /* nothing opened */
409 if (!infoPtr->hMMio)
410 return FALSE;
412 if (infoPtr->hThread || infoPtr->uTimer) {
413 FIXME("Already playing ? what should I do ??\n");
414 ANIMATE_DoStop(infoPtr);
417 infoPtr->nFromFrame = (INT)LOWORD(lParam);
418 infoPtr->nToFrame = (INT)HIWORD(lParam);
419 infoPtr->nLoop = (INT)wParam;
421 if (infoPtr->nToFrame == 0xFFFF)
422 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
424 TRACE("(repeat=%d from=%d to=%d);\n",
425 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
427 if (infoPtr->nFromFrame >= infoPtr->nToFrame ||
428 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
429 return FALSE;
431 infoPtr->currFrame = infoPtr->nFromFrame;
433 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TIMER) {
434 TRACE("Using a timer\n");
435 /* create a timer to display AVI */
436 infoPtr->uTimer = SetTimer(hWnd, 1, infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
437 } else {
438 DWORD threadID;
440 TRACE("Using an animation thread\n");
441 infoPtr->hThread = CreateThread(0,0,ANIMATE_AnimationThread,(LPVOID)infoPtr,0,0 &threadID);
442 if(!infoPtr->hThread)
444 ERR("Could not create animation thread!\n");
445 return FALSE;
450 ANIMATE_Notify(infoPtr, ACN_START);
452 return TRUE;
456 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
458 MMCKINFO ckMainRIFF;
459 MMCKINFO mmckHead;
460 MMCKINFO mmckList;
461 MMCKINFO mmckInfo;
462 DWORD numFrame;
463 DWORD insize;
465 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
466 WARN("Can't find 'RIFF' chunk\n");
467 return FALSE;
470 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
471 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
472 WARN("Can't find 'AVI ' chunk\n");
473 return FALSE;
476 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
477 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
478 WARN("Can't find 'hdrl' list\n");
479 return FALSE;
482 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
483 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
484 WARN("Can't find 'avih' chunk\n");
485 return FALSE;
488 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
490 TRACE("mah.dwMicroSecPerFrame=%ld\n", infoPtr->mah.dwMicroSecPerFrame);
491 TRACE("mah.dwMaxBytesPerSec=%ld\n", infoPtr->mah.dwMaxBytesPerSec);
492 TRACE("mah.dwPaddingGranularity=%ld\n", infoPtr->mah.dwPaddingGranularity);
493 TRACE("mah.dwFlags=%ld\n", infoPtr->mah.dwFlags);
494 TRACE("mah.dwTotalFrames=%ld\n", infoPtr->mah.dwTotalFrames);
495 TRACE("mah.dwInitialFrames=%ld\n", infoPtr->mah.dwInitialFrames);
496 TRACE("mah.dwStreams=%ld\n", infoPtr->mah.dwStreams);
497 TRACE("mah.dwSuggestedBufferSize=%ld\n", infoPtr->mah.dwSuggestedBufferSize);
498 TRACE("mah.dwWidth=%ld\n", infoPtr->mah.dwWidth);
499 TRACE("mah.dwHeight=%ld\n", infoPtr->mah.dwHeight);
501 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
503 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
504 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
505 WARN("Can't find 'strl' list\n");
506 return FALSE;
509 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
510 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
511 WARN("Can't find 'strh' chunk\n");
512 return FALSE;
515 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
517 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
518 HIBYTE(LOWORD(infoPtr->ash.fccType)),
519 LOBYTE(HIWORD(infoPtr->ash.fccType)),
520 HIBYTE(HIWORD(infoPtr->ash.fccType)));
521 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
522 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
523 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
524 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
525 TRACE("ash.dwFlags=%ld\n", infoPtr->ash.dwFlags);
526 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
527 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
528 TRACE("ash.dwInitialFrames=%ld\n", infoPtr->ash.dwInitialFrames);
529 TRACE("ash.dwScale=%ld\n", infoPtr->ash.dwScale);
530 TRACE("ash.dwRate=%ld\n", infoPtr->ash.dwRate);
531 TRACE("ash.dwStart=%ld\n", infoPtr->ash.dwStart);
532 TRACE("ash.dwLength=%ld\n", infoPtr->ash.dwLength);
533 TRACE("ash.dwSuggestedBufferSize=%ld\n", infoPtr->ash.dwSuggestedBufferSize);
534 TRACE("ash.dwQuality=%ld\n", infoPtr->ash.dwQuality);
535 TRACE("ash.dwSampleSize=%ld\n", infoPtr->ash.dwSampleSize);
536 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
537 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
539 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
541 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
542 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
543 WARN("Can't find 'strh' chunk\n");
544 return FALSE;
547 infoPtr->inbih = HeapAlloc(GetProcessHeap(), 0, mmckInfo.cksize);
548 if (!infoPtr->inbih) {
549 WARN("Can't alloc input BIH\n");
550 return FALSE;
553 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
555 TRACE("bih.biSize=%ld\n", infoPtr->inbih->biSize);
556 TRACE("bih.biWidth=%ld\n", infoPtr->inbih->biWidth);
557 TRACE("bih.biHeight=%ld\n", infoPtr->inbih->biHeight);
558 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
559 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
560 TRACE("bih.biCompression=%ld\n", infoPtr->inbih->biCompression);
561 TRACE("bih.biSizeImage=%ld\n", infoPtr->inbih->biSizeImage);
562 TRACE("bih.biXPelsPerMeter=%ld\n", infoPtr->inbih->biXPelsPerMeter);
563 TRACE("bih.biYPelsPerMeter=%ld\n", infoPtr->inbih->biYPelsPerMeter);
564 TRACE("bih.biClrUsed=%ld\n", infoPtr->inbih->biClrUsed);
565 TRACE("bih.biClrImportant=%ld\n", infoPtr->inbih->biClrImportant);
567 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
569 mmioAscend(infoPtr->hMMio, &mmckList, 0);
571 #if 0
572 /* an AVI has 0 or 1 video stream, and to be animated should not contain
573 * an audio stream, so only one strl is allowed
575 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
576 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
577 WARN("There should be a single 'strl' list\n");
578 return FALSE;
580 #endif
582 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
584 /* no need to read optional JUNK chunk */
586 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
587 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
588 WARN("Can't find 'movi' list\n");
589 return FALSE;
592 /* FIXME: should handle the 'rec ' LIST when present */
594 infoPtr->lpIndex = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
595 infoPtr->mah.dwTotalFrames * sizeof(DWORD));
596 if (!infoPtr->lpIndex) {
597 WARN("Can't alloc index array\n");
598 return FALSE;
601 numFrame = insize = 0;
602 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
603 numFrame < infoPtr->mah.dwTotalFrames) {
604 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
605 if (insize < mmckInfo.cksize)
606 insize = mmckInfo.cksize;
607 numFrame++;
608 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
610 if (numFrame != infoPtr->mah.dwTotalFrames) {
611 WARN("Found %ld frames (/%ld)\n", numFrame, infoPtr->mah.dwTotalFrames);
612 return FALSE;
614 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
615 WARN("insize=%ld suggestedSize=%ld\n", insize, infoPtr->ash.dwSuggestedBufferSize);
616 infoPtr->ash.dwSuggestedBufferSize = insize;
619 infoPtr->indata = HeapAlloc(GetProcessHeap(), 0, infoPtr->ash.dwSuggestedBufferSize);
620 if (!infoPtr->indata) {
621 WARN("Can't alloc input buffer\n");
622 return FALSE;
625 return TRUE;
629 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
631 DWORD outSize;
633 /* check uncompressed AVI */
634 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
635 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
636 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
638 infoPtr->hic = 0;
639 return TRUE;
642 /* try to get a decompressor for that type */
643 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
644 if (!infoPtr->hic) {
645 WARN("Can't load codec for the file\n");
646 return FALSE;
649 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
650 (DWORD)infoPtr->inbih, 0L);
652 infoPtr->outbih = HeapAlloc(GetProcessHeap(), 0, outSize);
653 if (!infoPtr->outbih) {
654 WARN("Can't alloc output BIH\n");
655 return FALSE;
658 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
659 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
660 WARN("Can't get output BIH\n");
661 return FALSE;
664 infoPtr->outdata = HeapAlloc(GetProcessHeap(), 0, infoPtr->outbih->biSizeImage);
665 if (!infoPtr->outdata) {
666 WARN("Can't alloc output buffer\n");
667 return FALSE;
670 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
671 (DWORD)infoPtr->inbih, (DWORD)infoPtr->outbih) != ICERR_OK) {
672 WARN("Can't begin decompression\n");
673 return FALSE;
676 return TRUE;
679 static LRESULT ANIMATE_OpenA(HWND hWnd, WPARAM wParam, LPARAM lParam)
681 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
682 HINSTANCE hInstance = (HINSTANCE)wParam;
684 ANIMATE_Free(infoPtr);
685 infoPtr->hWnd = hWnd;
687 if (!lParam) {
688 TRACE("Closing avi!\n");
689 return TRUE;
692 if (!hInstance)
693 hInstance = (HINSTANCE)GetWindowLongA(hWnd, GWL_HINSTANCE);
695 if (HIWORD(lParam)) {
696 TRACE("(\"%s\");\n", (LPSTR)lParam);
698 if (!ANIMATE_LoadResA(infoPtr, hInstance, (LPSTR)lParam)) {
699 TRACE("No AVI resource found!\n");
700 if (!ANIMATE_LoadFileA(infoPtr, (LPSTR)lParam)) {
701 WARN("No AVI file found!\n");
702 return FALSE;
705 } else {
706 TRACE("(%u);\n", (WORD)LOWORD(lParam));
708 if (!ANIMATE_LoadResA(infoPtr, hInstance,
709 MAKEINTRESOURCEA((INT)lParam))) {
710 WARN("No AVI resource found!\n");
711 return FALSE;
715 if (!ANIMATE_GetAviInfo(infoPtr)) {
716 WARN("Can't get AVI information\n");
717 ANIMATE_Free(infoPtr);
718 return FALSE;
721 if (!ANIMATE_GetAviCodec(infoPtr)) {
722 WARN("Can't get AVI Codec\n");
723 ANIMATE_Free(infoPtr);
724 return FALSE;
727 if (!GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
728 SetWindowPos(hWnd, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
729 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
732 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_AUTOPLAY) {
733 return ANIMATE_Play(hWnd, -1, (LPARAM)MAKELONG(0, infoPtr->mah.dwTotalFrames-1));
736 return TRUE;
740 /* << ANIMATE_Open32W >> */
742 static LRESULT ANIMATE_Stop(HWND hWnd, WPARAM wParam, LPARAM lParam)
744 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
746 /* nothing opened */
747 if (!infoPtr->hMMio)
748 return FALSE;
750 ANIMATE_DoStop(infoPtr);
751 return TRUE;
755 static LRESULT ANIMATE_Create(HWND hWnd, WPARAM wParam, LPARAM lParam)
757 ANIMATE_INFO* infoPtr;
759 if (!fnIC.hModule) /* FIXME: not thread safe */
761 /* since there's a circular dep between msvfw32 and comctl32, we could either:
762 * - fix the build chain to allow this circular dep
763 * - handle it by hand
764 * AJ wants the latter :-(
766 fnIC.hModule = LoadLibraryA("msvfw32.dll");
767 if (!fnIC.hModule) return FALSE;
769 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
770 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
771 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
772 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
775 /* allocate memory for info structure */
776 infoPtr = (ANIMATE_INFO *)COMCTL32_Alloc(sizeof(ANIMATE_INFO));
777 if (!infoPtr) {
778 ERR("could not allocate info memory!\n");
779 return 0;
782 TRACE("Animate style=0x%08lx, parent=%08lx\n", GetWindowLongA(hWnd, GWL_STYLE), (DWORD)GetParent(hWnd));
784 /* store crossref hWnd <-> info structure */
785 SetWindowLongA(hWnd, 0, (DWORD)infoPtr);
786 infoPtr->hWnd = hWnd;
787 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
788 infoPtr->hbmPrevFrame = 0;
790 InitializeCriticalSection(&infoPtr->cs);
792 return 0;
796 static LRESULT ANIMATE_Destroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
798 ANIMATE_INFO *infoPtr = ANIMATE_GetInfoPtr(hWnd);
801 /* free avi data */
802 ANIMATE_Free(infoPtr);
804 /* free animate info data */
805 COMCTL32_Free(infoPtr);
806 SetWindowLongA(hWnd, 0, 0);
808 return 0;
812 static LRESULT ANIMATE_EraseBackground(HWND hWnd, WPARAM wParam, LPARAM lParam)
814 RECT rect;
815 HBRUSH hBrush = 0;
817 if(GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
819 hBrush = (HBRUSH)SendMessageA(GetParent(hWnd),WM_CTLCOLORSTATIC,
820 wParam, (LPARAM)hWnd);
823 GetClientRect(hWnd, &rect);
824 FillRect((HDC)wParam, &rect, hBrush ? hBrush : GetCurrentObject((HDC)wParam, OBJ_BRUSH));
826 return TRUE;
829 static LRESULT WINAPI ANIMATE_Size(HWND hWnd, WPARAM wParam, LPARAM lParam)
831 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_CENTER) {
832 InvalidateRect(hWnd, NULL, TRUE);
834 return TRUE;
837 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
839 TRACE("hwnd=%p msg=%x wparam=%x lparam=%lx\n", hWnd, uMsg, wParam, lParam);
840 if (!ANIMATE_GetInfoPtr(hWnd) && (uMsg != WM_NCCREATE))
841 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
842 switch (uMsg)
844 case ACM_OPENA:
845 return ANIMATE_OpenA(hWnd, wParam, lParam);
847 /* case ACM_OPEN32W: FIXME!! */
848 /* return ANIMATE_Open32W(hWnd, wParam, lParam); */
850 case ACM_PLAY:
851 return ANIMATE_Play(hWnd, wParam, lParam);
853 case ACM_STOP:
854 return ANIMATE_Stop(hWnd, wParam, lParam);
856 case WM_NCCREATE:
857 ANIMATE_Create(hWnd, wParam, lParam);
858 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
860 case WM_NCHITTEST:
861 return HTTRANSPARENT;
863 case WM_DESTROY:
864 ANIMATE_Destroy(hWnd, wParam, lParam);
865 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
867 case WM_ERASEBKGND:
868 ANIMATE_EraseBackground(hWnd, wParam, lParam);
869 break;
871 /* case WM_STYLECHANGED: FIXME shall we do something ?? */
873 case WM_TIMER:
874 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
876 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
877 infoPtr->hbrushBG = (HBRUSH)SendMessageA(GetParent(hWnd),
878 WM_CTLCOLORSTATIC,
879 wParam, (LPARAM)hWnd);
881 return ANIMATE_DrawFrame(ANIMATE_GetInfoPtr(hWnd));
883 case WM_CLOSE:
884 ANIMATE_Free(ANIMATE_GetInfoPtr(hWnd));
885 return TRUE;
887 case WM_PAINT:
889 ANIMATE_INFO* infoPtr = ANIMATE_GetInfoPtr(hWnd);
891 /* the animation isn't playing, don't paint */
892 if(!infoPtr->uTimer && !infoPtr->hThread)
893 /* default paint handling */
894 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
896 if (GetWindowLongA(hWnd, GWL_STYLE) & ACS_TRANSPARENT)
897 infoPtr->hbrushBG = (HBRUSH)SendMessageA(GetParent(hWnd),
898 WM_CTLCOLORSTATIC,
899 wParam, (LPARAM)hWnd);
901 if (wParam)
903 EnterCriticalSection(&infoPtr->cs);
904 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
905 LeaveCriticalSection(&infoPtr->cs);
907 else
909 PAINTSTRUCT ps;
910 HDC hDC = BeginPaint(hWnd, &ps);
912 EnterCriticalSection(&infoPtr->cs);
913 ANIMATE_PaintFrame(infoPtr, hDC);
914 LeaveCriticalSection(&infoPtr->cs);
916 EndPaint(hWnd, &ps);
919 break;
921 case WM_SIZE:
922 ANIMATE_Size(hWnd, wParam, lParam);
923 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
925 default:
926 if ((uMsg >= WM_USER) && (uMsg < WM_APP))
927 ERR("unknown msg %04x wp=%08x lp=%08lx\n", uMsg, wParam, lParam);
929 return DefWindowProcA(hWnd, uMsg, wParam, lParam);
931 return 0;
934 void ANIMATE_Register(void)
936 WNDCLASSA wndClass;
938 ZeroMemory(&wndClass, sizeof(WNDCLASSA));
939 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
940 wndClass.lpfnWndProc = (WNDPROC)ANIMATE_WindowProc;
941 wndClass.cbClsExtra = 0;
942 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
943 wndClass.hCursor = LoadCursorA(0, IDC_ARROWA);
944 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
945 wndClass.lpszClassName = ANIMATE_CLASSA;
947 RegisterClassA(&wndClass);
951 void ANIMATE_Unregister(void)
953 UnregisterClassA(ANIMATE_CLASSA, (HINSTANCE)NULL);