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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\bench\t_select.cpp
22 LOCAL_D const TInt KArrayGranularity=16;
23 LOCAL_D const TInt KDriveSelector=0;
25 CSelectionBox* CSelectionBox::NewL(CConsoleBase* aConsole)
27 // Create a CSelection box
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);
39 selBox->iConsole=aConsole;
43 CSelectionBox::~CSelectionBox()
51 TInt count=iText->Count();
53 User::Free((*iText)[count].iText);
57 void CSelectionBox::AddLineL(const TDesC& aText,const TCallBack& aCallBack)
59 // Add a line to be displayed
64 entry.iText=aText.AllocL();
65 CleanupStack::PushL(entry.iText);
66 entry.iCallBack=aCallBack;
67 iText->InsertL(iText->Count()-1,entry);
71 void CSelectionBox::ReplaceTextL(TInt aLine,const TDesC& aText)
73 // Replace text at aLine
77 TSelBoxEntry& entry=(*iText)[aLine];
78 User::Free(entry.iText);
79 entry.iText=aText.AllocL();
82 void CSelectionBox::Display()
87 iConsole->ClearScreen();
89 TSize size=iConsole->ScreenSize();
91 TInt items=iText->Count();
95 widestLine=Max(widestLine,(*iText)[count].iText->Length());
97 iTopLeft=TPoint((size.iWidth-widestLine)/2,(size.iHeight-items)/2-2);
98 TInt downOffset=iTopLeft.iY;
100 for (count=0;count<items;count++)
102 if (count==iHighLight)
103 DisplayHighLight(ETrue);
104 iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX,downOffset));
105 iConsole->Printf(_L("%S"),(*iText)[count].iText);
109 iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-1,iTopLeft.iY+iHighLight));
112 void CSelectionBox::DisplayHighLight(TBool aOn)
114 // Draw aHighLight to the left of aLine
119 cursor[0]=(TUint8)((aOn) ? 16 : 32);
120 iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-2,iTopLeft.iY+iHighLight));
121 iConsole->Printf(_L("%S"),&cursor);
124 TChar CSelectionBox::CurrentKeyPress()
127 TInt CSelectionBox::CurrentDrive()
128 {return(iCurrentDrive);}
130 TBool CSelectionBox::MediaPresent(TInt aDrive)
132 // Return ETrue if a media is present in aDrive
136 TDriveInfo driveInfo;
137 iFs.Drive(driveInfo,aDrive);
138 if (driveInfo.iType!=EMediaNotPresent)
143 TInt CSelectionBox::NextDrive(TInt aDrive)
145 // Find the next drive in the driveList
149 for(TInt i=aDrive+1;i<=EDriveZ;i++)
151 if (iDriveList[i]!=0 && MediaPresent(i))
157 TInt CSelectionBox::PreviousDrive(TInt aDrive)
159 // Find the next drive in the driveList
163 for (TInt i=aDrive-1;i>=0;i--)
165 if (iDriveList[i]!=0 && MediaPresent(i))
171 void CSelectionBox::SetDriveName()
173 // Set the drive name
178 if (PreviousDrive(iCurrentDrive)!=iCurrentDrive)
180 driveName+=_L("Drive ");
181 driveName.Append('A'+iCurrentDrive);
182 driveName.Append(':');
183 if (NextDrive(iCurrentDrive)!=iCurrentDrive)
185 while (driveName.Length()<12)
187 (*iText)[0].iText->Des()=driveName;
190 void CSelectionBox::DisplayDrive()
196 iConsole->SetCursorPosAbs(iTopLeft);
197 iConsole->Printf(_L("%S"),(*iText)[0].iText);
198 iConsole->SetCursorPosAbs(TPoint(iTopLeft.iX-1,iTopLeft.iY));
201 GLDEF_C TInt ChangeDrives(TAny* aSelector)
203 // Move to next drive if it is mounted
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();
219 void CSelectionBox::AddDriveSelectorL(RFs aFs)
221 // Add a drive selector to the list
225 if (iDriveSelectorPresent)
227 iDriveSelectorPresent=ETrue;
229 iFs.DriveList(iDriveList);
230 iCurrentDrive=EDriveC;
232 entry.iText=_L("<-Drive X:->").AllocL();
233 CleanupStack::PushL(entry.iText);
234 entry.iCallBack=TCallBack(ChangeDrives,this);
235 iText->InsertL(0,entry);
240 void CSelectionBox::Run()
242 // Display the box and handle keypresses
250 iChar=iConsole->Getch();
256 DisplayHighLight(EFalse);
257 iHighLight=KDriveSelector;
258 DisplayHighLight(ETrue);
261 (*iText)[iHighLight].iCallBack.CallBack();
264 (*iText)[iHighLight].iCallBack.CallBack();
269 DisplayHighLight(EFalse);
271 DisplayHighLight(ETrue);
275 if (iHighLight+1<iText->Count())
277 DisplayHighLight(EFalse);
279 DisplayHighLight(ETrue);
283 if (iHighLight+1==iText->Count())
285 if (iHighLight==KDriveSelector && iDriveSelectorPresent)
287 iConsole->ClearScreen();
288 (*iText)[iHighLight].iCallBack.CallBack();