userdict/src/UdictContainer.cpp
changeset 0 9a3a17bfeb67
child 13 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 View Container control.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include "UdictContainer.h"
       
    24 #include "UdictContainer.inl"
       
    25 #include "UdictView.h"
       
    26 #include "UdictModel.h"
       
    27 #include "UdictModel.inl"
       
    28 #include "UdictApp.h"
       
    29 #include "UserDict.hrh"
       
    30 #include "udict.hlp.hrh"
       
    31 #include <UserDict.rsg>
       
    32 #include <aknconsts.h>
       
    33 #include <aknlists.h>
       
    34 #include <eikclbd.h>
       
    35 #include <AknIconArray.h>
       
    36 #include <avkon.mbg>
       
    37 #include <AknWaitNoteWrapper.h>
       
    38 #include <eikmenub.h>
       
    39 #include <gulicon.h>
       
    40 #include <AknIconUtils.h>
       
    41 
       
    42 // CONSTANTS
       
    43 const TInt KGulIconGranurality(10);
       
    44 const TInt KWaitNoteOneStepDeletion(20);
       
    45 
       
    46 // ============================ MEMBER FUNCTIONS ===============================
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CUserDictContainer::CUserDictContainer
       
    50 // C++ default constructor can NOT contain any code, that
       
    51 // might leave.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CUserDictContainer::CUserDictContainer(
       
    55                     CUserDictModel& aModel,
       
    56                     CAknView& aView)
       
    57 :iModel(aModel), iView(aView), iWaitNoteCounter(-1)
       
    58     {
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CUserDictContainer::ConstructL
       
    63 // Symbian 2nd phase constructor can leave.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CUserDictContainer::ConstructL()
       
    67     {
       
    68     CreateWindowL();
       
    69 
       
    70     iListBox = new (ELeave) CAknSingleStyleListBox;
       
    71     iListBox->SetContainerWindowL(*this);
       
    72     iListBox->ConstructL(this, EAknListBoxMarkableList);
       
    73     iListBox->CreateScrollBarFrameL(ETrue);
       
    74     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
    75         CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
    76     iModel.InitListL();
       
    77     iListBox->Model()->SetItemTextArray(iModel.List());
       
    78     iListBox->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);
       
    79     SetGraphicIconL();
       
    80 
       
    81     // Here is NOT call ActivateL(), because the client size isn't set yet.
       
    82     // The caller of this ConstructL() must call ActivateL().
       
    83     }
       
    84 
       
    85 // Destructor
       
    86 CUserDictContainer::~CUserDictContainer()
       
    87     {
       
    88     delete iListBox;
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CUserDictContainer::SetFocusL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CUserDictContainer::SetFocusL(TInt aIndex) const
       
    96     {
       
    97     AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(
       
    98         iListBox, aIndex, EFalse);
       
    99     DrawNow();
       
   100     }
       
   101 
       
   102 // -----------------------------------------------------------------------------
       
   103 // CUserDictContainer::DeleteItemL
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 void CUserDictContainer::DeleteItemL()
       
   107     {
       
   108     TInt count = iListBox->View()->SelectionIndexes()->Count();
       
   109     TInt index(iListBox->CurrentItemIndex());
       
   110     if (count > 0)
       
   111         {
       
   112         const CListBoxView::CSelectionIndexArray* selectionIndexes =
       
   113            iListBox->View()->SelectionIndexes();
       
   114         TKeyArrayFix key(0, ECmpTInt);
       
   115         const_cast<CListBoxView::CSelectionIndexArray*>(
       
   116                                 selectionIndexes)->Sort(key);
       
   117         iWaitNoteCounter = --count;
       
   118         ShowWaitNoteL();
       
   119         TInt selectionIndex;
       
   120         selectionIndexes->FindIsq(index, key, selectionIndex);
       
   121         // It sets the focused item index.
       
   122         // If the focused item has been removed, the focus goes to the next
       
   123         // item. Otherwise, the focus stays in there.
       
   124         SetFocusL(index - selectionIndex);
       
   125         }
       
   126     else
       
   127         {
       
   128         iModel.DeleteWordL(index);
       
   129         AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(
       
   130             iListBox, index, ETrue);
       
   131         DrawNow();
       
   132         }
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // void CUserDictContainer::SetGraphicIconL
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CUserDictContainer::SetGraphicIconL()
       
   140     {
       
   141     // Creates gul icon.
       
   142     CArrayPtr<CGulIcon>* icons = new( ELeave ) CAknIconArray(
       
   143                                   KGulIconGranurality );
       
   144     CleanupStack::PushL(icons);
       
   145 
       
   146     // Appends icon.
       
   147     CreateIconAndAddToArrayL(icons, KAvkonBitmapFile,
       
   148         EMbmAvkonQgn_indi_marked_add, EMbmAvkonQgn_indi_marked_add_mask);
       
   149 
       
   150     // Sets graphics as ListBox icon.
       
   151     iListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
       
   152 
       
   153     CleanupStack::Pop(); // icons
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CUserDictContainer::ShowWaitNoteL
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CUserDictContainer::ShowWaitNoteL()
       
   161     {
       
   162     CAknWaitNoteWrapper* note = CAknWaitNoteWrapper::NewL();
       
   163     iModel.DelaySavingL();
       
   164     TRAPD(err, note->ExecuteL(R_UDICT_WAIT_NOTE, *this));
       
   165     delete note;
       
   166     iModel.SaveNowL();
       
   167     iWaitNoteCounter = -1;
       
   168     User::LeaveIfError(err);
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CUserDictContainer::CreateIconAndAddToArrayL
       
   173 // Creates the icon and adds it to the array if it was successful
       
   174 // -----------------------------------------------------------------------------
       
   175 void CUserDictContainer::CreateIconAndAddToArrayL(CArrayPtr<CGulIcon>*& aIconsArray,
       
   176                                                const TDesC& aIconFile,
       
   177                                                TInt aBitmap, TInt aMask/* = -1*/)
       
   178     {
       
   179     CGulIcon* icon = CGulIcon::NewLC();
       
   180     CFbsBitmap* bitmap = NULL;
       
   181     CFbsBitmap* mask = NULL;
       
   182     if (aMask >= 0)
       
   183         {
       
   184         AknIconUtils::CreateIconL( bitmap, mask, aIconFile, aBitmap, aMask);
       
   185         }
       
   186     else
       
   187         {
       
   188         bitmap = AknIconUtils::CreateIconL( aIconFile, aBitmap);
       
   189         }
       
   190 
       
   191     if (bitmap)
       
   192         {
       
   193         icon->SetBitmap(bitmap);
       
   194         if (mask)
       
   195             icon->SetMask(mask);
       
   196         aIconsArray->AppendL(icon);
       
   197         CleanupStack::Pop(icon);
       
   198         }
       
   199     else
       
   200         {
       
   201         CleanupStack::PopAndDestroy(icon);
       
   202         }
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CUserDictContainer::SizeChanged()
       
   207 // Called by framework when the view size is changed
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 void CUserDictContainer::SizeChanged()
       
   211     {
       
   212     iListBox->SetRect(Rect());
       
   213     }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CUserDictContainer::CountComponentControls
       
   217 // From CCoeControl
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 TInt CUserDictContainer::CountComponentControls() const
       
   221     {
       
   222     return 1; // return a number of controls inside this container
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CUserDictContainer::ComponentControl
       
   227 // From CCoeControl
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 CCoeControl* CUserDictContainer::ComponentControl(TInt aIndex) const
       
   231     {
       
   232     switch (aIndex)
       
   233         {
       
   234         case 0:
       
   235             return iListBox;
       
   236         default:
       
   237             return NULL;
       
   238         }
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CUserDictContainer::OfferKeyEventL
       
   243 // From CCoeControl
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 TKeyResponse CUserDictContainer::OfferKeyEventL(
       
   247                 const TKeyEvent& aKeyEvent, TEventCode aType)
       
   248     {
       
   249     TKeyResponse res(EKeyWasNotConsumed);
       
   250 
       
   251     if (aType == EEventKeyDown
       
   252         && IsFocused())
       
   253         {
       
   254         // If user pushes some digit key, it starts the User dictionary editor.
       
   255         TChar key(aKeyEvent.iScanCode);
       
   256         if (key.IsDigit())
       
   257             {
       
   258             iView.HandleCommandL(EUserDictCmdNewWord);
       
   259             res = EKeyWasConsumed;
       
   260             }
       
   261         }
       
   262     else if (aType == EEventKey)
       
   263         {
       
   264         switch (aKeyEvent.iCode)
       
   265             {
       
   266             case EKeyBackspace:
       
   267                 if (iModel.List()->MdcaCount() > 0)
       
   268                     {
       
   269                     iView.HandleCommandL(EUserDictCmdDelete);
       
   270                     res = EKeyWasConsumed;
       
   271                     }
       
   272                 break;
       
   273             case EKeyOK:
       
   274                 if (iModel.List()->MdcaCount() <= 0
       
   275                     || iListBox->View()->SelectionIndexes()->Count() > 0)
       
   276                     {
       
   277                     // It shows context sensitive menu.
       
   278                     CEikMenuBar* menuBar = iView.MenuBar();
       
   279                     menuBar->SetMenuTitleResourceId(
       
   280                                 R_UDICT_MENUBAR_VIEW_CONTEXT);
       
   281                     menuBar->TryDisplayMenuBarL();
       
   282                     menuBar->SetMenuTitleResourceId(
       
   283                                 R_UDICT_MENUBAR_VIEW);
       
   284                     }
       
   285                 else
       
   286                     {
       
   287                     iView.HandleCommandL(EUserDictCmdOpen);
       
   288                     }
       
   289                 res = EKeyWasConsumed;
       
   290                 break;
       
   291             default:
       
   292                 break;
       
   293             }
       
   294         }
       
   295 
       
   296     if (res != EKeyWasConsumed && iListBox)
       
   297         {
       
   298         res = iListBox->OfferKeyEventL(aKeyEvent, aType);
       
   299         }
       
   300 
       
   301     return res;
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CUserDictContainer::GetHelpContext
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CUserDictContainer::GetHelpContext(TCoeHelpContext& aContext) const
       
   309     {
       
   310     aContext.iMajor = KUidUserDict;
       
   311     aContext.iContext = KUDICT_HLP;
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CUserDictContainer::StepL
       
   316 // From MAknBackgroundProcess
       
   317 // -----------------------------------------------------------------------------
       
   318 void CUserDictContainer::StepL()
       
   319     {
       
   320     // Deletes the selected item in descending order of list index.
       
   321     for (TInt i(0); i < KWaitNoteOneStepDeletion && iWaitNoteCounter >= 0; i++)
       
   322         {
       
   323         iModel.DeleteWordL(
       
   324             (*iListBox->View()->SelectionIndexes())[iWaitNoteCounter]);
       
   325         iWaitNoteCounter--;
       
   326         }
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // CUserDictContainer::IsProcessDone
       
   331 // From MAknBackgroundProcess
       
   332 // Tests if wait note process is done.
       
   333 // -----------------------------------------------------------------------------
       
   334 TBool CUserDictContainer::IsProcessDone() const
       
   335     {
       
   336     return (iWaitNoteCounter < 0) ? ETrue : EFalse;
       
   337     }
       
   338 
       
   339 //  End of File