Sync with trunk r63174.
[reactos.git] / dll / win32 / comctl32 / animate.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * Animation control
4 *
5 * Copyright 1998, 1999 Eric Kohl
6 * Copyright 1999 Eric Pouech
7 * Copyright 2005 Dimitrie O. Paun
8 *
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.
13 *
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.
18 *
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
22 *
23 * NOTES
24 *
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.
27 *
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.
31 *
32 * TODO:
33 * - check for the 'rec ' list in some AVI files
34 */
35
36 #include "comctl32.h"
37
38 #include <vfw.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(animate);
41
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;
49
50 typedef struct
51 {
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;
84
85 #define ANIMATE_COLOR_NONE 0xffffffff
86
87 static void ANIMATE_Notify(const ANIMATE_INFO *infoPtr, UINT notif)
88 {
89 PostMessageW(infoPtr->hwndNotify, WM_COMMAND,
90 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
91 (LPARAM)infoPtr->hwndSelf);
92 }
93
94 static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPCWSTR lpName)
95 {
96 static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
97 HRSRC hrsrc;
98 MMIOINFO mminfo;
99 LPVOID lpAvi;
100
101 hrsrc = FindResourceW(hInst, lpName, aviW);
102 if (!hrsrc)
103 return FALSE;
104
105 infoPtr->hRes = LoadResource(hInst, hrsrc);
106 if (!infoPtr->hRes)
107 return FALSE;
108
109 lpAvi = LockResource(infoPtr->hRes);
110 if (!lpAvi)
111 return FALSE;
112
113 memset(&mminfo, 0, sizeof(mminfo));
114 mminfo.fccIOProc = FOURCC_MEM;
115 mminfo.pchBuffer = lpAvi;
116 mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
117 infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
118 if (!infoPtr->hMMio)
119 {
120 FreeResource(infoPtr->hRes);
121 return FALSE;
122 }
123
124 return TRUE;
125 }
126
127
128 static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
129 {
130 infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
131
132 if(!infoPtr->hMMio) return FALSE;
133 return TRUE;
134 }
135
136
137 static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
138 {
139 BOOL stopped = FALSE;
140
141 EnterCriticalSection(&infoPtr->cs);
142
143 /* should stop playing */
144 if (infoPtr->hThread)
145 {
146 HANDLE handle = infoPtr->hThread;
147
148 TRACE("stopping animation thread\n");
149 infoPtr->hThread = 0;
150 SetEvent( infoPtr->hStopEvent );
151
152 if (infoPtr->threadId != GetCurrentThreadId())
153 {
154 LeaveCriticalSection(&infoPtr->cs); /* leave it a chance to run */
155 WaitForSingleObject( handle, INFINITE );
156 TRACE("animation thread stopped\n");
157 EnterCriticalSection(&infoPtr->cs);
158 }
159
160 CloseHandle( handle );
161 CloseHandle( infoPtr->hStopEvent );
162 infoPtr->hStopEvent = 0;
163 stopped = TRUE;
164 }
165 if (infoPtr->uTimer) {
166 KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
167 infoPtr->uTimer = 0;
168 stopped = TRUE;
169 }
170
171 LeaveCriticalSection(&infoPtr->cs);
172
173 if (stopped)
174 ANIMATE_Notify(infoPtr, ACN_STOP);
175
176 return TRUE;
177 }
178
179
180 static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
181 {
182 if (infoPtr->hMMio) {
183 ANIMATE_DoStop(infoPtr);
184 mmioClose(infoPtr->hMMio, 0);
185 if (infoPtr->hRes) {
186 FreeResource(infoPtr->hRes);
187 infoPtr->hRes = 0;
188 }
189 Free (infoPtr->lpIndex);
190 infoPtr->lpIndex = NULL;
191 if (infoPtr->hic) {
192 fnIC.fnICClose(infoPtr->hic);
193 infoPtr->hic = 0;
194 }
195 Free (infoPtr->inbih);
196 infoPtr->inbih = NULL;
197 Free (infoPtr->outbih);
198 infoPtr->outbih = NULL;
199 Free (infoPtr->indata);
200 infoPtr->indata = NULL;
201 Free (infoPtr->outdata);
202 infoPtr->outdata = NULL;
203 if( infoPtr->hbmPrevFrame )
204 {
205 DeleteObject(infoPtr->hbmPrevFrame);
206 infoPtr->hbmPrevFrame = 0;
207 }
208
209 memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
210 memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
211 infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
212 }
213 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
214 }
215
216 static void ANIMATE_TransparentBlt(ANIMATE_INFO const *infoPtr, HDC hdcDest, HDC hdcSource)
217 {
218 HDC hdcMask;
219 HBITMAP hbmMask;
220 HBITMAP hbmOld;
221
222 /* create a transparency mask */
223 hdcMask = CreateCompatibleDC(hdcDest);
224 hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
225 hbmOld = SelectObject(hdcMask, hbmMask);
226
227 SetBkColor(hdcSource,infoPtr->transparentColor);
228 BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
229
230 /* mask the source bitmap */
231 SetBkColor(hdcSource, RGB(0,0,0));
232 SetTextColor(hdcSource, RGB(255,255,255));
233 BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
234
235 /* mask the destination bitmap */
236 SetBkColor(hdcDest, RGB(255,255,255));
237 SetTextColor(hdcDest, RGB(0,0,0));
238 BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);
239
240 /* combine source and destination */
241 BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);
242
243 SelectObject(hdcMask, hbmOld);
244 DeleteObject(hbmMask);
245 DeleteDC(hdcMask);
246 }
247
248 static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
249 {
250 void const *pBitmapData;
251 BITMAPINFO const *pBitmapInfo;
252 HDC hdcMem;
253 HBITMAP hbmOld;
254 int nOffsetX = 0;
255 int nOffsetY = 0;
256 int nWidth;
257 int nHeight;
258
259 if (!hDC || !infoPtr->inbih)
260 return TRUE;
261
262 if (infoPtr->hic )
263 {
264 pBitmapData = infoPtr->outdata;
265 pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
266
267 nWidth = infoPtr->outbih->biWidth;
268 nHeight = infoPtr->outbih->biHeight;
269 }
270 else
271 {
272 pBitmapData = infoPtr->indata;
273 pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
274
275 nWidth = infoPtr->inbih->biWidth;
276 nHeight = infoPtr->inbih->biHeight;
277 }
278
279 if(!infoPtr->hbmPrevFrame)
280 {
281 infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
282 }
283
284 hdcMem = CreateCompatibleDC(hDC);
285 hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
286
287 SetDIBits(hdcMem, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, pBitmapInfo, DIB_RGB_COLORS);
288
289 /*
290 * we need to get the transparent color even without ACS_TRANSPARENT,
291 * because the style can be changed later on and the color should always
292 * be obtained in the first frame
293 */
294 if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
295 {
296 infoPtr->transparentColor = GetPixel(hdcMem,0,0);
297 }
298
299 if(infoPtr->dwStyle & ACS_TRANSPARENT)
300 {
301 HDC hdcFinal = CreateCompatibleDC(hDC);
302 HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
303 HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
304 RECT rect;
305
306 rect.left = 0;
307 rect.top = 0;
308 rect.right = nWidth;
309 rect.bottom = nHeight;
310
311 if(!infoPtr->hbrushBG)
312 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
313
314 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
315 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
316
317 SelectObject(hdcFinal, hbmOld2);
318 SelectObject(hdcMem, hbmFinal);
319 DeleteDC(hdcFinal);
320 DeleteObject(infoPtr->hbmPrevFrame);
321 infoPtr->hbmPrevFrame = hbmFinal;
322 }
323
324 if (infoPtr->dwStyle & ACS_CENTER)
325 {
326 RECT rect;
327
328 GetWindowRect(infoPtr->hwndSelf, &rect);
329 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
330 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
331 }
332 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
333
334 SelectObject(hdcMem, hbmOld);
335 DeleteDC(hdcMem);
336 return TRUE;
337 }
338
339 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr, HDC hDC)
340 {
341 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
342
343 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
344 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
345
346 if (infoPtr->hic &&
347 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
348 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
349 WARN("Decompression error\n");
350 return FALSE;
351 }
352
353 ANIMATE_PaintFrame(infoPtr, hDC);
354
355 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
356 infoPtr->currFrame = infoPtr->nFromFrame;
357 if (infoPtr->nLoop != -1) {
358 if (--infoPtr->nLoop == 0) {
359 ANIMATE_DoStop(infoPtr);
360 }
361 }
362 }
363
364 return TRUE;
365 }
366
367 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
368 {
369 HDC hDC;
370
371 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
372 {
373 EnterCriticalSection(&infoPtr->cs);
374 ANIMATE_DrawFrame(infoPtr, hDC);
375 LeaveCriticalSection(&infoPtr->cs);
376
377 ReleaseDC(infoPtr->hwndSelf, hDC);
378 }
379
380 return 0;
381 }
382
383 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
384 {
385 ANIMATE_INFO *infoPtr = ptr_;
386 HANDLE event;
387 DWORD timeout;
388
389 while(1)
390 {
391 HDC hDC = GetDC(infoPtr->hwndSelf);
392
393 EnterCriticalSection(&infoPtr->cs);
394 ANIMATE_DrawFrame(infoPtr, hDC);
395 timeout = infoPtr->mah.dwMicroSecPerFrame;
396 event = infoPtr->hStopEvent;
397 LeaveCriticalSection(&infoPtr->cs);
398
399 ReleaseDC(infoPtr->hwndSelf, hDC);
400
401 /* time is in microseconds, we should convert it to milliseconds */
402 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
403 break;
404 }
405 return TRUE;
406 }
407
408 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
409 {
410 /* nothing opened */
411 if (!infoPtr->hMMio)
412 return FALSE;
413
414 if (infoPtr->hThread || infoPtr->uTimer) {
415 TRACE("Already playing\n");
416 return TRUE;
417 }
418
419 infoPtr->nFromFrame = wFrom;
420 infoPtr->nToFrame = wTo;
421 infoPtr->nLoop = cRepeat;
422
423 if (infoPtr->nToFrame == 0xFFFF)
424 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
425
426 TRACE("(repeat=%d from=%d to=%d);\n",
427 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
428
429 if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
430 (SHORT)infoPtr->nFromFrame < 0)
431 infoPtr->nFromFrame = 0;
432
433 if (infoPtr->nFromFrame > infoPtr->nToFrame ||
434 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
435 return FALSE;
436
437 infoPtr->currFrame = infoPtr->nFromFrame;
438
439 /* seek - doesn't need to start a thread or set a timer and neither
440 * does it send a notification */
441 if (infoPtr->nFromFrame == infoPtr->nToFrame)
442 {
443 HDC hDC;
444
445 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
446 {
447 ANIMATE_DrawFrame(infoPtr, hDC);
448
449 ReleaseDC(infoPtr->hwndSelf, hDC);
450 }
451 return TRUE;
452 }
453
454 if (infoPtr->dwStyle & ACS_TIMER)
455 {
456 TRACE("Using a timer\n");
457 /* create a timer to display AVI */
458 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
459 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
460 }
461 else
462 {
463 TRACE("Using an animation thread\n");
464 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
465 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
466 infoPtr, 0, &infoPtr->threadId);
467 if(!infoPtr->hThread) return FALSE;
468
469 }
470
471 ANIMATE_Notify(infoPtr, ACN_START);
472
473 return TRUE;
474 }
475
476
477 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
478 {
479 MMCKINFO ckMainRIFF;
480 MMCKINFO mmckHead;
481 MMCKINFO mmckList;
482 MMCKINFO mmckInfo;
483 DWORD numFrame;
484 DWORD insize;
485
486 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
487 WARN("Can't find 'RIFF' chunk\n");
488 return FALSE;
489 }
490
491 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
492 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
493 WARN("Can't find 'AVI ' chunk\n");
494 return FALSE;
495 }
496
497 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
498 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
499 WARN("Can't find 'hdrl' list\n");
500 return FALSE;
501 }
502
503 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
504 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
505 WARN("Can't find 'avih' chunk\n");
506 return FALSE;
507 }
508
509 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
510
511 TRACE("mah.dwMicroSecPerFrame=%d\n", infoPtr->mah.dwMicroSecPerFrame);
512 TRACE("mah.dwMaxBytesPerSec=%d\n", infoPtr->mah.dwMaxBytesPerSec);
513 TRACE("mah.dwPaddingGranularity=%d\n", infoPtr->mah.dwPaddingGranularity);
514 TRACE("mah.dwFlags=%d\n", infoPtr->mah.dwFlags);
515 TRACE("mah.dwTotalFrames=%d\n", infoPtr->mah.dwTotalFrames);
516 TRACE("mah.dwInitialFrames=%d\n", infoPtr->mah.dwInitialFrames);
517 TRACE("mah.dwStreams=%d\n", infoPtr->mah.dwStreams);
518 TRACE("mah.dwSuggestedBufferSize=%d\n", infoPtr->mah.dwSuggestedBufferSize);
519 TRACE("mah.dwWidth=%d\n", infoPtr->mah.dwWidth);
520 TRACE("mah.dwHeight=%d\n", infoPtr->mah.dwHeight);
521
522 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
523
524 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
525 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
526 WARN("Can't find 'strl' list\n");
527 return FALSE;
528 }
529
530 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
531 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
532 WARN("Can't find 'strh' chunk\n");
533 return FALSE;
534 }
535
536 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
537
538 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
539 HIBYTE(LOWORD(infoPtr->ash.fccType)),
540 LOBYTE(HIWORD(infoPtr->ash.fccType)),
541 HIBYTE(HIWORD(infoPtr->ash.fccType)));
542 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
543 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
544 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
545 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
546 TRACE("ash.dwFlags=%d\n", infoPtr->ash.dwFlags);
547 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
548 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
549 TRACE("ash.dwInitialFrames=%d\n", infoPtr->ash.dwInitialFrames);
550 TRACE("ash.dwScale=%d\n", infoPtr->ash.dwScale);
551 TRACE("ash.dwRate=%d\n", infoPtr->ash.dwRate);
552 TRACE("ash.dwStart=%d\n", infoPtr->ash.dwStart);
553 TRACE("ash.dwLength=%d\n", infoPtr->ash.dwLength);
554 TRACE("ash.dwSuggestedBufferSize=%d\n", infoPtr->ash.dwSuggestedBufferSize);
555 TRACE("ash.dwQuality=%d\n", infoPtr->ash.dwQuality);
556 TRACE("ash.dwSampleSize=%d\n", infoPtr->ash.dwSampleSize);
557 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
558 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
559
560 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
561
562 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
563 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
564 WARN("Can't find 'strh' chunk\n");
565 return FALSE;
566 }
567
568 infoPtr->inbih = Alloc(mmckInfo.cksize);
569 if (!infoPtr->inbih) {
570 WARN("Can't alloc input BIH\n");
571 return FALSE;
572 }
573
574 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
575
576 TRACE("bih.biSize=%d\n", infoPtr->inbih->biSize);
577 TRACE("bih.biWidth=%d\n", infoPtr->inbih->biWidth);
578 TRACE("bih.biHeight=%d\n", infoPtr->inbih->biHeight);
579 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
580 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
581 TRACE("bih.biCompression=%d\n", infoPtr->inbih->biCompression);
582 TRACE("bih.biSizeImage=%d\n", infoPtr->inbih->biSizeImage);
583 TRACE("bih.biXPelsPerMeter=%d\n", infoPtr->inbih->biXPelsPerMeter);
584 TRACE("bih.biYPelsPerMeter=%d\n", infoPtr->inbih->biYPelsPerMeter);
585 TRACE("bih.biClrUsed=%d\n", infoPtr->inbih->biClrUsed);
586 TRACE("bih.biClrImportant=%d\n", infoPtr->inbih->biClrImportant);
587
588 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
589
590 mmioAscend(infoPtr->hMMio, &mmckList, 0);
591
592 #if 0
593 /* an AVI has 0 or 1 video stream, and to be animated should not contain
594 * an audio stream, so only one strl is allowed
595 */
596 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
597 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
598 WARN("There should be a single 'strl' list\n");
599 return FALSE;
600 }
601 #endif
602
603 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
604
605 /* no need to read optional JUNK chunk */
606
607 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
608 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
609 WARN("Can't find 'movi' list\n");
610 return FALSE;
611 }
612
613 /* FIXME: should handle the 'rec ' LIST when present */
614
615 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
616 if (!infoPtr->lpIndex)
617 return FALSE;
618
619 numFrame = insize = 0;
620 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
621 numFrame < infoPtr->mah.dwTotalFrames) {
622 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
623 if (insize < mmckInfo.cksize)
624 insize = mmckInfo.cksize;
625 numFrame++;
626 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
627 }
628 if (numFrame != infoPtr->mah.dwTotalFrames) {
629 WARN("Found %d frames (/%d)\n", numFrame, infoPtr->mah.dwTotalFrames);
630 return FALSE;
631 }
632 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
633 WARN("insize=%d suggestedSize=%d\n", insize, infoPtr->ash.dwSuggestedBufferSize);
634 infoPtr->ash.dwSuggestedBufferSize = insize;
635 }
636
637 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
638 if (!infoPtr->indata)
639 return FALSE;
640
641 return TRUE;
642 }
643
644
645 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
646 {
647 DWORD outSize;
648
649 /* check uncompressed AVI */
650 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
651 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
652 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
653 {
654 infoPtr->hic = 0;
655 return TRUE;
656 }
657
658 /* try to get a decompressor for that type */
659 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
660 if (!infoPtr->hic) {
661 WARN("Can't load codec for the file\n");
662 return FALSE;
663 }
664
665 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
666 (DWORD_PTR)infoPtr->inbih, 0L);
667
668 infoPtr->outbih = Alloc(outSize);
669 if (!infoPtr->outbih)
670 return FALSE;
671
672 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
673 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
674 {
675 WARN("Can't get output BIH\n");
676 return FALSE;
677 }
678
679 infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
680 if (!infoPtr->outdata)
681 return FALSE;
682
683 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
684 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
685 WARN("Can't begin decompression\n");
686 return FALSE;
687 }
688
689 return TRUE;
690 }
691
692
693 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
694 {
695 HDC hdc;
696
697 ANIMATE_Free(infoPtr);
698
699 if (!lpszName)
700 {
701 TRACE("Closing avi!\n");
702 /* installer of thebat! v1.62 requires FALSE here */
703 return (infoPtr->hMMio != 0);
704 }
705
706 if (!hInstance)
707 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
708
709 TRACE("(%s)\n", debugstr_w(lpszName));
710
711 if (!IS_INTRESOURCE(lpszName))
712 {
713 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
714 {
715 TRACE("No AVI resource found!\n");
716 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
717 {
718 WARN("No AVI file found!\n");
719 return FALSE;
720 }
721 }
722 }
723 else
724 {
725 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
726 {
727 WARN("No AVI resource found!\n");
728 return FALSE;
729 }
730 }
731
732 if (!ANIMATE_GetAviInfo(infoPtr))
733 {
734 WARN("Can't get AVI information\n");
735 ANIMATE_Free(infoPtr);
736 return FALSE;
737 }
738
739 if (!ANIMATE_GetAviCodec(infoPtr))
740 {
741 WARN("Can't get AVI Codec\n");
742 ANIMATE_Free(infoPtr);
743 return FALSE;
744 }
745
746 hdc = GetDC(infoPtr->hwndSelf);
747 /* native looks at the top left pixel of the first frame here too. */
748 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
749 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
750 ReleaseDC(infoPtr->hwndSelf, hdc);
751
752 if (!(infoPtr->dwStyle & ACS_CENTER))
753 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
754 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
755
756 if (infoPtr->dwStyle & ACS_AUTOPLAY)
757 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
758
759 return TRUE;
760 }
761
762
763 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
764 {
765 LPWSTR lpwszName;
766 LRESULT result;
767 INT len;
768
769 if (IS_INTRESOURCE(lpszName))
770 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
771
772 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
773 lpwszName = Alloc(len * sizeof(WCHAR));
774 if (!lpwszName) return FALSE;
775 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
776
777 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
778 Free (lpwszName);
779 return result;
780 }
781
782
783 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
784 {
785 /* nothing opened */
786 if (!infoPtr->hMMio)
787 return FALSE;
788
789 ANIMATE_DoStop(infoPtr);
790 return TRUE;
791 }
792
793
794 static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
795 {
796 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
797 ANIMATE_INFO *infoPtr;
798
799 if (!fnIC.hModule)
800 {
801 fnIC.hModule = LoadLibraryW(msvfw32W);
802 if (!fnIC.hModule) return FALSE;
803
804 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
805 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
806 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
807 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
808 }
809
810 /* allocate memory for info structure */
811 infoPtr = Alloc(sizeof(ANIMATE_INFO));
812 if (!infoPtr) return FALSE;
813
814 /* store crossref hWnd <-> info structure */
815 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
816 infoPtr->hwndSelf = hWnd;
817 infoPtr->hwndNotify = lpcs->hwndParent;
818 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
819 infoPtr->hbmPrevFrame = 0;
820 infoPtr->dwStyle = lpcs->style;
821
822 TRACE("Animate style=0x%08x, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
823
824 InitializeCriticalSection(&infoPtr->cs);
825 infoPtr->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ANIMATE_INFO*->cs");
826
827 return TRUE;
828 }
829
830
831 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
832 {
833 /* free avi data */
834 ANIMATE_Free(infoPtr);
835
836 /* free animate info data */
837 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
838
839 infoPtr->cs.DebugInfo->Spare[0] = 0;
840 DeleteCriticalSection(&infoPtr->cs);
841 Free(infoPtr);
842
843 return 0;
844 }
845
846
847 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
848 {
849 RECT rect;
850 HBRUSH hBrush;
851
852 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
853 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
854 GetClientRect(infoPtr->hwndSelf, &rect);
855 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
856
857 return TRUE;
858 }
859
860
861 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
862 {
863 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
864 wStyleType, lpss->styleOld, lpss->styleNew);
865
866 if (wStyleType != GWL_STYLE) return 0;
867
868 infoPtr->dwStyle = lpss->styleNew;
869
870 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
871 return 0;
872 }
873
874
875 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
876 {
877 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
878
879 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hWnd, uMsg, wParam, lParam);
880 if (!infoPtr && (uMsg != WM_NCCREATE))
881 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
882 switch (uMsg)
883 {
884 case ACM_OPENA:
885 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
886
887 case ACM_OPENW:
888 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
889
890 case ACM_PLAY:
891 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
892
893 case ACM_STOP:
894 return ANIMATE_Stop(infoPtr);
895
896 case WM_CLOSE:
897 ANIMATE_Free(infoPtr);
898 return 0;
899
900 case WM_NCCREATE:
901 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
902
903 case WM_NCHITTEST:
904 return HTTRANSPARENT;
905
906 case WM_DESTROY:
907 return ANIMATE_Destroy(infoPtr);
908
909 case WM_ERASEBKGND:
910 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
911
912 case WM_STYLECHANGED:
913 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
914
915 case WM_TIMER:
916 return ANIMATE_Timer(infoPtr);
917
918 case WM_PRINTCLIENT:
919 case WM_PAINT:
920 {
921 /* the animation has not decompressed
922 * (and displayed) the first frame yet, don't paint
923 */
924 if (!infoPtr->hbmPrevFrame)
925 {
926 /* default paint handling */
927 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
928 }
929
930 if (wParam)
931 {
932 EnterCriticalSection(&infoPtr->cs);
933 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
934 LeaveCriticalSection(&infoPtr->cs);
935 }
936 else
937 {
938 PAINTSTRUCT ps;
939 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
940
941 EnterCriticalSection(&infoPtr->cs);
942 ANIMATE_PaintFrame(infoPtr, hDC);
943 LeaveCriticalSection(&infoPtr->cs);
944
945 EndPaint(infoPtr->hwndSelf, &ps);
946 }
947 }
948 break;
949
950 case WM_SIZE:
951 if (infoPtr->dwStyle & ACS_CENTER)
952 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
953 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
954
955 default:
956 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
957 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
958
959 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
960 }
961 return 0;
962 }
963
964 void ANIMATE_Register(void)
965 {
966 WNDCLASSW wndClass;
967
968 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
969 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
970 wndClass.lpfnWndProc = ANIMATE_WindowProc;
971 wndClass.cbClsExtra = 0;
972 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
973 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
974 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
975 wndClass.lpszClassName = ANIMATE_CLASSW;
976
977 RegisterClassW(&wndClass);
978 }
979
980
981 void ANIMATE_Unregister(void)
982 {
983 UnregisterClassW(ANIMATE_CLASSW, NULL);
984 }