LibreOffice Module framework (master) 1
newmenucontroller.cxx
Go to the documentation of this file.
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
21#include <menuconfiguration.hxx>
22
23#include <services.h>
24
25#include <com/sun/star/awt/MenuItemType.hpp>
26#include <com/sun/star/beans/PropertyValue.hpp>
27#include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
28#include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
29#include <com/sun/star/ui/GlobalAcceleratorConfiguration.hpp>
30#include <com/sun/star/frame/ModuleManager.hpp>
31#include <com/sun/star/frame/XFrame.hpp>
32#include <com/sun/star/util/XURLTransformer.hpp>
33
35#include <vcl/svapp.hxx>
36#include <vcl/settings.hxx>
39#include <svtools/imagemgr.hxx>
41#include <tools/urlobj.hxx>
43#include <osl/mutex.hxx>
45
46// Defines
47constexpr OUStringLiteral aSlotNewDocDirect = u".uno:AddDirect";
48constexpr OUStringLiteral aSlotAutoPilot = u".uno:AutoPilotMenu";
49
50using namespace com::sun::star::uno;
51using namespace com::sun::star::lang;
52using namespace com::sun::star::frame;
53using namespace com::sun::star::beans;
54using namespace com::sun::star::util;
55using namespace com::sun::star::container;
56using namespace com::sun::star::ui;
57
58namespace framework
59{
60
62{
63 return "com.sun.star.comp.framework.NewMenuController";
64}
65
66sal_Bool SAL_CALL NewMenuController::supportsService( const OUString& sServiceName )
67{
69}
70
71css::uno::Sequence< OUString > SAL_CALL NewMenuController::getSupportedServiceNames()
72{
74}
75
76void NewMenuController::setMenuImages( PopupMenu* pPopupMenu, bool bSetImages )
77{
78 sal_uInt16 nItemCount = pPopupMenu->GetItemCount();
80
81 for ( sal_uInt16 i = 0; i < nItemCount; i++ )
82 {
83 sal_uInt16 nItemId = pPopupMenu->GetItemId( i );
84 if ( nItemId != 0 )
85 {
86 if ( bSetImages )
87 {
88 OUString aImageId;
89 OUString aCmd( pPopupMenu->GetItemCommand( nItemId ) );
90 void* nAttributePtr = pPopupMenu->GetUserValue( nItemId );
91 MenuAttributes* pAttributes = static_cast<MenuAttributes *>(nAttributePtr);
92 if (pAttributes)
93 aImageId = pAttributes->aImageId;
94
95 INetURLObject aURLObj( aImageId.isEmpty() ? aCmd : aImageId );
97 if ( !aImage )
99
100 if ( !!aImage )
101 pPopupMenu->SetItemImage( nItemId, aImage );
102 }
103 else
104 pPopupMenu->SetItemImage( nItemId, Image() );
105 }
106 }
107}
108
109void NewMenuController::determineAndSetNewDocAccel(const css::awt::KeyEvent& rKeyCode)
110{
111 sal_uInt16 nCount(m_xPopupMenu->getItemCount());
112 sal_uInt16 nId( 0 );
113 OUString aCommand;
114
115 if ( !m_aEmptyDocURL.isEmpty() )
116 {
117 // Search for the empty document URL
118
119 for ( sal_uInt16 i = 0; i < nCount; i++ )
120 {
121 if (m_xPopupMenu->getItemType(i) != css::awt::MenuItemType_SEPARATOR)
122 {
123 nId = m_xPopupMenu->getItemId(i);
124 aCommand = m_xPopupMenu->getCommand(nId);
125 if ( aCommand.startsWith( m_aEmptyDocURL ) )
126 {
127 m_xPopupMenu->setAcceleratorKeyEvent(nId, rKeyCode);
128 break;
129 }
130 }
131 }
132 }
133}
134
136{
137 if ( !m_bModuleIdentified )
138 return;
139
143
144 if ( !m_bAcceleratorCfg )
145 {
146 // Retrieve references on demand
147 m_bAcceleratorCfg = true;
148 if ( !xDocAccelCfg.is() )
149 {
152 if ( xController.is() )
153 {
154 xModel = xController->getModel();
155 if ( xModel.is() )
156 {
158 if ( xSupplier.is() )
159 {
160 Reference< XUIConfigurationManager > xDocUICfgMgr = xSupplier->getUIConfigurationManager();
161 if ( xDocUICfgMgr.is() )
162 {
163 xDocAccelCfg = xDocUICfgMgr->getShortCutManager();
164 m_xDocAcceleratorManager = xDocAccelCfg;
165 }
166 }
167 }
168 }
169 }
170
171 if ( !xModuleAccelCfg.is() )
172 {
174 theModuleUIConfigurationManagerSupplier::get( m_xContext );
175 Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( m_aModuleIdentifier );
176 if ( xUICfgMgr.is() )
177 {
178 xModuleAccelCfg = xUICfgMgr->getShortCutManager();
179 m_xModuleAcceleratorManager = xModuleAccelCfg;
180 }
181 }
182
183 if ( !xGlobalAccelCfg.is() )
184 {
185 xGlobalAccelCfg = GlobalAcceleratorConfiguration::create( m_xContext );
186 m_xGlobalAcceleratorManager = xGlobalAccelCfg;
187 }
188 }
189
190 vcl::KeyCode aEmptyKeyCode;
191 sal_uInt16 nItemCount(m_xPopupMenu->getItemCount());
192 std::vector< vcl::KeyCode > aMenuShortCuts;
193 std::vector< OUString > aCmds;
194 std::vector< sal_uInt16 > aIds;
195 for ( sal_uInt16 i = 0; i < nItemCount; i++ )
196 {
197 if (m_xPopupMenu->getItemType(i) != css::awt::MenuItemType_SEPARATOR)
198 {
199 sal_uInt16 nId(m_xPopupMenu->getItemId(i));
200 aIds.push_back( nId );
201 aMenuShortCuts.push_back( aEmptyKeyCode );
202 aCmds.push_back(m_xPopupMenu->getCommand(nId));
203 }
204 }
205
206 sal_uInt32 nSeqCount( aIds.size() );
207
208 if ( m_bNewMenu )
209 nSeqCount+=1;
210
211 Sequence< OUString > aSeq( nSeqCount );
212 auto aSeqRange = asNonConstRange(aSeq);
213
214 // Add a special command for our "New" menu.
215 if ( m_bNewMenu )
216 {
217 aSeqRange[nSeqCount-1] = m_aCommandURL;
218 aMenuShortCuts.push_back( aEmptyKeyCode );
219 }
220
221 const sal_uInt32 nCount = aCmds.size();
222 for ( sal_uInt32 i = 0; i < nCount; i++ )
223 aSeqRange[i] = aCmds[i];
224
226 retrieveShortcutsFromConfiguration( xGlobalAccelCfg, aSeq, aMenuShortCuts );
228 retrieveShortcutsFromConfiguration( xModuleAccelCfg, aSeq, aMenuShortCuts );
229 if ( m_xDocAcceleratorManager.is() )
230 retrieveShortcutsFromConfiguration( xDocAccelCfg, aSeq, aMenuShortCuts );
231
232 const sal_uInt32 nCount2 = aIds.size();
233 for ( sal_uInt32 i = 0; i < nCount2; i++ )
234 m_xPopupMenu->setAcceleratorKeyEvent(aIds[i], svt::AcceleratorExecute::st_VCLKey2AWTKey(aMenuShortCuts[i]));
235
236 // Special handling for "New" menu short-cut should be set at the
237 // document which will be opened using it.
238 if ( m_bNewMenu )
239 {
240 if ( aMenuShortCuts[nSeqCount-1] != aEmptyKeyCode )
242 }
243}
244
247 const Sequence< OUString >& rCommands,
248 std::vector< vcl::KeyCode >& aMenuShortCuts )
249{
250 if ( !rAccelCfg.is() )
251 return;
252
253 try
254 {
255 css::awt::KeyEvent aKeyEvent;
256 Sequence< Any > aSeqKeyCode = rAccelCfg->getPreferredKeyEventsForCommandList( rCommands );
257 for ( sal_Int32 i = 0; i < aSeqKeyCode.getLength(); i++ )
258 {
259 if ( aSeqKeyCode[i] >>= aKeyEvent )
260 aMenuShortCuts[i] = svt::AcceleratorExecute::st_AWTKey2VCLKey( aKeyEvent );
261 }
262 }
263 catch ( const IllegalArgumentException& )
264 {
265 }
266}
267
268NewMenuController::NewMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
269 svt::PopupMenuControllerBase( xContext ),
270 m_bShowImages( true ),
271 m_bNewMenu( false ),
272 m_bModuleIdentified( false ),
273 m_bAcceleratorCfg( false ),
274 m_aTargetFrame( "_default" ),
275 m_xContext( xContext )
276{
277}
278
280{
281}
282
283// private function
285{
286 VCLXPopupMenu* pPopupMenu = static_cast<VCLXPopupMenu *>(dynamic_cast<VCLXMenu*>( rPopupMenu.get() ));
287 PopupMenu* pVCLPopupMenu = nullptr;
288
289 SolarMutexGuard aSolarMutexGuard;
290
291 resetPopupMenu( rPopupMenu );
292 if ( pPopupMenu )
293 pVCLPopupMenu = static_cast<PopupMenu *>(pPopupMenu->GetMenu());
294
295 if ( !pVCLPopupMenu )
296 return;
297
298 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
300 aTargetURL.Complete = m_bNewMenu ? OUString(aSlotNewDocDirect) : OUString(aSlotAutoPilot);
301 m_xURLTransformer->parseStrict( aTargetURL );
302 Reference< XDispatch > xMenuItemDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
303 if(xMenuItemDispatch == nullptr)
304 return;
305
306 const std::vector< SvtDynMenuEntry > aDynamicMenuEntries =
307 SvtDynamicMenuOptions::GetMenu( m_bNewMenu ? EDynamicMenuType::NewMenu : EDynamicMenuType::WizardMenu );
308
309 sal_uInt16 nItemId = 1;
310
311 for ( const auto& aDynamicMenuEntry : aDynamicMenuEntries )
312 {
313 if ( aDynamicMenuEntry.sTitle.isEmpty() && aDynamicMenuEntry.sURL.isEmpty() )
314 continue;
315
316 if ( aDynamicMenuEntry.sURL == "private:separator" )
317 rPopupMenu->insertSeparator(-1);
318 else
319 {
320 rPopupMenu->insertItem(nItemId, aDynamicMenuEntry.sTitle, 0, -1);
321 rPopupMenu->setCommand(nItemId, aDynamicMenuEntry.sURL);
322
323 void* nAttributePtr = MenuAttributes::CreateAttribute( aDynamicMenuEntry.sTargetName, aDynamicMenuEntry.sImageIdentifier );
324 pPopupMenu->setUserValue(nItemId, nAttributePtr, MenuAttributes::ReleaseAttribute);
325
326 nItemId++;
327 }
328 }
329
330 if ( m_bShowImages )
331 setMenuImages( pVCLPopupMenu, m_bShowImages );
332}
333
334// XEventListener
335void SAL_CALL NewMenuController::disposing( const EventObject& )
336{
338
339 std::unique_lock aLock( m_aMutex );
340 m_xFrame.clear();
341 m_xDispatch.clear();
342 m_xContext.clear();
343
344 if ( m_xPopupMenu.is() )
345 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
346 m_xPopupMenu.clear();
347}
348
349// XStatusListener
350void SAL_CALL NewMenuController::statusChanged( const FeatureStateEvent& Event )
351{
352 Event.State >>= m_aEmptyDocURL;
353}
354
355// XMenuListener
356void SAL_CALL NewMenuController::itemSelected( const css::awt::MenuEvent& rEvent )
357{
360
361 {
362 std::unique_lock aLock(m_aMutex);
363 xPopupMenu = m_xPopupMenu;
364 xContext = m_xContext;
365 }
366
367 if ( !xPopupMenu.is() )
368 return;
369
370 VCLXPopupMenu* pPopupMenu = static_cast<VCLXPopupMenu *>(dynamic_cast<VCLXMenu*>( xPopupMenu.get() ));
371 if ( !pPopupMenu )
372 return;
373
374 OUString aURL;
375 OUString aTargetFrame( m_aTargetFrame );
376
377 {
378 SolarMutexGuard aSolarMutexGuard;
379 aURL = pPopupMenu->getCommand(rEvent.MenuId);
380 void* nAttributePtr = pPopupMenu->getUserValue(rEvent.MenuId);
381 MenuAttributes* pAttributes = static_cast<MenuAttributes *>(nAttributePtr);
382 if (pAttributes)
383 aTargetFrame = pAttributes->aTargetFrame;
384 }
385
387 OUString( "private:user" )) };
388
389 dispatchCommand( aURL, aArgsList, aTargetFrame );
390}
391
392void SAL_CALL NewMenuController::itemActivated( const css::awt::MenuEvent& )
393{
394 SolarMutexGuard aSolarMutexGuard;
395 if ( !(m_xFrame.is() && m_xPopupMenu.is()) )
396 return;
397
399 bool bShowImages( rSettings.GetUseImagesInMenus() );
400 OUString aIconTheme( rSettings.DetermineIconTheme() );
401
402 PopupMenu* pVCLPopupMenu = static_cast<PopupMenu *>(m_xPopupMenu->GetMenu());
403
404 if ( m_bShowImages != bShowImages || m_aIconTheme != aIconTheme )
405 {
406 m_bShowImages = bShowImages;
407 m_aIconTheme = aIconTheme;
408 setMenuImages( pVCLPopupMenu, m_bShowImages );
409 }
410
412}
413
414// XPopupMenuController
416{
417
418 if ( m_xPopupMenu.is() )
419 fillPopupMenu( m_xPopupMenu );
420
421 // Identify module that we are attach to. It's our context that we need to know.
422 Reference< XModuleManager2 > xModuleManager = ModuleManager::create( m_xContext );
423 try
424 {
425 m_aModuleIdentifier = xModuleManager->identify( m_xFrame );
426 m_bModuleIdentified = true;
427 }
428 catch ( const RuntimeException& )
429 {
430 throw;
431 }
432 catch ( const Exception& )
433 {
434 }
435}
436
437// XInitialization
438void NewMenuController::initializeImpl( std::unique_lock<std::mutex>& rGuard, const Sequence< Any >& aArguments )
439{
440 bool bInitialized( m_bInitialized );
441 if ( bInitialized )
442 return;
443
444 svt::PopupMenuControllerBase::initializeImpl( rGuard, aArguments );
445
446 if ( m_bInitialized )
447 {
449
451 m_aIconTheme = rSettings.DetermineIconTheme();
452 m_bNewMenu = m_aCommandURL == aSlotNewDocDirect;
453 }
454}
455
456}
457
458
459extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
461 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
462{
463 return cppu::acquire(new framework::NewMenuController(context));
464}
465
466/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
void SetItemImage(sal_uInt16 nItemId, const Image &rImage)
OUString GetItemCommand(sal_uInt16 nItemId) const
sal_uInt16 GetItemId(sal_uInt16 nPos) const
void * GetUserValue(sal_uInt16 nItemId) const
sal_uInt16 GetItemCount() const
OUString DetermineIconTheme() const
bool GetUseImagesInMenus() const
static SVT_DLLPUBLIC Image GetImageNoDefault(const INetURLObject &rURL, vcl::ImageType eImageType=vcl::ImageType::Small)
void * getUserValue(sal_uInt16 nItemId)
virtual OUString SAL_CALL getCommand(sal_Int16 nItemId) override
Menu * GetMenu() const
void setUserValue(sal_uInt16 nItemId, void *nUserValue, MenuUserDataReleaseFunction aFunc)
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
virtual ~NewMenuController() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
void retrieveShortcutsFromConfiguration(const css::uno::Reference< css::ui::XAcceleratorConfiguration > &rAccelCfg, const css::uno::Sequence< OUString > &rCommands, std::vector< vcl::KeyCode > &aMenuShortCuts)
void setMenuImages(PopupMenu *pPopupMenu, bool bSetImages)
virtual void impl_setPopupMenu() override
css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xModuleAcceleratorManager
NewMenuController(const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual OUString SAL_CALL getImplementationName() override
virtual void SAL_CALL itemSelected(const css::awt::MenuEvent &rEvent) override
void determineAndSetNewDocAccel(const css::awt::KeyEvent &rKeyCode)
virtual void initializeImpl(std::unique_lock< std::mutex > &rGuard, const css::uno::Sequence< css::uno::Any > &aArguments) override
css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xDocAcceleratorManager
virtual void SAL_CALL itemActivated(const css::awt::MenuEvent &rEvent) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
void fillPopupMenu(css::uno::Reference< css::awt::XPopupMenu > const &rPopupMenu)
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
css::uno::Reference< css::ui::XAcceleratorConfiguration > m_xGlobalAcceleratorManager
static vcl::KeyCode st_AWTKey2VCLKey(const css::awt::KeyEvent &aKey)
static css::awt::KeyEvent st_VCLKey2AWTKey(const vcl::KeyCode &aKey)
int nCount
URL aURL
float u
css::uno::Reference< css::uno::XComponentContext > m_xContext
Sequence< PropertyValue > aArguments
Sequence< sal_Int8 > aSeq
std::vector< SvtDynMenuEntry > GetMenu(EDynamicMenuType eMenu)
@ Exception
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_POPUPMENUCONTROLLER
Definition: services.h:33
int i
Image GetImageForCommand(const OUString &rsCommandName, const Reference< frame::XFrame > &rxFrame, vcl::ImageType eImageType)
constexpr OUStringLiteral aSlotAutoPilot
constexpr OUStringLiteral aSlotNewDocDirect
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_NewMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
sal_Int16 nId
static void ReleaseAttribute(void *nAttributePtr)
static void * CreateAttribute(const OUString &rFrame, const OUString &rImageIdStr)
Reference< XController > xController
Reference< XFrame > xFrame
Reference< XModel > xModel
OUString aCommand
unsigned char sal_Bool
std::mutex m_aMutex
OUString aTargetURL