uifw/EikStd/coctlsrc/aknstyluspopupmenu.cpp
changeset 0 2f259fa3e83a
child 4 8ca85d2f0db7
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Interface for controlling stylus popup menu.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <AknPreviewPopUpController.h>
       
    20 #include <eikmobs.h>
       
    21 #include <AknUtils.h>
       
    22 #include <barsread.h>
       
    23 #include <aknlayoutscalable_avkon.cdl.h>
       
    24 #include <eikapp.h>
       
    25 #include <aknappui.h>
       
    26 #include <AknDef.h>
       
    27 
       
    28 #include "aknstyluspopupmenu.h"
       
    29 #include "aknstyluspopupmenucontent.h"
       
    30 
       
    31 const TInt KDefaultPopUpShowDelay = 0;
       
    32 const TInt KDefaultPopUpHideDelay = 6000000; // 6 seconds
       
    33 
       
    34 enum TStylusPopUpFlags
       
    35     {
       
    36     EIdleDisabled // usage of iControllerIdle is disabled 
       
    37     };
       
    38 
       
    39 // ======== MEMBER FUNCTIONS ========
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // Default constructor
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CAknStylusPopUpMenu::CAknStylusPopUpMenu( MEikMenuObserver* aObserver, 
       
    46     const TPoint& aPoint,
       
    47     CAknPreviewPopUpController* aPopup ) 
       
    48     : iPosition ( aPoint ),
       
    49      iMenuObserver( aObserver ),
       
    50      iPreviewPopup( aPopup ),
       
    51      iPositionType( EPositionTypeLeftTop )
       
    52     {
       
    53     }
       
    54 
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CAknStylusPopUpMenu::ConstructL
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 
       
    61 void CAknStylusPopUpMenu::ConstructL()
       
    62     {
       
    63     iContent = CAknStylusPopUpMenuContent::NewL( *this );
       
    64     
       
    65     iContent->SetObserver(this); 
       
    66     }
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // CAknStylusPopUpMenu::NewL
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 EXPORT_C CAknStylusPopUpMenu* CAknStylusPopUpMenu::NewL( 
       
    74                             MEikMenuObserver* aObserver, const TPoint& aPoint,
       
    75                             CAknPreviewPopUpController* aPopup )
       
    76     {
       
    77     CAknStylusPopUpMenu* self = CAknStylusPopUpMenu::NewLC( aObserver, 
       
    78                                                             aPoint,
       
    79                                                             aPopup );
       
    80     CleanupStack::Pop( self );
       
    81     return self;
       
    82     }
       
    83 
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CAknStylusPopUpMenu::NewL
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C CAknStylusPopUpMenu* CAknStylusPopUpMenu::NewL( 
       
    90                             MEikMenuObserver* aObserver, const TPoint& aPoint )
       
    91     {
       
    92     CAknStylusPopUpMenu* self = CAknStylusPopUpMenu::NewLC( aObserver, 
       
    93                                                             aPoint, NULL );
       
    94     CleanupStack::Pop( self );
       
    95     return self;
       
    96     }
       
    97 
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CAknStylusPopUpMenu::NewLC
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C CAknStylusPopUpMenu* CAknStylusPopUpMenu::NewLC(
       
   104                             MEikMenuObserver* aObserver, const TPoint& aPoint,
       
   105                             CAknPreviewPopUpController* aPopup )
       
   106     {
       
   107     CAknStylusPopUpMenu* self = new ( ELeave ) CAknStylusPopUpMenu( aObserver,
       
   108                                                                     aPoint,
       
   109                                                                     aPopup );
       
   110     CleanupStack::PushL( self );
       
   111     self->ConstructL();
       
   112     return self;
       
   113     }
       
   114 
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // Destructor
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 CAknStylusPopUpMenu::~CAknStylusPopUpMenu()
       
   121     {
       
   122     if ( iIsDeleted )
       
   123         {
       
   124         *iIsDeleted = ETrue;
       
   125         iIsDeleted = NULL;
       
   126         }
       
   127 
       
   128     if ( iControllerIdle )
       
   129          {
       
   130          iControllerIdle->Cancel();
       
   131          }
       
   132     delete iControllerIdle;
       
   133     delete iController;
       
   134     delete iContent;
       
   135     }
       
   136 
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // CAknStylusPopUpMenu::AddMenuItemL
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C void CAknStylusPopUpMenu::AddMenuItemL( const TDesC& aItem, 
       
   143                                                         const TInt aCommandId )
       
   144     {
       
   145     iContent->AddMenuItemL( aItem, aCommandId );
       
   146     }
       
   147 
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // CAknStylusPopUpMenu::RemoveMenuItem
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 EXPORT_C void CAknStylusPopUpMenu::RemoveMenuItem( const TInt aCommandId )
       
   154     {
       
   155     iContent->RemoveMenuItem( aCommandId );
       
   156     }
       
   157 
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // CAknStylusPopUpMenu::SetItemDimmed
       
   161 // ---------------------------------------------------------------------------
       
   162 //
       
   163 EXPORT_C void CAknStylusPopUpMenu::SetItemDimmed( const TInt aCommandId, const TBool aDimmed )
       
   164     {
       
   165     iContent->SetItemDimmed( aCommandId, aDimmed );
       
   166     }
       
   167 
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // CAknStylusPopUpMenu::ShowMenu
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 EXPORT_C void CAknStylusPopUpMenu::ShowMenu()
       
   174     {
       
   175     TRAPD( err, 
       
   176            iController = CAknPreviewPopUpController::NewL( *iContent,
       
   177                    CAknPreviewPopUpController::ELayoutSubMenu | 
       
   178                    CAknPreviewPopUpController::EAutoMirror |
       
   179                    CAknPreviewPopUpController::EDontClose ) );
       
   180     if ( err )
       
   181         {
       
   182         return;
       
   183         }
       
   184             
       
   185     iController->SetPopUpShowDelay( KDefaultPopUpShowDelay );
       
   186     iController->SetPopUpHideDelay( KDefaultPopUpHideDelay );
       
   187     iContent->Parent()->DrawableWindow()->SetNonFading(ETrue);
       
   188     
       
   189     TSize size(iController->Size());
       
   190     iController->ShowPopUp();
       
   191 
       
   192     if ( size.iWidth == 0 && size.iHeight == 0 )
       
   193         {
       
   194         if ( iPositionType != KErrNotFound )
       
   195             {
       
   196             SetPosition( iPosition, TPositionType( iPositionType ) );
       
   197             }
       
   198         else
       
   199             {
       
   200             SetPosition( iPosition );
       
   201             }        
       
   202         }
       
   203     else
       
   204         {
       
   205         //position already adjusted.
       
   206         iController->SetPosition( iPosition );
       
   207         }
       
   208     
       
   209     }
       
   210 
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // CAknStylusPopUpMenu::SetPosition
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 EXPORT_C void CAknStylusPopUpMenu::SetPosition( const TPoint& aPoint )
       
   217     {
       
   218     SetPosition( aPoint, EPositionTypeLeftTop );
       
   219     }
       
   220 
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // CAknStylusPopUpMenu::SetPosition 
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 EXPORT_C void CAknStylusPopUpMenu::SetPosition( const TPoint& aPoint, TPositionType aPosType )
       
   227     {
       
   228     TPoint adjustedPoint( aPoint );
       
   229     if ( !iController )
       
   230         {
       
   231         iPosition = aPoint;
       
   232         iPositionType = aPosType;
       
   233         return;
       
   234         }
       
   235     iController->UpdateContentSize(); 
       
   236     TSize menuSize = iController->Size();
       
   237 	// calculate to right-top corner by aPosType and popup menu size
       
   238     switch ( aPosType )	
       
   239         {
       
   240         case EPositionTypeRightTop: 
       
   241             break;
       
   242         case EPositionTypeLeftTop:
       
   243             if( AknLayoutUtils::LayoutMirrored() ) 
       
   244                 {
       
   245                 adjustedPoint.iX -= menuSize.iWidth;
       
   246                 }
       
   247             else
       
   248                 {
       
   249                 adjustedPoint.iX += menuSize.iWidth;
       
   250                 }
       
   251             break;   
       
   252         case EPositionTypeRightBottom:
       
   253             adjustedPoint.iY -= menuSize.iHeight;
       
   254             break;
       
   255         case EPositionTypeLeftBottom: 
       
   256             if( AknLayoutUtils::LayoutMirrored() ) 
       
   257                 {
       
   258                 adjustedPoint.iX -= menuSize.iWidth;
       
   259                 }
       
   260             else
       
   261                 {
       
   262                 adjustedPoint.iX += menuSize.iWidth;
       
   263                 }
       
   264             adjustedPoint.iY -= menuSize.iHeight;
       
   265             break;    
       
   266         default:
       
   267             break;  
       
   268     
       
   269         }
       
   270     iPosition = adjustedPoint;           
       
   271     iController->SetPosition( adjustedPoint ); 
       
   272     }
       
   273 
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // CAknStylusPopUpMenu::ConstructFromResourceL
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 EXPORT_C void CAknStylusPopUpMenu::ConstructFromResourceL( TResourceReader&
       
   280                                                            aReader )
       
   281     {
       
   282     TInt count = aReader.ReadInt16();
       
   283     TInt commandId;
       
   284     for ( TInt i = 0; i < count; i++ )
       
   285         {
       
   286         TPtrC text = aReader.ReadTPtrC();
       
   287         commandId = aReader.ReadInt32();
       
   288         iContent->AddMenuItemL( text, commandId );
       
   289         aReader.ReadInt32(); // extension link
       
   290         }
       
   291     }
       
   292 
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // From class MCoeControlObserver.
       
   296 // Handles stylus tap events by forwarding the event to the observer.
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 EXPORT_C void CAknStylusPopUpMenu::HandleControlEventL( CCoeControl* aControl,
       
   300                                                         TCoeEvent aEventType )
       
   301     {
       
   302     if ( aControl && aEventType == EEventStateChanged )
       
   303         {
       
   304         if ( iPreviewPopup )
       
   305             {
       
   306             iPreviewPopup->HidePopUp();
       
   307             }
       
   308         if ( iController )
       
   309             {
       
   310             iController->HidePopUp();
       
   311             }
       
   312 
       
   313         if ( iMenuObserver )
       
   314             {
       
   315             iFlags.Set( EIdleDisabled );
       
   316             TBool isDeleted = EFalse;
       
   317             iIsDeleted = &isDeleted;
       
   318 
       
   319             iMenuObserver->ProcessCommandL( iContent->CurrentCommandId() );
       
   320 
       
   321             if ( isDeleted )
       
   322                 {
       
   323                 return;
       
   324                 }
       
   325 
       
   326             iIsDeleted = NULL;
       
   327             iFlags.Clear( EIdleDisabled );
       
   328             }
       
   329 
       
   330         StartControllerIdleL();
       
   331         }
       
   332     else if ( aControl && aEventType == EEventRequestExit )
       
   333         {
       
   334         if ( iPreviewPopup )
       
   335             {
       
   336             iPreviewPopup->HidePopUp();
       
   337             }
       
   338         if ( iController )
       
   339             {
       
   340             iController->HidePopUp();
       
   341             }      
       
   342 
       
   343         StartControllerIdleL();
       
   344         }
       
   345     else if ( aControl && aEventType == EEventRequestCancel )
       
   346         {
       
   347         if ( iMenuObserver )
       
   348             {
       
   349             iFlags.Set( EIdleDisabled );
       
   350             iMenuObserver->ProcessCommandL( KErrCancel );
       
   351             iFlags.Clear( EIdleDisabled );
       
   352             }
       
   353 
       
   354         StartControllerIdleL();
       
   355         }
       
   356     }
       
   357 
       
   358 
       
   359 // ---------------------------------------------------------------------------
       
   360 // CAknStylusPopUpMenu::UpdatePosition
       
   361 // ---------------------------------------------------------------------------
       
   362 //
       
   363 void CAknStylusPopUpMenu::UpdatePosition()
       
   364     {
       
   365 	SetPosition( iPosition );
       
   366     }
       
   367 
       
   368 
       
   369 // ---------------------------------------------------------------------------
       
   370 // CAknStylusPopUpMenu::Clear
       
   371 // ---------------------------------------------------------------------------
       
   372 //
       
   373 void CAknStylusPopUpMenu::Clear()
       
   374     {
       
   375     iContent->Clear();
       
   376     if ( iController )
       
   377         {
       
   378         iController->UpdateContentSize();
       
   379         }
       
   380     }
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CAknStylusPopUpMenu::StartControllerIdleL
       
   384 // -----------------------------------------------------------------------------
       
   385 //
       
   386 void CAknStylusPopUpMenu::StartControllerIdleL()
       
   387     {
       
   388     if ( !iFlags.IsSet( EIdleDisabled ) )
       
   389         {
       
   390         if ( !iControllerIdle )
       
   391             {
       
   392             iControllerIdle = CIdle::NewL( CActive::EPriorityIdle );
       
   393             }
       
   394     
       
   395         if ( !iControllerIdle->IsActive() && iController )
       
   396              {
       
   397              iControllerIdle->Start( TCallBack( ControllerCallBack, this ) );
       
   398              }
       
   399         }
       
   400     }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CAknStylusPopUpMenu::ControllerCallBack
       
   404 // -----------------------------------------------------------------------------
       
   405 //
       
   406 TInt CAknStylusPopUpMenu::ControllerCallBack( TAny* aThis )
       
   407     {
       
   408     CAknStylusPopUpMenu* self = static_cast<CAknStylusPopUpMenu*>( aThis );
       
   409     self->RemoveController();
       
   410     self->iFlags.Clear( EIdleDisabled );
       
   411     
       
   412     return KErrNone;
       
   413     }
       
   414 
       
   415 // -----------------------------------------------------------------------------
       
   416 // CAknStylusPopUpMenu::RemoveController
       
   417 // -----------------------------------------------------------------------------
       
   418 //
       
   419 void CAknStylusPopUpMenu::RemoveController()
       
   420     {
       
   421     if ( iController )
       
   422         {
       
   423         delete iController;
       
   424         iController = NULL;
       
   425         }
       
   426     }