LibreOffice Module svx (master)  1
selctrl.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 <sal/config.h>
21 
22 #include <string_view>
23 
25 #include <vcl/event.hxx>
26 #include <vcl/status.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/weldutils.hxx>
29 #include <svl/intitem.hxx>
30 #include <tools/urlobj.hxx>
31 
32 #include <svx/selctrl.hxx>
33 
34 #include <bitmaps.hlst>
35 
36 #include <com/sun/star/beans/PropertyValue.hpp>
37 #include <com/sun/star/frame/XFrame.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 
40 #include <svx/strings.hrc>
41 #include <svx/dialmgr.hxx>
42 
44 
45 namespace {
46 
48 class SelectionTypePopup
49 {
50  weld::Window* m_pPopupParent;
51  std::unique_ptr<weld::Builder> m_xBuilder;
52  std::unique_ptr<weld::Menu> m_xMenu;
53  static OString state_to_id(sal_uInt16 nState);
54 public:
55  SelectionTypePopup(weld::Window* pPopupParent, sal_uInt16 nCurrent);
56  OUString GetItemTextForState(sal_uInt16 nState) { return m_xMenu->get_label(state_to_id(nState)); }
57  OString popup_at_rect(const tools::Rectangle& rRect)
58  {
59  return m_xMenu->popup_at_rect(m_pPopupParent, rRect);
60  }
61  void HideSelectionType(const OString& rIdent)
62  {
63  m_xMenu->remove(rIdent);
64  }
65  static sal_uInt16 id_to_state(std::string_view ident);
66 };
67 
68 }
69 
70 sal_uInt16 SelectionTypePopup::id_to_state(std::string_view ident)
71 {
72  if (ident == "block")
73  return 3;
74  else if (ident == "adding")
75  return 2;
76  else if (ident == "extending")
77  return 1;
78  else // fall through
79  return 0;
80 }
81 
82 OString SelectionTypePopup::state_to_id(sal_uInt16 nState)
83 {
84  switch (nState)
85  {
86  default: // fall through
87  case 0: return "standard";
88  case 1: return "extending";
89  case 2: return "adding";
90  case 3: return "block";
91  }
92 }
93 
94 SelectionTypePopup::SelectionTypePopup(weld::Window* pPopupParent, sal_uInt16 nCurrent)
95  : m_pPopupParent(pPopupParent)
96  , m_xBuilder(Application::CreateBuilder(m_pPopupParent, "svx/ui/selectionmenu.ui"))
97  , m_xMenu(m_xBuilder->weld_menu("menu"))
98 {
99  m_xMenu->set_active(state_to_id(nCurrent), true);
100 }
101 
103  sal_uInt16 _nId,
104  StatusBar& rStb ) :
105  SfxStatusBarControl( _nSlotId, _nId, rStb ),
106  mnState( 0 ),
107  maImages{Image(StockImage::Yes, RID_SVXBMP_STANDARD_SELECTION),
108  Image(StockImage::Yes, RID_SVXBMP_EXTENDING_SELECTION),
109  Image(StockImage::Yes, RID_SVXBMP_ADDING_SELECTION),
110  Image(StockImage::Yes, RID_SVXBMP_BLOCK_SELECTION)},
111  mbFeatureEnabled(false)
112 {
113  GetStatusBar().SetQuickHelpText(GetId(), u"");
114 }
115 
117  const SfxPoolItem* pState )
118 {
119  mbFeatureEnabled = SfxItemState::DEFAULT == eState;
120  if (mbFeatureEnabled)
121  {
122  DBG_ASSERT( dynamic_cast< const SfxUInt16Item* >(pState) != nullptr, "invalid item type" );
123  const SfxUInt16Item* pItem = static_cast<const SfxUInt16Item*>(pState);
124  mnState = pItem->GetValue();
125  SelectionTypePopup aPop(GetStatusBar().GetFrameWeld(), mnState);
127  SvxResId(RID_SVXSTR_SELECTIONMODE_HELPTEXT).
128  replaceFirst("%1", aPop.GetItemTextForState(mnState)));
130  }
131 }
132 
134 {
135  if (!mbFeatureEnabled)
136  return true;
137 
138  ::tools::Rectangle aRect(rEvt.GetPosPixel(), Size(1, 1));
139  weld::Window* pPopupParent = weld::GetPopupParent(GetStatusBar(), aRect);
140  SelectionTypePopup aPop(pPopupParent, mnState);
141 
142  // Check if Calc is opened; if true, hide block selection mode tdf#122280
143  const css::uno::Reference < css::frame::XModel > xModel = m_xFrame->getController()->getModel();
144  css::uno::Reference< css::lang::XServiceInfo > xServices( xModel, css::uno::UNO_QUERY );
145  if ( xServices.is() )
146  {
147  bool bSpecModeCalc = xServices->supportsService("com.sun.star.sheet.SpreadsheetDocument");
148  if (bSpecModeCalc)
149  aPop.HideSelectionType("block");
150  }
151 
152  OString sIdent = aPop.popup_at_rect(aRect);
153  if (!sIdent.isEmpty())
154  {
155  sal_uInt16 nNewState = SelectionTypePopup::id_to_state(sIdent);
156  if ( nNewState != mnState )
157  {
158  mnState = nNewState;
159 
160  css::uno::Any a;
161  SfxUInt16Item aState( GetSlotId(), mnState );
162  aState.QueryValue( a );
163  INetURLObject aObj( m_aCommandURL );
164 
165  css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
166  aObj.GetURLPath(), a) };
167  execute( aArgs );
168  }
169  }
170 
171  return true;
172 }
173 
175 {
176 }
177 
179 {
180  const tools::Rectangle aControlRect = getControlRect();
181  vcl::RenderContext* pDev = rUsrEvt.GetRenderContext();
182  tools::Rectangle aRect = rUsrEvt.GetRect();
183 
184  Size aImgSize( maImages[mnState].GetSizePixel() );
185 
186  Point aPos( aRect.Left() + ( aControlRect.GetWidth() - aImgSize.Width() ) / 2,
187  aRect.Top() + ( aControlRect.GetHeight() - aImgSize.Height() ) / 2 );
188 
189  if (mbFeatureEnabled)
190  pDev->DrawImage(aPos, maImages[mnState]);
191  else
192  pDev->DrawImage(aPos, Image());
193 }
194 
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 GetSlotId() const
void DrawImage(const Point &rPos, const Image &rImage, DrawImageFlags nStyle=DrawImageFlags::NONE)
const tools::Rectangle & GetRect() const
constexpr tools::Long Left() const
OUString GetURLPath(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
SFX_IMPL_STATUSBAR_CONTROL(SvxSelectionModeControl, SfxUInt16Item)
Status bar control that indicates or changes the selection mode (standard / block / etc...
Definition: selctrl.hxx:27
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
css::uno::Reference< css::lang::XComponent > m_xFrame
sal_uInt16 GetValue() const
virtual sal_uInt32 GetId() const override
constexpr tools::Long Width() const
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
constexpr tools::Long GetWidth() const
virtual bool MouseButtonDown(const MouseEvent &rEvt) override
Definition: selctrl.cxx:133
SvxSelectionModeControl(sal_uInt16 nSlotId, sal_uInt16 nId, StatusBar &rStb)
Definition: selctrl.cxx:102
sal_uInt16 GetId() const
vcl::RenderContext * GetRenderContext() const
#define DBG_ASSERT(sCon, aError)
uno_Any a
weld::Window * GetPopupParent(vcl::Window &rOutWin, tools::Rectangle &rRect)
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
constexpr tools::Long Top() const
virtual void Click() override
Definition: selctrl.cxx:174
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
SfxItemState
constexpr tools::Long Height() const
StatusBar & GetStatusBar() const
virtual void StateChangedAtStatusBarControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: selctrl.cxx:116
const Point & GetPosPixel() const
Reference< XModel > xModel
virtual bool QueryValue(css::uno::Any &rVal, sal_uInt8 nMemberId=0) const override
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
constexpr tools::Long GetHeight() const
virtual void Paint(const UserDrawEvent &rEvt) override
Definition: selctrl.cxx:178