LibreOffice Module framework (master) 1
buttontoolbarcontroller.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
22#include <com/sun/star/util/URLTransformer.hpp>
23#include <com/sun/star/frame/XDispatchProvider.hpp>
24#include <com/sun/star/beans/PropertyValue.hpp>
25#include <com/sun/star/lang/DisposedException.hpp>
26#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27
31#include <utility>
32#include <vcl/svapp.hxx>
33#include <vcl/toolbox.hxx>
34
35using namespace ::com::sun::star;
36using namespace css::awt;
37using namespace css::uno;
38using namespace css::beans;
39using namespace css::lang;
40using namespace css::frame;
41using namespace css::util;
42
43namespace framework
44{
45
47 uno::Reference< uno::XComponentContext > xContext,
48 ToolBox* pToolBar,
49 OUString aCommand ) :
50 m_bInitialized( false ),
51 m_bDisposed( false ),
52 m_aCommandURL(std::move( aCommand )),
53 m_xContext(std::move( xContext )),
54 m_pToolbar( pToolBar )
55{
56}
57
59{
60}
61
62 // XInterface
64{
65 Any a = ::cppu::queryInterface(
66 rType ,
67 static_cast< frame::XStatusListener* >( this ),
68 static_cast< frame::XToolbarController* >( this ),
69 static_cast< lang::XInitialization* >( this ),
70 static_cast< lang::XComponent* >( this ),
71 static_cast< util::XUpdatable* >( this ));
72
73 if ( a.hasValue() )
74 return a;
75
77}
78
79void SAL_CALL ButtonToolbarController::acquire() noexcept
80{
82}
83
84void SAL_CALL ButtonToolbarController::release() noexcept
85{
87}
88
89// XInitialization
91 const css::uno::Sequence< css::uno::Any >& aArguments )
92{
93 SolarMutexGuard aSolarMutexGuard;
94
95 if ( m_bDisposed )
96 throw DisposedException();
97
98 if ( m_bInitialized )
99 return;
100
101 m_bInitialized = true;
102
103 PropertyValue aPropValue;
104 for ( const css::uno::Any& rArg : aArguments )
105 {
106 if ( rArg >>= aPropValue )
107 {
108 if ( aPropValue.Name == "Frame" )
109 m_xFrame.set(aPropValue.Value,UNO_QUERY);
110 else if ( aPropValue.Name == "CommandURL" )
111 aPropValue.Value >>= m_aCommandURL;
112 else if ( aPropValue.Name == "ServiceManager" )
113 {
114 Reference<XMultiServiceFactory> xServiceManager(aPropValue.Value,UNO_QUERY);
116 }
117 }
118 }
119}
120
121// XComponent
123{
124 Reference< XComponent > xThis = this;
125
126 {
127 SolarMutexGuard aSolarMutexGuard;
128 if ( m_bDisposed )
129 return;
130
131 m_xContext.clear();
132 m_xURLTransformer.clear();
133 m_xFrame.clear();
135 m_bDisposed = true;
136 }
137}
138
140 const css::uno::Reference< css::lang::XEventListener >& )
141{
142 // do nothing
143}
144
146 const css::uno::Reference< css::lang::XEventListener >& )
147{
148 // do nothing
149}
150
151// XUpdatable
153{
154 SolarMutexGuard aSolarMutexGuard;
155 if ( m_bDisposed )
156 throw DisposedException();
157}
158
159// XEventListener
161 const css::lang::EventObject& Source )
162{
163 uno::Reference< uno::XInterface > xSource( Source.Source );
164
165 SolarMutexGuard aSolarMutexGuard;
166
167 if ( m_bDisposed )
168 return;
169
170 uno::Reference< uno::XInterface > xIfac( m_xFrame, uno::UNO_QUERY );
171 if ( xIfac == xSource )
172 m_xFrame.clear();
173}
174
175void SAL_CALL ButtonToolbarController::statusChanged( const css::frame::FeatureStateEvent& )
176{
177 // do nothing
178 if ( m_bDisposed )
179 throw DisposedException();
180}
181
182// XToolbarController
183void SAL_CALL ButtonToolbarController::execute( sal_Int16 KeyModifier )
184{
185 uno::Reference< frame::XDispatch > xDispatch;
186 uno::Reference< frame::XFrame > xFrame;
187 uno::Reference< util::XURLTransformer > xURLTransformer;
188 OUString aCommandURL;
189 css::util::URL aTargetURL;
190
191 {
192 SolarMutexGuard aSolarMutexGuard;
193
194 if ( m_bDisposed )
195 throw DisposedException();
196
197 if ( m_bInitialized &&
198 m_xFrame.is() &&
199 m_xContext.is() &&
200 !m_aCommandURL.isEmpty() )
201 {
202 if ( !m_xURLTransformer.is() )
203 {
204 m_xURLTransformer = util::URLTransformer::create( m_xContext );
205 }
206
208 aCommandURL = m_aCommandURL;
209 xURLTransformer = m_xURLTransformer;
210 }
211 }
212
213 uno::Reference< frame::XDispatchProvider > xDispatchProvider( xFrame, uno::UNO_QUERY );
214 if ( xDispatchProvider.is() )
215 {
216 aTargetURL.Complete = aCommandURL;
217 xURLTransformer->parseStrict( aTargetURL );
218 xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
219 }
220
221 if ( !xDispatch.is() )
222 return;
223
224 try
225 {
226 // Provide key modifier information to dispatch function
227 Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue("KeyModifier", KeyModifier) };
228
229 xDispatch->dispatch( aTargetURL, aArgs );
230 }
231 catch ( const DisposedException& )
232 {
233 }
234}
235
237{
238 SolarMutexGuard aSolarMutexGuard;
239
240 if ( m_bDisposed )
241 throw DisposedException();
242
243 sal_Int16 nKeyModifier( static_cast<sal_Int16>(m_pToolbar->GetModifier()) );
244 execute( nKeyModifier );
245}
246
248{
249 // do nothing
250 if ( m_bDisposed )
251 throw DisposedException();
252}
253
254uno::Reference< awt::XWindow > SAL_CALL ButtonToolbarController::createPopupWindow()
255{
256 if ( m_bDisposed )
257 throw DisposedException();
258
259 return uno::Reference< awt::XWindow >();
260}
261
262uno::Reference< awt::XWindow > SAL_CALL ButtonToolbarController::createItemWindow(
263 const css::uno::Reference< css::awt::XWindow >& )
264{
265 if ( m_bDisposed )
266 throw DisposedException();
267
268 return uno::Reference< awt::XWindow >();
269}
270
271} // namespace
272
273/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void clear()
virtual void SAL_CALL acquire() SAL_NOEXCEPT SAL_OVERRIDE
virtual void SAL_CALL release() SAL_NOEXCEPT SAL_OVERRIDE
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) SAL_OVERRIDE
virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override
css::uno::Reference< css::frame::XFrame > m_xFrame
virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createItemWindow(const css::uno::Reference< css::awt::XWindow > &Parent) override
virtual void SAL_CALL execute(sal_Int16 KeyModifier) override
css::uno::Reference< css::util::XURLTransformer > m_xURLTransformer
virtual void SAL_CALL click() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
virtual void SAL_CALL addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener) override
virtual void SAL_CALL doubleClick() override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void SAL_CALL update() override
ButtonToolbarController(css::uno::Reference< css::uno::XComponentContext > xContext, ToolBox *pToolBar, OUString aCommand)
virtual void SAL_CALL dispose() override
virtual void SAL_CALL release() noexcept override
virtual void SAL_CALL acquire() noexcept override
virtual void SAL_CALL removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener) override
Reference< XDispatch > xDispatch
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool m_bDisposed
Sequence< PropertyValue > aArguments
uno_Any a
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Reference< XFrame > xFrame
OUString aCommand
OUString aTargetURL