tdf#144694 In direct SQL dialog, activate options 'Run SQL command
[LibreOffice.git] / sfx2 / source / statbar / stbitem.cxx
blob049f810a27f31cbadc3437f96e7f92296ce3b38b
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 <svl/stritem.hxx>
21 #include <svl/voiditem.hxx>
22 #include <com/sun/star/util/URL.hpp>
23 #include <com/sun/star/util/URLTransformer.hpp>
24 #include <com/sun/star/util/XURLTransformer.hpp>
25 #include <com/sun/star/frame/XController.hpp>
26 #include <com/sun/star/frame/XDispatchProvider.hpp>
27 #include <com/sun/star/lang/XUnoTunnel.hpp>
28 #include <com/sun/star/frame/status/ItemStatus.hpp>
29 #include <com/sun/star/awt/MouseButton.hpp>
31 #include <vcl/commandevent.hxx>
32 #include <vcl/event.hxx>
33 #include <vcl/status.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/unohelp.hxx>
37 #include <sfx2/app.hxx>
38 #include <sfx2/stbitem.hxx>
39 #include <sfx2/msg.hxx>
40 #include <sfx2/msgpool.hxx>
41 #include <sfx2/module.hxx>
42 #include <sfx2/dispatch.hxx>
43 #include <unoctitm.hxx>
44 #include <sfx2/objsh.hxx>
46 #include <comphelper/processfactory.hxx>
47 #include <comphelper/servicehelper.hxx>
48 #include <svl/eitem.hxx>
49 #include <svl/intitem.hxx>
50 #include <toolkit/helper/vclunohelper.hxx>
52 using namespace ::com::sun::star;
55 sal_uInt16 SfxStatusBarControl::convertAwtToVCLMouseButtons( sal_Int16 nAwtMouseButtons )
57 sal_uInt16 nVCLMouseButtons( 0 );
59 if ( nAwtMouseButtons & awt::MouseButton::LEFT )
60 nVCLMouseButtons |= MOUSE_LEFT;
61 if ( nAwtMouseButtons & awt::MouseButton::RIGHT )
62 nVCLMouseButtons |= MOUSE_RIGHT;
63 if ( nAwtMouseButtons & awt::MouseButton::MIDDLE )
64 nVCLMouseButtons |= MOUSE_MIDDLE;
66 return nVCLMouseButtons;
70 rtl::Reference<svt::StatusbarController> SfxStatusBarControllerFactory(
71 const uno::Reference< frame::XFrame >& rFrame,
72 StatusBar* pStatusBar,
73 unsigned short nID,
74 const OUString& aCommandURL )
76 SolarMutexGuard aGuard;
78 util::URL aTargetURL;
79 aTargetURL.Complete = aCommandURL;
80 uno::Reference < util::XURLTransformer > xTrans( util::URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
81 xTrans->parseStrict( aTargetURL );
83 uno::Reference < frame::XController > xController;
84 uno::Reference < frame::XModel > xModel;
85 if ( rFrame.is() )
87 xController = rFrame->getController();
88 if ( xController.is() )
89 xModel = xController->getModel();
92 SfxObjectShell* pObjShell = SfxObjectShell::GetShellFromComponent(xModel);
94 SfxModule* pModule = pObjShell ? pObjShell->GetModule() : nullptr;
95 SfxSlotPool* pSlotPool = nullptr;
97 if ( pModule )
98 pSlotPool = pModule->GetSlotPool();
99 else
100 pSlotPool = &(SfxSlotPool::GetSlotPool());
102 const SfxSlot* pSlot = pSlotPool->GetUnoSlot( aTargetURL.Path );
103 if ( pSlot )
105 sal_uInt16 nSlotId = pSlot->GetSlotId();
106 if ( nSlotId > 0 )
108 OUString aCmd = pSlot->GetCommand();
109 pStatusBar->SetHelpId( nSlotId, aCmd );
110 return SfxStatusBarControl::CreateControl( nSlotId, nID, pStatusBar, pModule );
114 return nullptr;
118 SfxStatusBarControl::SfxStatusBarControl
120 sal_uInt16 nSlotID, /* Slot-Id which is connected to this
121 instance. If a Slot-Id is set to != 0 at
122 registration it will always be set there.
126 sal_uInt16 nCtrlID, /* ID of this controller in the status bar */
128 StatusBar& rBar /* Reference to the StatusBar,for which
129 this Control was created. */
133 /* [Description]
135 Constructor of the SfxStatusBarControl Class. The Subclasses are
136 created at the Factory if necessary.
138 Instances of this base class are created for all StatusBar-fields
139 for which no specific ones have been registered.
142 : nSlotId( nSlotID ),
143 nId( nCtrlID ),
144 pBar( &rBar )
149 SfxStatusBarControl::~SfxStatusBarControl()
151 /* [Description]
153 Destructor of the SfxStatusBarControl Class. The Class and its Subclasses
154 are destroyed by SFx.
160 // XInterface
161 void SAL_CALL SfxStatusBarControl::acquire() noexcept
163 OWeakObject::acquire();
166 void SAL_CALL SfxStatusBarControl::release() noexcept
168 OWeakObject::release();
172 // XStatusListener
173 void SAL_CALL SfxStatusBarControl::statusChanged( const frame::FeatureStateEvent& rEvent )
175 SfxViewFrame* pViewFrame = nullptr;
176 uno::Reference < frame::XController > xController;
178 SolarMutexGuard aGuard;
179 if ( m_xFrame.is() )
180 xController = m_xFrame->getController();
182 uno::Reference < frame::XDispatchProvider > xProvider( xController, uno::UNO_QUERY );
183 if ( xProvider.is() )
185 uno::Reference < frame::XDispatch > xDisp = xProvider->queryDispatch( rEvent.FeatureURL, OUString(), 0 );
186 if ( xDisp.is() )
188 if (auto pDisp = dynamic_cast<SfxOfficeDispatch*>(xDisp.get()))
189 pViewFrame = pDisp->GetDispatcher_Impl()->GetFrame();
193 sal_uInt16 nSlotID = 0;
194 SfxSlotPool& rPool = SfxSlotPool::GetSlotPool( pViewFrame );
195 const SfxSlot* pSlot = rPool.GetUnoSlot( rEvent.FeatureURL.Path );
196 if ( pSlot )
197 nSlotID = pSlot->GetSlotId();
199 if ( nSlotID <= 0 )
200 return;
202 if ( rEvent.Requery )
203 svt::StatusbarController::statusChanged( rEvent );
204 else
206 SfxItemState eState = SfxItemState::DISABLED;
207 std::unique_ptr<SfxPoolItem> pItem;
208 if ( rEvent.IsEnabled )
210 eState = SfxItemState::DEFAULT;
211 uno::Type aType = rEvent.State.getValueType();
213 if ( aType == cppu::UnoType<void>::get() )
215 pItem.reset( new SfxVoidItem( nSlotID ) );
216 eState = SfxItemState::UNKNOWN;
218 else if ( aType == cppu::UnoType<bool>::get() )
220 bool bTemp = false;
221 rEvent.State >>= bTemp ;
222 pItem.reset( new SfxBoolItem( nSlotID, bTemp ) );
224 else if ( aType == ::cppu::UnoType< ::cppu::UnoUnsignedShortType >::get() )
226 sal_uInt16 nTemp = 0;
227 rEvent.State >>= nTemp ;
228 pItem.reset( new SfxUInt16Item( nSlotID, nTemp ) );
230 else if ( aType == cppu::UnoType<sal_uInt32>::get() )
232 sal_uInt32 nTemp = 0;
233 rEvent.State >>= nTemp ;
234 pItem.reset( new SfxUInt32Item( nSlotID, nTemp ) );
236 else if ( aType == cppu::UnoType<OUString>::get() )
238 OUString sTemp ;
239 rEvent.State >>= sTemp ;
240 pItem.reset( new SfxStringItem( nSlotID, sTemp ) );
242 else if ( aType == cppu::UnoType< css::frame::status::ItemStatus>::get() )
244 frame::status::ItemStatus aItemStatus;
245 rEvent.State >>= aItemStatus;
246 eState = static_cast<SfxItemState>(aItemStatus.State);
247 pItem.reset( new SfxVoidItem( nSlotID ) );
249 else
251 if ( pSlot )
252 pItem = pSlot->GetType()->CreateItem();
253 if ( pItem )
255 pItem->SetWhich( nSlotID );
256 pItem->PutValue( rEvent.State, 0 );
258 else
259 pItem.reset( new SfxVoidItem( nSlotID ) );
263 StateChangedAtStatusBarControl( nSlotID, eState, pItem.get() );
267 // XStatusbarController
268 sal_Bool SAL_CALL SfxStatusBarControl::mouseButtonDown(
269 const awt::MouseEvent& rMouseEvent )
271 SolarMutexGuard aGuard;
272 ::Point aPos( rMouseEvent.X, rMouseEvent.Y );
274 ::MouseEvent aMouseEvent( aPos,
275 static_cast<sal_uInt16>(rMouseEvent.ClickCount),
276 MouseEventModifiers::NONE,
277 convertAwtToVCLMouseButtons( rMouseEvent.Buttons ),
278 0 );
280 return MouseButtonDown( aMouseEvent );
283 sal_Bool SAL_CALL SfxStatusBarControl::mouseMove(
284 const awt::MouseEvent& rMouseEvent )
286 SolarMutexGuard aGuard;
287 ::Point aPos( rMouseEvent.X, rMouseEvent.Y );
289 ::MouseEvent aMouseEvent( aPos,
290 static_cast<sal_uInt16>(rMouseEvent.ClickCount),
291 MouseEventModifiers::NONE,
292 convertAwtToVCLMouseButtons( rMouseEvent.Buttons ),
293 0 );
294 return MouseMove( aMouseEvent );
297 sal_Bool SAL_CALL SfxStatusBarControl::mouseButtonUp(
298 const ::awt::MouseEvent& rMouseEvent )
300 SolarMutexGuard aGuard;
301 ::Point aPos( rMouseEvent.X, rMouseEvent.Y );
303 ::MouseEvent aMouseEvent( aPos,
304 static_cast<sal_uInt16>(rMouseEvent.ClickCount),
305 MouseEventModifiers::NONE,
306 convertAwtToVCLMouseButtons( rMouseEvent.Buttons ),
307 0 );
308 return MouseButtonUp( aMouseEvent );
311 void SAL_CALL SfxStatusBarControl::command(
312 const awt::Point& rPos,
313 ::sal_Int32 nCommand,
314 sal_Bool /*bMouseEvent*/,
315 const css::uno::Any& /*aData*/ )
317 SolarMutexGuard aGuard;
318 ::Point aPos( rPos.X, rPos.Y );
319 CommandEvent aCmdEvent( aPos, static_cast<CommandEventId>(nCommand), true, nullptr );
321 Command( aCmdEvent );
324 void SAL_CALL SfxStatusBarControl::paint(
325 const uno::Reference< awt::XGraphics >& xGraphics,
326 const awt::Rectangle& rOutputRectangle,
327 ::sal_Int32 /*nStyle*/ )
329 SolarMutexGuard aGuard;
331 OutputDevice* pOutDev = VCLUnoHelper::GetOutputDevice( xGraphics );
332 if ( pOutDev )
334 ::tools::Rectangle aRect = vcl::unohelper::ConvertToVCLRect(rOutputRectangle);
335 UserDrawEvent aUserDrawEvent(pOutDev, aRect, pBar->GetCurItemId());
336 Paint( aUserDrawEvent );
340 void SAL_CALL SfxStatusBarControl::click( const awt::Point& )
342 SolarMutexGuard aGuard;
343 Click();
346 void SAL_CALL SfxStatusBarControl::doubleClick( const awt::Point& )
350 // old sfx2 interface
351 void SfxStatusBarControl::StateChangedAtStatusBarControl
353 sal_uInt16 nSID,
354 SfxItemState eState,
355 const SfxPoolItem* pState /* Pointer to SfxPoolItem, is only valid
356 within this Method call. This can be a
357 Null-Pointer, a Pointer to SfxVoidItem
358 or of this Type found registered by the
359 Subclass of SfxStatusBarControl.
363 /* [Description]
365 The base implementation includes items of type SfxStringItem
366 where the text is entered in the status row field and
367 SfxVoidItem, where the field is emptied. The base implementation
368 should not be called in overriding methods.
372 DBG_ASSERT( pBar != nullptr, "setting state to dangling StatusBar" );
374 const SfxStringItem* pStr = dynamic_cast<const SfxStringItem*>( pState );
375 if ( eState == SfxItemState::DEFAULT && pStr )
376 pBar->SetItemText( nSID, pStr->GetValue() );
377 else
379 DBG_ASSERT( eState != SfxItemState::DEFAULT || IsDisabledItem(pState),
380 "wrong SfxPoolItem subclass in SfxStatusBarControl" );
381 pBar->SetItemText( nSID, OUString() );
386 bool SfxStatusBarControl::MouseButtonDown( const MouseEvent & )
388 /* [Description]
390 This virtual method forwards the Event MouseButtonDown() of the
391 StatusBar if the mouse position is within the range of the items,
392 or if the mouse was captured by <SfxStatusBarControl::CaptureMouse()>
394 The default implementation is empty and returns FALSE.
396 [Return value]
398 sal_Bool TRUE
399 The event has been processed and is not intended to
400 be forwarded to StatusBar
402 FALSE
403 The event was not processed and is to be
404 be forwarded to StatusBar
408 return false;
412 bool SfxStatusBarControl::MouseMove( const MouseEvent & )
414 /* [Description]
416 This virtual method forwards the Event MouseMove() of the
417 StatusBar if the mouse position is within the range of the items,
418 or if the mouse was captured by <SfxStatusBarControl::CaptureMouse()>
420 The default implementation is empty and returns FALSE.
422 [Return value]
424 sal_Bool TRUE
425 The event has been processed and is not intended to
426 be forwarded to StatusBar
428 FALSE
429 The event was not processed and is to be
430 be forwarded to StatusBar
434 return false;
438 bool SfxStatusBarControl::MouseButtonUp( const MouseEvent & )
440 /* [Description]
442 This virtual method forwards the Event MouseButtonUp() of the
443 StatusBar if the mouse position is within the range of the items,
444 or if the mouse was captured by <SfxStatusBarControl::CaptureMouse()>
446 The default implementation is empty and returns FALSE.
448 [Return value]
450 sal_Bool TRUE
451 The event has been processed and is not intended to
452 be forwarded to StatusBar
454 FALSE
455 The event was not processed and is to be
456 be forwarded to StatusBar
460 return false;
464 void SfxStatusBarControl::Command( const CommandEvent& )
466 /* [Description]
468 This virtual method is called when a CommandEvent is received by
469 SfxStatusBarControl.
471 The default implementation is empty.
478 void SfxStatusBarControl::Click()
480 /* [Description]
482 This virtual method is called when the user clicks on the
483 field in the status row that belongs to this control.
487 css::uno::Sequence< css::beans::PropertyValue > aArgs;
488 execute( aArgs );
492 void SfxStatusBarControl::Paint
494 const UserDrawEvent& /* Reference to an UserDrawEvent */
497 /* [Description]
499 This virtual method is called to paint the contents if the field
500 at hand is marked with StatusBarItemBits::UserDraw. The output must be obtained
501 within the Rectangle of rUDEvt.GetRect() by the OutputDevice
502 given by rUDEvt.GetDevice().
504 The default implementation is empty.
511 rtl::Reference<SfxStatusBarControl> SfxStatusBarControl::CreateControl
513 sal_uInt16 nSlotID,
514 sal_uInt16 nStbId,
515 StatusBar* pBar,
516 SfxModule const * pMod
519 SolarMutexGuard aGuard;
520 SfxApplication *pApp = SfxGetpApp();
522 SfxSlotPool *pSlotPool;
523 if ( pMod )
524 pSlotPool = pMod->GetSlotPool();
525 else
526 pSlotPool = &SfxSlotPool::GetSlotPool();
528 const std::type_info* aSlotType = pSlotPool->GetSlotType(nSlotID);
529 if ( aSlotType )
531 if ( pMod )
533 SfxStbCtrlFactory *pFact = pMod->GetStbCtrlFactory(*aSlotType, nSlotID);
534 if ( pFact )
535 return pFact->pCtor( nSlotID, nStbId, *pBar );
538 SfxStbCtrlFactory* pFact = pApp->GetStbCtrlFactory(*aSlotType, nSlotID);
539 if (pFact)
540 return pFact->pCtor( nSlotID, nStbId, *pBar );
543 return nullptr;
547 void SfxStatusBarControl::RegisterStatusBarControl(SfxModule* pMod, const SfxStbCtrlFactory& rFact)
549 SfxGetpApp()->RegisterStatusBarControl_Impl( pMod, rFact );
553 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */