fep/aknfep/src/AknFepUiInputStateEntryPinyin.cpp
changeset 36 a7632c26d895
parent 35 0f326f2e628e
child 42 b3eaa440ab06
equal deleted inserted replaced
35:0f326f2e628e 36:a7632c26d895
     1 /*
       
     2 * Copyright (c) 2002-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 CAknFepUIInputStatePinyin methods.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 #include "AknFepPanic.h"
       
    31 #include "AknFepUiInputStateEntryPinyin.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"
       
    38 #include "AknFepManager.h"
       
    39 
       
    40 #include <PtiEngine.h>                          //CPtiEngine
       
    41 #include <PtiDefs.h>                            //keys
       
    42 #include <avkon.rsg>
       
    43 
       
    44 const TText KPinyinTone4Valid = 0x0020;
       
    45 const TText KPinyinTone4Invalid = 0x02D9;
       
    46 
       
    47 
       
    48 TAknFepInputStateEntryPinyin::TAknFepInputStateEntryPinyin(
       
    49                                 MAknFepUIManagerStateInterface* aOwner,
       
    50                                 MAknFepUICtrlContainerChinese* aUIContainer)
       
    51     :TAknFepInputStateChineseBase(aOwner, aUIContainer)
       
    52     {
       
    53     iState = EEntry;
       
    54 
       
    55     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
    56     uiContainer->FocusCandidatePane(EFalse);
       
    57     uiContainer->CandidatePane()->ShowCandidateOrdinals(EFalse);
       
    58     uiContainer->SetLayout(MAknFepUICtrlContainerChinese::ELayoutInput);
       
    59     uiContainer->CandidatePane()->SelectFirst(); 
       
    60     uiContainer->ShowVerticalScrollArrows(EFalse);
       
    61     uiContainer->ShowHorizontalScrollArrows(EFalse);
       
    62     uiContainer->InputPane()->SetOverrideFontId(0);
       
    63 
       
    64     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
    65     if (ptiengine->InputMode() != EPtiEnginePinyin)
       
    66         {
       
    67         ptiengine->SetInputMode(EPtiEnginePinyin);
       
    68         }
       
    69     ptiengine->SetCandidatePageLength(MAknFepUICtrlContainerChinese::ELayoutInput);
       
    70     ptiengine->EnableToneMarks(ETrue); 
       
    71 
       
    72     // in the case that we are coming back to the input pane from the candidate pane, 
       
    73     // we need to ensure that the current selection is selected correctly
       
    74     ImplicitlyUpdateSelection();
       
    75 
       
    76     // however we also need to clear the deliberate selection, in case we are not
       
    77     // coming back to the input pane from the candidate pane
       
    78     ClearDeliberateSelection();
       
    79     }
       
    80 void TAknFepInputStateEntryPinyin::HandleCommandL(TInt /*aCommandId*/)
       
    81 	{	
       
    82     DeliberatelyUpdateSelection();	
       
    83  	}
       
    84 
       
    85 TBool TAknFepInputStateEntryPinyin::HandleKeyL(TInt aKey, TKeyPressLength aLength)
       
    86     {
       
    87     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
    88     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
    89     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
    90 
       
    91     if(aKey == EKeyBackspace)
       
    92         {
       
    93 		if (iOwner->PtiEngine()->DeleteKeyPress().Length())
       
    94             {
       
    95             ImplicitlyUpdateSelection();
       
    96             }
       
    97         else
       
    98             {
       
    99             ClearDeliberateSelection();
       
   100             iOwner->FepMan()->TryCloseUiL(); // no more keys, close the UI.
       
   101             if (aLength == ELongKeyPress)
       
   102                 {
       
   103                 iOwner->FepMan()->SetLongClearAfterCloseUI(ETrue);
       
   104                 }
       
   105             }
       
   106         }
       
   107     else if(aKey == EKeyRightArrow)
       
   108         {
       
   109         if(popup->IsEnabled())
       
   110             {
       
   111             if(popup->SelectNextPhrase())
       
   112                 {
       
   113                 DeliberatelyUpdateSelection();
       
   114                 }
       
   115             }
       
   116         }
       
   117     else if(aKey == EKeyLeftArrow)
       
   118         {
       
   119         if(popup->IsEnabled())
       
   120             {
       
   121             if(popup->SelectPrevPhrase())
       
   122                 {
       
   123                 DeliberatelyUpdateSelection();
       
   124                 }
       
   125             }
       
   126         }
       
   127     else if(aKey == EKeyOK || aKey == EKeyDownArrow)
       
   128         {
       
   129         if(popup->IsEnabled())
       
   130             {
       
   131             popup->Enable(EFalse);
       
   132             }
       
   133         iOwner->ChangeState(ECandidate);        
       
   134         }
       
   135     else if (aLength == EShortKeyPress) // don't want repeats on these keys
       
   136         {
       
   137      	iOwner->FepMan()->SetCcpuFlag(CAknFepManager::ECcpuStateIgnoreStarUp);        
       
   138         if(iOwner->IsValidChineseInputKey(aKey))
       
   139             {
       
   140             TInt stringBeforeLength(0);
       
   141             TInt stringAfterLength(0);
       
   142         
       
   143             stringBeforeLength = ptiengine->GetPhoneticSpelling(1).Length();
       
   144             stringAfterLength = ptiengine->AppendKeyPress((TPtiKey)aKey).Length();
       
   145             
       
   146             if (stringBeforeLength != stringAfterLength)
       
   147                 {
       
   148                 if (ptiengine->GetPhoneticSpelling(1).Length() == 1)
       
   149                     {
       
   150                     iOwner->FepMan()->UpdateCbaL(R_AVKON_SOFTKEYS_EMPTY);
       
   151                     }
       
   152                 
       
   153                 ImplicitlyUpdateSelection();
       
   154                 }
       
   155             
       
   156             else
       
   157                 {
       
   158                 iOwner->FepMan()->PlaySound(EAvkonSIDErrorTone);
       
   159                 }           
       
   160             }
       
   161         else if(aKey == EPtiKeyStar) // we increment the tone mark.
       
   162             {
       
   163             if(ptiengine->IncrementToneMark(ETrue))
       
   164                 {
       
   165                 ImplicitlyUpdateSelection();
       
   166                 }
       
   167             }
       
   168         }
       
   169         
       
   170     return ETrue;
       
   171     }
       
   172 
       
   173 void TAknFepInputStateEntryPinyin::DeliberatelyUpdateSelection()
       
   174     {
       
   175     TPtr deliberateSelection = iOwner->GetLatestDeliberateSelection();
       
   176     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   177     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   178     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   179 
       
   180     // the selected is based on whole spelling candidates, not only current display page
       
   181     TInt selected = popup->CurrentSelection(); 
       
   182     ptiengine->EnableToneMarks(EFalse);
       
   183     TPtrC spelling = ptiengine->GetPhoneticSpelling(selected + 1); // our index is zero based, engine index is one based
       
   184     deliberateSelection = spelling.Left(
       
   185                 MAknFepUICtrlInputPane::EMaxInputCharsPinyinPopupNotIncludingToneMark);
       
   186     ptiengine->EnableToneMarks(ETrue);
       
   187     
       
   188     RefreshUI(selected);
       
   189     }
       
   190 
       
   191 void TAknFepInputStateEntryPinyin::ImplicitlyUpdateSelection()
       
   192     {
       
   193     TPtr oldDeliberateSelection = iOwner->GetLatestDeliberateSelection();
       
   194     TInt oldDeliberateSelectionLength = oldDeliberateSelection.Length();
       
   195     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   196 
       
   197     ptiengine->EnableToneMarks(EFalse);
       
   198     TInt pinyinCount = ptiengine->PhoneticSpellingCount();
       
   199     TInt maxUIPinyinCount = MAknFepUICtrlPinyinPopup::EMaxCandidates;
       
   200     TInt visible = pinyinCount < maxUIPinyinCount ? pinyinCount : maxUIPinyinCount;
       
   201     TInt newSelection = 0;
       
   202     for(TInt i = 0; i < visible; i++)
       
   203         {
       
   204         TPtrC spelling = ptiengine->GetPhoneticSpelling(i + 1);  // our index is zero based, engine index is one based
       
   205         TInt spellingLength = spelling.Length();
       
   206 
       
   207         // figure out how many characters we are comparing
       
   208         TInt compareLength = oldDeliberateSelectionLength <= spellingLength ? 
       
   209             oldDeliberateSelectionLength : spellingLength;
       
   210 
       
   211         if(oldDeliberateSelection.Left(compareLength) == spelling.Left(compareLength))
       
   212             {
       
   213             // as soon as a substring match is found, the 
       
   214             // highlight position is set to that candidate in the new list. 
       
   215             // note that in the case of the first character entered, the
       
   216             // oldBuf will be empty so it will always match... which is 
       
   217             // fine as we want to select the top one anyway, so we will
       
   218             // quit the loop early.
       
   219             newSelection = i;
       
   220             break;
       
   221             }
       
   222         }
       
   223     ptiengine->EnableToneMarks(ETrue);
       
   224     UIContainer()->PinyinPopupWindow()->SetFlag(MAknFepUICtrlPinyinPopup::ESpellingChanged);
       
   225     RefreshUI(newSelection);
       
   226     }
       
   227 
       
   228 void TAknFepInputStateEntryPinyin::ClearDeliberateSelection()
       
   229     {
       
   230     // we must have just deleted the last character, 
       
   231     // or we are starting a new pinyin session, so wipe the last deliberate selection
       
   232     TPtr oldDeliberateSelection = iOwner->GetLatestDeliberateSelection();
       
   233     oldDeliberateSelection = KNullDesC;
       
   234     }
       
   235 
       
   236 void TAknFepInputStateEntryPinyin::RefreshUI(TInt aSelection)
       
   237     {
       
   238     CPtiEngine* ptiengine = iOwner->PtiEngine();
       
   239     MAknFepUICtrlContainerChinese* uiContainer = UIContainer();
       
   240     MAknFepUICtrlPinyinPopup* popup = uiContainer->PinyinPopupWindow();
       
   241     MAknFepUICtrlInputPane* inputPane = uiContainer->InputPane();
       
   242 
       
   243     // get cursor position
       
   244     TPoint baseLine = TPoint(0,0);  
       
   245     TInt height = 0;
       
   246     TInt ascent = 0;
       
   247     TRAPD(ret,iOwner->FepMan()->GetScreenCoordinatesL(baseLine,height,ascent));
       
   248     if (ret == KErrNone)
       
   249         {
       
   250         uiContainer->SetContainerPosition(baseLine, height);    
       
   251         }
       
   252 
       
   253     TText toneMark;
       
   254     TBool toneMarkEntered = ptiengine->ToneMark(toneMark);
       
   255     TText invalidToneMark = toneMark;
       
   256     TBuf<1> validToneMarkBuf;
       
   257     TBuf<1> invalidToneMarkBuf;
       
   258     if(toneMarkEntered)
       
   259         {
       
   260         // override specific invalid tonemark character only, the others are the same character 
       
   261         // when both valid and invalid
       
   262         if(toneMark == KPinyinTone4Valid)
       
   263             invalidToneMark = KPinyinTone4Invalid;
       
   264         validToneMarkBuf.Append(invalidToneMark);
       
   265         invalidToneMarkBuf.Append(invalidToneMark);
       
   266         }
       
   267 
       
   268     // to start with, disable the tone marks so we can get the list of all pinyin matches
       
   269     // later we will figure out which are invalid
       
   270     ptiengine->EnableToneMarks(EFalse);
       
   271     TInt pinyinCountWithoutToneMarks = ptiengine->PhoneticSpellingCount();
       
   272     TInt maxUIPinyinCount = popup->EMaxCandidates;
       
   273     TInt visible = pinyinCountWithoutToneMarks < maxUIPinyinCount ? 
       
   274             pinyinCountWithoutToneMarks : maxUIPinyinCount;
       
   275     TInt visibleCount = 0;
       
   276     TInt spellingStartIndex = 0;
       
   277     TBool pinyinSpellingChanged = popup->IsFlagSet(MAknFepUICtrlPinyinPopup::ESpellingChanged);
       
   278     TBool pinyinDisplayPageChanged = popup->IsFlagSet(MAknFepUICtrlPinyinPopup::EDispPageChanged);
       
   279 
       
   280     if(pinyinCountWithoutToneMarks > 0)
       
   281         {
       
   282         if(pinyinSpellingChanged)
       
   283             {
       
   284             visibleCount = visible;
       
   285             spellingStartIndex = 0;
       
   286             popup->SetVisibleCount(visible);
       
   287             }
       
   288         else if(pinyinDisplayPageChanged) 
       
   289             {
       
   290             visibleCount = popup->VisibleSelectionCount();
       
   291             spellingStartIndex = popup->CurrentPageStartIndex();
       
   292             }
       
   293         if(pinyinDisplayPageChanged || pinyinSpellingChanged)
       
   294             {
       
   295             TInt index;
       
   296             for(TInt i = 0; i < visibleCount; i++)
       
   297                 {
       
   298                 if(pinyinDisplayPageChanged)
       
   299                     {
       
   300                     index = spellingStartIndex + i;
       
   301                     }
       
   302                 else
       
   303                     {
       
   304                     index = i;
       
   305                     }
       
   306                 TPtrC spelling = ptiengine->GetPhoneticSpelling(index + 1);  // our index is zero based, engine index is one based
       
   307                 popup->SetItemText(i, spelling);
       
   308                 popup->SetToneMark(i, validToneMarkBuf); 
       
   309                 if(i == aSelection)
       
   310                     {
       
   311                     // fill in input pane even if it can't be seen, for when we have to hide popup window
       
   312                     inputPane->SetText(spelling);
       
   313                     inputPane->SetToneMark(validToneMarkBuf); 
       
   314                     }
       
   315                 }
       
   316             popup->ClearFlag(MAknFepUICtrlPinyinPopup::EDispPageChanged);
       
   317             }
       
   318         if(pinyinSpellingChanged)
       
   319             {
       
   320             popup->SplitSpellingIntoPages(visible);
       
   321             }
       
   322         popup->SetDisplayPage(aSelection);
       
   323         
       
   324         if(pinyinSpellingChanged)
       
   325             {
       
   326             popup->ClearFlag(MAknFepUICtrlPinyinPopup::ESpellingChanged);
       
   327             visibleCount = popup->VisibleSelectionCount();
       
   328             spellingStartIndex = popup->CurrentPageStartIndex();
       
   329             TInt index;
       
   330             for(TInt i = 0; i < visibleCount; i++)
       
   331                 {
       
   332                 index = spellingStartIndex + i;
       
   333                 
       
   334                 TPtrC spelling = ptiengine->GetPhoneticSpelling(index + 1);  // our index is zero based, engine index is one based
       
   335                 popup->SetItemText(i, spelling);
       
   336                 popup->SetToneMark(i, validToneMarkBuf); 
       
   337                 if(i == aSelection)
       
   338                     {
       
   339                     // fill in input pane even if it can't be seen, for when we have to hide popup window
       
   340                     inputPane->SetText(spelling);
       
   341                     inputPane->SetToneMark(validToneMarkBuf); 
       
   342                     }
       
   343                 }
       
   344             }
       
   345         else
       
   346             {
       
   347             inputPane->SetText(ptiengine->GetPhoneticSpelling(aSelection + 1));
       
   348             inputPane->SetToneMark(validToneMarkBuf); 
       
   349             }
       
   350         popup->PopupSizeChanged();
       
   351         }
       
   352 
       
   353     // turn tone marks back on so that we know where we are.
       
   354     ptiengine->EnableToneMarks(ETrue);
       
   355 
       
   356     // this is where we start to figure out whether the tonemarks are valid,
       
   357     // whether the selected tonemark is valid, and what is the index of the 
       
   358     // selected candidate in the list of candidates with tonemarks...
       
   359     TBool selectionToneMarkValid = EFalse;
       
   360     TInt selectionIndexAdjustedForToneMarkValidity = aSelection;
       
   361 
       
   362     // we only need to deal with tone marks if there is one
       
   363     if(toneMarkEntered)
       
   364         {
       
   365         if(pinyinCountWithoutToneMarks > 0)
       
   366             {
       
   367             visibleCount = visible;
       
   368 
       
   369             for(TInt i = 0; i < visibleCount; i++)
       
   370                 {
       
   371                 TBool valid = EFalse;
       
   372                 
       
   373                 // compare the list of answers with the ones in the UI controls... 
       
   374                 // any missing from this list don't support the tone mark
       
   375                 TBuf<MAknFepUICtrlInputPane::EMaxInputCharsPinyinPopupNotIncludingToneMark> nextCandidateWithoutToneMark;
       
   376                 popup->GetItemText(i, nextCandidateWithoutToneMark);
       
   377 
       
   378                 // disable tone marks temporarily, as we need to switch the spelling, and it might have 
       
   379                 // no valid tonemarks
       
   380                 ptiengine->EnableToneMarks(EFalse); 
       
   381 
       
   382                 // temporarily change the current selection in the engine, so 
       
   383                 // that we can see what tonemarks would be valid for it
       
   384                 // we will set the real selection later on
       
   385 
       
   386                 ptiengine->SelectPhoneticSpelling(spellingStartIndex + i + 1);
       
   387                 // if it's valid, we need to find it's position in the with tonemark list
       
   388                 // as the lists may be ordered differently, and different lengths
       
   389                 valid = ptiengine->IsToneMarkValidForSpelling();
       
   390                 if(valid)
       
   391                     {
       
   392                     ptiengine->EnableToneMarks(ETrue); 
       
   393                     TInt pinyinCountWithToneMarks = ptiengine->PhoneticSpellingCount();
       
   394 
       
   395                     for(TInt j = 0; j < pinyinCountWithToneMarks; j++)
       
   396                         {
       
   397 
       
   398                         // use j here not i as we are looking at the list with tonemarks
       
   399                         TPtrC nextCandidateWithToneMark = ptiengine->GetPhoneticSpelling(j + 1);  // our index is zero based, engine index is one based
       
   400 
       
   401                         if(nextCandidateWithToneMark == nextCandidateWithoutToneMark)
       
   402                             {
       
   403                             // we are setting the tone mark for the i'th entry in the list without tonemarks
       
   404                             // but if its tonemark is valid, the selection in the engine will have to be relative
       
   405                             // to the list with tonemarks
       
   406                             if( i + spellingStartIndex == aSelection)
       
   407                                 {
       
   408                                 selectionToneMarkValid = ETrue;
       
   409                                 selectionIndexAdjustedForToneMarkValidity =  j;
       
   410 
       
   411                                 // fill in input pane even if it can't be seen, for when we have to hide popup window
       
   412                                 inputPane->SetText(nextCandidateWithoutToneMark);
       
   413                                 }
       
   414                             break;
       
   415                             }
       
   416                         }
       
   417                     }
       
   418                 popup->SetItemToneMarkValidity(i, valid); 
       
   419                 popup->SetToneMark(i, valid ? validToneMarkBuf : invalidToneMarkBuf); 
       
   420 
       
   421                 // fill in input pane even if it can't be seen, for when we have to hide popup window
       
   422                 if(i == aSelection)
       
   423                     {
       
   424                     inputPane->SetToneMarkValidity(valid); 
       
   425                     inputPane->SetToneMark(valid ? validToneMarkBuf : invalidToneMarkBuf); 
       
   426                     }
       
   427                 
       
   428                 }
       
   429             }
       
   430     }
       
   431     // set the spelling for the selection with no tone mark
       
   432     ptiengine->EnableToneMarks(EFalse); 
       
   433     ptiengine->SelectPhoneticSpelling(popup->CurrentSelection() + 1); // our index is zero based, engine index is one based
       
   434     
       
   435     // now switch the tonemark on only if the selection has a valid tonemark
       
   436     // and then set the spelling to the adjusted tonemark position
       
   437     ptiengine->EnableToneMarks(selectionToneMarkValid); 
       
   438     ptiengine->SelectPhoneticSpelling(selectionIndexAdjustedForToneMarkValidity + 1); // our index is zero based, engine index is one based
       
   439 
       
   440     // update the candidate pane.
       
   441     uiContainer->CandidatePane()->SetCandidateBuffer(ptiengine->CandidatePage());
       
   442     uiContainer->Enable(ETrue);
       
   443 
       
   444     // need to enable the pinyin popup after the container so that it is raised to the front
       
   445     popup->Enable(pinyinCountWithoutToneMarks > 0);
       
   446     }
       
   447 
       
   448 void TAknFepInputStateEntryPinyin::InitializeStateL(void)
       
   449     {
       
   450     iOwner->FepMan()->UpdateCbaL(R_AVKON_SOFTKEYS_EMPTY);
       
   451     }
       
   452 // End of file