Update ooo320-m1
[ooovba.git] / svtools / source / uno / generictoolboxcontroller.cxx
blob778a07fb192bb0150ba0a98cf333e04a432d8047
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: generictoolboxcontroller.cxx,v $
10 * $Revision: 1.9 $
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_svtools.hxx"
33 #include <svtools/generictoolboxcontroller.hxx>
35 //_________________________________________________________________________________________________________________
36 // my own includes
37 //_________________________________________________________________________________________________________________
39 //_________________________________________________________________________________________________________________
40 // interface includes
41 //_________________________________________________________________________________________________________________
42 #include <com/sun/star/util/XURLTransformer.hpp>
43 #include <com/sun/star/frame/XDispatchProvider.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/lang/DisposedException.hpp>
46 #include <com/sun/star/frame/status/ItemStatus.hpp>
47 #include <com/sun/star/frame/status/ItemState.hpp>
49 //_________________________________________________________________________________________________________________
50 // other includes
51 //_________________________________________________________________________________________________________________
52 #include <vos/mutex.hxx>
53 #include <vcl/svapp.hxx>
55 using namespace ::com::sun::star::awt;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::frame;
60 using namespace ::com::sun::star::frame::status;
61 using namespace ::com::sun::star::util;
63 namespace svt
66 struct ExecuteInfo
68 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDispatch;
69 ::com::sun::star::util::URL aTargetURL;
70 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs;
73 GenericToolboxController::GenericToolboxController( const Reference< XMultiServiceFactory >& rServiceManager,
74 const Reference< XFrame >& rFrame,
75 ToolBox* pToolbox,
76 USHORT nID,
77 const ::rtl::OUString& aCommand ) :
78 svt::ToolboxController( rServiceManager, rFrame, aCommand )
79 , m_pToolbox( pToolbox )
80 , m_nID( nID )
82 // Initialization is done through ctor
83 m_bInitialized = sal_True;
85 // insert main command to our listener map
86 if ( m_aCommandURL.getLength() )
87 m_aListenerMap.insert( URLToDispatchMap::value_type( aCommand, Reference< XDispatch >() ));
90 GenericToolboxController::~GenericToolboxController()
94 void SAL_CALL GenericToolboxController::dispose()
95 throw ( RuntimeException )
97 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
99 svt::ToolboxController::dispose();
101 m_pToolbox = 0;
102 m_nID = 0;
105 void SAL_CALL GenericToolboxController::execute( sal_Int16 /*KeyModifier*/ )
106 throw ( RuntimeException )
108 Reference< XDispatch > xDispatch;
109 Reference< XURLTransformer > xURLTransformer;
110 ::rtl::OUString aCommandURL;
113 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
115 if ( m_bDisposed )
116 throw DisposedException();
118 if ( m_bInitialized &&
119 m_xFrame.is() &&
120 m_xServiceManager.is() &&
121 m_aCommandURL.getLength() )
123 xURLTransformer = Reference< XURLTransformer >( m_xServiceManager->createInstance(
124 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ))),
125 UNO_QUERY );
127 aCommandURL = m_aCommandURL;
128 URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
129 if ( pIter != m_aListenerMap.end() )
130 xDispatch = pIter->second;
134 if ( xDispatch.is() && xURLTransformer.is() )
136 com::sun::star::util::URL aTargetURL;
137 Sequence<PropertyValue> aArgs;
139 aTargetURL.Complete = aCommandURL;
140 xURLTransformer->parseStrict( aTargetURL );
142 // Execute dispatch asynchronously
143 ExecuteInfo* pExecuteInfo = new ExecuteInfo;
144 pExecuteInfo->xDispatch = xDispatch;
145 pExecuteInfo->aTargetURL = aTargetURL;
146 pExecuteInfo->aArgs = aArgs;
147 Application::PostUserEvent( STATIC_LINK(0, GenericToolboxController , ExecuteHdl_Impl), pExecuteInfo );
151 void GenericToolboxController::statusChanged( const FeatureStateEvent& Event )
152 throw ( RuntimeException )
154 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
156 if ( m_bDisposed )
157 return;
159 if ( m_pToolbox )
161 m_pToolbox->EnableItem( m_nID, Event.IsEnabled );
163 USHORT nItemBits = m_pToolbox->GetItemBits( m_nID );
164 nItemBits &= ~TIB_CHECKABLE;
165 TriState eTri = STATE_NOCHECK;
167 sal_Bool bValue = sal_Bool();
168 rtl::OUString aStrValue;
169 ItemStatus aItemState;
171 if ( Event.State >>= bValue )
173 // Boolean, treat it as checked/unchecked
174 m_pToolbox->SetItemBits( m_nID, nItemBits );
175 m_pToolbox->CheckItem( m_nID, bValue );
176 if ( bValue )
177 eTri = STATE_CHECK;
178 nItemBits |= TIB_CHECKABLE;
180 else if ( Event.State >>= aStrValue )
182 m_pToolbox->SetItemText( m_nID, aStrValue );
184 else if ( Event.State >>= aItemState )
186 eTri = STATE_DONTKNOW;
187 nItemBits |= TIB_CHECKABLE;
190 m_pToolbox->SetItemState( m_nID, eTri );
191 m_pToolbox->SetItemBits( m_nID, nItemBits );
195 IMPL_STATIC_LINK_NOINSTANCE( GenericToolboxController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
199 // Asynchronous execution as this can lead to our own destruction!
200 // Framework can recycle our current frame and the layout manager disposes all user interface
201 // elements if a component gets detached from its frame!
202 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
204 catch ( Exception& )
207 delete pExecuteInfo;
208 return 0;
211 } // namespace