sl@0: // Copyright (c) 1994-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Base classes used for building window server test code sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "TLIB.H" sl@0: sl@0: const TInt ButtonGap=20; sl@0: const TInt ButtonBorderGap=10; sl@0: const TInt ButtonBorderMargin=3; sl@0: const TInt TextMargin=5; sl@0: const TInt KAutoDelaySeconds=60; sl@0: sl@0: NONSHARABLE_CLASS(CTAutoKey): public CActive sl@0: { sl@0: public: sl@0: static CTAutoKey* NewL(RWsSession& aWs); sl@0: ~CTAutoKey(); sl@0: void Activate(TInt aDelay); sl@0: sl@0: void RunL(); sl@0: void DoCancel(); sl@0: sl@0: private: sl@0: CTAutoKey(RWsSession& aWs); sl@0: void ConstructL(); sl@0: sl@0: RTimer iTimer; sl@0: TInt iDelay; sl@0: RWsSession& iWs; sl@0: }; sl@0: sl@0: EXPORT_C CTDialog::CTDialog() : CTTitledWindow(), iNumButtons(1) sl@0: { sl@0: iButton[0].Copy(_L("Continue")); // Default button sl@0: } sl@0: sl@0: EXPORT_C CTDialog::~CTDialog() sl@0: { sl@0: if (iIsActive) sl@0: CActiveScheduler::Stop(); sl@0: delete iAutoKey; sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::ConstructLD(CTWinBase &aParent,CWindowGc &aGc) sl@0: { sl@0: iOldFocus=aParent.Group()->CurWin(); sl@0: TRAPD(err,CTTitledWindow::ConstructL(aParent)); sl@0: if (err!=KErrNone) sl@0: { sl@0: delete this; sl@0: User::Leave(err); sl@0: } sl@0: iActivated=ETrue; sl@0: iWin.SetBackgroundColor(TRgb::Gray256(238)); // Light gray sl@0: AssignGC(aGc); sl@0: iWin.SetPointerCapture(ETrue); sl@0: Group()->SetCurrentWindow(this, ETrue); sl@0: iAutoKey=CTAutoKey::NewL(Client()->iWs); sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::RelinquishFocus() sl@0: { sl@0: Group()->SetCurrentWindow(iOldFocus); sl@0: } sl@0: sl@0: EXPORT_C TInt CTDialog::Display() sl@0: { sl@0: TInt result; sl@0: sl@0: SetWindowSize(); sl@0: if (iWinActive) sl@0: BaseWin()->SetVisible(ETrue); sl@0: else sl@0: { sl@0: Activate(); sl@0: iWinActive=ETrue; sl@0: } sl@0: if (iTakeFocus) sl@0: Group()->SetCurrentWindow(this); sl@0: Group()->GroupWin()->SetOrdinalPosition(0); sl@0: iResultPtr=&result; sl@0: CTClient *client=Client(); sl@0: if (client->QueueRead()) sl@0: { sl@0: client->iWs.Flush(); sl@0: client=NULL; sl@0: } sl@0: iIsActive=ETrue; sl@0: iAutoKey->Activate(KAutoDelaySeconds); sl@0: CActiveScheduler::Start(); sl@0: if (client) sl@0: client->CancelRead(); sl@0: return(result); sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::SetFlags(TUint aFlags) sl@0: { sl@0: iFlags=aFlags; sl@0: } sl@0: sl@0: void CTDialog::SetMaxWid(TInt &aMax, TInt aWid) sl@0: { sl@0: if (aWid>aMax) sl@0: aMax=aWid; sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::SetWindowSize() sl@0: { sl@0: if (iActivated) sl@0: { sl@0: TInt max=0; sl@0: SetMaxWid(max,iFont->TextWidthInPixels(iTitle)); sl@0: SetMaxWid(max,iFont->TextWidthInPixels(iLine1)); sl@0: SetMaxWid(max,iFont->TextWidthInPixels(iLine2)); sl@0: max+=TextMargin*2; sl@0: iButWid=0; sl@0: if (iNumButtons>0) sl@0: { sl@0: for(TInt index=0;indexTextWidthInPixels(iButton[index])); sl@0: iButWid+=ButtonBorderMargin*2; sl@0: SetMaxWid(max,iButWid*iNumButtons+ButtonGap*(iNumButtons-1)+ButtonBorderGap*2); sl@0: } sl@0: TSize parSize=Parent()->Size(); sl@0: TSize size(max,iFont->HeightInPixels()*8); sl@0: TPoint pos((parSize.iWidth-size.iWidth)/2,(parSize.iHeight-size.iHeight)/2); sl@0: if (iFlags&EDialogDisplayAtBottom) sl@0: pos.iY*=2; sl@0: if (iFlags&EDialogDisplayAtLeft) sl@0: pos.iX=0; sl@0: SetExt(pos,size); sl@0: Invalidate(); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::SetLine1(const TDesC &aLine1) sl@0: { sl@0: iLine1.Copy(aLine1); sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::SetLine2(const TDesC &aLine2) sl@0: { sl@0: iLine2.Copy(aLine2); sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::SetNumButtons(TInt aNum) sl@0: { sl@0: if ((TUint)aNum>3) sl@0: TbPanic(EDialogButtonCount); sl@0: iNumButtons=aNum; sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::SetButtonText(TInt aNum,const TDesC &aButton) sl@0: { sl@0: if ((TUint)aNum>(TUint)iNumButtons) sl@0: TbPanic(EDialogButtonIndex); sl@0: iButton[aNum].Copy(aButton); sl@0: } sl@0: sl@0: TRect CTDialog::ButtonRect(TInt aIndex) const sl@0: { sl@0: TInt chunk=(iSize.iWidth-ButtonBorderMargin*2)/iNumButtons; sl@0: TInt midPos=ButtonBorderMargin+chunk*aIndex+chunk/2; sl@0: return(TRect(midPos-iButWid/2,iFont->HeightInPixels()*6,midPos+iButWid/2,iFont->HeightInPixels()*7+ButtonBorderMargin*2)); sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::Draw() sl@0: { sl@0: CTTitledWindow::Draw(); sl@0: iGc->SetPenColor(TRgb::Gray16(0)); sl@0: iGc->DrawText(iLine1, TPoint((iSize.iWidth-iFont->TextWidthInPixels(iLine1))/2,iFont->HeightInPixels()*3)); sl@0: iGc->DrawText(iLine2, TPoint((iSize.iWidth-iFont->TextWidthInPixels(iLine2))/2,iFont->HeightInPixels()*4+2)); sl@0: if (iNumButtons!=0) sl@0: { sl@0: for(TInt index=0;indexSetBrushStyle(CGraphicsContext::ESolidBrush); sl@0: iGc->SetBrushColor(TRgb::Gray256(255)); sl@0: iGc->DrawRect(rect); sl@0: iGc->DrawRect(rect); sl@0: iGc->SetBrushStyle(CGraphicsContext::ENullBrush); sl@0: TInt midPos=(rect.iTl.iX+rect.iBr.iX)/2; sl@0: iGc->DrawText(iButton[index], TPoint((midPos-iFont->TextWidthInPixels(iButton[index])/2), sl@0: iFont->HeightInPixels()*6+iFont->AscentInPixels()+ButtonBorderMargin)); sl@0: } sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::WinKeyL(const TKeyEvent &aKey,const TTime&) sl@0: { sl@0: switch(aKey.iCode) sl@0: { sl@0: case EKeyEscape: sl@0: case '0': sl@0: ButtonPressL(0); sl@0: break; sl@0: case EKeyEnter: sl@0: case '1': sl@0: ButtonPressL(iNumButtons>1 ? 1 : 0); // Same as ESC on a single button dialog sl@0: break; sl@0: case ' ': sl@0: case '2': sl@0: ButtonPressL(2); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void CTDialog::PointerL(const TPointerEvent &aPointer,const TTime &aTime) sl@0: { sl@0: if (aPointer.iType==TPointerEvent::EButton1Up) sl@0: { sl@0: if (iButtonClickOn>0) sl@0: { sl@0: ButtonPressL(iButtonClickOn-1); sl@0: return; sl@0: } sl@0: } sl@0: else if (aPointer.iType==TPointerEvent::EButton1Down) sl@0: { sl@0: for(TInt index=0;indexiGroup, aClient->iGc); sl@0: TRAPD(err,dialog->ConstructLD()); sl@0: if (err==KErrNone) sl@0: { sl@0: dialog->SetTitle(aTitle); sl@0: dialog->SetLine1(aLine1); sl@0: dialog->SetLine2(aLine2); sl@0: dialog->SetNumButtons(1); sl@0: dialog->SetButtonText(0,_L("Okay")); sl@0: if (dialog->Display()!=0) // delete dialog sl@0: TbPanic(EDialogDisplay); sl@0: } sl@0: } sl@0: sl@0: void doDisplayDialog(TInt aScreenNumber,const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, CTClient *&aClient, const RWindowGroup *aGroup) sl@0: { sl@0: aClient=new(ELeave) CTClient(); sl@0: aClient->SetScreenNumber(aScreenNumber); sl@0: aClient->ConstructL(); sl@0: // sl@0: aClient->iGroup=new(ELeave) CTWindowGroup(aClient); sl@0: aClient->iGroup->ConstructL(); sl@0: aClient->iGroup->GroupWin()->SetOrdinalPosition(0,10); sl@0: if (aGroup) sl@0: aClient->iGroup->GroupWin()->SetOwningWindowGroup(aGroup->Identifier()); sl@0: // sl@0: TRAP_IGNORE(DisplayDialog(aClient, aTitle, aLine1, aLine2)); sl@0: } sl@0: sl@0: void doDisplayDialog(const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, CTClient *&aClient, const RWindowGroup *aGroup) sl@0: { sl@0: doDisplayDialog(KDefaultScreen,aTitle,aLine1,aLine2,aClient,aGroup); sl@0: } sl@0: sl@0: EXPORT_C void DisplayDialog(TInt aScreenNumber,const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, const RWindowGroup *aGroup) sl@0: { sl@0: CTClient *client=NULL; sl@0: TRAP_IGNORE(doDisplayDialog(aScreenNumber,aTitle,aLine1,aLine2,client,aGroup)); sl@0: delete client; sl@0: } sl@0: sl@0: EXPORT_C void DisplayDialog(const TWindowTitle &aTitle, const TDesC &aLine1, const TDesC &aLine2, const RWindowGroup *aGroup) sl@0: { sl@0: DisplayDialog(KDefaultScreen,aTitle,aLine1,aLine2,aGroup); sl@0: } sl@0: sl@0: sl@0: sl@0: //CInfoDialog sl@0: sl@0: EXPORT_C CInfoDialog::CInfoDialog(CTWindowGroup *aGroupWin,CWindowGc *aGc) :CTDialog(), iGroupWin(aGroupWin), iGc(aGc) sl@0: {} sl@0: sl@0: EXPORT_C void CInfoDialog::ButtonPressL(TInt aButton) sl@0: { sl@0: if (aButton==0) sl@0: { sl@0: SetResult(0); sl@0: BaseWin()->SetVisible(EFalse); sl@0: CActiveScheduler::Stop(); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void CInfoDialog::ConstructLD() sl@0: { sl@0: _LIT(OK,"Okay"); sl@0: CTDialog::ConstructLD(*iGroupWin, *iGc); sl@0: SetNumButtons(1); sl@0: SetButtonText(0,OK); sl@0: SetTakeFocus(); sl@0: } sl@0: sl@0: EXPORT_C void CInfoDialog::TimerResults() sl@0: { sl@0: TProfile profile[eTimes]; sl@0: // TProfile only has default constructor - sl@0: // constructor of TProfile does not initialize its members sl@0: for (TInt jj=0; jj times; sl@0: TBuf<32> counts; sl@0: TInt ii=1; sl@0: FOREVER sl@0: { sl@0: AppendProfileTime(times,profile[ii].iTime); sl@0: AppendProfileCount(counts,profile[ii].iCount); sl@0: if (++ii==eTimes) sl@0: break; sl@0: AddComma(times); sl@0: AddComma(counts); sl@0: } sl@0: SetLine1(times); sl@0: SetLine2(counts); sl@0: times.Zero(); sl@0: AppendProfileTime(times,profile[0].iTime); sl@0: SetTitle(times); sl@0: } sl@0: sl@0: void CInfoDialog::AppendProfileTime(TDes &aDes, TInt aNum) sl@0: { sl@0: _LIT(ThreeDP,"%d.%03d"); sl@0: aDes.AppendFormat(ThreeDP,aNum/eSeconds,(aNum%eSeconds)/1000); sl@0: } sl@0: sl@0: void CInfoDialog::AppendProfileCount(TDes &aDes, TInt aNum) sl@0: { sl@0: _LIT(Int,"%d"); sl@0: aDes.AppendFormat(Int,aNum); sl@0: } sl@0: sl@0: void CInfoDialog::AddComma(TDes &aDes) sl@0: { sl@0: _LIT(Comma,", "); sl@0: aDes.Append(Comma); sl@0: } sl@0: sl@0: CTAutoKey::CTAutoKey(RWsSession& aWs): CActive(0), iWs(aWs) sl@0: { sl@0: CActiveScheduler::Add(this); sl@0: } sl@0: sl@0: CTAutoKey::~CTAutoKey() sl@0: { sl@0: Cancel(); sl@0: iTimer.Close(); sl@0: } sl@0: sl@0: void CTAutoKey::ConstructL() sl@0: { sl@0: User::LeaveIfError(iTimer.CreateLocal()); sl@0: } sl@0: sl@0: CTAutoKey* CTAutoKey::NewL(RWsSession& aWs) sl@0: { sl@0: CTAutoKey* self=new(ELeave) CTAutoKey(aWs); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: void CTAutoKey::Activate(TInt aDelay) sl@0: { sl@0: if (IsActive()) sl@0: Cancel(); sl@0: sl@0: iDelay=aDelay*1000000; sl@0: iTimer.After(iStatus,iDelay); sl@0: SetActive(); sl@0: } sl@0: void CTAutoKey::RunL() sl@0: { sl@0: // simulate key event, only needed to run once sl@0: TKeyEvent keyEvent; sl@0: keyEvent.iCode=EKeyEnter; sl@0: keyEvent.iScanCode=EKeyEnter; sl@0: keyEvent.iModifiers=0; sl@0: keyEvent.iRepeats=0; sl@0: iWs.SimulateKeyEvent(keyEvent); sl@0: iWs.Flush(); sl@0: } sl@0: sl@0: void CTAutoKey::DoCancel() sl@0: { sl@0: iTimer.Cancel(); sl@0: }