os/kernelhwsrv/kerneltest/f32test/bench/t_select.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1996-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 the License "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 // f32test\bench\t_select.cpp
    15 // 
    16 //
    17 
    18 #include <f32file.h>
    19 #include <e32test.h>
    20 #include "t_select.h"
    21 //
    22 LOCAL_D const TInt KArrayGranularity=16;
    23 LOCAL_D const TInt KDriveSelector=0;
    24 
    25 CSelectionBox* CSelectionBox::NewL(CConsoleBase* aConsole)
    26 //
    27 // Create a CSelection box
    28 //
    29 	{
    30 
    31 	CSelectionBox* selBox=new(ELeave) CSelectionBox;
    32 	CleanupStack::PushL(selBox);
    33 	selBox->iText=new(ELeave) CArrayFixFlat<TSelBoxEntry>(KArrayGranularity);
    34 	TSelBoxEntry firstEntry;
    35 	firstEntry.iText=_L("Exit").AllocL();
    36 	CleanupStack::PushL(firstEntry.iText);
    37 	selBox->iText->InsertL(0,firstEntry);
    38 	CleanupStack::Pop(2);
    39 	selBox->iConsole=aConsole;
    40 	return(selBox);
    41 	}
    42 
    43 CSelectionBox::~CSelectionBox()
    44 //
    45 // Destructor
    46 //
    47 	{
    48 
    49 	if (iText==NULL)
    50 		return;
    51 	TInt count=iText->Count();
    52 	while (count--)
    53 		User::Free((*iText)[count].iText);
    54 	delete iText;
    55 	}
    56 
    57 void CSelectionBox::AddLineL(const TDesC& aText,const TCallBack& aCallBack)
    58 //
    59 // Add a line to be displayed
    60 //
    61 	{
    62 	
    63 	TSelBoxEntry entry;
    64 	entry.iText=aText.AllocL();
    65 	CleanupStack::PushL(entry.iText);
    66 	entry.iCallBack=aCallBack;
    67 	iText->InsertL(iText->Count()-1,entry);
    68 	CleanupStack::Pop();
    69 	}
    70 
    71 void CSelectionBox::ReplaceTextL(TInt aLine,const TDesC& aText)
    72 //
    73 // Replace text at aLine
    74 //
    75 	{
    76 
    77 	TSelBoxEntry& entry=(*iText)[aLine];
    78 	User::Free(entry.iText);
    79 	entry.iText=aText.AllocL();
    80 	}
    81 
    82 void CSelectionBox::Display()
    83 //
    84 // Display the box
    85 //
    86 	{
    87 	iConsole->ClearScreen();
    88 
    89 	TSize size=iConsole->ScreenSize();
    90 	TInt widestLine=0;
    91 	TInt items=iText->Count();
    92 
    93 	TInt count=items;
    94 	while (count--)
    95 		widestLine=Max(widestLine,(*iText)[count].iText->Length());
    96 
    97 	iTopLeft=TPoint((size.iWidth-widestLine)/2,(size.iHeight-items)/2-2);
    98 	TInt downOffset=iTopLeft.iY;
    99 
   100 	for (count=0;count<items;count++)
   101 		{
   102 		if (count==iHighLight)
   103 			DisplayHighLight(ETrue);
   104 		iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX,downOffset));
   105 		iConsole->Printf(_L("%S"),(*iText)[count].iText);
   106 		downOffset++;
   107 		}
   108 
   109 	iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-1,iTopLeft.iY+iHighLight));
   110 	}
   111 
   112 void CSelectionBox::DisplayHighLight(TBool aOn)
   113 //
   114 // Draw aHighLight to the left of aLine
   115 //
   116 	{
   117 
   118 	TBuf<1> cursor(1);
   119 	cursor[0]=(TUint8)((aOn) ? 16 : 32);
   120 	iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-2,iTopLeft.iY+iHighLight));
   121 	iConsole->Printf(_L("%S"),&cursor);
   122 	}
   123 
   124 TChar CSelectionBox::CurrentKeyPress()
   125 	{return(iChar);}
   126 
   127 TInt CSelectionBox::CurrentDrive()
   128 	{return(iCurrentDrive);}
   129 
   130 TBool CSelectionBox::MediaPresent(TInt aDrive)
   131 //
   132 // Return ETrue if a media is present in aDrive
   133 //
   134 	{
   135 
   136 	TDriveInfo driveInfo;
   137 	iFs.Drive(driveInfo,aDrive);
   138 	if (driveInfo.iType!=EMediaNotPresent)
   139 		return(ETrue);
   140 	return(EFalse);
   141 	}
   142 
   143 TInt CSelectionBox::NextDrive(TInt aDrive)
   144 //
   145 // Find the next drive in the driveList
   146 //
   147 	{
   148 
   149 	for(TInt i=aDrive+1;i<=EDriveZ;i++)
   150 		{
   151 		if (iDriveList[i]!=0 && MediaPresent(i))
   152 			return(i);
   153 		}
   154 	return(aDrive);
   155 	}
   156 		
   157 TInt CSelectionBox::PreviousDrive(TInt aDrive)
   158 //
   159 // Find the next drive in the driveList
   160 //
   161 	{
   162 
   163 	for (TInt i=aDrive-1;i>=0;i--)
   164 		{
   165 		if (iDriveList[i]!=0 && MediaPresent(i))
   166 			return(i);
   167 		}
   168 	return(aDrive);
   169 	}
   170 
   171 void CSelectionBox::SetDriveName()
   172 //
   173 // Set the drive name
   174 //
   175 	{
   176 
   177 	TBuf<16> driveName;
   178 	if (PreviousDrive(iCurrentDrive)!=iCurrentDrive)
   179 		driveName+=_L("<-");
   180 	driveName+=_L("Drive ");
   181 	driveName.Append('A'+iCurrentDrive);
   182 	driveName.Append(':');
   183 	if (NextDrive(iCurrentDrive)!=iCurrentDrive)
   184 		driveName+=_L("->");
   185 	while (driveName.Length()<12)
   186 		driveName+=_L(" ");
   187 	(*iText)[0].iText->Des()=driveName;
   188 	}
   189 
   190 void CSelectionBox::DisplayDrive()
   191 //
   192 // Display the drive
   193 //
   194 	{
   195 
   196 	iConsole->SetCursorPosAbs(iTopLeft);
   197 	iConsole->Printf(_L("%S"),(*iText)[0].iText);
   198 	iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-1,iTopLeft.iY));
   199 	}
   200 
   201 GLDEF_C TInt ChangeDrives(TAny* aSelector)
   202 //
   203 // Move to next drive if it is mounted
   204 //
   205 	{
   206 	CSelectionBox& selBox=*(CSelectionBox*)aSelector;
   207 	TChar key=selBox.CurrentKeyPress();
   208 	TInt drive=selBox.iCurrentDrive;
   209 	if (key==EKeyLeftArrow)
   210 		drive=selBox.PreviousDrive(selBox.iCurrentDrive);
   211 	else if (key==EKeyRightArrow)
   212 		drive=selBox.NextDrive(selBox.iCurrentDrive);
   213 	selBox.iCurrentDrive=drive;
   214 	selBox.SetDriveName();
   215 	selBox.DisplayDrive();
   216 	return(KErrNone);
   217 	}
   218 
   219 void CSelectionBox::AddDriveSelectorL(RFs aFs)
   220 //
   221 // Add a drive selector to the list
   222 //
   223 	{
   224 
   225 	if (iDriveSelectorPresent)
   226 		return;
   227 	iDriveSelectorPresent=ETrue;
   228 	iFs=aFs;
   229 	iFs.DriveList(iDriveList);
   230 	iCurrentDrive=EDriveC;
   231 	TSelBoxEntry entry;
   232 	entry.iText=_L("<-Drive X:->").AllocL();
   233 	CleanupStack::PushL(entry.iText);
   234 	entry.iCallBack=TCallBack(ChangeDrives,this);
   235 	iText->InsertL(0,entry);
   236 	CleanupStack::Pop();
   237 	SetDriveName();
   238 	}
   239 
   240 void CSelectionBox::Run()
   241 //
   242 // Display the box and handle keypresses
   243 //
   244 	{
   245 	
   246 	Display();
   247 
   248 	FOREVER
   249 		{
   250 		iChar=iConsole->Getch();
   251 		switch (iChar)
   252 	    	{
   253 		case EKeyEscape:
   254 			return;
   255 		case EKeyHome:
   256 			DisplayHighLight(EFalse);
   257 			iHighLight=KDriveSelector;
   258 			DisplayHighLight(ETrue);
   259 			break;
   260 		case EKeyLeftArrow:
   261 			(*iText)[iHighLight].iCallBack.CallBack();
   262 			break;
   263 		case EKeyRightArrow:
   264 			(*iText)[iHighLight].iCallBack.CallBack();
   265 			break;
   266 		case EKeyUpArrow:
   267 			if (iHighLight)
   268 				{
   269 				DisplayHighLight(EFalse);
   270 				iHighLight--;
   271 				DisplayHighLight(ETrue);
   272 				}
   273 			break;
   274 		case EKeyDownArrow:
   275 			if (iHighLight+1<iText->Count())
   276 				{
   277 				DisplayHighLight(EFalse);
   278 				iHighLight++;
   279 				DisplayHighLight(ETrue);
   280 				}
   281 			break;
   282 		case EKeyEnter:
   283 			if (iHighLight+1==iText->Count())
   284 				return;
   285 			if (iHighLight==KDriveSelector && iDriveSelectorPresent)
   286 				break;
   287 			iConsole->ClearScreen();
   288 			(*iText)[iHighLight].iCallBack.CallBack();
   289 			Display();
   290 			break;
   291 		default:
   292 			break;
   293 			}
   294 		}
   295 	}
   296