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