os/graphics/windowing/windowserver/test/tlib/TLDIALOG.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/windowing/windowserver/test/tlib/TLDIALOG.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,463 @@
     1.4 +// Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Base classes used for building window server test code
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <e32std.h>
    1.22 +#include <w32std.h>
    1.23 +#include <e32svr.h>
    1.24 +#include <e32def_private.h>
    1.25 +#include "TLIB.H"
    1.26 +
    1.27 +const TInt ButtonGap=20;
    1.28 +const TInt ButtonBorderGap=10;
    1.29 +const TInt ButtonBorderMargin=3;
    1.30 +const TInt TextMargin=5;
    1.31 +const TInt KAutoDelaySeconds=60; 
    1.32 +
    1.33 +NONSHARABLE_CLASS(CTAutoKey): public CActive
    1.34 +	{
    1.35 +public:
    1.36 +	static CTAutoKey* NewL(RWsSession& aWs);
    1.37 +	~CTAutoKey();
    1.38 +	void Activate(TInt aDelay);
    1.39 +	
    1.40 +	void RunL();
    1.41 +	void DoCancel();
    1.42 +	
    1.43 +private:
    1.44 +	CTAutoKey(RWsSession& aWs);
    1.45 +	void ConstructL();	
    1.46 +	
    1.47 +	RTimer iTimer;
    1.48 +	TInt iDelay;
    1.49 +	RWsSession& iWs;
    1.50 +	};
    1.51 +
    1.52 +EXPORT_C CTDialog::CTDialog() : CTTitledWindow(), iNumButtons(1)
    1.53 +	{
    1.54 +	iButton[0].Copy(_L("Continue"));	// Default button
    1.55 +	}
    1.56 +
    1.57 +EXPORT_C CTDialog::~CTDialog()
    1.58 +	{
    1.59 +	if (iIsActive)
    1.60 +		CActiveScheduler::Stop();
    1.61 +	delete iAutoKey;
    1.62 +	}
    1.63 +
    1.64 +EXPORT_C void CTDialog::ConstructLD(CTWinBase &aParent,CWindowGc &aGc)
    1.65 +	{
    1.66 +	iOldFocus=aParent.Group()->CurWin();
    1.67 +	TRAPD(err,CTTitledWindow::ConstructL(aParent));
    1.68 +	if (err!=KErrNone)
    1.69 +		{
    1.70 +		delete this;
    1.71 +		User::Leave(err);
    1.72 +		}
    1.73 +	iActivated=ETrue;
    1.74 +	iWin.SetBackgroundColor(TRgb::Gray256(238));	// Light gray
    1.75 +	AssignGC(aGc);
    1.76 +	iWin.SetPointerCapture(ETrue);
    1.77 +	Group()->SetCurrentWindow(this, ETrue);
    1.78 +	iAutoKey=CTAutoKey::NewL(Client()->iWs);
    1.79 +	}
    1.80 +
    1.81 +EXPORT_C void CTDialog::RelinquishFocus()
    1.82 +	{
    1.83 +	Group()->SetCurrentWindow(iOldFocus);
    1.84 +	}
    1.85 +
    1.86 +EXPORT_C TInt CTDialog::Display()
    1.87 +	{
    1.88 +	TInt result;
    1.89 +
    1.90 +	SetWindowSize();
    1.91 +	if (iWinActive)
    1.92 +		BaseWin()->SetVisible(ETrue);
    1.93 +	else
    1.94 +		{
    1.95 +		Activate();
    1.96 +		iWinActive=ETrue;
    1.97 +		}
    1.98 +	if (iTakeFocus)
    1.99 +		Group()->SetCurrentWindow(this);
   1.100 +	Group()->GroupWin()->SetOrdinalPosition(0);
   1.101 +	iResultPtr=&result;
   1.102 +	CTClient *client=Client();
   1.103 +	if (client->QueueRead())
   1.104 +		{
   1.105 +		client->iWs.Flush();
   1.106 +		client=NULL;
   1.107 +		}
   1.108 +	iIsActive=ETrue;
   1.109 +	iAutoKey->Activate(KAutoDelaySeconds);
   1.110 +	CActiveScheduler::Start();
   1.111 +	if (client)
   1.112 +		client->CancelRead();
   1.113 +	return(result);
   1.114 +	}
   1.115 +
   1.116 +EXPORT_C void CTDialog::SetFlags(TUint aFlags)
   1.117 +	{
   1.118 +	iFlags=aFlags;
   1.119 +	}
   1.120 +
   1.121 +void CTDialog::SetMaxWid(TInt &aMax, TInt aWid)
   1.122 +	{
   1.123 +	if (aWid>aMax)
   1.124 +		aMax=aWid;
   1.125 +	}
   1.126 +
   1.127 +EXPORT_C void CTDialog::SetWindowSize()
   1.128 +	{
   1.129 +	if (iActivated)
   1.130 +		{
   1.131 +		TInt max=0;
   1.132 +		SetMaxWid(max,iFont->TextWidthInPixels(iTitle));
   1.133 +		SetMaxWid(max,iFont->TextWidthInPixels(iLine1));
   1.134 +		SetMaxWid(max,iFont->TextWidthInPixels(iLine2));
   1.135 +		max+=TextMargin*2;
   1.136 +		iButWid=0;
   1.137 +		if (iNumButtons>0)
   1.138 +			{
   1.139 +			for(TInt index=0;index<iNumButtons;index++)
   1.140 +				SetMaxWid(iButWid,iFont->TextWidthInPixels(iButton[index]));
   1.141 +			iButWid+=ButtonBorderMargin*2;
   1.142 +			SetMaxWid(max,iButWid*iNumButtons+ButtonGap*(iNumButtons-1)+ButtonBorderGap*2);
   1.143 +			}
   1.144 +		TSize parSize=Parent()->Size();
   1.145 +		TSize size(max,iFont->HeightInPixels()*8);
   1.146 +		TPoint pos((parSize.iWidth-size.iWidth)/2,(parSize.iHeight-size.iHeight)/2);
   1.147 +		if (iFlags&EDialogDisplayAtBottom)
   1.148 +			pos.iY*=2;
   1.149 +		if (iFlags&EDialogDisplayAtLeft)
   1.150 +			pos.iX=0;
   1.151 +		SetExt(pos,size);
   1.152 +		Invalidate();
   1.153 +		}
   1.154 +	}
   1.155 +
   1.156 +EXPORT_C void CTDialog::SetLine1(const TDesC &aLine1)
   1.157 +	{
   1.158 +	iLine1.Copy(aLine1);
   1.159 +	}
   1.160 +
   1.161 +EXPORT_C void CTDialog::SetLine2(const TDesC &aLine2)
   1.162 +	{
   1.163 +	iLine2.Copy(aLine2);
   1.164 +	}
   1.165 +
   1.166 +EXPORT_C void CTDialog::SetNumButtons(TInt aNum)
   1.167 +	{
   1.168 +	if ((TUint)aNum>3)
   1.169 +		TbPanic(EDialogButtonCount);
   1.170 +	iNumButtons=aNum;
   1.171 +	}
   1.172 +
   1.173 +EXPORT_C void CTDialog::SetButtonText(TInt aNum,const TDesC &aButton)
   1.174 +	{
   1.175 +	if ((TUint)aNum>(TUint)iNumButtons)
   1.176 +		TbPanic(EDialogButtonIndex);
   1.177 +	iButton[aNum].Copy(aButton);
   1.178 +	}
   1.179 +
   1.180 +TRect CTDialog::ButtonRect(TInt aIndex) const
   1.181 +	{
   1.182 +	TInt chunk=(iSize.iWidth-ButtonBorderMargin*2)/iNumButtons;
   1.183 +	TInt midPos=ButtonBorderMargin+chunk*aIndex+chunk/2;
   1.184 +	return(TRect(midPos-iButWid/2,iFont->HeightInPixels()*6,midPos+iButWid/2,iFont->HeightInPixels()*7+ButtonBorderMargin*2));
   1.185 +	}
   1.186 +
   1.187 +EXPORT_C void CTDialog::Draw()
   1.188 +	{
   1.189 +	CTTitledWindow::Draw();
   1.190 +	iGc->SetPenColor(TRgb::Gray16(0));
   1.191 +	iGc->DrawText(iLine1, TPoint((iSize.iWidth-iFont->TextWidthInPixels(iLine1))/2,iFont->HeightInPixels()*3));
   1.192 +	iGc->DrawText(iLine2, TPoint((iSize.iWidth-iFont->TextWidthInPixels(iLine2))/2,iFont->HeightInPixels()*4+2));
   1.193 +	if (iNumButtons!=0)
   1.194 +		{
   1.195 +		for(TInt index=0;index<iNumButtons;index++)
   1.196 +			{
   1.197 +			TRect rect=ButtonRect(index);
   1.198 +			iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
   1.199 +			iGc->SetBrushColor(TRgb::Gray256(255));
   1.200 +			iGc->DrawRect(rect);
   1.201 +			iGc->DrawRect(rect);
   1.202 +			iGc->SetBrushStyle(CGraphicsContext::ENullBrush);
   1.203 +			TInt midPos=(rect.iTl.iX+rect.iBr.iX)/2;
   1.204 +			iGc->DrawText(iButton[index], TPoint((midPos-iFont->TextWidthInPixels(iButton[index])/2),
   1.205 +									iFont->HeightInPixels()*6+iFont->AscentInPixels()+ButtonBorderMargin));
   1.206 +			}
   1.207 +		}
   1.208 +	}
   1.209 +
   1.210 +EXPORT_C void CTDialog::WinKeyL(const TKeyEvent &aKey,const TTime&)
   1.211 +	{
   1.212 +	switch(aKey.iCode)
   1.213 +		{
   1.214 +		case EKeyEscape:
   1.215 +		case '0':
   1.216 +			ButtonPressL(0);
   1.217 +			break;
   1.218 +		case EKeyEnter:
   1.219 +		case '1':
   1.220 +			ButtonPressL(iNumButtons>1 ? 1 : 0);	// Same as ESC on a single button dialog
   1.221 +			break;
   1.222 +		case ' ':
   1.223 +		case '2':
   1.224 +			ButtonPressL(2);
   1.225 +			break;
   1.226 +		}
   1.227 +	}
   1.228 +
   1.229 +EXPORT_C void CTDialog::PointerL(const TPointerEvent &aPointer,const TTime &aTime)
   1.230 +	{
   1.231 +	if (aPointer.iType==TPointerEvent::EButton1Up)
   1.232 +		{
   1.233 +		if (iButtonClickOn>0)
   1.234 +			{
   1.235 +			ButtonPressL(iButtonClickOn-1);
   1.236 +			return;
   1.237 +			}
   1.238 +		}
   1.239 +	else if (aPointer.iType==TPointerEvent::EButton1Down)
   1.240 +		{
   1.241 +		for(TInt index=0;index<iNumButtons;index++)
   1.242 +			if (ButtonRect(index).Contains(aPointer.iPosition))
   1.243 +				{
   1.244 +				if (iFlags&EDialogWaitForButtonUp)
   1.245 +					iButtonClickOn=index+1;
   1.246 +				else
   1.247 +					{
   1.248 +					ButtonPressL(index);
   1.249 +					return;
   1.250 +					}
   1.251 +				}
   1.252 +		}
   1.253 +	CTTitledWindow::PointerL(aPointer, aTime);
   1.254 +	}
   1.255 +
   1.256 +EXPORT_C void CTDialog::ButtonPressL(TInt aButton)
   1.257 +	{
   1.258 +	if (aButton<iNumButtons)
   1.259 +		{
   1.260 +		SetResult(aButton);
   1.261 +		CTTitledWindow::Delete(this);
   1.262 +		}
   1.263 +	}
   1.264 +
   1.265 +void CTDialog::SetResult(TInt aButton)
   1.266 +	{
   1.267 +	if (iResultPtr)
   1.268 +		*iResultPtr=aButton;
   1.269 +	}
   1.270 +
   1.271 +// Simple display dialog //
   1.272 +
   1.273 +class CDisplayDialog : public CTDialog
   1.274 +	{		  
   1.275 +public:
   1.276 +	CDisplayDialog(CTWindowGroup *aGroupWin,CWindowGc *aGc);
   1.277 +	void ConstructLD();
   1.278 +private:
   1.279 +	CTWindowGroup *iGroupWin;
   1.280 +	CWindowGc *iGc;
   1.281 +	};
   1.282 +
   1.283 +CDisplayDialog::CDisplayDialog(CTWindowGroup *aGroupWin,CWindowGc *aGc) : CTDialog(),
   1.284 +	iGroupWin(aGroupWin),
   1.285 +	iGc(aGc)
   1.286 +	{
   1.287 +	}
   1.288 +
   1.289 +void CDisplayDialog::ConstructLD()
   1.290 +	{
   1.291 +	CTDialog::ConstructLD(*iGroupWin, *iGc);
   1.292 +	}
   1.293 +
   1.294 +EXPORT_C void DisplayDialog(CTClient *aClient, const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2)
   1.295 +	{
   1.296 +	CDisplayDialog *dialog=NULL;
   1.297 +	dialog=new(ELeave) CDisplayDialog(aClient->iGroup, aClient->iGc);
   1.298 +	TRAPD(err,dialog->ConstructLD());
   1.299 +	if (err==KErrNone)
   1.300 +		{
   1.301 +		dialog->SetTitle(aTitle);
   1.302 +		dialog->SetLine1(aLine1);
   1.303 +		dialog->SetLine2(aLine2);
   1.304 +		dialog->SetNumButtons(1);
   1.305 +		dialog->SetButtonText(0,_L("Okay"));
   1.306 +		if (dialog->Display()!=0)		// delete dialog
   1.307 +			TbPanic(EDialogDisplay);
   1.308 +		}
   1.309 +	}
   1.310 +
   1.311 +void doDisplayDialog(TInt aScreenNumber,const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, CTClient *&aClient, const RWindowGroup *aGroup)
   1.312 +	{
   1.313 +	aClient=new(ELeave) CTClient();
   1.314 +	aClient->SetScreenNumber(aScreenNumber);
   1.315 +	aClient->ConstructL();
   1.316 +//
   1.317 +	aClient->iGroup=new(ELeave) CTWindowGroup(aClient);
   1.318 +	aClient->iGroup->ConstructL();
   1.319 +	aClient->iGroup->GroupWin()->SetOrdinalPosition(0,10);
   1.320 +	if (aGroup)
   1.321 +		aClient->iGroup->GroupWin()->SetOwningWindowGroup(aGroup->Identifier());
   1.322 +//
   1.323 +	TRAP_IGNORE(DisplayDialog(aClient, aTitle, aLine1, aLine2));
   1.324 +	}
   1.325 +
   1.326 +void doDisplayDialog(const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, CTClient *&aClient, const RWindowGroup *aGroup)
   1.327 +	{
   1.328 +	doDisplayDialog(KDefaultScreen,aTitle,aLine1,aLine2,aClient,aGroup);
   1.329 +	}
   1.330 +
   1.331 +EXPORT_C void DisplayDialog(TInt aScreenNumber,const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, const RWindowGroup *aGroup)
   1.332 +	{
   1.333 +	CTClient *client=NULL;
   1.334 +	TRAP_IGNORE(doDisplayDialog(aScreenNumber,aTitle,aLine1,aLine2,client,aGroup));
   1.335 +	delete client;
   1.336 +	}
   1.337 +
   1.338 +EXPORT_C void DisplayDialog(const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, const RWindowGroup *aGroup)
   1.339 +	{
   1.340 +	DisplayDialog(KDefaultScreen,aTitle,aLine1,aLine2,aGroup);
   1.341 +	}
   1.342 +
   1.343 +
   1.344 +
   1.345 +//CInfoDialog
   1.346 +
   1.347 +EXPORT_C CInfoDialog::CInfoDialog(CTWindowGroup *aGroupWin,CWindowGc *aGc) :CTDialog(), iGroupWin(aGroupWin), iGc(aGc)
   1.348 +	{}
   1.349 +
   1.350 +EXPORT_C void CInfoDialog::ButtonPressL(TInt aButton)
   1.351 +	{
   1.352 +	if (aButton==0)
   1.353 +		{
   1.354 +		SetResult(0);
   1.355 +		BaseWin()->SetVisible(EFalse);
   1.356 +		CActiveScheduler::Stop();
   1.357 +		}
   1.358 +	}
   1.359 +
   1.360 +EXPORT_C void CInfoDialog::ConstructLD()
   1.361 +	{
   1.362 +	_LIT(OK,"Okay");
   1.363 +	CTDialog::ConstructLD(*iGroupWin, *iGc);
   1.364 +	SetNumButtons(1);
   1.365 +	SetButtonText(0,OK);
   1.366 +	SetTakeFocus();
   1.367 +	}
   1.368 +
   1.369 +EXPORT_C void CInfoDialog::TimerResults()
   1.370 +	{
   1.371 +	TProfile profile[eTimes];
   1.372 +	// TProfile only has default constructor -
   1.373 +	// constructor of TProfile does not initialize its members
   1.374 +	for (TInt jj=0; jj<eTimes; jj++)
   1.375 +		{
   1.376 +		profile[jj].iTime=0;
   1.377 +		profile[jj].iCount=0;
   1.378 +		}
   1.379 +	__PROFILE_DISPLAY(eTimes)
   1.380 +	TBuf<64> times;
   1.381 +	TBuf<32> counts;
   1.382 +	TInt ii=1;
   1.383 +	FOREVER
   1.384 +		{
   1.385 +		AppendProfileTime(times,profile[ii].iTime);
   1.386 +		AppendProfileCount(counts,profile[ii].iCount);
   1.387 +		if (++ii==eTimes)
   1.388 +			break;
   1.389 +		AddComma(times);
   1.390 +		AddComma(counts);
   1.391 +		}
   1.392 +	SetLine1(times);
   1.393 +	SetLine2(counts);
   1.394 +	times.Zero();
   1.395 +	AppendProfileTime(times,profile[0].iTime);
   1.396 +	SetTitle(times);
   1.397 +	}
   1.398 +
   1.399 +void CInfoDialog::AppendProfileTime(TDes &aDes, TInt aNum)
   1.400 +	{
   1.401 +	_LIT(ThreeDP,"%d.%03d");
   1.402 +	aDes.AppendFormat(ThreeDP,aNum/eSeconds,(aNum%eSeconds)/1000);
   1.403 +	}
   1.404 +
   1.405 +void CInfoDialog::AppendProfileCount(TDes &aDes, TInt aNum)
   1.406 +	{
   1.407 +	_LIT(Int,"%d");
   1.408 +	aDes.AppendFormat(Int,aNum);
   1.409 +	}
   1.410 +
   1.411 +void CInfoDialog::AddComma(TDes &aDes)
   1.412 +	{
   1.413 +	_LIT(Comma,", ");
   1.414 +	aDes.Append(Comma);
   1.415 +	}
   1.416 +
   1.417 +CTAutoKey::CTAutoKey(RWsSession& aWs): CActive(0), iWs(aWs)
   1.418 +	{
   1.419 +	CActiveScheduler::Add(this);
   1.420 +	}
   1.421 +	
   1.422 +CTAutoKey::~CTAutoKey()
   1.423 +	{
   1.424 +	Cancel();
   1.425 +	iTimer.Close();
   1.426 +	}
   1.427 +	
   1.428 +void CTAutoKey::ConstructL()
   1.429 +	{
   1.430 +	User::LeaveIfError(iTimer.CreateLocal());
   1.431 +	}
   1.432 +	
   1.433 +CTAutoKey* CTAutoKey::NewL(RWsSession& aWs)
   1.434 +	{
   1.435 +	CTAutoKey* self=new(ELeave) CTAutoKey(aWs);
   1.436 +	CleanupStack::PushL(self);
   1.437 +	self->ConstructL();
   1.438 +	CleanupStack::Pop();
   1.439 +	return self;
   1.440 +	}
   1.441 +	
   1.442 +void CTAutoKey::Activate(TInt aDelay)
   1.443 +	{
   1.444 +	if (IsActive())
   1.445 +		Cancel();
   1.446 +	
   1.447 +	iDelay=aDelay*1000000;
   1.448 +	iTimer.After(iStatus,iDelay);
   1.449 +	SetActive();
   1.450 +	}
   1.451 +void CTAutoKey::RunL()
   1.452 +	{
   1.453 +	// simulate key event, only needed to run once
   1.454 +	TKeyEvent keyEvent;
   1.455 +	keyEvent.iCode=EKeyEnter;
   1.456 +	keyEvent.iScanCode=EKeyEnter;
   1.457 +	keyEvent.iModifiers=0;
   1.458 +	keyEvent.iRepeats=0;
   1.459 +	iWs.SimulateKeyEvent(keyEvent);
   1.460 +	iWs.Flush();
   1.461 +	}
   1.462 +	
   1.463 +void CTAutoKey::DoCancel()
   1.464 +	{
   1.465 +	iTimer.Cancel();
   1.466 +	}