animate.c 28.3 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
        SetRect(&rect, 0, 0, nWidth, nHeight);
316

317 318 319 320 321 322 323 324 325
        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);
326 327
        DeleteObject(infoPtr->hbmPrevFrame);
        infoPtr->hbmPrevFrame = hbmFinal;
328
    }
329

330
    if (infoPtr->dwStyle & ACS_CENTER)
331
    {
332
        RECT rect;
333

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

340 341
    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);
Eric Pouech's avatar
Eric Pouech committed
342 343
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
344

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

349 350
    mmioSeek(infoPtr->hMMio, infoPtr->lpIndex[infoPtr->currFrame], SEEK_SET);
    mmioRead(infoPtr->hMMio, infoPtr->indata, infoPtr->ash.dwSuggestedBufferSize);
351

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

359
    ANIMATE_PaintFrame(infoPtr, hDC);
Eric Pouech's avatar
Eric Pouech committed
360 361 362 363 364 365 366 367

    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
368 369 370 371 372
    }

    return TRUE;
}

373 374
static LRESULT ANIMATE_Timer(ANIMATE_INFO *infoPtr)
{
375 376 377 378 379 380 381 382 383 384
    HDC	hDC;

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

	ReleaseDC(infoPtr->hwndSelf, hDC);
    }
385 386 387 388

    return 0;
}

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

395
    while(1)
396
    {
397 398
        HDC hDC = GetDC(infoPtr->hwndSelf);

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

405 406
        ReleaseDC(infoPtr->hwndSelf, hDC);

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

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

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

425 426 427
    infoPtr->nFromFrame = wFrom;
    infoPtr->nToFrame   = wTo;
    infoPtr->nLoop      = cRepeat;
Eric Pouech's avatar
Eric Pouech committed
428 429 430 431

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

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

435 436 437 438
    if (infoPtr->nFromFrame >= infoPtr->mah.dwTotalFrames &&
        (SHORT)infoPtr->nFromFrame < 0)
        infoPtr->nFromFrame = 0;

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

    infoPtr->currFrame = infoPtr->nFromFrame;

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

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

	    ReleaseDC(infoPtr->hwndSelf, hDC);
        }
457 458 459
        return TRUE;
    }

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

Alexandre Julliard's avatar
Alexandre Julliard committed
475
    }
476

Eric Pouech's avatar
Eric Pouech committed
477 478 479
    ANIMATE_Notify(infoPtr, ACN_START);

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
480 481 482
}


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

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

Eric Pouech's avatar
Eric Pouech committed
497 498 499 500 501
    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
502

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

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

515 516
    mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->mah, sizeof(infoPtr->mah));

517 518 519 520 521 522 523 524 525 526
    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);
527 528

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

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

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

542 543
    mmioRead(infoPtr->hMMio, (LPSTR)&infoPtr->ash, sizeof(infoPtr->ash));

544 545 546
    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
547
	                                        HIBYTE(HIWORD(infoPtr->ash.fccType)));
548 549 550
    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
551
	                                        HIBYTE(HIWORD(infoPtr->ash.fccHandler)));
552
    TRACE("ash.dwFlags=%d\n", 			infoPtr->ash.dwFlags);
Eric Pouech's avatar
Eric Pouech committed
553 554
    TRACE("ash.wPriority=%d\n", 		infoPtr->ash.wPriority);
    TRACE("ash.wLanguage=%d\n", 		infoPtr->ash.wLanguage);
555 556 557 558 559 560 561 562
    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);
563
    TRACE("ash.rcFrame=(%d,%d,%d,%d)\n", 	infoPtr->ash.rcFrame.top, infoPtr->ash.rcFrame.left,
Eric Pouech's avatar
Eric Pouech committed
564
	  infoPtr->ash.rcFrame.bottom, infoPtr->ash.rcFrame.right);
565 566

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

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

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

580 581
    mmioRead(infoPtr->hMMio, (LPSTR)infoPtr->inbih, mmckInfo.cksize);

582 583 584
    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
585 586
    TRACE("bih.biPlanes=%d\n", 		infoPtr->inbih->biPlanes);
    TRACE("bih.biBitCount=%d\n", 	infoPtr->inbih->biBitCount);
587 588 589 590 591 592
    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
593

594 595 596
    mmioAscend(infoPtr->hMMio, &mmckInfo, 0);

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

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

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

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

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

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

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

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

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

    return TRUE;
}


651
static BOOL ANIMATE_GetAviCodec(ANIMATE_INFO *infoPtr)
Eric Pouech's avatar
Eric Pouech committed
652 653
{
    DWORD	outSize;
Alexandre Julliard's avatar
Alexandre Julliard committed
654

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

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

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

674
    infoPtr->outbih = Alloc(outSize);
675
    if (!infoPtr->outbih)
Eric Pouech's avatar
Eric Pouech committed
676
	return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
677

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

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

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

    return TRUE;
}

698

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

Eric Pouech's avatar
Eric Pouech committed
703 704
    ANIMATE_Free(infoPtr);

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

Eric Pouech's avatar
Eric Pouech committed
712
    if (!hInstance)
713
        hInstance = (HINSTANCE)GetWindowLongPtrW(infoPtr->hwndSelf, GWLP_HINSTANCE);
Eric Pouech's avatar
Eric Pouech committed
714

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

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

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

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

752 753 754 755 756 757
    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);

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

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

    return TRUE;
}


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

775
    if (IS_INTRESOURCE(lpszName))
776
        return ANIMATE_OpenW(infoPtr, hInstance, (LPWSTR)lpszName);
Alexandre Julliard's avatar
Alexandre Julliard committed
777

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

    result = ANIMATE_OpenW(infoPtr, hInstance, lpwszName);
784
    Free (lpwszName);
785 786 787 788 789 790
    return result;
}


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

    ANIMATE_DoStop(infoPtr);
Alexandre Julliard's avatar
Alexandre Julliard committed
796 797 798 799
    return TRUE;
}


800
static BOOL ANIMATE_Create(HWND hWnd, const CREATESTRUCTW *lpcs)
Alexandre Julliard's avatar
Alexandre Julliard committed
801
{
802 803
    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
804

805
    if (!fnIC.hModule)
806
    {
807
	fnIC.hModule = LoadLibraryW(msvfw32W);
808 809 810 811 812 813 814 815
	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
816
    /* allocate memory for info structure */
817
    infoPtr = Alloc(sizeof(ANIMATE_INFO));
818
    if (!infoPtr) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
819

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

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

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

833
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
834 835 836
}


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

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

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

    return 0;
}


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

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

Alexandre Julliard's avatar
Alexandre Julliard committed
863 864 865
    return TRUE;
}

866

867
static LRESULT ANIMATE_StyleChanged(ANIMATE_INFO *infoPtr, WPARAM wStyleType, const STYLESTRUCT *lpss)
Eric Pouech's avatar
Eric Pouech committed
868
{
869
    TRACE("(styletype=%lx, styleOld=0x%08x, styleNew=0x%08x)\n",
870 871 872 873 874 875 876 877
          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
878
}
Alexandre Julliard's avatar
Alexandre Julliard committed
879

880

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

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

893
    case ACM_OPENW:
894
	return ANIMATE_OpenW(infoPtr, (HINSTANCE)wParam, (LPWSTR)lParam);
895

Eric Pouech's avatar
Eric Pouech committed
896
    case ACM_PLAY:
897
	return ANIMATE_Play(infoPtr, (INT)wParam, LOWORD(lParam), HIWORD(lParam));
898

Eric Pouech's avatar
Eric Pouech committed
899
    case ACM_STOP:
900
	return ANIMATE_Stop(infoPtr);
901

902 903 904 905
    case WM_CLOSE:
	ANIMATE_Free(infoPtr);
	return 0;

Eric Pouech's avatar
Eric Pouech committed
906
    case WM_NCCREATE:
907
	return ANIMATE_Create(hWnd, (LPCREATESTRUCTW)lParam);
908

Eric Pouech's avatar
Eric Pouech committed
909 910 911 912
    case WM_NCHITTEST:
	return HTTRANSPARENT;

    case WM_DESTROY:
913
	return ANIMATE_Destroy(infoPtr);
914

Eric Pouech's avatar
Eric Pouech committed
915
    case WM_ERASEBKGND:
916
	return ANIMATE_EraseBackground(infoPtr, (HDC)wParam);
Eric Pouech's avatar
Eric Pouech committed
917

918 919
    case WM_STYLECHANGED:
        return ANIMATE_StyleChanged(infoPtr, wParam, (LPSTYLESTRUCT)lParam);
Eric Pouech's avatar
Eric Pouech committed
920 921

    case WM_TIMER:
922
        return ANIMATE_Timer(infoPtr);
923

924
    case WM_PRINTCLIENT:
Eric Pouech's avatar
Eric Pouech committed
925
    case WM_PAINT:
926
        {
927
            /* the animation has not decompressed
928 929
             * (and displayed) the first frame yet, don't paint
             */
930
            if (!infoPtr->hbmPrevFrame)
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
            {
                /* 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
955

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

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

965
	return DefWindowProcW(hWnd, uMsg, wParam, lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
966 967 968 969
    }
    return 0;
}

Eric Pouech's avatar
Eric Pouech committed
970
void ANIMATE_Register(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
971
{
972
    WNDCLASSW wndClass;
Alexandre Julliard's avatar
Alexandre Julliard committed
973

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

983
    RegisterClassW(&wndClass);
Alexandre Julliard's avatar
Alexandre Julliard committed
984
}
985 986


Eric Pouech's avatar
Eric Pouech committed
987
void ANIMATE_Unregister(void)
988
{
989
    UnregisterClassW(ANIMATE_CLASSW, NULL);
990
}