Bump version to 5.0-14
[LibreOffice.git] / svtools / source / uno / generictoolboxcontroller.cxx
blobda53d908b3e3188906f9a8681427a267b0ddf7e3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svtools/generictoolboxcontroller.hxx>
22 #include <com/sun/star/util/URLTransformer.hpp>
23 #include <com/sun/star/util/XURLTransformer.hpp>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <com/sun/star/frame/status/ItemStatus.hpp>
28 #include <com/sun/star/frame/status/ItemState.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <osl/mutex.hxx>
32 #include <vcl/svapp.hxx>
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::frame;
39 using namespace ::com::sun::star::frame::status;
40 using namespace ::com::sun::star::util;
42 namespace svt
45 struct ExecuteInfo
47 ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch > xDispatch;
48 ::com::sun::star::util::URL aTargetURL;
49 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > aArgs;
52 GenericToolboxController::GenericToolboxController( const Reference< XComponentContext >& rxContext,
53 const Reference< XFrame >& rFrame,
54 ToolBox* pToolbox,
55 sal_uInt16 nID,
56 const OUString& aCommand ) :
57 svt::ToolboxController( rxContext, rFrame, aCommand )
58 , m_pToolbox( pToolbox )
59 , m_nID( nID )
61 // Initialization is done through ctor
62 m_bInitialized = true;
64 // insert main command to our listener map
65 if ( !m_aCommandURL.isEmpty() )
66 m_aListenerMap.insert( URLToDispatchMap::value_type( aCommand, Reference< XDispatch >() ));
69 GenericToolboxController::~GenericToolboxController()
73 void SAL_CALL GenericToolboxController::dispose()
74 throw ( RuntimeException, std::exception )
76 SolarMutexGuard aSolarMutexGuard;
77 m_pToolbox.clear();
78 m_nID = 0;
79 svt::ToolboxController::dispose();
82 void SAL_CALL GenericToolboxController::execute( sal_Int16 /*KeyModifier*/ )
83 throw ( RuntimeException, std::exception )
85 Reference< XDispatch > xDispatch;
86 Reference< XURLTransformer > xURLTransformer;
87 OUString aCommandURL;
90 SolarMutexGuard aSolarMutexGuard;
92 if ( m_bDisposed )
93 throw DisposedException();
95 if ( m_bInitialized &&
96 m_xFrame.is() &&
97 m_xContext.is() &&
98 !m_aCommandURL.isEmpty() )
100 xURLTransformer = URLTransformer::create( m_xContext );
102 aCommandURL = m_aCommandURL;
103 URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
104 if ( pIter != m_aListenerMap.end() )
105 xDispatch = pIter->second;
109 if ( xDispatch.is() && xURLTransformer.is() )
111 com::sun::star::util::URL aTargetURL;
112 Sequence<PropertyValue> aArgs;
114 aTargetURL.Complete = aCommandURL;
115 xURLTransformer->parseStrict( aTargetURL );
117 // Execute dispatch asynchronously
118 ExecuteInfo* pExecuteInfo = new ExecuteInfo;
119 pExecuteInfo->xDispatch = xDispatch;
120 pExecuteInfo->aTargetURL = aTargetURL;
121 pExecuteInfo->aArgs = aArgs;
122 Application::PostUserEvent( LINK(0, GenericToolboxController , ExecuteHdl_Impl), pExecuteInfo );
126 void GenericToolboxController::statusChanged( const FeatureStateEvent& Event )
127 throw ( RuntimeException, std::exception )
129 SolarMutexGuard aSolarMutexGuard;
131 if ( m_bDisposed )
132 return;
134 if ( m_pToolbox )
136 m_pToolbox->EnableItem( m_nID, Event.IsEnabled );
138 ToolBoxItemBits nItemBits = m_pToolbox->GetItemBits( m_nID );
139 nItemBits &= ~ToolBoxItemBits::CHECKABLE;
140 TriState eTri = TRISTATE_FALSE;
142 bool bValue;
143 OUString aStrValue;
144 ItemStatus aItemState;
146 if ( Event.State >>= bValue )
148 // Boolean, treat it as checked/unchecked
149 m_pToolbox->SetItemBits( m_nID, nItemBits );
150 m_pToolbox->CheckItem( m_nID, bValue );
151 if ( bValue )
152 eTri = TRISTATE_TRUE;
153 nItemBits |= ToolBoxItemBits::CHECKABLE;
155 else if ( Event.State >>= aStrValue )
157 m_pToolbox->SetItemText( m_nID, aStrValue );
159 else if ( Event.State >>= aItemState )
161 eTri = TRISTATE_INDET;
162 nItemBits |= ToolBoxItemBits::CHECKABLE;
165 m_pToolbox->SetItemState( m_nID, eTri );
166 m_pToolbox->SetItemBits( m_nID, nItemBits );
170 IMPL_STATIC_LINK( GenericToolboxController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
174 // Asynchronous execution as this can lead to our own destruction!
175 // Framework can recycle our current frame and the layout manager disposes all user interface
176 // elements if a component gets detached from its frame!
177 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
179 catch ( Exception& )
182 delete pExecuteInfo;
183 return 0;
186 } // namespace
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */