idlehomescreen/xmluirendering/renderingplugins/xntexteditorfactory/src/xntexteditoradapter.cpp
changeset 0 f72a12da539e
child 9 f966699dea19
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     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 wrapper for CEikEdwin
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <e32base.h>
       
    20 #include <e32const.h>
       
    21 #include <e32property.h>
       
    22 
       
    23 #include <eikedwin.h>
       
    24 #include <AknUtils.h>
       
    25 #include <AknsUtils.h>
       
    26 #include <aknview.h>
       
    27 #include <aknedsts.h>
       
    28 #include <txtglobl.h>
       
    29 #include <txtfmlyr.h>
       
    30 #include <txtfrmat.h>
       
    31 #include <txtrich.h>
       
    32 #include <gdi.h>
       
    33 
       
    34 #include <activeidle2domainpskeys.h>
       
    35 
       
    36 // User includes
       
    37 #include "xnappuiadapter.h"
       
    38 #include "xndompropertyvalue.h"
       
    39 #include "xndomlist.h"
       
    40 #include "xndomproperty.h"
       
    41 #include "xnproperty.h"
       
    42 #include "xnnodepluginif.h"
       
    43 #include "xncontroladapter.h"
       
    44 #include "xntexteditoradapter.h"
       
    45 #include "xntexteditorpublisher.h"
       
    46 #include "xnnodepluginif.h"
       
    47 #include "xneditmode.h"
       
    48 #include "c_xnutils.h"
       
    49 
       
    50 const TInt KMaxLength = 100;
       
    51 
       
    52 _LIT8( KCpsPublishing, "cpspublishing" );
       
    53 _LIT8( KMaxLineAmount, "max-line-amount" );
       
    54 _LIT8( KMaxCharAmount, "max-char-amount" );
       
    55 
       
    56 _LIT( KEnterChar, "\x2029" );
       
    57 
       
    58 // Local macros
       
    59 #define IS_ARROW_KEY( k ) \
       
    60     ( k == EStdKeyLeftArrow || k == EStdKeyRightArrow || \
       
    61       k == EStdKeyUpArrow || k == EStdKeyDownArrow ) 
       
    62 
       
    63 // ============================ LOCAL FUNCTIONS ================================   
       
    64 
       
    65 // ============================ MEMBER FUNCTIONS ===============================
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CXnTextEditorAdapter::NewL
       
    69 // Symbian static 1st phase constructor
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 CXnTextEditorAdapter* CXnTextEditorAdapter::NewL( CXnControlAdapter* aParent, 
       
    73     CXnNodePluginIf& aNode )
       
    74     {
       
    75 	CXnTextEditorAdapter* self = 
       
    76         new( ELeave ) CXnTextEditorAdapter( aParent, aNode );
       
    77 
       
    78     CleanupStack::PushL( self );
       
    79     self->ConstructL();
       
    80     CleanupStack::Pop( self );
       
    81 
       
    82     return self;	
       
    83     }
       
    84     
       
    85 // -----------------------------------------------------------------------------
       
    86 // CXnTextEditorAdapter::CXnTextEditorAdapter
       
    87 // C++ default constructor
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CXnTextEditorAdapter::CXnTextEditorAdapter( CXnControlAdapter* aParent, 
       
    91     CXnNodePluginIf& aNode )
       
    92     : iParent( aParent ), iNode( aNode )
       
    93     {
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CXnTextEditorAdapter::~CXnTextEditorAdapter
       
    98 // C++ destructor
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CXnTextEditorAdapter::~CXnTextEditorAdapter()
       
   102     {    
       
   103     if ( iAvkonAppUi )
       
   104         {
       
   105         iAvkonAppUi->RemoveFromStack( iEditor );
       
   106         }
       
   107     
       
   108     if( iFont && iReleaseFont )
       
   109         {
       
   110         CWsScreenDevice* dev( iCoeEnv->ScreenDevice() );
       
   111         dev->ReleaseFont( iFont );
       
   112         }
       
   113 
       
   114     delete iEditorPublisher;
       
   115     delete iEditor;   
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CXnTextEditorAdapter::ConstructL
       
   120 // Symbian 2nd phase constructor can leave.
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 void CXnTextEditorAdapter::ConstructL()
       
   124     {
       
   125     CXnControlAdapter::ConstructL( iNode );    
       
   126     iUiEngine = iNode.UiEngineL();
       
   127 
       
   128     // Max line amount
       
   129     iMaxLines = 0;
       
   130     
       
   131     CXnProperty* maxlinesProp( iNode.GetPropertyL( KMaxLineAmount ) );
       
   132         
       
   133     if ( maxlinesProp )
       
   134         {
       
   135         iMaxLines = maxlinesProp->FloatValueL();
       
   136         }
       
   137 
       
   138     // Max char amount
       
   139     TInt maxChars( KMaxLength );
       
   140     
       
   141     CXnProperty* maxcharsProp( iNode.GetPropertyL( KMaxCharAmount ) );    
       
   142     
       
   143     if ( maxcharsProp )
       
   144         {
       
   145         maxChars = maxcharsProp->FloatValueL();
       
   146         }
       
   147     
       
   148     iEditor = new ( ELeave ) CEikEdwin;
       
   149 
       
   150     iEditor->SetContainerWindowL( *iParent );
       
   151     iEditor->ConstructL( 0, maxChars, maxChars, iMaxLines );
       
   152     
       
   153     // Set allowed input modes
       
   154     iEditor->SetAknEditorAllowedInputModes( EAknEditorAllInputModes );
       
   155 
       
   156     // Set the default input mode
       
   157     iEditor->SetAknEditorInputMode( 
       
   158         EAknEditorTextInputMode | EAknEditorNumericInputMode );
       
   159                                     
       
   160     // Set allowed case modes
       
   161     iEditor->SetAknEditorPermittedCaseModes( EAknEditorAllCaseModes );
       
   162 
       
   163     // Set the default case mode
       
   164     iEditor->SetAknEditorCase( EAknEditorTextCase );
       
   165         
       
   166     iEditor->AddFlagToUserFlags( 
       
   167         CEikEdwin::ENoAutoSelection | CEikEdwin::EAllowUndo );
       
   168                                      
       
   169     // Set numeric keymap
       
   170     iEditor->SetAknEditorNumericKeymap( EAknEditorPlainNumberModeKeymap );
       
   171     
       
   172     iEditor->SetSuppressBackgroundDrawing( ETrue );
       
   173                       
       
   174     iEditor->SetObserver( this );
       
   175                                         
       
   176     // Default not focused                                  
       
   177     iEditor->SetFocus( EFalse );                              
       
   178         
       
   179     SetPropertiesL();
       
   180 	}
       
   181  
       
   182 // -----------------------------------------------------------------------------
       
   183 // CXnTextEditorAdapter::CountComponentControls
       
   184 // (other items were commented in a header).
       
   185 // -----------------------------------------------------------------------------
       
   186 //  
       
   187 TInt CXnTextEditorAdapter::CountComponentControls() const
       
   188     {
       
   189     if( iEditor )
       
   190         {
       
   191         return 1;
       
   192         }
       
   193         
       
   194     return 0;        
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CXnTextEditorAdapter::ComponentControl
       
   199 // (other items were commented in a header).
       
   200 // -----------------------------------------------------------------------------
       
   201 // 
       
   202 CCoeControl* CXnTextEditorAdapter::ComponentControl( TInt aIndex ) const
       
   203     {
       
   204     if( aIndex == 0 )
       
   205         {
       
   206         return iEditor;
       
   207         }
       
   208         
       
   209     return NULL;        
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CXnTextEditorAdapter::SizeChanged
       
   214 // (other items were commented in a header).
       
   215 // -----------------------------------------------------------------------------
       
   216 // 
       
   217 void CXnTextEditorAdapter::SizeChanged()
       
   218     {        
       
   219     iEditor->SetRect( iNode.Rect() );  
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CXnTextEditorAdapter::OfferKeyEventL
       
   224 // (other items were commented in a header).
       
   225 // -----------------------------------------------------------------------------
       
   226 // 
       
   227 TKeyResponse CXnTextEditorAdapter::OfferKeyEventL( const TKeyEvent& aKeyEvent,
       
   228     TEventCode aType )
       
   229     {
       
   230     TKeyResponse ret( EKeyWasNotConsumed );
       
   231 
       
   232     if ( !IsFocused() )
       
   233         {
       
   234         return ret;
       
   235         }
       
   236 
       
   237     ret = iEditor->OfferKeyEventL( aKeyEvent, aType );                    
       
   238 
       
   239     if ( IS_ARROW_KEY( aKeyEvent.iScanCode ) )    
       
   240         {                        
       
   241         if ( ret == EKeyWasNotConsumed && aType == EEventKey )
       
   242             {            
       
   243             iRefusesFocusLoss = EFalse;
       
   244             
       
   245             ret = CXnControlAdapter::OfferKeyEventL( aKeyEvent, aType );
       
   246             }                    
       
   247         }
       
   248 
       
   249     if ( aKeyEvent.iCode == EKeyEnter )
       
   250         {
       
   251         if ( iEditorPublisher && iMaxLines == 1 )
       
   252             {
       
   253             TInt length( iEditor->TextLength() );
       
   254             
       
   255             HBufC* content = HBufC::NewLC( length + 1 );
       
   256             TPtr ptr( content->Des() );
       
   257             
       
   258             iEditor->GetText( ptr );
       
   259             
       
   260             ptr.Append( KEnterChar );
       
   261             
       
   262             iEditorPublisher->PublishTextL( *content );
       
   263             
       
   264             CleanupStack::PopAndDestroy( content );
       
   265             }
       
   266         }
       
   267 
       
   268     return ret;        
       
   269     }
       
   270  
       
   271 // -----------------------------------------------------------------------------
       
   272 // CXnTextEditorAdapter::RefusesFocusLoss
       
   273 // (other items were commented in a header).
       
   274 // -----------------------------------------------------------------------------
       
   275 // 
       
   276 TBool CXnTextEditorAdapter::RefusesFocusLoss() const
       
   277     {    
       
   278     return iRefusesFocusLoss;
       
   279     }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CXnTextEditorAdapter::FocusChanged
       
   283 // (other items were commented in a header).
       
   284 // -----------------------------------------------------------------------------
       
   285 // 
       
   286 void CXnTextEditorAdapter::FocusChanged( TDrawNow aDrawNow )
       
   287     {
       
   288     CXnAppUiAdapter* appui( 
       
   289         static_cast< CXnAppUiAdapter* >( iAvkonAppUi ) );
       
   290     
       
   291     TBool isFocused( IsFocused() ? ETrue : EFalse );
       
   292       
       
   293     TInt value;
       
   294 
       
   295     if ( isFocused )
       
   296         {      
       
   297         value = EPSAiDontForwardNumericKeysToPhone;
       
   298                        
       
   299         TRAP_IGNORE( appui->AddToStackL( appui->View(), iEditor ) );
       
   300         
       
   301         // AddToStackL calls iEditor->SetFocus( ETrue ); 
       
   302         }
       
   303     else
       
   304         {
       
   305         value = EPSAiForwardNumericKeysToPhone;
       
   306                                       
       
   307         appui->RemoveFromStack( iEditor );
       
   308         
       
   309         iEditor->SetFocus( EFalse, aDrawNow );
       
   310         }
       
   311     
       
   312     iRefusesFocusLoss = isFocused;
       
   313     
       
   314     RProperty::Set( KPSUidAiInformation,            
       
   315                     KActiveIdleForwardNumericKeysToPhone,
       
   316                     value );    
       
   317     }
       
   318     
       
   319 // -----------------------------------------------------------------------------
       
   320 // CXnTextEditorAdapter::Draw
       
   321 // Draws the editor component
       
   322 // -----------------------------------------------------------------------------
       
   323 //
       
   324 void CXnTextEditorAdapter::Draw( const TRect& aRect ) const
       
   325     {                           
       
   326     CXnControlAdapter::Draw( aRect );
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CXnTextEditorAdapter::HandleControlEventL
       
   331 // 
       
   332 // -----------------------------------------------------------------------------
       
   333 //    
       
   334 void CXnTextEditorAdapter::HandleControlEventL( CCoeControl* aControl, 
       
   335     TCoeEvent aEventType )
       
   336     {
       
   337     if ( aControl == iEditor )
       
   338         {
       
   339         // If background drawing is suppressed, then we need to call draw here
       
   340         if ( aEventType == EEventStateChanged )
       
   341             {
       
   342             DrawNow();                                    
       
   343             }        
       
   344         }
       
   345     }
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CXnTextEditorAdapter::SetTextL
       
   349 // Sets the new content to the underlying CEikEdwin
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 void CXnTextEditorAdapter::SetTextL( const TDesC& aText )
       
   353     {
       
   354     iEditor->SetTextL( &aText );
       
   355     }
       
   356     
       
   357 // -----------------------------------------------------------------------------
       
   358 // CXnTextEditorAdapter::Text
       
   359 // Returns the text contained in the underlying CEikEdwin
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 HBufC* CXnTextEditorAdapter::Text() const
       
   363     {
       
   364     HBufC* text( NULL );
       
   365     
       
   366     TRAP_IGNORE( text = iEditor->GetTextInHBufL() );
       
   367     
       
   368     // Ownership is transfered to the calller
       
   369     return text;
       
   370     }
       
   371 
       
   372 // -----------------------------------------------------------------------------
       
   373 // CXnTextEditorAdapter::SetPropertiesL
       
   374 // Sets text properties
       
   375 // -----------------------------------------------------------------------------
       
   376 //
       
   377 void CXnTextEditorAdapter::SetPropertiesL()
       
   378     {    
       
   379     // Whether to publish text to CPS
       
   380     CXnProperty* cpspublishingProp( iNode.GetPropertyL( KCpsPublishing ) );
       
   381     CXnProperty* idProp( iNode.IdL() );
       
   382     
       
   383     if ( cpspublishingProp && idProp && 
       
   384         cpspublishingProp->StringValue() == XnPropertyNames::KTrue )
       
   385         {
       
   386         iEditorPublisher = CXnTextEditorPublisher::NewL( 
       
   387                 *this, idProp->StringValue() );
       
   388         }    
       
   389 
       
   390     // Store current state
       
   391     if ( iFont && iReleaseFont )
       
   392         {
       
   393         CWsScreenDevice* dev( iCoeEnv->ScreenDevice() );
       
   394         dev->ReleaseFont( iFont ); 
       
   395         iFont = NULL;
       
   396         }
       
   397     
       
   398     // Get new font
       
   399     CXnUtils::CreateFontL( iNode, iFont, iReleaseFont );
       
   400     
       
   401     // And set font
       
   402     TCharFormat cf;
       
   403     TCharFormatMask cfm;
       
   404     cfm.SetAttrib( EAttColor );
       
   405     cfm.SetAttrib( EAttFontTypeface );
       
   406     cfm.SetAttrib( EAttFontHeight );
       
   407     cfm.SetAttrib( EAttFontPosture );
       
   408     cfm.SetAttrib( EAttFontStrokeWeight );
       
   409     cfm.SetAttrib( EAttLineSpacing );
       
   410     cfm.SetAttrib( EAttLineSpacingControl );
       
   411     cf.iFontSpec = iFont->FontSpecInTwips();
       
   412 
       
   413     // Set linespacing    
       
   414     CXnProperty* lineSpaceProp( 
       
   415         iNode.GetPropertyL( XnPropertyNames::appearance::common::KFontLineSpace ) );
       
   416         
       
   417     if ( lineSpaceProp )
       
   418         {
       
   419         TInt lineSpace = 
       
   420             iUiEngine->VerticalPixelValueL( lineSpaceProp, iNode.Rect().Height() );
       
   421         
       
   422         CParaFormatLayer*pFormatLayer = CEikonEnv::NewDefaultParaFormatLayerL();
       
   423         CleanupStack::PushL(pFormatLayer);
       
   424         CParaFormat* paraformat = CParaFormat::NewLC(); 
       
   425         TParaFormatMask paraFormatMask; 
       
   426         paraFormatMask.SetAttrib(EAttLineSpacing);
       
   427         paraFormatMask.SetAttrib(EAttLineSpacingControl);
       
   428         paraformat->iLineSpacingControl = CParaFormat::ELineSpacingExactlyInTwips;
       
   429         CGraphicsDevice* screenDevice = iEikonEnv->ScreenDevice();
       
   430         TInt lineHeight = screenDevice->VerticalPixelsToTwips( iFont->HeightInPixels() + lineSpace );
       
   431         paraformat->iLineSpacingInTwips = lineHeight;
       
   432         pFormatLayer->SetL(paraformat, paraFormatMask);
       
   433         iEditor->SetParaFormatLayer(pFormatLayer); // Edwin takes the ownership
       
   434         CleanupStack::PopAndDestroy(paraformat);
       
   435         CleanupStack::Pop(pFormatLayer); 
       
   436         }
       
   437  
       
   438     // Get text color and set it
       
   439     TRgb textColor;
       
   440     CXnProperty* colorProperty( 
       
   441         iNode.GetPropertyL( XnPropertyNames::appearance::common::KColor ) );
       
   442     
       
   443     if ( colorProperty )
       
   444         {
       
   445         CXnDomProperty* domProperty( colorProperty->Property() );
       
   446         
       
   447         if ( domProperty )
       
   448             {
       
   449             TInt error( KErrNotSupported );
       
   450             
       
   451             CXnDomPropertyValue* value = static_cast< CXnDomPropertyValue* >( 
       
   452                 domProperty->PropertyValueList().Item( 0 ) );
       
   453 
       
   454             if ( value->IsAutoIdent() )
       
   455                 {
       
   456                 MAknsSkinInstance* skinInstance = AknsUtils::SkinInstance();
       
   457                 error = AknsUtils::GetCachedColor(skinInstance, textColor, KAknsIIDQsnTextColors,
       
   458                 EAknsCIQsnTextColorsCG6);
       
   459                 }
       
   460             else if ( value->PrimitiveValueType() == CXnDomPropertyValue::ERgbColor )
       
   461                 {
       
   462                 textColor = value->RgbColorValueL();
       
   463                 error = KErrNone;
       
   464                 }
       
   465             else
       
   466                 {
       
   467                 HBufC* colorString = colorProperty->StringValueL();
       
   468                 CleanupStack::PushL( colorString );
       
   469                 CXnUtils::StripQuotes( colorString );
       
   470                 TInt index = 0;
       
   471                 TAknsItemID skinID;
       
   472                 TBool idResolved = CXnUtils::ResolveSkinItemIDL( colorString->Des(), skinID, index );
       
   473                 
       
   474                 if ( idResolved )
       
   475                     {
       
   476                     MAknsSkinInstance* skinInstance = AknsUtils::SkinInstance();
       
   477                     error = AknsUtils::GetCachedColor( skinInstance, textColor, skinID, index );
       
   478                     }
       
   479                 else // use auto value if skin id is invalid.
       
   480                     {
       
   481                     MAknsSkinInstance* skinInstance = AknsUtils::SkinInstance();
       
   482                     error = AknsUtils::GetCachedColor(skinInstance, textColor, KAknsIIDQsnTextColors,
       
   483                     EAknsCIQsnTextColorsCG6);
       
   484                     }
       
   485                 
       
   486                 CleanupStack::PopAndDestroy( colorString );                 
       
   487                 }
       
   488             
       
   489             if ( error == KErrNone )
       
   490                 {
       
   491                 cf.iFontPresentation.iTextColor = textColor;
       
   492                 }
       
   493             else
       
   494                 {
       
   495                 cf.iFontPresentation.iTextColor = KRgbBlack;                
       
   496                 }
       
   497             }
       
   498         }    
       
   499     
       
   500     CCharFormatLayer *pCharFL = CCharFormatLayer::NewL(cf,cfm);
       
   501     iEditor->SetCharFormatLayer(pCharFL);
       
   502     iEditor->SetTextBaselineSpacing( 2 );
       
   503     }
       
   504 
       
   505 // End of file