xref: /trunk/main/framework/source/uielement/toolbarwrapper.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
103     Any a = ::cppu::queryInterface(
104                 rType ,
105                 SAL_STATIC_CAST( ::com::sun::star::ui::XUIFunctionListener*, this ) );
106 
107     if( a.hasValue() )
108         return a;
109 
110     return UIConfigElementWrapperBase::queryInterface( rType );
111 }
112 
113 // XComponent
dispose()114 void SAL_CALL ToolBarWrapper::dispose()
115 {
116     Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
117 
118     {
119         ResetableGuard aLock( m_aLock );
120         if ( m_bDisposed )
121             return;
122     }
123 
124     com::sun::star::lang::EventObject aEvent( xThis );
125     m_aListenerContainer.disposeAndClear( aEvent );
126 
127     ResetableGuard aLock( m_aLock );
128 
129     if ( m_xToolBarManager.is() )
130         m_xToolBarManager->dispose();
131     m_xToolBarManager.clear();
132     m_xConfigSource.clear();
133     m_xConfigData.clear();
134     m_xToolBarWindow.clear();
135 
136     m_bDisposed = sal_True;
137 }
138 
139 // XInitialization
initialize(const Sequence<Any> & aArguments)140 void SAL_CALL ToolBarWrapper::initialize( const Sequence< Any >& aArguments )
141 {
142     ResetableGuard aLock( m_aLock );
143 
144     if ( m_bDisposed )
145         throw DisposedException();
146 
147     if ( !m_bInitialized )
148     {
149         UIConfigElementWrapperBase::initialize( aArguments );
150 
151         sal_Bool bPopupMode( sal_False );
152         for ( sal_Int32 i = 0; i < aArguments.getLength(); i++ )
153         {
154             PropertyValue aPropValue;
155             if ( aArguments[i] >>= aPropValue )
156             {
157                 if ( aPropValue.Name.equalsAsciiL( "PopupMode", 9 ))
158                 {
159                     aPropValue.Value >>= bPopupMode;
160                     break;
161                 }
162             }
163         }
164 
165         Reference< XFrame > xFrame( m_xWeakFrame );
166         if ( xFrame.is() && m_xConfigSource.is() )
167         {
168             // Create VCL based toolbar which will be filled with settings data
169             ToolBar* pToolBar = 0;
170             ToolBarManager* pToolBarManager = 0;
171             {
172                 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
173                 Window* pWindow = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
174                 if ( pWindow )
175                 {
176                     sal_uLong nStyles = WB_LINESPACING | WB_BORDER | WB_SCROLL | WB_MOVEABLE | WB_3DLOOK | WB_DOCKABLE | WB_SIZEABLE | WB_CLOSEABLE;
177 
178                     pToolBar = new ToolBar( pWindow, nStyles );
179                     m_xToolBarWindow = VCLUnoHelper::GetInterface( pToolBar );
180                     pToolBarManager = new ToolBarManager( m_xServiceFactory, xFrame, m_aResourceURL, pToolBar );
181                     pToolBar->SetToolBarManager( pToolBarManager );
182                     m_xToolBarManager = Reference< XComponent >( static_cast< OWeakObject *>( pToolBarManager ), UNO_QUERY );
183                     pToolBar->WillUsePopupMode( bPopupMode );
184                 }
185             }
186 
187             try
188             {
189                 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, sal_False );
190                 if ( m_xConfigData.is() && pToolBar && pToolBarManager )
191                 {
192                     // Fill toolbar with container contents
193                     pToolBarManager->FillToolbar( m_xConfigData );
194                     pToolBar->SetOutStyle( SvtMiscOptions().GetToolboxStyle() );
195                     pToolBar->EnableCustomize( sal_True );
196                     ::Size aActSize( pToolBar->GetSizePixel() );
197                     ::Size aSize( pToolBar->CalcWindowSizePixel() );
198                     aSize.Width() = aActSize.Width();
199                     pToolBar->SetOutputSizePixel( aSize );
200                 }
201             }
202             catch ( NoSuchElementException& )
203             {
204                 // No settings in our configuration manager. This means we are
205                 // a transient toolbar which has no persistent settings.
206                 m_bPersistent = sal_False;
207                 if ( pToolBar && pToolBarManager )
208                 {
209                     pToolBar->SetOutStyle( SvtMiscOptions().GetToolboxStyle() );
210                     pToolBar->EnableCustomize( sal_True );
211                     ::Size aActSize( pToolBar->GetSizePixel() );
212                     ::Size aSize( pToolBar->CalcWindowSizePixel() );
213                     aSize.Width() = aActSize.Width();
214                     pToolBar->SetOutputSizePixel( aSize );
215                 }
216             }
217         }
218     }
219 }
220 
221 // XEventListener
disposing(const::com::sun::star::lang::EventObject &)222 void SAL_CALL ToolBarWrapper::disposing( const ::com::sun::star::lang::EventObject& )
223 {
224     // nothing todo
225 }
226 
227 // XUpdatable
update()228 void SAL_CALL ToolBarWrapper::update()
229 {
230     ResetableGuard aLock( m_aLock );
231 
232     if ( m_bDisposed )
233         throw DisposedException();
234 
235     ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
236     if ( pToolBarManager )
237         pToolBarManager->CheckAndUpdateImages();
238 }
239 
240 // XUIElementSettings
updateSettings()241 void SAL_CALL ToolBarWrapper::updateSettings()
242 {
243     ResetableGuard aLock( m_aLock );
244 
245     if ( m_bDisposed )
246         throw DisposedException();
247 
248     if ( m_xToolBarManager.is() )
249     {
250         if ( m_xConfigSource.is() && m_bPersistent )
251         {
252             try
253             {
254                 ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
255 
256                 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, sal_False );
257                 if ( m_xConfigData.is() )
258                     pToolBarManager->FillToolbar( m_xConfigData );
259             }
260             catch ( NoSuchElementException& )
261             {
262             }
263         }
264         else if ( !m_bPersistent )
265         {
266             // Transient toolbar: do nothing
267         }
268     }
269 }
270 
impl_fillNewData()271 void ToolBarWrapper::impl_fillNewData()
272 {
273     // Transient toolbar => Fill toolbar with new data
274     ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
275     if ( pToolBarManager )
276         pToolBarManager->FillToolbar( m_xConfigData );
277 }
278 
279 // XUIElement interface
getRealInterface()280 Reference< XInterface > SAL_CALL ToolBarWrapper::getRealInterface(  )
281 {
282     ResetableGuard aLock( m_aLock );
283 
284     if ( m_xToolBarManager.is() )
285     {
286         ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
287         if ( pToolBarManager )
288         {
289             Window* pWindow = (Window *)pToolBarManager->GetToolBar();
290             return Reference< XInterface >( VCLUnoHelper::GetInterface( pWindow ), UNO_QUERY );
291         }
292     }
293 
294     return Reference< XInterface >();
295 }
296 
297 //XUIFunctionExecute
functionExecute(const::rtl::OUString & aUIElementName,const::rtl::OUString & aCommand)298 void SAL_CALL ToolBarWrapper::functionExecute(
299     const ::rtl::OUString& aUIElementName,
300     const ::rtl::OUString& aCommand )
301 {
302     ResetableGuard aLock( m_aLock );
303 
304     if ( m_xToolBarManager.is() )
305     {
306         ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
307         if ( pToolBarManager )
308             pToolBarManager->notifyRegisteredControllers( aUIElementName, aCommand );
309     }
310 }
311 
setFastPropertyValue_NoBroadcast(sal_Int32 nHandle,const com::sun::star::uno::Any & aValue)312 void SAL_CALL ToolBarWrapper::setFastPropertyValue_NoBroadcast( sal_Int32 nHandle, const com::sun::star::uno::Any&  aValue )
313 {
314     ResetableGuard aLock( m_aLock );
315     sal_Bool bNoClose( m_bNoClose );
316     aLock.unlock();
317 
318     UIConfigElementWrapperBase::setFastPropertyValue_NoBroadcast( nHandle, aValue );
319 
320     aLock.lock();
321 
322     sal_Bool bNewNoClose( m_bNoClose );
323     if ( m_xToolBarManager.is() && !m_bDisposed && ( bNewNoClose != bNoClose ))
324     {
325         ToolBarManager* pToolBarManager = static_cast< ToolBarManager *>( m_xToolBarManager.get() );
326         if ( pToolBarManager )
327         {
328             ToolBox* pToolBox = pToolBarManager->GetToolBar();
329             if ( pToolBox )
330             {
331                 if ( bNewNoClose )
332                 {
333                     pToolBox->SetStyle( pToolBox->GetStyle() & ~WB_CLOSEABLE );
334                     pToolBox->SetFloatStyle( pToolBox->GetFloatStyle() & ~WB_CLOSEABLE );
335                 }
336                 else
337                 {
338                     pToolBox->SetStyle( pToolBox->GetStyle() | WB_CLOSEABLE );
339                     pToolBox->SetFloatStyle( pToolBox->GetFloatStyle() | WB_CLOSEABLE );
340                 }
341             }
342         }
343     }
344 }
345 
346 } // namespace framework
347