idlehomescreen/xmluirendering/uiengine/src/xnpopupcontroladapter.cpp
branchRCL_3
changeset 83 5456b4e8b3a8
child 93 b01126ce0bec
equal deleted inserted replaced
82:5f0182e07bfb 83:5456b4e8b3a8
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Popup control adapter
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <e32base.h>
       
    20 #include <e32const.h>
       
    21 #include <coecntrl.h>
       
    22 #include <aknstyluspopupmenu.h>
       
    23 
       
    24 // User includes
       
    25 #include "xnappuiadapter.h"
       
    26 #include "xnviewadapter.h"
       
    27 #include "xnkeyeventdispatcher.h"
       
    28 #include "xnviewmanager.h"
       
    29 #include "xnviewdata.h"
       
    30 #include "xnplugindata.h"
       
    31 #include "xnproperty.h"
       
    32 #include "xncontroladapter.h"
       
    33 #include "xndomnode.h"
       
    34 #include "xnnodepluginif.h"
       
    35 #include "xnnode.h"
       
    36 #include "xntype.h"
       
    37 #include "xnuiengine.h"
       
    38 #include "xneditor.h"
       
    39 #include "xnfocuscontrol.h"
       
    40 
       
    41 #include "xnpopupcontroladapter.h"
       
    42 
       
    43 // Constants
       
    44 _LIT8( KMenuItem, "menuitem" );
       
    45 _LIT8( KDynMenuItem, "dynmenuitem" );
       
    46 _LIT8( KWidgetMenuItem, "widgetmenuitem" );
       
    47 _LIT8( KMenuExtension, "menuextension" );
       
    48 
       
    49 _LIT8( KSource, "source" );
       
    50 _LIT8( KTarget, "target" );
       
    51 
       
    52 // ============================ LOCAL FUNCTIONS ===============================
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // FindWidgetMenuItemL
       
    56 //
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 static CXnNode* FindWidgetMenuItemL( CXnPluginData& aPlugin,
       
    60     const TDesC8& aSource )     
       
    61     {        
       
    62     if ( !aPlugin.Occupied() )
       
    63         {
       
    64         return NULL;
       
    65         }
       
    66 
       
    67     CXnNode* widget( aPlugin.Node()->LayoutNode() );
       
    68 
       
    69     RPointerArray< CXnNode >& children( widget->Children() );
       
    70     
       
    71     for ( TInt i = children.Count() - 1; i >= 0; i-- )
       
    72         {
       
    73         if ( children[i]->Type()->Type() == KMenuExtension )
       
    74             {
       
    75             RPointerArray< CXnNode >& items( children[i]->Children() );
       
    76             
       
    77             for ( TInt i = 0; i < items.Count(); i++ )
       
    78                 {
       
    79                 CXnProperty* prop( items[i]->GetPropertyL( KTarget ) );
       
    80                 
       
    81                 if ( prop && prop->StringValue() == aSource )
       
    82                     {
       
    83                     return items[i];
       
    84                     }                
       
    85                 }
       
    86             
       
    87             break;
       
    88             }
       
    89         }
       
    90     
       
    91     return NULL;
       
    92     }
       
    93 
       
    94 // ============================ MEMBER FUNCTIONS ===============================
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CXnPopupControlAdapter::NewL
       
    98 // Two-phased constructor. Can leave.
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CXnPopupControlAdapter* CXnPopupControlAdapter::NewL( CXnNodePluginIf& aNode )
       
   102     {
       
   103     CXnPopupControlAdapter* self =
       
   104         new ( ELeave ) CXnPopupControlAdapter();
       
   105 
       
   106     CleanupStack::PushL( self );
       
   107 
       
   108     self->ConstructL( aNode );
       
   109 
       
   110     CleanupStack::Pop( self );
       
   111 
       
   112     return self;
       
   113     }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CXnPopupControlAdapter::~CXnPopupControlAdapter
       
   117 // Destructor.
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 CXnPopupControlAdapter::~CXnPopupControlAdapter()
       
   121     {           
       
   122     delete iStylusPopupMenu;
       
   123            
       
   124     iMenuItems.Reset();
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CXnPopupControlAdapter::CXnPopupControlAdapter
       
   129 // C++ default constructor. Must not leave.
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 CXnPopupControlAdapter::CXnPopupControlAdapter()    
       
   133     {
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CXnPopupControlAdapter::ConstructL
       
   138 // 2nd phase constructor. Can leave.
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 void CXnPopupControlAdapter::ConstructL( CXnNodePluginIf& aNode )
       
   142     {
       
   143     CXnControlAdapter::ConstructL( aNode ); 
       
   144     
       
   145     iNode = &aNode.Node();
       
   146     iUiEngine = iNode->UiEngine();
       
   147     
       
   148     iMenuShown = EFalse;
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CXnPopupControlAdapter::ProcessCommandL
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CXnPopupControlAdapter::ProcessCommandL( TInt aCommandId )
       
   156     {    
       
   157     // Resolve selected item
       
   158     for ( TInt i = 0; aCommandId != KErrCancel && i < iMenuItems.Count(); i++ )
       
   159         {
       
   160         if ( i == aCommandId )
       
   161             {
       
   162         	// Do the actual item selection			
       
   163             iMenuItems[i]->SetStateL( 
       
   164             	XnPropertyNames::style::common::KActive );
       
   165                 
       
   166             break;
       
   167             }
       
   168         }        
       
   169 
       
   170     iUiEngine->Editor()->SetTargetPlugin( NULL );
       
   171     
       
   172     iMenuItems.Reset();
       
   173     iCommandId = 0;
       
   174     
       
   175     iMenuShown = EFalse;
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CXnPopupControlAdapter::SetEmphasis
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CXnPopupControlAdapter::SetEmphasis( CCoeControl* /*aMenuControl*/, 
       
   183     TBool /*aEmphasis*/ )
       
   184     {         
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CXnPopupControlAdapter::SetObserver
       
   189 // -----------------------------------------------------------------------------
       
   190 //    
       
   191 void CXnPopupControlAdapter::SetObserver( 
       
   192     XnMenuInterface::MXnMenuObserver& aObserver )   
       
   193     {
       
   194     iObserver = &aObserver;
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CXnPopupControlAdapter::TryDisplayingStylusPopupL
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void CXnPopupControlAdapter::TryDisplayingStylusPopupL( CXnNode& aPlugin )
       
   202     {
       
   203     HideMenuL();
       
   204     
       
   205     CXnViewManager* manager( iUiEngine->ViewManager() );            
       
   206     CXnPluginData* plugin( manager->ActiveViewData().Plugin( &aPlugin ) );
       
   207     
       
   208     if ( !plugin )
       
   209         {
       
   210         return;
       
   211         }
       
   212     
       
   213     CXnViewAdapter& adapter( iUiEngine->AppUiAdapter().ViewAdapter() );
       
   214     
       
   215     const TPointerEvent& event( adapter.EventDispatcher()->PointerEvent() );
       
   216     
       
   217     delete iStylusPopupMenu;
       
   218     iStylusPopupMenu = NULL;
       
   219     
       
   220     // Popup is positioned relatively to screen
       
   221     iStylusPopupMenu = CAknStylusPopUpMenu::NewL( this, event.iParentPosition );
       
   222       
       
   223     RPointerArray< CXnNode >& children( iNode->Children() );       
       
   224     
       
   225     for ( TInt i = 0; i < children.Count(); i++ )
       
   226         {      
       
   227         PopulateMenuL( *plugin, children[i] );
       
   228         }
       
   229     
       
   230     ShowMenuL( *plugin, event.iParentPosition );
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CXnPopupControlAdapter::HandleScreenDeviceChangedL
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CXnPopupControlAdapter::HandleScreenDeviceChangedL()
       
   238     {
       
   239     HideMenuL();
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CXnPopupControlAdapter::PopulateMenuL
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 void CXnPopupControlAdapter::PopulateMenuL( CXnPluginData& aPlugin, 
       
   247     CXnNode* aItem )    
       
   248     {                     
       
   249     CXnNode* menuitem( NULL );    
       
   250     CXnProperty* prop( NULL );
       
   251     
       
   252     TBool mappedItem( EFalse );
       
   253 
       
   254     // Map widget menuitem first
       
   255     if ( aItem->Type()->Type() == KWidgetMenuItem )
       
   256         {
       
   257         // Widget menuitem is shown if a widget defined by the "aPlugin" 
       
   258         // defines a proper source - target menuitem pairing
       
   259         CXnProperty* source( aItem->GetPropertyL( KSource ) );
       
   260                        
       
   261         if ( source )
       
   262             {                       
       
   263             CXnNode* widgetItem( 
       
   264                 FindWidgetMenuItemL( aPlugin, source->StringValue() ) );
       
   265             
       
   266             if ( widgetItem )
       
   267                 {
       
   268                 mappedItem = ETrue;
       
   269                 
       
   270                 // Get label from the view item
       
   271                 prop = aItem->LabelL();
       
   272                 
       
   273                 if ( !prop )
       
   274                     {
       
   275                     // no localization in view, check from widget
       
   276                     prop = widgetItem->LabelL();
       
   277                     }
       
   278                 
       
   279                 // Pretend the original item is this widget item
       
   280                 aItem = widgetItem;            
       
   281                 }                                               
       
   282             }                              
       
   283         }
       
   284 
       
   285     const TDesC8& type( aItem->Type()->Type() );
       
   286     
       
   287     if ( type == KMenuItem )
       
   288         {
       
   289         // By default plain menuitems are always shown
       
   290         menuitem = aItem;                                       
       
   291         }
       
   292     else if ( type == KDynMenuItem && iObserver )
       
   293         {
       
   294         CXnNode* plugin( aPlugin.Owner()->LayoutNode() );
       
   295         
       
   296         // Let observer decide whether dynmenuitem is visible or not
       
   297         if ( iObserver->DynInitMenuItemL( aItem->AppIfL(), &plugin->AppIfL() ) )
       
   298             {
       
   299             menuitem = aItem;                       
       
   300             }
       
   301         }
       
   302         
       
   303     if ( menuitem )
       
   304         {
       
   305         CXnProperty* display( menuitem->DisplayL() );
       
   306         
       
   307         if ( display && display->StringValue() == 
       
   308             XnPropertyNames::style::common::display::KNone )
       
   309             {
       
   310             return;
       
   311             }
       
   312         
       
   313         if ( !mappedItem )
       
   314             {
       
   315             prop = menuitem->LabelL(); 
       
   316             }
       
   317 
       
   318         if ( prop )
       
   319             {
       
   320             HBufC* label( prop->StringValueL() );
       
   321             CleanupStack::PushL( label );
       
   322             
       
   323             iStylusPopupMenu->AddMenuItemL( *label, iCommandId++ );
       
   324             iMenuItems.AppendL( menuitem );
       
   325 
       
   326             CleanupStack::PopAndDestroy( label );                    
       
   327             }        
       
   328         }       
       
   329     }
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CXnPopupControlAdapter::ShowMenuL
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CXnPopupControlAdapter::ShowMenuL( CXnPluginData& aPlugin, 
       
   336     TPoint aPosition )
       
   337     {
       
   338     if ( iMenuItems.Count() > 0 && !iMenuShown )
       
   339         {
       
   340         iUiEngine->AppUiAdapter().HideFocus();            
       
   341     
       
   342         CXnNode* plugin( aPlugin.Owner()->LayoutNode() );
       
   343         
       
   344         iUiEngine->Editor()->SetTargetPlugin( plugin );
       
   345                                       
       
   346         iStylusPopupMenu->SetPosition( aPosition, 
       
   347             CAknStylusPopUpMenu::EPositionTypeRightBottom );                        
       
   348         
       
   349         iStylusPopupMenu->ShowMenu();
       
   350         iMenuShown = ETrue;
       
   351         }    
       
   352     }
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   355 // CXnPopupControlAdapter::HideMenuL
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 void CXnPopupControlAdapter::HideMenuL()
       
   359     {
       
   360     if ( iMenuShown )
       
   361         {        
       
   362         ProcessCommandL( KErrCancel );
       
   363         
       
   364         delete iStylusPopupMenu;
       
   365         iStylusPopupMenu = NULL;
       
   366         
       
   367         iMenuShown = EFalse;
       
   368         }    
       
   369     }
       
   370 
       
   371 
       
   372 // End of file