LibreOffice Module framework (master) 1
menubarwrapper.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
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>
27
32#include <utility>
33#include <vcl/svapp.hxx>
34
35using namespace com::sun::star;
36using namespace com::sun::star::uno;
37using namespace com::sun::star::beans;
38using namespace com::sun::star::frame;
39using namespace com::sun::star::lang;
40using namespace com::sun::star::container;
41using namespace com::sun::star::awt;
42using namespace com::sun::star::util;
43using namespace ::com::sun::star::ui;
44
45namespace framework
46{
47
49 css::uno::Reference< css::uno::XComponentContext > xContext
50 )
51: MenuBarWrapper_Base( UIElementType::MENUBAR ),
52 m_bRefreshPopupControllerCache( true ),
53 m_xContext(std::move( xContext ))
54{
55}
56
58{
59}
60
62{
63 Reference< XComponent > xThis(this);
64
65 css::lang::EventObject aEvent( xThis );
66 m_aListenerContainer.disposeAndClear( aEvent );
67
69
70 m_xMenuBarManager->dispose();
71 m_xMenuBarManager.clear();
72 m_xConfigSource.clear();
73 m_xConfigData.clear();
74
75 m_xMenuBar.clear();
76 m_bDisposed = true;
77}
78
79// XInitialization
80void SAL_CALL MenuBarWrapper::initialize( const Sequence< Any >& aArguments )
81{
83
84 if ( m_bDisposed )
85 throw DisposedException();
86
87 if ( m_bInitialized )
88 return;
89
90 OUString aModuleIdentifier;
92
93 Reference< XFrame > xFrame( m_xWeakFrame );
94 if ( !(xFrame.is() && m_xConfigSource.is()) )
95 return;
96
97 // Create VCL menubar which will be filled with settings data
98 VclPtr<MenuBar> pVCLMenuBar;
99 {
100 SolarMutexGuard aSolarMutexGuard;
101 pVCLMenuBar = VclPtr<MenuBar>::Create();
102 }
103
104 Reference< XModuleManager2 > xModuleManager = ModuleManager::create( m_xContext );
105
106 try
107 {
108 aModuleIdentifier = xModuleManager->identify( xFrame );
109 }
110 catch( const Exception& )
111 {
112 }
113
115 try
116 {
117 xTrans.set( URLTransformer::create(m_xContext) );
118 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, false );
119 if ( m_xConfigData.is() )
120 {
121 // Fill menubar with container contents
122 sal_uInt16 nId = 1;
123 MenuBarManager::FillMenuWithConfiguration( nId, pVCLMenuBar, aModuleIdentifier, m_xConfigData, xTrans );
124 }
125 }
126 catch ( const NoSuchElementException& )
127 {
128 }
129
130 bool bMenuOnly( false );
131 for ( const Any& rArg : aArguments )
132 {
133 PropertyValue aPropValue;
134 if ( rArg >>= aPropValue )
135 {
136 if ( aPropValue.Name == "MenuOnly" )
137 aPropValue.Value >>= bMenuOnly;
138 }
139 }
140
141 if ( !bMenuOnly )
142 {
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;
148
150 xFrame,
151 xTrans,
152 xDispatchProvider,
153 aModuleIdentifier,
154 pVCLMenuBar,
155 false );
156 }
157
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 );
161}
162
163// XUIElementSettings
165{
167
168 if ( m_bDisposed )
169 throw DisposedException();
170
171 if ( !m_xMenuBarManager.is() )
172 return;
173
174 if ( m_xConfigSource.is() && m_bPersistent )
175 {
176 try
177 {
178 MenuBarManager* pMenuBarManager = static_cast< MenuBarManager *>( m_xMenuBarManager.get() );
179
180 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, false );
181 if ( m_xConfigData.is() )
182 pMenuBarManager->SetItemContainer( m_xConfigData );
183 }
184 catch ( const NoSuchElementException& )
185 {
186 }
187 }
188 else if ( !m_bPersistent )
189 {
190 // Transient menubar: do nothing
191 }
192}
194{
195 // Transient menubar => Fill menubar with new data
196 MenuBarManager* pMenuBarManager = static_cast< MenuBarManager *>( m_xMenuBarManager.get() );
197
198 if ( pMenuBarManager )
199 pMenuBarManager->SetItemContainer( m_xConfigData );
200}
201
203{
205 {
206 MenuBarManager* pMenuBarManager = static_cast< MenuBarManager *>( m_xMenuBarManager.get() );
207 if ( pMenuBarManager )
209 if ( !m_aPopupControllerCache.empty() )
211 }
212}
213
214// XElementAccess
216{
218}
219
221{
223
224 if ( m_bDisposed )
225 throw DisposedException();
226
228 return ( !m_aPopupControllerCache.empty() );
229}
230
231// XNameAccess
233 const OUString& aName )
234{
236
237 if ( m_bDisposed )
238 throw DisposedException();
239
241
242 PopupControllerCache::const_iterator pIter = m_aPopupControllerCache.find( aName );
243 if ( pIter == m_aPopupControllerCache.end() )
244 throw container::NoSuchElementException();
245
246 uno::Reference< frame::XDispatchProvider > xDispatchProvider = pIter->second.m_xDispatchProvider;
247 return uno::Any( xDispatchProvider );
248}
249
251{
253
254 if ( m_bDisposed )
255 throw DisposedException();
256
258
260}
261
263 const OUString& aName )
264{
266
267 if ( m_bDisposed )
268 throw DisposedException();
269
271
272 PopupControllerCache::const_iterator pIter = m_aPopupControllerCache.find( aName );
273 if ( pIter != m_aPopupControllerCache.end() )
274 return true;
275 else
276 return false;
277}
278
279// XUIElement
281{
282 if ( m_bDisposed )
283 throw DisposedException();
284
285 return Reference< XInterface >( m_xMenuBarManager, UNO_QUERY );
286}
287
288} // namespace framework
289
290/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
static VclPtr< reference_type > Create(Arg &&... arg)
css::uno::Type const & get()
void GetPopupController(PopupControllerCache &rPopupController)
void SetItemContainer(const css::uno::Reference< css::container::XIndexAccess > &rItemContainer)
static void FillMenuWithConfiguration(sal_uInt16 &nId, Menu *pMenu, const OUString &rModuleIdentifier, const css::uno::Reference< css::container::XIndexAccess > &rItemContainer, const css::uno::Reference< css::util::XURLTransformer > &rTransformer)
virtual void impl_fillNewData() override
virtual void SAL_CALL dispose() override
virtual sal_Bool SAL_CALL hasElements() override
virtual css::uno::Type SAL_CALL getElementType() override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
css::uno::Reference< css::lang::XComponent > m_xMenuBarManager
MenuBarWrapper(css::uno::Reference< css::uno::XComponentContext > xContext)
virtual ~MenuBarWrapper() override
virtual void SAL_CALL updateSettings() override
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getRealInterface() override
PopupControllerCache m_aPopupControllerCache
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool m_bDisposed
Sequence< PropertyValue > aArguments
OUString aName
@ Exception
css::uno::Sequence< typename M::key_type > mapKeysToSequence(M const &map)
Type
cppu::ImplInheritanceHelper< UIConfigElementWrapperBase, css::container::XNameAccess > MenuBarWrapper_Base
sal_Int16 nId
Reference< XFrame > xFrame
unsigned char sal_Bool