charconvfw/inlinetext/src/InlineTextBase.cpp
changeset 0 1fb32624e06b
child 16 56cd22a7a1cb
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     1 /*
       
     2 * Copyright (c) 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "InlineTextBase.h"
       
    22 
       
    23 // LOCAL FUNCTIONS
       
    24 
       
    25 _LIT( KInlineTextPanicText ,"Inline Text Formatting" );
       
    26 
       
    27 GLDEF_C void Panic( TInlineTextPanic aPanic )
       
    28     {
       
    29     User::Panic( KInlineTextPanicText, aPanic );
       
    30     }
       
    31 
       
    32 // MODULE TEMPLATES
       
    33 
       
    34 // MODULE DATA STRUCTURES
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 ////////////////////////////////////////////////////////////////////////////////
       
    39 //
       
    40 // CInlineTextPositionedText
       
    41 //
       
    42 ////////////////////////////////////////////////////////////////////////////////
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // 2 stage constructor
       
    46 // -----------------------------------------------------------------------------
       
    47 
       
    48 CInlineTextPositionedText* CInlineTextPositionedText::NewL( const TTmDocPos& aPosition, const TDesC& aInlineText )
       
    49     {
       
    50     CInlineTextPositionedText* self = 
       
    51         new (ELeave) CInlineTextPositionedText( aPosition ); // Only partially constructed at this point
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL( aInlineText );
       
    54     CleanupStack::Pop();
       
    55     return self;
       
    56     }
       
    57 
       
    58 CInlineTextPositionedText::~CInlineTextPositionedText()
       
    59     {
       
    60     delete iText;
       
    61     }
       
    62 
       
    63 const TTmDocPos& CInlineTextPositionedText::DocPos() const
       
    64     {
       
    65     return iDocPos;
       
    66     }
       
    67 
       
    68 const TDesC& CInlineTextPositionedText::InlineText() const
       
    69     {
       
    70     return *iText;
       
    71     }
       
    72 
       
    73 CInlineTextPositionedText::CInlineTextPositionedText( const TTmDocPos& aPosition )
       
    74     : iDocPos(aPosition)
       
    75     {
       
    76     }
       
    77 
       
    78 void CInlineTextPositionedText::ConstructL( const TDesC& aInlineText )
       
    79     {
       
    80     iText = aInlineText.AllocL();
       
    81     }
       
    82 ////////////////////////////////////////////////////////////////////////////////
       
    83 //
       
    84 // CInlineTextStore
       
    85 //
       
    86 ////////////////////////////////////////////////////////////////////////////////
       
    87 
       
    88 const TInt KInlineTextStoreGranularity(2);
       
    89 
       
    90 CInlineTextStore* CInlineTextStore::NewL()
       
    91     {
       
    92     return new (ELeave) CInlineTextStore();
       
    93     }
       
    94        
       
    95 CInlineTextStore::~CInlineTextStore()
       
    96     {
       
    97     ResetAndDestroy();
       
    98     }
       
    99 
       
   100 void CInlineTextStore::Clear()
       
   101     {
       
   102     ResetAndDestroy();
       
   103     }
       
   104 
       
   105 void CInlineTextStore::ClearRange( const TTmDocPos& aStart, const TTmDocPos& aEnd )
       
   106     {
       
   107     TInt nToDelete(0);
       
   108     TInt firstToDelete(0);
       
   109     for (TInt index = Count() - 1; index >=0 ; --index)
       
   110 	    {
       
   111         CInlineTextPositionedText* inlineText = At(index);
       
   112         const TTmDocPos& docPos = inlineText->DocPos();
       
   113 		if ( aStart <= docPos && docPos <= aEnd )
       
   114             {
       
   115             delete ( inlineText );
       
   116             firstToDelete = index; // running lowest index to delete
       
   117             nToDelete++;
       
   118             }
       
   119 		}
       
   120     // Perform deletions from array in one go as this is an expensive operation
       
   121 	if ( nToDelete > 0 )
       
   122         Delete( firstToDelete, nToDelete );
       
   123     }
       
   124 
       
   125 void CInlineTextStore::InsertInlineTextL( CInlineTextPositionedText* aInlineText )
       
   126     {
       
   127     TInt insertPosition(0);
       
   128     for (TInt ii = Count()-1; ii >= 0; --ii)
       
   129 		{
       
   130 		if ( At(ii)->DocPos() <= aInlineText->DocPos() )
       
   131             {
       
   132             insertPosition = ii+1;
       
   133             break;
       
   134             }
       
   135 		}
       
   136 
       
   137     InsertL( insertPosition, aInlineText );
       
   138     }
       
   139 
       
   140 const TTmDocPos* CInlineTextStore::NextInlineTextDocPos( const TTmDocPos& aDocPos ) const
       
   141     {
       
   142     const TTmDocPos* retPtr = NULL;
       
   143     TInt index = NextIndexStartingAtDocPos( aDocPos );
       
   144     if ( index >=0 )
       
   145         retPtr = &(At( index )->DocPos());
       
   146     return retPtr;
       
   147     }
       
   148 
       
   149 TInt CInlineTextStore::NextIndexStartingAtDocPos( const TTmDocPos& aDocPos ) const
       
   150     {
       
   151 
       
   152     TInt count = Count();
       
   153     if ( count == 0 )
       
   154         return -1;
       
   155       
       
   156     if ( At(count-1)->DocPos() < aDocPos  ) // even biggest is at lower docpos
       
   157         return -1;
       
   158 
       
   159     TInt index;
       
   160     for (index = 0; index < count ; ++index)
       
   161 	    {
       
   162 		if ( At(index)->DocPos() >= aDocPos  )
       
   163             {
       
   164             break;
       
   165             }
       
   166 		}
       
   167 
       
   168     __ASSERT_DEBUG( index < count , Panic( EInlineTextStoreCorrupted) );
       
   169 
       
   170     return index;
       
   171     }
       
   172 
       
   173 TInt CInlineTextStore::IndexFromDocPos( const TTmDocPos& aDocPos ) const
       
   174     {
       
   175     TInt matchedIndex = -1;
       
   176     TInt indexFound = NextIndexStartingAtDocPos( aDocPos );
       
   177     if (indexFound != -1 &&  At(indexFound)->DocPos() == aDocPos )
       
   178         matchedIndex = indexFound;
       
   179     return matchedIndex;
       
   180     }
       
   181 
       
   182 CInlineTextStore::CInlineTextStore() : CArrayPtrFlat<CInlineTextPositionedText>( KInlineTextStoreGranularity )
       
   183     {}
       
   184  
       
   185 
       
   186 ////////////////////////////////////////////////////////////////////////////////
       
   187 //
       
   188 // CInlineTextSource
       
   189 //
       
   190 ////////////////////////////////////////////////////////////////////////////////
       
   191 
       
   192 CInlineTextSource::CInlineTextSource()
       
   193     {}
       
   194 
       
   195 void CInlineTextSource::ConstructL()
       
   196     {
       
   197     iInlineTextStore = CInlineTextStore::NewL();
       
   198     }
       
   199 
       
   200 CInlineTextSource::~CInlineTextSource()
       
   201     {
       
   202     delete iInlineTextStore;
       
   203     }
       
   204 
       
   205 TInt CInlineTextSource::GetNextInlineTextPosition(
       
   206     const TTmDocPos& aFrom, 
       
   207     TInt aMaxLength, 
       
   208     TTmDocPos& aNext)
       
   209     {
       
   210 
       
   211     TInt errReturn = KErrNotFound; // Default is that there is nothing found
       
   212 
       
   213     const TTmDocPos* nextPos = iInlineTextStore->NextInlineTextDocPos( aFrom );
       
   214     // This returns NULL if nothing found   
       
   215     if ( nextPos )
       
   216         {
       
   217 
       
   218         if ( // pos must be strictly less than aFrom + aMaxLength, but may be 1 further if trailing
       
   219             (nextPos->iPos < (aFrom.iPos + aMaxLength) )
       
   220             || 
       
   221             (!nextPos->iLeadingEdge && (nextPos->iPos <= (aFrom.iPos + aMaxLength)) ) 
       
   222            )
       
   223             {
       
   224             aNext = *nextPos; // Copies the structure
       
   225             errReturn = KErrNone; // Report valid data
       
   226             }
       
   227         }
       
   228         
       
   229     return errReturn;
       
   230     }
       
   231 
       
   232 TPtrC CInlineTextSource::GetInlineText(const TTmDocPos& aPos)
       
   233     {
       
   234     TInt index = iInlineTextStore->IndexFromDocPos( aPos );
       
   235     if ( index >= 0 )
       
   236         return iInlineTextStore->At( index )->InlineText();
       
   237     else
       
   238         {
       
   239         __ASSERT_DEBUG( EFalse, Panic(EInlineTextBadInlineTextFetch) );
       
   240         return KNullDesC();
       
   241         }
       
   242     }
       
   243 
       
   244 TBool CInlineTextSource::HasInlineTextAt(const TTmDocPos& aPos, TPtrC& aPtrFound) const
       
   245     {
       
   246     TBool retVal( EFalse );
       
   247     TInt index = iInlineTextStore->IndexFromDocPos( aPos );
       
   248     if ( index != -1 )
       
   249         {
       
   250         retVal = ETrue;
       
   251         aPtrFound.Set(iInlineTextStore->At( index )->InlineText()) ;
       
   252         }
       
   253     return retVal;
       
   254     }
       
   255 
       
   256 CInlineTextStore* CInlineTextSource::InlineTextStore() const
       
   257     {
       
   258     return iInlineTextStore;
       
   259     }
       
   260 
       
   261 /**
       
   262 * Empty implementation as all "when to format policy" is determined by sub-classes.  However, 
       
   263 * this class implements so that sub-classes only have to implement what they need.
       
   264 */
       
   265 void CInlineTextSource::CheckFormattingL(const TTmDocPos& /*aFrom*/, const TTmDocPos& /*aTo*/ )
       
   266     {}
       
   267 
       
   268 /**
       
   269 * Empty implementation as all upon-edit policy is determined by sub-classes.  However, 
       
   270 * this class implements so that sub-classes only have to implement what they need.
       
   271 */
       
   272 void CInlineTextSource::EditObserver( TInt /*aStart*/, TInt /*aExtent*/ ) 
       
   273     {}
       
   274 //  End of File