LibreOffice Module sfx2 (master) 1
tbxitem.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
20
21#ifdef __sun
22#include <ctime>
23#endif
24
25#include <com/sun/star/uno/Reference.h>
26#include <com/sun/star/frame/XFrame.hpp>
27#include <com/sun/star/awt/XWindow.hpp>
28#include <com/sun/star/util/URL.hpp>
29#include <com/sun/star/util/URLTransformer.hpp>
30#include <com/sun/star/util/XURLTransformer.hpp>
31#include <com/sun/star/frame/XController.hpp>
32#include <com/sun/star/frame/XDispatchProvider.hpp>
33#include <com/sun/star/lang/XUnoTunnel.hpp>
34#include <com/sun/star/beans/PropertyValue.hpp>
35#include <com/sun/star/frame/status/ItemStatus.hpp>
36#include <com/sun/star/ui/XUIElementFactory.hpp>
37#include <com/sun/star/frame/status/Visibility.hpp>
38#include <svl/eitem.hxx>
39#include <svl/stritem.hxx>
40#include <svl/intitem.hxx>
41#include <svl/visitem.hxx>
42#include <vcl/svapp.hxx>
43#include <vcl/toolbox.hxx>
44
48
50#include <sfx2/tbxctrl.hxx>
51#include <sfx2/dispatch.hxx>
52#include <sfx2/msg.hxx>
53#include <sfx2/msgpool.hxx>
54#include <sfx2/viewfrm.hxx>
55#include <sfx2/module.hxx>
56#include <sfx2/app.hxx>
57#include <unoctitm.hxx>
58
59using namespace ::com::sun::star;
60using namespace ::com::sun::star::beans;
61using namespace ::com::sun::star::frame;
62using namespace ::com::sun::star::frame::status;
63using namespace ::com::sun::star::lang;
64using namespace ::com::sun::star::uno;
65using namespace ::com::sun::star::util;
66using namespace ::com::sun::star::container;
67using namespace ::com::sun::star::ui;
68
69
71
72rtl::Reference<svt::ToolboxController> SfxToolBoxControllerFactory( const Reference< XFrame >& rFrame, ToolBox* pToolbox, ToolBoxItemId nID, const OUString& aCommandURL )
73{
74 SolarMutexGuard aGuard;
75
77 aTargetURL.Complete = aCommandURL;
78 Reference < XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
79 xTrans->parseStrict( aTargetURL );
80 if ( !aTargetURL.Arguments.isEmpty() )
81 return nullptr;
82
83 Reference < XController > xController;
84 Reference < XModel > xModel;
85 if ( rFrame.is() )
86 {
87 xController = rFrame->getController();
88 if ( xController.is() )
89 xModel = xController->getModel();
90 }
91
93 SfxModule* pModule = pObjShell ? pObjShell->GetModule() : nullptr;
94 SfxSlotPool* pSlotPool = nullptr;
95
96 if ( pModule )
97 pSlotPool = pModule->GetSlotPool();
98 else
99 pSlotPool = &(SfxSlotPool::GetSlotPool());
100
101 const SfxSlot* pSlot = pSlotPool->GetUnoSlot( aTargetURL.Path );
102 if ( pSlot )
103 {
104 sal_uInt16 nSlotId = pSlot->GetSlotId();
105 if ( nSlotId > 0 )
106 return SfxToolBoxControl::CreateControl( nSlotId, nID, pToolbox, pModule );
107 }
108
109 return nullptr;
110}
111
113{
117 sal_uInt16 nSlotId;
118};
119
121 sal_uInt16 nSlotID,
122 ToolBoxItemId nID,
123 ToolBox& rBox,
124 bool bShowStringItems )
125 : pImpl( new SfxToolBoxControl_Impl )
126{
127 pImpl->pBox = &rBox;
128 pImpl->bShowString = bShowStringItems;
129 pImpl->nTbxId = nID;
130 pImpl->nSlotId = nSlotID;
131}
132
133
135{
136}
137
138
140{
141 return *pImpl->pBox;
142}
144{
145 return pImpl->nTbxId;
146}
147unsigned short SfxToolBoxControl::GetSlotId() const
148{
149 return pImpl->nSlotId;
150}
151
152
154{
155 if ( m_bDisposed )
156 return;
157
159
160 // Remove and destroy our item window at our toolbox
161 SolarMutexGuard aGuard;
162 VclPtr< vcl::Window > pWindow = pImpl->pBox->GetItemWindow( pImpl->nTbxId );
163 pImpl->pBox->SetItemWindow( pImpl->nTbxId, nullptr );
164 pWindow.disposeAndClear();
165}
166
167
169{
170 SfxGetpApp()->RegisterToolBoxControl_Impl( pMod, rFact );
171}
172
174{
175 SolarMutexGuard aGuard;
176
177 SfxApplication *pApp = SfxGetpApp();
178
179 SfxSlotPool *pSlotPool;
180 if ( pMod )
181 pSlotPool = pMod->GetSlotPool();
182 else
183 pSlotPool = &SfxSlotPool::GetSlotPool();
184 const std::type_info* aSlotType = pSlotPool->GetSlotType( nSlotId );
185 if ( aSlotType )
186 {
187 if ( pMod )
188 {
189 SfxTbxCtrlFactory *pFact = pMod->GetTbxCtrlFactory(*aSlotType, nSlotId);
190 if ( pFact )
191 return pFact->pCtor( nSlotId, nTbxId, *pBox );
192 }
193
194 SfxTbxCtrlFactory* pFact = pApp->GetTbxCtrlFactory(*aSlotType, nSlotId);
195 if (pFact)
196 return pFact->pCtor( nSlotId, nTbxId, *pBox );
197 }
198
199 return nullptr;
200}
201
203 const SfxPoolItem* pState )
204/* [Description]
205
206 Static method for determining the status of the SfxPoolItem-pointer,
207 used in the method <SfxControllerItem::StateChanged(const SfxPoolItem*)>.
208
209 [Return value]
210
211 SfxItemState SfxItemState::UNKNOWN
212 Enabled, however no further status information is available.
213 Typical for <Slot>s, which are temporarily disabled a
214 anyway but other than that do not change their appearance.
215
216 SfxItemState::DISABLED
217 Disabled, no further status information is available.
218 All other displayed values should be reset to the default
219 if possible.
220
221 SfxItemState::DONTCARE
222 Enabled but there were only ambiguous values available
223 (i.e. none that could be queried).
224
225 SfxItemState::DEFAULT
226 Enabled and with available values which can be queried
227 through'pState'. The type is thus by the Slot clearly
228 defined in the entire Program.
229*/
230
231{
232 return !pState
233 ? SfxItemState::DISABLED
234 : IsInvalidItem(pState)
235 ? SfxItemState::DONTCARE
236 : pState->IsVoidItem() && !pState->Which()
237 ? SfxItemState::UNKNOWN
238 : SfxItemState::DEFAULT;
239}
240
242 const Reference< XDispatchProvider >& rProvider,
243 const OUString& rCommand,
244 Sequence< ::PropertyValue > const & aArgs )
245{
246 if ( rProvider.is() )
247 {
248 css::util::URL aTargetURL;
249 aTargetURL.Complete = rCommand;
250 Reference < XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
251 xTrans->parseStrict( aTargetURL );
252
253 Reference < XDispatch > xDispatch = rProvider->queryDispatch( aTargetURL, OUString(), 0 );
254 if ( xDispatch.is() )
255 xDispatch->dispatch( aTargetURL, aArgs );
256 }
257}
258
259void SfxToolBoxControl::Dispatch( const OUString& aCommand, css::uno::Sequence< css::beans::PropertyValue > const & aArgs )
260{
261 Reference < XController > xController;
262
263 SolarMutexGuard aGuard;
264 if ( getFrameInterface().is() )
265 xController = getFrameInterface()->getController();
266
267 Reference < XDispatchProvider > xProvider( xController, UNO_QUERY );
268 if ( xProvider.is() )
269 {
270 css::util::URL aTargetURL;
271 aTargetURL.Complete = aCommand;
272 getURLTransformer()->parseStrict( aTargetURL );
273
274 Reference < XDispatch > xDispatch = xProvider->queryDispatch( aTargetURL, OUString(), 0 );
275 if ( xDispatch.is() )
276 xDispatch->dispatch( aTargetURL, aArgs );
277 }
278}
279
280// XStatusListener
281void SAL_CALL SfxToolBoxControl::statusChanged( const FeatureStateEvent& rEvent )
282{
283 SfxViewFrame* pViewFrame = nullptr;
284 Reference < XController > xController;
285
286 SolarMutexGuard aGuard;
287 if ( getFrameInterface().is() )
288 xController = getFrameInterface()->getController();
289
290 Reference < XDispatchProvider > xProvider( xController, UNO_QUERY );
291 if ( xProvider.is() )
292 {
293 Reference < XDispatch > xDisp = xProvider->queryDispatch( rEvent.FeatureURL, OUString(), 0 );
294 if ( xDisp.is() )
295 {
296 if (auto pDisp = dynamic_cast<SfxOfficeDispatch*>(xDisp.get()))
297 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
298 }
299 }
300
301 sal_uInt16 nSlotId = 0;
302 SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame );
303 const SfxSlot* pSlot = rPool.GetUnoSlot( rEvent.FeatureURL.Path );
304 if ( pSlot )
305 nSlotId = pSlot->GetSlotId();
306 else if ( m_aCommandURL == rEvent.FeatureURL.Path )
307 nSlotId = GetSlotId();
308
309 if ( nSlotId <= 0 )
310 return;
311
312 if ( rEvent.Requery )
314 else
315 {
316 SfxItemState eState = SfxItemState::DISABLED;
317 std::unique_ptr<SfxPoolItem> pItem;
318 if ( rEvent.IsEnabled )
319 {
320 eState = SfxItemState::DEFAULT;
321 css::uno::Type aType = rEvent.State.getValueType();
322
323 if ( aType == cppu::UnoType<void>::get() )
324 {
325 pItem.reset(new SfxVoidItem( nSlotId ));
326 eState = SfxItemState::UNKNOWN;
327 }
328 else if ( aType == cppu::UnoType<bool>::get() )
329 {
330 bool bTemp = false;
331 rEvent.State >>= bTemp ;
332 pItem.reset(new SfxBoolItem( nSlotId, bTemp ));
333 }
335 {
336 sal_uInt16 nTemp = 0;
337 rEvent.State >>= nTemp ;
338 pItem.reset(new SfxUInt16Item( nSlotId, nTemp ));
339 }
340 else if ( aType == cppu::UnoType<sal_uInt32>::get() )
341 {
342 sal_uInt32 nTemp = 0;
343 rEvent.State >>= nTemp ;
344 pItem.reset(new SfxUInt32Item( nSlotId, nTemp ));
345 }
346 else if ( aType == cppu::UnoType<OUString>::get() )
347 {
348 OUString sTemp ;
349 rEvent.State >>= sTemp ;
350 pItem.reset(new SfxStringItem( nSlotId, sTemp ));
351 }
353 {
354 ItemStatus aItemStatus;
355 rEvent.State >>= aItemStatus;
356 SfxItemState tmpState = static_cast<SfxItemState>(aItemStatus.State);
357 // make sure no-one tries to send us a combination of states
358 if (tmpState != SfxItemState::UNKNOWN && tmpState != SfxItemState::DISABLED &&
359 tmpState != SfxItemState::DONTCARE &&
360 tmpState != SfxItemState::DEFAULT && tmpState != SfxItemState::SET)
361 throw css::uno::RuntimeException("unknown status");
362 eState = tmpState;
363 pItem.reset(new SfxVoidItem( nSlotId ));
364 }
366 {
367 Visibility aVisibilityStatus;
368 rEvent.State >>= aVisibilityStatus;
369 pItem.reset(new SfxVisibilityItem( nSlotId, aVisibilityStatus.bVisible ));
370 }
371 else
372 {
373 if ( pSlot )
374 pItem = pSlot->GetType()->CreateItem();
375 if ( pItem )
376 {
377 pItem->SetWhich( nSlotId );
378 pItem->PutValue( rEvent.State, 0 );
379 }
380 else
381 pItem.reset(new SfxVoidItem( nSlotId ));
382 }
383 }
384
385 StateChangedAtToolBoxControl( nSlotId, eState, pItem.get() );
386 }
387}
388
389// XToolbarController
390void SAL_CALL SfxToolBoxControl::execute( sal_Int16 KeyModifier )
391{
392 SolarMutexGuard aGuard;
393 Select( static_cast<sal_uInt16>(KeyModifier) );
394}
395
397{
398 SolarMutexGuard aGuard;
399 Click();
400}
401
403{
404 SolarMutexGuard aGuard;
405 DoubleClick();
406}
407
408Reference< css::awt::XWindow > SAL_CALL SfxToolBoxControl::createPopupWindow()
409{
410 SolarMutexGuard aGuard;
412 return nullptr;
413}
414
415Reference< css::awt::XWindow > SAL_CALL SfxToolBoxControl::createItemWindow( const Reference< css::awt::XWindow >& rParent )
416{
417 SolarMutexGuard aGuard;
419}
420
422(
423 sal_uInt16 /*nSlotId*/,
424 SfxItemState eState,
425 const SfxPoolItem* pState
426)
427{
428 DBG_ASSERT( pImpl->pBox != nullptr, "setting state to dangling ToolBox" );
429
430 // enabled/disabled-Flag correcting the lump sum
431 pImpl->pBox->EnableItem( GetId(), eState != SfxItemState::DISABLED );
432
433 ToolBoxItemBits nItemBits = pImpl->pBox->GetItemBits( GetId() );
434 nItemBits &= ~ToolBoxItemBits::CHECKABLE;
436 switch ( eState )
437 {
438 case SfxItemState::DEFAULT:
439 if ( pState )
440 {
441 if ( auto pBoolItem = dynamic_cast< const SfxBoolItem* >(pState) )
442 {
443 // BoolItem for checking
444 if ( pBoolItem->GetValue() )
445 eTri = TRISTATE_TRUE;
446 nItemBits |= ToolBoxItemBits::CHECKABLE;
447 }
448 else if ( auto pEnumItem = dynamic_cast< const SfxEnumItemInterface *>( pState ) )
449 {
450 if (pEnumItem->HasBoolValue())
451 {
452 // EnumItem is handled as Bool
453 if (pEnumItem->GetBoolValue())
454 eTri = TRISTATE_TRUE;
455 nItemBits |= ToolBoxItemBits::CHECKABLE;
456 }
457 }
458 else if ( pImpl->bShowString )
459 {
460 if (auto pStringItem = dynamic_cast< const SfxStringItem *>( pState ) )
461 pImpl->pBox->SetItemText(GetId(), pStringItem->GetValue() );
462 }
463 }
464 break;
465
466 case SfxItemState::DONTCARE:
467 {
468 eTri = TRISTATE_INDET;
469 nItemBits |= ToolBoxItemBits::CHECKABLE;
470 }
471 break;
472
473 default: break; // do nothing
474 }
475
476 pImpl->pBox->SetItemState( GetId(), eTri );
477 pImpl->pBox->SetItemBits( GetId(), nItemBits );
478}
479
480
481void SfxToolBoxControl::Select( sal_uInt16 nSelectModifier )
482{
483 svt::ToolboxController::execute( nSelectModifier );
484}
485
486
488{
489}
490
491
493{
494}
495
497{
498}
499
501{
503}
504
505/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxApplication * SfxGetpApp()
Definition: app.hxx:231
SAL_DLLPRIVATE SfxTbxCtrlFactory * GetTbxCtrlFactory(const std::type_info &rSlotType, sal_uInt16 nSlotID) const
Definition: app.cxx:335
SAL_DLLPRIVATE void RegisterToolBoxControl_Impl(SfxModule *, const SfxTbxCtrlFactory &)
Definition: appreg.cxx:58
SAL_DLLPRIVATE SfxTbxCtrlFactory * GetTbxCtrlFactory(const std::type_info &rSlotType, sal_uInt16 nSlotID) const
Definition: module.cxx:162
SfxSlotPool * GetSlotPool() const
Definition: module.cxx:102
static SfxObjectShell * GetShellFromComponent(const css::uno::Reference< css::uno::XInterface > &xComp)
Definition: objxtor.cxx:1039
SfxModule * GetModule() const
Definition: objmisc.cxx:1321
sal_uInt16 Which() const
virtual bool IsVoidItem() const
static SfxSlotPool & GetSlotPool(SfxViewFrame *pFrame=nullptr)
Definition: msgpool.cxx:316
const std::type_info * GetSlotType(sal_uInt16 nSlotId) const
Definition: msgpool.cxx:146
const SfxSlot * GetUnoSlot(const OUString &rUnoName) const
Definition: msgpool.cxx:300
Definition: msg.hxx:184
sal_uInt16 GetSlotId() const
Definition: msg.hxx:253
const SfxType * GetType() const
Definition: msg.hxx:236
virtual void Select(sal_uInt16 nSelectModifier)
Definition: tbxitem.cxx:481
void Dispatch(const OUString &aCommand, css::uno::Sequence< css::beans::PropertyValue > const &aArgs)
Definition: tbxitem.cxx:259
virtual void Click()
Definition: tbxitem.cxx:492
ToolBoxItemId GetId() const
Definition: tbxitem.cxx:143
virtual void SAL_CALL dispose() override
Definition: tbxitem.cxx:153
virtual ~SfxToolBoxControl() override
Definition: tbxitem.cxx:134
static void RegisterToolBoxControl(SfxModule *, const SfxTbxCtrlFactory &)
Definition: tbxitem.cxx:168
std::unique_ptr< SfxToolBoxControl_Impl > pImpl
Definition: tbxctrl.hxx:76
unsigned short GetSlotId() const
Definition: tbxitem.cxx:147
virtual void CreatePopupWindow()
Definition: tbxitem.cxx:496
virtual void SAL_CALL execute(sal_Int16 KeyModifier) override
Definition: tbxitem.cxx:390
virtual VclPtr< InterimItemWindow > CreateItemWindow(vcl::Window *pParent)
Definition: tbxitem.cxx:500
ToolBox & GetToolBox() const
Definition: tbxitem.cxx:139
virtual void DoubleClick()
Definition: tbxitem.cxx:487
static rtl::Reference< SfxToolBoxControl > CreateControl(sal_uInt16 nSlotId, ToolBoxItemId nTbxId, ToolBox *pBox, SfxModule const *pMod)
Definition: tbxitem.cxx:173
SfxToolBoxControl(sal_uInt16 nSlotID, ToolBoxItemId nId, ToolBox &rBox, bool bShowStrings=false)
Definition: tbxitem.cxx:120
virtual void StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState)
Definition: tbxitem.cxx:422
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
Definition: tbxitem.cxx:281
virtual void SAL_CALL click() override
Definition: tbxitem.cxx:396
static SfxItemState GetItemState(const SfxPoolItem *pState)
Definition: tbxitem.cxx:202
virtual void SAL_CALL doubleClick() override
Definition: tbxitem.cxx:402
virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override
Definition: tbxitem.cxx:408
virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createItemWindow(const css::uno::Reference< css::awt::XWindow > &rParent) override
Definition: tbxitem.cxx:415
SfxFrame & GetFrame() const
Definition: viewfrm.cxx:2782
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
void disposeAndClear()
virtual void SAL_CALL dispose() override
css::uno::Reference< css::frame::XFrame > getFrameInterface() const
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override=0
const css::uno::Reference< css::util::XURLTransformer > & getURLTransformer() const
virtual void SAL_CALL execute(sal_Int16 KeyModifier) override
#define DBG_ASSERT(sCon, aError)
Reference< XDispatch > xDispatch
TriState
TRISTATE_FALSE
TRISTATE_INDET
TRISTATE_TRUE
Visibility
SfxItemState
bool IsInvalidItem(const SfxPoolItem *pItem)
SfxToolBoxControlCtor pCtor
Definition: tbxctrl.hxx:45
VclPtr< ToolBox > pBox
Definition: tbxitem.cxx:114
ToolBoxItemId nTbxId
Definition: tbxitem.cxx:116
std::unique_ptr< SfxPoolItem > CreateItem() const
Definition: msg.hxx:111
Reference< XController > xController
Reference< XModel > xModel
rtl::Reference< svt::ToolboxController > SfxToolBoxControllerFactory(const Reference< XFrame > &rFrame, ToolBox *pToolbox, ToolBoxItemId nID, const OUString &aCommandURL)
Definition: tbxitem.cxx:72
SFX_IMPL_TOOLBOX_CONTROL_ARG(SfxToolBoxControl, SfxStringItem, true)
OUString aCommand
OUString aTargetURL
ToolBoxItemBits