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/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  , maCommandLabel(rCommandLabel)
47  , maCommandURL(rCommandURL)
48  , mxFrame(rFrame)
49  {
50  }
51 
52  void ToolboxButtonColorUpdaterBase::Init(sal_uInt16 nSlotId)
53  {
54  switch (nSlotId)
55  {
56  case SID_ATTR_CHAR_COLOR:
57  case SID_ATTR_CHAR_COLOR2:
58  Update(NamedColor(COL_DEFAULT_FONT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FONT)));
59  break;
60  case SID_FRAME_LINECOLOR:
61  Update(NamedColor(COL_DEFAULT_FRAMELINE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_FRAMELINE)));
62  break;
63  case SID_ATTR_CHAR_COLOR_BACKGROUND:
64  case SID_ATTR_CHAR_BACK_COLOR:
65  case SID_BACKGROUND_COLOR:
66  case SID_TABLE_CELL_BACKGROUND_COLOR:
67  Update(NamedColor(COL_DEFAULT_HIGHLIGHT, SvxResId(RID_SVXSTR_COLOR_DEFAULT_HIGHLIGHT)));
68  break;
69  case SID_ATTR_LINE_COLOR:
70  Update(NamedColor(COL_DEFAULT_SHAPE_STROKE, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_STROKE)));
71  break;
72  case SID_ATTR_FILL_COLOR:
73  Update(NamedColor(COL_DEFAULT_SHAPE_FILLING, SvxResId(RID_SVXSTR_COLOR_DEFAULT_SHAPE_FILLING)));
74  break;
75  default:
77  }
78  }
79 
81  sal_uInt16 nSlotId, sal_uInt16 nTbxBtnId, ToolBox* pToolBox, bool bWideButton,
82  const OUString& rCommandLabel, const OUString& rCommandURL,
83  const css::uno::Reference<css::frame::XFrame>& rFrame)
84  : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, rCommandURL, rFrame)
85  , mnBtnId(nTbxBtnId)
86  , mpTbx(pToolBox)
87  {
88  Init(nSlotId);
89  }
90 
92  {
94  }
95 
97  {
99  }
100 
102  {
104  }
105 
107  {
109  DeviceFormat::DEFAULT, DeviceFormat::DEFAULT);
110  xRet->SetBackground(mpTbx->GetControlBackground());
111  return xRet;
112  }
113 
115  {
116  return mpTbx->GetImageSize();
117  }
118 
120  {
121  if (mbWideButton)
123  Image aImage(mpTbx->GetItemImage(mnBtnId));
124  return aImage.GetSizePixel();
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  Size aItemSize(GetItemSize());
146 
147 #ifdef IOS // tdf#126966
148  // Oddly enough, it is in the "not wide button" case that we want the larger ones, hmm.
149  if (!mbWideButton)
150  {
151  // usually the normal size is 16
152  const long nIOSSize = 32;
153  if (aItemSize.getWidth() < nIOSSize)
154  {
155  aItemSize.setWidth(nIOSSize);
156  aItemSize.setHeight(nIOSSize);
157  }
158  }
159 #endif
160  const bool bSizeChanged = (maBmpSize != aItemSize);
161  const bool bDisplayModeChanged = (mbWasHiContrastMode != Application::GetSettings().GetStyleSettings().GetHighContrastMode());
162  Color aColor(rColor);
163 
164  // !!! #109290# Workaround for SetFillColor with COL_AUTO
165  if (aColor == COL_AUTO)
166  aColor = COL_TRANSPARENT;
167 
168  if ((maCurColor == aColor) && !bSizeChanged && !bDisplayModeChanged && !bForceUpdate)
169  return;
170 
171  if (!aItemSize.Width() || !aItemSize.Height())
172  return;
173 
175  pVirDev->SetOutputSizePixel(aItemSize);
176  maBmpSize = aItemSize;
177 
179  pVirDev->DrawImage(Point(0, 0), Image(xImage));
180 
181  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
182  mbWasHiContrastMode = rStyleSettings.GetHighContrastMode();
183 
184  if ((COL_TRANSPARENT != aColor) && (maBmpSize.Width() == maBmpSize.Height()))
185  pVirDev->SetLineColor(aColor);
186  else
187  pVirDev->SetLineColor(rStyleSettings.GetDisableColor());
188 
189  // use not only COL_TRANSPARENT for detection of transparence,
190  // but the method/way which is designed to do that
191  const bool bIsTransparent(0xff == aColor.GetTransparency());
192  maCurColor = aColor;
193 
194  if (bIsTransparent)
195  {
196  pVirDev->SetFillColor(Color(255, 0, 0, 0));
197  }
198  else
199  {
200  pVirDev->SetFillColor(maCurColor);
201  }
202 
203  if (maBmpSize.Width() == maBmpSize.Height())
204  // tdf#84985 align color bar with icon bottom edge; integer arithmetic e.g. 26 - 26/4 <> 26 * 3/4
206  else
208 
209  pVirDev->DrawRect(maUpdRect);
210 
211  SetImage(pVirDev.get());
212  }
213 
215  {
216  OUString sColorName = GetQuickHelpText();
217  // The obtained string is of format: color context (color name)
218  // Generate a substring which contains only the color name
219  sal_Int32 nStart = sColorName.indexOf('(');
220  sColorName = sColorName.copy( nStart + 1 );
221  sal_Int32 nLength = sColorName.getLength();
222  if(nLength > 0)
223  sColorName = sColorName.copy( 0, nLength - 1);
224  return sColorName;
225  }
226 
227  ToolboxButtonColorUpdater::ToolboxButtonColorUpdater(sal_uInt16 nSlotId, const OString& rTbxBtnId, weld::Toolbar* ptrTbx, bool bWideButton,
228  const OUString& rCommandLabel, const css::uno::Reference<css::frame::XFrame>& rFrame)
229  : ToolboxButtonColorUpdaterBase(bWideButton, rCommandLabel, OUString::fromUtf8(rTbxBtnId), rFrame)
230  , msBtnId(rTbxBtnId)
231  , mpTbx(ptrTbx)
232  {
233  Init(nSlotId);
234  }
235 
237  {
239  }
240 
242  {
244  }
245 
247  {
248  mpTbx->set_item_image(msBtnId, pVirDev);
249  }
250 
252  {
253  return mpTbx->create_virtual_device();
254  }
255 
257  {
258  return mpTbx->get_icon_size();
259  }
260 
262  {
263  vcl::ImageType eImageType = GetImageSize();
264  int nHeight(16);
265  switch (eImageType)
266  {
268  nHeight = 16;
269  break;
271  nHeight = 26;
272  break;
274  nHeight = 32;
275  break;
276  }
277  int nWidth = mbWideButton ? nHeight * 5 : nHeight;
278  return Size(nWidth, nHeight);
279  }
280 
282  : m_eXLS(css::drawing::LineStyle_NONE)
283  , m_nDashStyleIndex(-1)
284  {
285  }
286 
287  void ToolboxButtonLineStyleUpdater::Update(const com::sun::star::frame::FeatureStateEvent& rEvent)
288  {
289  if (rEvent.FeatureURL.Complete == ".uno:LineDash")
290  {
291  m_nDashStyleIndex = -1;
292 
294  if (!pSh)
295  return;
296  const SvxDashListItem* pItem = pSh->GetItem( SID_DASH_LIST );
297  if (!pItem)
298  return;
299 
300  XLineDashItem aDashItem;
301  aDashItem.PutValue(rEvent.State, 0);
302  const XDash& rDash = aDashItem.GetDashValue();
303 
304  XDashListRef xLineStyleList = pItem->GetDashList();
305  for (long i = 0; i < xLineStyleList->Count(); ++i)
306  {
307  const XDashEntry* pEntry = xLineStyleList->GetDash(i);
308  const XDash& rEntry = pEntry->GetDash();
309  if (rDash == rEntry)
310  {
312  break;
313  }
314  }
315  }
316  else if (rEvent.FeatureURL.Complete == ".uno:XLineStyle")
317  {
318  XLineStyleItem aLineStyleItem;
319  aLineStyleItem.PutValue(rEvent.State, 0);
320 
321  m_eXLS = aLineStyleItem.GetValue();
322  }
323  }
324 
326  {
327  int nRet;
328  switch (m_eXLS)
329  {
330  case css::drawing::LineStyle_NONE:
331  nRet = 0;
332  break;
333  case css::drawing::LineStyle_SOLID:
334  nRet = 1;
335  break;
336  default:
337  nRet = m_nDashStyleIndex + 2;
338  break;
339  }
340  return nRet;
341  }
342 }
343 
344 /* 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:381
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
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
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
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
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)