mpx/tsrc/public/basic/common/testviewframework/src/testmenuview.cpp
changeset 62 b276843a15ba
equal deleted inserted replaced
58:c76ea6caa649 62:b276843a15ba
       
     1 /*
       
     2 * Copyright (c) 2002 - 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  part of testviewframework.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <e32base.h>
       
    20 #include "testbaseview.h"
       
    21 #include "consolemain.h"
       
    22 #include "testmenuview.h"
       
    23 
       
    24 // CONSTANTS
       
    25 const TInt KMenuOverhead = 4;              // overhead
       
    26 const TInt KMaxLineLength = 80;            // Longest supported line length
       
    27 const TInt KArrGranularity = 4;            // DesC array granularity
       
    28 
       
    29 // LOCAL FUNCTION PROTOTYPES
       
    30 LOCAL_C void LimitedAppend( TDes& aOriginal, const TDesC& aAppend);
       
    31 
       
    32 // ==================== LOCAL FUNCTIONS =======================================
       
    33 
       
    34 LOCAL_C void LimitedAppend( TDes& aOriginal, const TDesC& aAppend)
       
    35 	{
       
    36 
       
    37 	TInt spaceLeft = aOriginal.MaxLength() - aOriginal.Length();
       
    38     
       
    39 	if (spaceLeft > aAppend.Length())
       
    40 		{
       
    41 		aOriginal.Append ( aAppend );
       
    42 		}
       
    43 	else
       
    44 		{		
       
    45 		aOriginal.Append ( aAppend.Left ( spaceLeft ) );
       
    46 		}
       
    47 
       
    48 	}
       
    49 
       
    50 //
       
    51 //=========================================================================== *
       
    52 //                    Implementation for Class CTestBaseView                     *
       
    53 //=========================================================================== *
       
    54 //
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 //  Method: ~CTestMenuView
       
    58 //  Description: Destructor
       
    59 //  Parameters: None
       
    60 //  Return Values: None
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 EXPORT_C CTestMenuView::~CTestMenuView()
       
    64     {
       
    65     iItems->Reset();
       
    66     delete iItems;
       
    67     }
       
    68     
       
    69 // ---------------------------------------------------------------------------
       
    70 //  Method: DisplayViewL
       
    71 //  Description: Prints the menu
       
    72 //  Parameters: None
       
    73 //  Return Values: None
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 EXPORT_C void CTestMenuView::DisplayViewL()
       
    77     {
       
    78     CTestBaseView::DisplayViewBase();
       
    79     
       
    80     TBuf<KMaxLineLength> line;
       
    81 
       
    82     // Clear display
       
    83     iConsole->ClearScreen();
       
    84 
       
    85     // Print menu title and header
       
    86     Print( Name() );
       
    87     
       
    88     TInt oldItemCount = iItemCount;
       
    89      
       
    90     iItemCount = ItemCount();
       
    91 
       
    92     // If first time in view, update start and end positions
       
    93     if( (iFirst == iLast) ||        // First time here..
       
    94         ( iLast >= iItemCount ) ||
       
    95         ( oldItemCount != iItemCount ) )      // View size changed
       
    96         {
       
    97         iLast = iItemCount - 1;
       
    98         iPosOnScreen = 0;
       
    99 
       
   100         // If "overflow", then adjust the end
       
   101         if (iLast > iScreenSize )
       
   102             {
       
   103             iLast = iScreenSize;
       
   104             }
       
   105         }
       
   106     
       
   107     // Print items
       
   108     for ( TInt i = iFirst; i <= iLast; i++ )
       
   109         {
       
   110         line.Zero();
       
   111         // Append text before line
       
   112         AppendBefore( i, line );
       
   113         // Get the menu line
       
   114 		LimitedAppend ( line, ItemL(i) );
       
   115 		// Print the line
       
   116         Print(line);
       
   117         }
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 //  Method: SelectL
       
   122 //  Description: Process keypresses in view. updates position
       
   123 //  Parameters: TKeyCode aSelection       :in:      Key
       
   124 //              TBool& aContinue          :out:     Has user pressed "Quit"
       
   125 //  Return Values: None
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C void CTestMenuView::SelectL( TKeyCode aSelection, TBool& aContinue )
       
   129     {
       
   130     iKey = aSelection;
       
   131 
       
   132 //	iConsole->Printf(_L("%d"),iKey);
       
   133 	
       
   134     switch ( (TInt) aSelection )
       
   135     {
       
   136     // SelectL item
       
   137     case EKeyEnter:
       
   138     case EKeyRightArrow:
       
   139         HandleRightKeyL();
       
   140         break;
       
   141     // Going back
       
   142     case EKeyLeftArrow:
       
   143         HandleLeftKeyL();
       
   144         break;
       
   145     // Number key
       
   146     case  92: // '1'
       
   147     case  97: // '2'
       
   148     case 100: // '3'
       
   149  //   case '2':
       
   150  //   case '3':
       
   151  //   case '4':
       
   152  //   case '5':
       
   153  //   case '6':
       
   154  //   case '7':
       
   155  //   case '8':
       
   156  //   case '9':
       
   157  //   case '0':
       
   158         HandleNumKeyL();
       
   159         break;
       
   160     // Go down
       
   161     case EKeyDownArrow:
       
   162         if ( iFirst + iPosOnScreen == iItemCount - 1 )
       
   163             {
       
   164             // If end of the list, go to beginning
       
   165             iLast = iLast - iFirst;
       
   166             iFirst = 0;
       
   167             iPosOnScreen = 0;
       
   168             }
       
   169         else 
       
   170             {
       
   171             if ( iPosOnScreen == iScreenSize )
       
   172                 {
       
   173                 // If in end of screen, update items
       
   174                 if ( iLast != (iItemCount - 1) )
       
   175                     {
       
   176                     // If not end of the list, then update first and last
       
   177                     // but do not update position in screen.
       
   178                     iLast++;
       
   179                     iFirst++;
       
   180                     }
       
   181                 }
       
   182             else
       
   183                 {
       
   184                 // Going down "in-screen", no need to update items
       
   185                 iPosOnScreen++;
       
   186                 }
       
   187             }
       
   188         break;
       
   189         
       
   190     // Go Up
       
   191     case EKeyUpArrow:
       
   192         if ( iFirst + iPosOnScreen == 0 )
       
   193             {
       
   194             // If in the beginning of the list
       
   195 
       
   196             if ( iItemCount <= iScreenSize ) 
       
   197                 {
       
   198                 // Wrap around when the list is not full
       
   199                 iPosOnScreen = iItemCount-1;
       
   200                 }
       
   201             else
       
   202                 {
       
   203                 // Wrap around when there are more items than
       
   204                 // can be shown at once.            
       
   205                 iPosOnScreen = iScreenSize;
       
   206                 iLast = iItemCount-1;
       
   207                 iFirst = iItemCount - iPosOnScreen - 1;
       
   208                 }
       
   209             }
       
   210         else if ( iPosOnScreen == 0 )
       
   211             {
       
   212             // If not at the beginning of the list, then update first and
       
   213             // last  but do not update position in screen.
       
   214             if ( iFirst != 0 )
       
   215                 {
       
   216                 iLast--;
       
   217                 iFirst--;
       
   218                 }
       
   219             }
       
   220         else
       
   221             {
       
   222             // Going up "in-screen", no need to update items
       
   223             iPosOnScreen--;
       
   224             }
       
   225         
       
   226         break;
       
   227 
       
   228 	// Additional keys
       
   229 	case EKeyHome:
       
   230 		iPosOnScreen = 0;
       
   231 		iFirst = 0;
       
   232 		iLast = iScreenSize;
       
   233 
       
   234 		if (iLast > iItemCount-1 )
       
   235 			{
       
   236 			iLast = iItemCount-1;
       
   237 			}
       
   238 		break;
       
   239 
       
   240 	case EKeyEnd:
       
   241 		iPosOnScreen = iScreenSize;
       
   242 		iLast = iItemCount-1;
       
   243 		iFirst = iLast - iScreenSize;
       
   244 
       
   245 		if (iFirst < 0)
       
   246 			{
       
   247 			iFirst = 0;
       
   248 			iPosOnScreen = iLast-1;
       
   249 			}
       
   250 		break;
       
   251 
       
   252 	case EKeyPageUp:
       
   253 		iFirst = iFirst - iScreenSize - 1;
       
   254 		iLast = iLast - iScreenSize - 1;
       
   255 
       
   256 		if ( iFirst < 0 )
       
   257 			{
       
   258 			iFirst = 0;
       
   259 			iPosOnScreen = 0;			
       
   260 			iLast = iScreenSize;
       
   261 			if (iLast > iItemCount-1 )
       
   262 				{
       
   263 				iLast = iItemCount-1;
       
   264 				}
       
   265 			}
       
   266 		break;
       
   267 
       
   268 	case EKeyPageDown:
       
   269 		iFirst = iFirst + iScreenSize + 1;
       
   270 		iLast = iLast + iScreenSize + 1;
       
   271 
       
   272 		// Going too far
       
   273 		if (iLast > iItemCount-1)
       
   274 			{
       
   275 			iLast = iItemCount-1;
       
   276 			iFirst = iLast - iScreenSize;
       
   277 			iPosOnScreen = iScreenSize;
       
   278 			}
       
   279 
       
   280 		// Ok, list is smaller than screen
       
   281 		if (iFirst < 0 )
       
   282 			{
       
   283 			iFirst = 0;
       
   284 			iLast = iItemCount-1;
       
   285 			iPosOnScreen = iLast;
       
   286 			}
       
   287 
       
   288 		break;
       
   289     case EKeyEscape:
       
   290         aContinue = EFalse;
       
   291         CurrentViewDoneL();
       
   292         return;
       
   293     default:  // Bypass the keypress
       
   294         break;
       
   295     }
       
   296     // Continue normally and keep in the same menu
       
   297     aContinue = ETrue;
       
   298     }
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 //  Method: TimerUpdate
       
   302 //  Description: TimerUpdate
       
   303 //  Parameters: None
       
   304 //  Return Values: None
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 EXPORT_C void CTestMenuView::TimerUpdate()
       
   308     {    
       
   309     TInt count = ItemCount();
       
   310 	// If list is empty, do not scroll.
       
   311 	if ( count < 1 )
       
   312 		{
       
   313 		return;
       
   314 		}
       
   315     else if( iFirst + iPosOnScreen > count )
       
   316         {
       
   317         iFirst = iPosOnScreen = 0;
       
   318         }
       
   319 
       
   320 	// If item selection is updated, then restart scrolling
       
   321 	if (iPrevPos != iFirst + iPosOnScreen)
       
   322 		{
       
   323 		iPrevPos = iFirst + iPosOnScreen;
       
   324 		iStart = 0;
       
   325 		iDirection = 1;
       
   326 		}
       
   327 
       
   328 	// If menu item have not been changed after last timer, then
       
   329 	// start scrolling	
       
   330 	TName name(KNullDesC);
       
   331 	TRAPD(err, name = ItemL(iFirst + iPosOnScreen));
       
   332 	if(err != KErrNone)
       
   333 	    return;
       
   334 	if ( name.Length() > iSize.iWidth)
       
   335 		{
       
   336 
       
   337 		TInt y = iConsole->WhereY();
       
   338 		TInt x = iConsole->WhereX();
       
   339 		TBuf<80> iTmp;				
       
   340 
       
   341 		iStart = iStart + iDirection;
       
   342 
       
   343 		// "Right end"
       
   344 		if ( iStart + iSize.iWidth > name.Length() +KMenuOverhead )
       
   345 			{
       
   346 			iStart--;
       
   347 			iDirection = -1;
       
   348 			}
       
   349 		
       
   350 		// "Left end"
       
   351 		if ( iStart == -1 )
       
   352 			{
       
   353 			iStart++;
       
   354 			iDirection = 1;
       
   355 			}
       
   356 
       
   357 		iTmp=_L(" *");
       
   358 		LimitedAppend( iTmp, name.Mid ( iStart ) );
       
   359 			
       
   360 		iConsole->SetPos( 0, iPosOnScreen+1);		
       
   361 		iConsole->Printf ( iTmp.Left( iSize.iWidth -2 )  );
       
   362 
       
   363 		iConsole->SetPos(x,y);
       
   364 		}    
       
   365     }
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 //  Method: CTestMenuView
       
   369 //  Description: C++ default constructor
       
   370 //  Parameters: CConsoleMain* aConsole  :in: Pointer to main console
       
   371 //              CTestBaseView* aParent     :in: Parent View
       
   372 //              const TDesC& aName      :in: Menu name
       
   373 //  Return Values: None
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 EXPORT_C CTestMenuView::CTestMenuView(CConsoleMain* aConsoleMain,
       
   377                                    CTestBaseView* aParent,
       
   378                                    const TDesC& aName)
       
   379     : CTestBaseView(aConsoleMain, aParent, aName)
       
   380     {
       
   381 	iDirection = 1;
       
   382 	iPrevPos  = -1;		// Invalid starting value
       
   383     }
       
   384     
       
   385 // ---------------------------------------------------------------------------
       
   386 //  Method: ConstructL
       
   387 //  Description: Second level constructor.
       
   388 //  Parameters: None
       
   389 //  Return Values: None
       
   390 // ---------------------------------------------------------------------------
       
   391 //
       
   392 EXPORT_C void CTestMenuView::ConstructL()
       
   393     {
       
   394     iItems = new (ELeave) CDesCArrayFlat(KArrGranularity);
       
   395     
       
   396     // Get display size
       
   397     const TInt KOverHead = 5;
       
   398     iSize = iConsole->ScreenSize();
       
   399     iScreenSize = iSize.iHeight - KOverHead;   // Size available for menus
       
   400     
       
   401     InitializeViewL();
       
   402     }
       
   403     
       
   404 // ---------------------------------------------------------------------------
       
   405 //  Method: Print
       
   406 //  Description: Prints one line text and changes to next line. If line is
       
   407 //  too long, overhead is not printed..
       
   408 //  Parameters: TDesC& aPrint   :in:    Text to be printed
       
   409 //  Return Values: None
       
   410 // ---------------------------------------------------------------------------
       
   411 //
       
   412 EXPORT_C void CTestMenuView::Print( const TDesC& aPrint )
       
   413     {
       
   414     iConsole->Printf( aPrint.Left( iSize.iWidth - KMenuOverhead ) );
       
   415     iConsole->Printf(_L("\n"));
       
   416     }
       
   417 
       
   418 // ---------------------------------------------------------------------------
       
   419 //  Method: PrintMulti
       
   420 //  Description: Prints text. If line is too long, it will be continued to
       
   421 //  following lines.
       
   422 //  Parameters: const TDesC& aPrint       :in:      Text to print
       
   423 //  Return Values: None
       
   424 // ---------------------------------------------------------------------------
       
   425 //
       
   426 EXPORT_C void CTestMenuView::PrintMulti( const TDesC& aPrint )
       
   427     {
       
   428     // Get current line
       
   429     const TInt KMenuOverHead = 2;
       
   430     TInt y = iConsole->WhereY();
       
   431 
       
   432     const TInt KLineLen =iSize.iWidth - 4;
       
   433     TBuf<KMaxLineLength+1> oneLine;
       
   434 
       
   435     // Loop through the line
       
   436     for (TInt i = 0; i < aPrint.Length(); i++)
       
   437         {
       
   438         oneLine.Append( aPrint[i] );
       
   439 
       
   440         // Print one line
       
   441         if (oneLine.Length() == KLineLen )
       
   442             {
       
   443             oneLine.Append (_L("\n"));
       
   444             iConsole->Printf(oneLine);
       
   445             oneLine=_L("");
       
   446             y++;
       
   447             }
       
   448     
       
   449         // Prevent display scrolling
       
   450         if ( y == iScreenSize + KMenuOverHead )
       
   451             {
       
   452             oneLine=_L("");
       
   453             break;
       
   454             }
       
   455         }
       
   456 
       
   457     // Print last part if required
       
   458     if ( oneLine.Length() != 0 )
       
   459         {
       
   460         oneLine.Append (_L("\n"));
       
   461         iConsole->Printf(oneLine);
       
   462         }
       
   463     }
       
   464     
       
   465 // ---------------------------------------------------------------------------
       
   466 //  Method: AddItem
       
   467 //  Description: Add new item to menu
       
   468 //  Parameters: TDesC& aItem              :in:      descriptor to be added
       
   469 //  Return Values: None
       
   470 // ---------------------------------------------------------------------------
       
   471 //
       
   472 EXPORT_C void CTestMenuView::AddItemL(const TDesC& aItem)
       
   473     {
       
   474     iItems->AppendL(aItem);
       
   475     }
       
   476 
       
   477 // ---------------------------------------------------------------------------
       
   478 //  Method: DeleteItem
       
   479 //  Description: Delete an item from menu
       
   480 //  Parameters: TInt    aIndex  :in:    position of item in Items
       
   481 //  Return Values: None
       
   482 // ---------------------------------------------------------------------------
       
   483 //
       
   484 EXPORT_C void CTestMenuView::DeleteItem( TInt aIndex )
       
   485     {
       
   486     iItems->Delete(aIndex);
       
   487     iItems->Compress();
       
   488     }
       
   489 
       
   490 // ---------------------------------------------------------------------------
       
   491 //  Method: LastKeyPressed
       
   492 //  Description: Returns last key pressed
       
   493 //  Parameters: None
       
   494 //  Return Values: TKeyCode     The last key pressed
       
   495 // ---------------------------------------------------------------------------
       
   496 //
       
   497 EXPORT_C TKeyCode CTestMenuView::LastKeyPressed()
       
   498     {
       
   499     return iKey;
       
   500     }
       
   501 // ---------------------------------------------------------------------------
       
   502 //  Method: ItemsCreated
       
   503 //  Description: Determine whether there are items added to iItems
       
   504 //  Parameters: None
       
   505 //  Return Values: TBool        Whether iItems contain content
       
   506 // ---------------------------------------------------------------------------
       
   507 //
       
   508 EXPORT_C TBool CTestMenuView::ItemsCreated()
       
   509     {
       
   510     return ( iItems->Count() != 0 );
       
   511     }
       
   512 
       
   513 // ---------------------------------------------------------------------------
       
   514 //  Method: CurrentIndex
       
   515 //  Description: Get the position that the cursor is point to in the items
       
   516 //  Parameters: None
       
   517 //  Return Values: TInt     Position of cursor in items array
       
   518 // ---------------------------------------------------------------------------
       
   519 //
       
   520 EXPORT_C TInt CTestMenuView::CurrentIndex()
       
   521     {
       
   522     return iFirst + iPosOnScreen;
       
   523     }
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 //  Method: CurrentPosition
       
   527 //  Description: Get the position that the cursor is point to on the screen
       
   528 //  Parameters: None
       
   529 //  Return Values: TInt     Position of cursor on screen
       
   530 // ---------------------------------------------------------------------------
       
   531 //
       
   532 EXPORT_C TInt CTestMenuView::CurrentPosition()
       
   533     {
       
   534     return iPosOnScreen;
       
   535     }
       
   536 
       
   537 // ---------------------------------------------------------------------------
       
   538 //  Method: Menu
       
   539 //  Description: Get the item
       
   540 //  Parameters: TInt aIndex     :in     index of the entry
       
   541 //  Return Values: TDesC&       entry descriptor
       
   542 // ---------------------------------------------------------------------------
       
   543 //
       
   544 EXPORT_C TPtrC CTestMenuView::ItemL(TInt aIndex)
       
   545     {
       
   546     if(aIndex < 0 || iItemCount <= aIndex)
       
   547         {
       
   548         User::Leave(KErrArgument);
       
   549         }
       
   550     //return iItems->operator[](iFirst + aIndex);
       
   551     return iItems->operator[](aIndex);
       
   552     }
       
   553 
       
   554 // -----------------------------------------------------------------------------
       
   555 // Method: ItemCount
       
   556 // Count the number of items in the current view
       
   557 // -----------------------------------------------------------------------------
       
   558 //
       
   559 EXPORT_C TInt CTestMenuView::ItemCount() const
       
   560     {
       
   561     return iItems->Count();
       
   562     }
       
   563 
       
   564 // -----------------------------------------------------------------------------
       
   565 // Method: ReadString
       
   566 // Reads user input into the start of the descriptor aDes
       
   567 // -----------------------------------------------------------------------------
       
   568 //
       
   569 EXPORT_C void CTestMenuView::ReadString( TDes& aDes )
       
   570     {
       
   571     iConsoleMain->ReadString(aDes);
       
   572     }
       
   573     
       
   574 // -----------------------------------------------------------------------------
       
   575 //  Method: AppendBefore
       
   576 //  Description: Append text before line.
       
   577 //  Parameters: TInt aLine  :in: line number 
       
   578 //              TDes& aLine :in: line text
       
   579 //  Return Values: None
       
   580 // -----------------------------------------------------------------------------
       
   581 //
       
   582 void CTestMenuView::AppendBefore( TInt aLineNum, TDes& aLine )
       
   583     {
       
   584     if( ( aLine.MaxLength() - aLine.Length() ) < 2 )
       
   585         {
       
   586         return;
       
   587         }        
       
   588     
       
   589     // If printing active line, print the marker
       
   590     if ( aLineNum == iPosOnScreen + iFirst )
       
   591         {
       
   592         aLine.Append( _L(" *") );
       
   593         }
       
   594     else
       
   595         {
       
   596         aLine.Append( _L("  ") );
       
   597         }   
       
   598     }