sl@0: // Copyright (c) 1996-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 the License "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: // f32test\bench\t_select.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include "t_select.h" sl@0: // sl@0: LOCAL_D const TInt KArrayGranularity=16; sl@0: LOCAL_D const TInt KDriveSelector=0; sl@0: sl@0: CSelectionBox* CSelectionBox::NewL(CConsoleBase* aConsole) sl@0: // sl@0: // Create a CSelection box sl@0: // sl@0: { sl@0: sl@0: CSelectionBox* selBox=new(ELeave) CSelectionBox; sl@0: CleanupStack::PushL(selBox); sl@0: selBox->iText=new(ELeave) CArrayFixFlat(KArrayGranularity); sl@0: TSelBoxEntry firstEntry; sl@0: firstEntry.iText=_L("Exit").AllocL(); sl@0: CleanupStack::PushL(firstEntry.iText); sl@0: selBox->iText->InsertL(0,firstEntry); sl@0: CleanupStack::Pop(2); sl@0: selBox->iConsole=aConsole; sl@0: return(selBox); sl@0: } sl@0: sl@0: CSelectionBox::~CSelectionBox() sl@0: // sl@0: // Destructor sl@0: // sl@0: { sl@0: sl@0: if (iText==NULL) sl@0: return; sl@0: TInt count=iText->Count(); sl@0: while (count--) sl@0: User::Free((*iText)[count].iText); sl@0: delete iText; sl@0: } sl@0: sl@0: void CSelectionBox::AddLineL(const TDesC& aText,const TCallBack& aCallBack) sl@0: // sl@0: // Add a line to be displayed sl@0: // sl@0: { sl@0: sl@0: TSelBoxEntry entry; sl@0: entry.iText=aText.AllocL(); sl@0: CleanupStack::PushL(entry.iText); sl@0: entry.iCallBack=aCallBack; sl@0: iText->InsertL(iText->Count()-1,entry); sl@0: CleanupStack::Pop(); sl@0: } sl@0: sl@0: void CSelectionBox::ReplaceTextL(TInt aLine,const TDesC& aText) sl@0: // sl@0: // Replace text at aLine sl@0: // sl@0: { sl@0: sl@0: TSelBoxEntry& entry=(*iText)[aLine]; sl@0: User::Free(entry.iText); sl@0: entry.iText=aText.AllocL(); sl@0: } sl@0: sl@0: void CSelectionBox::Display() sl@0: // sl@0: // Display the box sl@0: // sl@0: { sl@0: iConsole->ClearScreen(); sl@0: sl@0: TSize size=iConsole->ScreenSize(); sl@0: TInt widestLine=0; sl@0: TInt items=iText->Count(); sl@0: sl@0: TInt count=items; sl@0: while (count--) sl@0: widestLine=Max(widestLine,(*iText)[count].iText->Length()); sl@0: sl@0: iTopLeft=TPoint((size.iWidth-widestLine)/2,(size.iHeight-items)/2-2); sl@0: TInt downOffset=iTopLeft.iY; sl@0: sl@0: for (count=0;countSetCursorPosAbs(TPoint(iTopLeft.iX,downOffset)); sl@0: iConsole->Printf(_L("%S"),(*iText)[count].iText); sl@0: downOffset++; sl@0: } sl@0: sl@0: iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-1,iTopLeft.iY+iHighLight)); sl@0: } sl@0: sl@0: void CSelectionBox::DisplayHighLight(TBool aOn) sl@0: // sl@0: // Draw aHighLight to the left of aLine sl@0: // sl@0: { sl@0: sl@0: TBuf<1> cursor(1); sl@0: cursor[0]=(TUint8)((aOn) ? 16 : 32); sl@0: iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-2,iTopLeft.iY+iHighLight)); sl@0: iConsole->Printf(_L("%S"),&cursor); sl@0: } sl@0: sl@0: TChar CSelectionBox::CurrentKeyPress() sl@0: {return(iChar);} sl@0: sl@0: TInt CSelectionBox::CurrentDrive() sl@0: {return(iCurrentDrive);} sl@0: sl@0: TBool CSelectionBox::MediaPresent(TInt aDrive) sl@0: // sl@0: // Return ETrue if a media is present in aDrive sl@0: // sl@0: { sl@0: sl@0: TDriveInfo driveInfo; sl@0: iFs.Drive(driveInfo,aDrive); sl@0: if (driveInfo.iType!=EMediaNotPresent) sl@0: return(ETrue); sl@0: return(EFalse); sl@0: } sl@0: sl@0: TInt CSelectionBox::NextDrive(TInt aDrive) sl@0: // sl@0: // Find the next drive in the driveList sl@0: // sl@0: { sl@0: sl@0: for(TInt i=aDrive+1;i<=EDriveZ;i++) sl@0: { sl@0: if (iDriveList[i]!=0 && MediaPresent(i)) sl@0: return(i); sl@0: } sl@0: return(aDrive); sl@0: } sl@0: sl@0: TInt CSelectionBox::PreviousDrive(TInt aDrive) sl@0: // sl@0: // Find the next drive in the driveList sl@0: // sl@0: { sl@0: sl@0: for (TInt i=aDrive-1;i>=0;i--) sl@0: { sl@0: if (iDriveList[i]!=0 && MediaPresent(i)) sl@0: return(i); sl@0: } sl@0: return(aDrive); sl@0: } sl@0: sl@0: void CSelectionBox::SetDriveName() sl@0: // sl@0: // Set the drive name sl@0: // sl@0: { sl@0: sl@0: TBuf<16> driveName; sl@0: if (PreviousDrive(iCurrentDrive)!=iCurrentDrive) sl@0: driveName+=_L("<-"); sl@0: driveName+=_L("Drive "); sl@0: driveName.Append('A'+iCurrentDrive); sl@0: driveName.Append(':'); sl@0: if (NextDrive(iCurrentDrive)!=iCurrentDrive) sl@0: driveName+=_L("->"); sl@0: while (driveName.Length()<12) sl@0: driveName+=_L(" "); sl@0: (*iText)[0].iText->Des()=driveName; sl@0: } sl@0: sl@0: void CSelectionBox::DisplayDrive() sl@0: // sl@0: // Display the drive sl@0: // sl@0: { sl@0: sl@0: iConsole->SetCursorPosAbs(iTopLeft); sl@0: iConsole->Printf(_L("%S"),(*iText)[0].iText); sl@0: iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-1,iTopLeft.iY)); sl@0: } sl@0: sl@0: GLDEF_C TInt ChangeDrives(TAny* aSelector) sl@0: // sl@0: // Move to next drive if it is mounted sl@0: // sl@0: { sl@0: CSelectionBox& selBox=*(CSelectionBox*)aSelector; sl@0: TChar key=selBox.CurrentKeyPress(); sl@0: TInt drive=selBox.iCurrentDrive; sl@0: if (key==EKeyLeftArrow) sl@0: drive=selBox.PreviousDrive(selBox.iCurrentDrive); sl@0: else if (key==EKeyRightArrow) sl@0: drive=selBox.NextDrive(selBox.iCurrentDrive); sl@0: selBox.iCurrentDrive=drive; sl@0: selBox.SetDriveName(); sl@0: selBox.DisplayDrive(); sl@0: return(KErrNone); sl@0: } sl@0: sl@0: void CSelectionBox::AddDriveSelectorL(RFs aFs) sl@0: // sl@0: // Add a drive selector to the list sl@0: // sl@0: { sl@0: sl@0: if (iDriveSelectorPresent) sl@0: return; sl@0: iDriveSelectorPresent=ETrue; sl@0: iFs=aFs; sl@0: iFs.DriveList(iDriveList); sl@0: iCurrentDrive=EDriveC; sl@0: TSelBoxEntry entry; sl@0: entry.iText=_L("<-Drive X:->").AllocL(); sl@0: CleanupStack::PushL(entry.iText); sl@0: entry.iCallBack=TCallBack(ChangeDrives,this); sl@0: iText->InsertL(0,entry); sl@0: CleanupStack::Pop(); sl@0: SetDriveName(); sl@0: } sl@0: sl@0: void CSelectionBox::Run() sl@0: // sl@0: // Display the box and handle keypresses sl@0: // sl@0: { sl@0: sl@0: Display(); sl@0: sl@0: FOREVER sl@0: { sl@0: iChar=iConsole->Getch(); sl@0: switch (iChar) sl@0: { sl@0: case EKeyEscape: sl@0: return; sl@0: case EKeyHome: sl@0: DisplayHighLight(EFalse); sl@0: iHighLight=KDriveSelector; sl@0: DisplayHighLight(ETrue); sl@0: break; sl@0: case EKeyLeftArrow: sl@0: (*iText)[iHighLight].iCallBack.CallBack(); sl@0: break; sl@0: case EKeyRightArrow: sl@0: (*iText)[iHighLight].iCallBack.CallBack(); sl@0: break; sl@0: case EKeyUpArrow: sl@0: if (iHighLight) sl@0: { sl@0: DisplayHighLight(EFalse); sl@0: iHighLight--; sl@0: DisplayHighLight(ETrue); sl@0: } sl@0: break; sl@0: case EKeyDownArrow: sl@0: if (iHighLight+1Count()) sl@0: { sl@0: DisplayHighLight(EFalse); sl@0: iHighLight++; sl@0: DisplayHighLight(ETrue); sl@0: } sl@0: break; sl@0: case EKeyEnter: sl@0: if (iHighLight+1==iText->Count()) sl@0: return; sl@0: if (iHighLight==KDriveSelector && iDriveSelectorPresent) sl@0: break; sl@0: iConsole->ClearScreen(); sl@0: (*iText)[iHighLight].iCallBack.CallBack(); sl@0: Display(); sl@0: break; sl@0: default: sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: