fep/aknfep/src/AknFepUiInputStateEntryPinyinPhrase.cpp
changeset 0 eb1f2e154e89
child 12 5e18d8c489d6
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2 * Copyright (c) 2004 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:           
       
    15 *       Provides the CAknFepUIInputStatePinyinPhrase methods.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 #include "AknFepPanic.h"
       
    31 #include "AknFepUiInputStateEntryPinyinPhrase.h"
       
    32 #include "AknFepUIManagerStateInterface.h"      //MAknFepUIManagerStateInterface
       
    33 #include "AknFepManagerUIInterface.h"           //MAknFepManagerUIInterface
       
    34 #include "AknFepUiCtrlContainerChinese.h"
       
    35 #include "AknFepUICtrlInputPane.h"
       
    36 #include "AknFepUICtrlCandidatePane.h"
       
    37 #include "AknFepUICtrlPinyinPopup.h"		//Pinyin phrase
       
    38 #include "AknFepManager.h"
       
    39 
       
    40 #include <PtiEngine.h>                          //CPtiEngine
       
    41 #include <PtiDefs.h>                            //keys
       
    42 #include <avkon.rsg>
       
    43 #include <AknFep.rsg>
       
    44 #include <aknnotewrappers.h>
       
    45 
       
    46 const TText KPinyinTone0Valid = 0x02C9;
       
    47 const TText KPinyinTone1Valid = 0x02CA;
       
    48 const TText KPinyinTone2Valid = 0x02C7;
       
    49 const TText KPinyinTone3Valid = 0x02CB;
       
    50 const TText KPinyinTone4Valid = 0x02D9;
       
    51 const TText KPinyinTone4Invalid = 0x0020;
       
    52 
       
    53 const TInt KMaxPhraseCount = 100;
       
    54 const TInt KMaxKeystrokeCount = 100;
       
    55 const TInt KMaxShowKeystrokeCount = 31;
       
    56 
       
    57 const TInt KMaxPinyinLength = 60;
       
    58 
       
    59 const TInt KMaxStarCount = 5;
       
    60 
       
    61 const TInt KKey0Code = 48;
       
    62 const TInt KKey1Code = 49;
       
    63 const TInt KKey2Code = 50;
       
    64 const TInt KKey3Code = 51;
       
    65 const TInt KKey4Code = 52;
       
    66 const TInt KKey5Code = 53;
       
    67 const TInt KKey6Code = 54;
       
    68 const TInt KKey7Code = 55;
       
    69 const TInt KKey8Code = 56;
       
    70 const TInt KKey9Code = 57;
       
    71 
       
    72 _LIT( KPinyinListSeparator, "'" );
       
    73 _LIT( KPinyinStar, "*" );
       
    74 _LIT( KPinyinNote,"\x9020\x65B0\x8BCD" );
       
    75 _LIT( KPinyinManualSeparator, "\x002E");
       
    76 
       
    77 TAknFepInputStateEntryPinyinPhrase::TAknFepInputStateEntryPinyinPhrase(
       
    78                                 MAknFepUIManagerStateInterface* aOwner,
       
    79                                 MAknFepUICtrlContainerChinese* aUIContainer)
       
    80     :TAknFepInputStateChineseBase(aOwner, aUIContainer)
       
    81     {
       
    82     iState = EEntry;
       
    83 
       
    84     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
    85     uiContainer->FocusCandidatePane(EFalse);
       
    86     uiContainer->CandidatePane()->ShowCandidateOrdinals(EFalse);
       
    87     uiContainer->SetLayout(MAknFepUICtrlContainerChinese::ELayoutInput);
       
    88     uiContainer->CandidatePane()->SelectFirstPhrase(); 
       
    89     uiContainer->ShowVerticalScrollArrows(ETrue);
       
    90     uiContainer->ShowHorizontalScrollArrows(EFalse);
       
    91     uiContainer->InputPane()->SetOverrideFontId(0);
       
    92 
       
    93     iOwner->PtiEngine()->SetInputMode(EPtiEnginePinyinByPhrase);
       
    94     iOwner->PtiEngine()->SetCandidatePageLength(KMaxPhraseCount);
       
    95     uiContainer->PinyinPopupWindow()->SetTonemarkState( ETrue );
       
    96     // set the state is in input state,
       
    97     // so that it could handle EKeyCBA1
       
    98     iOwner->FepMan()->EntryPhrase( ETrue );
       
    99     uiContainer->SetFepMan( iOwner->FepMan() );
       
   100     
       
   101     ClearKeystrokeBuf();
       
   102     // in the case that we are coming back to the input pane from the candidate pane, 
       
   103     // we need to ensure that the current selection is selected correctly
       
   104     ImplicitlyUpdateSelection();
       
   105     // however we also need to clear the deliberate selection, in case we are not
       
   106     // coming back to the input pane from the candidate pane
       
   107     if ( uiContainer->PinyinPopupWindow()->GetFromCandidateChangeStateFlag() )
       
   108         {
       
   109         uiContainer->PinyinPopupWindow()->SetFromCandidateChangeStateFlag( EFalse );
       
   110         }
       
   111     else
       
   112         {
       
   113         ClearDeliberateSelection();
       
   114         }
       
   115     
       
   116     UpdateIndicator();
       
   117     TRAP_IGNORE( ChangeCbaL() );
       
   118     }
       
   119 
       
   120 void TAknFepInputStateEntryPinyinPhrase::HandleCommandL( TInt aCommandId)
       
   121     {
       
   122     CPtiEngine* ptiengine = iOwner->PtiEngine( );
       
   123     MAknFepUICtrlContainerChinese* uiContainer = UIContainer( );
       
   124     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow( );
       
   125     CDesCArrayFlat* keystrokeArray = popup->KeystrokeArray( );
       
   126     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray( );
       
   127     CDesCArrayFlat* showKeystrokeArray = popup->ShowKeystrokeArray( );
       
   128     TInt currentSelect = popup->CurrentSelection( );
       
   129     TInt inEffectSpellingCount = ptiengine->PhoneticSpellingCount( );
       
   130     TBuf<KMaxKeystrokeCount> keystrokeBuf;
       
   131     switch ( aCommandId )
       
   132         {
       
   133         // Handle the event frome command.
       
   134         case EAknSoftkeyCancel:
       
   135             //  case (TUint16)EAknSoftkeyCancel: //the Cancle in soft CBA
       
   136             TAknFepInputStateChineseBase::HandleCommandL( aCommandId );
       
   137             break;
       
   138         case EAknSoftkeySelect:
       
   139             //case (TUint16)EAknSoftkeySelect: //the Selected in soft CBA
       
   140             if ( (currentSelect < inEffectSpellingCount)
       
   141                 && (keystrokeArray->Count( )
       
   142                     == inEffectKeystrokeArray->Count( ) )
       
   143                 && (popup->GetTonemarkState( ) ) )
       
   144                 {
       
   145                 if ( popup->IsEnabled( ) )
       
   146                     {
       
   147                     popup->Enable( EFalse );
       
   148                     }
       
   149                 iOwner->FepMan()->PinyinPhraseCreation( EFalse );
       
   150                 popup->SetChangeState( ETrue );
       
   151                 iOwner->ChangeState( ECandidate );
       
   152                 }
       
   153             else
       
   154                 {
       
   155                 GetKeystrokeBuf( *keystrokeArray, keystrokeBuf );
       
   156                 iOwner->FepMan()->PinyinPhraseCreation( ETrue );
       
   157                 popup->SetChangeState( ETrue );
       
   158                 popup->SetPhraseCreationState( ETrue );
       
   159                 if ( GetChangeState( ) )
       
   160                     {
       
   161                     ClearDeliberateSelection( );
       
   162                     popup->Enable( EFalse );
       
   163                     iOwner->ChangeState( ESpellingSelection );
       
   164                     }
       
   165                 else
       
   166                     {
       
   167                     ClearDeliberateSelection( );
       
   168                     popup->Enable( EFalse );
       
   169                     iOwner->ChangeState( EKeySequenceEdit );
       
   170                     }
       
   171                 }
       
   172             break;
       
   173         default:
       
   174             DeliberatelyUpdateSelection( );
       
   175             break;
       
   176         }
       
   177 
       
   178     }
       
   179 
       
   180 TBool TAknFepInputStateEntryPinyinPhrase::HandleKeyL( TInt aKey, TKeyPressLength aLength )
       
   181     {
       
   182     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   183     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   184     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   185     CDesCArrayFlat* keystrokeArray = popup->KeystrokeArray();
       
   186     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
   187     CDesCArrayFlat* showKeystrokeArray = popup->ShowKeystrokeArray();
       
   188     TInt currentSelect = popup->CurrentSelection();
       
   189     TInt inEffectSpellingCount = ptiengine->PhoneticSpellingCount();
       
   190     TInt offset = 0;
       
   191     
       
   192     TBuf<1> keyBuf;
       
   193     TBuf<1> keyShow;
       
   194     TBuf<KMaxKeystrokeCount> keystrokeBuf;
       
   195     
       
   196     if(aKey == EKeyBackspace)
       
   197         {
       
   198         TBool state = ETrue;
       
   199         if ( popup->IsChangeStatet() && aLength == ELongKeyPress )
       
   200             {
       
   201             state = EFalse;
       
   202             }
       
   203         else
       
   204             {
       
   205             popup->SetChangeState( EFalse );
       
   206             }
       
   207         if ( state )
       
   208             {
       
   209             DeleteCurrentKeystroke();
       
   210             showKeystrokeArray->Delete( showKeystrokeArray->Count() - 1 );
       
   211             showKeystrokeArray->Compress();
       
   212             if ( inEffectKeystrokeArray->Count()> 0 )
       
   213                 {
       
   214                 ImplicitlyUpdateSelection();
       
   215                 }
       
   216             else
       
   217                 {
       
   218                 ClearDeliberateSelection();
       
   219                 iOwner->FepMan()->TryCloseUiL(); //no more keys, close the UI.
       
   220                 if (aLength == ELongKeyPress)
       
   221                     {
       
   222                     iOwner->FepMan()->SetLongClearAfterCloseUI(ETrue);
       
   223                     }
       
   224                 }
       
   225             }
       
   226         }
       
   227     else if(aKey == EKeyRightArrow)
       
   228         {
       
   229         if(popup->IsEnabled())
       
   230             {
       
   231             if(popup->SelectNextPhrase())
       
   232                 {
       
   233                 DeliberatelyUpdateSelection();
       
   234                 }
       
   235             }
       
   236         }
       
   237     else if(aKey == EKeyLeftArrow)
       
   238         {
       
   239         if(popup->IsEnabled())
       
   240             {
       
   241             if(popup->SelectPrevPhrase())
       
   242                 {
       
   243                 DeliberatelyUpdateSelection();
       
   244                 }
       
   245             }
       
   246         }
       
   247     else if( ( aKey == EKeyOK || aKey == EKeyDownArrow || aKey == EKeyCBA1)
       
   248         && aLength == EShortKeyPress)
       
   249         {
       
   250         if ( ( currentSelect < inEffectSpellingCount) && 
       
   251             ( keystrokeArray->Count() == inEffectKeystrokeArray->Count() ) &&
       
   252             ( popup->GetTonemarkState() ) )
       
   253             {
       
   254             if(popup->IsEnabled())
       
   255                 {
       
   256                 popup->Enable(EFalse);
       
   257                 }
       
   258             iOwner->FepMan()->PinyinPhraseCreation( EFalse );
       
   259             popup->SetChangeState( ETrue );
       
   260             iOwner->ChangeState(ECandidate);              
       
   261             }
       
   262         else
       
   263             {
       
   264             GetKeystrokeBuf( *keystrokeArray, keystrokeBuf );
       
   265             iOwner->FepMan()->PinyinPhraseCreation( ETrue );
       
   266             popup->SetChangeState( ETrue );
       
   267             popup->SetPhraseCreationState( ETrue );
       
   268             if ( GetChangeState() )
       
   269                 {
       
   270                 ClearDeliberateSelection();
       
   271                 popup->Enable( EFalse );
       
   272                 iOwner->ChangeState(ESpellingSelection);
       
   273                 }
       
   274             else
       
   275                 {
       
   276                 ClearDeliberateSelection();
       
   277                 popup->Enable( EFalse );
       
   278                 iOwner->ChangeState(EKeySequenceEdit);
       
   279                 }
       
   280             }
       
   281         }
       
   282     else if (aLength == EShortKeyPress) // don't want repeats on these keys
       
   283         {
       
   284     	iOwner->FepMan()->SetCcpuFlag(CAknFepManager::ECcpuStateIgnoreStarUp);            
       
   285         
       
   286         if ( ( iOwner->IsValidChineseInputKey(aKey) ) &&
       
   287              ( CheckKeystroke( aKey, *keystrokeArray ) ) )
       
   288             {
       
   289             TInt stringBeforeLength(0);
       
   290             TInt stringAfterLength(0);
       
   291             GetKeystrokeNum( aKey, keyBuf );
       
   292             keystrokeArray->AppendL( keyBuf );
       
   293             showKeystrokeArray->AppendL( keyBuf );
       
   294             
       
   295             if ( popup->GetTonemarkState() )
       
   296                 {
       
   297             stringBeforeLength = ptiengine->GetPhoneticSpelling(1).Length();
       
   298             stringAfterLength = ptiengine->AppendKeyPress((TPtiKey)aKey).Length();
       
   299                 if (stringBeforeLength != stringAfterLength)
       
   300                     {
       
   301                     if (ptiengine->GetPhoneticSpelling(1).Length() == 1)
       
   302                         {
       
   303                         iOwner->FepMan()->UpdateCbaL(R_AKNFEP_SOFTKEYS_PHRASE_CREATION_SELECT_CANCEL_SELECT);
       
   304                         }
       
   305                     inEffectKeystrokeArray->AppendL(keyBuf);
       
   306                     ImplicitlyUpdateSelection();
       
   307                     }
       
   308                 else
       
   309                     {
       
   310                     ptiengine->EnableToneMarks(ETrue);
       
   311                     UIContainer()->PinyinPopupWindow()->SetFlag(MAknFepUICtrlPinyinPopup::ESpellingChanged);
       
   312                     RefreshUI(0);
       
   313                     } 
       
   314                 }
       
   315             else
       
   316                 {
       
   317                 ImplicitlyUpdateSelection();
       
   318                 }
       
   319             }
       
   320         else if ( ( aKey == EPtiKeyStar ) &&  
       
   321                   ( CheckKeystroke( aKey, *keystrokeArray ) ) ) // we increment the tone mark.
       
   322             {
       
   323             keyBuf.Append( EPtiKeyStar );
       
   324             
       
   325             if ( keystrokeArray->Count() == inEffectKeystrokeArray->Count() )
       
   326                 {
       
   327                 inEffectKeystrokeArray->AppendL(keyBuf);
       
   328                 keystrokeArray->AppendL( keyBuf );
       
   329                 GetPinyinToneMark( *keystrokeArray, keyShow );
       
   330                 ShowKeystrokeAppendL( *showKeystrokeArray, *keystrokeArray, keyShow );
       
   331                 CheckTonemarkL();
       
   332                 }
       
   333             else
       
   334                 {
       
   335                 keystrokeArray->AppendL( keyBuf );
       
   336                 GetPinyinToneMark( *keystrokeArray, keyShow );
       
   337                 ShowKeystrokeAppendL( *showKeystrokeArray, *keystrokeArray, keyShow );
       
   338                 for ( TInt i = keystrokeArray->Count() - 1; i >= 0; i-- )
       
   339                     {
       
   340                     if ( 0 == keystrokeArray->MdcaPoint( i ).
       
   341                             Compare( KPinyinStar ) )
       
   342                         {
       
   343                         offset = offset + 1;
       
   344                         }
       
   345                     else
       
   346                         {
       
   347                         break;
       
   348                         }
       
   349                     }
       
   350                 if ( offset > KMaxStarCount )
       
   351                     {
       
   352                     for ( TInt j = 0; j < KMaxStarCount; ++j )
       
   353                         {
       
   354                         keystrokeArray->Delete( keystrokeArray->Count() - 1 );
       
   355                         }
       
   356                     keystrokeArray->Compress();
       
   357                     }
       
   358                 }
       
   359             ImplicitlyUpdateSelection();
       
   360             }
       
   361         else
       
   362             {
       
   363             iOwner->FepMan()->PlaySound(EAvkonSIDErrorTone);
       
   364             }
       
   365         }
       
   366     return ETrue;
       
   367     }
       
   368 
       
   369 // ---------------------------------------------------------
       
   370 // Optimize spelling
       
   371 // ---------------------------------------------------------
       
   372 //
       
   373 void TAknFepInputStateEntryPinyinPhrase::OptimizeSpellingL( CDesCArrayFlat& aOptimizeBefore, CDesCArrayFlat& aOptimizeAfter )
       
   374     {
       
   375     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   376     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   377     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
   378     TBool checkSeparator = EFalse;
       
   379     TBuf<KMaxPinyinLength> spellingBuf;
       
   380     TInt bufLength = 0;
       
   381     TInt countSeparator = 0;
       
   382 
       
   383     TInt countBuf[KMaxPhraseCount];
       
   384     memset( countBuf, 0, sizeof( countBuf ) );
       
   385     
       
   386     TInt position = 0;
       
   387     TInt i = 0;
       
   388     TInt j = 0;
       
   389     
       
   390     if ( 0 == inEffectKeystrokeArray->Count() )
       
   391         {
       
   392         return;
       
   393         }
       
   394     
       
   395     for ( i = 0 ; i < inEffectKeystrokeArray->Count(); ++i )
       
   396         {
       
   397         if ( 0 == inEffectKeystrokeArray->MdcaPoint( i ).Compare( KPinyinListSeparator ) )
       
   398             {
       
   399             checkSeparator = ETrue;
       
   400             countBuf[countSeparator] = j;
       
   401             countSeparator = countSeparator + 1;
       
   402             }
       
   403         else
       
   404             {
       
   405             if (  0 == i  )
       
   406                 {
       
   407                 j = j + 1;
       
   408                 }
       
   409             else
       
   410                 {
       
   411                 if ( ( 0 == inEffectKeystrokeArray->MdcaPoint( i ).Compare( KPinyinStar ) ) &&
       
   412                     ( 0 != inEffectKeystrokeArray->MdcaPoint( i - 1 ).Compare( KPinyinStar ) ))
       
   413                     {
       
   414                     j = j + 1;
       
   415                     }
       
   416                 else if ( 0 != inEffectKeystrokeArray->MdcaPoint( i ).Compare( KPinyinStar ) )
       
   417                     {
       
   418                     j = j + 1;
       
   419                     }
       
   420                 }
       
   421             }
       
   422         }
       
   423   
       
   424     j = 0;
       
   425     for ( i = 0; i < aOptimizeBefore.Count(); ++i )
       
   426         {
       
   427         j = 0;
       
   428         bufLength = 0;
       
   429         position = 0;
       
   430         spellingBuf.Size();
       
   431         spellingBuf.Copy( aOptimizeBefore.MdcaPoint( i ) );
       
   432         bufLength = spellingBuf.Length();
       
   433         while( j < bufLength )
       
   434             {
       
   435             position = spellingBuf.Find( KPinyinListSeparator );
       
   436             if ( position != KErrNotFound )
       
   437                 {
       
   438                 spellingBuf.Replace( position, 1, KPinyinManualSeparator );
       
   439                 j = position + 1;
       
   440                 }
       
   441             else
       
   442                 {
       
   443                 break;
       
   444                 }
       
   445             }
       
   446         aOptimizeAfter.AppendL( spellingBuf );
       
   447         }    
       
   448     
       
   449     if ( !checkSeparator )
       
   450         {
       
   451         return;
       
   452         }
       
   453     
       
   454     aOptimizeBefore.Reset();
       
   455     aOptimizeBefore.Compress();
       
   456     
       
   457     for ( i = 0; i < aOptimizeAfter.Count(); ++i )
       
   458         {
       
   459         aOptimizeBefore.AppendL( aOptimizeAfter.MdcaPoint( i ) );
       
   460         }
       
   461     
       
   462     aOptimizeAfter.Reset();
       
   463     aOptimizeAfter.Compress();
       
   464     
       
   465 
       
   466     for ( TInt k = 0; k < aOptimizeBefore.Count(); ++k )
       
   467         {
       
   468         j = 0;
       
   469         bufLength = 0;
       
   470         position = 0;
       
   471         spellingBuf.Size();
       
   472         spellingBuf.Copy( aOptimizeBefore.MdcaPoint( k ) );
       
   473         bufLength = spellingBuf.Length();
       
   474         for ( TInt kk = 0; kk < bufLength; ++kk )
       
   475             {
       
   476             if ( 0 == spellingBuf.Mid( kk, 1 ).Compare( KPinyinManualSeparator ) )
       
   477                 {
       
   478                 for ( TInt kkk = 0; kkk < countSeparator; ++kkk)
       
   479                     {
       
   480                     if ( j == countBuf[kkk] )
       
   481                         {
       
   482                         spellingBuf.Replace( kk, 1, KPinyinListSeparator );
       
   483                         }
       
   484                     }
       
   485                 }
       
   486             else
       
   487                 {
       
   488                 j = j + 1;
       
   489                 }
       
   490             }
       
   491         aOptimizeAfter.AppendL( spellingBuf );
       
   492         }
       
   493     }
       
   494 void TAknFepInputStateEntryPinyinPhrase::DeliberatelyUpdateSelection()
       
   495     {
       
   496     TPtr deliberateSelection = iOwner->GetLatestDeliberateSelection();
       
   497     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   498     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   499     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   500 
       
   501     TInt selected = popup->CurrentSelection();
       
   502     ptiengine->EnableToneMarks(EFalse);
       
   503     TPtrC spelling = ptiengine->GetPhoneticSpelling(selected + 1); // our index is zero based, engine index is one based
       
   504     deliberateSelection = spelling.Left(
       
   505                         MAknFepUICtrlInputPane::EMaxInputCharsPinyinPopupNotIncludingToneMark);
       
   506     ptiengine->EnableToneMarks(ETrue);
       
   507     UIContainer()->PinyinPopupWindow()->SetFlag(MAknFepUICtrlPinyinPopup::ESpellingNavigation);    
       
   508     RefreshUI(popup->CurrentVisibleSelection());
       
   509     }
       
   510 
       
   511 void TAknFepInputStateEntryPinyinPhrase::ImplicitlyUpdateSelection()
       
   512     {
       
   513     TPtr oldDeliberateSelection = iOwner->GetLatestDeliberateSelection();
       
   514     TInt oldDeliberateSelectionLength = oldDeliberateSelection.Length();
       
   515     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   516 
       
   517     // previously selected is compared in turn to each of the new candidates
       
   518     ptiengine->EnableToneMarks(EFalse);
       
   519     TInt pinyinCount = ptiengine->PhoneticSpellingCount();
       
   520     TInt newSelection = 0;
       
   521     for(TInt i = 0; i < pinyinCount; i++)
       
   522         {
       
   523         TPtrC spelling = ptiengine->GetPhoneticSpelling(i + 1);  // our index is zero based, engine index is one based
       
   524         TInt spellingLength = spelling.Length();
       
   525 
       
   526         // figure out how many characters we are comparing, this depends on the 
       
   527         // direction of the edit
       
   528         TInt compareLength = oldDeliberateSelectionLength <= spellingLength ? 
       
   529             oldDeliberateSelectionLength : spellingLength;
       
   530 
       
   531         if(oldDeliberateSelection.Left(compareLength) == spelling.Left(compareLength))
       
   532             {
       
   533             // as soon as a substring match is found, the 
       
   534             // highlight position is set to that candidate in the new list. 
       
   535             // note that in the case of the first character entered, the
       
   536             // oldBuf will be empty so it will always match... which is 
       
   537             // fine as we want to select the top one anyway, so we will
       
   538             // quit the loop early.
       
   539             newSelection = i;
       
   540             break;
       
   541             }
       
   542         }
       
   543     
       
   544         ptiengine->EnableToneMarks(ETrue);
       
   545         UIContainer()->PinyinPopupWindow()->SetFlag(MAknFepUICtrlPinyinPopup::ESpellingChanged);
       
   546     	RefreshUI(newSelection);
       
   547     }
       
   548 
       
   549 void TAknFepInputStateEntryPinyinPhrase::ClearDeliberateSelection()
       
   550     {
       
   551     // we must have just deleted the last character, 
       
   552     // or we are starting a new pinyin session, so wipe the last deliberate selection
       
   553     TPtr oldDeliberateSelection = iOwner->GetLatestDeliberateSelection();
       
   554     oldDeliberateSelection = KNullDesC;
       
   555     }
       
   556 
       
   557 void TAknFepInputStateEntryPinyinPhrase::RefreshUI(TInt aSelection)
       
   558     {
       
   559     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   560     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   561     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   562     MAknFepUICtrlInputPane* inputPane = uiContainer->InputPane();
       
   563     CDesCArrayFlat* keystrokeArray = popup->KeystrokeArray();
       
   564     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
   565     CDesCArrayFlat* showKeystrokeArray = popup->ShowKeystrokeArray();
       
   566     CDesCArrayFlat* optimizeSpelling = popup->PhoneticSpellingArray();
       
   567     CDesCArrayFlat* spelling = popup->OptimizeSpelling();
       
   568         
       
   569     TBuf<KMaxKeystrokeCount> keystrokeBuf;
       
   570     
       
   571     // get cursor position
       
   572     TPoint baseLine = TPoint(0,0);
       
   573     TInt height = 0;
       
   574     TInt ascent = 0;
       
   575 
       
   576     TBool checkTonemark = popup->GetTonemarkState();
       
   577     TRAPD(ret,iOwner->FepMan()->GetScreenCoordinatesL(baseLine,height,ascent));
       
   578     if (ret == KErrNone)
       
   579         {
       
   580         uiContainer->SetContainerPosition(baseLine, height);    
       
   581         }
       
   582 
       
   583     TText toneMark;
       
   584     TBool toneMarkEntered = ptiengine->ToneMark(toneMark);
       
   585     TBuf<1> validToneMarkBuf;
       
   586     if(toneMarkEntered)
       
   587         {
       
   588         // override specific invalid tonemark character only, the others are the same character 
       
   589         // when both valid and invalid
       
   590         if(toneMark == KPinyinTone4Invalid)
       
   591             toneMark = KPinyinTone4Valid;
       
   592         validToneMarkBuf.Append(toneMark);
       
   593         }
       
   594 
       
   595     // to start with, disable the tone marks so we can get the list of all pinyin matches
       
   596     // later we will figure out which ones are invalid
       
   597     ptiengine->EnableToneMarks(EFalse);
       
   598     TInt pinyinCountWithoutToneMarks = ptiengine->PhoneticSpellingCount();
       
   599     if(pinyinCountWithoutToneMarks > 0)
       
   600         {
       
   601         if(popup->IsFlagSet(MAknFepUICtrlPinyinPopup::ESpellingChanged))
       
   602             {
       
   603             if ( ( inEffectKeystrokeArray->Count() < keystrokeArray->Count() ) ||
       
   604                 ( !checkTonemark ) )
       
   605                 {
       
   606                 spelling->Reset();
       
   607                 optimizeSpelling->Reset();
       
   608                 spelling->Compress();
       
   609                 optimizeSpelling->Compress();
       
   610                 }
       
   611             else
       
   612                 {
       
   613                 spelling->Reset();
       
   614                 optimizeSpelling->Reset();
       
   615                 spelling->Compress();
       
   616                 optimizeSpelling->Compress();
       
   617             TRAPD(ignore, ptiengine->GetPhoneticSpellingsL(*spelling));
       
   618             if (ignore != KErrNone)
       
   619                     {
       
   620                     return;
       
   621                     }
       
   622                 
       
   623                 TRAPD( iOptimize, OptimizeSpellingL( *spelling, *optimizeSpelling ) );
       
   624                 if ( KErrNone != iOptimize )
       
   625                     {
       
   626                     return;
       
   627                     }
       
   628                 }
       
   629             
       
   630             GetKeystrokeBuf( *showKeystrokeArray, keystrokeBuf );
       
   631             
       
   632             TRAPD( retOptimize, optimizeSpelling->AppendL( keystrokeBuf ) );
       
   633             if ( KErrNone != retOptimize )
       
   634                 {
       
   635                 return;
       
   636                 }
       
   637             popup->SplitPhraseSpellingIntoPages(); 
       
   638             }
       
   639 
       
   640         // set display page for delibrate selection
       
   641         popup->SetDisplayPage(aSelection);
       
   642         popup->SetPhraseItemTexts();
       
   643  
       
   644         // fill in input pane although it's invisible
       
   645         if ( inEffectKeystrokeArray->Count() < keystrokeArray->Count() )
       
   646             {
       
   647             inputPane->SetText( keystrokeBuf );    
       
   648             }
       
   649         else
       
   650             {
       
   651             if ( optimizeSpelling->Count() > popup->CurrentSelection() )
       
   652                 {
       
   653                 inputPane->SetText( optimizeSpelling->MdcaPoint( popup->CurrentSelection() ) );
       
   654                 }
       
   655             else
       
   656                 {
       
   657                 inputPane->SetText( KNullDesC );
       
   658                 }
       
   659             }
       
   660         popup->PopupSizeChanged(); // phrase input
       
   661         }
       
   662     // turn tone marks back on so that we know where we are.
       
   663     ptiengine->EnableToneMarks(ETrue);
       
   664 
       
   665     // this is where we start to figure out whether the tonemarks are valid,
       
   666     // whether the selected tonemark is valid, and what is the index of the 
       
   667     // selected candidate in the list of candidates with tonemarks...
       
   668     TBool selectionToneMarkValid = EFalse;
       
   669     TInt selectionIndexAdjustedForToneMarkValidity = popup->CurrentSelection();
       
   670     
       
   671     // we only need to deal with tone marks if there is one
       
   672     if(toneMarkEntered)
       
   673         {
       
   674         if(pinyinCountWithoutToneMarks > 0)
       
   675             {
       
   676             for(TInt i = 0; i < pinyinCountWithoutToneMarks; i++)
       
   677                 {
       
   678                 TBool valid = EFalse;
       
   679                 
       
   680                 TBuf<MAknFepUICtrlInputPane::EMaxInputCharsPinyinPopupNotIncludingToneMark> nextCandidateWithoutToneMark;
       
   681                 nextCandidateWithoutToneMark.Copy(ptiengine->GetPhoneticSpelling(i + 1).Left(
       
   682                       MAknFepUICtrlInputPane::EMaxInputCharsPinyinPopupNotIncludingToneMark));
       
   683                 
       
   684                 ptiengine->EnableToneMarks(EFalse); 
       
   685                 ptiengine->SelectPhoneticSpelling(i + 1);
       
   686 
       
   687                 valid = ptiengine->IsToneMarkValidForSpelling();
       
   688                 if(valid)
       
   689                     {
       
   690                     ptiengine->EnableToneMarks(ETrue); 
       
   691                     TInt pinyinCountWithToneMarks = ptiengine->PhoneticSpellingCount();
       
   692 
       
   693                     for(TInt j = 0; j < pinyinCountWithToneMarks; j++)
       
   694                         {
       
   695                         // use j here not i as we are looking at the list with tonemarks
       
   696                         TPtrC nextCandidateWithToneMark = ptiengine->GetPhoneticSpelling(j + 1);  
       
   697                         if(nextCandidateWithToneMark == nextCandidateWithoutToneMark)
       
   698                             {
       
   699                             if(i == aSelection)
       
   700                                 {
       
   701                                 selectionToneMarkValid = ETrue;
       
   702                                 
       
   703                                 // fill in input pane even if it can't be seen, for when we have to hide popup window
       
   704                                 inputPane->SetText(nextCandidateWithoutToneMark);
       
   705                                 }
       
   706                             break;
       
   707                             }
       
   708                         }
       
   709                     }
       
   710                 }
       
   711             }
       
   712         }
       
   713     ptiengine->EnableToneMarks(EFalse); 
       
   714     ptiengine->SelectPhoneticSpelling(popup->CurrentSelection() + 1); 
       
   715     // next two lines are for tonemark validity
       
   716     ptiengine->EnableToneMarks(selectionToneMarkValid); 
       
   717     ptiengine->SelectPhoneticSpelling(selectionIndexAdjustedForToneMarkValidity + 1); // our index is zero based, engine index is one based
       
   718 
       
   719     // update the candidate pane.
       
   720     if(popup->IsFlagSet(MAknFepUICtrlPinyinPopup::ESpellingChanged) 
       
   721         || popup->IsFlagSet(MAknFepUICtrlPinyinPopup::ESpellingNavigation))
       
   722         {
       
   723         CDesCArrayFlat* phraseCandidates = uiContainer->CandidatePane()->CandidateArray();
       
   724         if ( ( pinyinCountWithoutToneMarks > popup->CurrentSelection() ) && 
       
   725              ( inEffectKeystrokeArray->Count() == keystrokeArray->Count() ) && 
       
   726              checkTonemark )      
       
   727             {
       
   728             TRAPD(ignore, ptiengine->GetChinesePhraseCandidatesL(*phraseCandidates));
       
   729             if (ignore != KErrNone)
       
   730                 {
       
   731                 return;
       
   732                 }            
       
   733             }        
       
   734         else if ( keystrokeArray->Count() != 0 )
       
   735             {
       
   736             phraseCandidates->Reset();
       
   737             TRAPD( retAddPinyinNote, phraseCandidates->AppendL( KPinyinNote ) );
       
   738             if ( KErrNone != retAddPinyinNote )
       
   739                 {
       
   740                 return;
       
   741                 }
       
   742             }
       
   743         uiContainer->CandidatePane()->SplitPhraseCandidatesIntoPages(); 
       
   744         popup->ClearFlag(MAknFepUICtrlPinyinPopup::ESpellingChanged 
       
   745                         | MAknFepUICtrlPinyinPopup::ESpellingNavigation);
       
   746         }
       
   747     uiContainer->CandidatePane()->SetCandidateBuffer();
       
   748     uiContainer->Enable(ETrue);
       
   749     // need to enable the pinyin popup after the container so that it is raised to the front
       
   750     popup->Enable(pinyinCountWithoutToneMarks > 0);
       
   751     UpdateIndicator();
       
   752     }
       
   753 
       
   754 void TAknFepInputStateEntryPinyinPhrase::InitializeStateL(void)
       
   755     {
       
   756     iOwner->FepMan()->UpdateCbaL(R_AKNFEP_SOFTKEYS_PHRASE_CREATION_SELECT_CANCEL_SELECT);
       
   757     }
       
   758     
       
   759 void TAknFepInputStateEntryPinyinPhrase::UpdateIndicator()
       
   760     {
       
   761     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   762     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();        
       
   763     TInt bufLength = iOwner->PtiEngine()->CandidatePage().Length();
       
   764     MAknFepUICtrlCandidatePane* candidatePane = uiContainer->CandidatePane();
       
   765         
       
   766     if (candidatePane->IsFirstPage())
       
   767         {
       
   768         uiContainer->CandidatePane()->ShowUpScrollArrows(EFalse);    
       
   769         }
       
   770     else
       
   771         {
       
   772         uiContainer->CandidatePane()->ShowUpScrollArrows(ETrue);    
       
   773         }
       
   774    
       
   775     if (candidatePane->IsLastPage())
       
   776         {
       
   777         uiContainer->CandidatePane()->ShowDownScrollArrows(EFalse);     
       
   778         }
       
   779     else
       
   780    	    {
       
   781    	    uiContainer->CandidatePane()->ShowDownScrollArrows(ETrue);      
       
   782    	    }
       
   783         
       
   784     if (candidatePane->SelectedIndex() == 0 && 
       
   785         candidatePane->IsFirstPage() )
       
   786         {
       
   787         uiContainer->CandidatePane()->ShowLeftScrollArrows(EFalse);
       
   788         }
       
   789     else
       
   790         {
       
   791         uiContainer->CandidatePane()->ShowLeftScrollArrows(ETrue);
       
   792         }
       
   793         
       
   794     if (candidatePane->IsLastPage() && 
       
   795         (candidatePane->SelectedIndex() == candidatePane->VisibleCandidateCount() - 1))
       
   796         {
       
   797         uiContainer->CandidatePane()->ShowRightScrollArrows(EFalse);        
       
   798         }
       
   799     else
       
   800         {
       
   801         uiContainer->CandidatePane()->ShowRightScrollArrows(ETrue);
       
   802         }
       
   803     } 
       
   804 // ---------------------------------------------------------
       
   805 // Delete current keystroke.
       
   806 // ---------------------------------------------------------
       
   807 //
       
   808 void TAknFepInputStateEntryPinyinPhrase::DeleteCurrentKeystroke()
       
   809     {
       
   810     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   811     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   812     CDesCArrayFlat* keystrokeArray = popup->KeystrokeArray();
       
   813     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
   814     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   815     TInt i = keystrokeArray->Count() - 1;
       
   816     TInt count = keystrokeArray->Count() - 1;
       
   817     TInt inECTKeystrokeCount = inEffectKeystrokeArray->Count();
       
   818     TInt validCount = 0;
       
   819     
       
   820     if ( keystrokeArray->Count() > inEffectKeystrokeArray->Count() )
       
   821         {
       
   822         for ( i = keystrokeArray->Count() - 1; i >= 0; i-- )
       
   823             {
       
   824             if ( 0 == keystrokeArray->MdcaPoint( i ).Compare( KPinyinStar ) )
       
   825                 {
       
   826                 keystrokeArray->Delete( i );
       
   827                 }
       
   828             else
       
   829                 {
       
   830                 if ( i == count )
       
   831                     {
       
   832                     keystrokeArray->Delete( i );
       
   833                     break;
       
   834                     }
       
   835                 break;
       
   836                 }
       
   837            }
       
   838         }
       
   839     else
       
   840         {
       
   841         for ( TInt j = 0; j < inECTKeystrokeCount; ++j )
       
   842             {            
       
   843             if ( 0 != inEffectKeystrokeArray->
       
   844                 MdcaPoint( j ).Compare( KPinyinStar ) )
       
   845                  {
       
   846                  validCount = validCount + 1;
       
   847                  }
       
   848             if ( j > 1 )
       
   849                 {
       
   850                 if ( ( 0 != inEffectKeystrokeArray->MdcaPoint( j ).
       
   851                     Compare( KPinyinStar ) ) && 
       
   852                     ( 0 == inEffectKeystrokeArray->MdcaPoint( j - 1).
       
   853                         Compare( KPinyinStar ) ) )
       
   854                      {
       
   855                      validCount = validCount + 1;
       
   856                      }
       
   857                 if  ( ( j == inECTKeystrokeCount - 1 ) && 
       
   858                     ( 0 == inEffectKeystrokeArray->MdcaPoint( j ).
       
   859                         Compare( KPinyinStar ) ) )
       
   860                     {
       
   861                     validCount = validCount + 1;
       
   862                     }
       
   863                 }
       
   864             }
       
   865         
       
   866         if ( inEffectKeystrokeArray->Count() > 0 )
       
   867             {
       
   868             if ( 0 == inEffectKeystrokeArray->MdcaPoint( 
       
   869                 inEffectKeystrokeArray->Count() - 1 ).Compare( KPinyinStar ) )
       
   870                 {
       
   871                 popup->SetTonemarkState( ETrue );
       
   872                 }
       
   873             }
       
   874         
       
   875         if ( 0 == keystrokeArray->MdcaPoint( keystrokeArray->Count() - 1 ).
       
   876             Compare( KPinyinStar ) )
       
   877             {
       
   878             ptiengine->DeleteKeyPress();
       
   879             for ( i = keystrokeArray->Count() - 1; i >= 0; i-- )
       
   880                 {
       
   881                 if ( 0 == keystrokeArray->MdcaPoint( i ).Compare( KPinyinStar ) )
       
   882                     {
       
   883                     keystrokeArray->Delete( i );
       
   884                     inEffectKeystrokeArray->Delete(i);
       
   885                     }
       
   886                 else
       
   887                     {
       
   888                     break;
       
   889                     }
       
   890                 }
       
   891             }
       
   892         else
       
   893             {
       
   894             keystrokeArray->Delete( i );
       
   895             inEffectKeystrokeArray->Delete(i);
       
   896             ptiengine->DeleteKeyPress();
       
   897             }
       
   898         }
       
   899     keystrokeArray->Compress();
       
   900     inEffectKeystrokeArray->Compress();
       
   901     }
       
   902 
       
   903 // ---------------------------------------------------------
       
   904 // Add current keystroke.
       
   905 // ---------------------------------------------------------
       
   906 void TAknFepInputStateEntryPinyinPhrase::AddCurrentKeystrokeL( const TDesC& keystroke )
       
   907     {
       
   908     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   909     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   910     CDesCArrayFlat* keystrokeArray = popup->KeystrokeArray();
       
   911     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
   912     const TInt offsetSize = 4;
       
   913     TInt count = keystrokeArray->Count();
       
   914     TInt offset = 0;
       
   915     TInt i = count - 1;
       
   916     
       
   917     if ( 0 != keystroke.Compare( KPinyinStar ) )
       
   918         {
       
   919         if ( keystrokeArray->Count() > inEffectKeystrokeArray->Count() )
       
   920             {
       
   921             keystrokeArray->AppendL( keystroke );
       
   922             keystrokeArray->Compress();
       
   923             return;
       
   924             }
       
   925         else
       
   926             {
       
   927             keystrokeArray->AppendL( keystroke );
       
   928             keystrokeArray->Compress();
       
   929             inEffectKeystrokeArray->AppendL( keystroke );
       
   930             inEffectKeystrokeArray->Compress();
       
   931             return;
       
   932             }
       
   933         }
       
   934     
       
   935     for ( i = count - 1; i >= 0; i-- )
       
   936         {
       
   937         if ( 0 == keystrokeArray->MdcaPoint( i ).Compare( KPinyinStar ) )
       
   938             {
       
   939             offset = offset + 1;
       
   940             }
       
   941         else
       
   942             {
       
   943             break;
       
   944             }
       
   945         }     
       
   946     if ( ( 0 <= offset ) && ( offset <= offsetSize ) )
       
   947         {
       
   948         if ( keystrokeArray->Count() > inEffectKeystrokeArray->Count() )
       
   949             {
       
   950             keystrokeArray->AppendL( keystroke );
       
   951             keystrokeArray->Compress();
       
   952             }
       
   953         else
       
   954             {
       
   955             keystrokeArray->AppendL( keystroke );
       
   956             keystrokeArray->Compress();
       
   957             inEffectKeystrokeArray->AppendL( keystroke );
       
   958             inEffectKeystrokeArray->Compress();
       
   959             }
       
   960         }
       
   961     else
       
   962         {
       
   963         if ( keystrokeArray->Count() > inEffectKeystrokeArray->Count() )
       
   964             {
       
   965             for ( TInt ii = 0; ii < offsetSize; ++ii )
       
   966             {
       
   967                 keystrokeArray->Delete( count - 1 -ii );
       
   968             }
       
   969             keystrokeArray->Compress();
       
   970             }
       
   971         else
       
   972             {
       
   973             for ( TInt ii = 0; ii < offsetSize; ++ii )
       
   974             {
       
   975                 keystrokeArray->Delete( count - 1 -ii );
       
   976                 inEffectKeystrokeArray->Delete( count - 1 -ii );
       
   977             }
       
   978             keystrokeArray->Compress();
       
   979             inEffectKeystrokeArray->Compress();
       
   980             }
       
   981         }    
       
   982     }
       
   983 
       
   984 
       
   985 // ---------------------------------------------------------
       
   986 // Get the keystroke.
       
   987 // ---------------------------------------------------------
       
   988 //
       
   989 void TAknFepInputStateEntryPinyinPhrase::GetKeystrokeNum( TInt aKey, TDes& aKeystroke ) 
       
   990     {
       
   991     switch(aKey)
       
   992         {
       
   993         case KKey0Code:
       
   994             aKeystroke.Append( KPinyinListSeparator );
       
   995             break;
       
   996         case KKey1Code:
       
   997             aKeystroke.Append( KPinyinListSeparator );
       
   998             break;
       
   999         case KKey2Code:
       
  1000             aKeystroke.Append(EPtiKey2);
       
  1001             break;
       
  1002         case KKey3Code:
       
  1003             aKeystroke.Append(EPtiKey3);
       
  1004             break;
       
  1005         case KKey4Code:
       
  1006             aKeystroke.Append(EPtiKey4);
       
  1007             break;
       
  1008         case KKey5Code:
       
  1009             aKeystroke.Append(EPtiKey5);
       
  1010             break;
       
  1011         case KKey6Code:
       
  1012             aKeystroke.Append(EPtiKey6);
       
  1013             break;
       
  1014         case KKey7Code:
       
  1015             aKeystroke.Append(EPtiKey7);
       
  1016             break;
       
  1017         case KKey8Code:
       
  1018             aKeystroke.Append(EPtiKey8);
       
  1019             break;
       
  1020         case KKey9Code:
       
  1021             aKeystroke.Append(EPtiKey9);
       
  1022             break;
       
  1023         default:
       
  1024             aKeystroke.Append(EPtiKeyStar);
       
  1025             break;
       
  1026         }
       
  1027     }
       
  1028 
       
  1029 // ---------------------------------------------------------
       
  1030 // Get the keystroke buf.
       
  1031 // ---------------------------------------------------------
       
  1032 //
       
  1033 void TAknFepInputStateEntryPinyinPhrase::GetKeystrokeBuf(const CDesCArrayFlat& aKeystrokeArray, TDes& aKeystrokeBuf )
       
  1034     {
       
  1035     for ( TInt i = 0; i < aKeystrokeArray.Count(); ++i )
       
  1036         {
       
  1037         aKeystrokeBuf.Append(aKeystrokeArray.MdcaPoint(i));
       
  1038         }
       
  1039     }
       
  1040 
       
  1041 // ---------------------------------------------------------
       
  1042 // clear keystroke.
       
  1043 // ---------------------------------------------------------
       
  1044 //
       
  1045 void TAknFepInputStateEntryPinyinPhrase::ClearKeystrokeBuf()
       
  1046     {
       
  1047     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
  1048     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
  1049     CDesCArrayFlat* keystrokeArray = popup->KeystrokeArray();
       
  1050     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
  1051     CDesCArrayFlat* showKeystrokeArray = popup->ShowKeystrokeArray();
       
  1052     CDesCArrayFlat* chooseChineseArray = popup->ChooseChineseCharacterArray();
       
  1053     CDesCArrayFlat* chooseChienseKeystroke = popup->ChooseChineseCharacterArrayKeystroke();
       
  1054     
       
  1055     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
  1056     TInt pinyinCount = ptiengine->PhoneticSpellingCount();
       
  1057     chooseChineseArray->Reset();
       
  1058     chooseChienseKeystroke->Reset();
       
  1059     
       
  1060     if ( 0 == pinyinCount )
       
  1061         {
       
  1062         keystrokeArray->Reset();
       
  1063         inEffectKeystrokeArray->Reset();
       
  1064         showKeystrokeArray->Reset();
       
  1065         }
       
  1066     }
       
  1067 
       
  1068 // ---------------------------------------------------------
       
  1069 // Get tome mark.
       
  1070 // ---------------------------------------------------------
       
  1071 //
       
  1072 void TAknFepInputStateEntryPinyinPhrase::GetPinyinToneMark( const CDesCArrayFlat& aKeystrokeArray, TDes& aPinyinToneMark )
       
  1073     {
       
  1074     TInt starCount = 0;
       
  1075     TInt pinyinToneMark;
       
  1076     TBuf<1> buf;
       
  1077     TInt count = aKeystrokeArray.Count();
       
  1078     const TInt toneMark0 = 0;
       
  1079     const TInt toneMark1 = 1;
       
  1080     const TInt toneMark2 = 2;
       
  1081     const TInt toneMark3 = 3;
       
  1082     const TInt toneMark4 = 4;
       
  1083     
       
  1084     if ( 0 == count )
       
  1085         {
       
  1086         aPinyinToneMark.Append( KPinyinTone0Valid );
       
  1087         return;
       
  1088         }
       
  1089     
       
  1090     for ( TInt i = count - 1; i >= 0; --i )
       
  1091         {
       
  1092         buf = aKeystrokeArray.MdcaPoint( i );
       
  1093         if ( 0 == buf.Compare( KPinyinStar ) )
       
  1094             {
       
  1095             starCount = starCount + 1; 
       
  1096             }
       
  1097         else
       
  1098             {
       
  1099             break;
       
  1100             }
       
  1101         }
       
  1102     
       
  1103     if ( 0 == starCount )
       
  1104         {
       
  1105         aPinyinToneMark.Append( KPinyinTone0Valid );
       
  1106         }
       
  1107     else
       
  1108         {
       
  1109         pinyinToneMark = starCount % KMaxStarCount;
       
  1110         switch ( pinyinToneMark )
       
  1111             {
       
  1112             case toneMark0:
       
  1113                 aPinyinToneMark.Append( KPinyinTone4Valid );
       
  1114                 break;
       
  1115             case toneMark1:
       
  1116                 aPinyinToneMark.Append( KPinyinTone0Valid );
       
  1117                 break;
       
  1118             case toneMark2:
       
  1119                 aPinyinToneMark.Append( KPinyinTone1Valid );
       
  1120                 break;
       
  1121             case toneMark3:
       
  1122                 aPinyinToneMark.Append( KPinyinTone2Valid );
       
  1123                 break;
       
  1124             case toneMark4:
       
  1125                 aPinyinToneMark.Append( KPinyinTone3Valid );
       
  1126                 break;
       
  1127             default:
       
  1128                break;
       
  1129             }
       
  1130         }
       
  1131     }
       
  1132 
       
  1133 // ---------------------------------------------------------
       
  1134 // Check keystroke.
       
  1135 // ---------------------------------------------------------
       
  1136 //
       
  1137 TBool TAknFepInputStateEntryPinyinPhrase::CheckKeystroke( TInt aKey, const CDesCArrayFlat& aKeystrokeArray )
       
  1138     {
       
  1139     CDesCArrayFlat* showKeystrokeArray = UIContainer()->
       
  1140         PinyinPopupWindow()->ShowKeystrokeArray();
       
  1141     
       
  1142     TInt count = showKeystrokeArray->Count();
       
  1143     TBuf<1> keyBuf;
       
  1144     
       
  1145     if ( count >= KMaxShowKeystrokeCount )
       
  1146         {
       
  1147         if ( ( 0 != aKeystrokeArray.MdcaPoint( aKeystrokeArray.Count() - 1 ).
       
  1148             Compare( KPinyinStar ) ) || ( aKey != EPtiKeyStar )  )
       
  1149             {
       
  1150             return EFalse;
       
  1151             }
       
  1152         }
       
  1153     
       
  1154     GetKeystrokeNum( aKey, keyBuf );
       
  1155 
       
  1156     if ( 0 == aKeystrokeArray.Count() )
       
  1157         {        
       
  1158         if ( ( 0 == keyBuf.Compare ( KPinyinStar ) ) || ( 0 == keyBuf.Compare ( KPinyinListSeparator ) ) )
       
  1159             {
       
  1160             return EFalse;
       
  1161             }        
       
  1162         }
       
  1163     else
       
  1164         {
       
  1165         if ( 0 == aKeystrokeArray.MdcaPoint( aKeystrokeArray.Count() - 1 ).Compare( KPinyinStar ) && 
       
  1166             ( 0 == keyBuf.Compare ( KPinyinListSeparator ) ) )
       
  1167             {
       
  1168             return EFalse;
       
  1169             }
       
  1170         
       
  1171         if ( 0 == aKeystrokeArray.MdcaPoint( aKeystrokeArray.Count() - 1 ).Compare( KPinyinListSeparator ) && 
       
  1172             ( 0 == keyBuf.Compare ( KPinyinStar ) ) )
       
  1173             {
       
  1174             return EFalse;
       
  1175             }
       
  1176         }
       
  1177     return ETrue;
       
  1178     }
       
  1179 
       
  1180 // ---------------------------------------------------------
       
  1181 // Check tome mark.
       
  1182 // ---------------------------------------------------------
       
  1183 //
       
  1184 void TAknFepInputStateEntryPinyinPhrase::CheckTonemarkL()
       
  1185     {
       
  1186     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
  1187     MAknFepUICtrlPinyinPopup* popup = UIContainer()->PinyinPopupWindow();
       
  1188     CDesCArrayFlat* keystrokeArray = popup->KeystrokeArray();
       
  1189     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
  1190     CDesCArrayFlat* showKeystrokeArray = popup->ShowKeystrokeArray();
       
  1191     CDesCArrayFlat* spelling = popup->OptimizeSpelling();
       
  1192 
       
  1193     TInt tonemarkCount = 0;
       
  1194     TInt spellTonemarkCount = 0;
       
  1195     TInt keystrokeStarCount = 0;
       
  1196 
       
  1197     TInt offset = 0;
       
  1198     TInt i = 0;
       
  1199     TBool checkReturn = EFalse;
       
  1200     
       
  1201     TBuf<1> buf;
       
  1202     TBuf<1> lastTonemark;
       
  1203     TBuf<1> lastSpellTonemark;
       
  1204     TBuf<1> tonmarkBuf0;
       
  1205     TBuf<1> tonmarkBuf1;
       
  1206     TBuf<1> tonmarkBuf2;
       
  1207     TBuf<1> tonmarkBuf3;
       
  1208     TBuf<1> tonmarkBuf4;
       
  1209     TBuf<KMaxPinyinLength> spellBuf;
       
  1210     
       
  1211     tonmarkBuf0.Append( KPinyinTone0Valid );
       
  1212     tonmarkBuf1.Append( KPinyinTone1Valid );
       
  1213     tonmarkBuf2.Append( KPinyinTone2Valid );
       
  1214     tonmarkBuf3.Append( KPinyinTone3Valid );
       
  1215     tonmarkBuf4.Append( KPinyinTone4Valid );
       
  1216     
       
  1217     if ( 0 == inEffectKeystrokeArray->Count() )
       
  1218         {
       
  1219         popup->SetTonemarkState( EFalse );
       
  1220         return;
       
  1221         }
       
  1222     
       
  1223     if ( keystrokeArray->Count() > inEffectKeystrokeArray->Count() )
       
  1224         {
       
  1225         popup->SetTonemarkState( EFalse );
       
  1226         return;
       
  1227         }
       
  1228     
       
  1229     for ( i = 0; i < showKeystrokeArray->Count(); ++i )
       
  1230         {
       
  1231         buf.Copy( showKeystrokeArray->MdcaPoint( i ) );
       
  1232         if ( 0 == buf.Compare( tonmarkBuf0 ) )
       
  1233             {
       
  1234             tonemarkCount = tonemarkCount + 1;
       
  1235             lastTonemark.Copy( showKeystrokeArray->MdcaPoint( i ) );
       
  1236             }
       
  1237         else if ( 0 == buf.Compare( tonmarkBuf1 ) )
       
  1238             {
       
  1239             tonemarkCount = tonemarkCount + 1;
       
  1240             lastTonemark.Copy( showKeystrokeArray->MdcaPoint( i ) );
       
  1241             }
       
  1242         else if ( 0 == buf.Compare( tonmarkBuf2 ) )
       
  1243             {
       
  1244             tonemarkCount = tonemarkCount + 1;
       
  1245             lastTonemark.Copy( showKeystrokeArray->MdcaPoint( i ) );
       
  1246             }
       
  1247         else if ( 0 == buf.Compare( tonmarkBuf3 ) )
       
  1248             {
       
  1249             tonemarkCount = tonemarkCount + 1;
       
  1250             lastTonemark.Copy( showKeystrokeArray->MdcaPoint( i ) );
       
  1251             }
       
  1252         else if ( 0 == buf.Compare( tonmarkBuf4 ) )
       
  1253             {
       
  1254             tonemarkCount = tonemarkCount + 1;
       
  1255             lastTonemark.Copy( showKeystrokeArray->MdcaPoint( i ) );
       
  1256             }
       
  1257         }
       
  1258     
       
  1259     if ( 0 == tonemarkCount )
       
  1260         {
       
  1261         popup->SetTonemarkState( ETrue );
       
  1262         return;
       
  1263         }
       
  1264 
       
  1265     for( i = 0; i < inEffectKeystrokeArray->Count(); ++i )
       
  1266         {
       
  1267         if ( 0 == inEffectKeystrokeArray->MdcaPoint( 
       
  1268             inEffectKeystrokeArray->Count() - 1 - i ).Compare( KPinyinStar ) )
       
  1269             {
       
  1270             keystrokeStarCount = keystrokeStarCount + 1;
       
  1271             }
       
  1272         else
       
  1273             {
       
  1274             break;
       
  1275             }
       
  1276         }
       
  1277     if ( keystrokeStarCount > 1 )
       
  1278         {
       
  1279         ptiengine->DeleteKeyPress();
       
  1280         }
       
  1281     
       
  1282     if ( keystrokeStarCount > KMaxStarCount )
       
  1283         {
       
  1284         offset = 1;
       
  1285         for ( i = 0; i < KMaxStarCount; ++i )
       
  1286             {
       
  1287             keystrokeArray->Delete( keystrokeArray->Count() - 1 );
       
  1288             inEffectKeystrokeArray->Delete( inEffectKeystrokeArray->Count() - 1 );
       
  1289             }
       
  1290         }
       
  1291     else
       
  1292         {
       
  1293         offset = keystrokeStarCount;
       
  1294         }
       
  1295     
       
  1296     if ( 0 == offset )
       
  1297         {
       
  1298         popup->SetTonemarkState( ETrue );
       
  1299         return;
       
  1300         }
       
  1301     else
       
  1302         {
       
  1303         for( i = 0; i < offset; ++i )
       
  1304             {
       
  1305             ptiengine->IncrementToneMark( ETrue );
       
  1306             spelling->Reset();
       
  1307             spelling->Compress();
       
  1308             ptiengine->GetPhoneticSpellingsL(*spelling);
       
  1309             for ( TInt j = 0; j < spelling->Count(); ++j )
       
  1310                 {
       
  1311                 spellTonemarkCount = 0;
       
  1312                 spellBuf.Copy( spelling->MdcaPoint( j ) );
       
  1313                 for ( TInt jj = 0; jj < spellBuf.Length(); ++jj )
       
  1314                     {
       
  1315                     if ( 0 == spellBuf.Mid( jj, 1 ).Compare( tonmarkBuf0 ) )
       
  1316                         {
       
  1317                         spellTonemarkCount = spellTonemarkCount + 1;
       
  1318                         lastSpellTonemark.Copy( tonmarkBuf0 );
       
  1319                         }
       
  1320                     else if ( 0 == spellBuf.Mid( jj, 1 ).Compare( tonmarkBuf1 ) )
       
  1321                         {
       
  1322                         spellTonemarkCount = spellTonemarkCount + 1;
       
  1323                         lastSpellTonemark.Copy( tonmarkBuf1 );
       
  1324                         }
       
  1325                     else if ( 0 == spellBuf.Mid( jj, 1 ).Compare( tonmarkBuf2 ) )
       
  1326                         {
       
  1327                         spellTonemarkCount = spellTonemarkCount + 1;
       
  1328                         lastSpellTonemark.Copy( tonmarkBuf2 );
       
  1329                         }
       
  1330                     else if ( 0 == spellBuf.Mid( jj, 1 ).Compare( tonmarkBuf3 ) )
       
  1331                         {
       
  1332                         spellTonemarkCount = spellTonemarkCount + 1;
       
  1333                         lastSpellTonemark.Copy( tonmarkBuf3 );
       
  1334                         }
       
  1335                     else if ( 0 == spellBuf.Mid( jj, 1 ).Compare( tonmarkBuf4 ) )
       
  1336                         {
       
  1337                         spellTonemarkCount = spellTonemarkCount + 1;
       
  1338                         lastSpellTonemark.Copy( tonmarkBuf4 );
       
  1339                         }
       
  1340                     }
       
  1341                 
       
  1342                 if ( ( tonemarkCount != spellTonemarkCount ) ||
       
  1343                     ( 0 != lastSpellTonemark.Compare( lastTonemark ) ))
       
  1344                     {
       
  1345                     checkReturn = EFalse;
       
  1346                     break;
       
  1347                     }
       
  1348                 else
       
  1349                     {
       
  1350                     checkReturn = ETrue;
       
  1351                     }
       
  1352                 }
       
  1353             if ( checkReturn )
       
  1354                 {
       
  1355                 break;
       
  1356                 }
       
  1357             }
       
  1358         }
       
  1359     
       
  1360     spelling->Reset();
       
  1361     spelling->Compress();
       
  1362     
       
  1363     if ( checkReturn )
       
  1364         {
       
  1365         popup->SetTonemarkState( ETrue );
       
  1366         return;
       
  1367         }
       
  1368     else
       
  1369         {
       
  1370         popup->SetTonemarkState( EFalse );
       
  1371         return;
       
  1372         }
       
  1373     
       
  1374     }
       
  1375 
       
  1376 // ---------------------------------------------------------
       
  1377 // Get change state.
       
  1378 // ---------------------------------------------------------
       
  1379 //
       
  1380 TBool TAknFepInputStateEntryPinyinPhrase::GetChangeState()
       
  1381     {
       
  1382     MAknFepUICtrlPinyinPopup* popup = UIContainer()->PinyinPopupWindow();
       
  1383     CDesCArrayFlat* KeystrokeArray = popup->KeystrokeArray();
       
  1384     CDesCArrayFlat* inEffectKeystrokeArray = popup->InEffectKeystrokeArray();
       
  1385     TInt firstIndex = -1;
       
  1386     TInt count = inEffectKeystrokeArray->Count() - 1;
       
  1387     
       
  1388 
       
  1389     
       
  1390     if ( 2 > inEffectKeystrokeArray->Count() )
       
  1391         {
       
  1392         return ETrue;
       
  1393         }
       
  1394     
       
  1395     for ( TInt i = count; i >= 0; i-- )
       
  1396         {
       
  1397         if ( 0 != inEffectKeystrokeArray->MdcaPoint( i ).
       
  1398             Compare( KPinyinStar ) )
       
  1399             {
       
  1400             firstIndex = i;
       
  1401             break;
       
  1402             }
       
  1403         }
       
  1404     
       
  1405     if ( ( 0 == firstIndex ) && ( !popup->GetTonemarkState() ) )
       
  1406         {
       
  1407         return EFalse;
       
  1408         }
       
  1409     
       
  1410     return ETrue;
       
  1411     }
       
  1412 
       
  1413 // ---------------------------------------------------------
       
  1414 // Append show keystroke.
       
  1415 // ---------------------------------------------------------
       
  1416 //
       
  1417 void TAknFepInputStateEntryPinyinPhrase::ShowKeystrokeAppendL( 
       
  1418                                           CDesCArrayFlat& aShowKeystrokeArray, 
       
  1419                                           const CDesCArrayFlat& aKeystrokeArray, 
       
  1420                                           const TDesC& aPinyinToneMark )
       
  1421     {
       
  1422     TInt showCount = 0;
       
  1423     TInt count = 0;
       
  1424     TBuf<1> buf;
       
  1425     count = aKeystrokeArray.Count();
       
  1426     showCount = aShowKeystrokeArray.Count();
       
  1427     
       
  1428     if ( ( count > 2 ) && ( showCount > 1 ) )
       
  1429         {
       
  1430         if ( 0 == aKeystrokeArray.MdcaPoint( count - 2 ).Compare( KPinyinStar ) )
       
  1431             {
       
  1432             aShowKeystrokeArray.Delete( showCount - 1 );
       
  1433             aShowKeystrokeArray.Compress();
       
  1434             aShowKeystrokeArray.AppendL( aPinyinToneMark );
       
  1435             }
       
  1436         else
       
  1437             {
       
  1438             aShowKeystrokeArray.AppendL( aPinyinToneMark );
       
  1439             }
       
  1440         }
       
  1441     else
       
  1442         {
       
  1443         aShowKeystrokeArray.AppendL( aPinyinToneMark );
       
  1444         }
       
  1445     }
       
  1446 
       
  1447 // ---------------------------------------------------------
       
  1448 // change  CBA.
       
  1449 // ---------------------------------------------------------
       
  1450 //
       
  1451 void TAknFepInputStateEntryPinyinPhrase::ChangeCbaL()
       
  1452     {
       
  1453     MAknFepUICtrlPinyinPopup* popup = UIContainer()->PinyinPopupWindow();
       
  1454     TInt lastRes = popup->GetLastResouce();
       
  1455     if ( lastRes == R_AKNFEP_SOFTKEYS_PHRASE_CREATION_SELECT_CANCEL_SELECT )
       
  1456         {
       
  1457         return;
       
  1458         }
       
  1459     iOwner->FepMan()->UpdateCbaL( R_AKNFEP_SOFTKEYS_PHRASE_CREATION_SELECT_CANCEL_SELECT );
       
  1460     popup->SetLastResouce( R_AKNFEP_SOFTKEYS_PHRASE_CREATION_SELECT_CANCEL_SELECT );
       
  1461     }
       
  1462 
       
  1463 // End of file