commonuis/CommonUi/src/FindItemController.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    <txtetext.h> // for CPlainText
       
    21 #include    "finditemcontroller.h"
       
    22 #include    <finditemdialog.h>
       
    23 #include    <txtfrmat.h> // for TCharFormatMask
       
    24 #include    <txtrich.h> // for CRichText
       
    25 #include    <centralrepository.h>
       
    26 #include    <CommonUiInternalCRKeys.h>
       
    27 
       
    28 const TInt KFindItemMinDigitsToFind = 5;
       
    29 
       
    30 // ================= MEMBER FUNCTIONS =======================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CFindItemController::TappedItemL
       
    34 //  Moves the selection to tapped item
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 TBool CFindItemController::TappedItemL( const TInt aPosition )
       
    38     {
       
    39     TBool tappedItem = EFalse;
       
    40 
       
    41     if( aPosition < 0 || aPosition >= ItemCount() )
       
    42         {
       
    43         return tappedItem; // Error: invalid position.
       
    44         }
       
    45 
       
    46     // Change selection.
       
    47     TInt steps = aPosition - iEngine->Position();
       
    48     TInt i = steps;
       
    49     do
       
    50         {
       
    51         tappedItem = ETrue;
       
    52         if ( steps < 0 ) // Backward
       
    53             {
       
    54             iEngine->PrevItem( iItem );
       
    55             i++;
       
    56             }
       
    57         else if ( steps > 0 ) // Forward
       
    58             {
       
    59             iEngine->NextItem( iItem );
       
    60             i--;
       
    61             }
       
    62         else
       
    63             {
       
    64             break;
       
    65             }
       
    66         }
       
    67      while ( i != 0 );
       
    68 
       
    69      iDialog->SelectionChangedL( iItem, ETrue );
       
    70 
       
    71      return tappedItem;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CFindItemController::NextItemL()
       
    76 //  Moves the selection to next item
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 TBool CFindItemController::NextItemL()
       
    80     {
       
    81     TBool notLastItem = iEngine->NextItem( iItem );
       
    82     if ( notLastItem )
       
    83         {
       
    84         iDialog->SelectionChangedL( iItem, ETrue );
       
    85         }
       
    86     return notLastItem;
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CFindItemController::PrevItemL
       
    91 //  Moves the selections to previous item
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 TBool CFindItemController::PrevItemL()
       
    95     {
       
    96     TBool notFirstItem = iEngine->PrevItem( iItem );
       
    97     if ( notFirstItem )
       
    98         {
       
    99         iDialog->SelectionChangedL( iItem, EFalse );
       
   100         }
       
   101     return notFirstItem;
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CFindItemController::MoveUpL
       
   106 //  Move into previous row where found item is
       
   107 //  (and select first item in that row)
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CFindItemController::MoveUpL()
       
   111     {
       
   112     if ( FirstItem() )
       
   113         {
       
   114         return;
       
   115         }
       
   116 
       
   117     TInt engPos = iEngine->Position();
       
   118 
       
   119     const CArrayFixFlat<CFindItemEngine::SFoundItem>*
       
   120         itemArray = iEngine->ItemArray();
       
   121 
       
   122     // Check that if at the first line MoveUpL does nothing
       
   123     if ( iDialog->AtSameLine(
       
   124             itemArray->At( iEngine->Position() ).iStartPos,
       
   125             itemArray->At( 0 ).iStartPos ) )
       
   126         {
       
   127         return;
       
   128         }
       
   129 
       
   130     // Go back until the prev item is not at the same line
       
   131     TBool firstItem;
       
   132     do
       
   133         {
       
   134         firstItem = iEngine->PrevItem( iItem );
       
   135         }
       
   136     while ( firstItem && iDialog->AtSameLine(
       
   137                 itemArray->At( iEngine->Position() ).iStartPos,
       
   138                 itemArray->At( engPos ).iStartPos ) );
       
   139 
       
   140     // Go to first item in the row
       
   141     while ( !FirstItem() &&
       
   142         iDialog->AtSameLine(
       
   143             itemArray->At( iEngine->Position() - 1 ).iStartPos,
       
   144             itemArray->At( iEngine->Position() ).iStartPos ) )
       
   145         {
       
   146         iEngine->PrevItem( iItem );
       
   147         }
       
   148 
       
   149     iDialog->SelectionChangedL( iItem, EFalse );
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CFindItemController::MoveDownL
       
   154 //  Move into next row where found item is
       
   155 //  (and select first item in that row)
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CFindItemController::MoveDownL()
       
   159     {
       
   160     // Return if there's no more items.
       
   161     if ( LastItem() )
       
   162         {
       
   163         return;
       
   164         }
       
   165 
       
   166     TInt engPos = iEngine->Position();
       
   167 
       
   168     const CArrayFixFlat<CFindItemEngine::SFoundItem>*
       
   169         itemArray = iEngine->ItemArray();
       
   170 
       
   171     // If the next item is not in the same line -> NextItem()
       
   172     if ( !( iDialog->AtSameLine(
       
   173             itemArray->At( engPos ).iStartPos,
       
   174             itemArray->At( engPos + 1 ).iStartPos ) ) )
       
   175         {
       
   176         NextItemL();
       
   177         return;
       
   178         }
       
   179 
       
   180     // Check that if at the last line MoveDownL does nothing
       
   181     if ( iDialog->AtSameLine(
       
   182             itemArray->At( engPos ).iStartPos,
       
   183             itemArray->At( itemArray->Count() - 1 ).iStartPos ) )
       
   184         {
       
   185         return;
       
   186         }
       
   187 
       
   188 
       
   189     // Go forward until the next item is not at the same line
       
   190     TBool lastItem;
       
   191     do
       
   192         {
       
   193         lastItem = iEngine->NextItem( iItem );
       
   194         }
       
   195     while ( lastItem &&
       
   196         iDialog->AtSameLine(
       
   197             itemArray->At( engPos ).iStartPos +
       
   198                 itemArray->At( engPos ).iLength,
       
   199             itemArray->At( iEngine->Position() ).iStartPos ) );
       
   200 
       
   201     iDialog->SelectionChangedL( iItem, ETrue );
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CFindItemController::MarkFoundItemsL
       
   206 //  Marks found items: Underlines all found strings and updates the selection
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void CFindItemController::MarkFoundItemsL()
       
   210     {
       
   211     const CArrayFixFlat<CFindItemEngine::SFoundItem>*
       
   212         itemArray = iEngine->ItemArray();
       
   213 
       
   214     iEngine->Item( iItem );
       
   215 
       
   216     // Underline all found items
       
   217     TCharFormatMask charFormatMask;
       
   218     TCharFormat charFormat;
       
   219 
       
   220     charFormatMask.SetAttrib( EAttFontUnderline );
       
   221     // interested in underline
       
   222     charFormat.iFontPresentation.iUnderline = EUnderlineOn; // set it on
       
   223     TInt i = 0;
       
   224 
       
   225     while ( i < itemArray->Count() )
       
   226         {
       
   227         iRichText->ApplyCharFormatL(
       
   228             charFormat,
       
   229             charFormatMask,
       
   230             ( *itemArray )[ i ].iStartPos,
       
   231             ( *itemArray )[ i ].iLength );
       
   232         i++;
       
   233         }
       
   234 
       
   235     // Highlight the 'selected' item
       
   236 
       
   237     iDialog->SelectionChangedL( iItem );
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CFindItemController::CFindItemController
       
   242 //  C++ default constructor
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 CFindItemController::CFindItemController()
       
   246     {
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CFindItemController::ConstructL
       
   251 //  EPOC constructor
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CFindItemController::ConstructL(
       
   255     const TDesC& aText,
       
   256     const CFindItemEngine::TFindItemSearchCase aSearchCase )
       
   257     {
       
   258     TInt minDigitsToFind = KFindItemMinDigitsToFind; // default
       
   259     TRAP_IGNORE( minDigitsToFind = GetMinDigitsToFindL(); );
       
   260 #ifdef _DEBUG
       
   261     RDebug::Print(_L("CFindItemController::ConstructL(), minDigitsToFind %d"), minDigitsToFind );
       
   262 #endif
       
   263     iEngine = CFindItemEngine::NewL( aText, aSearchCase, minDigitsToFind );
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // FindItemController::NewL
       
   268 //  Two-phased constructor
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 CFindItemController* CFindItemController::NewL(
       
   272     MFindItemDialogCallBack* aDialog,
       
   273     CRichText* aRichText,
       
   274     const TDesC& aText,
       
   275     const CFindItemEngine::TFindItemSearchCase aSearchCase )
       
   276     {
       
   277     CFindItemController* self = new (ELeave) CFindItemController;
       
   278 
       
   279     self->iDialog = aDialog;
       
   280     self->iRichText = aRichText;
       
   281 
       
   282     CleanupStack::PushL( self );
       
   283     self->ConstructL( aText, aSearchCase );
       
   284     CleanupStack::Pop();
       
   285 
       
   286     return self;
       
   287     }
       
   288 
       
   289 // -----------------------------------------------------------------------------
       
   290 // CFindItemController::~CFindItemController
       
   291 //  Destructor
       
   292 // -----------------------------------------------------------------------------
       
   293 //
       
   294 CFindItemController::~CFindItemController()
       
   295     {
       
   296     delete iEngine;
       
   297     }
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 // CFindItemController::FillScrollBarItemsArrayL
       
   301 //  Fills array with items (TInts) which causes scrollbar to move
       
   302 // -----------------------------------------------------------------------------
       
   303 //
       
   304 void CFindItemController::FillScrollBarItemsArrayL(
       
   305     CArrayFixFlat<TInt>* aArray )
       
   306     {
       
   307 
       
   308     // Doesn't work for long text (>=2000chars) and it's impossible to do
       
   309     // because if the text is long enough only part of it will be formatted
       
   310     // and you cannot check if two string are at the same line in band that
       
   311     // is not formatted. So for long text, every item is counted.
       
   312 
       
   313     const CArrayFixFlat<CFindItemEngine::SFoundItem>*
       
   314         itemArray = iEngine->ItemArray();
       
   315 
       
   316     for ( TInt i=0; i < itemArray->Count(); i++ )
       
   317         {
       
   318         TInt j = i + 1;
       
   319         while ( j < itemArray->Count() &&
       
   320             iDialog->AtSameLine(
       
   321                 itemArray->At( i ).iStartPos,
       
   322                 itemArray->At( j ).iStartPos ) )
       
   323             {
       
   324             j++;
       
   325             }
       
   326         if ( j < itemArray->Count() )
       
   327             {
       
   328             aArray->AppendL(j);
       
   329             }
       
   330         i = j - 1;
       
   331         }
       
   332     }
       
   333 
       
   334 // -----------------------------------------------------------------------------
       
   335 // CFindItemController::IsAtSameLineAsFirstItem
       
   336 //  Check if item is at the same line as first item
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 TBool CFindItemController::IsAtSameLineAsFirstItem()
       
   340     {
       
   341     if ( FirstItem() )
       
   342         {
       
   343         return ETrue;
       
   344         }
       
   345 
       
   346     const CArrayFixFlat<CFindItemEngine::SFoundItem>*
       
   347         itemArray = iEngine->ItemArray();
       
   348 
       
   349     return iDialog->AtSameLine(
       
   350         itemArray->At( Position() ).iStartPos,
       
   351         itemArray->At( 0 ).iStartPos );
       
   352     }
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   355 // CFindItemController::IsAtSameLineAsLastItem
       
   356 //  Check if item is at the same line as last item
       
   357 // -----------------------------------------------------------------------------
       
   358 //
       
   359 TBool CFindItemController::IsAtSameLineAsLastItem()
       
   360     {
       
   361     if ( LastItem() )
       
   362         {
       
   363         return ETrue;
       
   364         }
       
   365 
       
   366     const CArrayFixFlat<CFindItemEngine::SFoundItem>*
       
   367         itemArray = iEngine->ItemArray();
       
   368 
       
   369     return iDialog->AtSameLine(
       
   370         itemArray->At( Position() ).iStartPos,
       
   371         itemArray->At( ItemCount() - 1 ).iStartPos );
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CFindItemController::GetMinDigitsToFindL
       
   376 //  Get minimum number of digits to find.
       
   377 // -----------------------------------------------------------------------------
       
   378 //
       
   379 TInt CFindItemController::GetMinDigitsToFindL()
       
   380      {
       
   381     TInt err = KErrNone;
       
   382     CRepository* variation = CRepository::NewL( KCRUidCommonUi );
       
   383     TInt value;
       
   384     err = variation->Get( KCuiMinDigitsToFind, value );
       
   385     delete variation;
       
   386     variation = NULL;
       
   387     if ( err != KErrNone )
       
   388         {
       
   389         value = KFindItemMinDigitsToFind;
       
   390         }
       
   391     return value;
       
   392     }
       
   393 
       
   394 // -----------------------------------------------------------------------------
       
   395 // CFindItemController::Item()
       
   396 //  Get indexed item from engine side.
       
   397 // -----------------------------------------------------------------------------
       
   398 //
       
   399 TBool CFindItemController::Item(
       
   400     const TInt aIndex,
       
   401     CFindItemEngine::SFoundItem& aItem ) const
       
   402     {
       
   403     TInt validItem = EFalse;
       
   404     if( aIndex >= 0 && aIndex < ItemCount() )
       
   405         {
       
   406         aItem = iEngine->ItemArray()->At( aIndex );
       
   407         validItem = ETrue;
       
   408         }
       
   409     return validItem;
       
   410     }
       
   411 
       
   412 //  End of File