1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/menubarwrapper.hxx>
22 #include <com/sun/star/beans/XPropertySet.hpp>
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/ui/UIElementType.hpp>
25 #include <com/sun/star/frame/ModuleManager.hpp>
26 #include <com/sun/star/util/URLTransformer.hpp>
28 #include <comphelper/sequence.hxx>
29 #include <cppuhelper/typeprovider.hxx>
30 #include <cppuhelper/queryinterface.hxx>
31 #include <toolkit/awt/vclxmenu.hxx>
33 #include <vcl/svapp.hxx>
35 using namespace com::sun::star
;
36 using namespace com::sun::star::uno
;
37 using namespace com::sun::star::beans
;
38 using namespace com::sun::star::frame
;
39 using namespace com::sun::star::lang
;
40 using namespace com::sun::star::container
;
41 using namespace com::sun::star::awt
;
42 using namespace com::sun::star::util
;
43 using namespace ::com::sun::star::ui
;
48 MenuBarWrapper::MenuBarWrapper(
49 css::uno::Reference
< css::uno::XComponentContext
> xContext
51 : MenuBarWrapper_Base( UIElementType::MENUBAR
),
52 m_bRefreshPopupControllerCache( true ),
53 m_xContext(std::move( xContext
))
57 MenuBarWrapper::~MenuBarWrapper()
61 void SAL_CALL
MenuBarWrapper::dispose()
63 Reference
< XComponent
> xThis(this);
65 css::lang::EventObject
aEvent( xThis
);
66 m_aListenerContainer
.disposeAndClear( aEvent
);
70 m_xMenuBarManager
->dispose();
71 m_xMenuBarManager
.clear();
72 m_xConfigSource
.clear();
73 m_xConfigData
.clear();
80 void SAL_CALL
MenuBarWrapper::initialize( const Sequence
< Any
>& aArguments
)
85 throw DisposedException();
90 OUString aModuleIdentifier
;
91 UIConfigElementWrapperBase::initialize( aArguments
);
93 Reference
< XFrame
> xFrame( m_xWeakFrame
);
94 if ( !(xFrame
.is() && m_xConfigSource
.is()) )
97 // Create VCL menubar which will be filled with settings data
98 VclPtr
<MenuBar
> pVCLMenuBar
;
100 SolarMutexGuard aSolarMutexGuard
;
101 pVCLMenuBar
= VclPtr
<MenuBar
>::Create();
104 Reference
< XModuleManager2
> xModuleManager
= ModuleManager::create( m_xContext
);
108 aModuleIdentifier
= xModuleManager
->identify( xFrame
);
110 catch( const Exception
& )
114 Reference
< XURLTransformer
> xTrans
;
117 xTrans
.set( URLTransformer::create(m_xContext
) );
118 m_xConfigData
= m_xConfigSource
->getSettings( m_aResourceURL
, false );
119 if ( m_xConfigData
.is() )
121 // Fill menubar with container contents
123 MenuBarManager::FillMenuWithConfiguration( nId
, pVCLMenuBar
, aModuleIdentifier
, m_xConfigData
, xTrans
);
126 catch ( const NoSuchElementException
& )
130 bool bMenuOnly( false );
131 for ( const Any
& rArg
: aArguments
)
133 PropertyValue aPropValue
;
134 if ( rArg
>>= aPropValue
)
136 if ( aPropValue
.Name
== "MenuOnly" )
137 aPropValue
.Value
>>= bMenuOnly
;
143 // Initialize menubar manager with our vcl menu bar. There are some situations where we only want to get the menu without any
144 // interaction which is done by the menu bar manager. This must be requested by a special property called "MenuOnly". Be careful
145 // a menu bar created with this property is not fully supported. It must be attached to a real menu bar manager to have full
146 // support. This feature is currently used for "Inplace editing"!
147 Reference
< XDispatchProvider
> xDispatchProvider
;
149 m_xMenuBarManager
= new MenuBarManager( m_xContext
,
158 // Initialize toolkit menu bar implementation to have awt::XMenuBar for data exchange.
159 // Don't use this toolkit menu bar or one of its functions. It is only used as a data container!
160 m_xMenuBar
= new VCLXMenuBar( pVCLMenuBar
);
163 // XUIElementSettings
164 void SAL_CALL
MenuBarWrapper::updateSettings()
169 throw DisposedException();
171 if ( !m_xMenuBarManager
.is() )
174 if ( m_xConfigSource
.is() && m_bPersistent
)
178 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
180 m_xConfigData
= m_xConfigSource
->getSettings( m_aResourceURL
, false );
181 if ( m_xConfigData
.is() )
182 pMenuBarManager
->SetItemContainer( m_xConfigData
);
184 catch ( const NoSuchElementException
& )
188 else if ( !m_bPersistent
)
190 // Transient menubar: do nothing
193 void MenuBarWrapper::impl_fillNewData()
195 // Transient menubar => Fill menubar with new data
196 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
198 if ( pMenuBarManager
)
199 pMenuBarManager
->SetItemContainer( m_xConfigData
);
202 void MenuBarWrapper::fillPopupControllerCache()
204 if ( m_bRefreshPopupControllerCache
)
206 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
207 if ( pMenuBarManager
)
208 pMenuBarManager
->GetPopupController( m_aPopupControllerCache
);
209 if ( !m_aPopupControllerCache
.empty() )
210 m_bRefreshPopupControllerCache
= false;
215 Type SAL_CALL
MenuBarWrapper::getElementType()
217 return cppu::UnoType
<XDispatchProvider
>::get();
220 sal_Bool SAL_CALL
MenuBarWrapper::hasElements()
225 throw DisposedException();
227 fillPopupControllerCache();
228 return ( !m_aPopupControllerCache
.empty() );
232 Any SAL_CALL
MenuBarWrapper::getByName(
233 const OUString
& aName
)
238 throw DisposedException();
240 fillPopupControllerCache();
242 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.find( aName
);
243 if ( pIter
== m_aPopupControllerCache
.end() )
244 throw container::NoSuchElementException();
246 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider
= pIter
->second
.m_xDispatchProvider
;
247 return uno::Any( xDispatchProvider
);
250 Sequence
< OUString
> SAL_CALL
MenuBarWrapper::getElementNames()
255 throw DisposedException();
257 fillPopupControllerCache();
259 return comphelper::mapKeysToSequence( m_aPopupControllerCache
);
262 sal_Bool SAL_CALL
MenuBarWrapper::hasByName(
263 const OUString
& aName
)
268 throw DisposedException();
270 fillPopupControllerCache();
272 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.find( aName
);
273 if ( pIter
!= m_aPopupControllerCache
.end() )
280 Reference
< XInterface
> SAL_CALL
MenuBarWrapper::getRealInterface()
283 throw DisposedException();
285 return Reference
< XInterface
>( m_xMenuBarManager
, UNO_QUERY
);
288 } // namespace framework
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */