1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 //_________________________________________________________________________________________________________________
27 //	my own includes
28 //_________________________________________________________________________________________________________________
29 
30 
31 #include <uielement/toolbarwrapper.hxx>
32 #include <threadhelp/resetableguard.hxx>
33 #include <framework/actiontriggerhelper.hxx>
34 #include <uielement/constitemcontainer.hxx>
35 #include <uielement/rootitemcontainer.hxx>
36 #include <uielement/toolbarmanager.hxx>
37 
38 #ifndef __FRAMEWORK_UIELEMENT_TOOLBARW_HXX_
39 #include <uielement/toolbar.hxx>
40 #endif
41 
42 //_________________________________________________________________________________________________________________
43 //	interface includes
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/lang/DisposedException.hpp>
53 
54 //_________________________________________________________________________________________________________________
55 //	other includes
56 //_________________________________________________________________________________________________________________
57 
58 #ifndef _TOOLKIT_HELPER_VCLUNOHELPER_HXX_
59 #include <toolkit/helper/vclunohelper.hxx>
60 #endif
61 #include <toolkit/awt/vclxwindow.hxx>
62 #include <comphelper/processfactory.hxx>
63 
64 #include <svtools/miscopt.hxx>
65 #include <vcl/svapp.hxx>
66 #include <vcl/toolbox.hxx>
67 #include <rtl/logfile.hxx>
68 
69 using namespace com::sun::star;
70 using namespace com::sun::star::uno;
71 using namespace com::sun::star::beans;
72 using namespace com::sun::star::frame;
73 using namespace com::sun::star::lang;
74 using namespace com::sun::star::container;
75 using namespace com::sun::star::awt;
76 using namespace ::com::sun::star::ui;
77 
78 namespace framework
79 {
80 
ToolBarWrapper(const Reference<XMultiServiceFactory> & xServiceManager)81 ToolBarWrapper::ToolBarWrapper( const Reference< XMultiServiceFactory >& xServiceManager ) :
82     UIConfigElementWrapperBase( UIElementType::TOOLBAR,xServiceManager )
83 {
84 }
85 
~ToolBarWrapper()86 ToolBarWrapper::~ToolBarWrapper()
87 {
88 }
89 
90 // XInterface
acquire()91 void SAL_CALL ToolBarWrapper::acquire() throw()
92 {
93     UIConfigElementWrapperBase::acquire();
94 }
95 
release()96 void SAL_CALL ToolBarWrapper::release() throw()
97 {
98     UIConfigElementWrapperBase::release();
99 }
100 
queryInterface(const uno::Type & rType)101 uno::Any SAL_CALL ToolBarWrapper::queryInterface( const uno::Type & rType )
102 throw( ::com::sun::star::uno::RuntimeException )
103 {
104 	Any a = ::cppu::queryInterface(
105 				rType ,
106 				SAL_STATIC_CAST( ::com::sun::star::ui::XUIFunctionListener*, this ) );
107 
108 	if( a.hasValue() )
109 		return a;
110 
111 	return UIConfigElementWrapperBase::queryInterface( rType );
112 }
113 
114 // XComponent
dispose()115 void SAL_CALL ToolBarWrapper::dispose() throw ( RuntimeException )
116 {
117     Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
118 
119 	{
120 		ResetableGuard aLock( m_aLock );
121 		if ( m_bDisposed )
122 			return;
123 	}
124 
125 	com::sun::star::lang::EventObject aEvent( xThis );
126     m_aListenerContainer.disposeAndClear( aEvent );
127 
128     ResetableGuard aLock( m_aLock );
129 
130     if ( m_xToolBarManager.is() )
131         m_xToolBarManager->dispose();
132     m_xToolBarManager.clear();
133     m_xConfigSource.clear();
134     m_xConfigData.clear();
135     m_xToolBarWindow.clear();
136 
137     m_bDisposed = sal_True;
138 }
139 
140 // XInitialization
initialize(const Sequence<Any> & aArguments)141 void SAL_CALL ToolBarWrapper::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
142 {
143     ResetableGuard aLock( m_aLock );
144 
145     if ( m_bDisposed )
146         throw DisposedException();
147 
148     if ( !m_bInitialized )
149     {
150         UIConfigElementWrapperBase::initialize( aArguments );
151 
152         sal_Bool bPopupMode( sal_False );
153         for ( sal_Int32 i = 0; i < aArguments.getLength(); i++ )
154         {
155             PropertyValue aPropValue;
156             if ( aArguments[i] >>= aPropValue )
157             {
158                 if ( aPropValue.Name.equalsAsciiL( "PopupMode", 9 ))
159                 {
160                     aPropValue.Value >>= bPopupMode;
161                     break;
162                 }
163             }
164         }
165 
166         Reference< XFrame > xFrame( m_xWeakFrame );
167         if ( xFrame.is() && m_xConfigSource.is() )
168         {
169             // Create VCL based toolbar which will be filled with settings data
170             ToolBar* pToolBar = 0;
171             ToolBarManager* pToolBarManager = 0;
172             {
173                 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
174                 Window* pWindow = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
175                 if ( pWindow )
176                 {
177                     sal_uLong nStyles = WB_LINESPACING | WB_BORDER | WB_SCROLL | WB_MOVEABLE | WB_3DLOOK | WB_DOCKABLE | WB_SIZEABLE | WB_CLOSEABLE;
178 
179                     pToolBar = new ToolBar( pWindow, nStyles );
180                     m_xToolBarWindow = VCLUnoHelper::GetInterface( pToolBar );
181                     pToolBarManager = new ToolBarManager( m_xServiceFactory, xFrame, m_aResourceURL, pToolBar );
182                     pToolBar->SetToolBarManager( pToolBarManager );
183                     m_xToolBarManager = Reference< XComponent >( static_cast< OWeakObject *>( pToolBarManager ), UNO_QUERY );
184                     pToolBar->WillUsePopupMode( bPopupMode );
185                 }
186             }
187 
188             try
189             {
190                 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, sal_False );
191                 if ( m_xConfigData.is() && pToolBar && pToolBarManager )
192                 {
193                     // Fill toolbar with container contents
194                     pToolBarManager->FillToolbar( m_xConfigData );
195                     pToolBar->SetOutStyle( SvtMiscOptions().GetToolboxStyle() );
196                     pToolBar->EnableCustomize( sal_True );
197                     ::Size aActSize( pToolBar->GetSizePixel() );
198                     ::Size aSize( pToolBar->CalcWindowSizePixel() );
199                     aSize.Width() = aActSize.Width();
200                     pToolBar->SetOutputSizePixel( aSize );
201                 }
202             }
203             catch ( NoSuchElementException& )
204             {
205                 // No settings in our configuration manager. This means we are
206                 // a transient toolbar which has no persistent settings.
207                 m_bPersistent = sal_False;
208                 if ( pToolBar && pToolBarManager )
209                 {
210                     pToolBar->SetOutStyle( SvtMiscOptions().GetToolboxStyle() );
211                     pToolBar->EnableCustomize( sal_True );
212                     ::Size aActSize( pToolBar->GetSizePixel() );
213                     ::Size aSize( pToolBar->CalcWindowSizePixel() );
214                     aSize.Width() = aActSize.Width();
215                     pToolBar->SetOutputSizePixel( aSize );
216                 }
217             }
218         }
219     }
220 }
221 
222 // XEventListener
disposing(const::com::sun::star::lang::EventObject &)223 void SAL_CALL ToolBarWrapper::disposing( const ::com::sun::star::lang::EventObject& ) throw (::com::sun::star::uno::RuntimeException)
224 {
225     // nothing todo
226 }
227 
228 // XUpdatable
update()229 void SAL_CALL ToolBarWrapper::update() throw (::com::sun::star::uno::RuntimeException)
230 {
231     ResetableGuard aLock( m_aLock );
232 
233     if ( m_bDisposed )
234         throw DisposedException();
235 
236     ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
237     if ( pToolBarManager )
238         pToolBarManager->CheckAndUpdateImages();
239 }
240 
241 // XUIElementSettings
updateSettings()242 void SAL_CALL ToolBarWrapper::updateSettings() throw (::com::sun::star::uno::RuntimeException)
243 {
244     ResetableGuard aLock( m_aLock );
245 
246     if ( m_bDisposed )
247         throw DisposedException();
248 
249     if ( m_xToolBarManager.is() )
250     {
251         if ( m_xConfigSource.is() && m_bPersistent )
252         {
253             try
254             {
255                 ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
256 
257                 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, sal_False );
258                 if ( m_xConfigData.is() )
259                     pToolBarManager->FillToolbar( m_xConfigData );
260             }
261             catch ( NoSuchElementException& )
262             {
263             }
264         }
265         else if ( !m_bPersistent )
266         {
267             // Transient toolbar: do nothing
268         }
269     }
270 }
271 
impl_fillNewData()272 void ToolBarWrapper::impl_fillNewData()
273 {
274     // Transient toolbar => Fill toolbar with new data
275     ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
276     if ( pToolBarManager )
277         pToolBarManager->FillToolbar( m_xConfigData );
278 }
279 
280 // XUIElement interface
getRealInterface()281 Reference< XInterface > SAL_CALL ToolBarWrapper::getRealInterface(  ) throw (::com::sun::star::uno::RuntimeException)
282 {
283     ResetableGuard aLock( m_aLock );
284 
285     if ( m_xToolBarManager.is() )
286     {
287         ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
288         if ( pToolBarManager )
289         {
290             Window* pWindow = (Window *)pToolBarManager->GetToolBar();
291             return Reference< XInterface >( VCLUnoHelper::GetInterface( pWindow ), UNO_QUERY );
292         }
293     }
294 
295     return Reference< XInterface >();
296 }
297 
298 //XUIFunctionExecute
functionExecute(const::rtl::OUString & aUIElementName,const::rtl::OUString & aCommand)299 void SAL_CALL ToolBarWrapper::functionExecute(
300     const ::rtl::OUString& aUIElementName,
301     const ::rtl::OUString& aCommand )
302 throw (::com::sun::star::uno::RuntimeException)
303 {
304     ResetableGuard aLock( m_aLock );
305 
306     if ( m_xToolBarManager.is() )
307     {
308         ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
309         if ( pToolBarManager )
310             pToolBarManager->notifyRegisteredControllers( aUIElementName, aCommand );
311     }
312 }
313 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const com::sun::star::uno::Any & aValue)314 void SAL_CALL ToolBarWrapper::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const com::sun::star::uno::Any&  aValue ) throw( com::sun::star::uno::Exception )
315 {
316     ResetableGuard aLock( m_aLock );
317     sal_Bool bNoClose( m_bNoClose );
318     aLock.unlock();
319 
320     UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( nHandle, aValue );
321 
322     aLock.lock();
323 
324     sal_Bool bNewNoClose( m_bNoClose );
325     if ( m_xToolBarManager.is() && !m_bDisposed && ( bNewNoClose != bNoClose ))
326     {
327         ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
328         if ( pToolBarManager )
329         {
330             ToolBox* pToolBox = pToolBarManager->GetToolBar();
331             if ( pToolBox )
332             {
333                 if ( bNewNoClose )
334                 {
335                     pToolBox->SetStyle( pToolBox->GetStyle() & ~WB_CLOSEABLE );
336                     pToolBox->SetFloatStyle( pToolBox->GetFloatStyle() & ~WB_CLOSEABLE );
337                 }
338                 else
339                 {
340                     pToolBox->SetStyle( pToolBox->GetStyle() | WB_CLOSEABLE );
341                     pToolBox->SetFloatStyle( pToolBox->GetFloatStyle() | WB_CLOSEABLE );
342                 }
343             }
344         }
345     }
346 }
347 
348 } // namespace framework
349 
350