meetingrequest/mrgui/mrfieldbuildercommon/src/cesmreditor.cpp
changeset 0 8466d47a6819
child 16 4ce476e64c59
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  ESMR editor impl.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "emailtrace.h"
       
    20 #include <txtfrmat.h>
       
    21 #include <txtglobl.h>
       
    22 #include <txtrich.h>
       
    23 #include <gulfont.h>
       
    24 #include <eikenv.h>
       
    25 #include <avkon.hrh>
       
    26 
       
    27 #include "cesmreditor.h"
       
    28 #include "cesmrlayoutmgr.h"
       
    29 #include "esmrfieldbuilderdef.h"
       
    30 
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // CESMREditor::NewL
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 EXPORT_C CESMREditor* CESMREditor::NewL( )
       
    38     {
       
    39     FUNC_LOG;
       
    40     /*
       
    41      Default parameters for edwin. Note that as the parent
       
    42      is NULL, the edwin MUST be EOwnsWindow-type edwin.
       
    43      */
       
    44     return CESMREditor::NewL (
       
    45 		NULL, // parent
       
    46 		1, // number of lines
       
    47 		KTextLimit, // text limit
       
    48 		CEikEdwin::EResizable | CEikEdwin::EOwnsWindow );
       
    49     }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // CESMREditor::NewL
       
    53 // ---------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C CESMREditor* CESMREditor::NewL(
       
    56 		const CCoeControl* aParent, TInt aNumberOfLines,
       
    57         TInt aTextLimit, TInt aEdwinFlags )
       
    58     {
       
    59     FUNC_LOG;
       
    60     CESMREditor* self = new (ELeave) CESMREditor();
       
    61     CleanupStack::PushL ( self );
       
    62     self->ConstructL ( aParent, aNumberOfLines, aTextLimit, aEdwinFlags );
       
    63     CleanupStack::Pop ( self );
       
    64     return self;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CESMREditor::~CESMREditor
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 EXPORT_C CESMREditor::~CESMREditor( )
       
    72     {
       
    73     FUNC_LOG;
       
    74     delete iDefaultText;
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // CESMREditor::PositionChanged
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C void CESMREditor::PositionChanged( )
       
    82     {
       
    83     FUNC_LOG;
       
    84     CTextView* view = TextView();
       
    85     if ( view )
       
    86         {
       
    87         view->SetViewRect ( Rect() );
       
    88         }
       
    89     }
       
    90 
       
    91 // ---------------------------------------------------------------------------
       
    92 // CESMREditor::OfferKeyEventL
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C TKeyResponse CESMREditor::OfferKeyEventL(
       
    96 		const TKeyEvent& aKeyEvent,TEventCode aType )
       
    97     {
       
    98     FUNC_LOG;
       
    99 
       
   100     TKeyResponse ret = CEikRichTextEditor::OfferKeyEventL(aKeyEvent, aType);
       
   101 
       
   102     if ( aType == EEventKey )
       
   103         {
       
   104         TPoint firstRowExtra(0,RowHeight());
       
   105         TextLayout()->DocPosToXyPosL( 0, firstRowExtra);
       
   106         //firstRowExtra.iY
       
   107         TPoint rel(PositionRelativeToScreen());
       
   108         TPoint point(0,0);
       
   109         TextLayout()->DocPosToXyPosL( CursorPos(), point);
       
   110 
       
   111         // +1 because first line is zero
       
   112         TInt currentLineNumber = TextLayout()->GetLineNumber( CursorPos() ) + 1;
       
   113         TInt cursorLowerPosition = currentLineNumber * RowHeight();
       
   114         TInt cursorUpperPosition = ( currentLineNumber -1 )* RowHeight();
       
   115 
       
   116         if ( aKeyEvent.iCode == EKeyDownArrow && iObserver)
       
   117             {
       
   118             TPoint firstRowExtra(0,RowHeight());
       
   119             TextLayout()->DocPosToXyPosL( 0, firstRowExtra);
       
   120             TPoint cursorPos(0,0);
       
   121             TextLayout()->DocPosToXyPosL( CursorPos(), cursorPos);
       
   122             TInt edwinTlY = Position().iY;
       
   123             TInt listHeight = iObserver->ListHeight();
       
   124 
       
   125             // edwinTlY negative
       
   126             if ( cursorLowerPosition > ( listHeight - edwinTlY )) 
       
   127                 {
       
   128                 iObserver->MoveListAreaUpL(RowHeight());
       
   129                 }
       
   130             }
       
   131         else if ( aKeyEvent.iCode == EKeyUpArrow && iObserver)
       
   132             {
       
   133             TPoint firstRowExtra(0,RowHeight());
       
   134             TextLayout()->DocPosToXyPosL( 0, firstRowExtra);
       
   135             TPoint cursorPos(0,0);
       
   136             TextLayout()->DocPosToXyPosL( CursorPos(), cursorPos);
       
   137             TInt hiddenHeight = Position().iY;
       
   138             // upper corner of field not visible
       
   139             if ( hiddenHeight < 0 )
       
   140                 {
       
   141                 if ( (cursorPos.iY - firstRowExtra.iY * 2) < (-hiddenHeight))
       
   142                     {
       
   143                     iObserver->MoveListAreaDownL(RowHeight());
       
   144                     }
       
   145                 }
       
   146             }
       
   147         }
       
   148 
       
   149 
       
   150     return ret;
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // CESMREditor::ClearSelectionAndSetTextL
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C void CESMREditor::FocusChanged(TDrawNow aDrawNow)
       
   158     {
       
   159     FUNC_LOG;
       
   160     CEikRichTextEditor::FocusChanged( aDrawNow );
       
   161     TRAPD( error, TryToSetSelectionL() );
       
   162     if ( error != KErrNone )
       
   163         {
       
   164         CEikonEnv::Static()->HandleError( error );// codescanner::eikonenvstatic
       
   165         }
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CESMREditor::TryToSetSelectionL
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 void CESMREditor::TryToSetSelectionL()
       
   173     {
       
   174     FUNC_LOG;
       
   175     if ( IsFocused() )
       
   176         {
       
   177         // set selection to text if it is the default text
       
   178         HBufC* text = GetTextInHBufL();
       
   179         if ( text )
       
   180             {
       
   181             CleanupStack::PushL( text );
       
   182             if ( iDefaultText->Compare( *text ) == KErrNone )
       
   183                 {
       
   184                 SetSelectionL(0, text->Length() );
       
   185                 }
       
   186             CleanupStack::PopAndDestroy( text );
       
   187             }
       
   188         }
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CESMREditor::HandleEdwinEventL
       
   193 // ---------------------------------------------------------------------------
       
   194 //
       
   195 EXPORT_C void CESMREditor::HandleEdwinEventL(
       
   196 		CEikEdwin* aEdwin,TEdwinEvent aEventType )
       
   197     {
       
   198     FUNC_LOG;
       
   199     if ( aEdwin == this && aEventType == EEventNavigation && iObserver)
       
   200         {
       
   201         TInt curPos = CursorPos ();
       
   202         TInt textLength = TextLength();
       
   203         // Update viewarea scrolling in cases where the cursor location 
       
   204         // is changed directly from bottom to upmost position or vise versa.
       
   205         if ( CursorPos () == TextLength() )
       
   206             {
       
   207             // KErrNotFound as field id is interpret as focused field:
       
   208             iObserver->ScrollItemVisible( KErrNotFound );
       
   209             }
       
   210         if ( CursorPos () == 0 )
       
   211             {
       
   212             // KErrNotFound as field id is interpret as focused field:
       
   213             iObserver->ScrollItemVisible( KErrNotFound );
       
   214             }
       
   215         }
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------------------------
       
   219 // CESMREditor::ClearSelectionAndSetTextL
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 EXPORT_C void CESMREditor::ClearSelectionAndSetTextL( const TDesC& aText )
       
   223     {
       
   224     FUNC_LOG;
       
   225     ClearSelectionL();
       
   226     SetCursorPosL(0, EFalse );
       
   227     SetTextL( &aText );
       
   228     SetCursorPosL(0, EFalse );
       
   229     HandleTextChangedL();
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // CESMREditor::SetFontL
       
   234 // ---------------------------------------------------------------------------
       
   235 //
       
   236 EXPORT_C void CESMREditor::SetFontL( 
       
   237 		const CFont* aFont, CESMRLayoutManager* aLayout )
       
   238     {
       
   239     FUNC_LOG;
       
   240     const CFont* font = aFont;
       
   241     TFontSpec fontSpec = font->FontSpecInTwips();
       
   242 
       
   243     CParaFormat* paraFormat = CParaFormat::NewLC();
       
   244     TParaFormatMask paraFormatMask;
       
   245     paraFormat->iLineSpacingControl = CParaFormat::ELineSpacingExactlyInPixels;
       
   246 
       
   247     paraFormatMask.SetAttrib( EAttLineSpacing );
       
   248     paraFormat->iHorizontalAlignment = CParaFormat::ELeftAlign;
       
   249     paraFormatMask.SetAttrib( EAttAlignment );
       
   250 
       
   251     TCharFormat charFormat;
       
   252     TCharFormatMask formatMask;
       
   253     charFormat.iFontSpec = fontSpec;
       
   254 
       
   255     formatMask.SetAttrib( EAttFontTypeface );
       
   256     formatMask.SetAttrib( EAttFontHeight );
       
   257     formatMask.SetAttrib( EAttFontPosture );
       
   258     formatMask.SetAttrib( EAttFontStrokeWeight );
       
   259 
       
   260     charFormat.iFontPresentation.iTextColor = aLayout->NormalTextColor();
       
   261     formatMask.SetAttrib( EAttColor );
       
   262 
       
   263     CParaFormatLayer* paraFormatLayer =
       
   264         CParaFormatLayer::NewL( paraFormat, paraFormatMask );
       
   265     CleanupStack::PushL( paraFormatLayer );
       
   266     CCharFormatLayer* charFormatLayer =
       
   267         CCharFormatLayer::NewL( charFormat, formatMask );
       
   268 
       
   269     SetParaFormatLayer( paraFormatLayer );
       
   270     CleanupStack::Pop( paraFormatLayer );
       
   271     CleanupStack::PopAndDestroy( paraFormat );
       
   272     SetCharFormatLayer( charFormatLayer );
       
   273     
       
   274     // This forces CEikEdwin::OnReformatL to notify observer
       
   275     // through HandleEdwinSizeEventL about changed size.
       
   276     // It is notified only if linecount changes.
       
   277     CEikEdwin::iNumberOfLines = 0;
       
   278     CEikEdwin::FormatTextL();
       
   279     }
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CESMREditor::CursorLinePos
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 EXPORT_C TInt CESMREditor::CursorLinePos( ) const
       
   286     {
       
   287     FUNC_LOG;
       
   288     return TextLayout()->GetLineNumber ( CursorPos ( ) );
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // CESMREditor::LineCount
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 EXPORT_C TInt CESMREditor::LineCount( ) const
       
   296     {
       
   297     FUNC_LOG;
       
   298     return TextLayout()->GetLineNumber ( TextLength ( ) );
       
   299     }
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // CESMREditor::RowHeight
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 EXPORT_C TInt CESMREditor::RowHeight()
       
   306     {
       
   307     FUNC_LOG;
       
   308     TInt bandHeight = TextLayout()->BandHeight();
       
   309     // Starts from zero, so let's increase the count by one.
       
   310     TInt lineNumberCount = TextLayout()->GetLineNumber(TextLength()) + 1;
       
   311 
       
   312     return ( bandHeight / lineNumberCount );
       
   313     }
       
   314 
       
   315 // ---------------------------------------------------------------------------
       
   316 // CESMREditor::CurrentLineNumber
       
   317 // ---------------------------------------------------------------------------
       
   318 //
       
   319 EXPORT_C TInt CESMREditor::CurrentLineNumber()
       
   320     {
       
   321     FUNC_LOG;
       
   322     // first line is zero, let's increase it by one
       
   323     return TextLayout()->GetLineNumber( CursorPos() ) + 1;
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // CESMREditor::LineCount
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 EXPORT_C  void CESMREditor::SetDefaultTextL(
       
   331         HBufC* aDefaultText ) // codescanner::nonconsthbufc
       
   332     {
       
   333     FUNC_LOG;
       
   334     SetTextL( aDefaultText );
       
   335     
       
   336     if ( iDefaultText )
       
   337         {
       
   338         delete iDefaultText;
       
   339         iDefaultText = NULL;
       
   340         }
       
   341     iDefaultText = aDefaultText;
       
   342     }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // CESMREditor::LineCount
       
   346 // ---------------------------------------------------------------------------
       
   347 //
       
   348 EXPORT_C const TDesC& CESMREditor::DefaultText()
       
   349     {
       
   350     FUNC_LOG;
       
   351     return *iDefaultText;
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // CESMREditor::LineCount
       
   356 // ---------------------------------------------------------------------------
       
   357 //
       
   358 EXPORT_C void CESMREditor::SetListObserver( MESMRListObserver* aObserver )
       
   359     {
       
   360     FUNC_LOG;
       
   361     iObserver = aObserver;
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // CESMREditor::CESMREditor
       
   366 // ---------------------------------------------------------------------------
       
   367 //
       
   368 CESMREditor::CESMREditor( )
       
   369     {
       
   370     FUNC_LOG;
       
   371     // Do nothing
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // CESMREditor::ConstructL
       
   376 // ---------------------------------------------------------------------------
       
   377 //
       
   378 void CESMREditor::ConstructL(const CCoeControl* aParent, TInt aNumberOfLines,
       
   379         TInt aTextLimit, TInt aEdwinFlags )
       
   380     {
       
   381     FUNC_LOG;
       
   382     CEikRichTextEditor::ConstructL (aParent, aNumberOfLines, aTextLimit,
       
   383             aEdwinFlags );
       
   384 
       
   385     AddEdwinObserverL(this);
       
   386 
       
   387     SetUpperFullFormattingLength( aTextLimit );
       
   388 
       
   389     iLimitLength = aTextLimit;
       
   390     
       
   391     SetAlignment ( CESMRLayoutManager::IsMirrored ( ) ? EAknEditorAlignRight
       
   392             : EAknEditorAlignLeft );
       
   393     SetHighlightStyleL ( EEikEdwinHighlightLink );
       
   394     }
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // CESMREditor::GetLimitLength
       
   398 // ---------------------------------------------------------------------------
       
   399 //
       
   400 EXPORT_C TInt CESMREditor::GetLimitLength() const
       
   401 	{
       
   402     FUNC_LOG;
       
   403 	return iLimitLength;
       
   404 	}
       
   405 
       
   406 //EOF