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
23#include <iconview.hxx>
24#include "iconviewimpl.hxx"
27#include <tools/json_writer.hxx>
29#include <tools/stream.hxx>
30#include <vcl/cvtgrf.hxx>
31#include <comphelper/base64.hxx>
33
34namespace
35{
36const int separatorHeight = 10;
37const int nSpacing = 5; // 5 pixels from top, from bottom, between icon and label
38}
39
41 : SvTreeListBox(pParent, nBits)
42{
43 nColumns = 1;
45 SetEntryWidth(100);
46
47 pImpl.reset(new IconViewImpl(this, GetModel(), GetStyle()));
48}
49
51{
53 return { GetEntryWidth() * GetColumnsCount(), separatorHeight };
54 return { GetEntryWidth(), GetEntryHeight() };
55}
56
58{
59 int nHeight = nSpacing * 2;
60 SvViewDataEntry* pViewData = GetViewDataEntry(pEntry);
61 const size_t nCount = pEntry->ItemCount();
62 bool bHasIcon = false;
63 for (size_t nCur = 0; nCur < nCount; ++nCur)
64 {
65 nHeight += SvLBoxItem::GetHeight(pViewData, nCur);
66
67 if (!bHasIcon && pEntry->GetItem(nCur).GetType() == SvLBoxItemType::ContextBmp)
68 bHasIcon = true;
69 }
70
71 if (bHasIcon && nCount > 1)
72 nHeight += nSpacing; // between icon and label
73
74 if (nHeight > nEntryHeight)
75 {
76 nEntryHeight = nHeight;
78 pImpl->SetEntryHeight();
79 }
80}
81
83{
85
86 if (!aBoxSize.Width())
87 return;
88
89 nColumns = nEntryWidth ? aBoxSize.Width() / nEntryWidth : 1;
90
92}
93
95{
96 return { GetEntryPosition(pEntry), GetEntrySize(*pEntry) };
97}
98
100 vcl::RenderContext& rRenderContext)
101{
102 pImpl->UpdateContextBmpWidthMax(&rEntry);
103
104 const Size entrySize = GetEntrySize(rEntry);
105 short nTempEntryHeight = entrySize.Height();
106 short nTempEntryWidth = entrySize.Width();
107
108 Point aEntryPos(nX, nY);
109
110 const Color aBackupTextColor(rRenderContext.GetTextColor());
111 const vcl::Font aBackupFont(rRenderContext.GetFont());
112 const Color aBackupColor = rRenderContext.GetFillColor();
113
114 const StyleSettings& rSettings = rRenderContext.GetSettings().GetStyleSettings();
115
116 const Size aOutputSize = GetOutputSizePixel();
117 if (aOutputSize.getHeight() < nTempEntryHeight)
118 nTempEntryHeight = aOutputSize.getHeight();
119
120 const SvViewDataEntry* pViewDataEntry = GetViewDataEntry(&rEntry);
121
122 bool bCurFontIsSel = false;
123 if (pViewDataEntry->IsHighlighted())
124 {
125 vcl::Font aHighlightFont(rRenderContext.GetFont());
126 const Color aHighlightTextColor(rSettings.GetHighlightTextColor());
127 aHighlightFont.SetColor(aHighlightTextColor);
128
129 // set font color to highlight
130 rRenderContext.SetTextColor(aHighlightTextColor);
131 rRenderContext.SetFont(aHighlightFont);
132 bCurFontIsSel = true;
133 }
134
135 bool bFillColorSet = false;
136 // draw background
138 {
139 // set background pattern/color
140 Wallpaper aWallpaper = rRenderContext.GetBackground();
141
142 if (pViewDataEntry->IsHighlighted())
143 {
144 Color aNewWallColor = rSettings.GetHighlightColor();
145 // if the face color is bright then the deactivate color is also bright
146 // -> so you can't see any deactivate selection
147 const WinBits nWindowStyle = GetStyle();
148 const bool bHideSelection = (nWindowStyle & WB_HIDESELECTION) != 0 && !HasFocus();
149 if (bHideSelection && !rSettings.GetFaceColor().IsBright()
150 && aWallpaper.GetColor().IsBright() != rSettings.GetDeactiveColor().IsBright())
151 {
152 aNewWallColor = rSettings.GetDeactiveColor();
153 }
154 aWallpaper.SetColor(aNewWallColor);
155 }
156
157 Color aBackgroundColor = aWallpaper.GetColor();
158 if (aBackgroundColor != COL_TRANSPARENT)
159 {
160 rRenderContext.SetFillColor(aBackgroundColor);
161 bFillColorSet = true;
162 // this case may occur for smaller horizontal resizes
163 if (nTempEntryWidth > 1)
164 rRenderContext.DrawRect({ aEntryPos, Size(nTempEntryWidth, nTempEntryHeight) });
165 }
166 }
167
168 const size_t nItemCount = rEntry.ItemCount();
169 size_t nIconItem = nItemCount;
170
171 int nLabelHeight = 0;
172 std::vector<size_t> aTextItems;
173
174 for (size_t nCurItem = 0; nCurItem < nItemCount; ++nCurItem)
175 {
176 SvLBoxItem& rItem = rEntry.GetItem(nCurItem);
177 SvLBoxItemType nItemType = rItem.GetType();
178
179 if (nItemType == SvLBoxItemType::ContextBmp)
180 {
181 nIconItem = nCurItem;
182 continue;
183 }
184
185 aTextItems.push_back(nCurItem);
186 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
187 nLabelHeight += nItemHeight;
188 }
189
190 int nLabelYPos = nY + nTempEntryHeight - nLabelHeight - nSpacing; // padding from bottom
191 for (auto nCurItem : aTextItems)
192 {
193 aEntryPos.setY(nLabelYPos);
194
195 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
196 nLabelYPos += nItemHeight;
197
198 rEntry.GetItem(nCurItem).Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
199 }
200
201 if (bFillColorSet)
202 rRenderContext.SetFillColor(aBackupColor);
203
204 // draw icon
205 if (nIconItem < nItemCount)
206 {
207 SvLBoxItem& rItem = rEntry.GetItem(nIconItem);
208 auto nItemWidth = rItem.GetWidth(this, pViewDataEntry, nIconItem);
209 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nIconItem);
210
211 aEntryPos.setY(nY);
212
213 // center horizontally
214 aEntryPos.AdjustX((nTempEntryWidth - nItemWidth) / 2);
215 // center vertically
216 int nImageAreaHeight = nTempEntryHeight - nSpacing * 2; // spacings from top, from bottom
217 if (nLabelHeight > 0)
218 {
219 nImageAreaHeight -= nLabelHeight + nSpacing; // spacing between icon and label
220 }
221 aEntryPos.AdjustY((nImageAreaHeight - nItemHeight) / 2 + nSpacing);
222
223 rItem.Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
224 }
225
226 if (bCurFontIsSel)
227 {
228 rRenderContext.SetTextColor(aBackupTextColor);
229 rRenderContext.SetFont(aBackupFont);
230 }
231}
232
233css::uno::Reference<css::accessibility::XAccessible> IconView::CreateAccessible()
234{
235 if (vcl::Window* pParent = GetAccessibleParentWindow())
236 {
237 if (auto xAccParent = pParent->GetAccessible())
238 {
239 // need to be done here to get the vclxwindow later on in the accessible
240 css::uno::Reference<css::awt::XVclWindowPeer> xHoldAlive(GetComponentInterface());
241 return pImpl->m_aFactoryAccess.getFactory().createAccessibleIconView(*this, xAccParent);
242 }
243 }
244 return {};
245}
246
248{
249 assert(pEntry);
250
253
255}
256
258
259static OString extractPngString(const SvLBoxContextBmp* pBmpItem)
260{
261 BitmapEx aImage = pBmpItem->GetBitmap1().GetBitmapEx();
262 SvMemoryStream aOStm(65535, 65535);
263 // Use fastest compression "1"
264 css::uno::Sequence<css::beans::PropertyValue> aFilterData{
265 comphelper::makePropertyValue("Compression", sal_Int32(1)),
266 };
267 vcl::PngImageWriter aPNGWriter(aOStm);
268 aPNGWriter.setParameters(aFilterData);
269 if (aPNGWriter.write(aImage))
270 {
271 css::uno::Sequence<sal_Int8> aSeq(static_cast<sal_Int8 const*>(aOStm.GetData()),
272 aOStm.Tell());
273 OStringBuffer aBuffer("data:image/png;base64,");
275 return aBuffer.makeStringAndClear();
276 }
277
278 return "";
279}
280
282{
283 while (pEntry)
284 {
285 auto aNode = rJsonWriter.startStruct();
286
287 // simple listbox value
288 const SvLBoxItem* pIt = pEntry->GetFirstItem(SvLBoxItemType::String);
289 if (pIt)
290 rJsonWriter.put("text", static_cast<const SvLBoxString*>(pIt)->GetText());
291
292 const bool bHandled
294 && maDumpElemToPropertyTreeHdl.Call(json_prop_query(rJsonWriter, pEntry, "image"));
295 if (!bHandled)
296 {
298 if (pIt)
299 {
300 const SvLBoxContextBmp* pBmpItem = static_cast<const SvLBoxContextBmp*>(pIt);
301 if (pBmpItem)
302 rJsonWriter.put("image", extractPngString(pBmpItem));
303 }
304 }
305
306 if (const OUString tooltip = GetEntryTooltip(pEntry); !tooltip.isEmpty())
307 rJsonWriter.put("tooltip", tooltip);
308
309 if (IsSelected(pEntry))
310 rJsonWriter.put("selected", true);
311
313 rJsonWriter.put("separator", true);
314
315 rJsonWriter.put("row", GetModel()->GetAbsPos(pEntry));
316
317 pEntry = pEntry->NextSibling();
318 }
319}
320
322{
324 rJsonWriter.put("type", "iconview");
325 rJsonWriter.put("singleclickactivate", GetActivateOnSingleClick());
326 auto aNode = rJsonWriter.startArray("entries");
327 DumpEntryAndSiblings(rJsonWriter, First());
328}
329
330/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const StyleSettings & GetStyleSettings() const
bool IsBright() const
static std::unique_ptr< UIObject > create(vcl::Window *pWindow)
std::tuple< tools::JsonWriter &, SvTreeListEntry *, std::string_view > json_prop_query
Definition: iconview.hxx:51
virtual FactoryFunction GetUITestFactory() const override
Definition: iconview.cxx:257
void DumpEntryAndSiblings(tools::JsonWriter &rJsonWriter, SvTreeListEntry *pEntry)
Definition: iconview.cxx:281
IconView(vcl::Window *pParent, WinBits nBits)
Definition: iconview.cxx:40
Link< const json_prop_query &, bool > maDumpElemToPropertyTreeHdl
Definition: iconview.hxx:63
void PaintEntry(SvTreeListEntry &, tools::Long nX, tools::Long nY, vcl::RenderContext &rRenderContext)
Definition: iconview.cxx:99
virtual OUString GetEntryAccessibleDescription(SvTreeListEntry *pEntry) const override
Definition: iconview.cxx:247
virtual void Resize() override
Definition: iconview.cxx:82
Size GetEntrySize(const SvTreeListEntry &) const
Definition: iconview.cxx:50
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:321
Link< SvTreeListEntry *, OUString > maEntryAccessibleDescriptionHdl
Definition: iconview.hxx:62
virtual tools::Rectangle GetFocusRect(const SvTreeListEntry *, tools::Long) override
Definition: iconview.cxx:94
virtual void CalcEntryHeight(SvTreeListEntry const *pEntry) override
Definition: iconview.cxx:57
virtual css::uno::Reference< css::accessibility::XAccessible > CreateAccessible() override
Creates and returns the accessible object of the Box.
Definition: iconview.cxx:233
BitmapEx GetBitmapEx() const
Definition: Image.cxx:96
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:170
const vcl::Font & GetFont() const
Definition: outdev.hxx:529
void SetFont(const vcl::Font &rNewFont)
Definition: outdev/font.cxx:56
void DrawRect(const tools::Rectangle &rRect)
Definition: rect.cxx:50
const Wallpaper & GetBackground() const
Definition: outdev.hxx:523
void SetTextColor(const Color &rColor)
Definition: text.cxx:716
void SetFillColor()
Definition: fill.cxx:29
const Color & GetTextColor() const
Definition: outdev.hxx:1003
const AllSettings & GetSettings() const
Definition: outdev.hxx:288
const Color & GetFillColor() const
Definition: outdev.hxx:515
void setY(tools::Long nY)
tools::Long AdjustY(tools::Long nVertMove)
tools::Long AdjustX(tools::Long nHorzMove)
constexpr tools::Long getHeight() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
const Color & GetDeactiveColor() const
const Color & GetHighlightColor() const
const Color & GetFaceColor() const
const Color & GetHighlightTextColor() const
const Image & GetBitmap1() const
Definition: svlbitm.hxx:275
int GetHeight(const SvTreeListBox *pView, const SvTreeListEntry *pEntry) const
virtual SvLBoxItemType GetType() const =0
virtual void Paint(const Point &rPos, SvTreeListBox &rOutDev, vcl::RenderContext &rRenderContext, const SvViewDataEntry *pView, const SvTreeListEntry &rEntry)=0
int GetWidth(const SvTreeListBox *pView, const SvTreeListEntry *pEntry) const
bool IsSelected(const SvTreeListEntry *pEntry) const
Definition: treelist.cxx:1310
const void * GetData()
sal_uInt64 Tell() const
void SetEntryWidth(short nWidth)
friend class IconViewImpl
virtual void Resize() override
bool GetActivateOnSingleClick() const
OUString GetEntryTooltip(SvTreeListEntry *pEntry) const
Point GetEntryPosition(const SvTreeListEntry *) const
Size GetOutputSizePixel() const
short GetEntryHeight() const
virtual OUString GetEntryAccessibleDescription(SvTreeListEntry *pEntry) const
SvViewDataEntry * GetViewDataEntry(SvTreeListEntry const *pEntry) const
SvTreeListEntry * First() const
bool mbCenterAndClipText
short GetColumnsCount() const
SvTreeFlags nTreeFlags
std::unique_ptr< SvImpLBox > pImpl
short GetEntryWidth() const
SvTreeList * GetModel() const
SvTreeListEntry * NextSibling() const
const SvLBoxItem * GetFirstItem(SvLBoxItemType eType) const
size_t ItemCount() const
SvTLEntryFlags GetFlags() const
const SvLBoxItem & GetItem(size_t nPos) const
sal_uInt32 GetAbsPos(const SvTreeListEntry *pEntry) const
Definition: treelist.cxx:821
View-dependent data for a tree list entry created in the virtual function SvTreeListBox::CreateViewDa...
bool IsHighlighted() const
const Color & GetColor() const
Definition: wall.hxx:71
void SetColor(const Color &rColor)
Definition: wall.cxx:156
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
void put(std::u16string_view pPropName, const OUString &rPropValue)
ScopedJsonWriterStruct startStruct()
ScopedJsonWriterArray startArray(std::string_view)
void SetColor(const Color &)
Definition: font/font.cxx:107
void setParameters(css::uno::Sequence< css::beans::PropertyValue > const &rParameters)
bool write(const BitmapEx &rBitmap)
void SetFont(const vcl::Font &rNewFont)
Definition: window3.cxx:59
bool HasFocus() const
Definition: window.cxx:2981
vcl::Window * GetAccessibleParentWindow() const
WinBits GetStyle() const
Definition: window2.cxx:979
const vcl::Font & GetFont() const
Definition: window3.cxx:58
virtual css::uno::Reference< css::awt::XVclWindowPeer > GetComponentInterface(bool bCreate=true)
Definition: window.cxx:3145
virtual void DumpAsPropertyTree(tools::JsonWriter &)
Dumps itself and potentially its children to a property tree, to be written easily to JSON.
Definition: window.cxx:3356
Size GetOutputSizePixel() const
Definition: window3.cxx:89
virtual OUString GetText() const
Definition: window.cxx:3055
constexpr ::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
int nCount
static OString extractPngString(const SvLBoxContextBmp *pBmpItem)
Definition: iconview.cxx:259
std::function< std::unique_ptr< UIObject >(vcl::Window *)> FactoryFunction
Sequence< sal_Int8 > aSeq
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
long Long
SvLBoxItemType
Definition: treelistbox.hxx:96
signed char sal_Int8
sal_Int64 WinBits
Definition: wintypes.hxx:109
WinBits const WB_HIDESELECTION
Definition: wintypes.hxx:228
std::unique_ptr< char[]> aBuffer