textinput/peninputarc/src/peninputlayoutcontrol/peninputlayouteditareabase.cpp
changeset 0 eb1f2e154e89
child 9 e6a39382bb9c
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Implementation for CFepLayoutEditAreaBase
       
    15 *
       
    16 */
       
    17 
       
    18 #include <peninputcmd.h>
       
    19 #include <AknBidiTextUtils.h>
       
    20 #include <AknsDrawUtils.h>
       
    21 #include <avkon.hrh>
       
    22 
       
    23 #include "peninputlayouteditareabase.h"
       
    24 #include "peninputlayoutinsertionpoint.h"
       
    25 #include "peninputlayout.h"
       
    26 
       
    27 
       
    28 // CONSTANTS
       
    29 const TInt KGapLeftOfEachLine = 2;//1;
       
    30 const TInt KSelectionRectMarginY = 2;
       
    31 const TInt KDefaultHeight = 10;
       
    32 const TInt KCursorCorrectionX = -1 * ( KCursorWidth + KCursorPosCorrectionX );
       
    33 const TInt KDefaultRightMargin = 3 * KCursorWidth;
       
    34 const TInt KAddBufLen = 100;
       
    35 const TInt KSecretUpdateTimer = 1000000; // 1s
       
    36 const TInt KSecretInstantShowTimer = 100000; // 100ms
       
    37 const TInt KParagraphSeperator = 0x2029;
       
    38 
       
    39 
       
    40 // ============================ MEMBER FUNCTIONS ===============================
       
    41 
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CFepLayoutEditAreaBase::CFepLayoutEditAreaBase
       
    45 // C++ default constructor can NOT contain any code, that
       
    46 // might leave.
       
    47 // ---------------------------------------------------------------------------
       
    48 EXPORT_C CFepLayoutEditAreaBase::CFepLayoutEditAreaBase(TRect aRect,
       
    49                                       CFepUiLayout* aUiLayout,TInt aControlId):
       
    50     CFepUiBaseCtrl(aRect,aUiLayout,aControlId),
       
    51     iCorrectedRect(aRect),iFontOwnership(EFalse)
       
    52     {    
       
    53     SetControlType(ECtrlEditAreaBase);
       
    54     iCursorVisible = ETrue;
       
    55     iCursorSelVisible = ETrue;
       
    56     
       
    57 #ifdef RD_TACTILE_FEEDBACK     
       
    58     //register the area for tactile feedback
       
    59     if(aUiLayout)
       
    60     	{
       
    61     	//Advanced Tactile feedback REQ417-47932
       
    62     	if(IsKindOfControl(ECtrlTactileFeedback))
       
    63     		{
       
    64     		SetTactileFeedbackType(ETouchFeedbackSensitiveInput);
       
    65     		aUiLayout->RegisterFeedbackArea(reinterpret_cast<TInt>(this),aRect,ETouchFeedbackSensitiveInput);
       
    66     		}
       
    67     	}
       
    68             
       
    69 	
       
    70 #endif //RD_TACTILE_FEEDBACK   
       
    71     }
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CFepLayoutEditAreaBase::ConstructL
       
    75 // Symbian 2nd phase constructor can leave.
       
    76 // ---------------------------------------------------------------------------
       
    77 EXPORT_C void CFepLayoutEditAreaBase::BaseConstructL()
       
    78     {
       
    79     CFepUiBaseCtrl::BaseConstructL();
       
    80     
       
    81     // Initialize text color
       
    82     iTextColor = KRgbBlack;
       
    83     
       
    84     // Create the caret
       
    85     iInsertionPoint = CInsertionPoint::NewL(this,BitmapDevice(),BitGc(),KDefaultHeight);
       
    86     iSecretTextTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CFepLayoutEditAreaBase::~CFepLayoutEditAreaBase
       
    91 // Destructor
       
    92 // ---------------------------------------------------------------------------
       
    93 
       
    94 EXPORT_C CFepLayoutEditAreaBase::~CFepLayoutEditAreaBase()
       
    95     {
       
    96     delete iInsertionPoint;
       
    97     delete iBuffer;    
       
    98     delete iVisualBuffer;
       
    99 
       
   100     iSecretTextTimer->Cancel();
       
   101     delete iSecretTextTimer;
       
   102         
       
   103     if(iFontOwnership && iFont)
       
   104         {
       
   105         BitmapDevice()->ReleaseFont(iFont);
       
   106         }    
       
   107     
       
   108     iOverlappedCtrlList.Close();
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CFepLayoutEditAreaBase::SetRect
       
   113 // Sets the rectangle
       
   114 // (other items were commented in a header).
       
   115 // ---------------------------------------------------------------------------    
       
   116 //
       
   117 EXPORT_C void CFepLayoutEditAreaBase::SetRect(TRect aRect)
       
   118     {
       
   119     CFepUiBaseCtrl::SetRect(aRect);
       
   120     iCorrectedRect = aRect;
       
   121     iCorrectedRect.iBr.iX += KCursorCorrectionX;
       
   122 	
       
   123 	// resize the background bitmap
       
   124     if( BackgroundBmp() && BackgroundBmp()->SizeInPixels() != Rect().Size() )
       
   125 	    {
       
   126 	    AknIconUtils::SetSize( BackgroundBmp(), 
       
   127 	    					   Rect().Size(), 
       
   128 	    					   EAspectRatioNotPreserved );	    
       
   129 	    }
       
   130 	if( BkMaskBmp() && BkMaskBmp()->SizeInPixels() != Rect().Size() )
       
   131 		{
       
   132 	    AknIconUtils::SetSize( BkMaskBmp(), 
       
   133 	    					   Rect().Size(), 
       
   134 	    					   EAspectRatioNotPreserved );			
       
   135 		}
       
   136     if (iBuffer && iFont)
       
   137         {
       
   138         TRAP_IGNORE(CalculateDisplayTextL());
       
   139         iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow());
       
   140         Draw();
       
   141         UpdateArea(Rect(),EFalse);
       
   142         }
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // Handle pointer down event
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 EXPORT_C CFepUiBaseCtrl* CFepLayoutEditAreaBase::HandlePointerDownEventL(const TPoint& aPt)
       
   150     {
       
   151     if (iDimmed || !(iCursorVisible||iCursorSelVisible))
       
   152         {
       
   153         return NULL;
       
   154         }
       
   155         
       
   156     CFepUiBaseCtrl::HandlePointerDownEventL(aPt);
       
   157     
       
   158     // get the index of the character in render buffer where down event occurred
       
   159     TInt caretPos = PositionInVisualText(aPt.iX);
       
   160 
       
   161     //cancel any selection
       
   162     if(iDisplayTextCurSel.Length() && (iCursorVisible||iCursorSelVisible))
       
   163         {
       
   164         TRect rect = DrawSelection();
       
   165         UpdateArea(rect,EFalse);
       
   166         iDisplayTextCurSel.SetSelection(0,0);   
       
   167         iSelectedCompositionText.SetSelection(0,0);             
       
   168         }
       
   169     
       
   170     // set the new cursor position
       
   171     TInt newPosition = PositionInDisplayText(aPt.iX);
       
   172     SetPositionOfInsertionPointInBuffer(newPosition);   
       
   173     
       
   174     iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow()); 
       
   175     SetFocus();
       
   176     
       
   177     iScrolling = EFalse;
       
   178     
       
   179     CFepUiBaseCtrl* temp = CapturePointer();
       
   180     if (temp != this)
       
   181         {
       
   182         iPreCaptureCtrl = temp;
       
   183         }
       
   184 
       
   185     return this;
       
   186             
       
   187     }
       
   188 
       
   189 TInt CFepLayoutEditAreaBase::CursorDistance(TInt aX) const
       
   190     {
       
   191     TInt cursorDistance;
       
   192     if ( iTextDirection == TBidiText::ELeftToRight )
       
   193         {
       
   194         cursorDistance = aX - iCorrectedRect.iTl.iX - KGapLeftOfEachLine;
       
   195         }
       
   196     else
       
   197         {
       
   198         cursorDistance = iCorrectedRect.iBr.iX - aX - KGapLeftOfEachLine;
       
   199         }
       
   200     
       
   201     TInt maxWidth = iCorrectedRect.Width() - KGapLeftOfEachLine;
       
   202     if(cursorDistance < 0)
       
   203         {
       
   204         cursorDistance = 0;
       
   205         }
       
   206     else 
       
   207         {
       
   208         cursorDistance = cursorDistance > maxWidth ? maxWidth : cursorDistance;
       
   209         }
       
   210     return cursorDistance;
       
   211     }
       
   212 
       
   213 TBidiText::TDirectionality CFepLayoutEditAreaBase::CharDirection( TChar aChar ) const
       
   214     {
       
   215     TBuf<1> buf;
       
   216     buf.Zero();
       
   217     buf.Append( aChar );
       
   218     
       
   219     return TBidiText::TextDirectionality( buf );
       
   220     }
       
   221 
       
   222 void CFepLayoutEditAreaBase::AdjustPostionInVisualText( const TInt /*cursorDistance*/, TInt& /*retVal*/ )
       
   223     {
       
   224     switch ( iTextDirection )
       
   225         {
       
   226         case TBidiText::ERightToLeft:
       
   227             break;
       
   228         case TBidiText::ELeftToRight:
       
   229         default:
       
   230             break;
       
   231         }
       
   232     
       
   233     }
       
   234     
       
   235 TInt CFepLayoutEditAreaBase::PositionInVisualText(TInt aX)
       
   236     {
       
   237     TInt retVal;
       
   238         
       
   239     if(!iBuffer || !iFont)
       
   240         {
       
   241         // Edit area was empty, position is zero
       
   242         retVal = 0;
       
   243         return retVal;
       
   244         }
       
   245         
       
   246     // get the distance of the cursor from the beginning of iCorrectedRect
       
   247     TInt cursorDistance = CursorDistance(aX);
       
   248     
       
   249     // get the index of the cursor
       
   250     retVal = iFont->TextCount(iVisualText, cursorDistance);
       
   251     AdjustPostionInVisualText( cursorDistance, retVal ); 
       
   252  
       
   253     return retVal;
       
   254     }
       
   255     
       
   256 TPoint CFepLayoutEditAreaBase::PositionInVisualWindow(TInt aCharacterIndex) const //visual text position in ICF
       
   257     {
       
   258     TPoint position;
       
   259    
       
   260     if(iFont)
       
   261         position.iY = iCorrectedRect.iTl.iY + (iCorrectedRect.Height() - 
       
   262                                     iFont->HeightInPixels())/2;
       
   263     else
       
   264         position.iY = iCorrectedRect.iTl.iY + (iCorrectedRect.Height()/4);
       
   265 
       
   266     if ( iTextDirection ==TBidiText::ELeftToRight )
       
   267         {
       
   268         if(aCharacterIndex < 0)
       
   269             {
       
   270             // index too small, set the point to the beginning of iCorrectedRect
       
   271             position.iX = iCorrectedRect.iTl.iX;
       
   272             }
       
   273         else if (aCharacterIndex > iVisualText.Length())
       
   274             {
       
   275             // index too big, set the point to the end of iCorrectedRect
       
   276             position.iX = iCorrectedRect.iBr.iX;
       
   277             }
       
   278         else
       
   279             {
       
   280             // calculate the position of the character relative to parent window
       
   281             position.iX = KGapLeftOfEachLine + iCorrectedRect.iTl.iX;    
       
   282             if(iFont)
       
   283                 {
       
   284                 position.iX += iFont->TextWidthInPixels(iVisualText.Left(aCharacterIndex));
       
   285                 }
       
   286             }
       
   287         }
       
   288     else
       
   289         {
       
   290         if(aCharacterIndex < 0)
       
   291             {
       
   292             position.iX = iCorrectedRect.iBr.iX;
       
   293             }
       
   294         else if (aCharacterIndex > iVisualText.Length())
       
   295             {
       
   296             position.iX = iCorrectedRect.iTl.iX;
       
   297             }
       
   298         else
       
   299             {
       
   300             position.iX = iCorrectedRect.iBr.iX - KGapLeftOfEachLine;    
       
   301             if(iFont)
       
   302                 {
       
   303                 position.iX -= iFont->TextWidthInPixels(iVisualText.Left(aCharacterIndex));
       
   304                 }
       
   305             }
       
   306         }
       
   307 
       
   308     return position;
       
   309     }
       
   310 // ---------------------------------------------------------------------------
       
   311 // Handle pointer up event
       
   312 // ---------------------------------------------------------------------------
       
   313 //
       
   314 EXPORT_C CFepUiBaseCtrl* CFepLayoutEditAreaBase::HandlePointerUpEventL(const TPoint& aPt)
       
   315     {
       
   316     if (iDimmed || !(iCursorVisible||iCursorSelVisible))
       
   317         {
       
   318         return NULL;
       
   319         }
       
   320     
       
   321     CFepUiBaseCtrl::HandlePointerUpEventL(aPt);
       
   322     
       
   323     iScrolling = EFalse;
       
   324     
       
   325     if(iPreCaptureCtrl)
       
   326         {
       
   327         iPreCaptureCtrl->CapturePointer();
       
   328         }
       
   329     else
       
   330         {
       
   331         CapturePointer(EFalse);   
       
   332         }
       
   333 
       
   334     CalculateDisplayTextL();
       
   335     iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow());
       
   336     Draw();
       
   337     UpdateArea(Rect(),EFalse);
       
   338 
       
   339     return this;    
       
   340     }
       
   341     
       
   342 // ---------------------------------------------------------------------------
       
   343 // Handle pointer move event
       
   344 // ---------------------------------------------------------------------------
       
   345 //
       
   346 EXPORT_C CFepUiBaseCtrl* CFepLayoutEditAreaBase::HandlePointerMoveEventL(const TPoint& aPt)
       
   347     {
       
   348     if( !PointerDown() || iDimmed || !(iCursorVisible||iCursorSelVisible) ) 
       
   349         return this;
       
   350 
       
   351 	TInt newPosition = PositionInDisplayText(aPt.iX);
       
   352 
       
   353     TInt anchorPos = iDisplayTextCurSel.iAnchorPos - iDisplayTextOffset;
       
   354 
       
   355     TInt cursorPos = iDisplayTextCurSel.iCursorPos - iDisplayTextOffset;
       
   356 
       
   357     
       
   358 
       
   359     if(Rect().Contains(aPt) && newPosition == cursorPos)
       
   360 
       
   361         {
       
   362 
       
   363         return this;
       
   364 
       
   365         }
       
   366 
       
   367 
       
   368     iInsertionPoint->DelayCursorWhileDraging();    
       
   369     if(aPt.iX < iCorrectedRect.iTl.iX)
       
   370         {
       
   371         // selection was dragged beyond the left edge of iCorrectedRect -> scroll left
       
   372         ScrollLeft();
       
   373         } 
       
   374     else if(aPt.iX > iCorrectedRect.iBr.iX)
       
   375         {
       
   376         // selection was dragged beyond the right edge of iCorrectedRect -> scroll right
       
   377         ScrollRight();
       
   378         }
       
   379     else
       
   380         {
       
   381         // selection was dragged inside visible buffer -> update selection      
       
   382         SetPositionOfInsertionPointInBuffer(newPosition, anchorPos);
       
   383 
       
   384         iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow());
       
   385         if(iScrolling)
       
   386             {
       
   387             // text was scrolled just before user dragged the pen back inside iCorrectedRect
       
   388             // invalidate entire rect to show the new selection correctly
       
   389             Draw();
       
   390             UpdateArea(Rect(),EFalse);
       
   391             iScrolling = EFalse;
       
   392             }
       
   393         else
       
   394             {         
       
   395             if(newPosition != cursorPos )
       
   396                 {
       
   397                 TPoint prevPos = PositionInWindow(cursorPos);
       
   398                 TPoint newPos = PositionInWindow(newPosition);
       
   399 
       
   400                 // invalidate only changed selection, to minimize flickering
       
   401                 TRect invalidRect(Rect());
       
   402                 invalidRect.iTl.iX = Min(prevPos.iX, newPos.iX);
       
   403                 invalidRect.iBr.iX = Max(prevPos.iX, newPos.iX) + KCursorSelectionGap;
       
   404                 invalidRect.iBr.iX = invalidRect.iBr.iX > Rect().iBr.iX ? Rect().iBr.iX : invalidRect.iBr.iX;
       
   405               
       
   406                 Draw();
       
   407                 UpdateArea(invalidRect,EFalse);
       
   408                 }
       
   409             }
       
   410         }
       
   411     return this;
       
   412     }
       
   413 
       
   414 
       
   415 // ---------------------------------------------------------------------------
       
   416 // Drawa edit area
       
   417 // ---------------------------------------------------------------------------
       
   418 //
       
   419 EXPORT_C void CFepLayoutEditAreaBase::Draw()
       
   420     {
       
   421     if(!AbleToDraw())
       
   422         return;
       
   423     CFbsBitGc* gc = static_cast<CFbsBitGc*>(BitGc());
       
   424     
       
   425     TRect rect = Rect();
       
   426 	DrawOpaqueMaskBackground();
       
   427     // ----- draw bitmaps -----
       
   428 	DrawBackground();
       
   429     DrawContent(gc,rect);
       
   430     }
       
   431     
       
   432 
       
   433 // ---------------------------------------------------------------------------
       
   434 // CFepLayoutEditAreaBase::DrawSelection()
       
   435 // Draws the text selection
       
   436 // (other items were commented in a header).
       
   437 // ---------------------------------------------------------------------------
       
   438 //
       
   439 TRect CFepLayoutEditAreaBase::DrawSelection()
       
   440     {
       
   441     CFbsBitGc* gc = static_cast<CFbsBitGc*>(BitGc());
       
   442     gc->SetClippingRegion(&ValidClipRegion());
       
   443     TRect rect = Rect();
       
   444 
       
   445     if (!iFont || iInDragging)
       
   446         {
       
   447         return TRect(rect.iTl, TSize(0,0));
       
   448         }
       
   449     TInt baseLine = iFont->AscentInPixels() + (rect.Height() - 
       
   450                                                 iFont->HeightInPixels())/2;    
       
   451     
       
   452     TRect selRect(rect);
       
   453     TInt startIndex = iDisplayTextCurSel.LowerPos() - iDisplayTextOffset;
       
   454     TInt endIndex = iDisplayTextCurSel.HigherPos() - iDisplayTextOffset;
       
   455     if(startIndex < 0)
       
   456         {
       
   457         // set startIndex to the beginning of the buffer.
       
   458         startIndex = 0;
       
   459         }
       
   460     if(endIndex> iDisplayText.Length())
       
   461         {
       
   462         // set endIndex to the end of the buffer
       
   463         endIndex = iDisplayText.Length();
       
   464         }
       
   465     TInt pos1 = PositionInWindow(startIndex).iX;
       
   466     TInt pos2 = PositionInWindow(endIndex).iX;
       
   467     selRect.iTl.iX = Min( pos1, pos2 );
       
   468     selRect.iBr.iX = Max( pos1, pos2 );
       
   469     if(selRect.iBr.iX > rect.iBr.iX)
       
   470         {
       
   471         selRect.iBr.iX = rect.iBr.iX;
       
   472         }
       
   473     selRect.iTl.iY += ((baseLine - iFont->AscentInPixels()) - 
       
   474                                                     KSelectionRectMarginY);
       
   475     selRect.iBr.iY -= ((rect.Height()-(baseLine + iFont->DescentInPixels()))
       
   476                                              - KSelectionRectMarginY);
       
   477     selRect.Intersection( rect );
       
   478     
       
   479     // draw selection by inverting colors in the selected text rectancle
       
   480     gc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   481     gc->SetBrushColor(KRgbBlack);
       
   482     gc->SetDrawMode(CGraphicsContext::EDrawModeNOTSCREEN);
       
   483     gc->SetPenSize(PenSize());
       
   484     gc->SetPenColor(PenColor());
       
   485     gc->DrawRect(selRect);
       
   486 
       
   487     // restore normal draw mode
       
   488     gc->SetDrawMode(CGraphicsContext::EDrawModePEN);
       
   489     gc->SetBrushStyle(CGraphicsContext::ENullBrush);                
       
   490     gc->CancelClipping();        
       
   491     return selRect;
       
   492     }
       
   493 
       
   494 TInt CFepLayoutEditAreaBase::UpdateSecretText( TAny* aEditArea )
       
   495     {
       
   496     CFepLayoutEditAreaBase* editArea = static_cast<CFepLayoutEditAreaBase*>(aEditArea);
       
   497     
       
   498     if ( !editArea->iTextIsSecret )
       
   499         {
       
   500         editArea->iSecretTextTimer->Cancel();
       
   501         return KErrNone;
       
   502         }
       
   503     TPtr ptr = editArea->iBuffer->Des();
       
   504     TInt startPos = ptr.LocateReverse( '*' ) + 1;
       
   505     TBuf<1> buf;
       
   506     buf.Append( '*' );
       
   507  
       
   508     if ( startPos < ptr.Length() )
       
   509         {
       
   510         ptr.Replace( startPos, 1, buf );
       
   511         
       
   512         if ( startPos == ptr.Length() -1 )
       
   513             {
       
   514             editArea->iSecretTextTimer->Cancel();
       
   515             }
       
   516         }
       
   517     TRAP_IGNORE(editArea->CalculateDisplayTextL());
       
   518     editArea->Draw();
       
   519     editArea->UpdateArea(editArea->Rect(),EFalse);
       
   520     return KErrNone;
       
   521     }
       
   522  
       
   523 // ---------------------------------------------------------------------------
       
   524 // CFepLayoutEditAreaBase::DrawContent
       
   525 // Draws the text into the given graphic context
       
   526 // (other items were commented in a header).
       
   527 // ---------------------------------------------------------------------------
       
   528 //
       
   529 EXPORT_C void CFepLayoutEditAreaBase::DrawContent(CBitmapContext* aGc, const TRect& aRect)
       
   530     {    
       
   531 
       
   532     if(!iBuffer || !iFont || iInDragging)
       
   533         {
       
   534         return;
       
   535         }
       
   536     aGc->SetClippingRegion(ValidClipRegion());
       
   537     
       
   538     TRect editRect = Rect();
       
   539     if(aRect.Intersects(editRect))
       
   540         {
       
   541         // set brush invisible so the background of the edit area does not get painted white
       
   542         aGc->SetBrushStyle(CGraphicsContext::ENullBrush);
       
   543         aGc->UseFont(iFont);
       
   544     
       
   545         // draw text
       
   546 /*        TInt baseLine = iFont->AscentInPixels() + (editRect.Height()-iFont->
       
   547                                                     HeightInPixels())/2;*/
       
   548         TInt baseLine = iFont->AscentInPixels()/2 + editRect.Height()/2;
       
   549 	    TRgb color( KRgbBlack );  // sane default for nonskinned case
       
   550 	    if(iTextColor != KRgbRed)//temperarily to fix the vkb warning color defect.
       
   551     	    {
       
   552     	    
       
   553 
       
   554     	    if ( AknsUtils::AvkonSkinEnabled() )
       
   555     	        {
       
   556     	        AknsUtils::GetCachedColor( AknsUtils::SkinInstance(),
       
   557     	                 color, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG60 );
       
   558     	        }
       
   559             }
       
   560         else
       
   561             {
       
   562             color = iTextColor; //when color group is ready for chinese, pls delete this judgement.
       
   563             }
       
   564         aGc->SetPenColor(color);
       
   565         
       
   566         if( isCenter )
       
   567             {
       
   568             aGc->DrawText(iVisualText, editRect, baseLine,
       
   569                  CGraphicsContext::ECenter,KGapLeftOfEachLine);
       
   570             }
       
   571         else
       
   572             {
       
   573             if ( iTextDirection ==TBidiText::ELeftToRight )
       
   574                 {
       
   575                 aGc->DrawText(iVisualText, editRect, baseLine,
       
   576                      CGraphicsContext::ELeft,KGapLeftOfEachLine);
       
   577                 }
       
   578             else
       
   579                 {
       
   580                 aGc->DrawText(iVisualText, editRect, baseLine,
       
   581                      CGraphicsContext::ERight,KGapLeftOfEachLine);
       
   582                 }
       
   583             }
       
   584         // draw text selection (if any)
       
   585         TInt selectionLength = iDisplayTextCurSel.Length(); 
       
   586         if( selectionLength > 0 && (iCursorVisible||iCursorSelVisible) )
       
   587             {
       
   588             // calculate the bounding rectancle of the selected text
       
   589             DrawSelection();
       
   590             }
       
   591         iInsertionPoint->Draw(aGc, ETrue);
       
   592 
       
   593         aGc->DiscardFont();
       
   594         }
       
   595     aGc->CancelClippingRegion();
       
   596     }
       
   597 
       
   598 // ---------------------------------------------------------------------------
       
   599 // CFepLayoutEditAreaBase::SetPositionOfInsertionPointInBuffer
       
   600 // Sets the position of the cursor within the buffer, i.e.
       
   601 // what is the ordinal of the character where the cursor is.
       
   602 // (other items were commented in a header).
       
   603 // ---------------------------------------------------------------------------
       
   604 //
       
   605 EXPORT_C void CFepLayoutEditAreaBase::SetPositionOfInsertionPointInBuffer(
       
   606                                         TInt& aPositionOfInsertionPointInBuffer)
       
   607     {
       
   608     SetPositionOfInsertionPointInBuffer(aPositionOfInsertionPointInBuffer, 
       
   609                                             aPositionOfInsertionPointInBuffer);
       
   610     //make sure anchor pos is ajusted
       
   611     iDisplayTextCurSel.iAnchorPos = iDisplayTextCurSel.iCursorPos;
       
   612     iSelectedCompositionText.iAnchorPos = iSelectedCompositionText.iCursorPos;
       
   613     }
       
   614 
       
   615 // ---------------------------------------------------------------------------
       
   616 // CFepLayoutEditAreaBase::SetPositionOfInsertionPointInBuffer
       
   617 // See previous. In addition adjusts the anchor position also
       
   618 // (other items were commented in a header).
       
   619 // ---------------------------------------------------------------------------
       
   620 //
       
   621 void CFepLayoutEditAreaBase::SetPositionOfInsertionPointInBuffer(
       
   622         TInt& aPositionOfInsertionPointInBuffer, TInt& aPositionOfAnchorInBuffer)
       
   623     {
       
   624     TInt lastCurPos = iDisplayTextCurSel.iCursorPos;
       
   625     
       
   626     iDisplayTextCurSel.iCursorPos = aPositionOfInsertionPointInBuffer + iDisplayTextOffset;
       
   627     iDisplayTextCurSel.iAnchorPos = aPositionOfAnchorInBuffer + iDisplayTextOffset;
       
   628     AdjustSelectedCompositionText(iDisplayTextCurSel.iCursorPos);
       
   629     
       
   630     iSelectedCompositionText.iCursorPos = iDisplayTextCurSel.iCursorPos + iRelativePos;
       
   631     iSelectedCompositionText.iAnchorPos = iDisplayTextCurSel.iAnchorPos + iRelativePos;
       
   632     
       
   633     if (lastCurPos != iDisplayTextCurSel.iCursorPos)
       
   634         {
       
   635         ReportEvent(EEventSetAppCursorSelection);
       
   636         }
       
   637     }
       
   638 
       
   639 // ---------------------------------------------------------------------------
       
   640 // CFepLayoutEditAreaBase::PositionOfInsertionPointOnWindow
       
   641 // Calculates coordinates where the cursor should be drawn
       
   642 // (other items were commented in a header).
       
   643 // ---------------------------------------------------------------------------
       
   644 //
       
   645 EXPORT_C TPoint CFepLayoutEditAreaBase::PositionOfInsertionPointOnWindow()
       
   646     {
       
   647     // default position, this is returned if there is no text in the buffer
       
   648     TPoint position =iCorrectedRect.iTl; 
       
   649 
       
   650     if(iBuffer)
       
   651         {
       
   652         TInt pos = iDisplayTextCurSel.iCursorPos - iDisplayTextOffset;
       
   653         if(pos < 0)
       
   654             {
       
   655             pos = 0;
       
   656             }
       
   657         position = PositionInWindow(pos);
       
   658         }
       
   659         
       
   660     return position;
       
   661     }
       
   662 
       
   663 
       
   664 // ---------------------------------------------------------------------------
       
   665 // CVkbEditArea::PositionInWindow
       
   666 // Calculates the position of a character in iDisplayText
       
   667 // as pixels relative to the parent window
       
   668 // (other items were commented in a header).
       
   669 // ---------------------------------------------------------------------------
       
   670 //
       
   671 EXPORT_C TPoint CFepLayoutEditAreaBase::PositionInWindow(TInt aCharacterIndex)
       
   672     {
       
   673     TPoint position;
       
   674     if(iFont)
       
   675         position.iY = iCorrectedRect.iTl.iY + (iCorrectedRect.Height() - 
       
   676                                     iFont->HeightInPixels())/2;
       
   677     else
       
   678         position.iY = iCorrectedRect.iTl.iY + (iCorrectedRect.Height()/4);
       
   679     
       
   680     TInt offsetCenter = 0; 
       
   681      
       
   682     if(isCenter)   
       
   683         {
       
   684         offsetCenter = (iCorrectedRect.Width() - iFont->TextWidthInPixels(iDisplayText))/2;
       
   685         }
       
   686        
       
   687     if ( iTextDirection ==TBidiText::ELeftToRight )
       
   688         {
       
   689         if(aCharacterIndex < 0)
       
   690             {
       
   691             // index too small, set the point to the beginning of iCorrectedRect
       
   692             position.iX = iCorrectedRect.iTl.iX + offsetCenter;
       
   693             }
       
   694         else if (aCharacterIndex > iDisplayText.Length())
       
   695             {
       
   696             // index too big, set the point to the end of iCorrectedRect
       
   697             position.iX = iCorrectedRect.iBr.iX - offsetCenter;
       
   698             }
       
   699         else
       
   700             {
       
   701             // calculate the position of the character relative to parent window
       
   702             position.iX = KGapLeftOfEachLine + iCorrectedRect.iTl.iX;    
       
   703             if(iFont)
       
   704                 {
       
   705                 position.iX += TextWidth( 0, aCharacterIndex ) + offsetCenter;
       
   706                 }
       
   707             }
       
   708         }
       
   709     else
       
   710         {
       
   711         if(aCharacterIndex < 0)
       
   712             {
       
   713             position.iX = iCorrectedRect.iBr.iX - offsetCenter;
       
   714             }
       
   715         else if (aCharacterIndex > iDisplayText.Length())
       
   716             {
       
   717             position.iX = iCorrectedRect.iTl.iX + offsetCenter;
       
   718             }
       
   719         else
       
   720             {
       
   721             position.iX = iCorrectedRect.iBr.iX - KGapLeftOfEachLine;    
       
   722             if( iFont && ( aCharacterIndex > 0) )
       
   723                 {
       
   724                 /*TInt count = 0;
       
   725                 for ( TInt i = aCharacterIndex-1; i > 0; i-- )
       
   726                     {
       
   727                     if ( CharDirection( iDisplayText[i] ) == iTextDirection )
       
   728                         {
       
   729                         break;
       
   730                         }
       
   731                     else
       
   732                         {
       
   733                         count++;
       
   734                         }
       
   735                     }
       
   736                  position.iX -= TextWidth( 0, aCharacterIndex-count );*/
       
   737                 position.iX -= TextWidth( 0, aCharacterIndex ) + offsetCenter;
       
   738                 }
       
   739             }
       
   740         }
       
   741 
       
   742     return position;
       
   743     }
       
   744 
       
   745 // ---------------------------------------------------------------------------
       
   746 // CVkbEditArea::PositionInDisplayText
       
   747 // Gets the character offset in the text buffer 
       
   748 // of the given screen coordinate
       
   749 // (other items were commented in a header).
       
   750 // ---------------------------------------------------------------------------
       
   751 //
       
   752 EXPORT_C TInt CFepLayoutEditAreaBase::PositionInDisplayText(TInt aX) const
       
   753     {
       
   754     TInt retVal;
       
   755         
       
   756     if(!iBuffer || !iFont)
       
   757         {
       
   758         // Edit area was empty, position is zero
       
   759         retVal = 0;
       
   760         }
       
   761     else
       
   762         {
       
   763         TInt offsetCenter = 0;
       
   764         if(isCenter)
       
   765             {
       
   766             offsetCenter = (iCorrectedRect.Width() - iFont->TextWidthInPixels(iDisplayText))/2;
       
   767             }
       
   768         
       
   769         // get the distance of the cursor from the beginning of iCorrectedRect
       
   770         TInt cursorDistance;
       
   771         
       
   772         if ( iTextDirection == TBidiText::ELeftToRight)
       
   773             {
       
   774             cursorDistance = aX - iCorrectedRect.iTl.iX - KGapLeftOfEachLine - offsetCenter;
       
   775             }
       
   776         else
       
   777             {
       
   778             cursorDistance = iCorrectedRect.iBr.iX - aX - KGapLeftOfEachLine - offsetCenter;
       
   779             }
       
   780         
       
   781         TInt maxWidth = iCorrectedRect.Width() - KGapLeftOfEachLine;
       
   782         if(cursorDistance < 0)
       
   783             {
       
   784             cursorDistance = 0;
       
   785             }
       
   786         else 
       
   787             {
       
   788             cursorDistance = cursorDistance > maxWidth ? maxWidth : cursorDistance;
       
   789             }
       
   790         // get the index of the cursor
       
   791         retVal = iFont->TextCount(iDisplayText, cursorDistance);
       
   792 
       
   793         /*for ( TInt i = retVal; i < iDisplayText.Length(); i++ )
       
   794             {
       
   795             if ( CharDirection( iDisplayText[i] ) == iTextDirection )
       
   796                 {
       
   797                 break;
       
   798                 }
       
   799             else
       
   800                 {
       
   801                 retVal++;
       
   802                 }
       
   803             }*/
       
   804         
       
   805         if ( retVal < iDisplayText.Length() )
       
   806             {
       
   807             TInt textWidth = iFont->TextWidthInPixels( iDisplayText.Mid( retVal,1 ) );
       
   808             TInt offsetWidth = cursorDistance - iFont->TextWidthInPixels(iDisplayText.Left( retVal ));
       
   809             if ( offsetWidth > textWidth/2 )
       
   810                 {
       
   811                 retVal++;
       
   812                 }
       
   813             }
       
   814         }
       
   815 
       
   816     return retVal;
       
   817     }
       
   818 
       
   819 
       
   820 // ---------------------------------------------------------------------------
       
   821 // CFepLayoutEditAreaBase::SetTextColor
       
   822 // Sets the color of the text
       
   823 // (other items were commented in a header).
       
   824 // ---------------------------------------------------------------------------
       
   825 //
       
   826 EXPORT_C void CFepLayoutEditAreaBase::SetTextColor(const TRgb aColor)
       
   827     {
       
   828     iTextColor = aColor;
       
   829     }
       
   830 
       
   831 // ---------------------------------------------------------------------------
       
   832 // CVkbEditArea::ScrollLeft
       
   833 // Scrolls selection to the left exposing more text
       
   834 // (other items were commented in a header).
       
   835 // ---------------------------------------------------------------------------
       
   836 //
       
   837 void CFepLayoutEditAreaBase::ScrollLeft()
       
   838     {
       
   839     
       
   840     iScrolling = ETrue;
       
   841     if(iBuffer)
       
   842         {
       
   843         TInt cursorPos = iSelectedCompositionText.iCursorPos;
       
   844         TInt anchorPos = iSelectedCompositionText.iAnchorPos;
       
   845         // decrease the cursor position, the anchor remains unchanged
       
   846        /* TInt index = iDisplayTextCurSel.iCursorPos-1;
       
   847         index = ( index < 0 )? 0:index;
       
   848         index = ( index > iDisplayText.Length()-1 )? iDisplayText.Length()-1:index; 
       
   849         if ( CharDirection(iDisplayText[index]) == TBidiText::ELeftToRight )*/
       
   850         if ( iTextDirection == TBidiText::ELeftToRight )
       
   851             {
       
   852             cursorPos--;
       
   853             }
       
   854         else
       
   855             {
       
   856             cursorPos++;
       
   857             }
       
   858 
       
   859         UpdateContent(TCursorSelection(cursorPos, anchorPos) );
       
   860 
       
   861         if( (anchorPos == iSelectedCompositionText.iAnchorPos) && 
       
   862             (cursorPos == iSelectedCompositionText.iCursorPos) )
       
   863             {
       
   864             return;
       
   865             }
       
   866         else
       
   867             {
       
   868             ReportEvent(EEventSetAppCursorSelection);
       
   869             }
       
   870         }
       
   871     }
       
   872     
       
   873 
       
   874 // ---------------------------------------------------------------------------
       
   875 // CVkbEditArea::ScrollRight
       
   876 // Scrolls selection to the right exposing more text
       
   877 // (other items were commented in a header).
       
   878 // ---------------------------------------------------------------------------
       
   879 //
       
   880 void CFepLayoutEditAreaBase::ScrollRight()
       
   881     {
       
   882     
       
   883     iScrolling = ETrue;
       
   884     if(iBuffer)
       
   885         {
       
   886         // increase the cursor position, the anchor remains unchanged
       
   887         TInt cursorPos = iSelectedCompositionText.iCursorPos;
       
   888         TInt anchorPos = iSelectedCompositionText.iAnchorPos;
       
   889         
       
   890         /*TInt index = iDisplayTextCurSel.iCursorPos-1;
       
   891         index = ( index < 0 )? 0:index;
       
   892         index = ( index > iDisplayText.Length()-1 )? iDisplayText.Length()-1:index; 
       
   893         if ( CharDirection(iDisplayText[index]) == TBidiText::ELeftToRight )*/
       
   894         if ( iTextDirection == TBidiText::ELeftToRight )
       
   895             {
       
   896             cursorPos++;
       
   897             }
       
   898         else
       
   899             {
       
   900             cursorPos--;
       
   901             }
       
   902 
       
   903         UpdateContent(TCursorSelection(cursorPos, anchorPos) );
       
   904 
       
   905         if( (anchorPos == iSelectedCompositionText.iAnchorPos) && 
       
   906             (cursorPos == iSelectedCompositionText.iCursorPos) )
       
   907             {
       
   908             return;
       
   909             }
       
   910         else
       
   911             {
       
   912             ReportEvent(EEventSetAppCursorSelection);
       
   913             }
       
   914         }
       
   915     }
       
   916 
       
   917 // ---------------------------------------------------------------------------
       
   918 // CFepLayoutEditAreaBase::PrepareForFocusLoss
       
   919 // Sets the color of the text
       
   920 // (other items were commented in a header).
       
   921 // ---------------------------------------------------------------------------
       
   922 //
       
   923 EXPORT_C void CFepLayoutEditAreaBase::PrepareForFocusLoss()
       
   924     {
       
   925     iInsertionPoint->SetOn(EFalse);
       
   926     iHasFocus = EFalse;
       
   927 
       
   928     ReportEvent(EEventControlFocusLost);
       
   929     }
       
   930 
       
   931 // ---------------------------------------------------------------------------
       
   932 // CFepLayoutEditAreaBase::PrepareForFocusGain
       
   933 // Sets the color of the text
       
   934 // (other items were commented in a header).
       
   935 // ---------------------------------------------------------------------------
       
   936 //
       
   937 EXPORT_C TBool CFepLayoutEditAreaBase::PrepareForFocusGain()
       
   938     {
       
   939      if (CursorVisible())
       
   940         {
       
   941         iInsertionPoint->SetOn(ETrue);
       
   942         }
       
   943     iHasFocus = ETrue;
       
   944     ReportEvent(EEventControlFocusGained);
       
   945 
       
   946     return ETrue;
       
   947     }
       
   948 
       
   949 void CFepLayoutEditAreaBase::IncreaseBufferL(TInt aLength)
       
   950     {
       
   951 	if ( !iBuffer )
       
   952         {
       
   953         iBuffer = HBufC::NewL( aLength + KAddBufLen );
       
   954         }
       
   955     else if ( iBuffer->Length() + aLength > iBuffer->Des().MaxLength() )
       
   956         {
       
   957         //delete iBuffer;
       
   958         //iBuffer = NULL;
       
   959         iBuffer = iBuffer->ReAllocL( iBuffer->Length() + aLength + KAddBufLen);
       
   960         }
       
   961     }
       
   962     
       
   963 EXPORT_C void CFepLayoutEditAreaBase::SetTextAlignmentL(TInt aAlignment)
       
   964     {
       
   965     switch (aAlignment)
       
   966         {
       
   967         case EAknEditorAlignCenter:
       
   968             isCenter = ETrue;
       
   969             break;
       
   970         default:
       
   971             isCenter = EFalse;
       
   972             break;
       
   973         }
       
   974     
       
   975     }
       
   976     
       
   977 
       
   978 // ---------------------------------------------------------------------------
       
   979 // CFepLayoutEditAreaBase::SetTextL
       
   980 // Sets the text to be displayed
       
   981 // (other items were commented in a header).
       
   982 // ---------------------------------------------------------------------------
       
   983 //
       
   984 EXPORT_C void CFepLayoutEditAreaBase::SetTextL( const TFepInputContextFieldData& aData )
       
   985    {
       
   986     iSelectedCompositionText.iCursorPos = aData.iCurSel.iCursorPos;
       
   987     iSelectedCompositionText.iAnchorPos = aData.iCurSel.iAnchorPos;
       
   988     
       
   989     IncreaseBufferL( aData.iText.Length() );
       
   990     
       
   991     TPtr ptr = iBuffer->Des();    
       
   992     
       
   993     switch ( aData.iCmd )
       
   994         {
       
   995         case EPeninputICFInitial:
       
   996             {
       
   997             if ( aData.iFlag & EFepICFDataDirectionMFNE )
       
   998                 {
       
   999                 iMfneEditor = ETrue;
       
  1000                 }
       
  1001             else
       
  1002                 {
       
  1003                 iMfneEditor = EFalse;
       
  1004                 }
       
  1005             ptr = aData.iText;
       
  1006             if ( aData.iFlag == EFepICFDataDirectionRTL )
       
  1007                 {
       
  1008                 iTextDirection = TBidiText::ERightToLeft;
       
  1009                 }
       
  1010              else
       
  1011                 {
       
  1012                 iTextDirection = TBidiText::ELeftToRight;
       
  1013                 }
       
  1014             }
       
  1015             break;
       
  1016         case EPeninputICFReplace:
       
  1017             {
       
  1018             if(aData.iStartPos > ptr.Length() || 
       
  1019                aData.iStartPos + aData.iLength > ptr.Length())
       
  1020                 {
       
  1021                 return;
       
  1022                 }
       
  1023             ptr.Replace( aData.iStartPos, aData.iLength, aData.iText );
       
  1024                     
       
  1025             if ( aData.iMidPos >= 0 )
       
  1026                 {
       
  1027                 //inline
       
  1028                 } 
       
  1029            
       
  1030             }
       
  1031             break;
       
  1032         case EPeninputICFDelete:
       
  1033             {
       
  1034             if(aData.iStartPos > ptr.Length())
       
  1035                 {
       
  1036                 return;
       
  1037                 }
       
  1038             ptr.Delete( aData.iStartPos, aData.iLength );
       
  1039             }
       
  1040             break;
       
  1041         case EPeninputICFSetCurSel:
       
  1042             {
       
  1043             break;
       
  1044             }
       
  1045         default:
       
  1046             {
       
  1047             return;
       
  1048             }
       
  1049         }
       
  1050     
       
  1051     UpdateText( aData );
       
  1052     
       
  1053     CalculateDisplayTextL();
       
  1054     
       
  1055     SetCursorVisible( aData.iCursorVisibility, aData.iCursorSelVisible );
       
  1056     
       
  1057     iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow());
       
  1058     Draw();
       
  1059     UpdateArea(Rect(),EFalse);
       
  1060    }
       
  1061 
       
  1062 void CFepLayoutEditAreaBase::UpdateText( const TFepInputContextFieldData& aData )
       
  1063     {
       
  1064     if ( !iTextIsSecret )
       
  1065         {
       
  1066         return;
       
  1067         }
       
  1068     
       
  1069     TPtr ptr = iBuffer->Des();  
       
  1070     
       
  1071     switch( aData.iCmd )
       
  1072         {
       
  1073         case EPeninputICFInitial:
       
  1074             {
       
  1075             ptr.Fill( '*', ptr.Length() );
       
  1076             }
       
  1077             break;
       
  1078         case EPeninputICFReplace:
       
  1079             {                    
       
  1080             if ( iSecretTextTimer->IsActive() )
       
  1081                 {
       
  1082                 iSecretTextTimer->Cancel();
       
  1083                 TBuf<1> buf;
       
  1084                 buf.Append( '*' );
       
  1085                 
       
  1086                 if (aData.iStartPos != 0)
       
  1087                     {
       
  1088                     ptr.Replace( aData.iStartPos - 1, 1, buf );
       
  1089                     }
       
  1090                 }
       
  1091 
       
  1092             iSecretTextTimer->Start(
       
  1093                 aData.iText.Length()>1?KSecretInstantShowTimer:KSecretUpdateTimer,
       
  1094                 KSecretUpdateTimer, TCallBack( UpdateSecretText, this ) );
       
  1095             }
       
  1096             break;
       
  1097         case EPeninputICFSetCurSel:
       
  1098         default:
       
  1099             {
       
  1100             break;
       
  1101             }
       
  1102         }
       
  1103     }
       
  1104 // ---------------------------------------------------------------------------
       
  1105 // CFepLayoutEditAreaBase::UpdateCursorSelection
       
  1106 // update cursor
       
  1107 // (other items were commented in a header).
       
  1108 // ---------------------------------------------------------------------------
       
  1109 //
       
  1110 EXPORT_C void CFepLayoutEditAreaBase::UpdateCursorSelection( const TCursorSelection& aCursorSel)
       
  1111     {
       
  1112     if( (aCursorSel.iAnchorPos == iSelectedCompositionText.iAnchorPos) && 
       
  1113         (aCursorSel.iCursorPos == iSelectedCompositionText.iCursorPos) )
       
  1114         {
       
  1115         return;
       
  1116         }
       
  1117        
       
  1118     iSelectedCompositionText = aCursorSel;
       
  1119     
       
  1120     TRAP_IGNORE(CalculateDisplayTextL());
       
  1121         
       
  1122     iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow());
       
  1123     Draw();
       
  1124     UpdateArea(Rect(),EFalse);
       
  1125     }
       
  1126        
       
  1127 // ---------------------------------------------------------------------------
       
  1128 // CFepLayoutEditAreaBase::CalculateDisplayTextL
       
  1129 // calcuate display text according to text and cursor
       
  1130 // (other items were commented in a header).
       
  1131 // ---------------------------------------------------------------------------
       
  1132 //
       
  1133 void CFepLayoutEditAreaBase::CalculateDisplayTextL()   
       
  1134     {
       
  1135     if(!iBuffer || !iFont)
       
  1136         {
       
  1137         return;
       
  1138         }
       
  1139     
       
  1140     TInt cursorPos = iSelectedCompositionText.iCursorPos;
       
  1141     
       
  1142     // finding a paragraph include current cursor selection 
       
  1143     TInt beginPos = iBuffer->Left( cursorPos ).LocateReverse(KParagraphSeperator); 
       
  1144     beginPos = beginPos < 0? 0 : beginPos + 1; 
       
  1145     
       
  1146     if( cursorPos > iBuffer->Length() )
       
  1147         {
       
  1148         return;
       
  1149         }
       
  1150         
       
  1151     TInt endPos = iBuffer->Mid( cursorPos ).Locate(KParagraphSeperator); 
       
  1152     endPos = endPos < 0? iBuffer->Des().Length() - 1 : endPos + cursorPos - 1; 
       
  1153 
       
  1154     cursorPos -= beginPos;
       
  1155     iDisplayTextCurSel.iCursorPos = iSelectedCompositionText.iCursorPos - beginPos;
       
  1156     iDisplayTextCurSel.iAnchorPos = iSelectedCompositionText.iAnchorPos - beginPos;
       
  1157     
       
  1158     iDisplayText.Set( iBuffer->Mid( beginPos, endPos - beginPos +1 ) );
       
  1159     iRelativePos = beginPos;
       
  1160     
       
  1161     iDisplayTextOffset = 0;
       
  1162     
       
  1163     TInt width = iFont->TextWidthInPixels(iDisplayText);//*iBuffer
       
  1164     
       
  1165     // get visual text and direction
       
  1166     delete iVisualBuffer;
       
  1167     iVisualBuffer = NULL;
       
  1168     iVisualBuffer = HBufC::NewL(iDisplayText.Length() + TBidiLogicalToVisual::KMinCharAvailable);
       
  1169     //CleanupStack::PushL( iVisualBuffer );
       
  1170     if ( iMfneEditor )
       
  1171         {
       
  1172         TPtr text = iVisualBuffer->Des();
       
  1173         AknBidiTextUtils::ConvertToVisualAndClip(iDisplayText, text, *iFont, width, width);
       
  1174         iVisualText.Set( text );
       
  1175         }
       
  1176     else
       
  1177         {
       
  1178         iVisualText.Set( iDisplayText );
       
  1179         }
       
  1180     //CleanupStack::Pop();
       
  1181     
       
  1182     TInt rectWidth = iCorrectedRect.Width();
       
  1183 
       
  1184     TInt partRectWidth = iCorrectedRect.Width() - 
       
  1185                      Max(KDefaultRightMargin, iFont->MaxNormalCharWidthInPixels());
       
  1186 
       
  1187     TPtrC left = iDisplayText.Left(cursorPos);
       
  1188     TInt widthToCursor = iFont->TextWidthInPixels(left);          
       
  1189     
       
  1190     TPtrC right = iDisplayText.Mid(cursorPos);
       
  1191     TInt cursorToEnd = iFont->TextWidthInPixels(right);   
       
  1192 
       
  1193     if (widthToCursor > rectWidth/2 && cursorToEnd > rectWidth/2)
       
  1194         {
       
  1195         TInt newPos = widthToCursor - (rectWidth/2);
       
  1196         
       
  1197         TInt from = iFont->TextCount(iDisplayText, newPos);
       
  1198         iDisplayTextOffset = from;
       
  1199         iDisplayText.Set(iDisplayText.Mid(iDisplayTextOffset));
       
  1200         }
       
  1201     else if (widthToCursor > rectWidth/2 && cursorToEnd <= rectWidth/2)
       
  1202         {
       
  1203         TInt newPos = width - partRectWidth;
       
  1204         if(newPos < 0)
       
  1205             {
       
  1206             newPos = 0;                
       
  1207             }
       
  1208 
       
  1209         // find the index of the first character
       
  1210         TInt from = iFont->TextCount(iDisplayText, newPos);//*iBuffer
       
  1211         TInt usedWidth = iFont->TextWidthInPixels(iDisplayText.Left(from));//iBuffer
       
  1212     
       
  1213         //if there are half char not displayable (usedWidth < newPos)
       
  1214         //or if usedWidth == newPos but there are chars before, increase "from"
       
  1215         //also because of gap before chars
       
  1216         if (usedWidth <= newPos && newPos != 0)
       
  1217             {
       
  1218             from ++;
       
  1219             }
       
  1220 
       
  1221         iDisplayTextOffset = from;
       
  1222         iDisplayText.Set(iDisplayText.Mid(iDisplayTextOffset));
       
  1223         }
       
  1224     
       
  1225     if ( iTextDirection == TBidiText::ELeftToRight )
       
  1226         {
       
  1227         iVisualText.Set(iVisualText.Mid(iDisplayTextOffset));
       
  1228         }
       
  1229     else
       
  1230         {
       
  1231         iVisualText.Set(iVisualText.Left(iVisualText.Length()-iDisplayTextOffset));
       
  1232         }
       
  1233 
       
  1234     // clip the end of text if it exceeds editor rectancle
       
  1235     TInt maxCount = iFont->TextCount(iDisplayText,rectWidth);
       
  1236     if(maxCount < iDisplayText.Length())
       
  1237         {
       
  1238         iDisplayText.Set(iDisplayText.Left(maxCount));
       
  1239         if ( iTextDirection == TBidiText::ELeftToRight )
       
  1240             {
       
  1241             iVisualText.Set(iVisualText.Left(maxCount));
       
  1242             }
       
  1243         else
       
  1244             {
       
  1245             iVisualText.Set(iVisualText.Right(maxCount));
       
  1246             }
       
  1247         }
       
  1248    }
       
  1249     
       
  1250 // ---------------------------------------------------------------------------
       
  1251 // CFepLayoutEditAreaBase::IsFocused
       
  1252 // 
       
  1253 // (other items were commented in a header).
       
  1254 // ---------------------------------------------------------------------------
       
  1255 //
       
  1256 EXPORT_C TBool CFepLayoutEditAreaBase::IsFocused()
       
  1257     {
       
  1258     return iHasFocus;
       
  1259     }
       
  1260 
       
  1261 // ---------------------------------------------------------------------------
       
  1262 // CFepLayoutEditAreaBase::SetFont
       
  1263 // Set font
       
  1264 // (other items were commented in a header).
       
  1265 // ---------------------------------------------------------------------------
       
  1266 //       
       
  1267 EXPORT_C void CFepLayoutEditAreaBase::SetFont(const TFontSpec& aFontSpec)
       
  1268     {
       
  1269     iFontSpec = aFontSpec;
       
  1270     if(BitmapDevice())
       
  1271         {
       
  1272         if(iFontOwnership && iFont)
       
  1273             {
       
  1274             BitmapDevice()->ReleaseFont(iFont);
       
  1275             iFont = NULL;    
       
  1276             }    
       
  1277         
       
  1278         if (KErrNone != BitmapDevice()->GetNearestFontInPixels(iFont,iFontSpec))
       
  1279             iFont = NULL;
       
  1280         }
       
  1281     iFontOwnership = ETrue;
       
  1282     if(iFont && iInsertionPoint)
       
  1283         {
       
  1284         iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow()); 
       
  1285         iInsertionPoint->SetHeight(iFont->HeightInPixels());
       
  1286         }
       
  1287     }        
       
  1288 
       
  1289 // ---------------------------------------------------------------------------
       
  1290 // CFepLayoutEditAreaBase::SetFont
       
  1291 // Set font
       
  1292 // (other items were commented in a header).
       
  1293 // ---------------------------------------------------------------------------
       
  1294 //       
       
  1295 EXPORT_C void CFepLayoutEditAreaBase::SetFont(const CFont* aFont)
       
  1296     {
       
  1297     
       
  1298     if(BitmapDevice())
       
  1299         {
       
  1300         if(iFontOwnership && iFont)
       
  1301             {
       
  1302             BitmapDevice()->ReleaseFont(iFont);
       
  1303             }    
       
  1304         
       
  1305         iFont = const_cast<CFont*> (aFont);
       
  1306         if(iFont)
       
  1307             iFontSpec = iFont->FontSpecInTwips();
       
  1308         }
       
  1309     iFontOwnership = EFalse;
       
  1310         
       
  1311     if(iFont && iInsertionPoint)
       
  1312         {
       
  1313         iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow()); 
       
  1314         iInsertionPoint->SetHeight(iFont->HeightInPixels());
       
  1315         }
       
  1316     }        
       
  1317 
       
  1318 // ---------------------------------------------------------------------------
       
  1319 // CFepLayoutEditAreaBase::UpdateArea
       
  1320 // Update layout area
       
  1321 // (other items were commented in a header).
       
  1322 // ---------------------------------------------------------------------------
       
  1323 //       
       
  1324 /*void CFepLayoutEditAreaBase::UpdateArea(const TRect& aRect,TBool aUpdateFlag)
       
  1325     {
       
  1326     CFepUiBaseCtrl::UpdateArea(aRect,aUpdateFlag);
       
  1327     }
       
  1328   */  
       
  1329 // ---------------------------------------------------------------------------
       
  1330 // CFepLayoutEditAreaBase::Move
       
  1331 // Move the window
       
  1332 // (other items were commented in a header).
       
  1333 // ---------------------------------------------------------------------------
       
  1334 //            
       
  1335 EXPORT_C void CFepLayoutEditAreaBase::Move(const TPoint& aOffset)
       
  1336     {
       
  1337     CFepUiBaseCtrl::Move(aOffset);
       
  1338     //move text rect
       
  1339     iCorrectedRect.Move(aOffset);
       
  1340     //move cursor
       
  1341     if(iInsertionPoint)
       
  1342         {        
       
  1343         TPoint curPos = iInsertionPoint->Position();
       
  1344         curPos += aOffset;
       
  1345         iInsertionPoint->SetPosition(PositionOfInsertionPointOnWindow());
       
  1346         }
       
  1347     }
       
  1348 
       
  1349 // ---------------------------------------------------------------------------
       
  1350 // CFepLayoutEditAreaBase::CancelPointerDownL
       
  1351 // Move the window
       
  1352 // (other items were commented in a header).
       
  1353 // ---------------------------------------------------------------------------
       
  1354 //                
       
  1355 EXPORT_C void CFepLayoutEditAreaBase::CancelPointerDownL()
       
  1356     {
       
  1357     if (PointerDown())
       
  1358         {
       
  1359         SetPointerDown(EFalse);
       
  1360         if(iDisplayTextCurSel.Length() && (iCursorVisible||iCursorSelVisible))
       
  1361             {
       
  1362             TRect rect = DrawSelection();
       
  1363             UpdateArea(rect,EFalse);
       
  1364             }
       
  1365         }
       
  1366     }
       
  1367 
       
  1368 // ---------------------------------------------------------------------------
       
  1369 // CFepLayoutEditAreaBase::HandlePointerLeave
       
  1370 // Handle pointer leave event
       
  1371 // ---------------------------------------------------------------------------
       
  1372 //           
       
  1373 EXPORT_C void CFepLayoutEditAreaBase::HandlePointerLeave(const TPoint& aPt)
       
  1374     {
       
  1375     /*if(iDisplayTextCurSel.Length())
       
  1376         {
       
  1377         TRect rect = DrawSelection();
       
  1378         UpdateArea(rect,ETrue);
       
  1379         iDisplayTextCurSel.SetSelection(0,0);                
       
  1380         iInsertionPoint->SetOn(ETrue);
       
  1381         }    */
       
  1382     if (iDimmed || !(iCursorVisible||iCursorSelVisible))
       
  1383         {
       
  1384         return;
       
  1385         }
       
  1386 
       
  1387     CFepUiBaseCtrl::HandlePointerLeave(aPt);
       
  1388     }
       
  1389 
       
  1390 
       
  1391 // ---------------------------------------------------------------------------
       
  1392 // CFepLayoutEditAreaBase::SetFocus
       
  1393 // Set editor focus
       
  1394 // ---------------------------------------------------------------------------
       
  1395 //           
       
  1396 EXPORT_C void CFepLayoutEditAreaBase::SetFocus(TBool aFlag)
       
  1397     {
       
  1398     if(aFlag == iHasFocus)
       
  1399         return;// do nothing is no status changed.
       
  1400     if(aFlag) //gain focus
       
  1401         {
       
  1402         PrepareForFocusGain();
       
  1403         }
       
  1404     else
       
  1405         {
       
  1406         PrepareForFocusLoss();
       
  1407         }
       
  1408     }
       
  1409 
       
  1410 // ---------------------------------------------------------------------------
       
  1411 // CFepLayoutEditAreaBase::AdjustSelectedCompositionText
       
  1412 // Adjust cursor
       
  1413 // ---------------------------------------------------------------------------
       
  1414 //           
       
  1415 EXPORT_C void CFepLayoutEditAreaBase::AdjustSelectedCompositionText(
       
  1416         TInt& aPositionOfInsertionPointInBuffer)
       
  1417     {
       
  1418     if(!iBuffer)
       
  1419         {
       
  1420         aPositionOfInsertionPointInBuffer = 0;
       
  1421         return;
       
  1422         } 
       
  1423     
       
  1424     if(aPositionOfInsertionPointInBuffer < 0)
       
  1425         {
       
  1426         aPositionOfInsertionPointInBuffer = 0;
       
  1427         } 
       
  1428     
       
  1429     if(aPositionOfInsertionPointInBuffer > iBuffer->Length())
       
  1430         {
       
  1431         aPositionOfInsertionPointInBuffer = iBuffer->Length();
       
  1432         }
       
  1433 
       
  1434     }
       
  1435 
       
  1436 // ---------------------------------------------------------------------------
       
  1437 // CFepLayoutEditAreaBase::OnDeActivate
       
  1438 // Response to layout de activation event
       
  1439 // (other items were commented in a header).
       
  1440 // ---------------------------------------------------------------------------
       
  1441 //     
       
  1442 EXPORT_C void CFepLayoutEditAreaBase::OnDeActivate()
       
  1443     {    
       
  1444     CFepUiBaseCtrl::OnDeActivate();
       
  1445     //cancel the insertion pointer timer
       
  1446     if(iHasFocus)
       
  1447         iInsertionPoint->SetOn(EFalse);
       
  1448     
       
  1449     DisableRegionUpdating(); //temp fix for font problem when layout is destroyed.
       
  1450     
       
  1451     }
       
  1452 
       
  1453 // ---------------------------------------------------------------------------
       
  1454 // CFepLayoutEditAreaBase::OnActivate
       
  1455 // Response to layout activation event
       
  1456 // (other items were commented in a header).
       
  1457 // ---------------------------------------------------------------------------
       
  1458 //     
       
  1459 EXPORT_C void CFepLayoutEditAreaBase::OnActivate()
       
  1460     {
       
  1461     CFepUiBaseCtrl::OnActivate();
       
  1462     EnableRegionUpdating();
       
  1463     //set cursor if needed    
       
  1464     //cancel the insertion pointer timer
       
  1465     iOverlappedCtrlList.Reset();
       
  1466     TBool on = iCursorVisible && iHasFocus;
       
  1467     //TBool on = iCursorVisible && iHasFocus && (iOverlappedCtrlList.Count() <= 0) ;
       
  1468     //iInsertionPoint->SetOn(iCursorVisible && iHasFocus);    
       
  1469     iInsertionPoint->SetOn(on);
       
  1470     
       
  1471     }
       
  1472         
       
  1473 // ---------------------------------------------------------------------------
       
  1474 // CFepLayoutEditAreaBase::SetCursorVisible
       
  1475 // Set Cursor Visible
       
  1476 // ---------------------------------------------------------------------------
       
  1477 //           
       
  1478 EXPORT_C void CFepLayoutEditAreaBase::SetCursorVisible(TBool aCursorVisibleFlag,
       
  1479                                                        TBool aCursorSelVisibleFlag)
       
  1480     {
       
  1481     if ( (iCursorVisible == aCursorVisibleFlag) && 
       
  1482          (iCursorSelVisible == aCursorSelVisibleFlag) )
       
  1483         {
       
  1484         return;
       
  1485         }
       
  1486         
       
  1487     iCursorVisible = aCursorVisibleFlag;
       
  1488     iCursorSelVisible = aCursorSelVisibleFlag;
       
  1489     iInsertionPoint->SetOn(iCursorVisible && iHasFocus);
       
  1490     }
       
  1491 
       
  1492 // ---------------------------------------------------------------------------
       
  1493 // CFepLayoutEditAreaBase::CursorVisible
       
  1494 // Get Cursor Visible
       
  1495 // ---------------------------------------------------------------------------
       
  1496 //           
       
  1497 EXPORT_C TBool CFepLayoutEditAreaBase::CursorVisible() const
       
  1498     {
       
  1499     return iCursorVisible;
       
  1500     }
       
  1501 
       
  1502 EXPORT_C void CFepLayoutEditAreaBase::SetDimmed(TBool aDimFlag)
       
  1503     {
       
  1504     iDimmed = aDimFlag;
       
  1505     }
       
  1506 
       
  1507 // ---------------------------------------------------------------------------
       
  1508 // CFepLayoutEditAreaBase::OnLayoutDraggingStart
       
  1509 // Response to layout dragging start event
       
  1510 // (other items were commented in a header).
       
  1511 // ---------------------------------------------------------------------------
       
  1512 //     
       
  1513 EXPORT_C void CFepLayoutEditAreaBase::OnLayoutDraggingStart()
       
  1514     {
       
  1515     iInDragging = ETrue;
       
  1516     }
       
  1517     
       
  1518 // ---------------------------------------------------------------------------
       
  1519 // CFepLayoutEditAreaBase::OnLayoutDraggingEnd
       
  1520 // Response to layout dragging end event
       
  1521 // (other items were commented in a header).
       
  1522 // ---------------------------------------------------------------------------
       
  1523 //     
       
  1524 EXPORT_C void CFepLayoutEditAreaBase::OnLayoutDraggingEnd()
       
  1525     {
       
  1526     iInDragging = EFalse;
       
  1527     Draw();
       
  1528     UpdateArea(Rect(),EFalse);
       
  1529     }    
       
  1530 
       
  1531 // ---------------------------------------------------------------------------
       
  1532 // CFepLayoutEditAreaBase::GetEditorFieldMaxLen
       
  1533 // Get the maximum length of the editor field.
       
  1534 // (other items were commented in a header).
       
  1535 // ---------------------------------------------------------------------------
       
  1536 //
       
  1537 EXPORT_C TInt CFepLayoutEditAreaBase::GetEditorFieldMaxLen()
       
  1538     {
       
  1539     TFontSpec curFont;
       
  1540     if( iFont )
       
  1541         {
       
  1542         _LIT(KMinChars, "..''");
       
  1543         return iCorrectedRect.Width() * KMinChars().Length()/ iFont->TextWidthInPixels(KMinChars); 
       
  1544         }
       
  1545     return iCorrectedRect.Width() / 2;
       
  1546     }
       
  1547 // ---------------------------------------------------------------------------
       
  1548 // CFepLayoutEditAreaBase::UpdateValidRegion
       
  1549 // Update valid region
       
  1550 // (other items were commented in a header).
       
  1551 // ---------------------------------------------------------------------------
       
  1552 //    
       
  1553 EXPORT_C void CFepLayoutEditAreaBase::UpdateValidRegion(CFepUiBaseCtrl* /*aCtrl*/,
       
  1554                                                             TBool /*aRemoveFlag*/)
       
  1555     {
       
  1556     return;
       
  1557     /*
       
  1558     //check whether overlaps with others, so as we can show/hide cursor
       
  1559     if(!iUpdateFlagEnabled || !aCtrl || aCtrl == this)
       
  1560         return; // do nothing if not overlapped with ICF cursor
       
  1561     TPoint curPt = PositionOfInsertionPointOnWindow();
       
  1562     if(aRemoveFlag) //a control is going to be hiden
       
  1563         {
       
  1564         RestoreCursorState(aCtrl);
       
  1565         }
       
  1566     else //a shown control changes rect
       
  1567         {
       
  1568         TInt idx = FindOverlappedCtrl(aCtrl);
       
  1569         if(idx != KErrNotFound)
       
  1570             {
       
  1571             if(aCtrl->Rect().Contains(curPt))
       
  1572                 return;
       
  1573             //restore
       
  1574             RestoreCursorState(idx);
       
  1575             }
       
  1576         else  //new control
       
  1577             {
       
  1578             if(!aCtrl->Rect().Contains(curPt))
       
  1579                 return;
       
  1580         
       
  1581             SaveCursorState(aCtrl);
       
  1582             //disable cursor
       
  1583             iInsertionPoint->SetOn(EFalse);            
       
  1584             }
       
  1585         }*/
       
  1586     }
       
  1587 
       
  1588 // ---------------------------------------------------------------------------
       
  1589 // CFepLayoutEditAreaBase::SaveCursorState
       
  1590 // Save current cursor state
       
  1591 // (other items were commented in a header).
       
  1592 // ---------------------------------------------------------------------------
       
  1593 //    
       
  1594 void CFepLayoutEditAreaBase::SaveCursorState(CFepUiBaseCtrl* aCtrl)    
       
  1595     {
       
  1596     for(TInt i = 0 ; i < iOverlappedCtrlList.Count(); ++i)
       
  1597         {        
       
  1598         if(aCtrl == iOverlappedCtrlList[i].iCtrl)
       
  1599             return;
       
  1600         }
       
  1601     TOverlappedInfo ctrlInfo={aCtrl,iInsertionPoint->IsOn()};
       
  1602     
       
  1603     iOverlappedCtrlList.Append(ctrlInfo);
       
  1604     }
       
  1605 
       
  1606 
       
  1607 // ---------------------------------------------------------------------------
       
  1608 // CFepLayoutEditAreaBase::RestoreCursorState
       
  1609 // Restore cusror state
       
  1610 // (other items were commented in a header).
       
  1611 // ---------------------------------------------------------------------------
       
  1612 //    
       
  1613 void CFepLayoutEditAreaBase::RestoreCursorState(TInt aIdx)    
       
  1614     {
       
  1615     //no need to check index. as it's checked before called.							   
       
  1616     iInsertionPoint->SetOn(iOverlappedCtrlList[aIdx].iCursorStateBeforeOverlapped);
       
  1617     iOverlappedCtrlList.Remove(aIdx);
       
  1618     }
       
  1619 
       
  1620     
       
  1621 // ---------------------------------------------------------------------------
       
  1622 // CFepLayoutEditAreaBase::RestoreCursorState
       
  1623 // Restore cusror state
       
  1624 // (other items were commented in a header).
       
  1625 // ---------------------------------------------------------------------------
       
  1626 //    
       
  1627 void CFepLayoutEditAreaBase::RestoreCursorState(CFepUiBaseCtrl* aCtrl)    
       
  1628     {
       
  1629     //need check whether that control previously overlapped with ICF    
       
  1630     TInt idx = FindOverlappedCtrl(aCtrl);
       
  1631     if(idx != KErrNotFound)
       
  1632         RestoreCursorState(idx);
       
  1633     }
       
  1634     
       
  1635     
       
  1636 TInt CFepLayoutEditAreaBase::FindOverlappedCtrl(CFepUiBaseCtrl* aCtrl) 
       
  1637     {
       
  1638     for(TInt i = 0 ; i < iOverlappedCtrlList.Count(); ++i)
       
  1639         {        
       
  1640         if(aCtrl == iOverlappedCtrlList[i].iCtrl)
       
  1641             return i;
       
  1642         }
       
  1643     return KErrNotFound;
       
  1644     }
       
  1645 
       
  1646 
       
  1647 TInt CFepLayoutEditAreaBase::TextWidth( TInt aStart, TInt aEnd )
       
  1648     {
       
  1649     CFont::TMeasureTextInput input;
       
  1650     input.iStartInputChar = aStart;
       
  1651     input.iEndInputChar = aEnd;
       
  1652     input.iFlags = 0;
       
  1653     CFbsFont::TMeasureTextOutput output;
       
  1654     TInt width = iFont->MeasureText(iDisplayText, &input, &output);
       
  1655     return width;
       
  1656     }
       
  1657 
       
  1658 //  End of File