windowing/windowserver/tlib/TLDIALOG.CPP
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1994-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 // Base classes used for building window server test code
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32std.h>
       
    19 #include <w32std.h>
       
    20 #include <e32svr.h>
       
    21 #include <e32def_private.h>
       
    22 #include "TLIB.H"
       
    23 
       
    24 const TInt ButtonGap=20;
       
    25 const TInt ButtonBorderGap=10;
       
    26 const TInt ButtonBorderMargin=3;
       
    27 const TInt TextMargin=5;
       
    28 const TInt KAutoDelaySeconds=60; 
       
    29 
       
    30 NONSHARABLE_CLASS(CTAutoKey): public CActive
       
    31 	{
       
    32 public:
       
    33 	static CTAutoKey* NewL(RWsSession& aWs);
       
    34 	~CTAutoKey();
       
    35 	void Activate(TInt aDelay);
       
    36 	
       
    37 	void RunL();
       
    38 	void DoCancel();
       
    39 	
       
    40 private:
       
    41 	CTAutoKey(RWsSession& aWs);
       
    42 	void ConstructL();	
       
    43 	
       
    44 	RTimer iTimer;
       
    45 	TInt iDelay;
       
    46 	RWsSession& iWs;
       
    47 	};
       
    48 
       
    49 EXPORT_C CTDialog::CTDialog() : CTTitledWindow(), iNumButtons(1)
       
    50 	{
       
    51 	iButton[0].Copy(_L("Continue"));	// Default button
       
    52 	}
       
    53 
       
    54 EXPORT_C CTDialog::~CTDialog()
       
    55 	{
       
    56 	if (iIsActive)
       
    57 		CActiveScheduler::Stop();
       
    58 	delete iAutoKey;
       
    59 	}
       
    60 
       
    61 EXPORT_C void CTDialog::ConstructLD(CTWinBase &aParent,CWindowGc &aGc)
       
    62 	{
       
    63 	iOldFocus=aParent.Group()->CurWin();
       
    64 	TRAPD(err,CTTitledWindow::ConstructL(aParent));
       
    65 	if (err!=KErrNone)
       
    66 		{
       
    67 		delete this;
       
    68 		User::Leave(err);
       
    69 		}
       
    70 	iActivated=ETrue;
       
    71 	iWin.SetBackgroundColor(TRgb::Gray256(238));	// Light gray
       
    72 	AssignGC(aGc);
       
    73 	iWin.SetPointerCapture(ETrue);
       
    74 	Group()->SetCurrentWindow(this, ETrue);
       
    75 	iAutoKey=CTAutoKey::NewL(Client()->iWs);
       
    76 	}
       
    77 
       
    78 EXPORT_C void CTDialog::RelinquishFocus()
       
    79 	{
       
    80 	Group()->SetCurrentWindow(iOldFocus);
       
    81 	}
       
    82 
       
    83 EXPORT_C TInt CTDialog::Display()
       
    84 	{
       
    85 	TInt result;
       
    86 
       
    87 	SetWindowSize();
       
    88 	if (iWinActive)
       
    89 		BaseWin()->SetVisible(ETrue);
       
    90 	else
       
    91 		{
       
    92 		Activate();
       
    93 		iWinActive=ETrue;
       
    94 		}
       
    95 	if (iTakeFocus)
       
    96 		Group()->SetCurrentWindow(this);
       
    97 	Group()->GroupWin()->SetOrdinalPosition(0);
       
    98 	iResultPtr=&result;
       
    99 	CTClient *client=Client();
       
   100 	if (client->QueueRead())
       
   101 		{
       
   102 		client->iWs.Flush();
       
   103 		client=NULL;
       
   104 		}
       
   105 	iIsActive=ETrue;
       
   106 	iAutoKey->Activate(KAutoDelaySeconds);
       
   107 	CActiveScheduler::Start();
       
   108 	if (client)
       
   109 		client->CancelRead();
       
   110 	return(result);
       
   111 	}
       
   112 
       
   113 EXPORT_C void CTDialog::SetFlags(TUint aFlags)
       
   114 	{
       
   115 	iFlags=aFlags;
       
   116 	}
       
   117 
       
   118 void CTDialog::SetMaxWid(TInt &aMax, TInt aWid)
       
   119 	{
       
   120 	if (aWid>aMax)
       
   121 		aMax=aWid;
       
   122 	}
       
   123 
       
   124 EXPORT_C void CTDialog::SetWindowSize()
       
   125 	{
       
   126 	if (iActivated)
       
   127 		{
       
   128 		TInt max=0;
       
   129 		SetMaxWid(max,iFont->TextWidthInPixels(iTitle));
       
   130 		SetMaxWid(max,iFont->TextWidthInPixels(iLine1));
       
   131 		SetMaxWid(max,iFont->TextWidthInPixels(iLine2));
       
   132 		max+=TextMargin*2;
       
   133 		iButWid=0;
       
   134 		if (iNumButtons>0)
       
   135 			{
       
   136 			for(TInt index=0;index<iNumButtons;index++)
       
   137 				SetMaxWid(iButWid,iFont->TextWidthInPixels(iButton[index]));
       
   138 			iButWid+=ButtonBorderMargin*2;
       
   139 			SetMaxWid(max,iButWid*iNumButtons+ButtonGap*(iNumButtons-1)+ButtonBorderGap*2);
       
   140 			}
       
   141 		TSize parSize=Parent()->Size();
       
   142 		TSize size(max,iFont->HeightInPixels()*8);
       
   143 		TPoint pos((parSize.iWidth-size.iWidth)/2,(parSize.iHeight-size.iHeight)/2);
       
   144 		if (iFlags&EDialogDisplayAtBottom)
       
   145 			pos.iY*=2;
       
   146 		if (iFlags&EDialogDisplayAtLeft)
       
   147 			pos.iX=0;
       
   148 		SetExt(pos,size);
       
   149 		Invalidate();
       
   150 		}
       
   151 	}
       
   152 
       
   153 EXPORT_C void CTDialog::SetLine1(const TDesC &aLine1)
       
   154 	{
       
   155 	iLine1.Copy(aLine1);
       
   156 	}
       
   157 
       
   158 EXPORT_C void CTDialog::SetLine2(const TDesC &aLine2)
       
   159 	{
       
   160 	iLine2.Copy(aLine2);
       
   161 	}
       
   162 
       
   163 EXPORT_C void CTDialog::SetNumButtons(TInt aNum)
       
   164 	{
       
   165 	if ((TUint)aNum>3)
       
   166 		TbPanic(EDialogButtonCount);
       
   167 	iNumButtons=aNum;
       
   168 	}
       
   169 
       
   170 EXPORT_C void CTDialog::SetButtonText(TInt aNum,const TDesC &aButton)
       
   171 	{
       
   172 	if ((TUint)aNum>(TUint)iNumButtons)
       
   173 		TbPanic(EDialogButtonIndex);
       
   174 	iButton[aNum].Copy(aButton);
       
   175 	}
       
   176 
       
   177 TRect CTDialog::ButtonRect(TInt aIndex) const
       
   178 	{
       
   179 	TInt chunk=(iSize.iWidth-ButtonBorderMargin*2)/iNumButtons;
       
   180 	TInt midPos=ButtonBorderMargin+chunk*aIndex+chunk/2;
       
   181 	return(TRect(midPos-iButWid/2,iFont->HeightInPixels()*6,midPos+iButWid/2,iFont->HeightInPixels()*7+ButtonBorderMargin*2));
       
   182 	}
       
   183 
       
   184 EXPORT_C void CTDialog::Draw()
       
   185 	{
       
   186 	CTTitledWindow::Draw();
       
   187 	iGc->SetPenColor(TRgb::Gray16(0));
       
   188 	iGc->DrawText(iLine1, TPoint((iSize.iWidth-iFont->TextWidthInPixels(iLine1))/2,iFont->HeightInPixels()*3));
       
   189 	iGc->DrawText(iLine2, TPoint((iSize.iWidth-iFont->TextWidthInPixels(iLine2))/2,iFont->HeightInPixels()*4+2));
       
   190 	if (iNumButtons!=0)
       
   191 		{
       
   192 		for(TInt index=0;index<iNumButtons;index++)
       
   193 			{
       
   194 			TRect rect=ButtonRect(index);
       
   195 			iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   196 			iGc->SetBrushColor(TRgb::Gray256(255));
       
   197 			iGc->DrawRect(rect);
       
   198 			iGc->DrawRect(rect);
       
   199 			iGc->SetBrushStyle(CGraphicsContext::ENullBrush);
       
   200 			TInt midPos=(rect.iTl.iX+rect.iBr.iX)/2;
       
   201 			iGc->DrawText(iButton[index], TPoint((midPos-iFont->TextWidthInPixels(iButton[index])/2),
       
   202 									iFont->HeightInPixels()*6+iFont->AscentInPixels()+ButtonBorderMargin));
       
   203 			}
       
   204 		}
       
   205 	}
       
   206 
       
   207 EXPORT_C void CTDialog::WinKeyL(const TKeyEvent &aKey,const TTime&)
       
   208 	{
       
   209 	switch(aKey.iCode)
       
   210 		{
       
   211 		case EKeyEscape:
       
   212 		case '0':
       
   213 			ButtonPressL(0);
       
   214 			break;
       
   215 		case EKeyEnter:
       
   216 		case '1':
       
   217 			ButtonPressL(iNumButtons>1 ? 1 : 0);	// Same as ESC on a single button dialog
       
   218 			break;
       
   219 		case ' ':
       
   220 		case '2':
       
   221 			ButtonPressL(2);
       
   222 			break;
       
   223 		}
       
   224 	}
       
   225 
       
   226 EXPORT_C void CTDialog::PointerL(const TPointerEvent &aPointer,const TTime &aTime)
       
   227 	{
       
   228 	if (aPointer.iType==TPointerEvent::EButton1Up)
       
   229 		{
       
   230 		if (iButtonClickOn>0)
       
   231 			{
       
   232 			ButtonPressL(iButtonClickOn-1);
       
   233 			return;
       
   234 			}
       
   235 		}
       
   236 	else if (aPointer.iType==TPointerEvent::EButton1Down)
       
   237 		{
       
   238 		for(TInt index=0;index<iNumButtons;index++)
       
   239 			if (ButtonRect(index).Contains(aPointer.iPosition))
       
   240 				{
       
   241 				if (iFlags&EDialogWaitForButtonUp)
       
   242 					iButtonClickOn=index+1;
       
   243 				else
       
   244 					{
       
   245 					ButtonPressL(index);
       
   246 					return;
       
   247 					}
       
   248 				}
       
   249 		}
       
   250 	CTTitledWindow::PointerL(aPointer, aTime);
       
   251 	}
       
   252 
       
   253 EXPORT_C void CTDialog::ButtonPressL(TInt aButton)
       
   254 	{
       
   255 	if (aButton<iNumButtons)
       
   256 		{
       
   257 		SetResult(aButton);
       
   258 		CTTitledWindow::Delete(this);
       
   259 		}
       
   260 	}
       
   261 
       
   262 void CTDialog::SetResult(TInt aButton)
       
   263 	{
       
   264 	if (iResultPtr)
       
   265 		*iResultPtr=aButton;
       
   266 	}
       
   267 
       
   268 // Simple display dialog //
       
   269 
       
   270 class CDisplayDialog : public CTDialog
       
   271 	{		  
       
   272 public:
       
   273 	CDisplayDialog(CTWindowGroup *aGroupWin,CWindowGc *aGc);
       
   274 	void ConstructLD();
       
   275 private:
       
   276 	CTWindowGroup *iGroupWin;
       
   277 	CWindowGc *iGc;
       
   278 	};
       
   279 
       
   280 CDisplayDialog::CDisplayDialog(CTWindowGroup *aGroupWin,CWindowGc *aGc) : CTDialog(),
       
   281 	iGroupWin(aGroupWin),
       
   282 	iGc(aGc)
       
   283 	{
       
   284 	}
       
   285 
       
   286 void CDisplayDialog::ConstructLD()
       
   287 	{
       
   288 	CTDialog::ConstructLD(*iGroupWin, *iGc);
       
   289 	}
       
   290 
       
   291 EXPORT_C void DisplayDialog(CTClient *aClient, const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2)
       
   292 	{
       
   293 	CDisplayDialog *dialog=NULL;
       
   294 	dialog=new(ELeave) CDisplayDialog(aClient->iGroup, aClient->iGc);
       
   295 	TRAPD(err,dialog->ConstructLD());
       
   296 	if (err==KErrNone)
       
   297 		{
       
   298 		dialog->SetTitle(aTitle);
       
   299 		dialog->SetLine1(aLine1);
       
   300 		dialog->SetLine2(aLine2);
       
   301 		dialog->SetNumButtons(1);
       
   302 		dialog->SetButtonText(0,_L("Okay"));
       
   303 		if (dialog->Display()!=0)		// delete dialog
       
   304 			TbPanic(EDialogDisplay);
       
   305 		}
       
   306 	}
       
   307 
       
   308 void doDisplayDialog(TInt aScreenNumber,const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, CTClient *&aClient, const RWindowGroup *aGroup)
       
   309 	{
       
   310 	aClient=new(ELeave) CTClient();
       
   311 	aClient->SetScreenNumber(aScreenNumber);
       
   312 	aClient->ConstructL();
       
   313 //
       
   314 	aClient->iGroup=new(ELeave) CTWindowGroup(aClient);
       
   315 	aClient->iGroup->ConstructL();
       
   316 	aClient->iGroup->GroupWin()->SetOrdinalPosition(0,10);
       
   317 	if (aGroup)
       
   318 		aClient->iGroup->GroupWin()->SetOwningWindowGroup(aGroup->Identifier());
       
   319 //
       
   320 	TRAP_IGNORE(DisplayDialog(aClient, aTitle, aLine1, aLine2));
       
   321 	}
       
   322 
       
   323 void doDisplayDialog(const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, CTClient *&aClient, const RWindowGroup *aGroup)
       
   324 	{
       
   325 	doDisplayDialog(KDefaultScreen,aTitle,aLine1,aLine2,aClient,aGroup);
       
   326 	}
       
   327 
       
   328 EXPORT_C void DisplayDialog(TInt aScreenNumber,const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, const RWindowGroup *aGroup)
       
   329 	{
       
   330 	CTClient *client=NULL;
       
   331 	TRAP_IGNORE(doDisplayDialog(aScreenNumber,aTitle,aLine1,aLine2,client,aGroup));
       
   332 	delete client;
       
   333 	}
       
   334 
       
   335 EXPORT_C void DisplayDialog(const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, const RWindowGroup *aGroup)
       
   336 	{
       
   337 	DisplayDialog(KDefaultScreen,aTitle,aLine1,aLine2,aGroup);
       
   338 	}
       
   339 
       
   340 
       
   341 
       
   342 //CInfoDialog
       
   343 
       
   344 EXPORT_C CInfoDialog::CInfoDialog(CTWindowGroup *aGroupWin,CWindowGc *aGc) :CTDialog(), iGroupWin(aGroupWin), iGc(aGc)
       
   345 	{}
       
   346 
       
   347 EXPORT_C void CInfoDialog::ButtonPressL(TInt aButton)
       
   348 	{
       
   349 	if (aButton==0)
       
   350 		{
       
   351 		SetResult(0);
       
   352 		BaseWin()->SetVisible(EFalse);
       
   353 		CActiveScheduler::Stop();
       
   354 		}
       
   355 	}
       
   356 
       
   357 EXPORT_C void CInfoDialog::ConstructLD()
       
   358 	{
       
   359 	_LIT(OK,"Okay");
       
   360 	CTDialog::ConstructLD(*iGroupWin, *iGc);
       
   361 	SetNumButtons(1);
       
   362 	SetButtonText(0,OK);
       
   363 	SetTakeFocus();
       
   364 	}
       
   365 
       
   366 EXPORT_C void CInfoDialog::TimerResults()
       
   367 	{
       
   368 	TProfile profile[eTimes];
       
   369 	// TProfile only has default constructor -
       
   370 	// constructor of TProfile does not initialize its members
       
   371 	for (TInt jj=0; jj<eTimes; jj++)
       
   372 		{
       
   373 		profile[jj].iTime=0;
       
   374 		profile[jj].iCount=0;
       
   375 		}
       
   376 	__PROFILE_DISPLAY(eTimes)
       
   377 	TBuf<64> times;
       
   378 	TBuf<32> counts;
       
   379 	TInt ii=1;
       
   380 	FOREVER
       
   381 		{
       
   382 		AppendProfileTime(times,profile[ii].iTime);
       
   383 		AppendProfileCount(counts,profile[ii].iCount);
       
   384 		if (++ii==eTimes)
       
   385 			break;
       
   386 		AddComma(times);
       
   387 		AddComma(counts);
       
   388 		}
       
   389 	SetLine1(times);
       
   390 	SetLine2(counts);
       
   391 	times.Zero();
       
   392 	AppendProfileTime(times,profile[0].iTime);
       
   393 	SetTitle(times);
       
   394 	}
       
   395 
       
   396 void CInfoDialog::AppendProfileTime(TDes &aDes, TInt aNum)
       
   397 	{
       
   398 	_LIT(ThreeDP,"%d.%03d");
       
   399 	aDes.AppendFormat(ThreeDP,aNum/eSeconds,(aNum%eSeconds)/1000);
       
   400 	}
       
   401 
       
   402 void CInfoDialog::AppendProfileCount(TDes &aDes, TInt aNum)
       
   403 	{
       
   404 	_LIT(Int,"%d");
       
   405 	aDes.AppendFormat(Int,aNum);
       
   406 	}
       
   407 
       
   408 void CInfoDialog::AddComma(TDes &aDes)
       
   409 	{
       
   410 	_LIT(Comma,", ");
       
   411 	aDes.Append(Comma);
       
   412 	}
       
   413 
       
   414 CTAutoKey::CTAutoKey(RWsSession& aWs): CActive(0), iWs(aWs)
       
   415 	{
       
   416 	CActiveScheduler::Add(this);
       
   417 	}
       
   418 	
       
   419 CTAutoKey::~CTAutoKey()
       
   420 	{
       
   421 	Cancel();
       
   422 	iTimer.Close();
       
   423 	}
       
   424 	
       
   425 void CTAutoKey::ConstructL()
       
   426 	{
       
   427 	User::LeaveIfError(iTimer.CreateLocal());
       
   428 	}
       
   429 	
       
   430 CTAutoKey* CTAutoKey::NewL(RWsSession& aWs)
       
   431 	{
       
   432 	CTAutoKey* self=new(ELeave) CTAutoKey(aWs);
       
   433 	CleanupStack::PushL(self);
       
   434 	self->ConstructL();
       
   435 	CleanupStack::Pop();
       
   436 	return self;
       
   437 	}
       
   438 	
       
   439 void CTAutoKey::Activate(TInt aDelay)
       
   440 	{
       
   441 	if (IsActive())
       
   442 		Cancel();
       
   443 	
       
   444 	iDelay=aDelay*1000000;
       
   445 	iTimer.After(iStatus,iDelay);
       
   446 	SetActive();
       
   447 	}
       
   448 void CTAutoKey::RunL()
       
   449 	{
       
   450 	// simulate key event, only needed to run once
       
   451 	TKeyEvent keyEvent;
       
   452 	keyEvent.iCode=EKeyEnter;
       
   453 	keyEvent.iScanCode=EKeyEnter;
       
   454 	keyEvent.iModifiers=0;
       
   455 	keyEvent.iRepeats=0;
       
   456 	iWs.SimulateKeyEvent(keyEvent);
       
   457 	iWs.Flush();
       
   458 	}
       
   459 	
       
   460 void CTAutoKey::DoCancel()
       
   461 	{
       
   462 	iTimer.Cancel();
       
   463 	}