[NTDLL_APITEST] Add several tests for RtlDosApplyFileIsolationRedirection_Ustr
[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 SetRect(&rect, 0, 0, nWidth, nHeight);
307
308 if(!infoPtr->hbrushBG)
309 infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);
310
311 FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
312 ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);
313
314 SelectObject(hdcFinal, hbmOld2);
315 SelectObject(hdcMem, hbmFinal);
316 DeleteDC(hdcFinal);
317 DeleteObject(infoPtr->hbmPrevFrame);
318 infoPtr->hbmPrevFrame = hbmFinal;
319 }
320
321 if (infoPtr->dwStyle & ACS_CENTER)
322 {
323 RECT rect;
324
325 GetWindowRect(infoPtr->hwndSelf, &rect);
326 nOffsetX = ((rect.right - rect.left) - nWidth)/2;
327 nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
328 }
329 BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
330
331 SelectObject(hdcMem, hbmOld);
332 DeleteDC(hdcMem);
333 return TRUE;
334 }
335
336 static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr, HDC hDC)
337 {
338 TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);
339
340 mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
341 mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
342
343 if (infoPtr->hic &&
344 fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
345 infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
346 WARN("Decompression error\n");
347 return FALSE;
348 }
349
350 ANIMATE_PaintFrame(infoPtr, hDC);
351
352 if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
353 infoPtr->currFrame = infoPtr->nFromFrame;
354 if (infoPtr->nLoop != -1) {
355 if (--infoPtr->nLoop == 0) {
356 ANIMATE_DoStop(infoPtr);
357 }
358 }
359 }
360
361 return TRUE;
362 }
363
364 static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
365 {
366 HDC hDC;
367
368 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
369 {
370 EnterCriticalSection(&infoPtr->cs);
371 ANIMATE_DrawFrame(infoPtr, hDC);
372 LeaveCriticalSection(&infoPtr->cs);
373
374 ReleaseDC(infoPtr->hwndSelf, hDC);
375 }
376
377 return 0;
378 }
379
380 static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
381 {
382 ANIMATE_INFO *infoPtr = ptr_;
383 HANDLE event;
384 DWORD timeout;
385
386 while(1)
387 {
388 HDC hDC = GetDC(infoPtr->hwndSelf);
389
390 EnterCriticalSection(&infoPtr->cs);
391 ANIMATE_DrawFrame(infoPtr, hDC);
392 timeout = infoPtr->mah.dwMicroSecPerFrame;
393 event = infoPtr->hStopEvent;
394 LeaveCriticalSection(&infoPtr->cs);
395
396 ReleaseDC(infoPtr->hwndSelf, hDC);
397
398 /* time is in microseconds, we should convert it to milliseconds */
399 if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
400 break;
401 }
402 return TRUE;
403 }
404
405 static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
406 {
407 /* nothing opened */
408 if (!infoPtr->hMMio)
409 return FALSE;
410
411 if (infoPtr->hThread || infoPtr->uTimer) {
412 TRACE("Already playing\n");
413 return TRUE;
414 }
415
416 infoPtr->nFromFrame = wFrom;
417 infoPtr->nToFrame = wTo;
418 infoPtr->nLoop = cRepeat;
419
420 if (infoPtr->nToFrame == 0xFFFF)
421 infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;
422
423 TRACE("(repeat=%d from=%d to=%d);\n",
424 infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);
425
426 if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
427 (SHORT)infoPtr->nFromFrame < 0)
428 infoPtr->nFromFrame = 0;
429
430 if (infoPtr->nFromFrame > infoPtr->nToFrame ||
431 infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
432 return FALSE;
433
434 infoPtr->currFrame = infoPtr->nFromFrame;
435
436 /* seek - doesn't need to start a thread or set a timer and neither
437 * does it send a notification */
438 if (infoPtr->nFromFrame == infoPtr->nToFrame)
439 {
440 HDC hDC;
441
442 if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
443 {
444 ANIMATE_DrawFrame(infoPtr, hDC);
445
446 ReleaseDC(infoPtr->hwndSelf, hDC);
447 }
448 return TRUE;
449 }
450
451 if (infoPtr->dwStyle & ACS_TIMER)
452 {
453 TRACE("Using a timer\n");
454 /* create a timer to display AVI */
455 infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1,
456 infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
457 }
458 else
459 {
460 TRACE("Using an animation thread\n");
461 infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
462 infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
463 infoPtr, 0, &infoPtr->threadId);
464 if(!infoPtr->hThread) return FALSE;
465
466 }
467
468 ANIMATE_Notify(infoPtr, ACN_START);
469
470 return TRUE;
471 }
472
473
474 static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
475 {
476 MMCKINFO ckMainRIFF;
477 MMCKINFO mmckHead;
478 MMCKINFO mmckList;
479 MMCKINFO mmckInfo;
480 DWORD numFrame;
481 DWORD insize;
482
483 if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
484 WARN("Can't find 'RIFF' chunk\n");
485 return FALSE;
486 }
487
488 if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
489 (ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
490 WARN("Can't find 'AVI ' chunk\n");
491 return FALSE;
492 }
493
494 mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
495 if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
496 WARN("Can't find 'hdrl' list\n");
497 return FALSE;
498 }
499
500 mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
501 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
502 WARN("Can't find 'avih' chunk\n");
503 return FALSE;
504 }
505
506 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));
507
508 TRACE("mah.dwMicroSecPerFrame=%d\n", infoPtr->mah.dwMicroSecPerFrame);
509 TRACE("mah.dwMaxBytesPerSec=%d\n", infoPtr->mah.dwMaxBytesPerSec);
510 TRACE("mah.dwPaddingGranularity=%d\n", infoPtr->mah.dwPaddingGranularity);
511 TRACE("mah.dwFlags=%d\n", infoPtr->mah.dwFlags);
512 TRACE("mah.dwTotalFrames=%d\n", infoPtr->mah.dwTotalFrames);
513 TRACE("mah.dwInitialFrames=%d\n", infoPtr->mah.dwInitialFrames);
514 TRACE("mah.dwStreams=%d\n", infoPtr->mah.dwStreams);
515 TRACE("mah.dwSuggestedBufferSize=%d\n", infoPtr->mah.dwSuggestedBufferSize);
516 TRACE("mah.dwWidth=%d\n", infoPtr->mah.dwWidth);
517 TRACE("mah.dwHeight=%d\n", infoPtr->mah.dwHeight);
518
519 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
520
521 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
522 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
523 WARN("Can't find 'strl' list\n");
524 return FALSE;
525 }
526
527 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
528 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
529 WARN("Can't find 'strh' chunk\n");
530 return FALSE;
531 }
532
533 mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));
534
535 TRACE("ash.fccType='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccType)),
536 HIBYTE(LOWORD(infoPtr->ash.fccType)),
537 LOBYTE(HIWORD(infoPtr->ash.fccType)),
538 HIBYTE(HIWORD(infoPtr->ash.fccType)));
539 TRACE("ash.fccHandler='%c%c%c%c'\n", LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
540 HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
541 LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
542 HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
543 TRACE("ash.dwFlags=%d\n", infoPtr->ash.dwFlags);
544 TRACE("ash.wPriority=%d\n", infoPtr->ash.wPriority);
545 TRACE("ash.wLanguage=%d\n", infoPtr->ash.wLanguage);
546 TRACE("ash.dwInitialFrames=%d\n", infoPtr->ash.dwInitialFrames);
547 TRACE("ash.dwScale=%d\n", infoPtr->ash.dwScale);
548 TRACE("ash.dwRate=%d\n", infoPtr->ash.dwRate);
549 TRACE("ash.dwStart=%d\n", infoPtr->ash.dwStart);
550 TRACE("ash.dwLength=%d\n", infoPtr->ash.dwLength);
551 TRACE("ash.dwSuggestedBufferSize=%d\n", infoPtr->ash.dwSuggestedBufferSize);
552 TRACE("ash.dwQuality=%d\n", infoPtr->ash.dwQuality);
553 TRACE("ash.dwSampleSize=%d\n", infoPtr->ash.dwSampleSize);
554 TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
555 infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
556
557 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
558
559 mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
560 if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
561 WARN("Can't find 'strh' chunk\n");
562 return FALSE;
563 }
564
565 infoPtr->inbih = Alloc(mmckInfo.cksize);
566 if (!infoPtr->inbih) {
567 WARN("Can't alloc input BIH\n");
568 return FALSE;
569 }
570
571 mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);
572
573 TRACE("bih.biSize=%d\n", infoPtr->inbih->biSize);
574 TRACE("bih.biWidth=%d\n", infoPtr->inbih->biWidth);
575 TRACE("bih.biHeight=%d\n", infoPtr->inbih->biHeight);
576 TRACE("bih.biPlanes=%d\n", infoPtr->inbih->biPlanes);
577 TRACE("bih.biBitCount=%d\n", infoPtr->inbih->biBitCount);
578 TRACE("bih.biCompression=%d\n", infoPtr->inbih->biCompression);
579 TRACE("bih.biSizeImage=%d\n", infoPtr->inbih->biSizeImage);
580 TRACE("bih.biXPelsPerMeter=%d\n", infoPtr->inbih->biXPelsPerMeter);
581 TRACE("bih.biYPelsPerMeter=%d\n", infoPtr->inbih->biYPelsPerMeter);
582 TRACE("bih.biClrUsed=%d\n", infoPtr->inbih->biClrUsed);
583 TRACE("bih.biClrImportant=%d\n", infoPtr->inbih->biClrImportant);
584
585 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
586
587 mmioAscend(infoPtr->hMMio, &mmckList, 0);
588
589 #if 0
590 /* an AVI has 0 or 1 video stream, and to be animated should not contain
591 * an audio stream, so only one strl is allowed
592 */
593 mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
594 if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
595 WARN("There should be a single 'strl' list\n");
596 return FALSE;
597 }
598 #endif
599
600 mmioAscend(infoPtr->hMMio, &mmckHead, 0);
601
602 /* no need to read optional JUNK chunk */
603
604 mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
605 if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
606 WARN("Can't find 'movi' list\n");
607 return FALSE;
608 }
609
610 /* FIXME: should handle the 'rec ' LIST when present */
611
612 infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
613 if (!infoPtr->lpIndex)
614 return FALSE;
615
616 numFrame = insize = 0;
617 while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
618 numFrame < infoPtr->mah.dwTotalFrames) {
619 infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
620 if (insize < mmckInfo.cksize)
621 insize = mmckInfo.cksize;
622 numFrame++;
623 mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
624 }
625 if (numFrame != infoPtr->mah.dwTotalFrames) {
626 WARN("Found %d frames (/%d)\n", numFrame, infoPtr->mah.dwTotalFrames);
627 return FALSE;
628 }
629 if (insize > infoPtr->ash.dwSuggestedBufferSize) {
630 WARN("insize=%d suggestedSize=%d\n", insize, infoPtr->ash.dwSuggestedBufferSize);
631 infoPtr->ash.dwSuggestedBufferSize = insize;
632 }
633
634 infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
635 if (!infoPtr->indata)
636 return FALSE;
637
638 return TRUE;
639 }
640
641
642 static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
643 {
644 DWORD outSize;
645
646 /* check uncompressed AVI */
647 if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
648 (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
649 (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
650 {
651 infoPtr->hic = 0;
652 return TRUE;
653 }
654
655 /* try to get a decompressor for that type */
656 infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
657 if (!infoPtr->hic) {
658 WARN("Can't load codec for the file\n");
659 return FALSE;
660 }
661
662 outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
663 (DWORD_PTR)infoPtr->inbih, 0L);
664
665 infoPtr->outbih = Alloc(outSize);
666 if (!infoPtr->outbih)
667 return FALSE;
668
669 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
670 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK)
671 {
672 WARN("Can't get output BIH\n");
673 return FALSE;
674 }
675
676 infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
677 if (!infoPtr->outdata)
678 return FALSE;
679
680 if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
681 (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
682 WARN("Can't begin decompression\n");
683 return FALSE;
684 }
685
686 return TRUE;
687 }
688
689
690 static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
691 {
692 HDC hdc;
693
694 ANIMATE_Free(infoPtr);
695
696 if (!lpszName)
697 {
698 TRACE("Closing avi.\n");
699 /* installer of thebat! v1.62 requires FALSE here */
700 return (infoPtr->hMMio != 0);
701 }
702
703 if (!hInstance)
704 hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
705
706 TRACE("(%s)\n", debugstr_w(lpszName));
707
708 if (!IS_INTRESOURCE(lpszName))
709 {
710 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
711 {
712 TRACE("No AVI resource found.\n");
713 if (!ANIMATE_LoadFileW(infoPtr, lpszName))
714 {
715 WARN("No AVI file found.\n");
716 return FALSE;
717 }
718 }
719 }
720 else
721 {
722 if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
723 {
724 WARN("No AVI resource found.\n");
725 return FALSE;
726 }
727 }
728
729 if (!ANIMATE_GetAviInfo(infoPtr))
730 {
731 WARN("Can't get AVI information\n");
732 ANIMATE_Free(infoPtr);
733 return FALSE;
734 }
735
736 if (!ANIMATE_GetAviCodec(infoPtr))
737 {
738 WARN("Can't get AVI Codec\n");
739 ANIMATE_Free(infoPtr);
740 return FALSE;
741 }
742
743 hdc = GetDC(infoPtr->hwndSelf);
744 /* native looks at the top left pixel of the first frame here too. */
745 infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
746 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
747 ReleaseDC(infoPtr->hwndSelf, hdc);
748
749 if (!(infoPtr->dwStyle & ACS_CENTER))
750 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
751 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);
752
753 if (infoPtr->dwStyle & ACS_AUTOPLAY)
754 return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
755
756 return TRUE;
757 }
758
759
760 static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
761 {
762 LPWSTR lpwszName;
763 LRESULT result;
764 INT len;
765
766 if (IS_INTRESOURCE(lpszName))
767 return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
768
769 len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
770 lpwszName = Alloc(len * sizeof(WCHAR));
771 if (!lpwszName) return FALSE;
772 MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);
773
774 result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
775 Free (lpwszName);
776 return result;
777 }
778
779
780 static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
781 {
782 /* nothing opened */
783 if (!infoPtr->hMMio)
784 return FALSE;
785
786 ANIMATE_DoStop(infoPtr);
787 return TRUE;
788 }
789
790
791 static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
792 {
793 static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
794 ANIMATE_INFO *infoPtr;
795
796 if (!fnIC.hModule)
797 {
798 fnIC.hModule = LoadLibraryW(msvfw32W);
799 if (!fnIC.hModule) return FALSE;
800
801 fnIC.fnICOpen = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
802 fnIC.fnICClose = (void*)GetProcAddress(fnIC.hModule, "ICClose");
803 fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
804 fnIC.fnICDecompress = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
805 }
806
807 /* allocate memory for info structure */
808 infoPtr = Alloc(sizeof(ANIMATE_INFO));
809 if (!infoPtr) return FALSE;
810
811 /* store crossref hWnd <-> info structure */
812 SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
813 infoPtr->hwndSelf = hWnd;
814 infoPtr->hwndNotify = lpcs->hwndParent;
815 infoPtr->transparentColor = ANIMATE_COLOR_NONE;
816 infoPtr->hbmPrevFrame = 0;
817 infoPtr->dwStyle = lpcs->style;
818
819 TRACE("Animate style=0x%08x, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
820
821 InitializeCriticalSection(&infoPtr->cs);
822 infoPtr->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ANIMATE_INFO*->cs");
823
824 return TRUE;
825 }
826
827
828 static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
829 {
830 /* free avi data */
831 ANIMATE_Free(infoPtr);
832
833 /* free animate info data */
834 SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
835
836 infoPtr->cs.DebugInfo->Spare[0] = 0;
837 DeleteCriticalSection(&infoPtr->cs);
838 Free(infoPtr);
839
840 return 0;
841 }
842
843
844 static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
845 {
846 RECT rect;
847 HBRUSH hBrush;
848
849 hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
850 (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
851 GetClientRect(infoPtr->hwndSelf, &rect);
852 FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
853
854 return TRUE;
855 }
856
857
858 static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
859 {
860 TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
861 wStyleType, lpss->styleOld, lpss->styleNew);
862
863 if (wStyleType != GWL_STYLE) return 0;
864
865 infoPtr->dwStyle = lpss->styleNew;
866
867 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
868 return 0;
869 }
870
871
872 static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
873 {
874 ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);
875
876 TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hWnd, uMsg, wParam, lParam);
877 if (!infoPtr && (uMsg != WM_NCCREATE))
878 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
879 switch (uMsg)
880 {
881 case ACM_OPENA:
882 return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
883
884 case ACM_OPENW:
885 return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
886
887 case ACM_PLAY:
888 return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
889
890 case ACM_STOP:
891 return ANIMATE_Stop(infoPtr);
892
893 case WM_CLOSE:
894 ANIMATE_Free(infoPtr);
895 return 0;
896
897 case WM_NCCREATE:
898 return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
899
900 case WM_NCHITTEST:
901 return HTTRANSPARENT;
902
903 case WM_DESTROY:
904 return ANIMATE_Destroy(infoPtr);
905
906 case WM_ERASEBKGND:
907 return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
908
909 case WM_STYLECHANGED:
910 return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
911
912 case WM_TIMER:
913 return ANIMATE_Timer(infoPtr);
914
915 case WM_PRINTCLIENT:
916 case WM_PAINT:
917 {
918 /* the animation has not decompressed
919 * (and displayed) the first frame yet, don't paint
920 */
921 if (!infoPtr->hbmPrevFrame)
922 {
923 /* default paint handling */
924 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
925 }
926
927 if (wParam)
928 {
929 EnterCriticalSection(&infoPtr->cs);
930 ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
931 LeaveCriticalSection(&infoPtr->cs);
932 }
933 else
934 {
935 PAINTSTRUCT ps;
936 HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);
937
938 EnterCriticalSection(&infoPtr->cs);
939 ANIMATE_PaintFrame(infoPtr, hDC);
940 LeaveCriticalSection(&infoPtr->cs);
941
942 EndPaint(infoPtr->hwndSelf, &ps);
943 }
944 }
945 break;
946
947 case WM_SIZE:
948 if (infoPtr->dwStyle & ACS_CENTER)
949 InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
950 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
951
952 default:
953 if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
954 ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
955
956 return DefWindowProcW(hWnd, uMsg, wParam, lParam);
957 }
958 return 0;
959 }
960
961 void ANIMATE_Register(void)
962 {
963 WNDCLASSW wndClass;
964
965 ZeroMemory(&wndClass, sizeof(WNDCLASSW));
966 wndClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
967 wndClass.lpfnWndProc = ANIMATE_WindowProc;
968 wndClass.cbClsExtra = 0;
969 wndClass.cbWndExtra = sizeof(ANIMATE_INFO *);
970 wndClass.hCursor = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
971 wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
972 wndClass.lpszClassName = ANIMATE_CLASSW;
973
974 RegisterClassW(&wndClass);
975 }
976
977
978 void ANIMATE_Unregister(void)
979 {
980 UnregisterClassW(ANIMATE_CLASSW, NULL);
981 }