uifw/AvKon/akncompamode/src/akncompabutton.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Compatibility keyboard button
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <barsread.h>
       
    21 #include <fbs.h>
       
    22 #include <gulicon.h>
       
    23 #include <AknsSkinInstance.h>
       
    24 #include <AknsUtils.h>
       
    25 #include <aknlayoutscalable_avkon.cdl.h>
       
    26 #include <coecntrl.h>
       
    27 #include <touchfeedback.h>
       
    28 
       
    29 #include "akncompabutton.h"
       
    30 #include "akncompakb.h"
       
    31 
       
    32 // --------------------------------------------------------------------------
       
    33 // Factory function
       
    34 // --------------------------------------------------------------------------
       
    35 CAknCompaButton* CAknCompaButton::NewLC(TResourceReader& aReader)
       
    36     {
       
    37     CAknCompaButton* self = new(ELeave) CAknCompaButton(0);
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     self->ConstructFromResourceL(aReader);
       
    41     return self;
       
    42     }
       
    43 
       
    44 // --------------------------------------------------------------------------
       
    45 // Constructor
       
    46 // --------------------------------------------------------------------------
       
    47 CAknCompaButton::CAknCompaButton(TInt aFlags):CAknButton(aFlags)
       
    48     {
       
    49     }
       
    50 
       
    51 // --------------------------------------------------------------------------
       
    52 // Update colors for text and icon
       
    53 // --------------------------------------------------------------------------
       
    54 void CAknCompaButton::UpdateColors()
       
    55     {   
       
    56     TRgb buttonColor;
       
    57     
       
    58     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
    59 
       
    60     AknsUtils::GetCachedColor( skin, buttonColor,
       
    61         KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG20 );
       
    62     
       
    63     // If button has text, updating text colors
       
    64     if (iFlags.iHasText)
       
    65         {        
       
    66         TRAP_IGNORE(AknLayoutUtils::OverrideControlColorL(*this, 
       
    67             EColorButtonText, buttonColor));
       
    68         
       
    69         TRAP_IGNORE(AknLayoutUtils::OverrideControlColorL(*this, 
       
    70             EColorButtonTextPressed, buttonColor));
       
    71         }
       
    72        
       
    73     // If button has icon, updating icon color
       
    74     if (iFlags.iHasIcon)
       
    75         {                 
       
    76         CFbsBitmap* bitmap = State()->Icon()->Bitmap();
       
    77                                                           
       
    78         // Not updating colors of rocker button icons,
       
    79         // they remain original in all skin themes
       
    80         if (!iFlags.iIsRocker)
       
    81             {
       
    82             AknIconUtils::SetIconColor(bitmap, buttonColor);
       
    83             }
       
    84         AknIconUtils::SetSize(bitmap, iIconSize, EAspectRatioPreserved);
       
    85         }
       
    86     }
       
    87 
       
    88 // --------------------------------------------------------------------------
       
    89 // Set layout for text and icon
       
    90 // --------------------------------------------------------------------------
       
    91 void CAknCompaButton::LayoutIconAndText()
       
    92     {
       
    93     TRect parentRect(Rect());
       
    94     TAknLayoutText buttonText;
       
    95 
       
    96     if (iFlags.iHasText)
       
    97         {
       
    98         // Button contains text
       
    99         buttonText.LayoutText(parentRect,
       
   100             AknLayoutScalable_Avkon::cell_cmode_itu_pane_t2_cp(0,0,0));
       
   101         SetTextFont(buttonText.Font());
       
   102         }
       
   103 
       
   104     if (iFlags.iHasIcon)
       
   105         {
       
   106         TAknLayoutRect layoutRect;
       
   107          
       
   108         if (iFlags.iHasText)
       
   109             {
       
   110             // Button contains text and icon
       
   111             layoutRect.LayoutRect(parentRect, AknLayoutScalable_Avkon::
       
   112                 cell_cmode_itu_pane_g1_cp(0,0,0).LayoutLine());
       
   113             iIconSize = layoutRect.Rect().Size();
       
   114 
       
   115             TInt margin = (buttonText.TextRect().Height() * 2);
       
   116             SetMargins(TMargins8(0,0,margin,0));
       
   117             SetTextHorizontalAlignment(CGraphicsContext::ERight);
       
   118             SetIconHorizontalAlignment(CAknButton::ERight);
       
   119             SetTextAndIconAlignment(CAknButton::EIconAfterText);
       
   120             }
       
   121         else
       
   122             {
       
   123             // Button contains only icon
       
   124             layoutRect.LayoutRect(parentRect, AknLayoutScalable_Avkon::
       
   125                 cell_cmode_rocker_pane_g1_cp(0,0,0).LayoutLine());
       
   126             TRect tmp = layoutRect.Rect();
       
   127             tmp.Shrink(5,5);
       
   128             iIconSize = tmp.Size();
       
   129             SetMargins(TMargins8(0,0,0,0));
       
   130             }
       
   131 
       
   132         SetIconScaleMode(EAspectRatioPreserved);
       
   133         SetIconSize(iIconSize);
       
   134         }
       
   135     }
       
   136 
       
   137 // --------------------------------------------------------------------------
       
   138 // Check if button is locked down
       
   139 // --------------------------------------------------------------------------
       
   140 TBool CAknCompaButton::IsLockedDown()
       
   141     {
       
   142     return iFlags.iLongTap;
       
   143     }
       
   144 
       
   145 // --------------------------------------------------------------------------
       
   146 // Check if button is pressed
       
   147 // --------------------------------------------------------------------------
       
   148 TBool CAknCompaButton::IsPressed()
       
   149     {
       
   150     return iButtonPressed;
       
   151     }
       
   152 
       
   153 // --------------------------------------------------------------------------
       
   154 // Check if button is a rocker button
       
   155 // --------------------------------------------------------------------------
       
   156 TBool CAknCompaButton::IsRocker()
       
   157     {
       
   158     return iFlags.iIsRocker;
       
   159     }
       
   160 
       
   161 // --------------------------------------------------------------------------
       
   162 // Get icon from a resource
       
   163 // --------------------------------------------------------------------------
       
   164 CGulIcon* CAknCompaButton::GetIconLC(TInt aBmpId, TInt aMaskId )
       
   165     {
       
   166     _LIT(KBmpFilePath, "z:\\resource\\apps\\avkon2.mif");
       
   167 
       
   168     CFbsBitmap* bitmap;
       
   169     CFbsBitmap* mask;
       
   170 
       
   171     AknIconUtils::CreateIconL(bitmap, mask, KBmpFilePath, aBmpId,
       
   172         aMaskId);    
       
   173     CGulIcon* icon = CGulIcon::NewL(bitmap, mask);
       
   174     CleanupStack::PushL(icon);
       
   175     return icon;
       
   176     }
       
   177 
       
   178 // --------------------------------------------------------------------------
       
   179 // Construct button from a resource
       
   180 // --------------------------------------------------------------------------
       
   181 void CAknCompaButton::ConstructFromResourceL(TResourceReader& aReader)
       
   182     {
       
   183     iScanCode = aReader.ReadInt16();
       
   184     TInt textId = aReader.ReadInt32();
       
   185     HBufC* buttonText = textId ?
       
   186         iCoeEnv->AllocReadResourceLC(textId) : HBufC::NewLC(0);
       
   187 
       
   188     TInt bmpId = 0;
       
   189     TInt maskId = 0;
       
   190 
       
   191     bmpId = aReader.ReadInt16();
       
   192     maskId = aReader.ReadInt16();
       
   193 
       
   194     CGulIcon* icon = NULL;
       
   195     if (bmpId > 0)
       
   196         {
       
   197         icon = GetIconLC(bmpId, maskId);
       
   198         }                    
       
   199     AddStateL(icon, NULL, NULL, NULL, *buttonText, KNullDesC, 0);    
       
   200     
       
   201     if (icon)
       
   202         {
       
   203         CleanupStack::Pop(); // icon
       
   204         }
       
   205         
       
   206     iFlags.iIsRocker =
       
   207         iScanCode == EStdKeyUpArrow || iScanCode == EStdKeyDownArrow ||
       
   208         iScanCode == EStdKeyLeftArrow || iScanCode == EStdKeyRightArrow ||
       
   209         iScanCode == EStdKeyDevice3;
       
   210 
       
   211     iFlags.iHasIcon = (icon != NULL);
       
   212     iFlags.iHasText = (textId != 0);
       
   213 
       
   214     CleanupStack::PopAndDestroy(); // buttonText
       
   215     }
       
   216 
       
   217 // --------------------------------------------------------------------------
       
   218 // Set button to up state
       
   219 // --------------------------------------------------------------------------
       
   220 void CAknCompaButton::SetButtonUpL()
       
   221     {
       
   222     
       
   223     FeedEventToAknButtonL(EButtonUp);
       
   224     // AknButton does not offer an option to change state on both up and
       
   225     // down events. Since we have set AknButton flag to change state on
       
   226     // down events, we have to bring state up "manually" even when up
       
   227     // event was sent to button.
       
   228     static_cast<CAknCompaKb*>(Parent())->
       
   229         SimulateKeyPressL(iScanCode, EFalse);
       
   230         
       
   231     }
       
   232 
       
   233 // --------------------------------------------------------------------------
       
   234 // Get button key scan code
       
   235 // --------------------------------------------------------------------------
       
   236 TInt CAknCompaButton::ScanCode()
       
   237     {
       
   238     return iScanCode;
       
   239     }
       
   240 
       
   241 // --------------------------------------------------------------------------
       
   242 // Make button ready to draw
       
   243 // --------------------------------------------------------------------------
       
   244 void CAknCompaButton::ActivateL()
       
   245     {
       
   246     // CAknButton::ActivateL() is overriden to prevent it to enable pointer
       
   247     // drag events and stray events
       
   248     CAknControl::ActivateL();
       
   249     }
       
   250 
       
   251 // --------------------------------------------------------------------------
       
   252 // Pointer event handling
       
   253 // --------------------------------------------------------------------------
       
   254 void CAknCompaButton::HandlePointerEventL(const TPointerEvent& aPointerEvent)
       
   255     {
       
   256     // We want button to get into pressed state only when
       
   257     // stylus down occurs on it (not getting into pressed
       
   258     // state when dragged out and back again).
       
   259     // Button stays in a down state unless stulys up occurs
       
   260     // on top of it or less than x distance from it (button
       
   261     // can be left down by dragging stylus out of it far enough).
       
   262     // CAknButton doesn't offer this kind of behaviour.
       
   263     // Therefore pointer up events are handled here.
       
   264 
       
   265     // The framework calls button's HandlePointerEventL() if stylus
       
   266     // down hits the button or subsequent stulys up event
       
   267     if (aPointerEvent.iType == TPointerEvent::EButton1Down)
       
   268         {
       
   269         iFlags.iLongTap = EFalse; // long tap leaves key down
       
   270         TactileFeedback();
       
   271 
       
   272         // CAknButton::HandlePointerEventL() checks PenEnabled() which
       
   273         // returns false current layout is QVGA. Therefore use key-event
       
   274         // to put button into down state.
       
   275         FeedEventToAknButtonL(EButtonDown);
       
   276         }
       
   277     else if (aPointerEvent.iType == TPointerEvent::EButton1Up)
       
   278         {
       
   279         // If pen button is "locked down", it is released if any other than
       
   280         // rocker button is released. This way user doesn't have to release
       
   281         // it manually after copy-paste or list marking.
       
   282         if (!iFlags.iIsRocker && iScanCode != EStdKeyRightShift)
       
   283             {
       
   284             static_cast<CAknCompaKb*>(Parent())->SetPenButtonUpL();
       
   285             }
       
   286 
       
   287         if (!iFlags.iLongTap)
       
   288             {
       
   289             SetButtonUpL();
       
   290             }
       
   291         }
       
   292     else
       
   293         {} // lint
       
   294     }
       
   295 
       
   296 // --------------------------------------------------------------------------
       
   297 // Handle control events from CAknButton
       
   298 // --------------------------------------------------------------------------
       
   299 void CAknCompaButton::HandleControlEventL(CCoeControl* /*aControl*/,
       
   300     TCoeEvent aEventType)
       
   301     {
       
   302     switch(aEventType)
       
   303         {
       
   304         // Button reports of state change when it is pressed
       
   305         case EEventStateChanged:
       
   306             static_cast<CAknCompaKb*>(Parent())->
       
   307                 SimulateKeyPressL(iScanCode, ETrue); // key down
       
   308             break;
       
   309         // Button reports of long press
       
   310         case CAknButton::ELongPressEvent:
       
   311             TactileFeedback();
       
   312             iFlags.iLongTap = ETrue;
       
   313             break;
       
   314         default:
       
   315             break;
       
   316         }
       
   317     }
       
   318 
       
   319 // --------------------------------------------------------------------------
       
   320 // Feed button event to AknButton
       
   321 // --------------------------------------------------------------------------
       
   322 void CAknCompaButton::FeedEventToAknButtonL(TButtonState aState)
       
   323     {
       
   324     // Feed key-event to AknButton to change it's state
       
   325     static const TKeyEvent keyEvent = {0, EStdKeyDevice3, 0, 0};
       
   326     OfferKeyEventL(keyEvent,
       
   327         aState ==  EButtonDown ? EEventKeyDown:EEventKeyUp);
       
   328     }
       
   329 
       
   330 // --------------------------------------------------------------------------
       
   331 // Give tactile feedback for touch event
       
   332 // --------------------------------------------------------------------------
       
   333 void CAknCompaButton::TactileFeedback()
       
   334     {
       
   335     // Feedback/basic on down event
       
   336     MTouchFeedback* feedback = MTouchFeedback::Instance();
       
   337     if (feedback)
       
   338         {
       
   339         feedback->InstantFeedback( this, ETouchFeedbackBasic );
       
   340         }
       
   341     }
       
   342 
       
   343 // --------------------------------------------------------------------------
       
   344 // Handle changes to button resources
       
   345 // --------------------------------------------------------------------------
       
   346 void CAknCompaButton::HandleResourceChange(TInt aType)
       
   347     {
       
   348     
       
   349     // No need to call CAknButton::HandleResourceChange(),
       
   350     // button icon and text is updated by CompaButton      
       
   351     if(aType == KAknsMessageSkinChange)
       
   352         {
       
   353         UpdateColors();
       
   354         }
       
   355     }