tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / framework / source / uielement / controlmenucontroller.cxx
blobaaa86c78acfe34681bf734a770976c2f2a1c18a4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <com/sun/star/frame/XDispatch.hpp>
23 #include <com/sun/star/frame/XDispatchProvider.hpp>
24 #include <com/sun/star/frame/XFrame.hpp>
25 #include <com/sun/star/frame/XStatusListener.hpp>
26 #include <com/sun/star/util/XURLTransformer.hpp>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <vcl/graph.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/settings.hxx>
32 #include <vcl/image.hxx>
33 #include <svtools/popupmenucontrollerbase.hxx>
34 #include <toolkit/awt/vclxmenu.hxx>
35 #include <osl/mutex.hxx>
36 #include <unordered_map>
38 #include <classes/fwkresid.hxx>
39 #include <bitmaps.hlst>
40 #include <strings.hrc>
42 constexpr OUString aCommands[]
44 u".uno:ConvertToEdit"_ustr,
45 u".uno:ConvertToButton"_ustr,
46 u".uno:ConvertToFixed"_ustr,
47 u".uno:ConvertToList"_ustr,
48 u".uno:ConvertToCheckBox"_ustr,
49 u".uno:ConvertToRadio"_ustr,
50 u".uno:ConvertToGroup"_ustr,
51 u".uno:ConvertToCombo"_ustr,
52 u".uno:ConvertToImageBtn"_ustr,
53 u".uno:ConvertToFileControl"_ustr,
54 u".uno:ConvertToDate"_ustr,
55 u".uno:ConvertToTime"_ustr,
56 u".uno:ConvertToNumeric"_ustr,
57 u".uno:ConvertToCurrency"_ustr,
58 u".uno:ConvertToPattern"_ustr,
59 u".uno:ConvertToImageControl"_ustr,
60 u".uno:ConvertToFormatted"_ustr,
61 u".uno:ConvertToScrollBar"_ustr,
62 u".uno:ConvertToSpinButton"_ustr,
63 u".uno:ConvertToNavigationBar"_ustr
66 static TranslateId aLabels[] =
68 RID_STR_PROPTITLE_EDIT,
69 RID_STR_PROPTITLE_PUSHBUTTON,
70 RID_STR_PROPTITLE_FIXEDTEXT,
71 RID_STR_PROPTITLE_LISTBOX,
72 RID_STR_PROPTITLE_CHECKBOX,
73 RID_STR_PROPTITLE_RADIOBUTTON,
74 RID_STR_PROPTITLE_GROUPBOX,
75 RID_STR_PROPTITLE_COMBOBOX,
76 RID_STR_PROPTITLE_IMAGEBUTTON,
77 RID_STR_PROPTITLE_FILECONTROL,
78 RID_STR_PROPTITLE_DATEFIELD,
79 RID_STR_PROPTITLE_TIMEFIELD,
80 RID_STR_PROPTITLE_NUMERICFIELD,
81 RID_STR_PROPTITLE_CURRENCYFIELD,
82 RID_STR_PROPTITLE_PATTERNFIELD,
83 RID_STR_PROPTITLE_IMAGECONTROL,
84 RID_STR_PROPTITLE_FORMATTED,
85 RID_STR_PROPTITLE_SCROLLBAR,
86 RID_STR_PROPTITLE_SPINBUTTON,
87 RID_STR_PROPTITLE_NAVBAR
90 constexpr OUString aImgIds[]
92 RID_SVXBMP_EDITBOX,
93 RID_SVXBMP_BUTTON,
94 RID_SVXBMP_FIXEDTEXT,
95 RID_SVXBMP_LISTBOX,
96 RID_SVXBMP_CHECKBOX,
97 RID_SVXBMP_RADIOBUTTON,
98 RID_SVXBMP_GROUPBOX,
99 RID_SVXBMP_COMBOBOX,
100 RID_SVXBMP_IMAGEBUTTON,
101 RID_SVXBMP_FILECONTROL,
102 RID_SVXBMP_DATEFIELD,
103 RID_SVXBMP_TIMEFIELD,
104 RID_SVXBMP_NUMERICFIELD,
105 RID_SVXBMP_CURRENCYFIELD,
106 RID_SVXBMP_PATTERNFIELD,
107 RID_SVXBMP_IMAGECONTROL,
108 RID_SVXBMP_FORMATTEDFIELD,
109 RID_SVXBMP_SCROLLBAR,
110 RID_SVXBMP_SPINBUTTON,
111 RID_SVXBMP_NAVIGATIONBAR
114 using namespace css;
115 using namespace css::uno;
116 using namespace css::lang;
117 using namespace css::frame;
119 namespace {
121 class ControlMenuController : public svt::PopupMenuControllerBase
123 using svt::PopupMenuControllerBase::disposing;
125 public:
126 explicit ControlMenuController( const uno::Reference< uno::XComponentContext >& xContext );
128 // XServiceInfo
129 virtual OUString SAL_CALL getImplementationName() override
131 return u"com.sun.star.comp.framework.ControlMenuController"_ustr;
134 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
136 return cppu::supportsService(this, ServiceName);
139 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
141 return {u"com.sun.star.frame.PopupMenuController"_ustr};
144 // XPopupMenuController
145 virtual void SAL_CALL updatePopupMenu() override;
147 // XStatusListener
148 virtual void SAL_CALL statusChanged( const frame::FeatureStateEvent& Event ) override;
150 // XMenuListener
151 virtual void SAL_CALL itemActivated( const awt::MenuEvent& rEvent ) override;
153 // XEventListener
154 virtual void SAL_CALL disposing( const lang::EventObject& Source ) override;
156 private:
157 // XInitialization
158 virtual void initializeImpl( std::unique_lock<std::mutex>& rGuard, const uno::Sequence< uno::Any >& aArguments ) override;
160 class UrlToDispatchMap : public std::unordered_map< OUString,
161 uno::Reference< frame::XDispatch > >
163 public:
164 void free()
166 UrlToDispatchMap().swap( *this );// get rid of reserved capacity
170 void updateImagesPopupMenu(Reference<awt::XPopupMenu> const& rPopupMenu);
171 void fillPopupMenu(uno::Reference<awt::XPopupMenu> const& rPopupMenu);
173 bool m_bShowMenuImages : 1;
174 UrlToDispatchMap m_aURLToDispatchMap;
177 ControlMenuController::ControlMenuController(const css::uno::Reference< css::uno::XComponentContext >& xContext)
178 : svt::PopupMenuControllerBase(xContext)
180 const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
181 m_bShowMenuImages = rSettings.GetUseImagesInMenus();
185 // private function
186 void ControlMenuController::updateImagesPopupMenu(Reference<awt::XPopupMenu> const& rPopupMenu)
188 if (!rPopupMenu)
189 return;
190 for (size_t i=0; i < std::size(aCommands); ++i)
192 sal_Int16 nItemId = i + 1;
193 if (m_bShowMenuImages)
195 Image aImage(StockImage::Yes, aImgIds[i]);
196 Graphic aGraphic(aImage);
197 rPopupMenu->setItemImage(nItemId, aGraphic.GetXGraphic(), false);
199 else
200 rPopupMenu->setItemImage(nItemId, nullptr, false);
204 // private function
205 void ControlMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
207 resetPopupMenu( rPopupMenu );
209 for (size_t i=0; i < SAL_N_ELEMENTS(aCommands); ++i)
211 sal_Int16 nItemId = i + 1;
212 OUString sCommand(aCommands[i]);
213 rPopupMenu->insertItem(nItemId, FwkResId(aLabels[i]), 0, i);
214 rPopupMenu->setCommand(nItemId, sCommand);
215 rPopupMenu->enableItem(nItemId, false);
218 updateImagesPopupMenu(rPopupMenu);
220 rPopupMenu->hideDisabledEntries(true);
225 // XEventListener
226 void SAL_CALL ControlMenuController::disposing( const EventObject& )
228 Reference< css::awt::XMenuListener > xHolder(this);
230 std::unique_lock aLock( m_aMutex );
231 m_xFrame.clear();
232 m_xDispatch.clear();
234 if ( m_xPopupMenu.is() )
235 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
236 m_xPopupMenu.clear();
239 // XStatusListener
240 void SAL_CALL ControlMenuController::statusChanged( const FeatureStateEvent& Event )
242 std::unique_lock aLock( m_aMutex );
244 if (!m_xPopupMenu)
245 return;
247 sal_Int16 nItemId = 0;
248 for (size_t i=0; i < SAL_N_ELEMENTS(aCommands); ++i)
250 if ( Event.FeatureURL.Complete == aCommands[i] )
252 nItemId = i + 1;
253 break;
257 if (!nItemId)
258 return;
260 m_xPopupMenu->enableItem(nItemId, Event.IsEnabled);
263 // XMenuListener
264 void SAL_CALL ControlMenuController::itemActivated( const css::awt::MenuEvent& )
266 std::unique_lock aLock( m_aMutex );
268 SolarMutexGuard aSolarMutexGuard;
270 // Check if some modes have changed so we have to update our menu images
271 const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
272 bool bShowMenuImages = rSettings.GetUseImagesInMenus();
274 if (bShowMenuImages != m_bShowMenuImages)
276 m_bShowMenuImages = bShowMenuImages;
277 updateImagesPopupMenu(m_xPopupMenu);
281 // XPopupMenuController
282 void SAL_CALL ControlMenuController::updatePopupMenu()
284 std::unique_lock aLock( m_aMutex );
286 throwIfDisposed(aLock);
288 if ( !(m_xFrame.is() && m_xPopupMenu.is()) )
289 return;
291 css::util::URL aTargetURL;
292 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
293 fillPopupMenu( m_xPopupMenu );
294 m_aURLToDispatchMap.free();
296 for (const OUString& aCommand : aCommands)
298 aTargetURL.Complete = aCommand;
299 m_xURLTransformer->parseStrict( aTargetURL );
301 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
302 if ( xDispatch.is() )
304 aLock.unlock(); // the addStatusListener will call back into ::statusChanged
305 xDispatch->addStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
306 xDispatch->removeStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
307 aLock.lock();
308 m_aURLToDispatchMap.emplace( aTargetURL.Complete, xDispatch );
313 // XInitialization
314 void ControlMenuController::initializeImpl( std::unique_lock<std::mutex>& rGuard, const Sequence< Any >& aArguments )
316 svt::PopupMenuControllerBase::initializeImpl(rGuard, aArguments);
317 m_aBaseURL.clear();
320 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
321 com_sun_star_comp_framework_ControlMenuController_get_implementation(
322 css::uno::XComponentContext *context,
323 css::uno::Sequence<css::uno::Any> const &)
325 return cppu::acquire(new ControlMenuController(context));
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */