uifw/EikStd/coctlsrc/AknPhoneNumberInlineTextSource.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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 "AknPhoneNumberInlineTextSource.h"
       
    22 #include <NumberGrouping.h>
       
    23 #include <eikedwin.h>
       
    24 
       
    25 
       
    26 // MODULE TEMPLATES
       
    27 
       
    28 // MODULE DATA STRUCTURES
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 ////////////////////////////////////////////////////////////////////////////////
       
    33 //
       
    34 // CAknPhoneNumberInlineTextSource
       
    35 //
       
    36 ////////////////////////////////////////////////////////////////////////////////
       
    37 
       
    38 CAknPhoneNumberInlineTextSource::CAknPhoneNumberInlineTextSource( const CPlainText& aText )
       
    39     : CAknInlineTextSource(), iText(aText)
       
    40     {}
       
    41 
       
    42 CAknPhoneNumberInlineTextSource::~CAknPhoneNumberInlineTextSource()
       
    43     {
       
    44     delete iPhoneNumberGrouping;
       
    45     }
       
    46 
       
    47 const CPlainText& CAknPhoneNumberInlineTextSource::Text() const
       
    48     {
       
    49     return iText;
       
    50     }
       
    51 
       
    52 CPNGNumberGrouping* CAknPhoneNumberInlineTextSource::PhoneNumberGrouping() const
       
    53     {
       
    54     return iPhoneNumberGrouping;
       
    55     }
       
    56 
       
    57 void CAknPhoneNumberInlineTextSource::ConstructNumberGroupingEngineL( TInt aMaxFormattedLength )
       
    58     {
       
    59     // This ordering in order to guarantee that once we have a valid iPhoneNumberGrouping,
       
    60     // we never lose it.
       
    61     CPNGNumberGrouping* phoneNumberGrouping = CPNGNumberGrouping::NewL( aMaxFormattedLength ,EFalse );
       
    62     delete iPhoneNumberGrouping; // Can be deleted since the NewL did not leave
       
    63     iPhoneNumberGrouping = phoneNumberGrouping;
       
    64     }
       
    65 
       
    66 void CAknPhoneNumberInlineTextSource::FormatPhoneNumberL( const TDesC& aPhoneNumberBuf, TInt aOffset )
       
    67     {
       
    68     TInt inputCount = aPhoneNumberBuf.Length();
       
    69 
       
    70     // Check if the number grouping engine needs to be re-created:
       
    71     if ( inputCount > iPhoneNumberGrouping->MaxDisplayLength() )
       
    72         {
       
    73         ConstructNumberGroupingEngineL( 2 * inputCount ); // Arbitrary but reasonable increase factor of 2
       
    74         }
       
    75 
       
    76     iPhoneNumberGrouping->Set( aPhoneNumberBuf );
       
    77     (void)iPhoneNumberGrouping->FormattedNumber();
       
    78     if ( iPhoneNumberGrouping->IsChangedByGrouping() )
       
    79         {
       
    80         TInt count = iPhoneNumberGrouping->Length();
       
    81         TInt spaceCount = 0; // Accumulates spaces
       
    82         for ( TInt index = 0; index < count; index++ )
       
    83             {
       
    84             if ( iPhoneNumberGrouping->IsCharacterInsertedByNumberGrouping( index ) )
       
    85                 {
       
    86                 spaceCount++;
       
    87                 TTmDocPos docPos;
       
    88                 docPos.iLeadingEdge = EFalse; // Trailing edge;
       
    89                 if ( index == 0 ) // Use leading edge for the firt position
       
    90                     docPos.iLeadingEdge = ETrue;
       
    91                 docPos.iPos = aOffset + index - spaceCount + 1; // +1 because it is trailing edge 
       
    92                 TPtrC ptr( iPhoneNumberGrouping->FormattedNumber().Ptr()+index, 1 ); // handling one character at a time
       
    93                 CAknPositionedInlineText* inlineText = 
       
    94                     CAknPositionedInlineText::NewL( docPos, ptr );
       
    95                 CleanupStack::PushL( inlineText );
       
    96                 InlineTextStore()->InsertInlineTextL( inlineText ); // Ownership is passed in
       
    97                 CleanupStack::Pop(); // inlineText
       
    98                 }
       
    99             }
       
   100         }   
       
   101     }
       
   102 
       
   103 ////////////////////////////////////////////////////////////////////////////////
       
   104 //
       
   105 // CAknPlainPhoneNumberInlineTextSource
       
   106 //
       
   107 ////////////////////////////////////////////////////////////////////////////////
       
   108 
       
   109 const TInt KInitialPlainPhoneNumberGroupingSize(10); // Good enough for 10 digit 123 456 7890
       
   110 
       
   111 
       
   112 EXPORT_C CAknPlainPhoneNumberInlineTextSource* CAknPlainPhoneNumberInlineTextSource::NewL( 
       
   113     const CPlainText& aText )
       
   114     {
       
   115     CAknPlainPhoneNumberInlineTextSource* self = 
       
   116         new (ELeave) CAknPlainPhoneNumberInlineTextSource( aText );
       
   117     CleanupStack::PushL( self );
       
   118     self->ConstructL();
       
   119     CleanupStack::Pop();
       
   120     return self;
       
   121     }
       
   122 
       
   123 CAknPlainPhoneNumberInlineTextSource::CAknPlainPhoneNumberInlineTextSource( const CPlainText& aText )
       
   124     : CAknPhoneNumberInlineTextSource(aText)
       
   125     {}
       
   126 
       
   127 CAknPlainPhoneNumberInlineTextSource::~CAknPlainPhoneNumberInlineTextSource()
       
   128     {
       
   129     }
       
   130 
       
   131 void CAknPlainPhoneNumberInlineTextSource::ConstructL()
       
   132     {
       
   133     ConstructNumberGroupingEngineL( KInitialPlainPhoneNumberGroupingSize );
       
   134     CAknInlineTextSource::ConstructL();
       
   135     }
       
   136 
       
   137 TPtrC CAknPlainPhoneNumberInlineTextSource::PhoneNumberFromEditor(TInt& aStartPos) const
       
   138     {
       
   139     // Using DocumentLength() avoids including the 0x2029 (end of paragraph)
       
   140     // character at the end
       
   141     TInt length = Text().DocumentLength();
       
   142     aStartPos = 0;
       
   143     
       
   144     for(TInt i = 0; i < length; i++ )
       
   145         {
       
   146         TBuf<1> buf(Text().Read(i, 1 ));
       
   147         TLex lex(buf);
       
   148         TInt num = 0;
       
   149         //TBuf to TInt 
       
   150         TInt res = lex.Val(num);
       
   151 
       
   152         if(KErrNone != res)
       
   153             {
       
   154             aStartPos = i + 1;
       
   155             }
       
   156         }
       
   157     
       
   158     return Text().Read(aStartPos, length - aStartPos );
       
   159     }
       
   160 
       
   161 /* 
       
   162  * This dictates the update policy. Formatting is triggered every call to CheckFormatting
       
   163  *
       
   164  */
       
   165 void CAknPlainPhoneNumberInlineTextSource::CheckFormattingL(const TTmDocPos& /*aFrom*/, const TTmDocPos& /*aTo*/ )
       
   166     {
       
   167     DoFormatL();
       
   168     }
       
   169 
       
   170 void CAknPlainPhoneNumberInlineTextSource::DoFormatL()
       
   171     {
       
   172 
       
   173     if ( ( Text().DocumentLength() != StoredNumberLength() ) || 
       
   174         ( CompareTextWithUnformattedNumber() != 0 ) )
       
   175         {
       
   176         // Clear whole buffer
       
   177         InlineTextStore()->Clear();
       
   178         TInt sartPos = 0;
       
   179         TPtrC phoneNumberPtr = PhoneNumberFromEditor(sartPos);
       
   180         
       
   181         FormatPhoneNumberL( phoneNumberPtr, sartPos);
       
   182         }
       
   183     }
       
   184 
       
   185 TInt CAknPlainPhoneNumberInlineTextSource::CompareTextWithUnformattedNumber() const
       
   186     {
       
   187     TInt sartPos = 0;
       
   188     return PhoneNumberFromEditor(sartPos).Compare( PhoneNumberGrouping()->UnFormattedNumber() );
       
   189     }
       
   190 
       
   191 TInt CAknPlainPhoneNumberInlineTextSource::StoredNumberLength() const
       
   192     {
       
   193     return PhoneNumberGrouping()->UnFormattedNumber().Length();
       
   194     }
       
   195 
       
   196 //  End of File