Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / toolbarmodemenucontroller.cxx
blob62cfdc04dd413d6e7e84bd6221bc0770069a6366
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 <uielement/toolbarmodemenucontroller.hxx>
21 #include <services.h>
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>
29 #include <toolkit/awt/vclxmenu.hxx>
30 #include <officecfg/Office/Common.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/EnumContext.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <comphelper/processfactory.hxx>
35 #include <comphelper/propertysequence.hxx>
36 #include <comphelper/types.hxx>
37 #include <unotools/confignode.hxx>
38 #include <cppuhelper/supportsservice.hxx>
40 // Defines
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::frame;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::util;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::ui;
51 namespace framework
54 // XInterface, XTypeProvider, XServiceInfo
56 OUString SAL_CALL ToolbarModeMenuController::getImplementationName()
58 return "com.sun.star.comp.framework.ToolbarModeMenuController";
61 sal_Bool SAL_CALL ToolbarModeMenuController::supportsService( const OUString& sServiceName )
63 return cppu::supportsService(this, sServiceName);
66 css::uno::Sequence< OUString > SAL_CALL ToolbarModeMenuController::getSupportedServiceNames()
68 return { SERVICENAME_POPUPMENUCONTROLLER };
72 ToolbarModeMenuController::ToolbarModeMenuController( const css::uno::Reference< css::uno::XComponentContext >& xContext ) :
73 svt::PopupMenuControllerBase( xContext ),
74 m_xContext( xContext )
78 ToolbarModeMenuController::~ToolbarModeMenuController()
82 void ToolbarModeMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu > const & rPopupMenu )
84 if ( officecfg::Office::Common::Misc::DisableUICustomization::get() )
85 return;
87 SolarMutexGuard aSolarMutexGuard;
88 resetPopupMenu( rPopupMenu );
90 const Reference<XComponentContext> xContext (::comphelper::getProcessComponentContext() );
91 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( xContext );
92 vcl::EnumContext::Application eApp = vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(m_xFrame));
94 OUStringBuffer aPath("org.openoffice.Office.UI.ToolbarMode/Applications/");
95 switch ( eApp )
97 case vcl::EnumContext::Application::Writer:
98 aPath.append("Writer");
99 break;
100 case vcl::EnumContext::Application::Calc:
101 aPath.append("Calc");
102 break;
103 case vcl::EnumContext::Application::Impress:
104 aPath.append("Impress");
105 break;
106 case vcl::EnumContext::Application::Draw:
107 aPath.append("Draw");
108 break;
109 case vcl::EnumContext::Application::Formula:
110 aPath.append("Formula");
111 break;
112 case vcl::EnumContext::Application::Base:
113 aPath.append("Base");
114 break;
115 default:
116 break;
118 aPath.append("/Modes");
120 const utl::OConfigurationTreeRoot aModesNode(
121 m_xContext,
122 aPath.makeStringAndClear(),
123 false);
124 if ( !aModesNode.isValid() )
125 return;
127 const Sequence<OUString> aModeNodeNames (aModesNode.getNodeNames());
128 const sal_Int32 nCount(aModeNodeNames.getLength());
129 tools::Long nCountToolbar = 0;
131 for ( sal_Int32 nReadIndex = 0; nReadIndex < nCount; ++nReadIndex )
133 const utl::OConfigurationNode aModeNode(aModesNode.openNode(aModeNodeNames[nReadIndex]));
134 if ( !aModeNode.isValid() )
135 continue;
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" ) );
143 // Allow Notebookbar only in experimental mode
144 if ( isExperimental && !officecfg::Office::Common::Misc::ExperimentalMode::get() )
145 continue;
146 if (!hasNotebookbar)
147 nCountToolbar++;
149 m_xPopupMenu->insertItem( nReadIndex+1, aLabel, css::awt::MenuItemStyle::RADIOCHECK, nPosition );
150 rPopupMenu->setCommand( nReadIndex+1, aCommandArg );
152 rPopupMenu->insertSeparator(nCountToolbar);
155 // XEventListener
156 void SAL_CALL ToolbarModeMenuController::disposing( const EventObject& )
158 Reference< css::awt::XMenuListener > xHolder(this);
160 std::unique_lock aLock( m_aMutex );
161 m_xFrame.clear();
162 m_xDispatch.clear();
164 if ( m_xPopupMenu.is() )
165 m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(this) );
166 m_xPopupMenu.clear();
169 // XStatusListener
170 void SAL_CALL ToolbarModeMenuController::statusChanged( const FeatureStateEvent& Event )
172 OUString aFeatureURL( Event.FeatureURL.Complete );
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();
179 if ( !xPopupMenu.is() )
180 return;
182 SolarMutexGuard aGuard;
184 bool bSetCheckmark = false;
185 bool bCheckmark = false;
186 for (sal_Int16 i = 0, nCount = xPopupMenu->getItemCount(); i < nCount; ++i)
188 sal_Int16 nId = xPopupMenu->getItemId(i);
189 if ( nId == 0 )
190 continue;
192 OUString aCmd = xPopupMenu->getCommand(nId);
193 if ( aCmd == aFeatureURL )
195 // Enable/disable item
196 xPopupMenu->enableItem(nId, Event.IsEnabled);
198 // Checkmark
199 if ( Event.State >>= bCheckmark )
200 bSetCheckmark = true;
202 if ( bSetCheckmark )
203 xPopupMenu->checkItem(nId, bCheckmark);
204 else
206 OUString aItemText;
208 if ( Event.State >>= aItemText )
209 xPopupMenu->setItemText(nId, aItemText);
215 // XMenuListener
216 void SAL_CALL ToolbarModeMenuController::itemSelected( const css::awt::MenuEvent& rEvent )
218 auto aArgs(comphelper::InitPropertySequence({{"Mode", Any(m_xPopupMenu->getCommand(rEvent.MenuId))}}));
219 dispatchCommand(m_aCommandURL, aArgs);
222 void SAL_CALL ToolbarModeMenuController::itemActivated( const css::awt::MenuEvent& )
224 const Reference<frame::XModuleManager> xModuleManager = frame::ModuleManager::create( m_xContext );
225 vcl::EnumContext::Application eApp = vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(m_xFrame));
227 OUStringBuffer aPath("org.openoffice.Office.UI.ToolbarMode/Applications/");
228 switch ( eApp )
230 case vcl::EnumContext::Application::Writer:
231 aPath.append("Writer");
232 break;
233 case vcl::EnumContext::Application::Calc:
234 aPath.append("Calc");
235 break;
236 case vcl::EnumContext::Application::Impress:
237 aPath.append("Impress");
238 break;
239 case vcl::EnumContext::Application::Draw:
240 aPath.append("Draw");
241 break;
242 case vcl::EnumContext::Application::Formula:
243 aPath.append("Formula");
244 break;
245 case vcl::EnumContext::Application::Base:
246 aPath.append("Base");
247 break;
248 default:
249 break;
252 const utl::OConfigurationTreeRoot aModesNode(
253 m_xContext,
254 aPath.makeStringAndClear(),
255 false);
256 if ( !aModesNode.isValid() )
257 return;
259 OUString aMode = comphelper::getString( aModesNode.getNodeValue( "Active" ) );
261 for ( int i = 0; i < m_xPopupMenu->getItemCount(); ++i )
263 sal_Int16 nItemId(m_xPopupMenu->getItemId(i));
264 m_xPopupMenu->checkItem(nItemId, aMode == m_xPopupMenu->getCommand(nItemId));
268 // XPopupMenuController
269 void SAL_CALL ToolbarModeMenuController::setPopupMenu( const Reference< css::awt::XPopupMenu >& xPopupMenu )
271 std::unique_lock aLock( m_aMutex );
273 throwIfDisposed(aLock);
275 if ( m_xFrame.is() && !m_xPopupMenu.is() )
277 // Create popup menu on demand
278 SolarMutexGuard aSolarMutexGuard;
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 );
289 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
290 framework_ToolbarModeMenuController_get_implementation(
291 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
293 return cppu::acquire(new framework::ToolbarModeMenuController(context));
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */