os/graphics/windowing/windowserver/nga/SERVER/openwfc/SPRITE.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Sprite
    15 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include "sprite.h"
    20 #include "rootwin.h"
    21 #include "windowgroup.h"
    22 #include "ScrDev.H"
    23 #include "wstop.h"
    24 #include "ANIM.H"
    25 #include "panics.h"
    26 #include "EVENT.H"
    27 #include "bitgditomwsgraphicscontextmappings.h"
    28 #include "../debuglog/DEBUGLOG.H"
    29 
    30 static const TInt64 KFlashHalfSecond(500000); //interval for flashing sprites
    31 
    32 GLDEF_D CWsDeltaTimer *CWsSpriteBase::iDeltaTimer=NULL;
    33 
    34 TInt TimerCallBack(TAny *aPtr)
    35 	{
    36 	((CWsSpriteBase *)aPtr)->TimerExpired();
    37 	return(KErrNone);
    38 	}
    39 
    40 #ifndef _DEBUG
    41 
    42 #define LOG_SPRITE_REDRAW_START(sprite)
    43 #define LOG_SPRITE_REDRAW_END(sprite)
    44 #define LOG_SPRITE_FLASH(aSprite)
    45 
    46 #else
    47 
    48 #define LOG_SPRITE_REDRAW_START(sprite) LogSpriteRedrawStart(sprite)
    49 #define LOG_SPRITE_REDRAW_END(sprite) LogSpriteRedrawEnd(sprite)
    50 #define LOG_SPRITE_FLASH(sprite) LogSpriteFlash(sprite)
    51 
    52 extern CDebugLogBase *wsDebugLog;
    53 
    54 
    55 static void LogSpriteRedrawStart(const CWsSpriteBase& aSprite)
    56     {
    57     if (wsDebugLog)
    58         {
    59         _LIT(KAnnotateSpriteRedrawStart, ">> MWsDrawAnnotationObserver::FloatingSpriteRedrawStart [%S][app %d]");
    60         const TDesC& clientName = aSprite.WsOwner()->Client().FullName();
    61         TBuf<LogTBufSize> log;
    62         TTruncateOverflow overflow;
    63         log.AppendFormat(KAnnotateSpriteRedrawStart, &overflow, &clientName, aSprite.WsOwner()->ConnectionHandle());
    64         wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, log);
    65         }
    66     }
    67 
    68 static void LogSpriteRedrawEnd(const CWsSpriteBase& aSprite)
    69     {
    70     if (wsDebugLog)
    71         {
    72         _LIT(KAnnotateSpriteRedrawEnd, "<< MWsDrawAnnotationObserver::FloatingSpriteRedrawEnd [%S][app %d]");
    73         const TDesC& clientName = aSprite.WsOwner()->Client().FullName();
    74         TBuf<LogTBufSize> log;
    75         TTruncateOverflow overflow;
    76         log.AppendFormat(KAnnotateSpriteRedrawEnd, &overflow, &clientName, aSprite.WsOwner()->ConnectionHandle());
    77         wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, log);
    78         }
    79     }
    80 
    81 static void LogSpriteFlash(const CWsSpriteBase& /*aSprite*/)
    82     {
    83     if (wsDebugLog)
    84         {
    85         // The following code causes Exception and is commented out, see defect GFX09962
    86         
    87 //      _LIT(KAnnotateSpriteFlash, "-- MWsDrawAnnotationObserver::SpriteFlash:%d [%S][app %d]");
    88 //      const TDesC& clientName = aSprite.WsOwner()->Client().FullName();
    89 //      TBuf<LogTBufSize> log;
    90 //      TTruncateOverflow overflow;
    91 //      log.AppendFormat(KAnnotateSpriteFlash, &overflow, &clientName, aSprite.WsOwner()->ConnectionHandle());
    92 //      wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, log);
    93                 
    94         // This code is temporarily here until above problem is resolved
    95         _LIT(KAnnotateSpriteFlash, "-- MWsDrawAnnotationObserver::SpriteFlash");
    96         TBuf<LogTBufSize> log;
    97         log.AppendFormat(KAnnotateSpriteFlash);
    98         wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, log);
    99         }
   100     }
   101 
   102 
   103 #endif
   104 
   105 
   106 
   107 static void AnnotateSpriteRedrawStart(const CWsSpriteBase& aSprite, const TRegion& aRegion)
   108 	{
   109 	LOG_SPRITE_REDRAW_START(aSprite);
   110 	MWsDrawAnnotationObserver* annoObs = aSprite.Screen()->DrawAnnotationObserver();
   111 	if(annoObs)
   112 		{
   113 		annoObs->SpriteRedrawStart(aSprite, aRegion);
   114 		}
   115 	}
   116 
   117 static void AnnotateSpriteRedrawEnd(const CWsSpriteBase& aSprite)
   118 	{
   119 	LOG_SPRITE_REDRAW_END(aSprite);
   120 	MWsDrawAnnotationObserver* annoObs = aSprite.Screen()->DrawAnnotationObserver();
   121 	if(annoObs)
   122 		{
   123 		annoObs->SpriteRedrawEnd(aSprite);
   124 		}		
   125 	}
   126 
   127 static void AnnotateSpriteFlash(const CWsSpriteBase& aSprite, TBool aFlashOn)
   128 	{
   129 	LOG_SPRITE_FLASH(aSprite);
   130 	MWsDrawAnnotationObserver* annoObs = aSprite.Screen()->DrawAnnotationObserver();
   131 	if(annoObs)
   132 		{
   133 		annoObs->SpriteFlash(aSprite, aFlashOn);
   134 		}	
   135 	}
   136 
   137 
   138 //
   139 // CWsSpriteMember
   140 //
   141 
   142 CWsSpriteMember::CWsSpriteMember()
   143 	{
   144 	}
   145 
   146 CWsSpriteMember::~CWsSpriteMember()
   147 	{
   148 	delete iBitmap;
   149 	delete iMaskBitmap;
   150 	}
   151 
   152 TBool CWsSpriteMember::SetL(const TCmdSpriteMember &aCmdSpriteMember)
   153 	{
   154 	if (aCmdSpriteMember.iBitmap!=0)	// Null member of sequence, time is only valid field in member data
   155 		{
   156 		iBitmap=new(ELeave) CFbsBitmap();
   157 		TInt ret = iBitmap->Duplicate(aCmdSpriteMember.iBitmap);
   158 		if (ret == KErrNoMemory)
   159 			{
   160 			User::Leave(ret);
   161 			}
   162 		if (ret != KErrNone)
   163 			return(ETrue);
   164 
   165 		if (aCmdSpriteMember.iMaskBitmap)
   166 			{
   167 			iMaskBitmap=new(ELeave) CFbsBitmap();
   168 			TInt ret = iMaskBitmap->Duplicate(aCmdSpriteMember.iMaskBitmap);
   169 			if (ret == KErrNoMemory)
   170 				{
   171 				User::Leave(ret);
   172 				}
   173 			if (ret != KErrNone)
   174 				return(ETrue);
   175 			}
   176 		iInvertMask=aCmdSpriteMember.iInvertMask;
   177 		iDrawMode=aCmdSpriteMember.iDrawMode;
   178 		iOffset=aCmdSpriteMember.iOffset;
   179 		}
   180 	iInterval=aCmdSpriteMember.iInterval;
   181 	return(EFalse);
   182 	}
   183 
   184 //
   185 // CWsSpriteBase
   186 //
   187 
   188 TBool CWsSpriteBase::UpdateMemberL(CWsSpriteMember *aMember, const TCmdSpriteMember &aCmdSpriteMember)
   189 	{
   190 	CFbsBitmap *old=aMember->iBitmap;
   191 	CFbsBitmap *oldMask=aMember->iMaskBitmap;
   192 	aMember->iBitmap=NULL;
   193 	aMember->iMaskBitmap=NULL;
   194 	TBool ret=EFalse;
   195 	TRAPD(err,ret=aMember->SetL(aCmdSpriteMember));
   196 	if (err!=KErrNone)
   197 		{
   198 um_error:
   199 		delete aMember->iBitmap;
   200 		delete aMember->iMaskBitmap;
   201 		aMember->iBitmap=old;
   202 		aMember->iMaskBitmap=oldMask;
   203 		User::Leave(err);
   204 		}
   205 	TRAP(err,CheckSizesL());
   206 	if (err!=KErrNone)
   207 		goto um_error;
   208 	SetMember(0);
   209 	delete old;
   210 	delete oldMask;
   211 	return(ret);
   212 	}
   213 
   214 void CWsSpriteBase::InitStaticsL()
   215 	{
   216 	iDeltaTimer=CWsDeltaTimer::NewL(ESpriteAnimatePriority);
   217 	}
   218 
   219 void CWsSpriteBase::DeleteStatics()
   220 	{
   221 	delete iDeltaTimer;
   222 	}
   223 
   224 CWsSpriteBase::CWsSpriteBase(CWsClient *owner, WH_HANDLES aType) : CWsScreenObject(owner,aType,owner->Screen())
   225 	{
   226 	}
   227 
   228 CWsSpriteBase::~CWsSpriteBase()
   229 	{
   230 	Deactivate();
   231 	if(IsFloating() && iGroupWin)
   232 		{
   233 		iGroupWin->RemoveSprite(this);
   234 		}
   235 	
   236 	//iDeltaTimer->Remove(iDeltaTimerEntry);
   237 	if (iMembers)
   238 		{
   239 		iMembers->ResetAndDestroy();
   240 		delete iMembers;
   241 		}
   242 	}
   243 
   244 void CWsSpriteBase::ForceRedraw()
   245 	{
   246 	TRegionFix<1> region;
   247 	region.AddRect(Rect());
   248 	Screen()->AddRedrawRegion(region);
   249 	}
   250 
   251 void CWsSpriteBase::Deactivate()
   252 	{
   253 	//Disconnect from the sprite list and hide the sprite
   254 	if (iFlags & ESpriteActive)
   255 		{
   256 		iFlags&=~ESpriteActive;
   257 		
   258 		MWsWindowTreeObserver* const windowTreeObserver = Screen()->WindowTreeObserver();
   259 		if (windowTreeObserver)
   260 			{
   261 			windowTreeObserver->NodeReleased(*this);
   262 			}
   263 		
   264 		if (iMembers && iMembers->Count()>1)
   265 			{
   266 			iDeltaTimer->Remove(iDeltaTimerEntry);
   267 			}
   268 		
   269 		if (IsFloating())
   270 			{
   271 			Screen()->SpriteManager()->RemoveFloatingSprite(this);
   272 			if (!Screen()->ChangeTracking())
   273 				{
   274 				ForceRedraw();
   275 				}
   276 			}
   277 
   278 		// Note: This could be a floating sprite attached to the root window (PDEF138379)
   279 		if(iWin)
   280 			{
   281 			iWin->RemoveSprite(this);
   282 			}
   283 		}
   284 	}
   285 
   286 /** Called from groupwin destructor only */
   287 void CWsSpriteBase::DisconnectGroupWin()
   288 	{
   289 	WS_ASSERT_DEBUG(IsFloating(),EWsPanicFloatingSprite);
   290 	iGroupWin=NULL;
   291 	}
   292 
   293 void CWsSpriteBase::CheckSizesL()
   294 	{
   295 	iMaxSize.SetSize(0,0);
   296 	for(TInt index=0;index<iMembers->Count();index++)
   297 		{
   298 		CWsSpriteMember *wsm=(*iMembers)[index];
   299 		if (wsm->iBitmap)
   300 			{
   301 			TSize size=wsm->iBitmap->SizeInPixels();
   302 			if (wsm->iMaskBitmap)
   303 				{
   304 				TSize maskSize=wsm->iMaskBitmap->SizeInPixels();
   305 				if (maskSize.iWidth<size.iWidth || maskSize.iHeight<size.iHeight)
   306 					OwnerPanic(EWservPanicMaskSize);
   307 				}
   308 			if (size.iWidth>iMaxSize.iWidth)
   309 				iMaxSize.iWidth=size.iWidth;
   310 			if (size.iHeight>iMaxSize.iHeight)
   311 				iMaxSize.iHeight=size.iHeight;
   312 			}
   313 		}
   314 	}
   315 
   316 void CWsSpriteBase::ConstructL(TUint aFlags, CWsWindow *aWindow)
   317 	{
   318 	// Common part of construct for both sprites and pointer cursors
   319 	iFlags=aFlags;
   320 /*
   321 	if (iFlags&ESpriteNoChildClip)
   322 		iLink.iPriority+=ENoChildPriorityBoost;
   323 	if (iFlags&ESpritePointer)
   324 		iLink.iPriority+=EPointerPriorityBoost;
   325 */
   326 	iWin=aWindow;
   327 	TCallBack callback(TimerCallBack,this);
   328 	iDeltaTimerEntry.Set(callback);
   329 	iMembers=new(ELeave) CArrayPtrFlat<CWsSpriteMember>(10);
   330 	}
   331 
   332 void CWsSpriteBase::AppendMemberL(const TCmdSpriteMember &aCmdSpriteMember)
   333 	{
   334 	CWsSpriteMember *&pwsm=iMembers->ExtendL();
   335 	pwsm=NULL;	// In case new leaves
   336 	pwsm=new(ELeave) CWsSpriteMember();
   337 	// coverity[leave_without_push]
   338 	// pwsm is not owned by the stack and will be deleted by RWsSpriteBase::Close()
   339 	if (pwsm->SetL(aCmdSpriteMember))
   340 		OwnerPanic(EWservPanicBitmap);
   341 	}
   342 
   343 void CWsSpriteBase::CompleteL()
   344 	{
   345 	if (iMembers->Count() <= 0)
   346 		{
   347 		if(iWin)
   348 			iWin->OwnerPanic(EWservPanicNoSpriteMember);
   349 		//Not sure if this is a neccessary fall back if iWin is NULL.
   350 		else if(iWin==NULL && iGroupWin)
   351 			iGroupWin->OwnerPanic(EWservPanicNoSpriteMember);
   352 		}
   353 	else
   354 		{
   355 		iMembers->Compress();
   356 		CheckSizesL();
   357 		SetMember(0);
   358 		}
   359 	}
   360 
   361 void CWsSpriteBase::Activate()
   362 	{
   363 	if (iFlags&ESpriteDisabled)
   364 		{
   365 		iFlags&=~ESpriteDisabled;
   366 		}
   367 	if (iMembers->Count()>1)
   368 		{
   369 		QueueDeltaTimer();
   370 		iDeltaTimer->Activate();
   371 		}
   372 	iFlags|=ESpriteActive;
   373 	if(iWin)	
   374 		iWin->AddSprite(this);
   375 	SetDirty(ETrue);
   376 	Screen()->SpriteManager()->Schedule(this);
   377 	if(IsFloating())
   378 		{		
   379 		Screen()->SpriteManager()->AddFloatingSprite(this);
   380 		if (!Screen()->ChangeTracking())
   381 			ForceRedraw();
   382 		}
   383 	
   384 
   385 	// As custom text cursors are sprites (CWsCustomTextCursor) and can be activated/deactivated
   386 	// on various different windows during their lifetime, when activating
   387 	// a text cursor, we pretend it's just been created to give us the option
   388 	// of specifying a new parent window. Normal sprites (CWsSprite) are
   389 	// treated the same way just for consistency as it does no harm.
   390 	MWsWindowTreeObserver* const windowTreeObserver = Screen()->WindowTreeObserver();
   391 	if (windowTreeObserver)
   392 		{
   393 		windowTreeObserver->NodeCreated(*this, ParentNode());
   394 		windowTreeObserver->NodeExtentChanged(*this, Rect());
   395 		windowTreeObserver->NodeActivated(*this);
   396 		}
   397 	}
   398 
   399 void CWsSpriteBase::SetMember(TInt aIndex)
   400 	{
   401 	const TSize oldSize = iSize;
   402 	const TPoint oldPos = iPos;
   403 	if(IsFloating())
   404 		{		
   405 		TRect rect(iPos,iSize);
   406 		}
   407 	iCurIndex=aIndex;
   408 	iPos=iBasePos+(*iMembers)[iCurIndex]->iOffset;
   409 	if ((*iMembers)[iCurIndex]->iBitmap)
   410 		iSize=(*iMembers)[iCurIndex]->iBitmap->SizeInPixels();
   411 	else
   412 		iSize.SetSize(0,0);
   413 
   414 	if (iSize.iWidth > iMaxSize.iWidth || iSize.iHeight > iMaxSize.iHeight)
   415 		{
   416 		WS_ASSERT_DEBUG(EFalse, EWsPanicSpriteBitmapSizeChange);
   417 		CheckSizesL();
   418 		}
   419 
   420 	if(oldSize!=iSize || oldPos!=iPos)
   421 		NotifyExtentChanged();
   422 	
   423 	}
   424 
   425 void CWsSpriteBase::SetPos(const TPoint &aPos)
   426 	{
   427 	//Non-floating anim whose window is destroyed 
   428 	if (!IsFloating() && iWin==NULL) 
   429 		{ 
   430 		OwnerPanic(EWservPanicWindowDestroyed); 
   431 		} 
   432 
   433 	//Floating anim whose group window is destroyed 
   434 	if (IsFloating()  && iGroupWin==NULL && iWin->WinType() != EWinTypeRoot) 
   435 		{ 
   436 		OwnerPanic(EWservPanicWindowDestroyed); 
   437 		}
   438 	
   439 	if(IsFloating())
   440 		{		
   441 		TRect rect(iPos,iSize);
   442 		}
   443 
   444 	iBasePos=aPos;
   445 	TPoint newPos(iBasePos+(*iMembers)[iCurIndex]->iOffset);
   446 	if (iPos!=newPos)
   447 		{
   448 		if (!Screen()->ChangeTracking())
   449 			//Ensure the region covered by the sprite before as well as after the move gets scheduled for redraw
   450 			Screen()->SpriteManager()->Schedule(this);
   451 		iPos=newPos;
   452 		if (!Screen()->ChangeTracking())
   453 			Screen()->SpriteManager()->Schedule(this);
   454 		NotifyExtentChanged();
   455 		}
   456 	}
   457 
   458 void CWsSpriteBase::QueueDeltaTimer()
   459 	{
   460 	iDeltaTimer->Queue((*iMembers)[iCurIndex]->iInterval,iDeltaTimerEntry);
   461 	}
   462 
   463 void CWsSpriteBase::TimerExpired()
   464 	{
   465 	if (!Screen()->ChangeTracking())
   466 		Screen()->SpriteManager()->Schedule(this);
   467 	SetMember((iCurIndex+1)==iMembers->Count() ? 0 : iCurIndex+1);
   468 	SetDirty(ETrue);
   469 	Screen()->SpriteManager()->Schedule(this);
   470 	QueueDeltaTimer();
   471 	}
   472 
   473 TPoint CWsSpriteBase::Pos() const
   474 	{
   475 	if (iGroupWin || iWin==NULL || iWin->WinType() == EWinTypeRoot )
   476 		{
   477 		return(iPos);
   478 		}
   479 	return(iPos+iWin->Origin());
   480 	}
   481 
   482 TRect CWsSpriteBase::Rect() const
   483 	{
   484 	TRect rect;
   485 	rect.iTl=Pos();
   486 	rect.iBr=rect.iTl+iSize;
   487 	return(rect);
   488 	}
   489 
   490 MWsSprite::TSpriteType CWsSpriteBase::SpriteType() const
   491 	{
   492 	if (IsFloating())
   493 		return EFloatingSprite;
   494 
   495 	TSpriteType spriteType = EWindowSprite; 
   496 	switch(Type())
   497 		{
   498 	case WS_HANDLE_SPRITE:
   499 		spriteType = EWindowSprite;
   500 		break;
   501 	case WS_HANDLE_POINTER_CURSOR:
   502 		spriteType = EPointerCursorSprite;
   503 		break;
   504 	case WS_HANDLE_TEXT_CURSOR:
   505 		spriteType = ECustomTextCursorSprite;
   506 		break;
   507 	default:
   508 		ASSERT(0);
   509 		}
   510 	return spriteType;
   511 	}
   512 
   513 void CWsSpriteBase::CommandL(TInt aOpcode, const TAny *aCmdData)
   514 	{
   515 	TWsSpriteCmdUnion pData;
   516 
   517 	pData.any=aCmdData;
   518 	switch(aOpcode)
   519 		{
   520 		case EWsSpriteOpAppendMember:
   521 			AppendMemberL(*pData.SpriteMember);
   522 			break;
   523 		case EWsSpriteOpActivate:
   524 			if(!(iFlags&ESpriteActive))
   525 				CompleteL();
   526 			break;
   527 		case EWsSpriteOpUpdateMember:
   528 			if (pData.UpdateMember->index==iCurIndex)
   529 				{
   530 				SetDirty(ETrue);
   531 				TRect rect(Pos(), iMaxSize);
   532 				Screen()->SpriteManager()->Schedule(this,&rect);
   533 				}
   534 			break;
   535 		case EWsSpriteOpUpdateMember2:
   536 			{
   537 			SetDirty(ETrue);
   538 			Screen()->SpriteManager()->Schedule(this);
   539 			if (pData.UpdateMember->index<0 || pData.UpdateMember->index>=iMembers->Count())
   540 				User::Leave(KErrArgument);
   541 			CWsSpriteMember *member=(*iMembers)[pData.UpdateMember->index];
   542 			TBool ret=EFalse;
   543 			TRAPD(err,ret=UpdateMemberL(member,pData.UpdateMember->data));
   544 			if (err==KErrNone)
   545 				{
   546 				TRAP(err,CheckSizesL());
   547 				SetMember(0);
   548 				}
   549 			Screen()->SpriteManager()->Schedule(this);
   550 			User::LeaveIfError(err);
   551 			if (ret)
   552 				OwnerPanic(EWservPanicBitmap);
   553 			}
   554 			break;
   555 		default:
   556 			OwnerPanic(EWservPanicOpcode);
   557 			break;
   558 		}
   559 	}
   560 
   561 TBool CWsSpriteBase::CanBeSeen() const
   562 	{
   563 	if(iWin)
   564 		return (!(iFlags&ESpriteDisabled)) && (!iWin->VisibleRegion().IsEmpty());
   565 	else 
   566 		return (!(iFlags&ESpriteDisabled));
   567 	}
   568 
   569 void CWsSpriteBase::CalcRedrawRegion(const TRegion& aSourceRegion, TRegion& aTarget) const
   570 	{
   571 	aTarget.Copy(aSourceRegion);
   572 	if (ClipSprite())
   573 		{
   574 		TPoint origin(0,0);
   575 		if(iWin)
   576 			origin = iWin->Origin();
   577 		TRect rect(iBasePos + origin + iClipOffset, iClipSize);
   578 		aTarget.ClipRect(rect);
   579 		}
   580 	aTarget.ClipRect(RootWindow()->Abs());
   581 
   582 	// Only need to draw if the region being redrawn overlaps the sprite
   583 	const TRect spriteRect(Pos(), iSize);
   584 	STACK_REGION spriteRegion;
   585 	spriteRegion.AddRect(spriteRect);
   586 	aTarget.Intersect(spriteRegion);
   587 	spriteRegion.Close();
   588 	}
   589 
   590 /**
   591  @pre	CWsSpriteBase::CalcRedrawRegion(TRegion&,TRegion&) should have been called.
   592  @param aRegion Is the region that will definitely be redrawn if dirty. This param should
   593                 be calculated by calling CalcRedrawRegion(TRegion&,TRegion&)
   594  */
   595 void CWsSpriteBase::Redraw(MWsGraphicsContext * aGc, const TRegion& aRegion)
   596 	{
   597 	TFlashState currentState=EFlashOn;
   598 	if(IsFlashingEnabled())
   599 		{
   600 		currentState=Screen()->SpriteManager()->CurrentSpriteFlashState(this);
   601 		AnnotateSpriteFlash(*this, currentState==EFlashOn);
   602 		}
   603 
   604 	if(currentState==EFlashOn && (IsDirty() || HasAnimation()) )
   605 		{
   606 		const TRegion * pr = &aRegion;
   607 
   608 		if (pr->CheckError())
   609 			{
   610 			if(iWin)
   611 				{
   612 				if (Screen()->ChangeTracking())
   613 					pr = &iWin->WindowArea();
   614 				else
   615 					pr = &iWin->VisibleRegion();
   616 				}
   617 			else
   618 				pr = &RootWindow()->WindowArea();
   619 			}
   620 
   621 		if (!pr->IsEmpty())
   622 			{
   623 			CWsSpriteMember *member=(*iMembers)[iCurIndex];
   624 			if (member->iBitmap)
   625 				{
   626 				aGc->SetClippingRegion(*pr);
   627 
   628 				// Calculate which piece (rect) of the bitmap needs to be drawn
   629 				const TRect redrawRect = pr->BoundingRect();
   630 				TRect bitmapRect(Pos(), iSize); // sprite rect relative to screen
   631 				bitmapRect.Intersection(redrawRect);
   632 				bitmapRect.Move(-Pos()); // adjust relative to bitmap origin
   633 
   634 				if (member->iMaskBitmap)
   635 					aGc->BitBltMasked(Pos() + bitmapRect.iTl, *member->iBitmap, bitmapRect, *member->iMaskBitmap, member->iInvertMask);
   636 				else
   637 					{
   638 					aGc->SetDrawMode(BitGdiToMWsGraphicsContextMappings::LossyConvert(member->iDrawMode));
   639 					aGc->BitBlt(Pos() + bitmapRect.iTl, *member->iBitmap, bitmapRect);
   640 					aGc->SetDrawMode(MWsGraphicsContext::EDrawModePEN);
   641 					}
   642 				aGc->ResetClippingRegion();
   643 				}
   644 			}
   645 		if (Screen()->ChangeTracking())
   646 			SetDirty(EFalse);
   647 		
   648 		}
   649 		
   650 	//Flashing sprites need to reschedule themselves after drawing (unless they have
   651 	//an animation, because for animating sprites the rescheduling is done in CWsAnim).
   652 	if(IsFlashingEnabled() && !HasAnimation())
   653 		Screen()->SpriteManager()->Schedule(this);
   654 	}
   655 
   656 TBool CWsSpriteBase::IsActivated() const
   657 	{
   658 	return(iFlags&ESpriteActive);
   659 	}
   660 
   661 void CWsSpriteBase::SendState(MWsWindowTreeObserver& aWindowTreeObserver) const
   662 	{
   663 	if(iNext)
   664 		iNext->SendState(aWindowTreeObserver);
   665 	
   666 	if(IsActivated())
   667 		{
   668 		//Sprite NodeCreated must only be sent if activated
   669 		aWindowTreeObserver.NodeCreated(*this, ParentNode());
   670 		aWindowTreeObserver.NodeExtentChanged(*this, Rect());
   671 		aWindowTreeObserver.NodeActivated(*this);
   672 		}
   673 	}
   674 
   675 /** @see MWsWindowTreeNode */
   676 MWsWindowTreeNode::TType CWsSpriteBase::NodeType() const
   677 	{
   678 	return MWsWindowTreeNode::EWinTreeNodeSprite; 
   679 	}
   680 
   681 /** @see MWsWindowTreeNode */
   682 const MWsWindow* CWsSpriteBase::Window() const
   683 	{
   684 	return NULL;
   685 	}
   686 
   687 /** @see MWsWindowTreeNode */
   688 const MWsSprite* CWsSpriteBase::Sprite() const
   689 	{
   690 	return this;
   691 	}
   692 
   693 /** @see MWsWindowTreeNode */
   694 const MWsStandardTextCursor* CWsSpriteBase::StandardTextCursor() const
   695 	{
   696 	return NULL;
   697 	}
   698 
   699 /** @see MWsWindowTreeNode */
   700 const MWsWindowGroup* CWsSpriteBase::WindowGroup() const
   701 	{
   702 	if(iWin)
   703 		return iWin->WindowGroup();
   704 	else if (iGroupWin) 
   705 		return static_cast<MWsWindowGroup*>(iGroupWin); //floating Sprite
   706 	
   707 	WS_ASSERT_DEBUG(EFalse,EWsPanicInvalidOperation);
   708 	return NULL;
   709 	}
   710 
   711 /** @see MWsWindowTreeNode */
   712 const MWsWindowTreeNode* CWsSpriteBase::ParentNode() const
   713 	{
   714 	if (iWin)
   715 		return iWin;
   716 	else if (iGroupWin)
   717 		return iGroupWin->BaseParent(); //floating Sprite, will return the rootwin
   718 	
   719 	WS_ASSERT_DEBUG(EFalse,EWsPanicInvalidOperation);
   720 	return NULL;
   721 	}
   722 
   723 void CWsSpriteBase::NotifyExtentChanged() const
   724 	{
   725 	if (Screen())
   726 		{
   727 		MWsWindowTreeObserver* windowTreeObserver = Screen()->WindowTreeObserver();
   728 		if (windowTreeObserver && iFlags&ESpriteActive)
   729 			{
   730 			windowTreeObserver->NodeExtentChanged(*this, Rect());
   731 			}
   732 		}
   733 	}
   734 
   735 //
   736 // CWsSprite
   737 //
   738 
   739 CWsSprite::CWsSprite(CWsClient *owner) : CWsSpriteBase(owner,WS_HANDLE_SPRITE)
   740 	{
   741 	}
   742 
   743 CWsSprite::~CWsSprite()
   744 	{
   745 	if (!IsFloating() && IsActivated() && iWin && iWin->IsVisible() && !Screen()->ChangeTracking())
   746 	    ForceRedraw();
   747 	if (iAnim)
   748 		CWsAnim::CloseAnim(iAnim);
   749 	}
   750 
   751 void CWsSprite::ConstructL(const TWsClCmdCreateSprite &aParams)
   752 	{
   753 	NewObjL();
   754 	CWsWindowBase *win;
   755 	WsOwner()->HandleToWindow(aParams.window,&win);
   756 	WS_ASSERT_DEBUG(win,EWsPanicWindowNull);
   757 	if (win->WinType()==EWinTypeGroup)
   758 		{
   759 		//If a sprite is attached to a group window it is floating. 
   760 		//Floating sprite drawing is performed by the sprite manager.
   761 		iGroupWin=(CWsWindowGroup *)win;
   762 		win=NULL; //Floating sprites aren't associated with any particular window.
   763 		SetIsFloating(ETrue);
   764 		//In case the GroupWin is destroyed before the sprite it needs to call DisconnectGroupWin 
   765 		iGroupWin->AddSprite(this);
   766 		}
   767 	CWsSpriteBase::ConstructL(aParams.flags&ESpriteNonSystemFlags,(CWsWindow *)win);
   768 	iBasePos=aParams.pos;
   769 	}
   770 
   771 void CWsSprite::CompleteL()
   772 	{
   773 	CWsSpriteBase::CompleteL();
   774 	Activate();
   775 	}
   776 
   777 void CWsSprite::CommandL(TInt aOpcode, const TAny *aCmdData)
   778 	{
   779 	TWsSpriteCmdUnion pData;
   780 	pData.any=aCmdData;
   781 	switch(aOpcode)
   782 		{
   783 		case EWsSpriteOpSetPosition:
   784 			SetPos(*pData.Point);
   785 			break;
   786 		case EWsSpriteOpFree:
   787 			{
   788 			delete this;
   789 			break;
   790 			}
   791 		default:
   792 			CWsSpriteBase::CommandL(aOpcode, aCmdData);
   793 			break;
   794 		}
   795 	}
   796 
   797 /**
   798 @see MAnimSpriteFunctions::UpdateMember
   799 @param aFullUpdate	Not used. Wserv2 always do full back to front rendering, so there is no distinction between changes needing aFullUpdate or not 
   800  */
   801 void CWsSprite::Update(TInt aMember,TRect aRect,TBool /*aFullUpdate*/) 
   802 	{
   803 	if (iCurIndex!=aMember)
   804 		return;
   805 	aRect.Move(Pos());
   806 	aRect.Intersection(iScreen->CurrentScreenSize());
   807 	SetDirty(ETrue);
   808 	Screen()->SpriteManager()->Schedule(this, &aRect);
   809 	}
   810 
   811 //
   812 // CWsPointerCursor
   813 //
   814 
   815 CWsPointerCursor::CWsPointerCursor(CWsClient *owner) : CWsSpriteBase(owner,WS_HANDLE_POINTER_CURSOR)
   816 	{
   817 	}
   818 
   819 void CWsPointerCursor::CloseObject()
   820 	{
   821 	RemoveFromIndex();
   822 	Close();
   823 	}
   824 
   825 void CWsPointerCursor::Close()
   826 	{
   827 	WS_ASSERT_DEBUG(iAccessCount>0, EWsPanicPointerCursorAccessCount);
   828 	if (--iAccessCount==0)
   829 		delete this;
   830 	}
   831 
   832 void CWsPointerCursor::Open()
   833 	{
   834 	iAccessCount++;
   835 	}
   836 
   837 CWsPointerCursor::~CWsPointerCursor()
   838 	{
   839 	WS_ASSERT_DEBUG(iAccessCount==0, EWsPanicPointerCursorAccessCount);
   840 	}
   841 
   842 void CWsPointerCursor::ConstructL(const TWsClCmdCreatePointerCursor &aParams)
   843 	{
   844 	NewObjL();
   845 	SetIsFloating(ETrue);
   846 	CWsSpriteBase::ConstructL(ESpriteNoShadows|ESpriteNoChildClip|ESpritePointer|(aParams.flags&ESpriteNonSystemFlags),RootWindow());
   847 	Open();
   848 	}
   849 
   850 void CWsPointerCursor::CommandL(TInt aOpcode, const TAny *aCmdData)
   851 	{
   852 	switch(aOpcode)
   853 		{
   854 		case EWsSpriteOpFree:
   855 			CloseObject();
   856 			break;
   857 		default:
   858 			CWsSpriteBase::CommandL(aOpcode, aCmdData);
   859 			break;
   860 		}
   861 	}
   862 
   863 //
   864 // CWsCustomTextCursor
   865 //
   866 
   867 CWsCustomTextCursor::CWsCustomTextCursor (CWsClient *aOwner, RWsSession::TCustomTextCursorAlignment aAlignment) 
   868 : CWsSpriteBase(aOwner, WS_HANDLE_TEXT_CURSOR), iAlignment(aAlignment)
   869 	{
   870 	}
   871 
   872 CWsCustomTextCursor::~CWsCustomTextCursor()
   873 	{
   874 	}
   875 
   876 void CWsCustomTextCursor::ConstructL(TInt aFlags)
   877 	{
   878 	NewObjL();
   879 	CWsSpriteBase::ConstructL(ESpriteNoShadows|ESpriteNoChildClip|ESpritePointer|(aFlags&ESpriteNonSystemFlags), NULL);
   880 	}
   881 
   882 void CWsCustomTextCursor::CompleteL(CWsWindow *aWin, TBool aFlash, TBool aClipSprite, const TPoint& aClipOffset, const TSize& aClipSize)
   883 	{
   884 	iWin = aWin;
   885 	iFlags = aFlash ? iFlags | ESpriteFlash : iFlags & ~ESpriteFlash;
   886 	SetClipSprite(aClipSprite);
   887 	iClipOffset = aClipOffset;
   888 	iClipSize = aClipSize;
   889 	CWsSpriteBase::CompleteL();
   890 	}
   891 
   892 // Use SetPositionNoRedraw instead of SetPos when you just want to update
   893 // the custom text cursor position without redrawing it
   894 void CWsCustomTextCursor::SetPositionNoRedraw(const TPoint& aPos)
   895 	{
   896 	iBasePos = aPos;
   897 	TPoint newPos(iBasePos+(*iMembers)[iCurIndex]->iOffset);
   898 	iPos=newPos;
   899 	}
   900 
   901 void CWsCustomTextCursor::CommandL(TInt aOpcode, const TAny *aCmdData)
   902 	{
   903 	switch(aOpcode)
   904 		{
   905 		case EWsSpriteOpFree:
   906 			// CWsCustomTextCursor objects are owned by the text cursor list.
   907 			// They are not deleted when the client closes it's R class.
   908 			RemoveFromIndex();
   909 			break;
   910 		default:
   911 			CWsSpriteBase::CommandL(aOpcode, aCmdData);
   912 			break;
   913 		}
   914 	}
   915 
   916 //
   917 // CWsDeltaTimer, nicked from CDeltaTimer and tweaked so it doesn't re-activate			//
   918 // the timers until RunL has finished running all ready timers.							//
   919 //																						//
   920 // This is to stop a problem in Wserv where sprites could hog 100% CPU if the time		//
   921 // it took to process them was longer than the time of the timer queued when the first	//
   922 // sprite was updated																	//
   923 //
   924 
   925 CWsDeltaTimer* CWsDeltaTimer::NewL(TInt aPriority)
   926 	{
   927 	CWsDeltaTimer* wsdt=new(ELeave) CWsDeltaTimer(aPriority);
   928 	CleanupStack::PushL(wsdt);
   929 	User::LeaveIfError(wsdt->iTimer.CreateLocal());
   930 	CActiveScheduler::Add(wsdt);
   931 	CleanupStack::Pop(wsdt);
   932 	return(wsdt);
   933 	}
   934 
   935 CWsDeltaTimer::CWsDeltaTimer(TInt aPriority) : CActive(aPriority),iQueue(_FOFF(TWsDeltaTimerEntry,iLink))
   936 	{
   937 	}
   938 
   939 CWsDeltaTimer::~CWsDeltaTimer()
   940 	{
   941 	Cancel();
   942 	while(iQueue.RemoveFirst()!=NULL)
   943 		{}
   944 	}
   945 
   946 void CWsDeltaTimer::Queue(TTimeIntervalMicroSeconds32 aTimeInMicroSeconds,TWsDeltaTimerEntry& anEntry)
   947 	{
   948 	TInt intervals=aTimeInMicroSeconds.Int()/CWsDeltaTimerGranularity;
   949 	if (intervals==0)
   950 		intervals=1;
   951 	iQueue.Add(anEntry,intervals);
   952 	}
   953 
   954 void CWsDeltaTimer::Activate()
   955 	{
   956 	// Queue a request on the timer.
   957 	// The timer runs every tenth of a second and decremented the delta of the head of the queue.
   958 	if (IsActive())
   959 		return;
   960 	if (!iQueue.IsEmpty())
   961 		{
   962 		SetActive();
   963 		iTimer.After(iStatus,CWsDeltaTimerGranularity-1); // -1 to compensate for +1 in kernel!
   964 		}
   965 	}
   966 
   967 void CWsDeltaTimer::RunL()
   968 	{
   969 	// Call all zero delta callbacks
   970 	iQueue.CountDown();
   971 	TWsDeltaTimerEntry* ent=iQueue.RemoveFirst();
   972 	while (ent)
   973 		{       
   974 		ent->iCallBack.CallBack();
   975 		ent=iQueue.RemoveFirst();
   976 		}
   977 	Activate();
   978 	}
   979 
   980 void CWsDeltaTimer::DoCancel()
   981 	{
   982 	iTimer.Cancel();
   983 	}
   984 
   985 void CWsDeltaTimer::Remove(TWsDeltaTimerEntry& anEntry)
   986 	{
   987 	if (anEntry.IsPending())
   988 		{
   989 		iQueue.Remove(anEntry);
   990 		Activate();
   991 		}
   992 	}
   993 
   994 
   995 //
   996 // CWsSpriteManager -handles floating and flashing sprites including flashing custom text cursors. i.e. cursors
   997 //					that have an associated sprite.
   998 
   999 
  1000 CWsSpriteManager::CWsSpriteManager()
  1001 	{
  1002 	}
  1003 	
  1004 CWsSpriteManager* CWsSpriteManager::NewL()
  1005 	{
  1006 	CWsSpriteManager* self = new (ELeave) CWsSpriteManager();
  1007 	CleanupStack::PushL(self);
  1008 	self->ConstructL();
  1009 	CleanupStack::Pop(self);
  1010 	return self;	
  1011 	}
  1012 
  1013 void CWsSpriteManager::ConstructL()
  1014 	{
  1015 	}
  1016 
  1017 CWsSpriteManager::~CWsSpriteManager()
  1018     {
  1019     iFloatingSprites.ResetAndDestroy();
  1020     }
  1021 void CWsSpriteManager::AddFloatingSprite(CWsSpriteBase* aSprite)
  1022 	{
  1023 	iFloatingSprites.Append(aSprite);
  1024 	}
  1025 	
  1026 void CWsSpriteManager::RemoveFloatingSprite(CWsSpriteBase* aSprite)
  1027 	{
  1028 #ifdef _DEBUG 
  1029 	TInt removed = 0; 
  1030 #endif
  1031 	for (TInt i=0 ; i<iFloatingSprites.Count() ; i++)
  1032 		{
  1033 		if(iFloatingSprites[i]==aSprite)
  1034 			{
  1035 			//Just remove the sprite don't delete it. the manager doesn't have ownership
  1036 			iFloatingSprites.Remove(i);
  1037 #ifdef _DEBUG
  1038 			removed++;
  1039 #else
  1040 			break;
  1041 #endif
  1042 			}
  1043 		}
  1044 	WS_ASSERT_DEBUG(removed==1,EWsPanicFloatingSprite);
  1045 	}
  1046 
  1047 void CWsSpriteManager::DrawFloatingSprites(MWsGraphicsContext* aGc,const TRegion& aRegion)
  1048 	{
  1049 	if (iFloatingSprites.Count() == 0)
  1050 		return; //avoid sending events unless necessary
  1051 
  1052 	for (TInt i = iFloatingSprites.Count() - 1; i >= 0 ; i--)
  1053 		{
  1054 		STACK_REGION redrawRegion;
  1055 		CWsSpriteBase* sprite = iFloatingSprites[i];
  1056 		sprite->CalcRedrawRegion(aRegion, redrawRegion);
  1057 		if(redrawRegion.CheckError() || !redrawRegion.IsEmpty())
  1058 			{
  1059 			if (sprite->IsFlashingEnabled() || sprite->IsDirty() || sprite->HasAnimation())
  1060 				{
  1061 				AnnotateSpriteRedrawStart(*sprite, redrawRegion);
  1062 				
  1063 				if(sprite->HasAnimation())
  1064 					{
  1065 					CWsAnim* anim = static_cast<CWsSprite*>(sprite)->iAnim;
  1066 					ASSERT(anim);
  1067 					
  1068 					TRAPD(err, anim->AnimateSpriteAnimL(sprite->Screen()->Now()));
  1069 					if(err!=KErrNone)
  1070 						{
  1071 						AnnotateSpriteRedrawEnd(*sprite);						
  1072 						anim->Panic(EWservPanicAnimLeave);
  1073 						continue;
  1074 						}
  1075 					}
  1076 				
  1077 				aGc->Reset();
  1078 				sprite->Redraw(aGc, redrawRegion);
  1079 				
  1080 				AnnotateSpriteRedrawEnd(*sprite);
  1081 				}
  1082 			}
  1083 		redrawRegion.Close();
  1084 		}
  1085 	}
  1086 	
  1087 void CWsSpriteManager::Schedule(CWsSpriteBase* aSprite, TRect* aRect)
  1088 	{
  1089 	if (aRect != NULL && aRect->IsEmpty())
  1090 		return;
  1091 
  1092 	TRect rect = aSprite->Rect();
  1093 	if (aRect)
  1094 		rect.Intersection(*aRect);
  1095 		
  1096 	const TAnimType type = aSprite->Win() ? EWindowSprite : EFloatingSprite;
  1097 	
  1098 	if(aSprite->IsFlashingEnabled())
  1099 		{
  1100 		aSprite->Screen()->ScheduleAnimation(type, rect,NextSpriteFlashStateChange(aSprite),0,0, aSprite->Win());
  1101 		}
  1102 	else
  1103 		{
  1104 		//Scheduling an animation "now" means it will take place at next animation which might 
  1105 		//be the full animation grace period into the future (see KAnimationGrace in server.cpp)
  1106 		aSprite->Screen()->ScheduleAnimation(type, rect,0,0,0, aSprite->Win());
  1107 		}
  1108 	}	
  1109 
  1110 // Sprite flashing is clamped to half second intervals in relation to the global time.
  1111 // For the first half of each second all sprites have the EFlashOn state (visible)
  1112 // For the second half of each second all sprites have the EFlashOff state (not visible) 
  1113 TTimeIntervalMicroSeconds CWsSpriteManager::NextSpriteFlashStateChange(const CWsSpriteBase* aSprite) const
  1114 	{
  1115 	const TTimeIntervalMicroSeconds remainder = aSprite->Screen()->Now().DateTime().MicroSecond();
  1116 	return CalculateTimeToNextFlash(remainder);
  1117 	}
  1118 
  1119 TTimeIntervalMicroSeconds CWsSpriteManager::NextCursorFlashStateChange() const
  1120 	{
  1121 	const TTimeIntervalMicroSeconds remainder = CWsTop::CurrentFocusScreen()->Now().DateTime().MicroSecond();
  1122 	return CalculateTimeToNextFlash(remainder);
  1123 	}
  1124 
  1125 TTimeIntervalMicroSeconds CWsSpriteManager::CalculateTimeToNextFlash(TTimeIntervalMicroSeconds aTime) const
  1126 	{
  1127 	TInt64 nextStateChange;
  1128 	if(aTime<KFlashHalfSecond)
  1129 		nextStateChange=KFlashHalfSecond-aTime.Int64();
  1130 	else
  1131 		nextStateChange=KFlashHalfSecond - (aTime.Int64() - KFlashHalfSecond);
  1132 	ASSERT(nextStateChange > 0);
  1133 	return TTimeIntervalMicroSeconds(nextStateChange);
  1134 	}	
  1135 	
  1136 TFlashState CWsSpriteManager::CurrentSpriteFlashState(const CWsSpriteBase* aSprite) const
  1137 	{
  1138 	return (aSprite->Screen()->Now().DateTime().MicroSecond()<KFlashHalfSecond)?EFlashOn:EFlashOff;
  1139 	}
  1140 
  1141 void CWsSpriteManager::CalcFloatingSpriteRgn( TRegion& aResultRgn, const TRect& aDefaultRect )
  1142 	{
  1143 	aResultRgn.Clear();
  1144 	for (TInt i=0 ; i<iFloatingSprites.Count() && !aResultRgn.CheckError(); i++)
  1145 		{
  1146 		CWsSpriteBase* sprite = iFloatingSprites[i]; 
  1147 		if ( sprite->CanBeSeen() && ( sprite->IsActive() || sprite->IsActivated() ) )
  1148 			{
  1149 			aResultRgn.AddRect( sprite->Rect() );
  1150 			}
  1151 		}
  1152 	aResultRgn.Tidy();
  1153 	if ( aResultRgn.CheckError() && iFloatingSprites.Count() > 0 )
  1154 		{
  1155 		aResultRgn.Clear();
  1156 		aResultRgn.AddRect( aDefaultRect );
  1157 		}
  1158 	}
  1159 
  1160 void CWsSpriteManager::SendState(MWsWindowTreeObserver& aWindowTreeObserver) const
  1161 	{
  1162 	for(TInt i=iFloatingSprites.Count()-1; i>=0; i--)
  1163 		{
  1164 		CWsSpriteBase* sprite = iFloatingSprites[i];
  1165 		sprite->SendState(aWindowTreeObserver);
  1166 		}
  1167 	}