textinput/peninputcommonctrls/src/peninputdropdownlist/peninputcandidate.cpp
changeset 40 2cb9bae34d17
parent 31 f1bdd6b078d1
child 49 37f5d84451bd
equal deleted inserted replaced
31:f1bdd6b078d1 40:2cb9bae34d17
     1 /*
       
     2 * Copyright (c) 2002-2005 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:   candidate stands for every cell in drop down list
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <gdi.h>
       
    20 #include <e32base.h>
       
    21 #include <w32std.h>
       
    22 #include <biditext.h> 
       
    23 
       
    24 #include "peninputcandidate.h"
       
    25 #include "peninputdropdownlist.h"
       
    26 
       
    27 _LIT(KEllipsis, "\x2026");
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // Implementation of Class CCandidate 
       
    32 
       
    33 CCandidate* CCandidate::NewL(const TDesC& aString,
       
    34                              MFepCtrlDropdownListContext* aOwner)
       
    35     {
       
    36     CCandidate* self = new (ELeave) CCandidate(aString, aOwner);  // Q: is here will call ctor of base class?
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL(); 
       
    39     CleanupStack::Pop();
       
    40     return self;
       
    41     }
       
    42     
       
    43 void CCandidate::ConstructL()
       
    44     {
       
    45     iDisplayText = iCandidate.AllocL();
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CCandidate::CCandidate
       
    50 // construct function
       
    51 // (other items were commented in a header).
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CCandidate::CCandidate(const TDesC& aString, MFepCtrlDropdownListContext* aOwner)
       
    55 : iCandidate(aString),iBaselineOffset(0), iOwner(aOwner)
       
    56     {
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CCandidate::~CCandidate
       
    61 // destructor function
       
    62 // (other items were commented in a header).
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CCandidate::~CCandidate() 
       
    66     {
       
    67     delete iDisplayText;
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CCandidate::GetCandidate
       
    72 // get candidate string
       
    73 // (other items were commented in a header).
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 const TDesC& CCandidate::GetCandidate()
       
    77     {
       
    78     return iCandidate;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CCandidate::GetRect
       
    83 // get rect of the candidate
       
    84 // (other items were commented in a header).
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 const TRect& CCandidate::GetRect() const
       
    88     {
       
    89     return iRect;
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CCandidate::Move
       
    94 // move rect of the candidate
       
    95 // (other items were commented in a header).
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void CCandidate::Move(const TPoint& aOffsetPoint)
       
    99     {
       
   100     iRect.Move(aOffsetPoint);
       
   101     }
       
   102     
       
   103 // -----------------------------------------------------------------------------
       
   104 // CCandidate::SetPosition
       
   105 // set position for the candidate
       
   106 // (other items were commented in a header).
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 TInt CCandidate::SetPositionL(const TPoint& aLeftTopPosition, 
       
   110                               TInt aWidthForOneUnit, 
       
   111                               TInt aHeight, 
       
   112                               const CFont* aTextFont,
       
   113                               TInt aCellHorizontalMargin)
       
   114     {
       
   115     iTextFont = aTextFont;
       
   116     
       
   117     // Calculate base line offset
       
   118     iBaselineOffset = aHeight/2 + aTextFont->AscentInPixels()/2 ; 
       
   119     
       
   120     // Calculate the cell for the candidate
       
   121     //const TInt cellCount = CalculateCells(aWidthForOneUnit, aTextFont);
       
   122     const TInt cellCount = CalculateDisplayTextL(aWidthForOneUnit, aTextFont, iOwner->GetColNum());
       
   123 
       
   124     // modified by txin
       
   125     // original:
       
   126     /*
       
   127     iRect.iBr = aLeftTopPosition;
       
   128     
       
   129     iRect.SetRect(aLeftTopPosition, 
       
   130                   TSize(cellCount * aWidthForOneUnit + (cellCount - 1) * aCellHorizontalMargin,
       
   131                         aHeight));
       
   132     */
       
   133     // if owner's horizontal direction is RToL, then aLeftTopPosition actually mean RightTop pos
       
   134     TInt candHDir = iOwner->CandHDirection();
       
   135     TSize candSize = TSize(cellCount * aWidthForOneUnit + (cellCount - 1) * aCellHorizontalMargin,
       
   136                            aHeight);
       
   137     (candHDir == CFepCtrlDropdownList::ECandFromLToR) ?
       
   138         iRect.SetRect(aLeftTopPosition, candSize) : 
       
   139         iRect.SetRect(TPoint(aLeftTopPosition.iX - candSize.iWidth, aLeftTopPosition.iY), candSize);
       
   140     // end modify
       
   141 
       
   142     return cellCount;
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CCandidate::DrawText
       
   147 // draw text of the candidate
       
   148 // (other items were commented in a header).
       
   149 // -----------------------------------------------------------------------------
       
   150 //
       
   151 void CCandidate::DrawText(CFbsBitGc& aGc, const CGraphicsContext::TTextAlign aAlignment, 
       
   152                           const TInt aTextMargin, TRgb aTextColor, 
       
   153                           TRgb aSpecialTextColor, const TDesC& aFixedText)
       
   154     {
       
   155     
       
   156     aGc.SetBrushStyle(CGraphicsContext::ENullBrush);
       
   157     if ( aFixedText.Length() == 0 )    
       
   158         {
       
   159         // If fixed text has no any data
       
   160         aGc.SetPenColor(aTextColor);
       
   161         if(TBidiText::TextDirectionality( *iDisplayText ) == TBidiText:: ERightToLeft)
       
   162             {
       
   163             // If find the fixed text in the candidate and the index is equal to 0
       
   164             TInt posX = 0;
       
   165             if(!iTextFont)
       
   166                 {
       
   167                 iTextFont = iOwner->GetFont();
       
   168                 }
       
   169             if ( aAlignment == CGraphicsContext::ECenter )
       
   170                 {
       
   171                 posX = iRect.iTl.iX + 
       
   172                        (iRect.Width() - iTextFont->TextWidthInPixels(*iDisplayText)) / 2;
       
   173                 }
       
   174             else  if ( aAlignment == CGraphicsContext::ELeft )
       
   175                 {
       
   176                 posX = iRect.iTl.iX + aTextMargin;
       
   177                 }
       
   178             else if ( aAlignment == CGraphicsContext::ERight )
       
   179                 {
       
   180                 posX = iRect.iTl.iX + iRect.Width() - 
       
   181                        iTextFont->TextWidthInPixels(*iDisplayText) - aTextMargin;
       
   182                 }
       
   183             
       
   184             CGraphicsContext::TDrawTextExtendedParam param;
       
   185             param.iParRightToLeft = ETrue;
       
   186             aGc.DrawTextExtended(*iDisplayText, TPoint(posX,iRect.iTl.iY + iBaselineOffset), param);            
       
   187             }
       
   188         else
       
   189             {
       
   190             aGc.DrawText( *iDisplayText, iRect, iBaselineOffset, aAlignment);
       
   191             }
       
   192         }
       
   193     else
       
   194         {
       
   195         if ( iCandidate.Find(aFixedText) == 0 )
       
   196             {
       
   197             // If find the fixed text in the candidate and the index is equal to 0
       
   198             TInt posX = 0;
       
   199             if(!iTextFont)
       
   200                 {
       
   201                 iTextFont = iOwner->GetFont();
       
   202                 }            
       
   203             if ( aAlignment == CGraphicsContext::ECenter )
       
   204                 {
       
   205                 posX = iRect.iTl.iX + 
       
   206                        (iRect.Width() - iTextFont->TextWidthInPixels(iCandidate)) / 2;
       
   207                 }
       
   208             else  if ( aAlignment == CGraphicsContext::ELeft )
       
   209                 {
       
   210                 posX = iRect.iTl.iX + aTextMargin;
       
   211                 }
       
   212             else if ( aAlignment == CGraphicsContext::ERight )
       
   213                 {
       
   214                 posX = iRect.iTl.iX + iRect.Width() - 
       
   215                        iTextFont->TextWidthInPixels(iCandidate) - aTextMargin;
       
   216                 }
       
   217             
       
   218             
       
   219             // Draw Fixed text
       
   220             aGc.SetPenColor(aTextColor);
       
   221             aGc.DrawText(aFixedText, TPoint(posX,iRect.iTl.iY + iBaselineOffset));
       
   222             
       
   223             // Snatch the special text
       
   224             TPtrC buf = iCandidate.Mid(aFixedText.Length());      
       
   225             
       
   226             // Set X position for the special text
       
   227             posX = posX + iTextFont->TextWidthInPixels(aFixedText);
       
   228             
       
   229             // Draw special text
       
   230             aGc.SetPenColor(aSpecialTextColor);
       
   231             aGc.DrawText(buf, TPoint(posX, iRect.iTl.iY + iBaselineOffset));
       
   232             }
       
   233         else
       
   234             {
       
   235             aGc.SetPenColor(aTextColor);
       
   236             if(TBidiText::TextDirectionality( iCandidate ) == TBidiText:: ERightToLeft)
       
   237                 {
       
   238                 // If find the fixed text in the candidate and the index is equal to 0
       
   239                 TInt posX = 0;
       
   240                 if(!iTextFont)
       
   241                     {
       
   242                     iTextFont = iOwner->GetFont();
       
   243                     }                
       
   244                 if ( aAlignment == CGraphicsContext::ECenter )
       
   245                     {
       
   246                     posX = iRect.iTl.iX + 
       
   247                            (iRect.Width() - iTextFont->TextWidthInPixels(iCandidate)) / 2;
       
   248                     }
       
   249                 else  if ( aAlignment == CGraphicsContext::ELeft )
       
   250                     {
       
   251                     posX = iRect.iTl.iX + aTextMargin;
       
   252                     }
       
   253                 else if ( aAlignment == CGraphicsContext::ERight )
       
   254                     {
       
   255                     posX = iRect.iTl.iX + iRect.Width() - 
       
   256                            iTextFont->TextWidthInPixels(iCandidate) - aTextMargin;
       
   257                     }
       
   258                 
       
   259                 CGraphicsContext::TDrawTextExtendedParam param;
       
   260                 param.iParRightToLeft = ETrue;
       
   261                 aGc.DrawTextExtended(iCandidate, TPoint(posX,iRect.iTl.iY + iBaselineOffset), param);            
       
   262                 }
       
   263             else
       
   264                 {
       
   265                 aGc.DrawText( iCandidate, iRect, iBaselineOffset, CGraphicsContext::ECenter);
       
   266                 }
       
   267             }
       
   268         }
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CCandidate::CalculateCells
       
   273 // calculate the cell for the candidate
       
   274 // (other items were commented in a header).
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 TInt CCandidate::CalculateCells(const TInt aWidthForOneUnit, 
       
   278                                 const CFont* aTextFont,
       
   279                                 const TDesC& aText)
       
   280     {
       
   281     const TInt width = aTextFont->TextWidthInPixels(aText) + 2 * iOwner->GetTextMargin();
       
   282 
       
   283     if (width <= aWidthForOneUnit)
       
   284         {
       
   285         return 1;
       
   286         }
       
   287     
       
   288     return (width + aWidthForOneUnit + 
       
   289      iOwner->GetCellHorizontalMargin() - 1)/(aWidthForOneUnit + iOwner->GetCellHorizontalMargin());
       
   290     }
       
   291     
       
   292 TInt CCandidate::CalculateDisplayTextL(const TInt aWidthForOneUnit, 
       
   293                                        const CFont* aTextFont,
       
   294                                        TInt aColNum)
       
   295     {
       
   296     TInt cellcnt = CalculateCells(aWidthForOneUnit, aTextFont, *iDisplayText);
       
   297     
       
   298     if (cellcnt <= aColNum)
       
   299         {
       
   300         return cellcnt;
       
   301         }
       
   302 
       
   303     TPtr ptr = iDisplayText->Des();
       
   304 
       
   305     if (iOwner->CandTruncateType() == CFepCtrlDropdownList::ECandTruncateFromBeginning)
       
   306         {
       
   307         while (cellcnt > aColNum && ptr.Length() > 0)
       
   308             {
       
   309             ptr.Delete(0, 1);
       
   310             cellcnt = CalculateCells(aWidthForOneUnit, aTextFont, ptr);
       
   311             }
       
   312         }
       
   313     else if (iOwner->CandTruncateType() == CFepCtrlDropdownList::ECandTruncateAsEllipsis)
       
   314         {
       
   315         while (cellcnt > aColNum && ptr.Length() > 1)
       
   316             {
       
   317             ptr.Delete(ptr.Length()-2, 2);
       
   318             ptr.Append(KEllipsis);
       
   319             cellcnt = CalculateCells(aWidthForOneUnit, aTextFont, ptr);
       
   320             }
       
   321         }
       
   322     else if (iOwner->CandTruncateType() == CFepCtrlDropdownList::ECandTruncateFromEnd)
       
   323         {
       
   324         while (cellcnt > aColNum && ptr.Length() > 0)
       
   325             {
       
   326             ptr.Delete(ptr.Length()-1, 1);
       
   327             cellcnt = CalculateCells(aWidthForOneUnit, aTextFont, ptr);
       
   328             }
       
   329         }
       
   330         
       
   331     return cellcnt;    
       
   332     }
       
   333 
       
   334 // End Of File