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 <svx/tbxcolorupdate.hxx>
23 #include <svx/svxids.hrc>
24 #include <svx/unomid.hxx>
25 #include <svx/xdef.hxx>
26 #include <svx/xlineit0.hxx>
27 #include <svx/xlndsit.hxx>
28 
30 #include <vcl/svapp.hxx>
31 #include <vcl/toolbox.hxx>
32 #include <vcl/virdev.hxx>
33 #include <vcl/weld.hxx>
34 #include <vcl/settings.hxx>
35 #include <tools/debug.hxx>
36 
37 #include <svx/strings.hrc>
38 #include <svx/dialmgr.hxx>
39 
40 namespace svx
41 {
42  ToolboxButtonColorUpdaterBase::ToolboxButtonColorUpdaterBase(bool bWideButton, const OUString& rCommandLabel,
43  const OUString& rCommandURL,
44  const css::uno::Reference<css::frame::XFrame>& rFrame)
45  : mbWideButton(bWideButton)
46  , mbWasHiContrastMode(Application::GetSettings().GetStyleSettings().GetHighContrastMode())
47  , maCurColor(COL_TRANSPARENT)
48  , maCommandLabel(rCommandLabel)
49  , maCommandURL(rCommandURL)
50  , mxFrame(rFrame)
51  {
52  }
53 
54  void ToolboxButtonColorUpdaterBase::Init(sal_uInt16 nSlotId)
55  {
56  switch (nSlotId)
57  {
58  case SID_ATTR_CHAR_COLOR:
59  case SID_ATTR_CHAR_COLOR2:
60  Update(NamedColor(COL_DEFAULT_FONT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FONT)));
61  break;
62  case SID_FRAME_LINECOLOR:
63  Update(NamedColor(COL_DEFAULT_FRAMELINE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FRAMELINE)));
64  break;
65  case SID_ATTR_CHAR_COLOR_BACKGROUND:
66  case SID_ATTR_CHAR_BACK_COLOR:
67  case SID_BACKGROUND_COLOR:
68  case SID_TABLE_CELL_BACKGROUND_COLOR:
69  Update(NamedColor(COL_DEFAULT_HIGHLIGHT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_HIGHLIGHT)));
70  break;
71  case SID_ATTR_LINE_COLOR:
72  Update(NamedColor(COL_DEFAULT_SHAPE_STROKE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_STROKE)));
73  break;
74  case SID_ATTR_FILL_COLOR:
75  Update(NamedColor(COL_DEFAULT_SHAPE_FILLING, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_FILLING)));
76  break;
77  default:
79  }
80  }
81 
83  sal_uInt16 nSlotId, sal_uInt16 nTbxBtnId, ToolBox* pToolBox, bool bWideButton,
84  const OUString& rCommandLabel, const OUString& rCommandURL,
85  const css::uno::Reference<css::frame::XFrame>& rFrame)
86  : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, rCommandURL, rFrame)
87  , mnBtnId(nTbxBtnId)
88  , mpTbx(pToolBox)
89  {
90  Init(nSlotId);
91  }
92 
94  {
96  }
97 
99  {
100  return mpTbx->GetQuickHelpText(mnBtnId);
101  }
102 
104  {
106  }
107 
109  {
111  DeviceFormat::DEFAULT, DeviceFormat::DEFAULT);
112  xRet->SetBackground(mpTbx->GetControlBackground());
113  return xRet;
114  }
115 
117  {
118  return mpTbx->GetImageSize();
119  }
120 
122  {
123  if (mbWideButton)
125  Image aImage(mpTbx->GetItemImage(mnBtnId));
126  return aImage.GetSizePixel();
127  }
128 
130  {}
131 
133  {
134  Update(rNamedColor.first);
135  if (!mbWideButton)
136  {
137  // Also show the current color as QuickHelpText
138  OUString colorSuffix = OUString(" (%1)").replaceFirst("%1", rNamedColor.second);
139  OUString colorHelpText = maCommandLabel + colorSuffix;
140 
141  SetQuickHelpText(colorHelpText);
142  }
143  }
144 
145  void ToolboxButtonColorUpdaterBase::Update(const Color& rColor, bool bForceUpdate)
146  {
147  Size aItemSize(GetItemSize());
148 
149 #ifdef IOS // tdf#126966
150  // Oddly enough, it is in the "not wide button" case that we want the larger ones, hmm.
151  if (!mbWideButton)
152  {
153  // usually the normal size is 16
154  const long nIOSSize = 32;
155  if (aItemSize.getWidth() < nIOSSize)
156  {
157  aItemSize.setWidth(nIOSSize);
158  aItemSize.setHeight(nIOSSize);
159  }
160  }
161 #endif
162  const bool bSizeChanged = (maBmpSize != aItemSize);
163  const bool bDisplayModeChanged = (mbWasHiContrastMode != Application::GetSettings().GetStyleSettings().GetHighContrastMode());
164  Color aColor(rColor);
165 
166  // !!! #109290# Workaround for SetFillColor with COL_AUTO
167  if (aColor == COL_AUTO)
168  aColor = COL_TRANSPARENT;
169 
170  if ((maCurColor == aColor) && !bSizeChanged && !bDisplayModeChanged && !bForceUpdate)
171  return;
172 
173  if (!aItemSize.Width() || !aItemSize.Height())
174  return;
175 
177  pVirDev->SetOutputSizePixel(aItemSize);
178  maBmpSize = aItemSize;
179 
181  pVirDev->DrawImage(Point(0, 0), Image(xImage));
182 
183  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
184  mbWasHiContrastMode = rStyleSettings.GetHighContrastMode();
185 
186  if ((COL_TRANSPARENT != aColor) && (maBmpSize.Width() == maBmpSize.Height()))
187  pVirDev->SetLineColor(aColor);
188  else
189  pVirDev->SetLineColor(rStyleSettings.GetDisableColor());
190 
191  // use not only COL_TRANSPARENT for detection of transparence,
192  // but the method/way which is designed to do that
193  const bool bIsTransparent(0xff == aColor.GetTransparency());
194  maCurColor = aColor;
195 
196  if (bIsTransparent)
197  {
198  pVirDev->SetFillColor(Color(255, 0, 0, 0));
199  }
200  else
201  {
202  pVirDev->SetFillColor(maCurColor);
203  }
204 
205  if (maBmpSize.Width() == maBmpSize.Height())
206  // tdf#84985 align color bar with icon bottom edge; integer arithmetic e.g. 26 - 26/4 <> 26 * 3/4
208  else
210 
211  pVirDev->DrawRect(maUpdRect);
212 
213  SetImage(pVirDev.get());
214  }
215 
217  {
218  OUString sColorName = GetQuickHelpText();
219  // The obtained string is of format: color context (color name)
220  // Generate a substring which contains only the color name
221  sal_Int32 nStart = sColorName.indexOf('(');
222  sColorName = sColorName.copy( nStart + 1 );
223  sal_Int32 nLength = sColorName.getLength();
224  if(nLength > 0)
225  sColorName = sColorName.copy( 0, nLength - 1);
226  return sColorName;
227  }
228 
229  ToolboxButtonColorUpdater::ToolboxButtonColorUpdater(sal_uInt16 nSlotId, const OString& rTbxBtnId, weld::Toolbar* ptrTbx, bool bWideButton,
230  const OUString& rCommandLabel, const css::uno::Reference<css::frame::XFrame>& rFrame)
231  : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, OUString::fromUtf8(rTbxBtnId), rFrame)
232  , msBtnId(rTbxBtnId)
233  , mpTbx(ptrTbx)
234  {
235  Init(nSlotId);
236  }
237 
239  {
241  }
242 
244  {
246  }
247 
249  {
250  mpTbx->set_item_image(msBtnId, pVirDev);
251  }
252 
254  {
255  return mpTbx->create_virtual_device();
256  }
257 
259  {
260  return mpTbx->get_icon_size();
261  }
262 
264  {
265  vcl::ImageType eImageType = GetImageSize();
266  int nHeight(16);
267  switch (eImageType)
268  {
270  nHeight = 16;
271  break;
273  nHeight = 26;
274  break;
276  nHeight = 32;
277  break;
278  }
279  int nWidth = mbWideButton ? nHeight * 5 : nHeight;
280  return Size(nWidth, nHeight);
281  }
282 
284  : m_eXLS(css::drawing::LineStyle_NONE)
285  , m_nDashStyleIndex(-1)
286  {
287  }
288 
289  void ToolboxButtonLineStyleUpdater::Update(const com::sun::star::frame::FeatureStateEvent& rEvent)
290  {
291  if (rEvent.FeatureURL.Complete == ".uno:LineDash")
292  {
293  m_nDashStyleIndex = -1;
294 
296  if (!pSh)
297  return;
298  const SvxDashListItem* pItem = pSh->GetItem( SID_DASH_LIST );
299  if (!pItem)
300  return;
301 
302  XLineDashItem aDashItem;
303  aDashItem.PutValue(rEvent.State, 0);
304  const XDash& rDash = aDashItem.GetDashValue();
305 
306  XDashListRef xLineStyleList = pItem->GetDashList();
307  for (long i = 0; i < xLineStyleList->Count(); ++i)
308  {
309  const XDashEntry* pEntry = xLineStyleList->GetDash(i);
310  const XDash& rEntry = pEntry->GetDash();
311  if (rDash == rEntry)
312  {
314  break;
315  }
316  }
317  }
318  else if (rEvent.FeatureURL.Complete == ".uno:XLineStyle")
319  {
320  XLineStyleItem aLineStyleItem;
321  aLineStyleItem.PutValue(rEvent.State, 0);
322 
323  m_eXLS = aLineStyleItem.GetValue();
324  }
325  }
326 
328  {
329  int nRet;
330  switch (m_eXLS)
331  {
332  case css::drawing::LineStyle_NONE:
333  nRet = 0;
334  break;
335  case css::drawing::LineStyle_SOLID:
336  nRet = 1;
337  break;
338  default:
339  nRet = m_nDashStyleIndex + 2;
340  break;
341  }
342  return nRet;
343  }
344 }
345 
346 /* 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:27
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
Image GetItemImage(sal_uInt16 nItemId) const
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 OUString GetQuickHelpText() const override
#define COL_DEFAULT_FONT
Definition: xdef.hxx:81
virtual Size GetItemSize() const =0
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:390
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
virtual Size GetItemSize() const override
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
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
int i
const XDash & GetDashValue() const
Definition: xlndsit.hxx:57
virtual Size GetItemSize() const override
Size GetItemContentSize(sal_uInt16 nItemId)
#define COL_DEFAULT_SHAPE_STROKE
Definition: xdef.hxx:85
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
virtual vcl::ImageType GetImageSize() const override
sal_Int32 const nLength
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:734
long getWidth() const
void setWidth(long nWidth)
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
void setHeight(long nHeight)