update dev300-m58
[ooovba.git] / framework / source / uielement / generictoolbarcontroller.cxx
blob0a6d92975dea75efe0c9e4d3d05a5dffe951d4c8
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: generictoolbarcontroller.cxx,v $
10 * $Revision: 1.23 $
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_GENERICTOOLBARCONTROLLER_HXX
35 #include "uielement/generictoolbarcontroller.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>
57 //_________________________________________________________________________________________________________________
58 // other includes
59 //_________________________________________________________________________________________________________________
60 #include <svtools/toolboxcontroller.hxx>
61 #include <vos/mutex.hxx>
62 #include <vcl/svapp.hxx>
63 #ifndef _VCL_MNEMONIC_HXX_
64 #include <vcl/mnemonic.hxx>
65 #endif
66 #include <tools/urlobj.hxx>
67 #include <classes/resource.hrc>
68 #include <classes/fwkresid.hxx>
69 #include <dispatch/uieventloghelper.hxx>
71 #include <xml/menuconfiguration.hxx>
72 #include <uielement/menubarmanager.hxx>
74 using namespace ::com::sun::star::awt;
75 using namespace ::com::sun::star::uno;
76 using namespace ::com::sun::star::beans;
77 using namespace ::com::sun::star::lang;
78 using namespace ::com::sun::star::frame;
79 using namespace ::com::sun::star::frame::status;
80 using namespace ::com::sun::star::util;
81 using namespace ::com::sun::star::container;
83 namespace framework
86 static sal_Bool isEnumCommand( const rtl::OUString& rCommand )
88 INetURLObject aURL( rCommand );
90 if (( aURL.GetProtocol() == INET_PROT_UNO ) &&
91 ( aURL.GetURLPath().indexOf( '.' ) != -1))
92 return sal_True;
94 return sal_False;
97 static rtl::OUString getEnumCommand( const rtl::OUString& rCommand )
99 INetURLObject aURL( rCommand );
101 rtl::OUString aEnumCommand;
102 String aURLPath = aURL.GetURLPath();
103 xub_StrLen nIndex = aURLPath.Search( '.' );
104 if (( nIndex > 0 ) && ( nIndex < aURLPath.Len() ))
105 aEnumCommand = aURLPath.Copy( nIndex+1 );
107 return aEnumCommand;
110 static rtl::OUString getMasterCommand( const rtl::OUString& rCommand )
112 rtl::OUString aMasterCommand( rCommand );
113 INetURLObject aURL( rCommand );
114 if ( aURL.GetProtocol() == INET_PROT_UNO )
116 sal_Int32 nIndex = aURL.GetURLPath().indexOf( '.' );
117 if ( nIndex )
119 aURL.SetURLPath( aURL.GetURLPath().copy( 0, nIndex ) );
120 aMasterCommand = aURL.GetMainURL( INetURLObject::NO_DECODE );
123 return aMasterCommand;
126 struct ExecuteInfo
128 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDispatch;
129 ::com::sun::star::util::URL aTargetURL;
130 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs;
133 GenericToolbarController::GenericToolbarController( const Reference< XMultiServiceFactory >& rServiceManager,
134 const Reference< XFrame >& rFrame,
135 ToolBox* pToolbar,
136 USHORT nID,
137 const ::rtl::OUString& aCommand ) :
138 svt::ToolboxController( rServiceManager, rFrame, aCommand )
139 , m_pToolbar( pToolbar )
140 , m_nID( nID )
141 , m_bEnumCommand( isEnumCommand( aCommand ))
142 , m_bMadeInvisible( sal_False )
143 , m_aEnumCommand( getEnumCommand( aCommand ))
145 if ( m_bEnumCommand )
146 addStatusListener( getMasterCommand( aCommand ) );
149 GenericToolbarController::~GenericToolbarController()
153 void SAL_CALL GenericToolbarController::dispose()
154 throw ( RuntimeException )
156 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
158 svt::ToolboxController::dispose();
160 m_pToolbar = 0;
161 m_nID = 0;
164 void SAL_CALL GenericToolbarController::execute( sal_Int16 KeyModifier )
165 throw ( RuntimeException )
167 Reference< XDispatch > xDispatch;
168 Reference< XURLTransformer > xURLTransformer;
169 ::rtl::OUString aCommandURL;
172 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
174 if ( m_bDisposed )
175 throw DisposedException();
177 if ( m_bInitialized &&
178 m_xFrame.is() &&
179 m_xServiceManager.is() &&
180 m_aCommandURL.getLength() )
182 xURLTransformer = Reference< XURLTransformer >( m_xServiceManager->createInstance(
183 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
184 UNO_QUERY );
186 aCommandURL = m_aCommandURL;
187 URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
188 if ( pIter != m_aListenerMap.end() )
189 xDispatch = pIter->second;
193 if ( xDispatch.is() && xURLTransformer.is() )
195 com::sun::star::util::URL aTargetURL;
196 Sequence<PropertyValue> aArgs( 1 );
198 // Add key modifier to argument list
199 aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
200 aArgs[0].Value <<= KeyModifier;
202 aTargetURL.Complete = aCommandURL;
203 xURLTransformer->parseStrict( aTargetURL );
205 // Execute dispatch asynchronously
206 ExecuteInfo* pExecuteInfo = new ExecuteInfo;
207 pExecuteInfo->xDispatch = xDispatch;
208 pExecuteInfo->aTargetURL = aTargetURL;
209 pExecuteInfo->aArgs = aArgs;
210 if(::comphelper::UiEventsLogger::isEnabled()) //#i88653#
211 UiEventLogHelper(::rtl::OUString::createFromAscii("GenericToolbarController")).log( m_xServiceManager, m_xFrame, aTargetURL, aArgs);
212 Application::PostUserEvent( STATIC_LINK(0, GenericToolbarController , ExecuteHdl_Impl), pExecuteInfo );
216 void GenericToolbarController::statusChanged( const FeatureStateEvent& Event )
217 throw ( RuntimeException )
219 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
221 if ( m_bDisposed )
222 return;
224 if ( m_pToolbar )
226 m_pToolbar->EnableItem( m_nID, Event.IsEnabled );
228 USHORT nItemBits = m_pToolbar->GetItemBits( m_nID );
229 nItemBits &= ~TIB_CHECKABLE;
230 TriState eTri = STATE_NOCHECK;
232 sal_Bool bValue = sal_Bool();
233 rtl::OUString aStrValue;
234 ItemStatus aItemState;
235 Visibility aItemVisibility;
237 if (( Event.State >>= bValue ) && !m_bEnumCommand )
239 // Boolean, treat it as checked/unchecked
240 if ( m_bMadeInvisible )
241 m_pToolbar->ShowItem( m_nID, TRUE );
242 m_pToolbar->CheckItem( m_nID, bValue );
243 if ( bValue )
244 eTri = STATE_CHECK;
245 nItemBits |= TIB_CHECKABLE;
247 else if ( Event.State >>= aStrValue )
249 if ( m_bEnumCommand )
251 if ( aStrValue == m_aEnumCommand )
252 bValue = sal_True;
253 else
254 bValue = sal_False;
256 m_pToolbar->CheckItem( m_nID, bValue );
257 if ( bValue )
258 eTri = STATE_CHECK;
259 nItemBits |= TIB_CHECKABLE;
261 else
263 // Replacement for place holders
264 if ( aStrValue.matchAsciiL( "($1)", 4 ))
266 String aResStr = String( FwkResId( STR_UPDATEDOC ));
267 rtl::OUString aTmp( aResStr );
268 aTmp += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( " " ));
269 aTmp += aStrValue.copy( 4 );
270 aStrValue = aTmp;
272 else if ( aStrValue.matchAsciiL( "($2)", 4 ))
274 String aResStr = String( FwkResId( STR_CLOSEDOC_ANDRETURN ));
275 rtl::OUString aTmp( aResStr );
276 aTmp += aStrValue.copy( 4 );
277 aStrValue = aTmp;
279 else if ( aStrValue.matchAsciiL( "($3)", 4 ))
281 String aResStr = String( FwkResId( STR_SAVECOPYDOC ));
282 rtl::OUString aTmp( aResStr );
283 aTmp += aStrValue.copy( 4 );
284 aStrValue = aTmp;
286 ::rtl::OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
287 m_pToolbar->SetItemText( m_nID, aText );
288 m_pToolbar->SetQuickHelpText( m_nID, aText );
291 if ( m_bMadeInvisible )
292 m_pToolbar->ShowItem( m_nID, TRUE );
294 else if (( Event.State >>= aItemState ) && !m_bEnumCommand )
296 eTri = STATE_DONTKNOW;
297 nItemBits |= TIB_CHECKABLE;
298 if ( m_bMadeInvisible )
299 m_pToolbar->ShowItem( m_nID, TRUE );
301 else if ( Event.State >>= aItemVisibility )
303 m_pToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
304 m_bMadeInvisible = !aItemVisibility.bVisible;
306 else if ( m_bMadeInvisible )
307 m_pToolbar->ShowItem( m_nID, TRUE );
309 m_pToolbar->SetItemState( m_nID, eTri );
310 m_pToolbar->SetItemBits( m_nID, nItemBits );
314 IMPL_STATIC_LINK_NOINSTANCE( GenericToolbarController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
316 const sal_uInt32 nRef = Application::ReleaseSolarMutex();
319 // Asynchronous execution as this can lead to our own destruction!
320 // Framework can recycle our current frame and the layout manager disposes all user interface
321 // elements if a component gets detached from its frame!
322 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
324 catch ( Exception& )
328 Application::AcquireSolarMutex( nRef );
329 delete pExecuteInfo;
330 return 0;
333 MenuToolbarController::MenuToolbarController( const Reference< XMultiServiceFactory >& rServiceManager, const Reference< XFrame >& rFrame, ToolBox* pToolBar, USHORT nID, const rtl::OUString& aCommand, const rtl::OUString& aModuleIdentifier, const Reference< XIndexAccess >& xMenuDesc ) : GenericToolbarController( rServiceManager, rFrame, pToolBar, nID, aCommand ), m_xMenuDesc( xMenuDesc ), pMenu( NULL ), m_aModuleIdentifier( aModuleIdentifier )
337 MenuToolbarController::~MenuToolbarController()
341 if ( m_xMenuManager.is() )
342 m_xMenuManager->dispose();
344 catch( Exception& ) {}
345 if ( pMenu )
347 delete pMenu;
348 pMenu = NULL;
353 class Toolbarmenu : public PopupMenu
355 public:
356 Toolbarmenu();
357 ~Toolbarmenu();
360 Toolbarmenu::Toolbarmenu()
362 OSL_TRACE("**** contstructing Toolbarmenu 0x%x", this );
365 Toolbarmenu::~Toolbarmenu()
367 OSL_TRACE("**** destructing Toolbarmenu 0x%x", this );
370 void SAL_CALL MenuToolbarController::click() throw (RuntimeException)
372 createPopupWindow();
375 Reference< XWindow > SAL_CALL
376 MenuToolbarController::createPopupWindow() throw (::com::sun::star::uno::RuntimeException)
378 if ( !pMenu )
380 Reference< XDispatchProvider > xDispatch;
381 Reference< XURLTransformer > xURLTransformer( m_xServiceManager->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))), UNO_QUERY );
382 pMenu = new Toolbarmenu();
383 m_xMenuManager.set( new MenuBarManager( m_xServiceManager, m_xFrame, xURLTransformer, xDispatch, m_aModuleIdentifier, pMenu, sal_True, sal_True ) );
384 if ( m_xMenuManager.is() )
386 MenuBarManager* pMgr = dynamic_cast< MenuBarManager* >( m_xMenuManager.get() );
387 pMgr->SetItemContainer( m_xMenuDesc );
391 ::Rectangle aRect( m_pToolbar->GetItemRect( m_nID ) );
392 pMenu->Execute( m_pToolbar, aRect, POPUPMENU_EXECUTE_DOWN );
393 return NULL;
395 } // namespace