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