LibreOffice Module framework (master) 1
complextoolbarcontroller.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/util/XURLTransformer.hpp>
24#include <com/sun/star/beans/PropertyValue.hpp>
25#include <com/sun/star/lang/DisposedException.hpp>
26#include <com/sun/star/frame/status/ItemStatus.hpp>
27#include <com/sun/star/frame/status/Visibility.hpp>
28#include <com/sun/star/frame/XControlNotificationListener.hpp>
29#include <com/sun/star/frame/XFrame.hpp>
30
33#include <vcl/svapp.hxx>
34#include <vcl/mnemonic.hxx>
35#include <vcl/toolbox.hxx>
36
37using namespace ::com::sun::star;
38using namespace css::awt;
39using namespace css::uno;
40using namespace css::beans;
41using namespace css::lang;
42using namespace css::frame;
43using namespace css::frame::status;
44using namespace css::util;
45
46namespace framework
47{
48
50 const Reference< XComponentContext >& rxContext,
51 const Reference< XFrame >& rFrame,
52 ToolBox* pToolbar,
53 ToolBoxItemId nID,
54 const OUString& aCommand ) :
55 svt::ToolboxController( rxContext, rFrame, aCommand )
56 , m_xToolbar( pToolbar )
57 , m_nID( nID )
58 , m_bMadeInvisible( false )
59{
60 m_xURLTransformer.set( URLTransformer::create(m_xContext) );
61}
62
64{
65}
66
68{
69 SolarMutexGuard aSolarMutexGuard;
70
71 m_xToolbar->SetItemWindow( m_nID, nullptr );
73
74 m_xURLTransformer.clear();
77}
78
79Sequence<PropertyValue> ComplexToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
80{
81 // Add key modifier to argument list
82 Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue("KeyModifier", KeyModifier) };
83 return aArgs;
84}
85
86void SAL_CALL ComplexToolbarController::execute( sal_Int16 KeyModifier )
87{
88 Reference< XDispatch > xDispatch;
89 Reference< XURLTransformer > xURLTransformer;
90 css::util::URL aTargetURL;
91 Sequence<PropertyValue> aArgs;
92
93 {
94 SolarMutexGuard aSolarMutexGuard;
95
96 if ( m_bDisposed )
97 throw DisposedException();
98
99 if ( m_bInitialized &&
100 m_xFrame.is() &&
101 !m_aCommandURL.isEmpty() )
102 {
103 xURLTransformer = m_xURLTransformer;
106 aArgs = getExecuteArgs(KeyModifier);
107 }
108 }
109
110 if ( xDispatch.is() && !aTargetURL.Complete.isEmpty() )
111 {
112 // Execute dispatch asynchronously
113 ExecuteInfo* pExecuteInfo = new ExecuteInfo;
114 pExecuteInfo->xDispatch = xDispatch;
115 pExecuteInfo->aTargetURL = aTargetURL;
116 pExecuteInfo->aArgs = aArgs;
117 Application::PostUserEvent( LINK(nullptr, ComplexToolbarController , ExecuteHdl_Impl), pExecuteInfo );
118 }
119}
120
121void ComplexToolbarController::statusChanged( const FeatureStateEvent& Event )
122{
123 SolarMutexGuard aSolarMutexGuard;
124
125 if ( m_bDisposed )
126 return;
127
128 if ( !m_xToolbar )
129 return;
130
131 m_xToolbar->EnableItem( m_nID, Event.IsEnabled );
132
133 ToolBoxItemBits nItemBits = m_xToolbar->GetItemBits( m_nID );
134 nItemBits &= ~ToolBoxItemBits::CHECKABLE;
136
137 bool bValue;
138 OUString aStrValue;
139 ItemStatus aItemState;
140 Visibility aItemVisibility;
141 ControlCommand aControlCommand;
142
143 if ( Event.State >>= bValue )
144 {
145 // Boolean, treat it as checked/unchecked
146 if ( m_bMadeInvisible )
147 m_xToolbar->ShowItem( m_nID );
148 m_xToolbar->CheckItem( m_nID, bValue );
149 if ( bValue )
150 eTri = TRISTATE_TRUE;
151 nItemBits |= ToolBoxItemBits::CHECKABLE;
152 }
153 else if ( Event.State >>= aStrValue )
154 {
155 OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
156 m_xToolbar->SetItemText( m_nID, aText );
157 m_xToolbar->SetQuickHelpText( m_nID, aText );
158
159 if ( m_bMadeInvisible )
160 m_xToolbar->ShowItem( m_nID );
161 }
162 else if ( Event.State >>= aItemState )
163 {
164 eTri = TRISTATE_INDET;
165 nItemBits |= ToolBoxItemBits::CHECKABLE;
166 if ( m_bMadeInvisible )
167 m_xToolbar->ShowItem( m_nID );
168 }
169 else if ( Event.State >>= aItemVisibility )
170 {
171 m_xToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
172 m_bMadeInvisible = !aItemVisibility.bVisible;
173 }
174 else if ( Event.State >>= aControlCommand )
175 {
176 if (aControlCommand.Command == "SetQuickHelpText")
177 {
178 for (NamedValue const & rArg : std::as_const(aControlCommand.Arguments))
179 {
180 if (rArg.Name == "HelpText")
181 {
182 OUString aHelpText;
183 rArg.Value >>= aHelpText;
184 m_xToolbar->SetQuickHelpText(m_nID, aHelpText);
185 break;
186 }
187 }
188 }
189 else
190 {
191 executeControlCommand( aControlCommand );
192 }
193 if ( m_bMadeInvisible )
194 m_xToolbar->ShowItem( m_nID );
195 }
196
197 else if ( m_bMadeInvisible )
198 m_xToolbar->ShowItem( m_nID );
199
200 m_xToolbar->SetItemState( m_nID, eTri );
201 m_xToolbar->SetItemBits( m_nID, nItemBits );
202}
203
204IMPL_STATIC_LINK( ComplexToolbarController, ExecuteHdl_Impl, void*, p, void )
205{
206 ExecuteInfo* pExecuteInfo = static_cast<ExecuteInfo*>(p);
207 SolarMutexReleaser aReleaser;
208 try
209 {
210 // Asynchronous execution as this can lead to our own destruction!
211 // Framework can recycle our current frame and the layout manager disposes all user interface
212 // elements if a component gets detached from its frame!
213 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
214 }
215 catch ( const Exception& )
216 {
217 }
218
219 delete pExecuteInfo;
220}
221
222IMPL_STATIC_LINK( ComplexToolbarController, Notify_Impl, void*, p, void )
223{
224 NotifyInfo* pNotifyInfo = static_cast<NotifyInfo*>(p);
225 SolarMutexReleaser aReleaser;
226 try
227 {
228 // Asynchronous execution: As this can lead to our own destruction!
229 // Framework can recycle our current frame and the layout manager disposes all user interface
230 // elements if a component gets detached from its frame!
231 frame::ControlEvent aEvent;
232 aEvent.aURL = pNotifyInfo->aSourceURL;
233 aEvent.Event = pNotifyInfo->aEventName;
234 aEvent.aInformation = pNotifyInfo->aInfoSeq;
235 pNotifyInfo->xNotifyListener->controlEvent( aEvent );
236 }
237 catch ( const Exception& )
238 {
239 }
240
241 delete pNotifyInfo;
242}
243
245 const OUString& aEventName,
246 const uno::Reference< frame::XDispatch >& xDispatch,
247 const uno::Sequence< beans::NamedValue >& rInfo )
248{
249 uno::Reference< frame::XControlNotificationListener > xControlNotify( xDispatch, uno::UNO_QUERY );
250
251 if ( !xControlNotify.is() )
252 return;
253
254 // Execute notification asynchronously
255 NotifyInfo* pNotifyInfo = new NotifyInfo;
256
257 pNotifyInfo->aEventName = aEventName;
258 pNotifyInfo->xNotifyListener = xControlNotify;
259 pNotifyInfo->aSourceURL = getInitializedURL();
260
261 // Add frame as source to the information sequence
262 sal_Int32 nCount = rInfo.getLength();
263 uno::Sequence< beans::NamedValue > aInfoSeq( rInfo );
264 aInfoSeq.realloc( nCount+1 );
265 auto pInfoSeq = aInfoSeq.getArray();
266 pInfoSeq[nCount].Name = "Source";
267 pInfoSeq[nCount].Value <<= getFrameInterface();
268 pNotifyInfo->aInfoSeq = aInfoSeq;
269
270 Application::PostUserEvent( LINK(nullptr, ComplexToolbarController, Notify_Impl), pNotifyInfo );
271}
272
273uno::Reference< frame::XDispatch > ComplexToolbarController::getDispatchFromCommand( const OUString& aCommand ) const
274{
275 uno::Reference< frame::XDispatch > xDispatch;
276
277 if ( m_bInitialized && m_xFrame.is() && !aCommand.isEmpty() )
278 {
279 URLToDispatchMap::const_iterator pIter = m_aListenerMap.find( aCommand );
280 if ( pIter != m_aListenerMap.end() )
281 xDispatch = pIter->second;
282 }
283
284 return xDispatch;
285}
286
288{
289 if ( m_aURL.Complete.isEmpty() )
290 {
291 m_aURL.Complete = m_aCommandURL;
292 m_xURLTransformer->parseStrict( m_aURL );
293 }
294 return m_aURL;
295}
296
298{
299 // send focus get notification
300 uno::Sequence< beans::NamedValue > aInfo;
301 addNotifyInfo( "FocusSet",
303 aInfo );
304}
305
307{
308 // send focus lost notification
309 uno::Sequence< beans::NamedValue > aInfo;
310 addNotifyInfo( "FocusLost",
312 aInfo );
313}
314
316{
317 // send text changed notification
318 uno::Sequence< beans::NamedValue > aInfo { { "Text", css::uno::Any(aText) } };
319 addNotifyInfo( "TextChanged",
321 aInfo );
322}
323
324} // namespace
325
326/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static OUString EraseAllMnemonicChars(const OUString &rStr)
void clear()
css::uno::Reference< css::frame::XDispatch > getDispatchFromCommand(const OUString &aCommand) const
css::uno::Reference< css::util::XURLTransformer > m_xURLTransformer
virtual void executeControlCommand(const css::frame::ControlCommand &rControlCommand)=0
virtual void SAL_CALL dispose() override
void addNotifyInfo(const OUString &aEventName, const css::uno::Reference< css::frame::XDispatch > &xDispatch, const css::uno::Sequence< css::beans::NamedValue > &rInfo)
virtual css::uno::Sequence< css::beans::PropertyValue > getExecuteArgs(sal_Int16 KeyModifier) const
ComplexToolbarController(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::frame::XFrame > &rFrame, ToolBox *pToolBar, ToolBoxItemId nID, const OUString &aCommand)
virtual void SAL_CALL execute(sal_Int16 KeyModifier) override
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
virtual void SAL_CALL dispose() override
URLToDispatchMap m_aListenerMap
css::uno::Reference< css::frame::XFrame > getFrameInterface() const
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Reference< css::frame::XFrame > m_xFrame
int nCount
Reference< XDispatch > xDispatch
TriState
TRISTATE_FALSE
TRISTATE_INDET
TRISTATE_TRUE
void * p
@ Exception
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
IMPL_STATIC_LINK(ComplexToolbarController, ExecuteHdl_Impl, void *, p, void)
Visibility
css::uno::Sequence< css::beans::PropertyValue > aArgs
css::uno::Reference< css::frame::XDispatch > xDispatch
css::uno::Reference< css::frame::XControlNotificationListener > xNotifyListener
css::uno::Sequence< css::beans::NamedValue > aInfoSeq
OUString aCommand
OUString aTargetURL
ToolBoxItemBits