textinput/peninputhwrtrui/src/truicontainerbase.cpp
changeset 27 694fa80c203c
parent 24 fc42a86c98e3
child 35 0f326f2e628e
equal deleted inserted replaced
24:fc42a86c98e3 27:694fa80c203c
     1 /*
       
     2 * Copyright (c) 2007 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:  Implement of class CTruiContainerBase
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <AknDef.h>
       
    20 #include <aknbutton.h>
       
    21 #include <barsread.h>
       
    22 #include <akntoolbar.h>
       
    23 #include <e32property.h>
       
    24 #include <AvkonInternalCRKeys.h>
       
    25 #include <eikapp.h>
       
    26 #include <AknsUtils.h>
       
    27 #include <aknlayoutscalable_apps.cdl.h>
       
    28 #include <layoutmetadata.cdl.h>
       
    29 #include <gulicon.h>
       
    30 #include <aknview.h>
       
    31 #include <s32stor.h>
       
    32 
       
    33 #include "truicontainerbase.h"
       
    34 #include "truiappui.h"
       
    35 
       
    36 _LIT( KChangeLine, "\n" );
       
    37 
       
    38 const TSize KHwrBoxSizeLandscape = TSize( 200, 200 );
       
    39 
       
    40 // ======== MEMBER FUNCTIONS ========
       
    41 
       
    42 CTruiContainerBase::CTruiContainerBase()
       
    43     {
       
    44     iAppUi = static_cast<CTruiAppUi*>( iAvkonViewAppUi );
       
    45     iEngine = iAppUi->HwrEngine();
       
    46     }
       
    47 
       
    48 CTruiContainerBase::~CTruiContainerBase()
       
    49     {
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Get the origin size in which input the model for preset symbols. 
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 TSize CTruiContainerBase::OriginSymbolSize()
       
    57     {   
       
    58     TRect rect = iAvkonViewAppUi->ClientRect();
       
    59     
       
    60     TAknWindowComponentLayout main_pane_layout = 
       
    61                             AknLayoutScalable_Apps::main_hwr_training_pane();
       
    62     TAknLayoutRect main_pane_layout_rect;
       
    63     main_pane_layout_rect.LayoutRect( rect, main_pane_layout );
       
    64     TRect main_pane_rect = main_pane_layout_rect.Rect();
       
    65     
       
    66     // hwr_training_write_pane
       
    67     TAknWindowComponentLayout write_pane_layout = 
       
    68                AknLayoutScalable_Apps::hwr_training_write_pane( 2 );
       
    69     TAknLayoutRect write_pane_layout_rect;
       
    70     write_pane_layout_rect.LayoutRect( main_pane_rect, write_pane_layout );
       
    71     
       
    72     TAknWindowComponentLayout drawable_rect_layout = 
       
    73                AknLayoutScalable_Apps::hwr_training_write_pane_g7( 0 );
       
    74     TAknLayoutRect drawable_rect_layout_rect;
       
    75     drawable_rect_layout_rect.LayoutRect( write_pane_layout_rect.Rect(), 
       
    76                                           drawable_rect_layout );
       
    77     return drawable_rect_layout_rect.Rect().Size();
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // Wrap the text to fit the minisize of Label.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 TInt CTruiContainerBase::WrapLabelText( const CEikLabel* aLabel, 
       
    85                                             const TDesC& aText,
       
    86                                             TInt aMaxLines, 
       
    87                                             HBufC* aWrappedText )
       
    88     {
       
    89     return DoWrapLabelText( aLabel->Font(), aLabel->Size().iWidth, aText, 
       
    90                             aMaxLines, aWrappedText );
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // Wrap the text to fit the minisize of Label.
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 TInt CTruiContainerBase::WrapLabelText(  const TAknLayoutText& aLayoutText,
       
    98                                          const TDesC& aText,
       
    99                                          TInt aMaxLines, 
       
   100                                          HBufC* aWrappedText )
       
   101     {
       
   102     const CFont* font = aLayoutText.Font();
       
   103     TInt width = aLayoutText.TextRect().Size().iWidth;
       
   104     return DoWrapLabelText( font, width, aText, aMaxLines, aWrappedText );
       
   105     }    
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // Wrap the text to fit the minisize of Label.
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 void CTruiContainerBase::WrapLabelTextL(  const CFont* aFont, const TDesC& aText,
       
   112     const RArray<TInt>& aLineWidthArray, 
       
   113     HBufC* aWrappedText )
       
   114     {    
       
   115     if ( aWrappedText && aLineWidthArray.Count() > 0 )
       
   116         {
       
   117         CArrayFixFlat<TInt>* lineWidthArray = new (ELeave) 
       
   118                                 CArrayFixFlat<TInt>( aLineWidthArray.Count() );
       
   119         CleanupStack::PushL( lineWidthArray );
       
   120         // Create line array        
       
   121         for ( TInt i = 0; i < aLineWidthArray.Count(); i++ )
       
   122             {
       
   123             lineWidthArray->AppendL( aLineWidthArray[i] );
       
   124             }
       
   125         // Do wrap and clip
       
   126         TPtr wrappedTextPtr = aWrappedText->Des();
       
   127         AknTextUtils::WrapToStringAndClipL( aText, *lineWidthArray, *aFont, 
       
   128             wrappedTextPtr );        
       
   129         CleanupStack::PopAndDestroy( lineWidthArray );
       
   130         }    
       
   131     }    
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // Create skin specified button from resource.
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 CAknButton* CTruiContainerBase::CreateButtonL( TInt aResourceId, 
       
   138                                                const CCoeControl* aParent, 
       
   139                                                MCoeControlObserver* aObserver )
       
   140     {
       
   141     TResourceReader reader;
       
   142     iEikonEnv->CreateResourceReaderLC( reader, aResourceId );
       
   143     CAknButton* button = ConstructButtonFromResourceL( reader );
       
   144     if ( aParent )
       
   145         {
       
   146         button->SetContainerWindowL( *aParent );
       
   147         }   
       
   148     if ( aObserver )
       
   149         {
       
   150         button->SetObserver( aObserver );    
       
   151         }    
       
   152     CleanupStack::PopAndDestroy(); // reader    
       
   153     return button;
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // Add skin specified buttons into toolbar.
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 void CTruiContainerBase::AddButtonsToToolbarL( CAknToolbar* aToolbar, TInt aResourceId )
       
   161     {
       
   162     TResourceReader reader;
       
   163     iEikonEnv->CreateResourceReaderLC( reader, aResourceId );
       
   164     const TInt buttonCount = reader.ReadInt16();
       
   165     for ( TInt i = 0; i < buttonCount; i++ )
       
   166         {
       
   167         // Create each toolbar item
       
   168         TInt controlType = reader.ReadInt16();
       
   169         TInt commandId = reader.ReadInt16();
       
   170         reader.ReadInt16(); // flags
       
   171         reader.ReadInt16(); // length        
       
   172         CAknButton* button = ConstructButtonFromResourceL( reader );
       
   173         aToolbar->AddItemL( button, controlType, commandId, 0, i );       
       
   174         }
       
   175     CleanupStack::PopAndDestroy(); // reader    
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // Return current keyboard mode.
       
   180 // ---------------------------------------------------------------------------
       
   181 // 
       
   182 TInt CTruiContainerBase::CurrentKeyBoardModeL()
       
   183     {
       
   184     RProperty keyboardProperty;
       
   185     CleanupClosePushL( keyboardProperty );
       
   186     User::LeaveIfError( keyboardProperty.Attach( KCRUidAvkon, 
       
   187                                                  KAknQwertyInputModeActive ) );
       
   188     TInt keyboardMode = 0;
       
   189     keyboardProperty.Get( keyboardMode );    
       
   190     CleanupStack::PopAndDestroy( &keyboardProperty ); 
       
   191     return keyboardMode;
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // Read setting from ini file to decide if display wizard view.
       
   196 // ---------------------------------------------------------------------------
       
   197 // 
       
   198 void CTruiContainerBase::GetSettingFromIniFileL( TUint32 aKey, TInt& aValue )
       
   199     {
       
   200     CDictionaryStore* iniFile = iAppUi->Application()->OpenIniFileLC( iEikonEnv->FsSession() );
       
   201     RDictionaryReadStream readSteam;
       
   202     readSteam.OpenLC( *iniFile, TUid::Uid( aKey ) );
       
   203     TInt16 value;
       
   204     readSteam >> value;
       
   205     aValue = value;
       
   206     CleanupStack::PopAndDestroy( &readSteam );
       
   207     CleanupStack::PopAndDestroy( iniFile );    
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------------------------
       
   211 // Save settings into file.
       
   212 // ---------------------------------------------------------------------------
       
   213 // 
       
   214 void CTruiContainerBase::SaveSettingIntoIniFileL( TUint32 aKey, TInt aValue )
       
   215     {
       
   216     CDictionaryStore* iniFile = 
       
   217                  iAppUi->Application()->OpenIniFileLC( iEikonEnv->FsSession() );
       
   218     RDictionaryWriteStream stream;
       
   219     stream.AssignLC( *iniFile, TUid::Uid( aKey ) );
       
   220     stream << (TInt16)aValue;
       
   221     stream.CommitL();
       
   222     iniFile->CommitL();
       
   223     CleanupStack::PopAndDestroy( &stream );
       
   224     CleanupStack::PopAndDestroy( iniFile );
       
   225     }
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // Get cached color of skin for label control
       
   229 // ---------------------------------------------------------------------------
       
   230 // 
       
   231 TInt CTruiContainerBase::GetCachedLabelTextColor( TRgb& aColor )
       
   232     {
       
   233     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   234     // Set pen color to the color of test in main area
       
   235     return AknsUtils::GetCachedColor( skin,
       
   236                                       aColor,
       
   237                                       KAknsIIDQsnTextColors,
       
   238                                       EAknsCIQsnTextColorsCG6 );
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // Get cached color of skin for icon
       
   243 // ---------------------------------------------------------------------------
       
   244 // 
       
   245 TInt CTruiContainerBase::GetCachedIcontColor( TRgb& aColor, 
       
   246                                               const TAknsItemID& aColorId, 
       
   247                                               TInt aColorIndex )
       
   248     {
       
   249     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   250     // Set pen color to the color of test in main area
       
   251     return AknsUtils::GetCachedColor( skin,
       
   252                                       aColor,
       
   253                                       aColorId,
       
   254                                       aColorIndex );
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // Create skin specified button from resource.
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 CAknButton* CTruiContainerBase::ConstructButtonFromResourceL( TResourceReader& aReader )
       
   262     {
       
   263     TInt flags = aReader.ReadInt16();
       
   264     TPtrC helpText = aReader.ReadTPtrC();
       
   265     TPtrC fileName = aReader.ReadTPtrC();
       
   266     TInt bmpId = aReader.ReadInt16();    
       
   267     TInt bmpMaskId = aReader.ReadInt16();
       
   268     TInt pressId = aReader.ReadInt16();
       
   269     TInt pressMaskId = aReader.ReadInt16();
       
   270     TInt mainItemId = aReader.ReadInt32();
       
   271     TInt minorItemId = aReader.ReadInt32();
       
   272     TInt mainItemPressId = aReader.ReadInt32();
       
   273     TInt minorItemPressId = aReader.ReadInt32();
       
   274     
       
   275     CGulIcon* icon = NULL;
       
   276     TInt length = fileName.Length();
       
   277     if ( fileName.Length() )
       
   278         {
       
   279         TAknsItemID aknItemId = { mainItemId, minorItemId };        
       
   280         
       
   281         // Create icon
       
   282         CFbsBitmap* bitmap;
       
   283         CFbsBitmap* bitmapm;
       
   284         MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   285         AknsUtils::CreateColorIconL( skin,
       
   286                                      aknItemId,
       
   287                                      KAknsIIDQsnIconColors,
       
   288                                      EAknsCIQsnIconColorsCG30,
       
   289                                      bitmap,
       
   290                                      bitmapm,
       
   291                                      fileName,
       
   292                                      bmpId,
       
   293                                      bmpMaskId,
       
   294                                      KRgbBlack );
       
   295         CleanupStack::PushL( bitmap );
       
   296         CleanupStack::PushL( bitmapm );
       
   297         icon = CGulIcon::NewL( bitmap, bitmapm ); // Ownership transfered
       
   298         CleanupStack::Pop( bitmapm );
       
   299         CleanupStack::Pop( bitmap );        
       
   300         }
       
   301     CleanupStack::PushL( icon );
       
   302     CAknButton* button =  CAknButton::NewL ( icon, 
       
   303                                              NULL,
       
   304                                              NULL,
       
   305                                              NULL,
       
   306                                              KNullDesC, 
       
   307                                              helpText, 
       
   308                                              flags,
       
   309                                              NULL );
       
   310     CleanupStack::Pop( icon );
       
   311     return button;    
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // Construct icons from resource.
       
   316 // ---------------------------------------------------------------------------
       
   317 //
       
   318 CGulIcon* CTruiContainerBase::ConstructIconFromResourceL( TInt aResourceId )
       
   319     {
       
   320     TResourceReader reader;
       
   321     iEikonEnv->CreateResourceReaderLC( reader, aResourceId );
       
   322     TInt flags = reader.ReadInt16();
       
   323     TPtrC helpText = reader.ReadTPtrC();
       
   324     TPtrC fileName = reader.ReadTPtrC();
       
   325     TInt bmpId = reader.ReadInt16();    
       
   326     TInt bmpMaskId = reader.ReadInt16();
       
   327     TInt pressId = reader.ReadInt16();
       
   328     TInt pressMaskId = reader.ReadInt16();
       
   329     TInt mainItemId = reader.ReadInt32();
       
   330     TInt minorItemId = reader.ReadInt32();
       
   331     TInt mainItemPressId = reader.ReadInt32();
       
   332     TInt minorItemPressId = reader.ReadInt32();
       
   333     
       
   334     TAknsItemID aknItemId = { mainItemId, minorItemId };    
       
   335     
       
   336     // Create icon
       
   337     CFbsBitmap* bitmap;
       
   338     CFbsBitmap* bitmapm;
       
   339     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   340     AknsUtils::CreateColorIconL( skin,
       
   341                                  aknItemId,
       
   342                                  KAknsIIDQsnIconColors,                                 
       
   343                                  EAknsCIQsnIconColorsCG30,
       
   344                                  bitmap,
       
   345                                  bitmapm,
       
   346                                  fileName,
       
   347                                  bmpId,
       
   348                                  bmpMaskId,
       
   349                                  KRgbBlack );
       
   350     CleanupStack::PushL( bitmap );
       
   351     CleanupStack::PushL( bitmapm );
       
   352     CGulIcon* icon = CGulIcon::NewL( bitmap, bitmapm ); // Ownership transfered
       
   353     CleanupStack::Pop( bitmapm );
       
   354     CleanupStack::Pop( bitmap );    
       
   355     CleanupStack::PopAndDestroy(); // reader  
       
   356     return icon;      
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------------------------
       
   360 // Wrap the text to fit the minisize of Label.
       
   361 // ---------------------------------------------------------------------------
       
   362 //
       
   363 TInt CTruiContainerBase::DoWrapLabelText(  const CFont* aFont, TInt aWidth,
       
   364                                            const TDesC& aText, TInt aMaxLines, 
       
   365                                            HBufC* aWrappedText )
       
   366     {        
       
   367     TInt lineCount;
       
   368     TInt startPos = 0;
       
   369     TInt lines = 1;
       
   370     if ( aWidth <= 0 || !aWrappedText )
       
   371         {
       
   372         return 0;
       
   373         }    
       
   374     TPtrC subText = aText.Mid( startPos, aText.Length() - startPos );    
       
   375     TPtr newTextPtr = aWrappedText->Des();
       
   376     newTextPtr.Copy( KNullDesC ); 
       
   377     while ( ( lineCount = aFont->TextCount( subText, aWidth ) ) < subText.Length() )
       
   378         {
       
   379         // Need to wrap
       
   380         if ( ++lines > aMaxLines )
       
   381             {
       
   382             // Check if it is up to maximum line
       
   383             break;
       
   384             }
       
   385         // get the last char of every line
       
   386         TChar nextCharLine = aText[ startPos + lineCount ];
       
   387         
       
   388         // Decide at which position insert the \n
       
   389         if ( nextCharLine == TChar( ' ' ) )
       
   390             {
       
   391             // Replace nextCharLine with \n
       
   392             newTextPtr.Append( subText.Mid( 0, lineCount ) );
       
   393             newTextPtr.Append( KChangeLine );
       
   394             // And move the startPos for the next loop.
       
   395             startPos += lineCount + 1;
       
   396             }
       
   397         else
       
   398             {
       
   399             // Go forward to find " " and replace it with \n
       
   400             // And move the startPos for the next loop.
       
   401             TPtrC subTextPtr = subText.Mid( 0, lineCount );
       
   402             TInt pos = subTextPtr.LocateReverse( TChar( ' ' ) );
       
   403             if ( pos != KErrNotFound )
       
   404                 {
       
   405                 newTextPtr.Append( subTextPtr.Left( pos ) );
       
   406                 newTextPtr.Append( KChangeLine );
       
   407                 startPos += pos + 1;
       
   408                 }
       
   409             else
       
   410                 {
       
   411                 newTextPtr.Append( subTextPtr );
       
   412                 newTextPtr.Append( KChangeLine );
       
   413                 startPos += lineCount;
       
   414                 }
       
   415             }
       
   416         subText.Set( aText.Mid( startPos, aText.Length() - startPos ) );
       
   417         }
       
   418     newTextPtr.Append( subText );
       
   419     return lines;
       
   420     }
       
   421         
       
   422 // ---------------------------------------------------------------------------
       
   423 // From class CCoeControl.
       
   424 // Handles a change to the control's resources.
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427 void CTruiContainerBase::HandleResourceChange( TInt aType )
       
   428     {
       
   429     CCoeControl::HandleResourceChange( aType );
       
   430     if ( aType==KEikDynamicLayoutVariantSwitch && iView )
       
   431         {
       
   432         SetRect( iView->ClientRect() );            
       
   433         }     
       
   434     }