LibreOffice Module framework (master) 1
controlmenucontroller.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
20#include <sal/config.h>
21
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>
27
29#include <vcl/graph.hxx>
30#include <vcl/svapp.hxx>
31#include <vcl/settings.hxx>
32#include <vcl/image.hxx>
35#include <osl/mutex.hxx>
36#include <unordered_map>
37
38#include <classes/fwkresid.hxx>
39#include <bitmaps.hlst>
40#include <strings.hrc>
41
42static const char* aCommands[] =
43{
44 ".uno:ConvertToEdit",
45 ".uno:ConvertToButton",
46 ".uno:ConvertToFixed",
47 ".uno:ConvertToList",
48 ".uno:ConvertToCheckBox",
49 ".uno:ConvertToRadio",
50 ".uno:ConvertToGroup",
51 ".uno:ConvertToCombo",
52 ".uno:ConvertToImageBtn",
53 ".uno:ConvertToFileControl",
54 ".uno:ConvertToDate",
55 ".uno:ConvertToTime",
56 ".uno:ConvertToNumeric",
57 ".uno:ConvertToCurrency",
58 ".uno:ConvertToPattern",
59 ".uno:ConvertToImageControl",
60 ".uno:ConvertToFormatted",
61 ".uno:ConvertToScrollBar",
62 ".uno:ConvertToSpinButton",
63 ".uno:ConvertToNavigationBar"
64};
65
67{
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
88};
89
90const rtl::OUStringConstExpr aImgIds[]
91{
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
112};
113
114using namespace css;
115using namespace css::uno;
116using namespace css::lang;
117using namespace css::frame;
118using namespace css::beans;
119
120namespace {
121
122class ControlMenuController : public svt::PopupMenuControllerBase
123{
124 using svt::PopupMenuControllerBase::disposing;
125
126public:
127 explicit ControlMenuController( const uno::Reference< uno::XComponentContext >& xContext );
128
129 // XServiceInfo
130 virtual OUString SAL_CALL getImplementationName() override
131 {
132 return "com.sun.star.comp.framework.ControlMenuController";
133 }
134
135 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
136 {
137 return cppu::supportsService(this, ServiceName);
138 }
139
140 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
141 {
142 return {"com.sun.star.frame.PopupMenuController"};
143 }
144
145 // XPopupMenuController
146 virtual void SAL_CALL updatePopupMenu() override;
147
148 // XStatusListener
149 virtual void SAL_CALL statusChanged( const frame::FeatureStateEvent& Event ) override;
150
151 // XMenuListener
152 virtual void SAL_CALL itemActivated( const awt::MenuEvent& rEvent ) override;
153
154 // XEventListener
155 virtual void SAL_CALL disposing( const lang::EventObject& Source ) override;
156
157private:
158 // XInitialization
159 virtual void initializeImpl( std::unique_lock<std::mutex>& rGuard, const uno::Sequence< uno::Any >& aArguments ) override;
160
161 class UrlToDispatchMap : public std::unordered_map< OUString,
162 uno::Reference< frame::XDispatch > >
163 {
164 public:
165 void free()
166 {
167 UrlToDispatchMap().swap( *this );// get rid of reserved capacity
168 }
169 };
170
171 void updateImagesPopupMenu(Reference<awt::XPopupMenu> const& rPopupMenu);
172 void fillPopupMenu(uno::Reference<awt::XPopupMenu> const& rPopupMenu);
173
174 bool m_bShowMenuImages : 1;
175 UrlToDispatchMap m_aURLToDispatchMap;
176};
177
178ControlMenuController::ControlMenuController(const css::uno::Reference< css::uno::XComponentContext >& xContext)
179 : svt::PopupMenuControllerBase(xContext)
180{
182 m_bShowMenuImages = rSettings.GetUseImagesInMenus();
183
184}
185
186// private function
187void ControlMenuController::updateImagesPopupMenu(Reference<awt::XPopupMenu> const& rPopupMenu)
188{
189 if (!rPopupMenu)
190 return;
191 for (size_t i=0; i < SAL_N_ELEMENTS(aCommands); ++i)
192 {
193 sal_Int16 nItemId = i + 1;
194 if (m_bShowMenuImages)
195 {
196 Image aImage(StockImage::Yes, OUString(aImgIds[i]));
197 Graphic aGraphic(aImage);
198 rPopupMenu->setItemImage(nItemId, aGraphic.GetXGraphic(), false);
199 }
200 else
201 rPopupMenu->setItemImage(nItemId, nullptr, false);
202 }
203}
204
205// private function
206void ControlMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
207{
208 resetPopupMenu( rPopupMenu );
209
210 for (size_t i=0; i < SAL_N_ELEMENTS(aCommands); ++i)
211 {
212 sal_Int16 nItemId = i + 1;
213 OUString sCommand(OUString::createFromAscii(aCommands[i]));
214 rPopupMenu->insertItem(nItemId, FwkResId(aLabels[i]), 0, i);
215 rPopupMenu->setCommand(nItemId, sCommand);
216 rPopupMenu->enableItem(nItemId, false);
217 }
218
219 updateImagesPopupMenu(rPopupMenu);
220
221 rPopupMenu->hideDisabledEntries(true);
222}
223
224}
225
226// XEventListener
227void SAL_CALL ControlMenuController::disposing( const EventObject& )
228{
229 Reference< css::awt::XMenuListener > xHolder(this);
230
231 std::unique_lock aLock( m_aMutex );
232 m_xFrame.clear();
233 m_xDispatch.clear();
234
235 if ( m_xPopupMenu.is() )
236 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
237 m_xPopupMenu.clear();
238}
239
240// XStatusListener
241void SAL_CALL ControlMenuController::statusChanged( const FeatureStateEvent& Event )
242{
243 std::unique_lock aLock( m_aMutex );
244
245 if (!m_xPopupMenu)
246 return;
247
248 sal_Int16 nItemId = 0;
249 for (size_t i=0; i < SAL_N_ELEMENTS(aCommands); ++i)
250 {
251 if ( Event.FeatureURL.Complete.equalsAscii( aCommands[i] ))
252 {
253 nItemId = i + 1;
254 break;
255 }
256 }
257
258 if (!nItemId)
259 return;
260
261 m_xPopupMenu->enableItem(nItemId, Event.IsEnabled);
262}
263
264// XMenuListener
265void SAL_CALL ControlMenuController::itemActivated( const css::awt::MenuEvent& )
266{
267 std::unique_lock aLock( m_aMutex );
268
269 SolarMutexGuard aSolarMutexGuard;
270
271 // Check if some modes have changed so we have to update our menu images
273 bool bShowMenuImages = rSettings.GetUseImagesInMenus();
274
275 if (bShowMenuImages != m_bShowMenuImages)
276 {
277 m_bShowMenuImages = bShowMenuImages;
278 updateImagesPopupMenu(m_xPopupMenu);
279 }
280}
281
282// XPopupMenuController
283void SAL_CALL ControlMenuController::updatePopupMenu()
284{
285 std::unique_lock aLock( m_aMutex );
286
287 throwIfDisposed(aLock);
288
289 if ( !(m_xFrame.is() && m_xPopupMenu.is()) )
290 return;
291
292 css::util::URL aTargetURL;
293 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
294 fillPopupMenu( m_xPopupMenu );
295 m_aURLToDispatchMap.free();
296
297 for (const char* aCommand : aCommands)
298 {
299 aTargetURL.Complete = OUString::createFromAscii( aCommand );
300 m_xURLTransformer->parseStrict( aTargetURL );
301
302 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
303 if ( xDispatch.is() )
304 {
305 aLock.unlock(); // the addStatusListener will call back into ::statusChanged
306 xDispatch->addStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
307 xDispatch->removeStatusListener( static_cast< XStatusListener* >(this), aTargetURL );
308 aLock.lock();
309 m_aURLToDispatchMap.emplace( aTargetURL.Complete, xDispatch );
310 }
311 }
312}
313
314// XInitialization
315void ControlMenuController::initializeImpl( std::unique_lock<std::mutex>& rGuard, const Sequence< Any >& aArguments )
316{
317 svt::PopupMenuControllerBase::initializeImpl(rGuard, aArguments);
318 m_aBaseURL.clear();
319}
320
321extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
323 css::uno::XComponentContext *context,
324 css::uno::Sequence<css::uno::Any> const &)
325{
326 return cppu::acquire(new ControlMenuController(context));
327}
328
329/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::frame::XDispatch > m_xDispatch
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
bool GetUseImagesInMenus() const
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_framework_ControlMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
const rtl::OUStringConstExpr aImgIds[]
static TranslateId aLabels[]
static const char * aCommands[]
Reference< XDispatch > xDispatch
OUString FwkResId(TranslateId aId)
Definition: fwkresid.cxx:22
void SAL_CALL itemActivated(const css::awt::MenuEvent &rEvent) override
#define SAL_N_ELEMENTS(arr)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
OUString aCommand
unsigned char sal_Bool
std::mutex m_aMutex
OUString aTargetURL