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  tools::Rectangle aRect; // multi purpose
86 
87  pImpl->UpdateContextBmpWidthMax(&rEntry);
88 
89  short nTempEntryHeight = GetEntryHeight();
90  short nTempEntryWidth = GetEntryWidth();
91 
92  Point aEntryPos;
93 
94  Color aBackupTextColor(rRenderContext.GetTextColor());
95  vcl::Font aBackupFont(rRenderContext.GetFont());
96  Color aBackupColor = rRenderContext.GetFillColor();
97 
98  bool bCurFontIsSel = false;
99  const WinBits nWindowStyle = GetStyle();
100  const bool bHideSelection = (nWindowStyle & WB_HIDESELECTION) != 0 && !HasFocus();
101  const StyleSettings& rSettings = rRenderContext.GetSettings().GetStyleSettings();
102 
103  vcl::Font aHighlightFont(rRenderContext.GetFont());
104  const Color aHighlightTextColor(rSettings.GetHighlightTextColor());
105  aHighlightFont.SetColor(aHighlightTextColor);
106 
107  Size aOutputSize = GetOutputSizePixel();
108  if (aOutputSize.getHeight() < nTempEntryHeight)
109  nTempEntryHeight = aOutputSize.getHeight();
110 
111  Size aRectSize(nTempEntryWidth, nTempEntryHeight);
112 
113  SvViewDataEntry* pViewDataEntry = GetViewDataEntry(&rEntry);
114 
115  sal_uInt16 nItemCount = rEntry.ItemCount();
116  sal_uInt16 nCurItem = 0;
117  sal_uInt16 nIconItem = nItemCount;
118 
119  while (nCurItem < nItemCount)
120  {
121  SvLBoxItem* pItem = nCurItem < nItemCount ? &rEntry.GetItem(nCurItem) : nullptr;
122  SvLBoxItemType nItemType = pItem->GetType();
123 
124  if (nItemType == SvLBoxItemType::ContextBmp)
125  {
126  nIconItem = nCurItem;
127  nCurItem++;
128  continue;
129  }
130 
131  auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
132 
133  aEntryPos.setX(nX);
134  aEntryPos.setY(nY);
135 
136  // set background pattern/color
137 
138  Wallpaper aWallpaper = rRenderContext.GetBackground();
139 
140  if (pViewDataEntry->IsHighlighted())
141  {
142  Color aNewWallColor = rSettings.GetHighlightColor();
143  // if the face color is bright then the deactivate color is also bright
144  // -> so you can't see any deactivate selection
145  if (bHideSelection && !rSettings.GetFaceColor().IsBright()
146  && aWallpaper.GetColor().IsBright() != rSettings.GetDeactiveColor().IsBright())
147  {
148  aNewWallColor = rSettings.GetDeactiveColor();
149  }
150  // set font color to highlight
151  if (!bCurFontIsSel)
152  {
153  rRenderContext.SetTextColor(aHighlightTextColor);
154  rRenderContext.SetFont(aHighlightFont);
155  bCurFontIsSel = true;
156  }
157  aWallpaper.SetColor(aNewWallColor);
158  }
159  else // no selection
160  {
161  if (bCurFontIsSel)
162  {
163  bCurFontIsSel = false;
164  rRenderContext.SetTextColor(aBackupTextColor);
165  rRenderContext.SetFont(aBackupFont);
166  }
167  else
168  {
169  aWallpaper.SetColor(rEntry.GetBackColor());
170  }
171  }
172 
173  // draw background
175  {
176  aRect.SetPos(aEntryPos);
177  aRect.SetSize(aRectSize);
178 
179  Color aBackgroundColor = aWallpaper.GetColor();
180  if (aBackgroundColor != COL_TRANSPARENT)
181  {
182  rRenderContext.SetFillColor(aBackgroundColor);
183  // this case may occur for smaller horizontal resizes
184  if (aRect.Left() < aRect.Right())
185  rRenderContext.DrawRect(aRect);
186  }
187  }
188 
189  // center vertically
190  aEntryPos.AdjustY((nTempEntryHeight - nItemHeight) / 2);
191 
192  aEntryPos.AdjustY(15);
193 
194  pItem->Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
195 
196  rRenderContext.SetFillColor(aBackupColor);
197 
198  nCurItem++;
199  }
200 
201  // draw icon
202  if (nIconItem != nItemCount && nIconItem < nItemCount)
203  {
204  SvLBoxItem* pItem = &rEntry.GetItem(nIconItem);
205  auto nItemWidth = pItem->GetWidth(this, pViewDataEntry, nIconItem);
206  auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nIconItem);
207 
208  aEntryPos.setX(nX);
209  aEntryPos.setY(nY);
210 
211  // center horizontally
212  aEntryPos.AdjustX((nTempEntryWidth - nItemWidth) / 2);
213  // center vertically
214  aEntryPos.AdjustY((nTempEntryHeight - nItemHeight) / 2);
215 
216  aEntryPos.AdjustY(-10);
217 
218  pItem->Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
219  }
220 
221  if (bCurFontIsSel)
222  {
223  rRenderContext.SetTextColor(aBackupTextColor);
224  rRenderContext.SetFont(aBackupFont);
225  }
226 }
227 
229 
230 static OUString extractPngString(const SvLBoxContextBmp* pBmpItem)
231 {
232  BitmapEx aImage = pBmpItem->GetBitmap1().GetBitmapEx();
233  SvMemoryStream aOStm(65535, 65535);
235  {
236  css::uno::Sequence<sal_Int8> aSeq(static_cast<sal_Int8 const*>(aOStm.GetData()),
237  aOStm.Tell());
238  OUStringBuffer aBuffer("data:image/png;base64,");
240  return aBuffer.makeStringAndClear();
241  }
242 
243  return "";
244 }
245 
247  SvTreeListBox* pTabListBox)
248 {
249  while (pEntry)
250  {
251  auto aNode = rJsonWriter.startStruct();
252 
253  // simple listbox value
254  const SvLBoxItem* pIt = pEntry->GetFirstItem(SvLBoxItemType::String);
255  if (pIt)
256  rJsonWriter.put("text", static_cast<const SvLBoxString*>(pIt)->GetText());
257 
259  if (pIt)
260  {
261  const SvLBoxContextBmp* pBmpItem = static_cast<const SvLBoxContextBmp*>(pIt);
262  if (pBmpItem)
263  rJsonWriter.put("image", extractPngString(pBmpItem));
264  }
265 
266  if (pTabListBox->IsSelected(pEntry))
267  rJsonWriter.put("selected", "true");
268 
269  rJsonWriter.put("row",
270  OString::number(pTabListBox->GetModel()->GetAbsPos(pEntry)).getStr());
271 
272  pEntry = pEntry->NextSibling();
273  }
274 }
275 
277 {
279  rJsonWriter.put("type", "iconview");
280  auto aNode = rJsonWriter.startArray("entries");
281  lcl_DumpEntryAndSiblings(rJsonWriter, First(), this);
282 }
283 
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IconView(vcl::Window *pParent, WinBits nBits)
Definition: iconview.cxx:31
const Color & GetTextColor() const
Definition: outdev.hxx:1136
void SetPos(const Point &rPoint)
const Wallpaper & GetBackground() const
Definition: outdev.hxx:647
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:1261
void SetColor(const Color &rColor)
Definition: wall.cxx:190
short GetColumnsCount() const
View-dependent data for a tree list entry created in the virtual function SvTreeListBox::CreateViewDa...
const Color & GetHighlightColor() const
SvLBoxItemType
int GetHeight(const SvTreeListBox *pView, const SvTreeListEntry *pEntry) const
sal_Int64 WinBits
tools::Long Left() const
virtual void DumpAsPropertyTree(tools::JsonWriter &)
Dumps itself and potentially its children to a property tree, to be written easily to JSON...
Definition: window.cxx:3341
const vcl::Font & GetFont() const
Definition: outdev.hxx:653
virtual FactoryFunction GetUITestFactory() const override
Definition: iconview.cxx:228
SvTreeListEntry * NextSibling() 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
tools::Long getHeight() const
short GetEntryWidth() const
size_t ItemCount() const
sal_uLong GetAbsPos(const SvTreeListEntry *pEntry) const
Definition: treelist.cxx:824
void SetEntryWidth(short nWidth)
static void lcl_DumpEntryAndSiblings(tools::JsonWriter &rJsonWriter, SvTreeListEntry *pEntry, SvTreeListBox *pTabListBox)
Definition: iconview.cxx:246
void SetSize(const Size &rSize)
bool IsBright() const
virtual void Resize() override
const Color & GetColor() const
Definition: wall.cxx:199
bool IsSelected(const SvTreeListEntry *pEntry) const
Definition: treelist.cxx:1328
void SetFillColor()
tools::Long Width() const
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:303
std::unique_ptr< SvImpLBox > pImpl
void SetTextColor(const Color &rColor)
Definition: text.cxx:662
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:418
Size GetOutputSizePixel() const
Definition: outdev.hxx:441
const SvLBoxItem & GetItem(size_t nPos) const
vcl::Window * GetParent() const
Definition: window2.cxx:1096
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:55
WinBits const WB_HIDESELECTION
static OUString extractPngString(const SvLBoxContextBmp *pBmpItem)
Definition: iconview.cxx:230
std::unique_ptr< char[]> aBuffer
Size GetOutputSizePixel() const
#define ERRCODE_NONE
Definition: errcode.hxx:196
void SetFont(const vcl::Font &rNewFont)
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
void setHeight(tools::Long nHeight)
WinBits GetStyle() const
Definition: window2.cxx:952
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:2973
friend class IconViewImpl
tools::Long Right() const
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:276
const Color & GetFillColor() const
Definition: outdev.hxx:639
const void * GetData()