userdict/src/UdictFrm.cpp
changeset 0 9a3a17bfeb67
child 9 38691ba2fda5
equal deleted inserted replaced
-1:000000000000 0:9a3a17bfeb67
       
     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:     Japanese user dictionary's editor form.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include    "UdictFrm.h"
       
    24 #include    "UdictView.h"
       
    25 #include    "UdictElement.h"
       
    26 #include    "UserDict.hrh"
       
    27 #include    <UserDict.rsg>
       
    28 #include    <avkon.hrh>
       
    29 #include    <eikedwin.h>
       
    30 
       
    31 // CONSTANTS
       
    32 const TInt KNumOfInvalidFirstChar(12);
       
    33 const TText KInvalidFirstChars[KNumOfInvalidFirstChar] =
       
    34     {
       
    35     0x3041, // small A
       
    36     0x3043, // small I
       
    37     0x3045, // small U
       
    38     0x3047, // small E
       
    39     0x3049, // small O
       
    40     0x3063, // small TU
       
    41     0x3083, // small YA
       
    42     0x3085, // small YU
       
    43     0x3087, // small YO
       
    44     0x3092, // WO
       
    45     0x3093, // N
       
    46     0x30fc  // prolonged sound mark
       
    47     };
       
    48 
       
    49 // ============================ MEMBER FUNCTIONS ===============================
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CUserDictForm::NewL
       
    53 // Two-phased constructor.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CUserDictForm* CUserDictForm::NewL(const TUDictElement& aItem,
       
    57                                    CUserDictView& aView)
       
    58     {
       
    59     CUserDictForm* self = new(ELeave) CUserDictForm(aItem, aView);
       
    60 
       
    61     CleanupStack::PushL(self);
       
    62     self->ConstructL();
       
    63     CleanupStack::Pop(); // self
       
    64 
       
    65     return self;
       
    66     }
       
    67 
       
    68 // Destructor
       
    69 CUserDictForm ::~CUserDictForm ()
       
    70     {
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CUserDictForm::CUserDictForm
       
    75 // C++ default constructor can NOT contain any code, that
       
    76 // might leave.
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CUserDictForm::CUserDictForm(const TUDictElement& aItem, CUserDictView& aView)
       
    80 :iInitEntry(aItem), iView(aView)
       
    81     {
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CUserDictForm::ConstructL
       
    86 // Symbian 2nd phase constructor can leave.
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void CUserDictForm::ConstructL()
       
    90     {
       
    91     CAknForm::ConstructL();
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CUserDictForm::SaveL
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 TBool CUserDictForm::SaveL()
       
    99     {
       
   100     TBool res(ETrue);
       
   101 
       
   102     TUDictElement entry;
       
   103     GetData(entry);
       
   104     if (entry != iInitEntry)
       
   105         {
       
   106         if (IsValidReadingL(entry))
       
   107             {
       
   108             TRAPD(err, iView.SaveL(entry, iInitEntry));
       
   109             switch (err)
       
   110                 {
       
   111                 case KErrDiskFull: // No space to register
       
   112                     iView.ShowInformationNoteL(
       
   113                         R_UDICT_INFO_MEMORY_FULL_MESSAGE);
       
   114                     // if it's a new entry, returns EFalse
       
   115                     res = (iInitEntry.IsEmpty()) ? EFalse : ETrue;
       
   116                     break;
       
   117                 case KErrAlreadyExists: // Already registered
       
   118                     iView.ShowInformationNoteL(R_UDICT_ALREADY_REG_MESSAGE);
       
   119                     res = EFalse;
       
   120                     break;
       
   121                 default:
       
   122                     User::LeaveIfError(err);
       
   123                 }
       
   124             }
       
   125         else
       
   126             {
       
   127             TryChangeFocusToL(EUserDictReadingEdwin);
       
   128             static_cast<CEikEdwin*>(
       
   129                 Control(EUserDictReadingEdwin))->SetCursorPosL(0, EFalse);
       
   130             res = EFalse;
       
   131             }
       
   132         }
       
   133     return res;
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CUserDictForm::GetData
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 void CUserDictForm::GetData(TUDictElement& aEntry) const
       
   141     {
       
   142     CPlainText*  text =
       
   143         static_cast<CEikEdwin*>(Control(EUserDictWordEdwin))->Text();
       
   144     TInt docLen = text->DocumentLength();
       
   145     if (docLen > EUseDictWordBuffer)
       
   146         {
       
   147         docLen = EUseDictWordBuffer;
       
   148         }
       
   149     TPtrC word(static_cast<CEikEdwin*>(
       
   150         Control(EUserDictWordEdwin))->Text()->Read(0, docLen));
       
   151     aEntry.SetWord(word);
       
   152     text = static_cast<CEikEdwin*>(Control(EUserDictReadingEdwin))->Text();
       
   153     TPtrC reading(text->Read(0, text->DocumentLength()));
       
   154     aEntry.SetReading(reading);
       
   155     }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // CUserDictForm::SetDataL
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CUserDictForm::SetDataL() const
       
   162     {
       
   163     TPtrC ptrWord(iInitEntry.Word());
       
   164     static_cast<CEikEdwin*>(
       
   165         Control(EUserDictWordEdwin))->SetTextL(&ptrWord);
       
   166     TPtrC ptrReading(iInitEntry.Reading());
       
   167     static_cast<CEikEdwin*>(
       
   168         Control(EUserDictReadingEdwin))->SetTextL(&ptrReading);
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CUserDictForm::IsBlank
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 TBool CUserDictForm::IsBlank() const
       
   176     {
       
   177     return (((static_cast<CEikEdwin*>(
       
   178                 Control(EUserDictWordEdwin))->TextLength() <= 0)
       
   179         || (static_cast<CEikEdwin*>(
       
   180                 Control(EUserDictReadingEdwin))->TextLength() <= 0)
       
   181         ) ? ETrue : EFalse);
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CUserDictForm::IsValidReadingL
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 TBool CUserDictForm::IsValidReadingL(const TUDictElement& aItem) const
       
   189     {
       
   190     TBool valid(ETrue);
       
   191     const TDesC& reading = aItem.Reading();
       
   192     if (reading.Length() <= 0)
       
   193         return EFalse;
       
   194 
       
   195     // If the reading has a invalid first character,
       
   196     // it shows the information note.
       
   197     for (TInt i(0); i < KNumOfInvalidFirstChar && valid; i++)
       
   198         {
       
   199         if (reading[0] == KInvalidFirstChars[i])
       
   200             {
       
   201             iView.ShowInformationNoteL(R_UDICT_INVALID_1STCHAR_MESSAGE);
       
   202             valid = EFalse;
       
   203             }
       
   204         }
       
   205 
       
   206     return valid;
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CUserDictForm::PreLayoutDynInitL
       
   211 // From CEikDialog
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CUserDictForm::PreLayoutDynInitL()
       
   215     {
       
   216     SetDataL();
       
   217     // if the form edits a new word, "Done" softkey is hidden.
       
   218     if (iInitEntry.IsEmpty())
       
   219         {
       
   220         ButtonGroupContainer().SetCommandSetL( R_UDICT_SOFTKEYS_EMPTY_CANCEL_EMPTY );
       
   221         ButtonGroupContainer().DrawNow();
       
   222         iSoftkeyResource = R_UDICT_SOFTKEYS_EMPTY_CANCEL_EMPTY;
       
   223         }
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CUserDictForm::OkToExitL
       
   228 // From CEikDialog
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 TBool CUserDictForm::OkToExitL(TInt aKeycode)
       
   232     {
       
   233     TBool ret = ETrue;
       
   234     switch (aKeycode)
       
   235         {
       
   236         case EAknSoftkeyDone:
       
   237             ret = SaveL();
       
   238             break;
       
   239         case EAknSoftkeyEmpty:
       
   240             ret = EFalse;
       
   241             break;
       
   242         case EAknSoftkeyCancel:
       
   243         default:
       
   244             break;
       
   245         }
       
   246     return ret;
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CUserDictForm::HandleControlStateChangeL
       
   251 // From MCoeControlObserver
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CUserDictForm::HandleControlStateChangeL(TInt aControlId)
       
   255     {
       
   256     // If the entry fields are blank, "Done" softkey is hidden.
       
   257     if (aControlId == EUserDictWordEdwin
       
   258        || aControlId == EUserDictReadingEdwin)
       
   259         {
       
   260         TUint res = 0;
       
   261         if (IsBlank())
       
   262             {
       
   263             res = R_UDICT_SOFTKEYS_EMPTY_CANCEL_EMPTY;
       
   264             }
       
   265         else
       
   266             {
       
   267             res = R_UDICT_SOFTKEYS_DONE_CANCEL_DONE;
       
   268             }
       
   269         if (iSoftkeyResource != res)
       
   270             {
       
   271             ButtonGroupContainer().SetCommandSetL( res );
       
   272             ButtonGroupContainer().DrawDeferred();
       
   273             iSoftkeyResource = res;
       
   274             }
       
   275         }
       
   276     CAknForm::HandleControlStateChangeL(aControlId);
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // CUserDictForm::OfferKeyEventL
       
   281 // From CCoeControl
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 TKeyResponse CUserDictForm::OfferKeyEventL(
       
   285     const TKeyEvent& aKeyEvent,TEventCode aType)
       
   286     {
       
   287     TKeyResponse res(EKeyWasConsumed);
       
   288     if (aType == EEventKey && aKeyEvent.iCode == EKeyOK)
       
   289         {
       
   290         if (aKeyEvent.iRepeats <= 0 && !IsBlank())
       
   291             {
       
   292             TryExitL(EAknSoftkeySelect);
       
   293             }
       
   294         }
       
   295     else
       
   296         {
       
   297         res = CAknForm::OfferKeyEventL(aKeyEvent, aType);
       
   298         }
       
   299     return res;
       
   300     }
       
   301 
       
   302 //  End of File