1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: menubarwrapper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
37 #include <uielement/menubarwrapper.hxx>
38 #include <threadhelp/resetableguard.hxx>
39 #include <helper/actiontriggerhelper.hxx>
42 //_________________________________________________________________________________________________________________
44 //_________________________________________________________________________________________________________________
45 #include <com/sun/star/lang/XServiceInfo.hpp>
46 #include <com/sun/star/beans/XPropertySet.hpp>
47 #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
48 #include <com/sun/star/awt/XMenuBar.hpp>
49 #include <com/sun/star/container/XIndexContainer.hpp>
50 #include <com/sun/star/container/XNameAccess.hpp>
51 #include <com/sun/star/ui/UIElementType.hpp>
52 #include <com/sun/star/frame/XModuleManager.hpp>
54 //_________________________________________________________________________________________________________________
56 //_________________________________________________________________________________________________________________
57 #include <comphelper/processfactory.hxx>
58 #include <tools/solar.h>
59 #include <vcl/svapp.hxx>
60 #include <rtl/logfile.hxx>
62 using namespace com::sun::star
;
63 using namespace com::sun::star::uno
;
64 using namespace com::sun::star::beans
;
65 using namespace com::sun::star::frame
;
66 using namespace com::sun::star::lang
;
67 using namespace com::sun::star::container
;
68 using namespace com::sun::star::awt
;
69 using namespace com::sun::star::util
;
70 using namespace ::com::sun::star::ui
;
75 //*****************************************************************************************************************
76 // XInterface, XTypeProvider
77 //*****************************************************************************************************************
78 DEFINE_XINTERFACE_11 ( MenuBarWrapper
,
79 UIConfigElementWrapperBase
,
80 DIRECT_INTERFACE( ::com::sun::star::lang::XTypeProvider
),
81 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElement
),
82 DIRECT_INTERFACE( ::com::sun::star::ui::XUIElementSettings
),
83 DIRECT_INTERFACE( ::com::sun::star::beans::XMultiPropertySet
),
84 DIRECT_INTERFACE( ::com::sun::star::beans::XFastPropertySet
),
85 DIRECT_INTERFACE( ::com::sun::star::beans::XPropertySet
),
86 DIRECT_INTERFACE( ::com::sun::star::lang::XInitialization
),
87 DIRECT_INTERFACE( ::com::sun::star::lang::XComponent
),
88 DIRECT_INTERFACE( ::com::sun::star::util::XUpdatable
),
89 DIRECT_INTERFACE( ::com::sun::star::ui::XUIConfigurationListener
),
90 DERIVED_INTERFACE( ::com::sun::star::container::XNameAccess
, ::com::sun::star::container::XElementAccess
)
93 DEFINE_XTYPEPROVIDER_11 ( MenuBarWrapper
,
94 ::com::sun::star::lang::XTypeProvider
,
95 ::com::sun::star::ui::XUIElement
,
96 ::com::sun::star::ui::XUIElementSettings
,
97 ::com::sun::star::beans::XMultiPropertySet
,
98 ::com::sun::star::beans::XFastPropertySet
,
99 ::com::sun::star::beans::XPropertySet
,
100 ::com::sun::star::lang::XInitialization
,
101 ::com::sun::star::lang::XComponent
,
102 ::com::sun::star::util::XUpdatable
,
103 ::com::sun::star::ui::XUIConfigurationListener
,
104 ::com::sun::star::container::XNameAccess
108 MenuBarWrapper::MenuBarWrapper(
109 const com::sun::star::uno::Reference
< com::sun::star::lang::XMultiServiceFactory
>& xServiceManager
111 : UIConfigElementWrapperBase( UIElementType::MENUBAR
,xServiceManager
),
112 m_bRefreshPopupControllerCache( sal_True
)
116 MenuBarWrapper::~MenuBarWrapper()
120 void SAL_CALL
MenuBarWrapper::dispose() throw (::com::sun::star::uno::RuntimeException
)
122 Reference
< XComponent
> xThis( static_cast< OWeakObject
* >(this), UNO_QUERY
);
124 com::sun::star::lang::EventObject
aEvent( xThis
);
125 m_aListenerContainer
.disposeAndClear( aEvent
);
127 ResetableGuard
aLock( m_aLock
);
129 m_xMenuBarManager
->dispose();
130 m_xMenuBarManager
.clear();
131 m_xConfigSource
.clear();
132 m_xConfigData
.clear();
135 m_bDisposed
= sal_True
;
139 void SAL_CALL
MenuBarWrapper::initialize( const Sequence
< Any
>& aArguments
) throw ( Exception
, RuntimeException
)
141 RTL_LOGFILE_CONTEXT( aLog
, "framework (cd100003) ::MenuBarWrapper::initialize" );
143 ResetableGuard
aLock( m_aLock
);
146 throw DisposedException();
148 if ( !m_bInitialized
)
150 rtl::OUString aModuleIdentifier
;
151 UIConfigElementWrapperBase::initialize( aArguments
);
153 Reference
< XFrame
> xFrame( m_xWeakFrame
);
154 if ( xFrame
.is() && m_xConfigSource
.is() )
156 // Create VCL menubar which will be filled with settings data
157 MenuBar
* pVCLMenuBar
= 0;
158 VCLXMenuBar
* pAwtMenuBar
= 0;
160 vos::OGuard
aSolarMutexGuard( Application::GetSolarMutex() );
161 pVCLMenuBar
= new MenuBar();
164 Reference
< XModuleManager
> xModuleManager
;
165 xModuleManager
= Reference
< XModuleManager
>(
166 m_xServiceFactory
->createInstance(
167 SERVICENAME_MODULEMANAGER
), UNO_QUERY_THROW
);
171 aModuleIdentifier
= xModuleManager
->identify( xFrame
);
177 Reference
< XURLTransformer
> xTrans
;
180 xTrans
.set( m_xServiceFactory
->createInstance(
181 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
182 "com.sun.star.util.URLTransformer" ))), UNO_QUERY
);
183 m_xConfigData
= m_xConfigSource
->getSettings( m_aResourceURL
, sal_False
);
184 if ( m_xConfigData
.is() )
186 // Fill menubar with container contents
188 MenuBarManager::FillMenuWithConfiguration( nId
, pVCLMenuBar
, aModuleIdentifier
, m_xConfigData
, xTrans
);
191 catch ( NoSuchElementException
& )
195 sal_Bool
bMenuOnly( sal_False
);
196 for ( sal_Int32 n
= 0; n
< aArguments
.getLength(); n
++ )
198 PropertyValue aPropValue
;
199 if ( aArguments
[n
] >>= aPropValue
)
201 if ( aPropValue
.Name
.equalsAscii( "MenuOnly" ))
202 aPropValue
.Value
>>= bMenuOnly
;
208 // Initialize menubar manager with our vcl menu bar. There are some situations where we only want to get the menu without any
209 // interaction which is done by the menu bar manager. This must be requested by a special property called "MenuOnly". Be careful
210 // a menu bar created with this property is not fully supported. It must be attached to a real menu bar manager to have full
211 // support. This feature is currently used for "Inplace editing"!
212 Reference
< XDispatchProvider
> xDispatchProvider
;
215 MenuBarManager
* pMenuBarManager
= new MenuBarManager( m_xServiceFactory
,
224 m_xMenuBarManager
= Reference
< XComponent
>( static_cast< OWeakObject
*>( pMenuBarManager
), UNO_QUERY
);
227 // Initialize toolkit menu bar implementation to have awt::XMenuBar for data exchange.
228 // Don't use this toolkit menu bar or one of its functions. It is only used as a data container!
229 pAwtMenuBar
= new VCLXMenuBar( pVCLMenuBar
);
230 m_xMenuBar
= Reference
< XMenuBar
>( static_cast< OWeakObject
*>( pAwtMenuBar
), UNO_QUERY
);
235 // XUIElementSettings
236 void SAL_CALL
MenuBarWrapper::updateSettings() throw ( RuntimeException
)
238 ResetableGuard
aLock( m_aLock
);
241 throw DisposedException();
243 if ( m_xMenuBarManager
.is() )
245 if ( m_xConfigSource
.is() && m_bPersistent
)
249 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
251 m_xConfigData
= m_xConfigSource
->getSettings( m_aResourceURL
, sal_False
);
252 if ( m_xConfigData
.is() )
253 pMenuBarManager
->SetItemContainer( m_xConfigData
);
255 catch ( NoSuchElementException
& )
259 else if ( !m_bPersistent
)
261 // Transient menubar: do nothing
265 void MenuBarWrapper::impl_fillNewData()
267 // Transient menubar => Fill menubar with new data
268 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
270 if ( pMenuBarManager
)
271 pMenuBarManager
->SetItemContainer( m_xConfigData
);
275 void MenuBarWrapper::fillPopupControllerCache()
277 if ( m_bRefreshPopupControllerCache
)
279 MenuBarManager
* pMenuBarManager
= static_cast< MenuBarManager
*>( m_xMenuBarManager
.get() );
280 if ( pMenuBarManager
)
281 pMenuBarManager
->GetPopupController( m_aPopupControllerCache
);
282 if ( !m_aPopupControllerCache
.empty() )
283 m_bRefreshPopupControllerCache
= sal_False
;
288 Type SAL_CALL
MenuBarWrapper::getElementType()
289 throw (::com::sun::star::uno::RuntimeException
)
291 return ::getCppuType(( Reference
< XDispatchProvider
>*)0);
294 ::sal_Bool SAL_CALL
MenuBarWrapper::hasElements()
295 throw (::com::sun::star::uno::RuntimeException
)
297 ResetableGuard
aLock( m_aLock
);
300 throw DisposedException();
302 fillPopupControllerCache();
303 return ( !m_aPopupControllerCache
.empty() );
307 Any SAL_CALL
MenuBarWrapper::getByName(
308 const ::rtl::OUString
& aName
)
309 throw ( container::NoSuchElementException
,
310 lang::WrappedTargetException
,
311 uno::RuntimeException
)
313 ResetableGuard
aLock( m_aLock
);
316 throw DisposedException();
318 fillPopupControllerCache();
320 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.find( aName
);
321 if ( pIter
!= m_aPopupControllerCache
.end() )
323 uno::Reference
< frame::XDispatchProvider
> xDispatchProvider
;
324 xDispatchProvider
= pIter
->second
.m_xDispatchProvider
;
325 return uno::makeAny( xDispatchProvider
);
328 throw container::NoSuchElementException();
331 Sequence
< ::rtl::OUString
> SAL_CALL
MenuBarWrapper::getElementNames()
332 throw (::com::sun::star::uno::RuntimeException
)
334 ResetableGuard
aLock( m_aLock
);
337 throw DisposedException();
339 fillPopupControllerCache();
341 Sequence
< rtl::OUString
> aSeq( m_aPopupControllerCache
.size() );
344 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.begin();
345 while ( pIter
!= m_aPopupControllerCache
.end() )
347 aSeq
[i
++] = pIter
->first
;
354 ::sal_Bool SAL_CALL
MenuBarWrapper::hasByName(
355 const ::rtl::OUString
& aName
)
356 throw (::com::sun::star::uno::RuntimeException
)
358 ResetableGuard
aLock( m_aLock
);
361 throw DisposedException();
363 fillPopupControllerCache();
365 PopupControllerCache::const_iterator pIter
= m_aPopupControllerCache
.find( aName
);
366 if ( pIter
!= m_aPopupControllerCache
.end() )
373 Reference
< XInterface
> SAL_CALL
MenuBarWrapper::getRealInterface() throw ( RuntimeException
)
376 throw DisposedException();
378 return Reference
< XInterface
>( m_xMenuBarManager
, UNO_QUERY
);
381 } // namespace framework