textinput/peninputcommonctrls/src/peninputselectionlist/peninputselectionlist.cpp
changeset 0 eb1f2e154e89
child 9 e6a39382bb9c
equal deleted inserted replaced
-1:000000000000 0:eb1f2e154e89
       
     1 /*
       
     2 * Copyright (c) 2009 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 *
       
    16 */
       
    17 #include <barsread.h>
       
    18 #include <coemain.h>
       
    19 #include <AknsItemID.h>
       
    20 #include <AknsUtils.h>
       
    21 #include <AknUtils.h>
       
    22 #include <peninputlayout.h>
       
    23 #include <skinlayout.cdl.h>
       
    24 #include <AknsDrawUtils.h>
       
    25 
       
    26 #include "peninputselectionlist.h"
       
    27 
       
    28 // constant definition
       
    29 const TInt KInvalidResId = -1;
       
    30 const TInt KInvalidBmp = -1;
       
    31 const TInt KInvalidItemIndex = -1;
       
    32 
       
    33 EXPORT_C CPenInputSelectionList* CPenInputSelectionList::NewL(CFepUiLayout* aUiLayout,
       
    34                                                               TInt aControlId,
       
    35                                                               TPoint aTopLeftPoint,
       
    36                                                               TSize aItemSize,
       
    37                                                               TInt aItemHorizontalMargin)
       
    38     {
       
    39     CPenInputSelectionList* self = new (ELeave) CPenInputSelectionList(aUiLayout,
       
    40                                                                        aControlId,
       
    41                                                                        aTopLeftPoint,
       
    42                                                                        aItemSize,
       
    43                                                                        aItemHorizontalMargin);
       
    44     CleanupStack::PushL(self);
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop(self);
       
    47     return self;
       
    48     }
       
    49 
       
    50 EXPORT_C CPenInputSelectionList::~CPenInputSelectionList()
       
    51     {
       
    52     iItemList.Reset();
       
    53     iItemList.Close();
       
    54 
       
    55     iBmpList.ResetAndDestroy();
       
    56     iBmpList.Close();
       
    57     }
       
    58 
       
    59 void CPenInputSelectionList::RecalculateCtrlRect()
       
    60     {
       
    61     if (iItemList.Count() == 0)
       
    62         {
       
    63         SetRect(TRect(0, 0, 0, 0));
       
    64         }
       
    65     else
       
    66         {
       
    67         SetRect(TRect(iItemList[0].iRect.iTl,
       
    68                 iItemList[iItemList.Count() - 1].iRect.iBr));
       
    69         }
       
    70     }
       
    71 
       
    72 EXPORT_C void CPenInputSelectionList::SizeChanged(TPoint aTopLeftPoint,
       
    73                                                   TSize aItemSize,
       
    74                                                   TInt aItemHorizontalMargin)
       
    75     {
       
    76     iTopLeftPoint = aTopLeftPoint;
       
    77     iItemSize = aItemSize;
       
    78     iItemHorizontalMargin = aItemHorizontalMargin;
       
    79 
       
    80     for (TInt i = 0; i < iItemList.Count(); i++)
       
    81         {
       
    82         iItemList[i].iRect = TRect(TPoint(aTopLeftPoint.iX,
       
    83                                           aTopLeftPoint.iY + 
       
    84                                           i * (aItemHorizontalMargin + iItemSize.iHeight)),
       
    85                                    aItemSize);
       
    86         }
       
    87 
       
    88     RecalculateCtrlRect();
       
    89     }
       
    90 
       
    91 EXPORT_C void CPenInputSelectionList::SetItemsL(const RPointerArray<HBufC>& aItemList)
       
    92     {
       
    93     Hide(ETrue);
       
    94 
       
    95     iItemList.Reset();
       
    96 
       
    97     for (TInt i = 0; i < aItemList.Count(); i++)
       
    98         {
       
    99         SItem item(TRect(TPoint(iTopLeftPoint.iX,
       
   100                                iTopLeftPoint.iY + i * (iItemHorizontalMargin + iItemSize.iHeight)),
       
   101                          iItemSize),
       
   102                    *aItemList[i]);
       
   103         iItemList.AppendL(item);
       
   104         }
       
   105 
       
   106     RecalculateCtrlRect();
       
   107     }
       
   108 
       
   109 EXPORT_C void CPenInputSelectionList::AddItemL(const TDesC& aItem)
       
   110     {
       
   111     SItem item(TRect(TPoint(iTopLeftPoint.iX,
       
   112                             iTopLeftPoint.iY + iItemList.Count() * (iItemHorizontalMargin + 
       
   113                             iItemSize.iHeight)),iItemSize), aItem);
       
   114     iItemList.AppendL(item);
       
   115 
       
   116     RecalculateCtrlRect();
       
   117     }
       
   118 
       
   119 EXPORT_C void  CPenInputSelectionList::RemoveItem(TInt aItemIndex)
       
   120     {
       
   121     if (aItemIndex < 0 || aItemIndex >= iItemList.Count())
       
   122         {
       
   123         return;
       
   124         }
       
   125     
       
   126     iItemList.Remove(aItemIndex);
       
   127     
       
   128     for (TInt i = aItemIndex; i < iItemList.Count(); i++)
       
   129         {
       
   130         iItemList[i].iRect.Move(-iItemHorizontalMargin, 0);
       
   131         }
       
   132     
       
   133     RecalculateCtrlRect();
       
   134     }
       
   135 
       
   136 EXPORT_C void CPenInputSelectionList::RemoveAllItems()
       
   137     {
       
   138     iItemList.Reset();
       
   139     RecalculateCtrlRect();
       
   140     }
       
   141 
       
   142 EXPORT_C void CPenInputSelectionList::ConstructFromResourceL()
       
   143     {
       
   144     if (iResourceId == KInvalidResId)
       
   145     	{
       
   146     	User::Leave(KErrArgument);
       
   147     	}
       
   148    
       
   149     iBmpList.ResetAndDestroy();
       
   150     
       
   151     TResourceReader reader;
       
   152     CCoeEnv::Static()->CreateResourceReaderLC(reader, iResourceId);
       
   153 
       
   154     TPtrC bmpFileName = reader.ReadTPtrC();
       
   155     TInt imgMajorSkinId = reader.ReadInt32();
       
   156     
       
   157     TAknsItemID id;
       
   158     
       
   159     for (TInt i = 0; i < EBmpLastType + 1; i+=2)
       
   160         {
       
   161         TInt bmpid = reader.ReadInt16();
       
   162         TInt maskid = reader.ReadInt16();
       
   163         TInt minorskinid = reader.ReadInt16();
       
   164 
       
   165         id.Set(imgMajorSkinId, minorskinid);
       
   166 
       
   167         CFbsBitmap* bmp = NULL;
       
   168         CFbsBitmap* maskbmp = NULL;
       
   169 
       
   170         if (bmpid != KInvalidBmp)
       
   171             {
       
   172             if (maskid != KInvalidBmp)
       
   173                 {
       
   174         		AknsUtils::CreateIconL(AknsUtils::SkinInstance(),
       
   175         		                       id,
       
   176         		                       bmp,
       
   177         		                       maskbmp,
       
   178         		                       bmpFileName,
       
   179         		                       bmpid,
       
   180         		                       maskid);
       
   181 
       
   182                 AknIconUtils::SetSize(maskbmp, iItemSize, EAspectRatioNotPreserved);
       
   183                 }
       
   184             else
       
   185                 {
       
   186         	    AknsUtils::CreateIconL(AknsUtils::SkinInstance(),
       
   187         	                           id,
       
   188         	                           bmp,
       
   189         	                           bmpFileName,
       
   190         	                           bmpid);
       
   191                 }
       
   192 
       
   193             AknIconUtils::SetSize(bmp, iItemSize, EAspectRatioNotPreserved);
       
   194             }
       
   195 
       
   196         iBmpList.AppendL(bmp);
       
   197         iBmpList.AppendL(maskbmp);
       
   198         }
       
   199 
       
   200     CleanupStack::PopAndDestroy(); // reader
       
   201     }
       
   202 
       
   203 EXPORT_C void CPenInputSelectionList::SetTextProperty(const CFont* aFont,
       
   204                                                       TRgb aFontColor)
       
   205     {
       
   206     iFont = aFont;
       
   207     iFontColor = aFontColor;
       
   208 
       
   209     iBaselineOffset = iItemSize.iHeight/2 + aFont->AscentInPixels()/2;
       
   210     }
       
   211 
       
   212 TInt CPenInputSelectionList::HitTest(const TPoint& aPoint)
       
   213     {
       
   214     if (!Rect().Contains(aPoint))
       
   215         {
       
   216         return KErrGeneral;
       
   217         }
       
   218         
       
   219     for (TInt i = 0; i < iItemList.Count(); i++)
       
   220         {
       
   221         if (iItemList[i].iRect.Contains(aPoint))
       
   222             {
       
   223             return i;
       
   224             }
       
   225         }
       
   226     
       
   227     return KErrNotFound;    
       
   228     }
       
   229 
       
   230 EXPORT_C CFepUiBaseCtrl* CPenInputSelectionList::HandlePointerDownEventL(const TPoint& aPoint)
       
   231     {
       
   232     CFepUiBaseCtrl::HandlePointerDownEventL(aPoint);
       
   233 
       
   234     TInt index = HitTest(aPoint);
       
   235     
       
   236     if (index >= 0)
       
   237         {
       
   238         if (index != iFocusItemIndex)
       
   239             {
       
   240             TInt prev = iFocusItemIndex;
       
   241             iFocusItemIndex = index;
       
   242             
       
   243             if (prev != KInvalidItemIndex)
       
   244                 {
       
   245                 DrawItem(prev);
       
   246                 }
       
   247             
       
   248             DrawItem(iFocusItemIndex);
       
   249             }
       
   250         }
       
   251     else if (index == KErrGeneral)
       
   252         {
       
   253         SetPointerDown(EFalse);
       
   254         CloseWindow();
       
   255         }
       
   256     
       
   257     return this;    
       
   258     }
       
   259 
       
   260 EXPORT_C CFepUiBaseCtrl* CPenInputSelectionList::HandlePointerUpEventL(const TPoint& aPoint)
       
   261     {
       
   262     CFepUiBaseCtrl::HandlePointerUpEventL(aPoint);
       
   263     
       
   264     TInt index = HitTest(aPoint);
       
   265     
       
   266     if (index >= 0)
       
   267         {
       
   268         CloseWindow();
       
   269         
       
   270         if (iFocusItemIndex != KInvalidItemIndex)
       
   271             {
       
   272             TPtrC ptr;
       
   273             ptr.Set(reinterpret_cast<TText*>(&iFocusItemIndex), sizeof(TInt)/sizeof(TText));
       
   274             ReportEvent(iEventId, ptr);
       
   275             }
       
   276         }
       
   277     
       
   278     iFocusItemIndex = KInvalidItemIndex;
       
   279 
       
   280     return this;    
       
   281     }
       
   282     
       
   283 EXPORT_C CFepUiBaseCtrl* CPenInputSelectionList::HandlePointerMoveEventL(const TPoint& aPoint)
       
   284     {
       
   285     CFepUiBaseCtrl::HandlePointerMoveEventL(aPoint);
       
   286 
       
   287     if (PointerDown())
       
   288         {
       
   289         TInt index = HitTest(aPoint);
       
   290 
       
   291         if (index != iFocusItemIndex && index != KInvalidItemIndex)
       
   292             {
       
   293             TInt prev = iFocusItemIndex;
       
   294             iFocusItemIndex = index;
       
   295         
       
   296             if( prev != -1 )
       
   297                 {
       
   298                 DrawItem(prev);
       
   299                 }
       
   300 
       
   301             DrawItem(iFocusItemIndex);
       
   302             }
       
   303         }
       
   304 
       
   305     return this;
       
   306     }
       
   307 
       
   308 EXPORT_C void CPenInputSelectionList::Display()
       
   309     {
       
   310     //Hide(EFalse);
       
   311     BringToTop();
       
   312     Draw();
       
   313     }
       
   314 
       
   315 EXPORT_C void CPenInputSelectionList::CloseWindow()
       
   316     {
       
   317     Hide(ETrue);
       
   318     RemoveAllItems();
       
   319     }
       
   320 
       
   321 CPenInputSelectionList::CPenInputSelectionList(CFepUiLayout* aUiLayout,
       
   322                                                TInt aControlId,
       
   323                                                TPoint aTopLeftPoint,
       
   324                                                TSize aItemSize,
       
   325                                                TInt aItemHorizontalMargin)
       
   326     :CFepUiBaseCtrl(TRect(0, 0, 0, 0), aUiLayout, aControlId),
       
   327      iTopLeftPoint(aTopLeftPoint),
       
   328      iItemSize(aItemSize),
       
   329      iItemHorizontalMargin(aItemHorizontalMargin),
       
   330      iFocusItemIndex(KInvalidItemIndex),
       
   331      iFrId(KAknsIIDQsnFrPopupSub),
       
   332      iCenterFrId(KAknsIIDQsnFrPopupCenterSubmenu)
       
   333     {
       
   334     SetControlType(EUserCtrlBase | ECtrlBaseCtrl);
       
   335     }
       
   336 
       
   337 void CPenInputSelectionList::ConstructL()
       
   338     {
       
   339     BaseConstructL();
       
   340     Hide(ETrue);
       
   341     }
       
   342 
       
   343 void CPenInputSelectionList::Draw()
       
   344     {
       
   345 	if(!AbleToDraw())
       
   346         return;
       
   347 
       
   348     for (TInt i = 0; i < iItemList.Count(); i++)
       
   349         {
       
   350         DrawItem(i);
       
   351         }
       
   352         
       
   353     UiLayout()->UpdateArea(this, this->Rect(), EFalse);
       
   354     }
       
   355 
       
   356 void CPenInputSelectionList::DrawItem(TInt aItemIndex)
       
   357     {
       
   358     CFbsBitGc* gc = static_cast<CFbsBitGc*>(BitGc());        
       
   359     
       
   360     //mask bitmaps
       
   361     gc->Activate( MaskBitmapDevice() );
       
   362 
       
   363     gc->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   364     gc->SetBrushColor(TRgb(KOpaqueColor));//Non transparent at all
       
   365     gc->SetPenStyle(CGraphicsContext::ESolidPen );
       
   366     gc->SetPenSize(PenSize());
       
   367     gc->SetPenColor(TRgb(KOpaqueColor) );
       
   368     gc->DrawRect(iItemList[aItemIndex].iRect);
       
   369 
       
   370     // ----- draw bitmaps -----
       
   371     gc->Activate(BitmapDevice());   
       
   372 
       
   373 	// draw frame
       
   374 	MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   375 
       
   376     TRect outerRect;
       
   377     TRect innerRect;
       
   378     CalculateFrameRects( iItemList[aItemIndex].iRect, outerRect, innerRect);
       
   379 
       
   380     AknsDrawUtils::DrawFrame(
       
   381 	    skin, *gc, 
       
   382 	    outerRect, innerRect, 
       
   383 	    iFrId, iCenterFrId); 
       
   384 
       
   385     // draw text
       
   386     if (iTextLineSet)
       
   387         {
       
   388         TAknLayoutText textLayout;
       
   389         textLayout.LayoutText(iItemList[aItemIndex].iRect, 
       
   390                               iTextLine);
       
   391         textLayout.DrawText(*gc, iItemList[aItemIndex].iText);
       
   392         }
       
   393     else if (iFont)
       
   394         {
       
   395         gc->UseFont(iFont);
       
   396 
       
   397         gc->SetBrushStyle(CGraphicsContext::ENullBrush);
       
   398         gc->SetBrushColor(KRgbBlack);
       
   399         gc->SetPenColor(iFontColor);
       
   400         gc->SetPenStyle(CGraphicsContext::ESolidPen);
       
   401         gc->SetPenSize(PenSize());
       
   402         
       
   403         gc->DrawText(iItemList[aItemIndex].iText,
       
   404                      iItemList[aItemIndex].iRect,
       
   405                      iBaselineOffset,
       
   406                      CGraphicsContext::ECenter);
       
   407 
       
   408         gc->DiscardFont();
       
   409         }    
       
   410     }
       
   411 void CPenInputSelectionList::CalculateFrameRects( const TRect aRect,
       
   412 												  TRect& aOuterRect, 
       
   413 												  TRect& aInnerRect ) const
       
   414     {
       
   415     TRect windowRect = aRect;
       
   416     
       
   417     TAknLayoutRect topLeft;
       
   418     topLeft.LayoutRect(windowRect, SkinLayout::Submenu_skin_placing_Line_2());
       
   419 
       
   420     TAknLayoutRect bottomRight;
       
   421     bottomRight.LayoutRect(windowRect, SkinLayout::Submenu_skin_placing_Line_5());
       
   422 
       
   423     aOuterRect = TRect(topLeft.Rect().iTl, bottomRight.Rect().iBr);
       
   424     aInnerRect = TRect(topLeft.Rect().iBr, bottomRight.Rect().iTl);
       
   425     }
       
   426 // End Of File