animate.c 28.4 KB
Newer Older
Eric Pouech's avatar
Eric Pouech committed
1
/* -*- tab-width: 8; c-basic-offset: 4 -*- */
Alexandre Julliard's avatar
Alexandre Julliard committed
2 3 4
/*
 * Animation control
 *
5
 * Copyright 1998, 1999 Eric Kohl
6 7
 * Copyright 1999 Eric Pouech
 * Copyright 2005 Dimitrie O. Paun
Alexandre Julliard's avatar
Alexandre Julliard committed
8
 *
9 10 11 12 13 14 15 16 17 18 19 20
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
23 24
 * NOTES
 *
25 26 27 28 29 30 31
 * This code was audited for completeness against the documented features
 * of Comctl32.dll version 6.0 on Mar. 15, 2005, by Dimitrie O. Paun.
 * 
 * Unless otherwise noted, we believe this code to be complete, as per
 * the specification mentioned above.
 * If you discover missing features, or bugs, please note them below.
 * 
Alexandre Julliard's avatar
Alexandre Julliard committed
32
 * TODO:
Eric Pouech's avatar
Eric Pouech committed
33
 *   - check for the 'rec ' list in some AVI files
Alexandre Julliard's avatar
Alexandre Julliard committed
34 35
 */

36
#include <stdarg.h>
37
#include <string.h>
38
#include "windef.h"
39
#include "winbase.h"
40 41 42
#include "wingdi.h"
#include "winuser.h"
#include "winnls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
43
#include "commctrl.h"
44
#include "vfw.h"
Eric Pouech's avatar
Eric Pouech committed
45
#include "mmsystem.h"
46
#include "comctl32.h"
47
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
48

49
WINE_DEFAULT_DEBUG_CHANNEL(animate);
50

51 52
static struct {
    HMODULE	hModule;
53 54
    HIC         (WINAPI *fnICOpen)(DWORD, DWORD, UINT);
    LRESULT     (WINAPI *fnICClose)(HIC);
Frank Richter's avatar
Frank Richter committed
55
    LRESULT     (WINAPI *fnICSendMessage)(HIC, UINT, DWORD_PTR, DWORD_PTR);
56
    DWORD       (WINAPIV *fnICDecompress)(HIC,DWORD,LPBITMAPINFOHEADER,LPVOID,LPBITMAPINFOHEADER,LPVOID);
57 58
} fnIC;

59 60 61 62
typedef struct
{
   /* reference to input stream (file or resource) */
   HGLOBAL 		hRes;
63
   HMMIO		hMMio;	/* handle to mmio stream */
64 65
   HWND			hwndSelf;
   HWND			hwndNotify;
66
   DWORD		dwStyle;
67 68 69 70 71 72 73 74 75 76 77 78
   /* information on the loaded AVI file */
   MainAVIHeader	mah;
   AVIStreamHeader	ash;
   LPBITMAPINFOHEADER	inbih;
   LPDWORD		lpIndex;
   /* data for the decompressor */
   HIC			hic;
   LPBITMAPINFOHEADER	outbih;
   LPVOID		indata;
   LPVOID		outdata;
   /* data for the background mechanism */
   CRITICAL_SECTION	cs;
79
   HANDLE		hStopEvent;
80
   HANDLE		hThread;
81
   DWORD		threadId;
82 83 84 85 86 87
   UINT			uTimer;
   /* data for playing the file */
   int			nFromFrame;
   int			nToFrame;
   int			nLoop;
   int			currFrame;
88
   /* transparency info*/
89
   COLORREF         	transparentColor;
90
   HBRUSH           	hbrushBG;
91
   HBITMAP  	    	hbmPrevFrame;
92 93
} ANIMATE_INFO;

94
#define ANIMATE_COLOR_NONE  	0xffffffff
Alexandre Julliard's avatar
Alexandre Julliard committed
95

96
static void ANIMATE_Notify(const ANIMATE_INFO *infoPtr, UINT notif)
Alexandre Julliard's avatar
Alexandre Julliard committed
97
{
98
    PostMessageW(infoPtr->hwndNotify, WM_COMMAND,
99 100
		 MAKEWPARAM(GetDlgCtrlID(infoPtr->hwndSelf), notif),
		 (LPARAM)infoPtr->hwndSelf);
Eric Pouech's avatar
Eric Pouech committed
101
}
Alexandre Julliard's avatar
Alexandre Julliard committed
102

103
static BOOL ANIMATE_LoadResW(ANIMATE_INFO *infoPtr, HINSTANCE hInst, LPCWSTR lpName)
Eric Pouech's avatar
Eric Pouech committed
104
{
105
    static const WCHAR aviW[] = { 'A', 'V', 'I', 0 };
Eric Pouech's avatar
Eric Pouech committed
106 107 108
    HRSRC 	hrsrc;
    MMIOINFO	mminfo;
    LPVOID	lpAvi;
109

110
    hrsrc = FindResourceW(hInst, lpName, aviW);
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112
    if (!hrsrc)
	return FALSE;
113

Eric Pouech's avatar
Eric Pouech committed
114 115 116
    infoPtr->hRes = LoadResource(hInst, hrsrc);
    if (!infoPtr->hRes)
 	return FALSE;
117

Eric Pouech's avatar
Eric Pouech committed
118 119 120
    lpAvi = LockResource(infoPtr->hRes);
    if (!lpAvi)
	return FALSE;
121

Eric Pouech's avatar
Eric Pouech committed
122 123
    memset(&mminfo, 0, sizeof(mminfo));
    mminfo.fccIOProc = FOURCC_MEM;
124
    mminfo.pchBuffer = lpAvi;
Eric Pouech's avatar
Eric Pouech committed
125
    mminfo.cchBuffer = SizeofResource(hInst, hrsrc);
126 127 128
    infoPtr->hMMio = mmioOpenW(NULL, &mminfo, MMIO_READ);
    if (!infoPtr->hMMio) 
    {
129
	FreeResource(infoPtr->hRes);
Alexandre Julliard's avatar
Alexandre Julliard committed
130
	return FALSE;
Eric Pouech's avatar
Eric Pouech committed
131
    }
132

Eric Pouech's avatar
Eric Pouech committed
133 134 135
    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
136

137
static BOOL ANIMATE_LoadFileW(ANIMATE_INFO *infoPtr, LPWSTR lpName)
Eric Pouech's avatar
Eric Pouech committed
138
{
139
    infoPtr->hMMio = mmioOpenW(lpName, 0, MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
140

Frank Richter's avatar
Frank Richter committed
141 142
    if(!infoPtr->hMMio) return FALSE;
    return TRUE;
Eric Pouech's avatar
Eric Pouech committed
143 144 145
}


146
static BOOL ANIMATE_DoStop(ANIMATE_INFO *infoPtr)
147
{
148 149
    BOOL stopped = FALSE;

150 151 152
    EnterCriticalSection(&infoPtr->cs);

    /* should stop playing */
153
    if (infoPtr->hThread)
154
    {
155 156 157
        HANDLE handle = infoPtr->hThread;

        TRACE("stopping animation thread\n");
158
        infoPtr->hThread = 0;
159
        SetEvent( infoPtr->hStopEvent );
160 161 162 163

        if (infoPtr->threadId != GetCurrentThreadId())
        {
            LeaveCriticalSection(&infoPtr->cs);  /* leave it a chance to run */
164
            WaitForSingleObject( handle, INFINITE );
165 166 167 168 169
            TRACE("animation thread stopped\n");
            EnterCriticalSection(&infoPtr->cs);
        }

        CloseHandle( handle );
170
        CloseHandle( infoPtr->hStopEvent );
171
        infoPtr->hStopEvent = 0;
172
        stopped = TRUE;
173 174
    }
    if (infoPtr->uTimer) {
175
	KillTimer(infoPtr->hwndSelf, infoPtr->uTimer);
176
	infoPtr->uTimer = 0;
177
	stopped = TRUE;
178 179 180 181
    }

    LeaveCriticalSection(&infoPtr->cs);

182 183
    if (stopped)
        ANIMATE_Notify(infoPtr, ACN_STOP);
184 185 186 187 188

    return TRUE;
}


Eric Pouech's avatar
Eric Pouech committed
189 190 191
static void ANIMATE_Free(ANIMATE_INFO *infoPtr)
{
    if (infoPtr->hMMio) {
192
	ANIMATE_DoStop(infoPtr);
193
	mmioClose(infoPtr->hMMio, 0);
Eric Pouech's avatar
Eric Pouech committed
194 195 196 197
	if (infoPtr->hRes) {
 	    FreeResource(infoPtr->hRes);
	    infoPtr->hRes = 0;
	}
198
        Free (infoPtr->lpIndex);
199
        infoPtr->lpIndex = NULL;
Eric Pouech's avatar
Eric Pouech committed
200
	if (infoPtr->hic) {
201
	    fnIC.fnICClose(infoPtr->hic);
Eric Pouech's avatar
Eric Pouech committed
202 203
	    infoPtr->hic = 0;
	}
204
        Free (infoPtr->inbih);
205
        infoPtr->inbih = NULL;
206
        Free (infoPtr->outbih);
207
        infoPtr->outbih = NULL;
208
	Free (infoPtr->indata);
209
        infoPtr->indata = NULL;
210
	Free (infoPtr->outdata);
211
        infoPtr->outdata = NULL;
212 213 214 215 216
    	if( infoPtr->hbmPrevFrame )
        {
	    DeleteObject(infoPtr->hbmPrevFrame);
            infoPtr->hbmPrevFrame = 0;
        }
217

Eric Pouech's avatar
Eric Pouech committed
218 219 220 221
	memset(&infoPtr->mah, 0, sizeof(infoPtr->mah));
	memset(&infoPtr->ash, 0, sizeof(infoPtr->ash));
	infoPtr->nFromFrame = infoPtr->nToFrame = infoPtr->nLoop = infoPtr->currFrame = 0;
    }
222
    infoPtr->transparentColor = ANIMATE_COLOR_NONE;
Eric Pouech's avatar
Eric Pouech committed
223 224
}

225
static void ANIMATE_TransparentBlt(ANIMATE_INFO const *infoPtr, HDC hdcDest, HDC hdcSource)
226
{
227 228
    HDC hdcMask;
    HBITMAP hbmMask;
229 230
    HBITMAP hbmOld;

231 232
    /* create a transparency mask */
    hdcMask = CreateCompatibleDC(hdcDest);
233 234
    hbmMask = CreateBitmap(infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, 1,1,NULL);
    hbmOld = SelectObject(hdcMask, hbmMask);
235 236 237

    SetBkColor(hdcSource,infoPtr->transparentColor);
    BitBlt(hdcMask,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCCOPY);
238

239
    /* mask the source bitmap */
240 241
    SetBkColor(hdcSource, RGB(0,0,0));
    SetTextColor(hdcSource, RGB(255,255,255));
242 243 244
    BitBlt(hdcSource, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);

    /* mask the destination bitmap */
245 246
    SetBkColor(hdcDest, RGB(255,255,255));
    SetTextColor(hdcDest, RGB(0,0,0));
247 248 249 250 251 252 253 254 255 256
    BitBlt(hdcDest, 0, 0, infoPtr->inbih->biWidth, infoPtr->inbih->biHeight, hdcMask, 0, 0, SRCAND);

    /* combine source and destination */
    BitBlt(hdcDest,0,0,infoPtr->inbih->biWidth, infoPtr->inbih->biHeight,hdcSource,0,0,SRCPAINT);

    SelectObject(hdcMask, hbmOld);
    DeleteObject(hbmMask);
    DeleteDC(hdcMask);
}

257
static BOOL ANIMATE_PaintFrame(ANIMATE_INFO* infoPtr, HDC hDC)
258
{
259 260
    void const *pBitmapData;
    BITMAPINFO const *pBitmapInfo;
261 262 263 264 265 266
    HDC hdcMem;
    HBITMAP hbmOld;
    int nOffsetX = 0;
    int nOffsetY = 0;
    int nWidth;
    int nHeight;
267

268 269
    if (!hDC || !infoPtr->inbih)
	return TRUE;
270

271
    if (infoPtr->hic )
272 273 274
    {
        pBitmapData = infoPtr->outdata;
        pBitmapInfo = (LPBITMAPINFO)infoPtr->outbih;
275

276
        nWidth = infoPtr->outbih->biWidth;
277
        nHeight = infoPtr->outbih->biHeight;
278 279
    } 
    else
280
    {
281 282
        pBitmapData = infoPtr->indata;
        pBitmapInfo = (LPBITMAPINFO)infoPtr->inbih;
283

284
        nWidth = infoPtr->inbih->biWidth;
285 286
        nHeight = infoPtr->inbih->biHeight;
    }
287

288 289 290 291
    if(!infoPtr->hbmPrevFrame)
    {
        infoPtr->hbmPrevFrame=CreateCompatibleBitmap(hDC, nWidth,nHeight );
    }
292

293
    hdcMem = CreateCompatibleDC(hDC);
294
    hbmOld = SelectObject(hdcMem, infoPtr->hbmPrevFrame);
295

296 297
    SetDIBits(hdcMem, infoPtr->hbmPrevFrame, 0, nHeight, pBitmapData, pBitmapInfo, DIB_RGB_COLORS);

298 299
    /*
     * we need to get the transparent color even without ACS_TRANSPARENT,
300
     * because the style can be changed later on and the color should always
301
     * be obtained in the first frame
302 303 304 305
     */
    if(infoPtr->transparentColor == ANIMATE_COLOR_NONE)
    {
        infoPtr->transparentColor = GetPixel(hdcMem,0,0);
306
    }
307

308
    if(infoPtr->dwStyle & ACS_TRANSPARENT)
309
    {
310 311 312 313
        HDC hdcFinal = CreateCompatibleDC(hDC);
        HBITMAP hbmFinal = CreateCompatibleBitmap(hDC,nWidth, nHeight);
        HBITMAP hbmOld2 = SelectObject(hdcFinal, hbmFinal);
        RECT rect;
314

315 316 317 318
        rect.left = 0;
        rect.top = 0;
        rect.right = nWidth;
        rect.bottom = nHeight;
319

320 321 322 323 324 325 326 327 328
        if(!infoPtr->hbrushBG)
            infoPtr->hbrushBG = GetCurrentObject(hDC, OBJ_BRUSH);

        FillRect(hdcFinal, &rect, infoPtr->hbrushBG);
        ANIMATE_TransparentBlt(infoPtr, hdcFinal, hdcMem);

        SelectObject(hdcFinal, hbmOld2);
        SelectObject(hdcMem, hbmFinal);
        DeleteDC(hdcFinal);
329 330
        DeleteObject(infoPtr->hbmPrevFrame);
        infoPtr->hbmPrevFrame = hbmFinal;
331
    }
332

333
    if (infoPtr->dwStyle & ACS_CENTER)
334
    {
335
        RECT rect;
336

337 338 339
        GetWindowRect(infoPtr->hwndSelf, &rect);
        nOffsetX = ((rect.right - rect.left) - nWidth)/2;
        nOffsetY = ((rect.bottom - rect.top) - nHeight)/2;
340
    }
341
    BitBlt(hDC, nOffsetX, nOffsetY, nWidth, nHeight, hdcMem, 0, 0, SRCCOPY);
342

343 344
    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);
Eric Pouech's avatar
Eric Pouech committed
345 346
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
347

348
static BOOL ANIMATE_DrawFrame(ANIMATE_INFO *infoPtr, HDC hDC)
Alexandre Julliard's avatar
Alexandre Julliard committed
349
{
Eric Pouech's avatar
Eric Pouech committed
350 351
    TRACE("Drawing frame %d (loop %d)\n", infoPtr->currFrame, infoPtr->nLoop);

352 353
    mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
    mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
354

355
    if (infoPtr->hic &&
356
	fnIC.fnICDecompress(infoPtr->hic, 0, infoPtr->inbih, infoPtr->indata,
357
		     infoPtr->outbih, infoPtr->outdata) != ICERR_OK) {
Eric Pouech's avatar
Eric Pouech committed
358
	WARN("Decompression error\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
359 360 361
	return FALSE;
    }

362
    ANIMATE_PaintFrame(infoPtr, hDC);
Eric Pouech's avatar
Eric Pouech committed
363 364 365 366 367 368 369 370

    if (infoPtr->currFrame++ >= infoPtr->nToFrame) {
	infoPtr->currFrame = infoPtr->nFromFrame;
	if (infoPtr->nLoop != -1) {
	    if (--infoPtr->nLoop == 0) {
		ANIMATE_DoStop(infoPtr);
	    }
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
371 372 373 374 375
    }

    return TRUE;
}

376 377
static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
{
378 379 380 381 382 383 384 385 386 387
    HDC	hDC;

    if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
    {
        EnterCriticalSection(&infoPtr->cs);
        ANIMATE_DrawFrame(infoPtr, hDC);
        LeaveCriticalSection(&infoPtr->cs);

	ReleaseDC(infoPtr->hwndSelf, hDC);
    }
388 389 390 391

    return 0;
}

392
static DWORD CALLBACK ANIMATE_AnimationThread(LPVOID ptr_)
Eric Pouech's avatar
Eric Pouech committed
393
{
394
    ANIMATE_INFO *infoPtr = ptr_;
395 396
    HANDLE event;
    DWORD timeout;
Eric Pouech's avatar
Eric Pouech committed
397

398
    while(1)
399
    {
400 401
        HDC hDC = GetDC(infoPtr->hwndSelf);

Andreas Mohr's avatar
Andreas Mohr committed
402
        EnterCriticalSection(&infoPtr->cs);
403
        ANIMATE_DrawFrame(infoPtr, hDC);
404 405
        timeout = infoPtr->mah.dwMicroSecPerFrame;
        event = infoPtr->hStopEvent;
Andreas Mohr's avatar
Andreas Mohr committed
406
        LeaveCriticalSection(&infoPtr->cs);
407

408 409
        ReleaseDC(infoPtr->hwndSelf, hDC);

410
        /* time is in microseconds, we should convert it to milliseconds */
411
        if ((event == 0) || WaitForSingleObject( event, (timeout+500)/1000) == WAIT_OBJECT_0)
412
            break;
Andreas Mohr's avatar
Andreas Mohr committed
413
    }
414
    return TRUE;
Eric Pouech's avatar
Eric Pouech committed
415
}
Alexandre Julliard's avatar
Alexandre Julliard committed
416

417
static LRESULT ANIMATE_Play(ANIMATE_INFO *infoPtr, UINT cRepeat, WORD wFrom, WORD wTo)
Alexandre Julliard's avatar
Alexandre Julliard committed
418
{
Eric Pouech's avatar
Eric Pouech committed
419 420 421 422
    /* nothing opened */
    if (!infoPtr->hMMio)
	return FALSE;

423
    if (infoPtr->hThread || infoPtr->uTimer) {
424 425
	TRACE("Already playing\n");
	return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
426
    }
Eric Pouech's avatar
Eric Pouech committed
427

428 429 430
    infoPtr->nFromFrame = wFrom;
    infoPtr->nToFrame   = wTo;
    infoPtr->nLoop      = cRepeat;
Eric Pouech's avatar
Eric Pouech committed
431 432 433 434

    if (infoPtr->nToFrame == 0xFFFF)
	infoPtr->nToFrame = infoPtr->mah.dwTotalFrames - 1;

435
    TRACE("(repeat=%d from=%d to=%d);\n",
Eric Pouech's avatar
Eric Pouech committed
436 437
	  infoPtr->nLoop, infoPtr->nFromFrame, infoPtr->nToFrame);

438 439 440 441
    if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
        (SHORT)infoPtr->nFromFrame < 0)
        infoPtr->nFromFrame = 0;

442
    if (infoPtr->nFromFrame > infoPtr->nToFrame ||
Eric Pouech's avatar
Eric Pouech committed
443 444 445 446 447
	infoPtr->nToFrame >= infoPtr->mah.dwTotalFrames)
	return FALSE;

    infoPtr->currFrame = infoPtr->nFromFrame;

448 449 450 451
    /* seek - doesn't need to start a thread or set a timer and neither
     * does it send a notification */
    if (infoPtr->nFromFrame == infoPtr->nToFrame)
    {
452 453 454 455 456 457 458 459
        HDC hDC;

        if ((hDC = GetDC(infoPtr->hwndSelf)) != 0)
        {
            ANIMATE_DrawFrame(infoPtr, hDC);

	    ReleaseDC(infoPtr->hwndSelf, hDC);
        }
460 461 462
        return TRUE;
    }

463
    if (infoPtr->dwStyle & ACS_TIMER) 
464
    {
Eric Pouech's avatar
Eric Pouech committed
465 466
	TRACE("Using a timer\n");
	/* create a timer to display AVI */
467 468 469 470 471
	infoPtr->uTimer = SetTimer(infoPtr->hwndSelf, 1, 
                                   infoPtr->mah.dwMicroSecPerFrame / 1000, NULL);
    } 
    else 
    {
472
	TRACE("Using an animation thread\n");
473
        infoPtr->hStopEvent = CreateEventW( NULL, TRUE, FALSE, NULL );
474
        infoPtr->hThread = CreateThread(0, 0, ANIMATE_AnimationThread,
475
                                        infoPtr, 0, &infoPtr->threadId);
476
        if(!infoPtr->hThread) return FALSE;
477

Alexandre Julliard's avatar
Alexandre Julliard committed
478
    }
479

Eric Pouech's avatar
Eric Pouech committed
480 481 482
    ANIMATE_Notify(infoPtr, ACN_START);

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
483 484 485
}


Eric Pouech's avatar
Eric Pouech committed
486
static BOOL ANIMATE_GetAviInfo(ANIMATE_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
487
{
Eric Pouech's avatar
Eric Pouech committed
488 489 490 491 492 493 494
    MMCKINFO		ckMainRIFF;
    MMCKINFO		mmckHead;
    MMCKINFO		mmckList;
    MMCKINFO		mmckInfo;
    DWORD		numFrame;
    DWORD		insize;

495
    if (mmioDescend(infoPtr->hMMio, &ckMainRIFF, NULL, 0) != 0) {
Eric Pouech's avatar
Eric Pouech committed
496 497 498
	WARN("Can't find 'RIFF' chunk\n");
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
499

Eric Pouech's avatar
Eric Pouech committed
500 501 502 503 504
    if ((ckMainRIFF.ckid != FOURCC_RIFF) ||
	(ckMainRIFF.fccType != mmioFOURCC('A', 'V', 'I', ' '))) {
	WARN("Can't find 'AVI ' chunk\n");
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
505

Eric Pouech's avatar
Eric Pouech committed
506
    mmckHead.fccType = mmioFOURCC('h', 'd', 'r', 'l');
507
    if (mmioDescend(infoPtr->hMMio, &mmckHead, &ckMainRIFF, MMIO_FINDLIST) != 0) {
Eric Pouech's avatar
Eric Pouech committed
508 509 510
	WARN("Can't find 'hdrl' list\n");
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
511

Eric Pouech's avatar
Eric Pouech committed
512
    mmckInfo.ckid = mmioFOURCC('a', 'v', 'i', 'h');
513
    if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckHead, MMIO_FINDCHUNK) != 0) {
Eric Pouech's avatar
Eric Pouech committed
514 515 516
	WARN("Can't find 'avih' chunk\n");
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
517

518 519
    mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));

520 521 522 523 524 525 526 527 528 529
    TRACE("mah.dwMicroSecPerFrame=%d\n", 	infoPtr->mah.dwMicroSecPerFrame);
    TRACE("mah.dwMaxBytesPerSec=%d\n",		infoPtr->mah.dwMaxBytesPerSec);
    TRACE("mah.dwPaddingGranularity=%d\n", 	infoPtr->mah.dwPaddingGranularity);
    TRACE("mah.dwFlags=%d\n",			infoPtr->mah.dwFlags);
    TRACE("mah.dwTotalFrames=%d\n",		infoPtr->mah.dwTotalFrames);
    TRACE("mah.dwInitialFrames=%d\n",		infoPtr->mah.dwInitialFrames);
    TRACE("mah.dwStreams=%d\n",			infoPtr->mah.dwStreams);
    TRACE("mah.dwSuggestedBufferSize=%d\n",	infoPtr->mah.dwSuggestedBufferSize);
    TRACE("mah.dwWidth=%d\n",			infoPtr->mah.dwWidth);
    TRACE("mah.dwHeight=%d\n",			infoPtr->mah.dwHeight);
530 531

    mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
Eric Pouech's avatar
Eric Pouech committed
532 533

    mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
534
    if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) != 0) {
Eric Pouech's avatar
Eric Pouech committed
535 536 537
	WARN("Can't find 'strl' list\n");
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
538

Eric Pouech's avatar
Eric Pouech committed
539
    mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'h');
540
    if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
Eric Pouech's avatar
Eric Pouech committed
541 542 543
	WARN("Can't find 'strh' chunk\n");
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
544

545 546
    mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));

547 548 549
    TRACE("ash.fccType='%c%c%c%c'\n", 		LOBYTE(LOWORD(infoPtr->ash.fccType)),
	                                        HIBYTE(LOWORD(infoPtr->ash.fccType)),
	                                        LOBYTE(HIWORD(infoPtr->ash.fccType)),
Eric Pouech's avatar
Eric Pouech committed
550
	                                        HIBYTE(HIWORD(infoPtr->ash.fccType)));
551 552 553
    TRACE("ash.fccHandler='%c%c%c%c'\n",	LOBYTE(LOWORD(infoPtr->ash.fccHandler)),
	                                        HIBYTE(LOWORD(infoPtr->ash.fccHandler)),
	                                        LOBYTE(HIWORD(infoPtr->ash.fccHandler)),
Eric Pouech's avatar
Eric Pouech committed
554
	                                        HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
555
    TRACE("ash.dwFlags=%d\n", 			infoPtr->ash.dwFlags);
Eric Pouech's avatar
Eric Pouech committed
556 557
    TRACE("ash.wPriority=%d\n", 		infoPtr->ash.wPriority);
    TRACE("ash.wLanguage=%d\n", 		infoPtr->ash.wLanguage);
558 559 560 561 562 563 564 565
    TRACE("ash.dwInitialFrames=%d\n", 		infoPtr->ash.dwInitialFrames);
    TRACE("ash.dwScale=%d\n", 			infoPtr->ash.dwScale);
    TRACE("ash.dwRate=%d\n", 			infoPtr->ash.dwRate);
    TRACE("ash.dwStart=%d\n", 			infoPtr->ash.dwStart);
    TRACE("ash.dwLength=%d\n",			infoPtr->ash.dwLength);
    TRACE("ash.dwSuggestedBufferSize=%d\n", 	infoPtr->ash.dwSuggestedBufferSize);
    TRACE("ash.dwQuality=%d\n", 		infoPtr->ash.dwQuality);
    TRACE("ash.dwSampleSize=%d\n", 		infoPtr->ash.dwSampleSize);
566
    TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", 	infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
Eric Pouech's avatar
Eric Pouech committed
567
	  infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
568 569

    mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
Eric Pouech's avatar
Eric Pouech committed
570 571

    mmckInfo.ckid = mmioFOURCC('s', 't', 'r', 'f');
572
    if (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, MMIO_FINDCHUNK) != 0) {
Eric Pouech's avatar
Eric Pouech committed
573 574 575 576
	WARN("Can't find 'strh' chunk\n");
	return FALSE;
    }

577
    infoPtr->inbih = Alloc(mmckInfo.cksize);
Eric Pouech's avatar
Eric Pouech committed
578 579 580
    if (!infoPtr->inbih) {
	WARN("Can't alloc input BIH\n");
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
581
    }
Eric Pouech's avatar
Eric Pouech committed
582

583 584
    mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);

585 586 587
    TRACE("bih.biSize=%d\n", 		infoPtr->inbih->biSize);
    TRACE("bih.biWidth=%d\n", 		infoPtr->inbih->biWidth);
    TRACE("bih.biHeight=%d\n",		infoPtr->inbih->biHeight);
Eric Pouech's avatar
Eric Pouech committed
588 589
    TRACE("bih.biPlanes=%d\n", 		infoPtr->inbih->biPlanes);
    TRACE("bih.biBitCount=%d\n", 	infoPtr->inbih->biBitCount);
590 591 592 593 594 595
    TRACE("bih.biCompression=%d\n", 	infoPtr->inbih->biCompression);
    TRACE("bih.biSizeImage=%d\n", 	infoPtr->inbih->biSizeImage);
    TRACE("bih.biXPelsPerMeter=%d\n", 	infoPtr->inbih->biXPelsPerMeter);
    TRACE("bih.biYPelsPerMeter=%d\n", 	infoPtr->inbih->biYPelsPerMeter);
    TRACE("bih.biClrUsed=%d\n", 	infoPtr->inbih->biClrUsed);
    TRACE("bih.biClrImportant=%d\n", 	infoPtr->inbih->biClrImportant);
Eric Pouech's avatar
Eric Pouech committed
596

597 598 599
    mmioAscend(infoPtr->hMMio, &mmckInfo, 0);

    mmioAscend(infoPtr->hMMio, &mmckList, 0);
600

Eric Pouech's avatar
Eric Pouech committed
601 602
#if 0
    /* an AVI has 0 or 1 video stream, and to be animated should not contain
603
     * an audio stream, so only one strl is allowed
Eric Pouech's avatar
Eric Pouech committed
604 605
     */
    mmckList.fccType = mmioFOURCC('s', 't', 'r', 'l');
606
    if (mmioDescend(infoPtr->hMMio, &mmckList, &mmckHead, MMIO_FINDLIST) == 0) {
Eric Pouech's avatar
Eric Pouech committed
607 608 609 610
	WARN("There should be a single 'strl' list\n");
	return FALSE;
    }
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
611

612
    mmioAscend(infoPtr->hMMio, &mmckHead, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
613

Eric Pouech's avatar
Eric Pouech committed
614
    /* no need to read optional JUNK chunk */
Alexandre Julliard's avatar
Alexandre Julliard committed
615

Eric Pouech's avatar
Eric Pouech committed
616
    mmckList.fccType = mmioFOURCC('m', 'o', 'v', 'i');
617
    if (mmioDescend(infoPtr->hMMio, &mmckList, &ckMainRIFF, MMIO_FINDLIST) != 0) {
Eric Pouech's avatar
Eric Pouech committed
618 619
	WARN("Can't find 'movi' list\n");
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
620 621
    }

Eric Pouech's avatar
Eric Pouech committed
622 623
    /* FIXME: should handle the 'rec ' LIST when present */

624
    infoPtr->lpIndex = Alloc(infoPtr->mah.dwTotalFrames * sizeof(DWORD));
625
    if (!infoPtr->lpIndex) 
Eric Pouech's avatar
Eric Pouech committed
626
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
627

Eric Pouech's avatar
Eric Pouech committed
628
    numFrame = insize = 0;
629
    while (mmioDescend(infoPtr->hMMio, &mmckInfo, &mmckList, 0) == 0 &&
Eric Pouech's avatar
Eric Pouech committed
630 631 632 633 634
	   numFrame < infoPtr->mah.dwTotalFrames) {
	infoPtr->lpIndex[numFrame] = mmckInfo.dwDataOffset;
	if (insize < mmckInfo.cksize)
	    insize = mmckInfo.cksize;
	numFrame++;
635
	mmioAscend(infoPtr->hMMio, &mmckInfo, 0);
Eric Pouech's avatar
Eric Pouech committed
636 637
    }
    if (numFrame != infoPtr->mah.dwTotalFrames) {
638
	WARN("Found %d frames (/%d)\n", numFrame, infoPtr->mah.dwTotalFrames);
Eric Pouech's avatar
Eric Pouech committed
639 640 641
	return FALSE;
    }
    if (insize > infoPtr->ash.dwSuggestedBufferSize) {
642
	WARN("insize=%d suggestedSize=%d\n", insize, infoPtr->ash.dwSuggestedBufferSize);
Eric Pouech's avatar
Eric Pouech committed
643 644 645
	infoPtr->ash.dwSuggestedBufferSize = insize;
    }

646
    infoPtr->indata = Alloc(infoPtr->ash.dwSuggestedBufferSize);
647
    if (!infoPtr->indata) 
Eric Pouech's avatar
Eric Pouech committed
648
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
649 650 651 652 653

    return TRUE;
}


654
static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
Eric Pouech's avatar
Eric Pouech committed
655 656
{
    DWORD	outSize;
Alexandre Julliard's avatar
Alexandre Julliard committed
657

658
    /* check uncompressed AVI */
659
    if ((infoPtr->ash.fccHandler == mmioFOURCC('D', 'I', 'B', ' ')) ||
660 661
       (infoPtr->ash.fccHandler == mmioFOURCC('R', 'L', 'E', ' ')) ||
       (infoPtr->ash.fccHandler == mmioFOURCC(0, 0, 0, 0)))
662
    {
663
        infoPtr->hic = 0;
664 665
	return TRUE;
    }
666 667

    /* try to get a decompressor for that type */
668
    infoPtr->hic = fnIC.fnICOpen(ICTYPE_VIDEO, infoPtr->ash.fccHandler, ICMODE_DECOMPRESS);
Eric Pouech's avatar
Eric Pouech committed
669 670 671 672
    if (!infoPtr->hic) {
	WARN("Can't load codec for the file\n");
	return FALSE;
    }
673 674

    outSize = fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
Frank Richter's avatar
Frank Richter committed
675
			    (DWORD_PTR)infoPtr->inbih, 0L);
676

677
    infoPtr->outbih = Alloc(outSize);
678
    if (!infoPtr->outbih)
Eric Pouech's avatar
Eric Pouech committed
679
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
680

681
    if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_GET_FORMAT,
682
		      (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) 
683
    {
Eric Pouech's avatar
Eric Pouech committed
684 685 686
	WARN("Can't get output BIH\n");
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
687

688
    infoPtr->outdata = Alloc(infoPtr->outbih->biSizeImage);
689
    if (!infoPtr->outdata) 
Alexandre Julliard's avatar
Alexandre Julliard committed
690
	return FALSE;
Eric Pouech's avatar
Eric Pouech committed
691

692
    if (fnIC.fnICSendMessage(infoPtr->hic, ICM_DECOMPRESS_BEGIN,
Frank Richter's avatar
Frank Richter committed
693
		      (DWORD_PTR)infoPtr->inbih, (DWORD_PTR)infoPtr->outbih) != ICERR_OK) {
Eric Pouech's avatar
Eric Pouech committed
694 695 696 697 698 699 700
	WARN("Can't begin decompression\n");
	return FALSE;
    }

    return TRUE;
}

701

702
static BOOL ANIMATE_OpenW(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPWSTR lpszName)
Eric Pouech's avatar
Eric Pouech committed
703
{
704 705
    HDC hdc;

Eric Pouech's avatar
Eric Pouech committed
706 707
    ANIMATE_Free(infoPtr);

708 709
    if (!lpszName) 
    {
Eric Pouech's avatar
Eric Pouech committed
710
	TRACE("Closing avi!\n");
711 712
        /* installer of thebat! v1.62 requires FALSE here */
	return (infoPtr->hMMio != 0);
Eric Pouech's avatar
Eric Pouech committed
713
    }
714

Eric Pouech's avatar
Eric Pouech committed
715
    if (!hInstance)
716
        hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
Eric Pouech's avatar
Eric Pouech committed
717

Frank Richter's avatar
Frank Richter committed
718
    TRACE("(%s)\n", debugstr_w(lpszName));
Alexandre Julliard's avatar
Alexandre Julliard committed
719

720
    if (!IS_INTRESOURCE(lpszName))
Frank Richter's avatar
Frank Richter committed
721
    {
722 723
	if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName)) 
        {
Eric Pouech's avatar
Eric Pouech committed
724
	    TRACE("No AVI resource found!\n");
725 726
	    if (!ANIMATE_LoadFileW(infoPtr, lpszName)) 
            {
Eric Pouech's avatar
Eric Pouech committed
727 728 729 730
		WARN("No AVI file found!\n");
		return FALSE;
	    }
	}
731 732 733
    } 
    else 
    {
Frank Richter's avatar
Frank Richter committed
734
	if (!ANIMATE_LoadResW(infoPtr, hInstance, lpszName))
735
        {
Eric Pouech's avatar
Eric Pouech committed
736 737 738
	    WARN("No AVI resource found!\n");
	    return FALSE;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
739 740
    }

741 742
    if (!ANIMATE_GetAviInfo(infoPtr)) 
    {
Eric Pouech's avatar
Eric Pouech committed
743 744 745 746
	WARN("Can't get AVI information\n");
	ANIMATE_Free(infoPtr);
	return FALSE;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
747

748 749
    if (!ANIMATE_GetAviCodec(infoPtr)) 
    {
Eric Pouech's avatar
Eric Pouech committed
750 751 752
	WARN("Can't get AVI Codec\n");
	ANIMATE_Free(infoPtr);
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
753 754
    }

755 756 757 758 759 760
    hdc = GetDC(infoPtr->hwndSelf);
    /* native looks at the top left pixel of the first frame here too. */
    infoPtr->hbrushBG = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
                                             (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
    ReleaseDC(infoPtr->hwndSelf, hdc);

761
    if (!(infoPtr->dwStyle & ACS_CENTER))
762
	SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, infoPtr->mah.dwWidth, infoPtr->mah.dwHeight,
Eric Pouech's avatar
Eric Pouech committed
763 764
		     SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER);

765
    if (infoPtr->dwStyle & ACS_AUTOPLAY) 
766
	return ANIMATE_Play(infoPtr, -1, 0, infoPtr->mah.dwTotalFrames - 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
767 768 769 770 771

    return TRUE;
}


772
static BOOL ANIMATE_OpenA(ANIMATE_INFO *infoPtr, HINSTANCE hInstance, LPSTR lpszName)
Alexandre Julliard's avatar
Alexandre Julliard committed
773
{
774 775 776 777
    LPWSTR lpwszName;
    LRESULT result;
    INT len;

778
    if (IS_INTRESOURCE(lpszName))
779
        return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
Alexandre Julliard's avatar
Alexandre Julliard committed
780

781
    len = MultiByteToWideChar(CP_ACP, 0, lpszName, -1, NULL, 0);
782
    lpwszName = Alloc(len * sizeof(WCHAR));
783 784 785 786
    if (!lpwszName) return FALSE;
    MultiByteToWideChar(CP_ACP, 0, lpszName, -1, lpwszName, len);

    result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
787
    Free (lpwszName);
788 789 790 791 792 793
    return result;
}


static BOOL ANIMATE_Stop(ANIMATE_INFO *infoPtr)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
794
    /* nothing opened */
Eric Pouech's avatar
Eric Pouech committed
795
    if (!infoPtr->hMMio)
Alexandre Julliard's avatar
Alexandre Julliard committed
796
	return FALSE;
Eric Pouech's avatar
Eric Pouech committed
797 798

    ANIMATE_DoStop(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
799 800 801 802
    return TRUE;
}


803
static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
804
{
805 806
    static const WCHAR msvfw32W[] = { 'm', 's', 'v', 'f', 'w', '3', '2', '.', 'd', 'l', 'l', 0 };
    ANIMATE_INFO *infoPtr;
Alexandre Julliard's avatar
Alexandre Julliard committed
807

808
    if (!fnIC.hModule)
809
    {
810
	fnIC.hModule = LoadLibraryW(msvfw32W);
811 812 813 814 815 816 817 818
	if (!fnIC.hModule) return FALSE;

	fnIC.fnICOpen        = (void*)GetProcAddress(fnIC.hModule, "ICOpen");
	fnIC.fnICClose       = (void*)GetProcAddress(fnIC.hModule, "ICClose");
	fnIC.fnICSendMessage = (void*)GetProcAddress(fnIC.hModule, "ICSendMessage");
	fnIC.fnICDecompress  = (void*)GetProcAddress(fnIC.hModule, "ICDecompress");
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
819
    /* allocate memory for info structure */
820
    infoPtr = Alloc(sizeof(ANIMATE_INFO));
821
    if (!infoPtr) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
822

Eric Pouech's avatar
Eric Pouech committed
823
    /* store crossref hWnd <-> info structure */
824
    SetWindowLongPtrW(hWnd, 0, (DWORD_PTR)infoPtr);
825
    infoPtr->hwndSelf = hWnd;
826
    infoPtr->hwndNotify = lpcs->hwndParent;
827
    infoPtr->transparentColor = ANIMATE_COLOR_NONE;
828
    infoPtr->hbmPrevFrame = 0;
829
    infoPtr->dwStyle = lpcs->style;
830

831
    TRACE("Animate style=0x%08x, parent=%p\n", infoPtr->dwStyle, infoPtr->hwndNotify);
832

Eric Pouech's avatar
Eric Pouech committed
833
    InitializeCriticalSection(&infoPtr->cs);
834
    infoPtr->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ANIMATE_INFO*->cs");
835

836
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
837 838 839
}


840
static LRESULT ANIMATE_Destroy(ANIMATE_INFO *infoPtr)
Alexandre Julliard's avatar
Alexandre Julliard committed
841
{
Alexandre Julliard's avatar
Alexandre Julliard committed
842
    /* free avi data */
Eric Pouech's avatar
Eric Pouech committed
843
    ANIMATE_Free(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
844 845

    /* free animate info data */
846
    SetWindowLongPtrW(infoPtr->hwndSelf, 0, 0);
847

848
    infoPtr->cs.DebugInfo->Spare[0] = 0;
849
    DeleteCriticalSection(&infoPtr->cs);
850
    Free(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
851 852 853 854 855

    return 0;
}


856
static BOOL ANIMATE_EraseBackground(ANIMATE_INFO const *infoPtr, HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
857
{
Eric Pouech's avatar
Eric Pouech committed
858
    RECT rect;
859
    HBRUSH hBrush;
Alexandre Julliard's avatar
Alexandre Julliard committed
860

861 862
    hBrush = (HBRUSH)SendMessageW(infoPtr->hwndNotify, WM_CTLCOLORSTATIC,
                                  (WPARAM)hdc, (LPARAM)infoPtr->hwndSelf);
863 864
    GetClientRect(infoPtr->hwndSelf, &rect);
    FillRect(hdc, &rect, hBrush ? hBrush : GetCurrentObject(hdc, OBJ_BRUSH));
Eric Pouech's avatar
Eric Pouech committed
865

Alexandre Julliard's avatar
Alexandre Julliard committed
866 867 868
    return TRUE;
}

869

870
static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
Eric Pouech's avatar
Eric Pouech committed
871
{
872
    TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
873 874 875 876 877 878 879 880
          wStyleType, lpss->styleOld, lpss->styleNew);

    if (wStyleType != GWL_STYLE) return 0;
  
    infoPtr->dwStyle = lpss->styleNew;

    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
    return 0;
Eric Pouech's avatar
Eric Pouech committed
881
}
Alexandre Julliard's avatar
Alexandre Julliard committed
882

883

Eric Pouech's avatar
Eric Pouech committed
884
static LRESULT WINAPI ANIMATE_WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
885
{
886 887
    ANIMATE_INFO *infoPtr = (ANIMATE_INFO *)GetWindowLongPtrW(hWnd, 0);

888
    TRACE("hwnd=%p msg=%x wparam=%lx lparam=%lx\n", hWnd, uMsg, wParam, lParam);
889 890
    if (!infoPtr && (uMsg != WM_NCCREATE))
	return DefWindowProcW(hWnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
891 892
    switch (uMsg)
    {
Eric Pouech's avatar
Eric Pouech committed
893
    case ACM_OPENA:
894
	return ANIMATE_OpenA(infoPtr, (HINSTANCE)wParam, (LPSTR)lParam);
895

896
    case ACM_OPENW:
897
	return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
898

Eric Pouech's avatar
Eric Pouech committed
899
    case ACM_PLAY:
900
	return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
901

Eric Pouech's avatar
Eric Pouech committed
902
    case ACM_STOP:
903
	return ANIMATE_Stop(infoPtr);
904

905 906 907 908
    case WM_CLOSE:
	ANIMATE_Free(infoPtr);
	return 0;

Eric Pouech's avatar
Eric Pouech committed
909
    case WM_NCCREATE:
910
	return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
911

Eric Pouech's avatar
Eric Pouech committed
912 913 914 915
    case WM_NCHITTEST:
	return HTTRANSPARENT;

    case WM_DESTROY:
916
	return ANIMATE_Destroy(infoPtr);
917

Eric Pouech's avatar
Eric Pouech committed
918
    case WM_ERASEBKGND:
919
	return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
Eric Pouech's avatar
Eric Pouech committed
920

921 922
    case WM_STYLECHANGED:
        return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
Eric Pouech's avatar
Eric Pouech committed
923 924

    case WM_TIMER:
925
        return ANIMATE_Timer(infoPtr);
926

927
    case WM_PRINTCLIENT:
Eric Pouech's avatar
Eric Pouech committed
928
    case WM_PAINT:
929
        {
930
            /* the animation has not decompressed
931 932
             * (and displayed) the first frame yet, don't paint
             */
933
            if (!infoPtr->hbmPrevFrame)
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
            {
                /* default paint handling */
                return DefWindowProcW(hWnd, uMsg, wParam, lParam);
            }

            if (wParam)
            {
                EnterCriticalSection(&infoPtr->cs);
                ANIMATE_PaintFrame(infoPtr, (HDC)wParam);
                LeaveCriticalSection(&infoPtr->cs);
            }
            else
            {
                PAINTSTRUCT ps;
                HDC hDC = BeginPaint(infoPtr->hwndSelf, &ps);

                EnterCriticalSection(&infoPtr->cs);
                ANIMATE_PaintFrame(infoPtr, hDC);
                LeaveCriticalSection(&infoPtr->cs);

                EndPaint(infoPtr->hwndSelf, &ps);
            }
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
958

Eric Pouech's avatar
Eric Pouech committed
959
    case WM_SIZE:
960 961 962
        if (infoPtr->dwStyle & ACS_CENTER) 
	    InvalidateRect(infoPtr->hwndSelf, NULL, TRUE);
	return DefWindowProcW(hWnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
963

Eric Pouech's avatar
Eric Pouech committed
964
    default:
965
	if ((uMsg >= WM_USER) && (uMsg < WM_APP) && !COMCTL32_IsReflectedMessage(uMsg))
966
	    ERR("unknown msg %04x wp=%08lx lp=%08lx\n", uMsg, wParam, lParam);
967

968
	return DefWindowProcW(hWnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
969 970 971 972
    }
    return 0;
}

Eric Pouech's avatar
Eric Pouech committed
973
void ANIMATE_Register(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
974
{
975
    WNDCLASSW wndClass;
Alexandre Julliard's avatar
Alexandre Julliard committed
976

977
    ZeroMemory(&wndClass, sizeof(WNDCLASSW));
Alexandre Julliard's avatar
Alexandre Julliard committed
978
    wndClass.style         = CS_GLOBALCLASS | CS_DBLCLKS;
979
    wndClass.lpfnWndProc   = ANIMATE_WindowProc;
Alexandre Julliard's avatar
Alexandre Julliard committed
980 981
    wndClass.cbClsExtra    = 0;
    wndClass.cbWndExtra    = sizeof(ANIMATE_INFO *);
982
    wndClass.hCursor       = LoadCursorW(0, (LPCWSTR)IDC_ARROW);
983
    wndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
984
    wndClass.lpszClassName = ANIMATE_CLASSW;
985

986
    RegisterClassW(&wndClass);
Alexandre Julliard's avatar
Alexandre Julliard committed
987
}
988 989


Eric Pouech's avatar
Eric Pouech committed
990
void ANIMATE_Unregister(void)
991
{
992
    UnregisterClassW(ANIMATE_CLASSW, NULL);
993
}