uifw/EikStd/coctlsrc/smileyiconrecord.cpp
changeset 0 2f259fa3e83a
child 38 c52421ed5f07
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2008 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:  smiely engine class
       
    15 *
       
    16 */
       
    17 
       
    18 #include "smileymanager.h"
       
    19 #include "smileyiconrecord.h"
       
    20 #include "smileyimagedata.h"
       
    21 
       
    22 // ======== MEMBER FUNCTIONS ========
       
    23 // ---------------------------------------------------------------------------
       
    24 // CSmileyIcon::CSmileyIcon
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 CSmileyIcon::CSmileyIcon( TText aCode ) : iCode( aCode )
       
    28     {
       
    29     }
       
    30 
       
    31 // ---------------------------------------------------------------------------
       
    32 // CSmileyIcon::~CSmileyIcon
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 CSmileyIcon::~CSmileyIcon()
       
    36     {
       
    37     if ( iImage )
       
    38         {
       
    39         iImage->RemoveFromRefArray( this );
       
    40         }
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CSmileyIcon::SetSmileyString
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 void CSmileyIcon::SetSmileyString( const TDesC& aString )
       
    48     { 
       
    49     TInt strLength( aString.Length() );
       
    50     if ( strLength > CSmileyManager::KMaxLength )
       
    51         {
       
    52         strLength = CSmileyManager::KMaxLength;
       
    53         }
       
    54     iStr.Copy( aString.Mid( 0, strLength ) ); 
       
    55     };
       
    56 
       
    57 // ======== MEMBER FUNCTIONS ========
       
    58 // ---------------------------------------------------------------------------
       
    59 // CSmileyIconRecord::CSmileyIconRecord
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CSmileyIconRecord::CSmileyIconRecord()
       
    63     {
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CSmileyIconRecord::~CSmileyIconRecord
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CSmileyIconRecord::~CSmileyIconRecord()
       
    71     {
       
    72     iIconArray.ResetAndDestroy();
       
    73     iIconArray.Close();
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CSmileyIconRecord::InsertIcon
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 void CSmileyIconRecord::InsertIconL( CSmileyIcon* aIcon )
       
    81     {
       
    82     if ( !aIcon )
       
    83         {
       
    84         return;
       
    85         }
       
    86     TInt index( 0 );
       
    87     for ( ; index < iIconArray.Count(); index++ )
       
    88         {
       
    89         if ( iIconArray[index]->DocPos() >= aIcon->DocPos() )
       
    90             {
       
    91             break;
       
    92             }
       
    93         }
       
    94     InsertIconAtL( aIcon, index );
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // CSmileyIconRecord::HandleTextDelete
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 void CSmileyIconRecord::HandleTextDelete( TInt aStart, TInt aLength )
       
   102     {
       
   103     DeleteIconsIn( aStart, aLength );
       
   104     TInt index( FirstIndexAfter( aStart ) );
       
   105     for ( ; index != KErrNotFound && index < iIconArray.Count(); index++ )
       
   106         {
       
   107         TInt newPos( iIconArray[index]->DocPos() - aLength );
       
   108         iIconArray[index]->SetDocPos( newPos );
       
   109         }
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // CSmileyIconRecord::HandleTextDelete
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void CSmileyIconRecord::HandleTextInsert( TInt aStart, TInt aLength )
       
   117     {
       
   118     TInt index( FirstIndexAfter( aStart ) );
       
   119     for ( ; index != KErrNotFound && index < iIconArray.Count(); index++ )
       
   120         {
       
   121         TInt newPos( iIconArray[index]->DocPos() + aLength );
       
   122         iIconArray[index]->SetDocPos( newPos );
       
   123         }
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // CSmileyIconRecord::SmileyRange
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 CSmileyIcon* CSmileyIconRecord::SmileyIconAtPos( TInt aDocPos )
       
   131     {
       
   132     TInt count( iIconArray.Count() );
       
   133     for ( TInt i( 0 ); i < iIconArray.Count(); i++ )
       
   134         {
       
   135         CSmileyIcon* icon( iIconArray[i] );
       
   136         if ( iIconArray[i]->DocPos() <= aDocPos && 
       
   137             iIconArray[i]->DocPos() + iIconArray[i]->SmileyLength() > aDocPos )
       
   138             {
       
   139             return iIconArray[i];
       
   140             }
       
   141         }
       
   142     return NULL;
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // CSmileyIconRecord::SmileyRange
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CSmileyIconRecord::CancelSelection()
       
   150     {
       
   151     for ( TInt i( 0 ); i < iIconArray.Count(); i++ )
       
   152         {
       
   153         iIconArray[i]->EnableHighlight( EFalse );
       
   154         }
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // CSmileyIconRecord::SmileyRange
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CSmileyIconRecord::SetSelection( TInt aStart, TInt aLength )
       
   162     {
       
   163     TInt firstIndex( FirstIndexIn( aStart, aLength ) );
       
   164     TInt lastIndex( LastIndexIn( aStart, aLength, firstIndex ) );
       
   165     for ( TInt i( firstIndex ); i != KErrNotFound && i <= lastIndex; i++ )
       
   166         {
       
   167         iIconArray[i]->EnableHighlight( ETrue );
       
   168         }
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CSmileyIconRecord::HasSmileyIcon
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 TBool CSmileyIconRecord::HasSmileyIcon()
       
   176     {
       
   177     return ( iIconArray.Count() > 0 );
       
   178     }
       
   179 
       
   180 //-------------------Private member function----------------------------------
       
   181 // ---------------------------------------------------------------------------
       
   182 // CSmileyIconRecord::InsertIconAtL
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 void CSmileyIconRecord::InsertIconAtL( CSmileyIcon* aIcon, TInt aIndex )
       
   186     {
       
   187     TInt index( aIndex < 0 ? 0 : aIndex );
       
   188     if ( index >= iIconArray.Count() )
       
   189         {
       
   190         iIconArray.AppendL( aIcon );
       
   191         }
       
   192     else
       
   193         {
       
   194         iIconArray.InsertL( aIcon, index );
       
   195         }
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 // CSmileyIconRecord::DeleteIconsIn
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CSmileyIconRecord::DeleteIconsIn( TInt aStart, TInt aLength )
       
   203     {
       
   204     TInt startIndex( FirstIndexIn( aStart, aLength ) );
       
   205     TInt endIndex( LastIndexIn( aStart, aLength, startIndex ) );
       
   206     TInt count( endIndex - startIndex );
       
   207     for ( TInt i( 0 ); startIndex != KErrNotFound && i <= count; i++ )
       
   208         {
       
   209         CSmileyIcon* icon( iIconArray[startIndex] );
       
   210         iIconArray.Remove( startIndex );
       
   211         delete icon;
       
   212         }
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // CSmileyIconRecord::InsertIconAtL
       
   217 // ---------------------------------------------------------------------------
       
   218 //
       
   219 TInt CSmileyIconRecord::FirstIndexAfter( TInt aDocPos, TInt aSearchStart )
       
   220     {
       
   221     TInt i = ( aSearchStart < 0 || aSearchStart >= iIconArray.Count() ? 0 : 
       
   222         aSearchStart );
       
   223     for ( ; i < iIconArray.Count(); i++ )
       
   224         {
       
   225         if ( iIconArray[i]->DocPos() >= aDocPos )
       
   226             {
       
   227             return i;            
       
   228             }
       
   229         }
       
   230     return KErrNotFound;
       
   231     }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // CSmileyIconRecord::InsertIconAtL
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 TInt CSmileyIconRecord::FirstIndexIn( TInt aStart, TInt aLength )
       
   238     {
       
   239     for ( TInt i( 0 ); i < iIconArray.Count(); i++ )
       
   240         {
       
   241         if ( iIconArray[i]->DocPos() < aStart + aLength && 
       
   242             iIconArray[i]->EndPos() > aStart )
       
   243             {
       
   244             return i;
       
   245             }
       
   246         }
       
   247     return KErrNotFound;
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // CSmileyIconRecord::InsertIconAtL
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 TInt CSmileyIconRecord::LastIndexIn( TInt aStart, TInt aLength, 
       
   255     TInt aFirstIndex )
       
   256     {
       
   257     TInt index( FirstIndexAfter( aStart + aLength, aFirstIndex ) );
       
   258     if ( index == KErrNotFound )
       
   259         {
       
   260         return iIconArray.Count() > 0 ? iIconArray.Count() - 1 : index;
       
   261         }
       
   262     if ( index > 0 )
       
   263         {
       
   264         return index - 1;
       
   265         }
       
   266     return aFirstIndex;
       
   267     }
       
   268 
       
   269 
       
   270