LCOV - code coverage report
Current view: top level - libreoffice/framework/source/fwe/helper - actiontriggerhelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 162 0.0 %
Date: 2012-12-27 Functions: 0 10 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <framework/actiontriggerhelper.hxx>
      21             : #include <classes/actiontriggerseparatorpropertyset.hxx>
      22             : #include <classes/rootactiontriggercontainer.hxx>
      23             : #include <classes/imagewrapper.hxx>
      24             : #include <framework/addonsoptions.hxx>
      25             : #include <com/sun/star/lang/XServiceInfo.hpp>
      26             : #include <com/sun/star/beans/XPropertySet.hpp>
      27             : #include <com/sun/star/awt/XBitmap.hpp>
      28             : #include <vcl/svapp.hxx>
      29             : #include <osl/mutex.hxx>
      30             : #include <tools/stream.hxx>
      31             : #include <cppuhelper/weak.hxx>
      32             : #include <comphelper/processfactory.hxx>
      33             : 
      34             : 
      35             : const sal_uInt16 START_ITEMID = 1000;
      36             : 
      37             : using namespace com::sun::star::awt;
      38             : using namespace com::sun::star::uno;
      39             : using namespace com::sun::star::lang;
      40             : using namespace com::sun::star::beans;
      41             : using namespace com::sun::star::container;
      42             : 
      43             : using ::rtl::OUString;
      44             : 
      45             : namespace framework
      46             : {
      47             : 
      48             : // ----------------------------------------------------------------------------
      49             : // implementation helper ( menu => ActionTrigger )
      50             : // ----------------------------------------------------------------------------
      51             : 
      52           0 : sal_Bool IsSeparator( Reference< XPropertySet > xPropertySet )
      53             : {
      54           0 :     Reference< XServiceInfo > xServiceInfo( xPropertySet, UNO_QUERY );
      55             :     try
      56             :     {
      57           0 :         return xServiceInfo->supportsService( OUString( RTL_CONSTASCII_USTRINGPARAM( SERVICENAME_ACTIONTRIGGERSEPARATOR )) );
      58             :     }
      59           0 :     catch (const Exception&)
      60             :     {
      61             :     }
      62             : 
      63           0 :     return sal_False;
      64             : }
      65             : 
      66           0 : void GetMenuItemAttributes( Reference< XPropertySet > xActionTriggerPropertySet,
      67             :                             OUString& aMenuLabel,
      68             :                             OUString& aCommandURL,
      69             :                             OUString& aHelpURL,
      70             :                             Reference< XBitmap >& xBitmap,
      71             :                             Reference< XIndexContainer >& xSubContainer )
      72             : {
      73           0 :     Any a;
      74             : 
      75             :     try
      76             :     {
      77             :         // mandatory properties
      78           0 :         a = xActionTriggerPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" )) );
      79           0 :         a >>= aMenuLabel;
      80           0 :         a = xActionTriggerPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "CommandURL" )) );
      81           0 :         a >>= aCommandURL;
      82           0 :         a = xActionTriggerPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Image" )) );
      83           0 :         a >>= xBitmap;
      84           0 :         a = xActionTriggerPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "SubContainer" )) );
      85           0 :         a >>= xSubContainer;
      86             :     }
      87           0 :     catch (const Exception&)
      88             :     {
      89             :     }
      90             : 
      91             :     // optional properties
      92             :     try
      93             :     {
      94           0 :         a = xActionTriggerPropertySet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "HelpURL" )) );
      95           0 :         a >>= aHelpURL;
      96             :     }
      97           0 :     catch (const Exception&)
      98             :     {
      99           0 :     }
     100           0 : }
     101             : 
     102           0 : void InsertSubMenuItems( Menu* pSubMenu, sal_uInt16& nItemId, Reference< XIndexContainer > xActionTriggerContainer )
     103             : {
     104           0 :     Reference< XIndexAccess > xIndexAccess( xActionTriggerContainer, UNO_QUERY );
     105           0 :     if ( xIndexAccess.is() )
     106             :     {
     107           0 :         AddonsOptions aAddonOptions;
     108           0 :         OUString aSlotURL( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
     109             : 
     110           0 :         for ( sal_Int32 i = 0; i < xIndexAccess->getCount(); i++ )
     111             :         {
     112             :             try
     113             :             {
     114           0 :                 Reference< XPropertySet > xPropSet;
     115           0 :                 if (( xIndexAccess->getByIndex( i ) >>= xPropSet ) && ( xPropSet.is() ))
     116             :                 {
     117           0 :                     if ( IsSeparator( xPropSet ))
     118             :                     {
     119             :                         // Separator
     120           0 :                         SolarMutexGuard aGuard;
     121           0 :                         pSubMenu->InsertSeparator();
     122             :                     }
     123             :                     else
     124             :                     {
     125             :                         // Menu item
     126           0 :                         OUString aLabel;
     127           0 :                         OUString aCommandURL;
     128           0 :                         OUString aHelpURL;
     129           0 :                         Reference< XBitmap > xBitmap;
     130           0 :                         Reference< XIndexContainer > xSubContainer;
     131             : 
     132           0 :                         sal_uInt16 nNewItemId = nItemId++;
     133           0 :                         GetMenuItemAttributes( xPropSet, aLabel, aCommandURL, aHelpURL, xBitmap, xSubContainer );
     134             : 
     135           0 :                         SolarMutexGuard aGuard;
     136             :                         {
     137             :                             // insert new menu item
     138           0 :                             sal_Int32 nIndex = aCommandURL.indexOf( aSlotURL );
     139           0 :                             if ( nIndex >= 0 )
     140             :                             {
     141             :                                 // Special code for our menu implementation: some menu items don't have a
     142             :                                 // command url but uses the item id as a unqiue identifier. These entries
     143             :                                 // got a special url during conversion from menu=>actiontriggercontainer.
     144             :                                 // Now we have to extract this special url and set the correct item id!!!
     145           0 :                                 nNewItemId = (sal_uInt16)aCommandURL.copy( nIndex+aSlotURL.getLength() ).toInt32();
     146           0 :                                 pSubMenu->InsertItem( nNewItemId, aLabel );
     147             :                             }
     148             :                             else
     149             :                             {
     150           0 :                                 pSubMenu->InsertItem( nNewItemId, aLabel );
     151           0 :                                 pSubMenu->SetItemCommand( nNewItemId, aCommandURL );
     152             :                             }
     153             : 
     154             :                             // handle bitmap
     155           0 :                             if ( xBitmap.is() )
     156             :                             {
     157           0 :                                 sal_Bool bImageSet = sal_False;
     158             : 
     159           0 :                                 Reference< XUnoTunnel > xUnoTunnel( xBitmap, UNO_QUERY );
     160           0 :                                 if ( xUnoTunnel.is() )
     161             :                                 {
     162             :                                     // Try to get implementation pointer through XUnoTunnel
     163           0 :                                     sal_Int64 nPointer = xUnoTunnel->getSomething( ImageWrapper::GetUnoTunnelId() );
     164           0 :                                     if ( nPointer )
     165             :                                     {
     166             :                                         // This is our own optimized implementation of menu images!
     167           0 :                                         ImageWrapper* pImageWrapper = reinterpret_cast< ImageWrapper * >( nPointer );
     168           0 :                                         Image aMenuImage = pImageWrapper->GetImage();
     169             : 
     170           0 :                                         if ( !!aMenuImage )
     171           0 :                                             pSubMenu->SetItemImage( nNewItemId, aMenuImage );
     172             : 
     173           0 :                                         bImageSet = sal_True;
     174             :                                     }
     175             :                                 }
     176             : 
     177           0 :                                 if ( !bImageSet )
     178             :                                 {
     179             :                                     // This is an unknown implementation of a XBitmap interface. We have to
     180             :                                     // use a more time consuming way to build an Image!
     181           0 :                                     Image   aImage;
     182           0 :                                     Bitmap  aBitmap;
     183             : 
     184           0 :                                     Sequence< sal_Int8 > aDIBSeq;
     185             :                                     {
     186           0 :                                         aDIBSeq = xBitmap->getDIB();
     187           0 :                                         SvMemoryStream aMem( (void *)aDIBSeq.getConstArray(), aDIBSeq.getLength(), STREAM_READ );
     188           0 :                                         aMem >> aBitmap;
     189             :                                     }
     190             : 
     191           0 :                                     aDIBSeq = xBitmap->getMaskDIB();
     192           0 :                                     if ( aDIBSeq.getLength() > 0 )
     193             :                                     {
     194           0 :                                         Bitmap aMaskBitmap;
     195           0 :                                         SvMemoryStream aMem( (void *)aDIBSeq.getConstArray(), aDIBSeq.getLength(), STREAM_READ );
     196           0 :                                         aMem >> aMaskBitmap;
     197           0 :                                         aImage = Image( aBitmap, aMaskBitmap );
     198             :                                     }
     199             :                                     else
     200           0 :                                         aImage = Image( aBitmap );
     201             : 
     202           0 :                                     if ( !!aImage )
     203           0 :                                         pSubMenu->SetItemImage( nNewItemId, aImage );
     204           0 :                                 }
     205             :                             }
     206             :                             else
     207             :                             {
     208             :                                 // Support add-on images for context menu interceptors
     209           0 :                                 Image aImage = aAddonOptions.GetImageFromURL( aCommandURL, sal_False, sal_True );
     210           0 :                                 if ( !!aImage )
     211           0 :                                     pSubMenu->SetItemImage( nNewItemId, aImage );
     212             :                             }
     213             : 
     214           0 :                             if ( xSubContainer.is() )
     215             :                             {
     216           0 :                                 PopupMenu* pNewSubMenu = new PopupMenu;
     217             : 
     218             :                                 // Sub menu (recursive call CreateSubMenu )
     219           0 :                                 InsertSubMenuItems( pNewSubMenu, nItemId, xSubContainer );
     220           0 :                                 pSubMenu->SetPopupMenu( nNewItemId, pNewSubMenu );
     221             :                             }
     222           0 :                         }
     223             :                     }
     224           0 :                 }
     225             :             }
     226           0 :             catch (const IndexOutOfBoundsException&)
     227             :             {
     228             :                 return;
     229             :             }
     230           0 :             catch (const WrappedTargetException&)
     231             :             {
     232             :                 return;
     233             :             }
     234           0 :             catch (const RuntimeException&)
     235             :             {
     236             :                 return;
     237             :             }
     238           0 :         }
     239           0 :     }
     240             : }
     241             : 
     242             : 
     243             : // ----------------------------------------------------------------------------
     244             : // implementation helper ( ActionTrigger => menu )
     245             : // ----------------------------------------------------------------------------
     246             : 
     247           0 : Reference< XPropertySet > CreateActionTrigger( sal_uInt16 nItemId, const Menu* pMenu, const Reference< XIndexContainer >& rActionTriggerContainer ) throw ( RuntimeException )
     248             : {
     249           0 :     Reference< XPropertySet > xPropSet;
     250             : 
     251           0 :     Reference< XMultiServiceFactory > xMultiServiceFactory( rActionTriggerContainer, UNO_QUERY );
     252           0 :     if ( xMultiServiceFactory.is() )
     253             :     {
     254           0 :         xPropSet = Reference< XPropertySet >(   xMultiServiceFactory->createInstance(
     255           0 :                                                     OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.ActionTrigger" )) ),
     256           0 :                                                 UNO_QUERY );
     257             : 
     258           0 :         Any a;
     259             : 
     260             :         try
     261             :         {
     262             :             // Retrieve the menu attributes and set them in our PropertySet
     263           0 :             OUString aLabel = pMenu->GetItemText( nItemId );
     264           0 :             a <<= aLabel;
     265           0 :             xPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" )), a );
     266             : 
     267           0 :             OUString aCommandURL = pMenu->GetItemCommand( nItemId );
     268             : 
     269           0 :             if ( aCommandURL.isEmpty() )
     270             :             {
     271           0 :                 aCommandURL = OUString( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
     272           0 :                 aCommandURL += OUString::valueOf( (sal_Int32)nItemId );
     273             :             }
     274             : 
     275           0 :             a <<= aCommandURL;
     276           0 :             xPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "CommandURL" )), a );
     277             : 
     278           0 :             Image aImage = pMenu->GetItemImage( nItemId );
     279           0 :             if ( !!aImage )
     280             :             {
     281             :                 // We use our own optimized XBitmap implementation
     282           0 :                 Reference< XBitmap > xBitmap( static_cast< cppu::OWeakObject* >( new ImageWrapper( aImage )), UNO_QUERY );
     283           0 :                 a <<= xBitmap;
     284           0 :                 xPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "Image" )), a );
     285           0 :             }
     286             :         }
     287           0 :         catch (const Exception&)
     288             :         {
     289           0 :         }
     290             :     }
     291             : 
     292           0 :     return xPropSet;
     293             : }
     294             : 
     295           0 : Reference< XPropertySet > CreateActionTriggerSeparator( const Reference< XIndexContainer >& rActionTriggerContainer ) throw ( RuntimeException )
     296             : {
     297           0 :     Reference< XMultiServiceFactory > xMultiServiceFactory( rActionTriggerContainer, UNO_QUERY );
     298           0 :     if ( xMultiServiceFactory.is() )
     299             :     {
     300           0 :         return Reference< XPropertySet >(   xMultiServiceFactory->createInstance(
     301           0 :                                                 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.ActionTriggerSeparator" )) ),
     302           0 :                                             UNO_QUERY );
     303             :     }
     304             : 
     305           0 :     return Reference< XPropertySet >();
     306             : }
     307             : 
     308           0 : Reference< XIndexContainer > CreateActionTriggerContainer( const Reference< XIndexContainer >& rActionTriggerContainer ) throw ( RuntimeException )
     309             : {
     310           0 :     Reference< XMultiServiceFactory > xMultiServiceFactory( rActionTriggerContainer, UNO_QUERY );
     311           0 :     if ( xMultiServiceFactory.is() )
     312             :     {
     313           0 :         return Reference< XIndexContainer >( xMultiServiceFactory->createInstance(
     314           0 :                                                 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.ActionTriggerContainer" )) ),
     315           0 :                                              UNO_QUERY );
     316             :     }
     317             : 
     318           0 :     return Reference< XIndexContainer >();
     319             : }
     320             : 
     321           0 : void FillActionTriggerContainerWithMenu( const Menu* pMenu, Reference< XIndexContainer >& rActionTriggerContainer )
     322             : {
     323           0 :     SolarMutexGuard aGuard;
     324             : 
     325           0 :     for ( sal_uInt16 nPos = 0; nPos < pMenu->GetItemCount(); nPos++ )
     326             :     {
     327           0 :         sal_uInt16          nItemId = pMenu->GetItemId( nPos );
     328           0 :         MenuItemType    nType   = pMenu->GetItemType( nPos );
     329             : 
     330             :         try
     331             :         {
     332           0 :             Any a;
     333           0 :             Reference< XPropertySet > xPropSet;
     334             : 
     335           0 :             if ( nType == MENUITEM_SEPARATOR )
     336             :             {
     337           0 :                 xPropSet = CreateActionTriggerSeparator( rActionTriggerContainer );
     338             : 
     339           0 :                 a <<= xPropSet;
     340           0 :                 rActionTriggerContainer->insertByIndex( nPos, a );
     341             :             }
     342             :             else
     343             :             {
     344           0 :                 xPropSet = CreateActionTrigger( nItemId, pMenu, rActionTriggerContainer );
     345             : 
     346           0 :                 a <<= xPropSet;
     347           0 :                 rActionTriggerContainer->insertByIndex( nPos, a );
     348             : 
     349           0 :                 PopupMenu* pPopupMenu = pMenu->GetPopupMenu( nItemId );
     350           0 :                 if ( pPopupMenu )
     351             :                 {
     352             :                     // recursive call to build next sub menu
     353           0 :                     Reference< XIndexContainer > xSubContainer = CreateActionTriggerContainer( rActionTriggerContainer );
     354             : 
     355           0 :                     a <<= xSubContainer;
     356           0 :                     xPropSet->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "SubContainer" )), a );
     357           0 :                     FillActionTriggerContainerWithMenu( pPopupMenu, xSubContainer );
     358             :                 }
     359           0 :             }
     360             :         }
     361           0 :         catch (const Exception&)
     362             :         {
     363             :         }
     364           0 :     }
     365           0 : }
     366             : 
     367           0 : void ActionTriggerHelper::CreateMenuFromActionTriggerContainer(
     368             :     Menu* pNewMenu,
     369             :     const Reference< XIndexContainer >& rActionTriggerContainer )
     370             : {
     371           0 :     sal_uInt16 nItemId = START_ITEMID;
     372             : 
     373           0 :     if ( rActionTriggerContainer.is() )
     374           0 :         InsertSubMenuItems( pNewMenu, nItemId, rActionTriggerContainer );
     375           0 : }
     376             : 
     377           0 : void ActionTriggerHelper::FillActionTriggerContainerFromMenu(
     378             :     Reference< XIndexContainer >& xActionTriggerContainer,
     379             :     const Menu* pMenu )
     380             : {
     381           0 :     FillActionTriggerContainerWithMenu( pMenu, xActionTriggerContainer );
     382           0 : }
     383             : 
     384           0 : Reference< XIndexContainer > ActionTriggerHelper::CreateActionTriggerContainerFromMenu(
     385             :     const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
     386             :     const Menu* pMenu,
     387             :     const ::rtl::OUString* pMenuIdentifier )
     388             : {
     389           0 :     return new RootActionTriggerContainer( pMenu, pMenuIdentifier, xServiceFactory );
     390             : }
     391             : 
     392             : }
     393             : 
     394             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10