LibreOffice Module svx (master)  1
tbxcolorupdate.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 <sfx2/objsh.hxx>
21 #include <svx/drawitem.hxx>
22 #include <tbxcolorupdate.hxx>
23 #include <svx/svxids.hrc>
24 #include <svx/xdef.hxx>
25 #include <svx/xlineit0.hxx>
26 #include <svx/xlndsit.hxx>
27 
29 #include <vcl/svapp.hxx>
30 #include <vcl/toolbox.hxx>
31 #include <vcl/virdev.hxx>
32 #include <vcl/weld.hxx>
33 #include <vcl/settings.hxx>
34 
35 #include <svx/strings.hrc>
36 #include <svx/dialmgr.hxx>
37 
38 namespace svx
39 {
40  ToolboxButtonColorUpdaterBase::ToolboxButtonColorUpdaterBase(bool bWideButton, const OUString& rCommandLabel,
41  const OUString& rCommandURL,
42  const css::uno::Reference<css::frame::XFrame>& rFrame)
43  : mbWideButton(bWideButton)
44  , mbWasHiContrastMode(Application::GetSettings().GetStyleSettings().GetHighContrastMode())
45  , maCurColor(COL_TRANSPARENT)
46  , meImageType(vcl::ImageType::Size16)
47  , maCommandLabel(rCommandLabel)
48  , maCommandURL(rCommandURL)
49  , mxFrame(rFrame)
50  {
51  }
52 
53  void ToolboxButtonColorUpdaterBase::Init(sal_uInt16 nSlotId)
54  {
55  switch (nSlotId)
56  {
57  case SID_ATTR_CHAR_COLOR:
58  case SID_ATTR_CHAR_COLOR2:
59  Update(NamedColor(COL_DEFAULT_FONT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FONT)));
60  break;
61  case SID_FRAME_LINECOLOR:
62  Update(NamedColor(COL_DEFAULT_FRAMELINE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FRAMELINE)));
63  break;
64  case SID_ATTR_CHAR_COLOR_BACKGROUND:
65  case SID_ATTR_CHAR_BACK_COLOR:
66  case SID_BACKGROUND_COLOR:
67  case SID_TABLE_CELL_BACKGROUND_COLOR:
68  Update(NamedColor(COL_DEFAULT_HIGHLIGHT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_HIGHLIGHT)));
69  break;
70  case SID_ATTR_LINE_COLOR:
71  Update(NamedColor(COL_DEFAULT_SHAPE_STROKE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_STROKE)));
72  break;
73  case SID_ATTR_FILL_COLOR:
74  Update(NamedColor(COL_DEFAULT_SHAPE_FILLING, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_FILLING)));
75  break;
76  default:
78  }
79  }
80 
82  sal_uInt16 nSlotId, sal_uInt16 nTbxBtnId, ToolBox* pToolBox, bool bWideButton,
83  const OUString& rCommandLabel, const OUString& rCommandURL,
84  const css::uno::Reference<css::frame::XFrame>& rFrame)
85  : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, rCommandURL, rFrame)
86  , mnBtnId(nTbxBtnId)
87  , mpTbx(pToolBox)
88  {
89  Init(nSlotId);
90  }
91 
93  {
95  }
96 
98  {
100  }
101 
103  {
105  }
106 
108  {
110  DeviceFormat::DEFAULT, DeviceFormat::DEFAULT);
111  xRet->SetBackground(mpTbx->GetControlBackground());
112  return xRet;
113  }
114 
116  {
117  return mpTbx->GetImageSize();
118  }
119 
121  {
122  if (mbWideButton)
124  return rImageSize;
125  }
126 
128  {}
129 
131  {
132  Update(rNamedColor.first);
133  if (!mbWideButton)
134  {
135  // Also show the current color as QuickHelpText
136  OUString colorSuffix = OUString(" (%1)").replaceFirst("%1", rNamedColor.second);
137  OUString colorHelpText = maCommandLabel + colorSuffix;
138 
139  SetQuickHelpText(colorHelpText);
140  }
141  }
142 
143  void ToolboxButtonColorUpdaterBase::Update(const Color& rColor, bool bForceUpdate)
144  {
145  vcl::ImageType eImageType = GetImageSize();
146 
147 #ifdef IOS // tdf#126966
148  eImageType = vcl::ImageType::Size32;
149 #endif
150 
151  const bool bSizeChanged = (meImageType != eImageType);
152  meImageType = eImageType;
153  const bool bDisplayModeChanged = (mbWasHiContrastMode != Application::GetSettings().GetStyleSettings().GetHighContrastMode());
154  Color aColor(rColor);
155 
156  // !!! #109290# Workaround for SetFillColor with COL_AUTO
157  if (aColor == COL_AUTO)
158  aColor = COL_TRANSPARENT;
159 
160  if ((maCurColor == aColor) && !bSizeChanged && !bDisplayModeChanged && !bForceUpdate)
161  return;
162 
164  Image aImage(xImage);
165 
166  Size aItemSize = GetItemSize(aImage.GetSizePixel());
167  if (!aItemSize.Width() || !aItemSize.Height())
168  return;
169 
171  pVirDev->SetOutputSizePixel(aItemSize);
172  maBmpSize = aItemSize;
173 
174  if (maBmpSize.Width() == maBmpSize.Height())
175  // tdf#84985 align color bar with icon bottom edge; integer arithmetic e.g. 26 - 26/4 <> 26 * 3/4
177  else
179 
180  pVirDev->Push(PushFlags::CLIPREGION);
181 
182  // tdf#135121 don't include the part of the image which we will
183  // overwrite with the target color so that for the transparent color
184  // case the original background of the device is shown
185  vcl::Region aRegion(tools::Rectangle(Point(0, 0), maBmpSize));
186  aRegion.Exclude(maUpdRect);
187  pVirDev->SetClipRegion(aRegion);
188 
189  pVirDev->DrawImage(Point(0, 0), aImage);
190 
191  pVirDev->Pop();
192 
193  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
194  mbWasHiContrastMode = rStyleSettings.GetHighContrastMode();
195 
196  if ((COL_TRANSPARENT != aColor) && (maBmpSize.Width() == maBmpSize.Height()))
197  pVirDev->SetLineColor(aColor);
198  else
199  pVirDev->SetLineColor(rStyleSettings.GetDisableColor());
200 
201  // use not only COL_TRANSPARENT for detection of transparence,
202  // but the method/way which is designed to do that
203  const bool bIsTransparent(0xff == aColor.GetTransparency());
204  maCurColor = aColor;
205 
206  if (bIsTransparent)
207  {
208  pVirDev->SetFillColor(Color(255, 0, 0, 0));
209  }
210  else
211  {
212  pVirDev->SetFillColor(maCurColor);
213  }
214 
215  pVirDev->DrawRect(maUpdRect);
216 
217  SetImage(pVirDev.get());
218  }
219 
221  {
222  OUString sColorName = GetQuickHelpText();
223  // The obtained string is of format: color context (color name)
224  // Generate a substring which contains only the color name
225  sal_Int32 nStart = sColorName.indexOf('(');
226  sColorName = sColorName.copy( nStart + 1 );
227  sal_Int32 nLength = sColorName.getLength();
228  if(nLength > 0)
229  sColorName = sColorName.copy( 0, nLength - 1);
230  return sColorName;
231  }
232 
233  ToolboxButtonColorUpdater::ToolboxButtonColorUpdater(sal_uInt16 nSlotId, const OString& rTbxBtnId, weld::Toolbar* ptrTbx, bool bWideButton,
234  const OUString& rCommandLabel, const css::uno::Reference<css::frame::XFrame>& rFrame)
235  : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, OUString::fromUtf8(rTbxBtnId), rFrame)
236  , msBtnId(rTbxBtnId)
237  , mpTbx(ptrTbx)
238  {
239  Init(nSlotId);
240  }
241 
243  {
245  }
246 
248  {
250  }
251 
253  {
254  mpTbx->set_item_image(msBtnId, pVirDev);
255  }
256 
258  {
259  return mpTbx->create_virtual_device();
260  }
261 
263  {
264  return mpTbx->get_icon_size();
265  }
266 
268  {
269  auto nWidth = rImageSize.Width();
270  if (mbWideButton)
271  nWidth = nWidth * 5;
272  return Size(nWidth, rImageSize.Height());
273  }
274 
276  : m_eXLS(css::drawing::LineStyle_NONE)
277  , m_nDashStyleIndex(-1)
278  {
279  }
280 
281  void ToolboxButtonLineStyleUpdater::Update(const com::sun::star::frame::FeatureStateEvent& rEvent)
282  {
283  if (rEvent.FeatureURL.Complete == ".uno:LineDash")
284  {
285  m_nDashStyleIndex = -1;
286 
288  if (!pSh)
289  return;
290  const SvxDashListItem* pItem = pSh->GetItem( SID_DASH_LIST );
291  if (!pItem)
292  return;
293 
294  XLineDashItem aDashItem;
295  aDashItem.PutValue(rEvent.State, 0);
296  const XDash& rDash = aDashItem.GetDashValue();
297 
298  XDashListRef xLineStyleList = pItem->GetDashList();
299  for (long i = 0; i < xLineStyleList->Count(); ++i)
300  {
301  const XDashEntry* pEntry = xLineStyleList->GetDash(i);
302  const XDash& rEntry = pEntry->GetDash();
303  if (rDash == rEntry)
304  {
306  break;
307  }
308  }
309  }
310  else if (rEvent.FeatureURL.Complete == ".uno:XLineStyle")
311  {
312  XLineStyleItem aLineStyleItem;
313  aLineStyleItem.PutValue(rEvent.State, 0);
314 
315  m_eXLS = aLineStyleItem.GetValue();
316  }
317  }
318 
320  {
321  int nRet;
322  switch (m_eXLS)
323  {
324  case css::drawing::LineStyle_NONE:
325  nRet = 0;
326  break;
327  case css::drawing::LineStyle_SOLID:
328  nRet = 1;
329  break;
330  default:
331  nRet = m_nDashStyleIndex + 2;
332  break;
333  }
334  return nRet;
335  }
336 }
337 
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
Size GetSizePixel() const
virtual OUString GetQuickHelpText() const =0
std::pair< Color, OUString > NamedColor
Definition: Palette.hxx:26
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
long Height() const
#define COL_DEFAULT_SHAPE_FILLING
Definition: xdef.hxx:84
void Init(sal_uInt16 nSlotId)
OUString GetQuickHelpText(sal_uInt16 nItemId) const
sal_uInt8 GetTransparency() const
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
virtual Size GetItemSize(const Size &rImageSize) const override
virtual OUString GetQuickHelpText() const override
virtual Size GetItemSize(const Size &rImageSize) const override
#define COL_DEFAULT_FONT
Definition: xdef.hxx:81
virtual OUString GetQuickHelpText() const override
static SfxObjectShell * Current()
virtual void set_item_tooltip_text(const OString &rIdent, const OUString &rTip)=0
const Color & GetControlBackground() const
virtual OUString get_item_tooltip_text(const OString &rIdent) const =0
virtual VclPtr< VirtualDevice > CreateVirtualDevice() const override
virtual VclPtr< VirtualDevice > CreateVirtualDevice() const override
constexpr::Color COL_AUTO(0xFF, 0xFF, 0xFF, 0xFF)
constexpr::Color COL_TRANSPARENT(0xFF, 0xFF, 0xFF, 0xFF)
Definition: xdash.hxx:31
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: xattr.cxx:381
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
ToolboxButtonColorUpdaterBase(bool bWideButton, const OUString &rCommandLabel, const OUString &rCommandURL, const css::uno::Reference< css::frame::XFrame > &rFrame)
bool GetHighContrastMode() const
virtual vcl::ImageType GetImageSize() const =0
const XDash & GetDash() const
Definition: xtable.hxx:80
VclToolboxButtonColorUpdater(sal_uInt16 nSlotId, sal_uInt16 nTbxBtnId, ToolBox *ptrTbx, bool bWideButton, const OUString &rCommandLabel, const OUString &rCommandURL, const css::uno::Reference< css::frame::XFrame > &rFrame)
virtual void SetImage(VirtualDevice *pVirDev) override
virtual void SetQuickHelpText(const OUString &rText) override
const SfxPoolItem * GetItem(sal_uInt16 nSlotId) const
const XDashListRef & GetDashList() const
Definition: drawitem.hxx:171
const Color & GetDisableColor() const
int i
virtual void SetQuickHelpText(const OUString &rText) override
Reference< graphic::XGraphic > GetXGraphicForCommand(const OUString &rsCommandName, const Reference< frame::XFrame > &rxFrame, vcl::ImageType eImageType)
virtual void SetQuickHelpText(const OUString &rText)=0
virtual Size GetItemSize(const Size &rImageSize) const =0
const XDash & GetDashValue() const
Definition: xlndsit.hxx:57
Size GetItemContentSize(sal_uInt16 nItemId)
#define COL_DEFAULT_SHAPE_STROKE
Definition: xdef.hxx:85
void Exclude(const tools::Rectangle &rRegion)
virtual vcl::ImageType GetImageSize() const override
ToolboxButtonColorUpdater(sal_uInt16 nSlotId, const OString &rTbxBtnId, weld::Toolbar *ptrTbx, bool bWideButton, const OUString &rCommandLabel, const css::uno::Reference< css::frame::XFrame > &rFrame)
helper class to update a color in a toolbox button image
void SetItemImage(sal_uInt16 nItemId, const Image &rImage)
#define COL_DEFAULT_HIGHLIGHT
Definition: xdef.hxx:83
virtual void SetImage(VirtualDevice *pVirDev)=0
static VclPtr< reference_type > Create(Arg &&...arg)
virtual VclPtr< VirtualDevice > CreateVirtualDevice() const =0
virtual vcl::ImageType get_icon_size() const =0
virtual VclPtr< VirtualDevice > create_virtual_device() const =0
BitmapEx GetBitmapEx(const Point &rSrcPt, const Size &rSize) const
VirtualDevice * get() const
css::uno::Reference< css::frame::XFrame > mxFrame
ImageType
sal_Int32 nLength
virtual vcl::ImageType GetImageSize() const override
virtual void set_item_image(const OString &rIdent, const css::uno::Reference< css::graphic::XGraphic > &rIcon)=0
virtual bool PutValue(const css::uno::Any &rVal, sal_uInt8 nMemberId) override
Definition: xattr.cxx:725
vcl::ImageType GetImageSize() const
#define COL_DEFAULT_FRAMELINE
Definition: xdef.hxx:82
void Update(const css::frame::FeatureStateEvent &rEvent)
virtual void SetImage(VirtualDevice *pVirDev) override
void Update(const NamedColor &rNamedColor)
EnumT GetValue() const