LibreOffice Module framework (master) 1
toolbarmodemenucontroller.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 <services.h>
22
23#include <com/sun/star/awt/MenuItemStyle.hpp>
24#include <com/sun/star/ui/UIElementType.hpp>
25#include <com/sun/star/frame/XModuleManager.hpp>
26#include <com/sun/star/frame/ModuleManager.hpp>
27
28
30#include <officecfg/Office/Common.hxx>
31#include <vcl/svapp.hxx>
32#include <vcl/EnumContext.hxx>
33#include <rtl/ustrbuf.hxx>
36#include <comphelper/types.hxx>
39
40// Defines
41
42using namespace ::com::sun::star;
43using namespace ::com::sun::star::uno;
44using namespace ::com::sun::star::lang;
45using namespace ::com::sun::star::frame;
46using namespace ::com::sun::star::beans;
47using namespace ::com::sun::star::util;
48using namespace ::com::sun::star::container;
49using namespace ::com::sun::star::ui;
50
51namespace framework
52{
53
54// XInterface, XTypeProvider, XServiceInfo
55
57{
58 return "com.sun.star.comp.framework.ToolbarModeMenuController";
59}
60
61sal_Bool SAL_CALL ToolbarModeMenuController::supportsService( const OUString& sServiceName )
62{
64}
65
66css::uno::Sequence< OUString > SAL_CALL ToolbarModeMenuController::getSupportedServiceNames()
67{
69}
70
71
72ToolbarModeMenuController::ToolbarModeMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
73 svt::PopupMenuControllerBase( xContext ),
74 m_xContext( xContext )
75{
76}
77
79{
80}
81
82void ToolbarModeMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
83{
84 if ( officecfg::Office::Common::Misc::DisableUICustomization::get() )
85 return;
86
87 SolarMutexGuard aSolarMutexGuard;
88 resetPopupMenu( rPopupMenu );
89
90 const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext() );
91 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
93
94 OUStringBuffer aPath("org.openoffice.Office.UI.ToolbarMode/Applications/");
95 switch ( eApp )
96 {
98 aPath.append("Writer");
99 break;
101 aPath.append("Calc");
102 break;
104 aPath.append("Impress");
105 break;
107 aPath.append("Draw");
108 break;
110 aPath.append("Formula");
111 break;
113 aPath.append("Base");
114 break;
115 default:
116 break;
117 }
118 aPath.append("/Modes");
119
120 const utl::OConfigurationTreeRoot aModesNode(
122 aPath.makeStringAndClear(),
123 false);
124 if ( !aModesNode.isValid() )
125 return;
126
127 const Sequence<OUString> aModeNodeNames (aModesNode.getNodeNames());
128 const sal_Int32 nCount(aModeNodeNames.getLength());
129 tools::Long nCountToolbar = 0;
130
131 for ( sal_Int32 nReadIndex = 0; nReadIndex < nCount; ++nReadIndex )
132 {
133 const utl::OConfigurationNode aModeNode(aModesNode.openNode(aModeNodeNames[nReadIndex]));
134 if ( !aModeNode.isValid() )
135 continue;
136
137 OUString aLabel = comphelper::getString( aModeNode.getNodeValue( "Label" ) );
138 OUString aCommandArg = comphelper::getString( aModeNode.getNodeValue( "CommandArg" ) );
139 tools::Long nPosition = comphelper::getINT32( aModeNode.getNodeValue( "MenuPosition" ) );
140 bool isExperimental = comphelper::getBOOL( aModeNode.getNodeValue( "IsExperimental" ) );
141 bool hasNotebookbar = comphelper::getBOOL( aModeNode.getNodeValue( "HasNotebookbar" ) );
142
143 // Allow Notebookbar only in experimental mode
144 if ( isExperimental && !officecfg::Office::Common::Misc::ExperimentalMode::get() )
145 continue;
146 if (!hasNotebookbar)
147 nCountToolbar++;
148
149 m_xPopupMenu->insertItem( nReadIndex+1, aLabel, css::awt::MenuItemStyle::RADIOCHECK, nPosition );
150 rPopupMenu->setCommand( nReadIndex+1, aCommandArg );
151 }
152 rPopupMenu->insertSeparator(nCountToolbar);
153}
154
155// XEventListener
156void SAL_CALL ToolbarModeMenuController::disposing( const EventObject& )
157{
158 Reference< css::awt::XMenuListener > xHolder(this);
159
160 std::unique_lock aLock( m_aMutex );
161 m_xFrame.clear();
162 m_xDispatch.clear();
163
164 if ( m_xPopupMenu.is() )
165 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
166 m_xPopupMenu.clear();
167}
168
169// XStatusListener
170void SAL_CALL ToolbarModeMenuController::statusChanged( const FeatureStateEvent& Event )
171{
172 OUString aFeatureURL( Event.FeatureURL.Complete );
173
174 // All other status events will be processed here
175 std::unique_lock aLock( m_aMutex );
176 Reference< css::awt::XPopupMenu > xPopupMenu( m_xPopupMenu );
177 aLock.unlock();
178
179 if ( !xPopupMenu.is() )
180 return;
181
182 SolarMutexGuard aGuard;
183
184 bool bSetCheckmark = false;
185 bool bCheckmark = false;
186 for (sal_Int16 i = 0, nCount = xPopupMenu->getItemCount(); i < nCount; ++i)
187 {
188 sal_Int16 nId = xPopupMenu->getItemId(i);
189 if ( nId == 0 )
190 continue;
191
192 OUString aCmd = xPopupMenu->getCommand(nId);
193 if ( aCmd == aFeatureURL )
194 {
195 // Enable/disable item
196 xPopupMenu->enableItem(nId, Event.IsEnabled);
197
198 // Checkmark
199 if ( Event.State >>= bCheckmark )
200 bSetCheckmark = true;
201
202 if ( bSetCheckmark )
203 xPopupMenu->checkItem(nId, bCheckmark);
204 else
205 {
206 OUString aItemText;
207
208 if ( Event.State >>= aItemText )
209 xPopupMenu->setItemText(nId, aItemText);
210 }
211 }
212 }
213}
214
215// XMenuListener
216void SAL_CALL ToolbarModeMenuController::itemSelected( const css::awt::MenuEvent& rEvent )
217{
218 auto aArgs(comphelper::InitPropertySequence({{"Mode", Any(m_xPopupMenu->getCommand(rEvent.MenuId))}}));
219 dispatchCommand(m_aCommandURL, aArgs);
220}
221
222void SAL_CALL ToolbarModeMenuController::itemActivated( const css::awt::MenuEvent& )
223{
224 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( m_xContext );
226
227 OUStringBuffer aPath("org.openoffice.Office.UI.ToolbarMode/Applications/");
228 switch ( eApp )
229 {
231 aPath.append("Writer");
232 break;
234 aPath.append("Calc");
235 break;
237 aPath.append("Impress");
238 break;
240 aPath.append("Draw");
241 break;
243 aPath.append("Formula");
244 break;
246 aPath.append("Base");
247 break;
248 default:
249 break;
250 }
251
252 const utl::OConfigurationTreeRoot aModesNode(
254 aPath.makeStringAndClear(),
255 false);
256 if ( !aModesNode.isValid() )
257 return;
258
259 OUString aMode = comphelper::getString( aModesNode.getNodeValue( "Active" ) );
260
261 for ( int i = 0; i < m_xPopupMenu->getItemCount(); ++i )
262 {
263 sal_Int16 nItemId(m_xPopupMenu->getItemId(i));
264 m_xPopupMenu->checkItem(nItemId, aMode == m_xPopupMenu->getCommand(nItemId));
265 }
266}
267
268// XPopupMenuController
269void SAL_CALL ToolbarModeMenuController::setPopupMenu( const Reference< css::awt::XPopupMenu >& xPopupMenu )
270{
271 std::unique_lock aLock( m_aMutex );
272
273 throwIfDisposed(aLock);
274
275 if ( m_xFrame.is() && !m_xPopupMenu.is() )
276 {
277 // Create popup menu on demand
278 SolarMutexGuard aSolarMutexGuard;
279
280 m_xPopupMenu = dynamic_cast<VCLXPopupMenu*>(xPopupMenu.get());
281 assert(bool(xPopupMenu) == bool(m_xPopupMenu) && "we only support VCLXPopupMenu");
282 m_xPopupMenu->addMenuListener( Reference< css::awt::XMenuListener >(this) );
283 fillPopupMenu( m_xPopupMenu );
284 }
285}
286
287}
288
289extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
291 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
292{
293 return cppu::acquire(new framework::ToolbarModeMenuController(context));
294}
295
296/* 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
virtual void SAL_CALL setPopupMenu(const css::uno::Reference< css::awt::XPopupMenu > &PopupMenu) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
ToolbarModeMenuController(const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void SAL_CALL itemActivated(const css::awt::MenuEvent &rEvent) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL itemSelected(const css::awt::MenuEvent &rEvent) override
void fillPopupMenu(css::uno::Reference< css::awt::XPopupMenu > const &rPopupMenu)
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
css::uno::Sequence< OUString > getNodeNames() const noexcept
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
OConfigurationNode openNode(const OUString &_rPath) const noexcept
static Application GetApplicationEnum(const OUString &rsApplicationName)
int nCount
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool getBOOL(const Any &_rAny)
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)
sal_Int32 getINT32(const Any &_rAny)
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
OUString getString(const Any &_rAny)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral SERVICENAME_POPUPMENUCONTROLLER
Definition: services.h:33
int i
long Long
sal_Int16 nId
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_ToolbarModeMenuController_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool
std::mutex m_aMutex
OUString aLabel