cid#1640468 Dereference after null check
[LibreOffice.git] / framework / source / uielement / toolbarwrapper.cxx
blob09381a79ead2e1776516bfff371546e29f6b4659
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/toolbarwrapper.hxx>
21 #include <uielement/toolbarmanager.hxx>
23 #include <com/sun/star/ui/ContextChangeEventMultiplexer.hpp>
24 #include <com/sun/star/ui/UIElementType.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <toolkit/helper/vclunohelper.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/toolbox.hxx>
31 #include <vcl/weldutils.hxx>
32 #include <vcl/wintypes.hxx>
34 using namespace com::sun::star;
35 using namespace com::sun::star::uno;
36 using namespace com::sun::star::beans;
37 using namespace com::sun::star::frame;
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::container;
40 using namespace com::sun::star::awt;
41 using namespace ::com::sun::star::ui;
43 namespace framework
46 ToolBarWrapper::ToolBarWrapper( const Reference< XComponentContext >& rxContext ) :
47 ImplInheritanceHelper( UIElementType::TOOLBAR ),
48 m_xContext( rxContext )
52 ToolBarWrapper::~ToolBarWrapper()
54 m_xWeldedToolbar.reset(nullptr);
55 m_xTopLevel.reset(nullptr);
56 m_xBuilder.reset(nullptr);
59 // XComponent
60 void SAL_CALL ToolBarWrapper::dispose()
62 Reference< XComponent > xThis(this);
65 SolarMutexGuard g;
66 if ( m_bDisposed )
67 return;
70 css::lang::EventObject aEvent( xThis );
71 m_aListenerContainer.disposeAndClear( aEvent );
73 SolarMutexGuard g;
75 auto xMultiplexer( ContextChangeEventMultiplexer::get( m_xContext ) );
76 xMultiplexer->removeAllContextChangeEventListeners( this );
78 Reference< XComponent > xComponent( m_xSubElement, UNO_QUERY );
79 if ( xComponent.is() )
80 xComponent->removeEventListener( Reference< XUIConfigurationListener >( this ));
81 m_xSubElement.clear();
83 if ( m_xToolBarManager.is() )
84 m_xToolBarManager->dispose();
85 m_xToolBarManager.clear();
86 m_xConfigSource.clear();
87 m_xConfigData.clear();
89 m_bDisposed = true;
92 // XInitialization
93 void SAL_CALL ToolBarWrapper::initialize( const Sequence< Any >& aArguments )
95 SolarMutexGuard g;
97 if ( m_bDisposed )
98 throw DisposedException();
100 if ( m_bInitialized )
101 return;
103 UIConfigElementWrapperBase::initialize( aArguments );
105 bool bPopupMode( false );
106 Reference< XWindow > xParentWindow;
107 for ( Any const & arg : aArguments )
109 PropertyValue aPropValue;
110 if ( arg >>= aPropValue )
112 if ( aPropValue.Name == "PopupMode" )
113 aPropValue.Value >>= bPopupMode;
114 else if ( aPropValue.Name == "ParentWindow" )
115 xParentWindow.set( aPropValue.Value, UNO_QUERY );
119 Reference< XFrame > xFrame( m_xWeakFrame );
120 if ( !(xFrame.is() && m_xConfigSource.is()) )
121 return;
123 std::u16string_view aContextPart;
124 if ( m_aResourceURL.startsWith( "private:resource/toolbar/singlemode", &aContextPart ) && aContextPart.empty() )
126 auto xMultiplexer( ContextChangeEventMultiplexer::get( m_xContext ) );
129 xMultiplexer->addContextChangeEventListener( this, xFrame->getController() );
131 catch( const Exception& )
134 // Avoid flickering on context change
135 bPopupMode = true;
138 // Create VCL based toolbar which will be filled with settings data
139 VclPtr<ToolBox> pToolBar;
140 rtl::Reference<ToolBarManager> pToolBarManager;
141 if ( aContextPart.empty() )
143 SolarMutexGuard aSolarMutexGuard;
144 if ( !xParentWindow.is() )
145 xParentWindow.set( xFrame->getContainerWindow() );
146 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xParentWindow );
147 if ( pWindow )
149 WinBits nStyles = WB_BORDER | WB_SCROLL | WB_MOVEABLE | WB_3DLOOK | WB_DOCKABLE | WB_SIZEABLE | WB_CLOSEABLE;
151 pToolBar = VclPtr<ToolBox>::Create( pWindow, nStyles );
152 pToolBar->SetLineSpacing(true);
153 pToolBarManager = new ToolBarManager( m_xContext, xFrame, m_aResourceURL, pToolBar );
154 m_xToolBarManager = pToolBarManager;
155 pToolBar->WillUsePopupMode( bPopupMode );
157 else if (weld::TransportAsXWindow* pTunnel = dynamic_cast<weld::TransportAsXWindow*>(xParentWindow.get()))
159 m_xBuilder = Application::CreateBuilder(pTunnel->getWidget(), u"svt/ui/managedtoolbar.ui"_ustr);
160 m_xTopLevel = m_xBuilder->weld_container(u"toolbarcontainer"_ustr);
161 m_xWeldedToolbar = m_xBuilder->weld_toolbar(u"managedtoolbar"_ustr);
162 if ( m_xWeldedToolbar )
164 pToolBarManager = new ToolBarManager( m_xContext, xFrame, m_aResourceURL, m_xWeldedToolbar.get(), m_xBuilder.get() );
165 m_xToolBarManager = pToolBarManager;
172 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, false );
173 if ( m_xConfigData.is() && (pToolBar || m_xWeldedToolbar) && pToolBarManager )
175 // Fill toolbar with container contents
176 impl_fillNewData();
177 if (pToolBar)
179 pToolBar->EnableCustomize();
180 ::Size aActSize( pToolBar->GetSizePixel() );
181 ::Size aSize( pToolBar->CalcWindowSizePixel() );
182 aSize.setWidth( aActSize.Width() );
183 pToolBar->SetOutputSizePixel( aSize );
187 catch ( const NoSuchElementException& )
189 // No settings in our configuration manager. This means we are
190 // a transient toolbar which has no persistent settings.
191 m_bPersistent = false;
192 if ( pToolBar && pToolBarManager )
194 pToolBar->EnableCustomize();
195 ::Size aActSize( pToolBar->GetSizePixel() );
196 ::Size aSize( pToolBar->CalcWindowSizePixel() );
197 aSize.setWidth( aActSize.Width() );
198 pToolBar->SetOutputSizePixel( aSize );
203 // XEventListener
204 void SAL_CALL ToolBarWrapper::disposing( const css::lang::EventObject& aEvent )
206 if ( aEvent.Source == m_xSubElement )
207 m_xSubElement.clear();
210 // XUpdatable
211 void SAL_CALL ToolBarWrapper::update()
213 SolarMutexGuard g;
215 if ( m_bDisposed )
216 throw DisposedException();
218 if ( m_xToolBarManager )
219 m_xToolBarManager->CheckAndUpdateImages();
222 // XUIElementSettings
223 void SAL_CALL ToolBarWrapper::updateSettings()
225 SolarMutexGuard g;
227 if ( m_bDisposed )
228 throw DisposedException();
230 if ( m_xConfigSource.is() && m_bPersistent )
234 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, false );
235 if ( m_xConfigData.is() )
236 impl_fillNewData();
238 catch ( const NoSuchElementException& )
242 auto pContainer( m_aListenerContainer.getContainer( cppu::UnoType< XEventListener >::get() ) );
243 if ( pContainer )
244 pContainer->forEach< XUIElementSettings >([]( const Reference<XUIElementSettings>& xListener ){ xListener->updateSettings(); });
246 else if ( !m_bPersistent )
248 // Transient toolbar: do nothing
252 void ToolBarWrapper::impl_fillNewData()
254 if ( m_xToolBarManager )
256 Reference< XUIElementSettings > xUIElementSettings( m_xSubElement, UNO_QUERY );
257 Reference< XIndexAccess > xContextData = xUIElementSettings.is() ? xUIElementSettings->getSettings( false ) : nullptr;
258 OUString aContextToolbar = xContextData.is() ? m_xSubElement->getResourceURL() : OUString();
259 m_xToolBarManager->FillToolbar( m_xConfigData, xContextData, aContextToolbar );
263 //XContextChangeEventListener
264 void SAL_CALL ToolBarWrapper::notifyContextChangeEvent( const ContextChangeEventObject& aEvent )
266 SolarMutexGuard g;
268 if ( m_bDisposed )
269 throw DisposedException();
271 if ( aEvent.ContextName.isEmpty() || aEvent.ContextName == "default" )
272 return;
274 const OUString aContextToolbar( m_aResourceURL + "-" + aEvent.ContextName.toAsciiLowerCase() );
275 if ( m_xSubElement.is() && m_xSubElement->getResourceURL() == aContextToolbar )
276 return;
278 Reference< XComponent > xComponent( m_xSubElement, UNO_QUERY );
279 if ( xComponent.is() )
280 xComponent->removeEventListener( Reference< XUIConfigurationListener >( this ));
281 m_xSubElement.clear();
283 Reference< XLayoutManager > xLayoutManager;
284 Reference< XPropertySet > xPropSet( m_xWeakFrame.get(), UNO_QUERY );
285 if ( xPropSet.is() )
286 xPropSet->getPropertyValue(u"LayoutManager"_ustr) >>= xLayoutManager;
287 if ( !xLayoutManager.is() )
288 return;
290 xLayoutManager->createElement( aContextToolbar );
291 m_xSubElement.set( xLayoutManager->getElement( aContextToolbar ) );
292 xComponent.set( m_xSubElement, UNO_QUERY );
293 if ( xComponent.is() )
294 xComponent->addEventListener( Reference< XUIConfigurationListener >( this ));
296 if ( m_xConfigData.is() )
298 xLayoutManager->lock();
299 impl_fillNewData();
300 xLayoutManager->unlock();
304 // XUIElement interface
305 Reference< XInterface > SAL_CALL ToolBarWrapper::getRealInterface( )
307 SolarMutexGuard g;
309 if ( m_xToolBarManager )
311 vcl::Window* pWindow = m_xToolBarManager->GetToolBar();
312 return Reference< XInterface >( VCLUnoHelper::GetInterface( pWindow ), UNO_QUERY );
315 return Reference< XInterface >();
318 //XUIFunctionExecute
319 void SAL_CALL ToolBarWrapper::functionExecute(
320 const OUString& aUIElementName,
321 const OUString& aCommand )
323 SolarMutexGuard g;
325 if ( m_xToolBarManager )
326 m_xToolBarManager->notifyRegisteredControllers( aUIElementName, aCommand );
329 void SAL_CALL ToolBarWrapper::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const css::uno::Any& aValue )
331 SolarMutexResettableGuard aLock;
332 bool bNoClose( m_bNoClose );
333 aLock.clear();
335 UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( nHandle, aValue );
337 aLock.reset();
339 bool bNewNoClose( m_bNoClose );
340 if ( !(m_xToolBarManager.is() && !m_bDisposed && ( bNewNoClose != bNoClose )))
341 return;
343 if ( !m_xToolBarManager )
344 return;
346 ToolBox* pToolBox = m_xToolBarManager->GetToolBar();
347 if ( !pToolBox )
348 return;
350 if ( bNewNoClose )
352 pToolBox->SetStyle( pToolBox->GetStyle() & ~WB_CLOSEABLE );
353 pToolBox->SetFloatStyle( pToolBox->GetFloatStyle() & ~WB_CLOSEABLE );
355 else
357 pToolBox->SetStyle( pToolBox->GetStyle() | WB_CLOSEABLE );
358 pToolBox->SetFloatStyle( pToolBox->GetFloatStyle() | WB_CLOSEABLE );
362 } // namespace framework
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */