Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / uielement / complextoolbarcontroller.cxx
blobc3f9f191df0e2fbe7b30bd4664538a1dba2bbfa0
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/complextoolbarcontroller.hxx>
22 #include <com/sun/star/util/URLTransformer.hpp>
23 #include <com/sun/star/util/XURLTransformer.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <com/sun/star/frame/status/ItemStatus.hpp>
27 #include <com/sun/star/frame/status/Visibility.hpp>
28 #include <com/sun/star/frame/XControlNotificationListener.hpp>
29 #include <com/sun/star/frame/XFrame.hpp>
31 #include <comphelper/propertyvalue.hxx>
32 #include <svtools/toolboxcontroller.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/mnemonic.hxx>
35 #include <vcl/toolbox.hxx>
37 using namespace ::com::sun::star;
38 using namespace css::awt;
39 using namespace css::uno;
40 using namespace css::beans;
41 using namespace css::lang;
42 using namespace css::frame;
43 using namespace css::frame::status;
44 using namespace css::util;
46 namespace framework
49 ComplexToolbarController::ComplexToolbarController(
50 const Reference< XComponentContext >& rxContext,
51 const Reference< XFrame >& rFrame,
52 ToolBox* pToolbar,
53 ToolBoxItemId nID,
54 const OUString& aCommand ) :
55 svt::ToolboxController( rxContext, rFrame, aCommand )
56 , m_xToolbar( pToolbar )
57 , m_nID( nID )
58 , m_bMadeInvisible( false )
60 m_xURLTransformer.set( URLTransformer::create(m_xContext) );
63 ComplexToolbarController::~ComplexToolbarController()
67 void SAL_CALL ComplexToolbarController::dispose()
69 SolarMutexGuard aSolarMutexGuard;
71 m_xToolbar->SetItemWindow( m_nID, nullptr );
72 svt::ToolboxController::dispose();
74 m_xURLTransformer.clear();
75 m_xToolbar.clear();
76 m_nID = ToolBoxItemId(0);
79 Sequence<PropertyValue> ComplexToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
81 // Add key modifier to argument list
82 Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue("KeyModifier", KeyModifier) };
83 return aArgs;
86 void SAL_CALL ComplexToolbarController::execute( sal_Int16 KeyModifier )
88 Reference< XDispatch > xDispatch;
89 Reference< XURLTransformer > xURLTransformer;
90 css::util::URL aTargetURL;
91 Sequence<PropertyValue> aArgs;
94 SolarMutexGuard aSolarMutexGuard;
96 if ( m_bDisposed )
97 throw DisposedException();
99 if ( m_bInitialized &&
100 m_xFrame.is() &&
101 !m_aCommandURL.isEmpty() )
103 xURLTransformer = m_xURLTransformer;
104 xDispatch = getDispatchFromCommand( m_aCommandURL );
105 aTargetURL = getInitializedURL();
106 aArgs = getExecuteArgs(KeyModifier);
110 if ( xDispatch.is() && !aTargetURL.Complete.isEmpty() )
112 // Execute dispatch asynchronously
113 ExecuteInfo* pExecuteInfo = new ExecuteInfo;
114 pExecuteInfo->xDispatch = xDispatch;
115 pExecuteInfo->aTargetURL = aTargetURL;
116 pExecuteInfo->aArgs = aArgs;
117 Application::PostUserEvent( LINK(nullptr, ComplexToolbarController , ExecuteHdl_Impl), pExecuteInfo );
121 void ComplexToolbarController::statusChanged( const FeatureStateEvent& Event )
123 SolarMutexGuard aSolarMutexGuard;
125 if ( m_bDisposed )
126 return;
128 if ( !m_xToolbar )
129 return;
131 m_xToolbar->EnableItem( m_nID, Event.IsEnabled );
133 ToolBoxItemBits nItemBits = m_xToolbar->GetItemBits( m_nID );
134 nItemBits &= ~ToolBoxItemBits::CHECKABLE;
135 TriState eTri = TRISTATE_FALSE;
137 bool bValue;
138 OUString aStrValue;
139 ItemStatus aItemState;
140 Visibility aItemVisibility;
141 ControlCommand aControlCommand;
143 if ( Event.State >>= bValue )
145 // Boolean, treat it as checked/unchecked
146 if ( m_bMadeInvisible )
147 m_xToolbar->ShowItem( m_nID );
148 m_xToolbar->CheckItem( m_nID, bValue );
149 if ( bValue )
150 eTri = TRISTATE_TRUE;
151 nItemBits |= ToolBoxItemBits::CHECKABLE;
153 else if ( Event.State >>= aStrValue )
155 OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
156 m_xToolbar->SetItemText( m_nID, aText );
157 m_xToolbar->SetQuickHelpText( m_nID, aText );
159 if ( m_bMadeInvisible )
160 m_xToolbar->ShowItem( m_nID );
162 else if ( Event.State >>= aItemState )
164 eTri = TRISTATE_INDET;
165 nItemBits |= ToolBoxItemBits::CHECKABLE;
166 if ( m_bMadeInvisible )
167 m_xToolbar->ShowItem( m_nID );
169 else if ( Event.State >>= aItemVisibility )
171 m_xToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
172 m_bMadeInvisible = !aItemVisibility.bVisible;
174 else if ( Event.State >>= aControlCommand )
176 if (aControlCommand.Command == "SetQuickHelpText")
178 for (NamedValue const & rArg : std::as_const(aControlCommand.Arguments))
180 if (rArg.Name == "HelpText")
182 OUString aHelpText;
183 rArg.Value >>= aHelpText;
184 m_xToolbar->SetQuickHelpText(m_nID, aHelpText);
185 break;
189 else
191 executeControlCommand( aControlCommand );
193 if ( m_bMadeInvisible )
194 m_xToolbar->ShowItem( m_nID );
197 else if ( m_bMadeInvisible )
198 m_xToolbar->ShowItem( m_nID );
200 m_xToolbar->SetItemState( m_nID, eTri );
201 m_xToolbar->SetItemBits( m_nID, nItemBits );
204 IMPL_STATIC_LINK( ComplexToolbarController, ExecuteHdl_Impl, void*, p, void )
206 ExecuteInfo* pExecuteInfo = static_cast<ExecuteInfo*>(p);
207 SolarMutexReleaser aReleaser;
210 // Asynchronous execution as this can lead to our own destruction!
211 // Framework can recycle our current frame and the layout manager disposes all user interface
212 // elements if a component gets detached from its frame!
213 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
215 catch ( const Exception& )
219 delete pExecuteInfo;
222 IMPL_STATIC_LINK( ComplexToolbarController, Notify_Impl, void*, p, void )
224 NotifyInfo* pNotifyInfo = static_cast<NotifyInfo*>(p);
225 SolarMutexReleaser aReleaser;
228 // Asynchronous execution: As this can lead to our own destruction!
229 // Framework can recycle our current frame and the layout manager disposes all user interface
230 // elements if a component gets detached from its frame!
231 frame::ControlEvent aEvent;
232 aEvent.aURL = pNotifyInfo->aSourceURL;
233 aEvent.Event = pNotifyInfo->aEventName;
234 aEvent.aInformation = pNotifyInfo->aInfoSeq;
235 pNotifyInfo->xNotifyListener->controlEvent( aEvent );
237 catch ( const Exception& )
241 delete pNotifyInfo;
244 void ComplexToolbarController::addNotifyInfo(
245 const OUString& aEventName,
246 const uno::Reference< frame::XDispatch >& xDispatch,
247 const uno::Sequence< beans::NamedValue >& rInfo )
249 uno::Reference< frame::XControlNotificationListener > xControlNotify( xDispatch, uno::UNO_QUERY );
251 if ( !xControlNotify.is() )
252 return;
254 // Execute notification asynchronously
255 NotifyInfo* pNotifyInfo = new NotifyInfo;
257 pNotifyInfo->aEventName = aEventName;
258 pNotifyInfo->xNotifyListener = xControlNotify;
259 pNotifyInfo->aSourceURL = getInitializedURL();
261 // Add frame as source to the information sequence
262 sal_Int32 nCount = rInfo.getLength();
263 uno::Sequence< beans::NamedValue > aInfoSeq( rInfo );
264 aInfoSeq.realloc( nCount+1 );
265 auto pInfoSeq = aInfoSeq.getArray();
266 pInfoSeq[nCount].Name = "Source";
267 pInfoSeq[nCount].Value <<= getFrameInterface();
268 pNotifyInfo->aInfoSeq = aInfoSeq;
270 Application::PostUserEvent( LINK(nullptr, ComplexToolbarController, Notify_Impl), pNotifyInfo );
273 uno::Reference< frame::XDispatch > ComplexToolbarController::getDispatchFromCommand( const OUString& aCommand ) const
275 uno::Reference< frame::XDispatch > xDispatch;
277 if ( m_bInitialized && m_xFrame.is() && !aCommand.isEmpty() )
279 URLToDispatchMap::const_iterator pIter = m_aListenerMap.find( aCommand );
280 if ( pIter != m_aListenerMap.end() )
281 xDispatch = pIter->second;
284 return xDispatch;
287 const css::util::URL& ComplexToolbarController::getInitializedURL()
289 if ( m_aURL.Complete.isEmpty() )
291 m_aURL.Complete = m_aCommandURL;
292 m_xURLTransformer->parseStrict( m_aURL );
294 return m_aURL;
297 void ComplexToolbarController::notifyFocusGet()
299 // send focus get notification
300 uno::Sequence< beans::NamedValue > aInfo;
301 addNotifyInfo( "FocusSet",
302 getDispatchFromCommand( m_aCommandURL ),
303 aInfo );
306 void ComplexToolbarController::notifyFocusLost()
308 // send focus lost notification
309 uno::Sequence< beans::NamedValue > aInfo;
310 addNotifyInfo( "FocusLost",
311 getDispatchFromCommand( m_aCommandURL ),
312 aInfo );
315 void ComplexToolbarController::notifyTextChanged( const OUString& aText )
317 // send text changed notification
318 uno::Sequence< beans::NamedValue > aInfo { { "Text", css::uno::Any(aText) } };
319 addNotifyInfo( "TextChanged",
320 getDispatchFromCommand( m_aCommandURL ),
321 aInfo );
324 } // namespace
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */