LibreOffice Module sfx2 (master) 1
stbitem.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#include <svl/stritem.hxx>
21#include <com/sun/star/util/URL.hpp>
22#include <com/sun/star/util/URLTransformer.hpp>
23#include <com/sun/star/util/XURLTransformer.hpp>
24#include <com/sun/star/frame/XController.hpp>
25#include <com/sun/star/frame/XDispatchProvider.hpp>
26#include <com/sun/star/lang/XUnoTunnel.hpp>
27#include <com/sun/star/frame/status/ItemStatus.hpp>
28#include <com/sun/star/awt/MouseButton.hpp>
29
30#include <vcl/commandevent.hxx>
31#include <vcl/event.hxx>
32#include <vcl/status.hxx>
33#include <vcl/svapp.hxx>
34
35#include <sfx2/app.hxx>
36#include <sfx2/stbitem.hxx>
37#include <sfx2/msg.hxx>
38#include <sfx2/msgpool.hxx>
39#include <sfx2/module.hxx>
40#include <sfx2/dispatch.hxx>
41#include <unoctitm.hxx>
42#include <sfx2/objsh.hxx>
43
46#include <svl/eitem.hxx>
47#include <svl/intitem.hxx>
50
51using namespace ::com::sun::star;
52
53
54sal_uInt16 SfxStatusBarControl::convertAwtToVCLMouseButtons( sal_Int16 nAwtMouseButtons )
55{
56 sal_uInt16 nVCLMouseButtons( 0 );
57
58 if ( nAwtMouseButtons & awt::MouseButton::LEFT )
59 nVCLMouseButtons |= MOUSE_LEFT;
60 if ( nAwtMouseButtons & awt::MouseButton::RIGHT )
61 nVCLMouseButtons |= MOUSE_RIGHT;
62 if ( nAwtMouseButtons & awt::MouseButton::MIDDLE )
63 nVCLMouseButtons |= MOUSE_MIDDLE;
64
65 return nVCLMouseButtons;
66}
67
68
70 const uno::Reference< frame::XFrame >& rFrame,
71 StatusBar* pStatusBar,
72 unsigned short nID,
73 const OUString& aCommandURL )
74{
75 SolarMutexGuard aGuard;
76
77 util::URL aTargetURL;
78 aTargetURL.Complete = aCommandURL;
79 uno::Reference < util::XURLTransformer > xTrans( util::URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
80 xTrans->parseStrict( aTargetURL );
81
82 uno::Reference < frame::XController > xController;
83 uno::Reference < frame::XModel > xModel;
84 if ( rFrame.is() )
85 {
86 xController = rFrame->getController();
87 if ( xController.is() )
88 xModel = xController->getModel();
89 }
90
92
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 {
107 OUString aCmd = pSlot->GetCommand();
108 pStatusBar->SetHelpId( nSlotId, aCmd );
109 return SfxStatusBarControl::CreateControl( nSlotId, nID, pStatusBar, pModule );
110 }
111 }
112
113 return nullptr;
114}
115
116
118(
119 sal_uInt16 nSlotID, /* Slot-Id which is connected to this
120 instance. If a Slot-Id is set to != 0 at
121 registration it will always be set there.
122 */
123
124
125 sal_uInt16 nCtrlID, /* ID of this controller in the status bar */
126
127 StatusBar& rBar /* Reference to the StatusBar,for which
128 this Control was created. */
129)
130
131
132/* [Description]
133
134 Constructor of the SfxStatusBarControl Class. The Subclasses are
135 created at the Factory if necessary.
136
137 Instances of this base class are created for all StatusBar-fields
138 for which no specific ones have been registered.
139*/
140
141: nSlotId( nSlotID ),
142 nId( nCtrlID ),
143 pBar( &rBar )
144{
145}
146
147
149
150/* [Description]
151
152 Destructor of the SfxStatusBarControl Class. The Class and its Subclasses
153 are destroyed by SFx.
154*/
155
156{}
157
158
159// XInterface
160void SAL_CALL SfxStatusBarControl::acquire() noexcept
161{
162 OWeakObject::acquire();
163}
164
165void SAL_CALL SfxStatusBarControl::release() noexcept
166{
167 OWeakObject::release();
168}
169
170
171// XStatusListener
172void SAL_CALL SfxStatusBarControl::statusChanged( const frame::FeatureStateEvent& rEvent )
173{
174 SfxViewFrame* pViewFrame = nullptr;
175 uno::Reference < frame::XController > xController;
176
177 SolarMutexGuard aGuard;
178 if ( m_xFrame.is() )
179 xController = m_xFrame->getController();
180
181 uno::Reference < frame::XDispatchProvider > xProvider( xController, uno::UNO_QUERY );
182 if ( xProvider.is() )
183 {
184 uno::Reference < frame::XDispatch > xDisp = xProvider->queryDispatch( rEvent.FeatureURL, OUString(), 0 );
185 if ( xDisp.is() )
186 {
187 if (auto pDisp = dynamic_cast<SfxOfficeDispatch*>(xDisp.get()))
188 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
189 }
190 }
191
192 sal_uInt16 nSlotID = 0;
193 SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame );
194 const SfxSlot* pSlot = rPool.GetUnoSlot( rEvent.FeatureURL.Path );
195 if ( pSlot )
196 nSlotID = pSlot->GetSlotId();
197
198 if ( nSlotID <= 0 )
199 return;
200
201 if ( rEvent.Requery )
203 else
204 {
205 SfxItemState eState = SfxItemState::DISABLED;
206 std::unique_ptr<SfxPoolItem> pItem;
207 if ( rEvent.IsEnabled )
208 {
209 eState = SfxItemState::DEFAULT;
210 uno::Type aType = rEvent.State.getValueType();
211
212 if ( aType == cppu::UnoType<void>::get() )
213 {
214 pItem.reset( new SfxVoidItem( nSlotID ) );
215 eState = SfxItemState::UNKNOWN;
216 }
217 else if ( aType == cppu::UnoType<bool>::get() )
218 {
219 bool bTemp = false;
220 rEvent.State >>= bTemp ;
221 pItem.reset( new SfxBoolItem( nSlotID, bTemp ) );
222 }
224 {
225 sal_uInt16 nTemp = 0;
226 rEvent.State >>= nTemp ;
227 pItem.reset( new SfxUInt16Item( nSlotID, nTemp ) );
228 }
229 else if ( aType == cppu::UnoType<sal_uInt32>::get() )
230 {
231 sal_uInt32 nTemp = 0;
232 rEvent.State >>= nTemp ;
233 pItem.reset( new SfxUInt32Item( nSlotID, nTemp ) );
234 }
235 else if ( aType == cppu::UnoType<OUString>::get() )
236 {
237 OUString sTemp ;
238 rEvent.State >>= sTemp ;
239 pItem.reset( new SfxStringItem( nSlotID, sTemp ) );
240 }
242 {
243 frame::status::ItemStatus aItemStatus;
244 rEvent.State >>= aItemStatus;
245 eState = static_cast<SfxItemState>(aItemStatus.State);
246 pItem.reset( new SfxVoidItem( nSlotID ) );
247 }
248 else
249 {
250 if ( pSlot )
251 pItem = pSlot->GetType()->CreateItem();
252 if ( pItem )
253 {
254 pItem->SetWhich( nSlotID );
255 pItem->PutValue( rEvent.State, 0 );
256 }
257 else
258 pItem.reset( new SfxVoidItem( nSlotID ) );
259 }
260 }
261
262 StateChangedAtStatusBarControl( nSlotID, eState, pItem.get() );
263 }
264}
265
266// XStatusbarController
268 const awt::MouseEvent& rMouseEvent )
269{
270 SolarMutexGuard aGuard;
271 ::Point aPos( rMouseEvent.X, rMouseEvent.Y );
272
273 ::MouseEvent aMouseEvent( aPos,
274 static_cast<sal_uInt16>(rMouseEvent.ClickCount),
275 MouseEventModifiers::NONE,
276 convertAwtToVCLMouseButtons( rMouseEvent.Buttons ),
277 0 );
278
279 return MouseButtonDown( aMouseEvent );
280}
281
283 const awt::MouseEvent& rMouseEvent )
284{
285 SolarMutexGuard aGuard;
286 ::Point aPos( rMouseEvent.X, rMouseEvent.Y );
287
288 ::MouseEvent aMouseEvent( aPos,
289 static_cast<sal_uInt16>(rMouseEvent.ClickCount),
290 MouseEventModifiers::NONE,
291 convertAwtToVCLMouseButtons( rMouseEvent.Buttons ),
292 0 );
293 return MouseMove( aMouseEvent );
294}
295
297 const ::awt::MouseEvent& rMouseEvent )
298{
299 SolarMutexGuard aGuard;
300 ::Point aPos( rMouseEvent.X, rMouseEvent.Y );
301
302 ::MouseEvent aMouseEvent( aPos,
303 static_cast<sal_uInt16>(rMouseEvent.ClickCount),
304 MouseEventModifiers::NONE,
305 convertAwtToVCLMouseButtons( rMouseEvent.Buttons ),
306 0 );
307 return MouseButtonUp( aMouseEvent );
308}
309
311 const awt::Point& rPos,
312 ::sal_Int32 nCommand,
313 sal_Bool /*bMouseEvent*/,
314 const css::uno::Any& /*aData*/ )
315{
316 SolarMutexGuard aGuard;
317 ::Point aPos( rPos.X, rPos.Y );
318 CommandEvent aCmdEvent( aPos, static_cast<CommandEventId>(nCommand), true, nullptr );
319
320 Command( aCmdEvent );
321}
322
324 const uno::Reference< awt::XGraphics >& xGraphics,
325 const awt::Rectangle& rOutputRectangle,
326 ::sal_Int32 /*nStyle*/ )
327{
328 SolarMutexGuard aGuard;
329
330 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( xGraphics );
331 if ( pOutDev )
332 {
333 ::tools::Rectangle aRect = VCLRectangle( rOutputRectangle );
334 UserDrawEvent aUserDrawEvent(pOutDev, aRect, pBar->GetCurItemId());
335 Paint( aUserDrawEvent );
336 }
337}
338
339void SAL_CALL SfxStatusBarControl::click( const awt::Point& )
340{
341 SolarMutexGuard aGuard;
342 Click();
343}
344
345void SAL_CALL SfxStatusBarControl::doubleClick( const awt::Point& )
346{
347}
348
349// old sfx2 interface
351(
352 sal_uInt16 nSID,
353 SfxItemState eState,
354 const SfxPoolItem* pState /* Pointer to SfxPoolItem, is only valid
355 within this Method call. This can be a
356 Null-Pointer, a Pointer to SfxVoidItem
357 or of this Type found registered by the
358 Subclass of SfxStatusBarControl.
359 */
360)
361
362/* [Description]
363
364 The base implementation includes items of type SfxStringItem
365 where the text is entered in the status row field and
366 SfxVoidItem, where the field is emptied. The base implementation
367 should not be called in overriding methods.
368*/
369
370{
371 DBG_ASSERT( pBar != nullptr, "setting state to dangling StatusBar" );
372
373 const SfxStringItem* pStr = dynamic_cast<const SfxStringItem*>( pState );
374 if ( eState == SfxItemState::DEFAULT && pStr )
375 pBar->SetItemText( nSID, pStr->GetValue() );
376 else
377 {
378 DBG_ASSERT( eState != SfxItemState::DEFAULT || pState->IsVoidItem(),
379 "wrong SfxPoolItem subclass in SfxStatusBarControl" );
380 pBar->SetItemText( nSID, OUString() );
381 }
382}
383
384
386
387/* [Description]
388
389 This virtual method forwards the Event MouseButtonDown() of the
390 StatusBar if the mouse position is within the range of the items,
391 or if the mouse was captured by <SfxStatusBarControl::CaptureMouse()>
392
393 The default implementation is empty and returns FALSE.
394
395 [Return value]
396
397 sal_Bool TRUE
398 The event has been processed and is not intended to
399 be forwarded to StatusBar
400
401 FALSE
402 The event was not processed and is to be
403 be forwarded to StatusBar
404*/
405
406{
407 return false;
408}
409
410
412
413/* [Description]
414
415 This virtual method forwards the Event MouseMove() of the
416 StatusBar if the mouse position is within the range of the items,
417 or if the mouse was captured by <SfxStatusBarControl::CaptureMouse()>
418
419 The default implementation is empty and returns FALSE.
420
421 [Return value]
422
423 sal_Bool TRUE
424 The event has been processed and is not intended to
425 be forwarded to StatusBar
426
427 FALSE
428 The event was not processed and is to be
429 be forwarded to StatusBar
430*/
431
432{
433 return false;
434}
435
436
438
439/* [Description]
440
441 This virtual method forwards the Event MouseButtonUp() of the
442 StatusBar if the mouse position is within the range of the items,
443 or if the mouse was captured by <SfxStatusBarControl::CaptureMouse()>
444
445 The default implementation is empty and returns FALSE.
446
447 [Return value]
448
449 sal_Bool TRUE
450 The event has been processed and is not intended to
451 be forwarded to StatusBar
452
453 FALSE
454 The event was not processed and is to be
455 be forwarded to StatusBar
456*/
457
458{
459 return false;
460}
461
462
464
465/* [Description]
466
467 This virtual method is called when a CommandEvent is received by
468 SfxStatusBarControl.
469
470 The default implementation is empty.
471*/
472
473{
474}
475
476
478
479/* [Description]
480
481 This virtual method is called when the user clicks on the
482 field in the status row that belongs to this control.
483*/
484
485{
486 css::uno::Sequence< css::beans::PropertyValue > aArgs;
487 execute( aArgs );
488}
489
490
492(
493 const UserDrawEvent& /* Reference to an UserDrawEvent */
494)
495
496/* [Description]
497
498 This virtual method is called to paint the contents if the field
499 at hand is marked with StatusBarItemBits::UserDraw. The output must be obtained
500 within the Rectangle of rUDEvt.GetRect() by the OutputDevice
501 given by rUDEvt.GetDevice().
502
503 The default implementation is empty.
504*/
505
506{
507}
508
509
511(
512 sal_uInt16 nSlotID,
513 sal_uInt16 nStbId,
514 StatusBar* pBar,
515 SfxModule const * pMod
516)
517{
518 SolarMutexGuard aGuard;
519 SfxApplication *pApp = SfxGetpApp();
520
521 SfxSlotPool *pSlotPool;
522 if ( pMod )
523 pSlotPool = pMod->GetSlotPool();
524 else
525 pSlotPool = &SfxSlotPool::GetSlotPool();
526
527 const std::type_info* aSlotType = pSlotPool->GetSlotType(nSlotID);
528 if ( aSlotType )
529 {
530 if ( pMod )
531 {
532 SfxStbCtrlFactory *pFact = pMod->GetStbCtrlFactory(*aSlotType, nSlotID);
533 if ( pFact )
534 return pFact->pCtor( nSlotID, nStbId, *pBar );
535 }
536
537 SfxStbCtrlFactory* pFact = pApp->GetStbCtrlFactory(*aSlotType, nSlotID);
538 if (pFact)
539 return pFact->pCtor( nSlotID, nStbId, *pBar );
540 }
541
542 return nullptr;
543}
544
545
547{
549}
550
551
552/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SfxApplication * SfxGetpApp()
Definition: app.hxx:231
const OUString & GetValue() const
SAL_DLLPRIVATE void RegisterStatusBarControl_Impl(SfxModule *, const SfxStbCtrlFactory &)
Definition: appreg.cxx:82
SAL_DLLPRIVATE SfxStbCtrlFactory * GetStbCtrlFactory(const std::type_info &rSlotType, sal_uInt16 nSlotID) const
Definition: app.cxx:351
SAL_DLLPRIVATE SfxStbCtrlFactory * GetStbCtrlFactory(const std::type_info &rSlotType, sal_uInt16 nSlotID) const
Definition: module.cxx:179
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
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
SFX2_DLLPUBLIC OUString GetCommand() const
Definition: msg.cxx:46
virtual bool MouseButtonUp(const MouseEvent &)
Definition: stbitem.cxx:437
virtual void Paint(const UserDrawEvent &rUDEvt)
Definition: stbitem.cxx:492
virtual void StateChangedAtStatusBarControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState)
Definition: stbitem.cxx:351
virtual void SAL_CALL click(const css::awt::Point &aPos) override
Definition: stbitem.cxx:339
VclPtr< StatusBar > pBar
Definition: stbitem.hxx:62
virtual void Command(const CommandEvent &rCEvt)
Definition: stbitem.cxx:463
virtual bool MouseMove(const MouseEvent &)
Definition: stbitem.cxx:411
static rtl::Reference< SfxStatusBarControl > CreateControl(sal_uInt16 nSlotID, sal_uInt16 nId, StatusBar *pBar, SfxModule const *)
Definition: stbitem.cxx:511
virtual void SAL_CALL release() noexcept override
Definition: stbitem.cxx:165
virtual sal_Bool SAL_CALL mouseButtonDown(const css::awt::MouseEvent &aMouseEvent) override
Definition: stbitem.cxx:267
virtual void SAL_CALL acquire() noexcept override
Definition: stbitem.cxx:160
static sal_uInt16 convertAwtToVCLMouseButtons(sal_Int16 nAwtMouseButtons)
Definition: stbitem.cxx:54
virtual ~SfxStatusBarControl() override
Definition: stbitem.cxx:148
virtual void SAL_CALL paint(const css::uno::Reference< css::awt::XGraphics > &xGraphics, const css::awt::Rectangle &rOutputRectangle, ::sal_Int32 nStyle) override
Definition: stbitem.cxx:323
virtual sal_Bool SAL_CALL mouseButtonUp(const css::awt::MouseEvent &aMouseEvent) override
Definition: stbitem.cxx:296
virtual bool MouseButtonDown(const MouseEvent &)
Definition: stbitem.cxx:385
static void RegisterStatusBarControl(SfxModule *, const SfxStbCtrlFactory &)
Definition: stbitem.cxx:546
virtual sal_Bool SAL_CALL mouseMove(const css::awt::MouseEvent &aMouseEvent) override
Definition: stbitem.cxx:282
virtual void SAL_CALL command(const css::awt::Point &aPos, ::sal_Int32 nCommand, sal_Bool bMouseEvent, const css::uno::Any &aData) override
Definition: stbitem.cxx:310
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
Definition: stbitem.cxx:172
virtual void SAL_CALL doubleClick(const css::awt::Point &aPos) override
Definition: stbitem.cxx:345
virtual void Click()
Definition: stbitem.cxx:477
SfxStatusBarControl(sal_uInt16 nSlotID, sal_uInt16 nId, StatusBar &rBar)
Definition: stbitem.cxx:118
SfxFrame & GetFrame() const
Definition: viewfrm.cxx:2782
void SetHelpId(sal_uInt16 nItemId, const OUString &rHelpId)
static OutputDevice * GetOutputDevice(const css::uno::Reference< css::awt::XDevice > &rxDevice)
css::uno::Reference< css::frame::XFrame > m_xFrame
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &Event) override
void execute(const css::uno::Sequence< css::beans::PropertyValue > &aArgs)
CommandEventId
inline ::tools::Rectangle VCLRectangle(const css::awt::Rectangle &rAWTRect)
#define DBG_ASSERT(sCon, aError)
#define MOUSE_LEFT
#define MOUSE_MIDDLE
#define MOUSE_RIGHT
sal_Int16 nId
SfxItemState
rtl::Reference< svt::StatusbarController > SfxStatusBarControllerFactory(const uno::Reference< frame::XFrame > &rFrame, StatusBar *pStatusBar, unsigned short nID, const OUString &aCommandURL)
Definition: stbitem.cxx:69
SfxStatusBarControlCtor pCtor
Definition: stbitem.hxx:41
std::unique_ptr< SfxPoolItem > CreateItem() const
Definition: msg.hxx:111
Reference< XController > xController
Reference< XModel > xModel
unsigned char sal_Bool
OUString aTargetURL