update dev300-m58
[ooovba.git] / framework / source / uielement / complextoolbarcontroller.cxx
blob3b5d1972615c034ac4509736ff2ada01b5abd0cc
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: complextoolbarcontroller.cxx,v $
10 * $Revision: 1.10.26.1 $
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 #ifndef __FRAMEWORK_UIELEMENT_COMPLEXTOOLBARCONTROLLER_HXX
35 #include "uielement/complextoolbarcontroller.hxx"
36 #endif
38 //_________________________________________________________________________________________________________________
39 // my own includes
40 //_________________________________________________________________________________________________________________
42 #ifndef __FRAMEWORK_TOOLBAR_HXX_
43 #include "uielement/toolbar.hxx"
44 #endif
46 //_________________________________________________________________________________________________________________
47 // interface includes
48 //_________________________________________________________________________________________________________________
49 #include <com/sun/star/util/XURLTransformer.hpp>
50 #include <com/sun/star/frame/XDispatchProvider.hpp>
51 #include <com/sun/star/beans/PropertyValue.hpp>
52 #include <com/sun/star/lang/DisposedException.hpp>
53 #include <com/sun/star/frame/status/ItemStatus.hpp>
54 #include <com/sun/star/frame/status/ItemState.hpp>
55 #include <com/sun/star/frame/status/Visibility.hpp>
56 #include <com/sun/star/frame/XControlNotificationListener.hpp>
58 //_________________________________________________________________________________________________________________
59 // other includes
60 //_________________________________________________________________________________________________________________
61 #include <svtools/toolboxcontroller.hxx>
62 #include <vos/mutex.hxx>
63 #include <vcl/svapp.hxx>
64 #ifndef _VCL_MNEMONIC_HXX_
65 #include <vcl/mnemonic.hxx>
66 #endif
67 #include <tools/urlobj.hxx>
68 #include <dispatch/uieventloghelper.hxx>
70 using namespace ::com::sun::star;
71 using namespace ::com::sun::star::awt;
72 using namespace ::com::sun::star::uno;
73 using namespace ::com::sun::star::beans;
74 using namespace ::com::sun::star::lang;
75 using namespace ::com::sun::star::frame;
76 using namespace ::com::sun::star::frame::status;
77 using namespace ::com::sun::star::util;
79 namespace framework
82 // ------------------------------------------------------------------
84 ComplexToolbarController::ComplexToolbarController(
85 const Reference< XMultiServiceFactory >& rServiceManager,
86 const Reference< XFrame >& rFrame,
87 ToolBox* pToolbar,
88 USHORT nID,
89 const ::rtl::OUString& aCommand ) :
90 svt::ToolboxController( rServiceManager, rFrame, aCommand )
91 , m_pToolbar( pToolbar )
92 , m_nID( nID )
93 , m_bMadeInvisible( sal_False )
95 m_xURLTransformer.set( m_xServiceManager->createInstance(
96 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
97 UNO_QUERY_THROW );
100 // ------------------------------------------------------------------
102 ComplexToolbarController::~ComplexToolbarController()
106 // ------------------------------------------------------------------
108 void SAL_CALL ComplexToolbarController::dispose()
109 throw ( RuntimeException )
111 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
113 m_pToolbar->SetItemWindow( m_nID, 0 );
114 svt::ToolboxController::dispose();
116 m_xURLTransformer.clear();
117 m_pToolbar = 0;
118 m_nID = 0;
121 // ------------------------------------------------------------------
122 Sequence<PropertyValue> ComplexToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
124 Sequence<PropertyValue> aArgs( 1 );
126 // Add key modifier to argument list
127 aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
128 aArgs[0].Value <<= KeyModifier;
129 return aArgs;
131 // -----------------------------------------------------------------------------
132 void SAL_CALL ComplexToolbarController::execute( sal_Int16 KeyModifier )
133 throw ( RuntimeException )
135 Reference< XDispatch > xDispatch;
136 Reference< XURLTransformer > xURLTransformer;
137 ::rtl::OUString aCommandURL;
138 ::com::sun::star::util::URL aTargetURL;
139 Sequence<PropertyValue> aArgs;
142 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
144 if ( m_bDisposed )
145 throw DisposedException();
147 if ( m_bInitialized &&
148 m_xFrame.is() &&
149 m_xServiceManager.is() &&
150 m_aCommandURL.getLength() )
152 xURLTransformer = m_xURLTransformer;
153 xDispatch = getDispatchFromCommand( m_aCommandURL );
154 aCommandURL = m_aCommandURL;
155 aTargetURL = getInitializedURL();
156 aArgs = getExecuteArgs(KeyModifier);
160 if ( xDispatch.is() && aTargetURL.Complete.getLength() > 0 )
162 // Execute dispatch asynchronously
163 ExecuteInfo* pExecuteInfo = new ExecuteInfo;
164 pExecuteInfo->xDispatch = xDispatch;
165 pExecuteInfo->aTargetURL = aTargetURL;
166 pExecuteInfo->aArgs = aArgs;
167 if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
168 UiEventLogHelper(::rtl::OUString::createFromAscii("ComplexToolbarController")).log(
169 m_xServiceManager,
170 m_xFrame,
171 aTargetURL,
172 aArgs);
173 Application::PostUserEvent( STATIC_LINK(0, ComplexToolbarController , ExecuteHdl_Impl), pExecuteInfo );
177 // ------------------------------------------------------------------
179 void ComplexToolbarController::statusChanged( const FeatureStateEvent& Event )
180 throw ( RuntimeException )
182 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
184 if ( m_bDisposed )
185 return;
187 if ( m_pToolbar )
189 m_pToolbar->EnableItem( m_nID, Event.IsEnabled );
191 USHORT nItemBits = m_pToolbar->GetItemBits( m_nID );
192 nItemBits &= ~TIB_CHECKABLE;
193 TriState eTri = STATE_NOCHECK;
195 sal_Bool bValue = sal_Bool();
196 rtl::OUString aStrValue;
197 ItemStatus aItemState;
198 Visibility aItemVisibility;
199 ControlCommand aControlCommand;
201 if ( Event.State >>= bValue )
203 // Boolean, treat it as checked/unchecked
204 if ( m_bMadeInvisible )
205 m_pToolbar->ShowItem( m_nID, TRUE );
206 m_pToolbar->CheckItem( m_nID, bValue );
207 if ( bValue )
208 eTri = STATE_CHECK;
209 nItemBits |= TIB_CHECKABLE;
211 else if ( Event.State >>= aStrValue )
213 ::rtl::OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
214 m_pToolbar->SetItemText( m_nID, aText );
215 m_pToolbar->SetQuickHelpText( m_nID, aText );
217 if ( m_bMadeInvisible )
218 m_pToolbar->ShowItem( m_nID, TRUE );
220 else if ( Event.State >>= aItemState )
222 eTri = STATE_DONTKNOW;
223 nItemBits |= TIB_CHECKABLE;
224 if ( m_bMadeInvisible )
225 m_pToolbar->ShowItem( m_nID, TRUE );
227 else if ( Event.State >>= aItemVisibility )
229 m_pToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
230 m_bMadeInvisible = !aItemVisibility.bVisible;
232 else if ( Event.State >>= aControlCommand )
234 executeControlCommand( aControlCommand );
235 if ( m_bMadeInvisible )
236 m_pToolbar->ShowItem( m_nID, TRUE );
239 else if ( m_bMadeInvisible )
240 m_pToolbar->ShowItem( m_nID, TRUE );
242 m_pToolbar->SetItemState( m_nID, eTri );
243 m_pToolbar->SetItemBits( m_nID, nItemBits );
247 // ------------------------------------------------------------------
249 IMPL_STATIC_LINK_NOINSTANCE( ComplexToolbarController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
251 const sal_uInt32 nRef = Application::ReleaseSolarMutex();
254 // Asynchronous execution as this can lead to our own destruction!
255 // Framework can recycle our current frame and the layout manager disposes all user interface
256 // elements if a component gets detached from its frame!
257 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
259 catch ( Exception& )
263 Application::AcquireSolarMutex( nRef );
264 delete pExecuteInfo;
265 return 0;
268 // ------------------------------------------------------------------
270 IMPL_STATIC_LINK_NOINSTANCE( ComplexToolbarController, Notify_Impl, NotifyInfo*, pNotifyInfo )
272 const sal_uInt32 nRef = Application::ReleaseSolarMutex();
275 // Asynchronous execution: As this can lead to our own destruction!
276 // Framework can recycle our current frame and the layout manager disposes all user interface
277 // elements if a component gets detached from its frame!
278 frame::ControlEvent aEvent;
279 aEvent.aURL = pNotifyInfo->aSourceURL;
280 aEvent.Event = pNotifyInfo->aEventName;
281 aEvent.aInformation = pNotifyInfo->aInfoSeq;
282 pNotifyInfo->xNotifyListener->controlEvent( aEvent );
284 catch ( Exception& )
288 Application::AcquireSolarMutex( nRef );
289 delete pNotifyInfo;
290 return 0;
293 // ------------------------------------------------------------------
295 void ComplexToolbarController::addNotifyInfo(
296 const rtl::OUString& aEventName,
297 const uno::Reference< frame::XDispatch >& xDispatch,
298 const uno::Sequence< beans::NamedValue >& rInfo )
300 uno::Reference< frame::XControlNotificationListener > xControlNotify( xDispatch, uno::UNO_QUERY );
302 if ( xControlNotify.is() )
304 // Execute notification asynchronously
305 NotifyInfo* pNotifyInfo = new NotifyInfo;
307 pNotifyInfo->aEventName = aEventName;
308 pNotifyInfo->xNotifyListener = xControlNotify;
309 pNotifyInfo->aSourceURL = getInitializedURL();
311 // Add frame as source to the information sequence
312 sal_Int32 nCount = rInfo.getLength();
313 uno::Sequence< beans::NamedValue > aInfoSeq( rInfo );
314 aInfoSeq.realloc( nCount+1 );
315 aInfoSeq[nCount].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Source" ));
316 aInfoSeq[nCount].Value = uno::makeAny( getFrameInterface() );
317 pNotifyInfo->aInfoSeq = aInfoSeq;
319 Application::PostUserEvent( STATIC_LINK(0, ComplexToolbarController, Notify_Impl), pNotifyInfo );
323 // --------------------------------------------------------
324 sal_Int32 ComplexToolbarController::getFontSizePixel( const Window* pWindow )
326 const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
327 const Font& rFont = rSettings.GetAppFont();
329 // Calculate height of the application font used by window
330 sal_Int32 nHeight = sal_Int32( rFont.GetHeight() );
331 ::Size aPixelSize = pWindow->LogicToPixel( ::Size( 0, nHeight ), MAP_APPFONT );
332 return aPixelSize.Height();
335 // --------------------------------------------------------
337 uno::Reference< frame::XDispatch > ComplexToolbarController::getDispatchFromCommand( const rtl::OUString& aCommand ) const
339 uno::Reference< frame::XDispatch > xDispatch;
341 if ( m_bInitialized && m_xFrame.is() && m_xServiceManager.is() && aCommand.getLength() )
343 URLToDispatchMap::const_iterator pIter = m_aListenerMap.find( aCommand );
344 if ( pIter != m_aListenerMap.end() )
345 xDispatch = pIter->second;
348 return xDispatch;
351 // --------------------------------------------------------
353 const ::com::sun::star::util::URL& ComplexToolbarController::getInitializedURL()
355 if ( m_aURL.Complete.getLength() == 0 )
357 m_aURL.Complete = m_aCommandURL;
358 m_xURLTransformer->parseStrict( m_aURL );
360 return m_aURL;
363 void ComplexToolbarController::notifyFocusGet()
365 // send focus get notification
366 uno::Sequence< beans::NamedValue > aInfo;
367 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FocusSet" )),
368 getDispatchFromCommand( m_aCommandURL ),
369 aInfo );
372 void ComplexToolbarController::notifyFocusLost()
374 // send focus lost notification
375 uno::Sequence< beans::NamedValue > aInfo;
376 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FocusLost" )),
377 getDispatchFromCommand( m_aCommandURL ),
378 aInfo );
381 void ComplexToolbarController::notifyTextChanged( const ::rtl::OUString& aText )
383 // send text changed notification
384 uno::Sequence< beans::NamedValue > aInfo( 1 );
385 aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
386 aInfo[0].Value <<= aText;
387 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TextChanged" )),
388 getDispatchFromCommand( m_aCommandURL ),
389 aInfo );
392 } // namespace