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