LibreOffice Module vcl (master)  1
iconview.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 
22 #include <iconview.hxx>
23 #include "iconviewimpl.hxx"
24 #include <vcl/uitest/uiobject.hxx>
25 #include <tools/json_writer.hxx>
26 #include <vcl/toolkit/svlbitm.hxx>
27 #include <tools/stream.hxx>
28 #include <vcl/cvtgrf.hxx>
29 #include <comphelper/base64.hxx>
30 
32  : SvTreeListBox(pParent, nBits)
33 {
34  nColumns = 1;
35  mbCenterAndClipText = true;
36  SetEntryHeight(100);
37  SetEntryWidth(100);
38 
39  pImpl.reset(new IconViewImpl(this, GetModel(), GetStyle()));
40 }
41 
43 {
45 
46  if (!aBoxSize.Width())
47  return;
48 
49  SetSizePixel(aBoxSize);
50 
51  nColumns = aBoxSize.Width() / nEntryWidth;
52 
54 }
55 
57 {
58  Size aSize;
59  aSize.setHeight(nEntryHeight);
60  aSize.setWidth(nEntryWidth);
61 
62  Point aPos;
63  aPos.setX(0);
64  aPos.setY(0);
65 
66  tools::Rectangle aRect;
67 
68  short nCols = GetColumnsCount();
69 
70  if (nCols)
71  {
72  aPos.setY((nEntryPos / nCols) * nEntryHeight);
73  aPos.setX((nEntryPos % nCols) * nEntryWidth);
74  }
75 
76  aRect.SetPos(aPos);
77  aRect.SetSize(aSize);
78 
79  return aRect;
80 }
81 
83  vcl::RenderContext& rRenderContext)
84 {
85  const int nSpacing = 5; // 5 pixels from top, from bottom, between icon and label
86 
87  pImpl->UpdateContextBmpWidthMax(&rEntry);
88 
89  short nTempEntryHeight = GetEntryHeight();
90  short nTempEntryWidth = GetEntryWidth();
91 
92  Point aEntryPos(nX, nY);
93 
94  const Color aBackupTextColor(rRenderContext.GetTextColor());
95  const vcl::Font aBackupFont(rRenderContext.GetFont());
96  const Color aBackupColor = rRenderContext.GetFillColor();
97 
98  const StyleSettings& rSettings = rRenderContext.GetSettings().GetStyleSettings();
99 
100  const Size aOutputSize = GetOutputSizePixel();
101  if (aOutputSize.getHeight() < nTempEntryHeight)
102  nTempEntryHeight = aOutputSize.getHeight();
103 
104  const SvViewDataEntry* pViewDataEntry = GetViewDataEntry(&rEntry);
105 
106  bool bCurFontIsSel = false;
107  if (pViewDataEntry->IsHighlighted())
108  {
109  vcl::Font aHighlightFont(rRenderContext.GetFont());
110  const Color aHighlightTextColor(rSettings.GetHighlightTextColor());
111  aHighlightFont.SetColor(aHighlightTextColor);
112 
113  // set font color to highlight
114  rRenderContext.SetTextColor(aHighlightTextColor);
115  rRenderContext.SetFont(aHighlightFont);
116  bCurFontIsSel = true;
117  }
118 
119  bool bFillColorSet = false;
120  // draw background
122  {
123  // set background pattern/color
124  Wallpaper aWallpaper = rRenderContext.GetBackground();
125 
126  if (pViewDataEntry->IsHighlighted())
127  {
128  Color aNewWallColor = rSettings.GetHighlightColor();
129  // if the face color is bright then the deactivate color is also bright
130  // -> so you can't see any deactivate selection
131  const WinBits nWindowStyle = GetStyle();
132  const bool bHideSelection = (nWindowStyle & WB_HIDESELECTION) != 0 && !HasFocus();
133  if (bHideSelection && !rSettings.GetFaceColor().IsBright()
134  && aWallpaper.GetColor().IsBright() != rSettings.GetDeactiveColor().IsBright())
135  {
136  aNewWallColor = rSettings.GetDeactiveColor();
137  }
138  aWallpaper.SetColor(aNewWallColor);
139  }
140  else // no selection
141  {
142  aWallpaper.SetColor(rEntry.GetBackColor());
143  }
144 
145  Color aBackgroundColor = aWallpaper.GetColor();
146  if (aBackgroundColor != COL_TRANSPARENT)
147  {
148  rRenderContext.SetFillColor(aBackgroundColor);
149  bFillColorSet = true;
150  // this case may occur for smaller horizontal resizes
151  if (nTempEntryWidth > 1)
152  rRenderContext.DrawRect({ aEntryPos, Size(nTempEntryWidth, nTempEntryHeight) });
153  }
154  }
155 
156  const size_t nItemCount = rEntry.ItemCount();
157  size_t nIconItem = nItemCount;
158 
159  int nLabelHeight = 0;
160  std::vector<size_t> aTextItems;
161 
162  for (size_t nCurItem = 0; nCurItem < nItemCount; ++nCurItem)
163  {
164  SvLBoxItem& rItem = rEntry.GetItem(nCurItem);
165  SvLBoxItemType nItemType = rItem.GetType();
166 
167  if (nItemType == SvLBoxItemType::ContextBmp)
168  {
169  nIconItem = nCurItem;
170  continue;
171  }
172 
173  aTextItems.push_back(nCurItem);
174  auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
175  nLabelHeight += nItemHeight;
176  }
177 
178  int nLabelYPos = nY + nTempEntryHeight - nLabelHeight - nSpacing; // padding from bottom
179  for (auto nCurItem : aTextItems)
180  {
181  aEntryPos.setY(nLabelYPos);
182 
183  auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
184  nLabelYPos += nItemHeight;
185 
186  rEntry.GetItem(nCurItem).Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
187  }
188 
189  if (bFillColorSet)
190  rRenderContext.SetFillColor(aBackupColor);
191 
192  // draw icon
193  if (nIconItem < nItemCount)
194  {
195  SvLBoxItem& rItem = rEntry.GetItem(nIconItem);
196  auto nItemWidth = rItem.GetWidth(this, pViewDataEntry, nIconItem);
197  auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nIconItem);
198 
199  aEntryPos.setY(nY);
200 
201  // center horizontally
202  aEntryPos.AdjustX((nTempEntryWidth - nItemWidth) / 2);
203  // center vertically
204  int nImageAreaHeight = nTempEntryHeight - nSpacing * 2; // spacings from top, from bottom
205  if (nLabelHeight > 0)
206  {
207  nImageAreaHeight -= nLabelHeight + nSpacing; // spacing between icon and label
208  }
209  aEntryPos.AdjustY((nImageAreaHeight - nItemHeight) / 2 + nSpacing);
210 
211  rItem.Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
212  }
213 
214  if (bCurFontIsSel)
215  {
216  rRenderContext.SetTextColor(aBackupTextColor);
217  rRenderContext.SetFont(aBackupFont);
218  }
219 }
220 
222 
223 static OUString extractPngString(const SvLBoxContextBmp* pBmpItem)
224 {
225  BitmapEx aImage = pBmpItem->GetBitmap1().GetBitmapEx();
226  SvMemoryStream aOStm(65535, 65535);
228  {
229  css::uno::Sequence<sal_Int8> aSeq(static_cast<sal_Int8 const*>(aOStm.GetData()),
230  aOStm.Tell());
231  OUStringBuffer aBuffer("data:image/png;base64,");
233  return aBuffer.makeStringAndClear();
234  }
235 
236  return "";
237 }
238 
240  const SvTreeListBox* pTabListBox)
241 {
242  while (pEntry)
243  {
244  auto aNode = rJsonWriter.startStruct();
245 
246  // simple listbox value
247  const SvLBoxItem* pIt = pEntry->GetFirstItem(SvLBoxItemType::String);
248  if (pIt)
249  rJsonWriter.put("text", static_cast<const SvLBoxString*>(pIt)->GetText());
250 
252  if (pIt)
253  {
254  const SvLBoxContextBmp* pBmpItem = static_cast<const SvLBoxContextBmp*>(pIt);
255  if (pBmpItem)
256  rJsonWriter.put("image", extractPngString(pBmpItem));
257  }
258 
259  if (pTabListBox->IsSelected(pEntry))
260  rJsonWriter.put("selected", "true");
261 
262  rJsonWriter.put("row",
263  OString::number(pTabListBox->GetModel()->GetAbsPos(pEntry)).getStr());
264 
265  pEntry = pEntry->NextSibling();
266  }
267 }
268 
270 {
272  rJsonWriter.put("type", "iconview");
273  auto aNode = rJsonWriter.startArray("entries");
274  lcl_DumpEntryAndSiblings(rJsonWriter, First(), this);
275 }
276 
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IconView(vcl::Window *pParent, WinBits nBits)
Definition: iconview.cxx:31
const Color & GetTextColor() const
Definition: outdev.hxx:1016
void SetPos(const Point &rPoint)
const Wallpaper & GetBackground() const
Definition: outdev.hxx:534
const Color & GetDeactiveColor() const
const Color & GetBackColor() const
void setWidth(tools::Long nWidth)
ScopedJsonWriterStruct startStruct()
const Color & GetHighlightTextColor() const
bool IsHighlighted() const
SvTreeList * GetModel() const
long Long
constexpr::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
const StyleSettings & GetStyleSettings() const
ScopedJsonWriterArray startArray(const char *)
bool mbCenterAndClipText
const Color & GetFaceColor() const
std::function< std::unique_ptr< UIObject >vcl::Window *)> FactoryFunction
virtual void SetSizePixel(const Size &rNewSize)
Definition: window2.cxx:1285
void SetColor(const Color &rColor)
Definition: wall.cxx:156
sal_uInt32 GetAbsPos(const SvTreeListEntry *pEntry) const
Definition: treelist.cxx:820
short GetColumnsCount() const
View-dependent data for a tree list entry created in the virtual function SvTreeListBox::CreateViewDa...
const Color & GetHighlightColor() const
constexpr tools::Long Width() const
SvLBoxItemType
int GetHeight(const SvTreeListBox *pView, const SvTreeListEntry *pEntry) const
sal_Int64 WinBits
virtual void DumpAsPropertyTree(tools::JsonWriter &)
Dumps itself and potentially its children to a property tree, to be written easily to JSON...
Definition: window.cxx:3363
const vcl::Font & GetFont() const
Definition: outdev.hxx:540
virtual FactoryFunction GetUITestFactory() const override
Definition: iconview.cxx:221
SvTreeListEntry * NextSibling() const
constexpr tools::Long getHeight() const
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
short GetEntryHeight() const
void DrawRect(const tools::Rectangle &rRect)
Definition: rect.cxx:51
virtual void Paint(const Point &rPos, SvTreeListBox &rOutDev, vcl::RenderContext &rRenderContext, const SvViewDataEntry *pView, const SvTreeListEntry &rEntry)=0
short GetEntryWidth() const
size_t ItemCount() const
void SetEntryWidth(short nWidth)
void SetSize(const Size &rSize)
bool IsBright() const
virtual void Resize() override
static void lcl_DumpEntryAndSiblings(tools::JsonWriter &rJsonWriter, SvTreeListEntry *pEntry, const SvTreeListBox *pTabListBox)
Definition: iconview.cxx:239
const Color & GetColor() const
Definition: wall.hxx:71
bool IsSelected(const SvTreeListEntry *pEntry) const
Definition: treelist.cxx:1324
void SetFillColor()
Definition: fill.cxx:29
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:175
std::unique_ptr< SvImpLBox > pImpl
void SetTextColor(const Color &rColor)
Definition: text.cxx:714
SvViewDataEntry * GetViewDataEntry(SvTreeListEntry const *pEntry) const
virtual tools::Rectangle GetFocusRect(const SvTreeListEntry *, tools::Long nEntryPos) override
Definition: iconview.cxx:56
const Image & GetBitmap1() const
Definition: svlbitm.hxx:275
const AllSettings & GetSettings() const
Definition: outdev.hxx:295
const SvLBoxItem & GetItem(size_t nPos) const
vcl::Window * GetParent() const
Definition: window2.cxx:1120
virtual void Resize() override
Definition: iconview.cxx:42
void put(const char *pPropName, const OUString &rPropValue)
virtual SvLBoxItemType GetType() const =0
int GetWidth(const SvTreeListBox *pView, const SvTreeListEntry *pEntry) const
SvTreeListEntry * First() const
static ErrCode Export(SvStream &rOStm, const Graphic &rGraphic, ConvertDataFormat nFormat)
Definition: cvtgrf.cxx:51
WinBits const WB_HIDESELECTION
static OUString extractPngString(const SvLBoxContextBmp *pBmpItem)
Definition: iconview.cxx:223
std::unique_ptr< char[]> aBuffer
Size GetOutputSizePixel() const
#define ERRCODE_NONE
Definition: errcode.hxx:196
void SetFont(const vcl::Font &rNewFont)
Definition: outdev/font.cxx:53
VCL_DLLPRIVATE void SetEntryHeight(SvTreeListEntry const *pEntry)
const SvLBoxItem * GetFirstItem(SvLBoxItemType eType) const
sal_uInt64 Tell() const
Sequence< sal_Int8 > aSeq
BitmapEx GetBitmapEx() const
Definition: Image.cxx:94
Size GetOutputSizePixel() const
Definition: window3.cxx:89
void setHeight(tools::Long nHeight)
WinBits GetStyle() const
Definition: window2.cxx:976
static std::unique_ptr< UIObject > create(vcl::Window *pWindow)
void PaintEntry(SvTreeListEntry &, tools::Long nX, tools::Long nY, vcl::RenderContext &rRenderContext)
Definition: iconview.cxx:82
SvTreeFlags nTreeFlags
bool HasFocus() const
Definition: window.cxx:2986
friend class IconViewImpl
virtual void DumpAsPropertyTree(tools::JsonWriter &rJsonWriter) override
Dumps itself and potentially its children to a property tree, to be written easily to JSON...
Definition: iconview.cxx:269
const Color & GetFillColor() const
Definition: outdev.hxx:526
const void * GetData()