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"
26#include <tools/json_writer.hxx>
28#include <tools/stream.hxx>
29#include <vcl/cvtgrf.hxx>
30#include <comphelper/base64.hxx>
31
32namespace
33{
34const int separatorHeight = 10;
35const int nSpacing = 5; // 5 pixels from top, from bottom, between icon and label
36}
37
39 : SvTreeListBox(pParent, nBits)
40{
41 nColumns = 1;
43 SetEntryWidth(100);
44
45 pImpl.reset(new IconViewImpl(this, GetModel(), GetStyle()));
46}
47
49{
51 return { GetEntryWidth() * GetColumnsCount(), separatorHeight };
52 return { GetEntryWidth(), GetEntryHeight() };
53}
54
56{
57 int nHeight = nSpacing * 2;
58 SvViewDataEntry* pViewData = GetViewDataEntry(pEntry);
59 const size_t nCount = pEntry->ItemCount();
60 bool bHasIcon = false;
61 for (size_t nCur = 0; nCur < nCount; ++nCur)
62 {
63 nHeight += SvLBoxItem::GetHeight(pViewData, nCur);
64
65 if (!bHasIcon && pEntry->GetItem(nCur).GetType() == SvLBoxItemType::ContextBmp)
66 bHasIcon = true;
67 }
68
69 if (bHasIcon && nCount > 1)
70 nHeight += nSpacing; // between icon and label
71
72 if (nHeight > nEntryHeight)
73 {
74 nEntryHeight = nHeight;
76 pImpl->SetEntryHeight();
77 }
78}
79
81{
83
84 if (!aBoxSize.Width())
85 return;
86
87 nColumns = nEntryWidth ? aBoxSize.Width() / nEntryWidth : 1;
88
90}
91
93{
94 return { GetEntryPosition(pEntry), GetEntrySize(*pEntry) };
95}
96
98 vcl::RenderContext& rRenderContext)
99{
100 pImpl->UpdateContextBmpWidthMax(&rEntry);
101
102 const Size entrySize = GetEntrySize(rEntry);
103 short nTempEntryHeight = entrySize.Height();
104 short nTempEntryWidth = entrySize.Width();
105
106 Point aEntryPos(nX, nY);
107
108 const Color aBackupTextColor(rRenderContext.GetTextColor());
109 const vcl::Font aBackupFont(rRenderContext.GetFont());
110 const Color aBackupColor = rRenderContext.GetFillColor();
111
112 const StyleSettings& rSettings = rRenderContext.GetSettings().GetStyleSettings();
113
114 const Size aOutputSize = GetOutputSizePixel();
115 if (aOutputSize.getHeight() < nTempEntryHeight)
116 nTempEntryHeight = aOutputSize.getHeight();
117
118 const SvViewDataEntry* pViewDataEntry = GetViewDataEntry(&rEntry);
119
120 bool bCurFontIsSel = false;
121 if (pViewDataEntry->IsHighlighted())
122 {
123 vcl::Font aHighlightFont(rRenderContext.GetFont());
124 const Color aHighlightTextColor(rSettings.GetHighlightTextColor());
125 aHighlightFont.SetColor(aHighlightTextColor);
126
127 // set font color to highlight
128 rRenderContext.SetTextColor(aHighlightTextColor);
129 rRenderContext.SetFont(aHighlightFont);
130 bCurFontIsSel = true;
131 }
132
133 bool bFillColorSet = false;
134 // draw background
136 {
137 // set background pattern/color
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 const WinBits nWindowStyle = GetStyle();
146 const bool bHideSelection = (nWindowStyle & WB_HIDESELECTION) != 0 && !HasFocus();
147 if (bHideSelection && !rSettings.GetFaceColor().IsBright()
148 && aWallpaper.GetColor().IsBright() != rSettings.GetDeactiveColor().IsBright())
149 {
150 aNewWallColor = rSettings.GetDeactiveColor();
151 }
152 aWallpaper.SetColor(aNewWallColor);
153 }
154
155 Color aBackgroundColor = aWallpaper.GetColor();
156 if (aBackgroundColor != COL_TRANSPARENT)
157 {
158 rRenderContext.SetFillColor(aBackgroundColor);
159 bFillColorSet = true;
160 // this case may occur for smaller horizontal resizes
161 if (nTempEntryWidth > 1)
162 rRenderContext.DrawRect({ aEntryPos, Size(nTempEntryWidth, nTempEntryHeight) });
163 }
164 }
165
166 const size_t nItemCount = rEntry.ItemCount();
167 size_t nIconItem = nItemCount;
168
169 int nLabelHeight = 0;
170 std::vector<size_t> aTextItems;
171
172 for (size_t nCurItem = 0; nCurItem < nItemCount; ++nCurItem)
173 {
174 SvLBoxItem& rItem = rEntry.GetItem(nCurItem);
175 SvLBoxItemType nItemType = rItem.GetType();
176
177 if (nItemType == SvLBoxItemType::ContextBmp)
178 {
179 nIconItem = nCurItem;
180 continue;
181 }
182
183 aTextItems.push_back(nCurItem);
184 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
185 nLabelHeight += nItemHeight;
186 }
187
188 int nLabelYPos = nY + nTempEntryHeight - nLabelHeight - nSpacing; // padding from bottom
189 for (auto nCurItem : aTextItems)
190 {
191 aEntryPos.setY(nLabelYPos);
192
193 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
194 nLabelYPos += nItemHeight;
195
196 rEntry.GetItem(nCurItem).Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
197 }
198
199 if (bFillColorSet)
200 rRenderContext.SetFillColor(aBackupColor);
201
202 // draw icon
203 if (nIconItem < nItemCount)
204 {
205 SvLBoxItem& rItem = rEntry.GetItem(nIconItem);
206 auto nItemWidth = rItem.GetWidth(this, pViewDataEntry, nIconItem);
207 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nIconItem);
208
209 aEntryPos.setY(nY);
210
211 // center horizontally
212 aEntryPos.AdjustX((nTempEntryWidth - nItemWidth) / 2);
213 // center vertically
214 int nImageAreaHeight = nTempEntryHeight - nSpacing * 2; // spacings from top, from bottom
215 if (nLabelHeight > 0)
216 {
217 nImageAreaHeight -= nLabelHeight + nSpacing; // spacing between icon and label
218 }
219 aEntryPos.AdjustY((nImageAreaHeight - nItemHeight) / 2 + nSpacing);
220
221 rItem.Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
222 }
223
224 if (bCurFontIsSel)
225 {
226 rRenderContext.SetTextColor(aBackupTextColor);
227 rRenderContext.SetFont(aBackupFont);
228 }
229}
230
231css::uno::Reference<css::accessibility::XAccessible> IconView::CreateAccessible()
232{
233 if (vcl::Window* pParent = GetAccessibleParentWindow())
234 {
235 if (auto xAccParent = pParent->GetAccessible())
236 {
237 // need to be done here to get the vclxwindow later on in the accessible
238 css::uno::Reference<css::awt::XWindowPeer> xHoldAlive(GetComponentInterface());
239 return pImpl->m_aFactoryAccess.getFactory().createAccessibleIconView(*this, xAccParent);
240 }
241 }
242 return {};
243}
244
246{
247 assert(pEntry);
248
251
253}
254
256
257static OString extractPngString(const SvLBoxContextBmp* pBmpItem)
258{
259 BitmapEx aImage = pBmpItem->GetBitmap1().GetBitmapEx();
260 SvMemoryStream aOStm(65535, 65535);
262 {
263 css::uno::Sequence<sal_Int8> aSeq(static_cast<sal_Int8 const*>(aOStm.GetData()),
264 aOStm.Tell());
265 OStringBuffer aBuffer("data:image/png;base64,");
267 return aBuffer.makeStringAndClear();
268 }
269
270 return "";
271}
272
274 const SvTreeListBox* pTabListBox)
275{
276 while (pEntry)
277 {
278 auto aNode = rJsonWriter.startStruct();
279
280 // simple listbox value
281 const SvLBoxItem* pIt = pEntry->GetFirstItem(SvLBoxItemType::String);
282 if (pIt)
283 rJsonWriter.put("text", static_cast<const SvLBoxString*>(pIt)->GetText());
284
286 if (pIt)
287 {
288 const SvLBoxContextBmp* pBmpItem = static_cast<const SvLBoxContextBmp*>(pIt);
289 if (pBmpItem)
290 rJsonWriter.put("image", extractPngString(pBmpItem));
291 }
292
293 if (const OUString tooltip = pTabListBox->GetEntryTooltip(pEntry); !tooltip.isEmpty())
294 rJsonWriter.put("tooltip", tooltip);
295
296 if (pTabListBox->IsSelected(pEntry))
297 rJsonWriter.put("selected", true);
298
300 rJsonWriter.put("separator", true);
301
302 rJsonWriter.put("row", pTabListBox->GetModel()->GetAbsPos(pEntry));
303
304 pEntry = pEntry->NextSibling();
305 }
306}
307
309{
311 rJsonWriter.put("type", "iconview");
312 rJsonWriter.put("singleclickactivate", GetActivateOnSingleClick());
313 auto aNode = rJsonWriter.startArray("entries");
314 lcl_DumpEntryAndSiblings(rJsonWriter, First(), this);
315}
316
317/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const StyleSettings & GetStyleSettings() const
bool IsBright() const
static ErrCode Export(SvStream &rOStm, const Graphic &rGraphic, ConvertDataFormat nFormat)
Definition: cvtgrf.cxx:51
static std::unique_ptr< UIObject > create(vcl::Window *pWindow)
virtual FactoryFunction GetUITestFactory() const override
Definition: iconview.cxx:255
IconView(vcl::Window *pParent, WinBits nBits)
Definition: iconview.cxx:38
void PaintEntry(SvTreeListEntry &, tools::Long nX, tools::Long nY, vcl::RenderContext &rRenderContext)
Definition: iconview.cxx:97
virtual OUString GetEntryAccessibleDescription(SvTreeListEntry *pEntry) const override
Definition: iconview.cxx:245
virtual void Resize() override
Definition: iconview.cxx:80
Size GetEntrySize(const SvTreeListEntry &) const
Definition: iconview.cxx:48
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:308
Link< SvTreeListEntry *, OUString > maEntryAccessibleDescriptionHdl
Definition: iconview.hxx:54
virtual tools::Rectangle GetFocusRect(const SvTreeListEntry *, tools::Long) override
Definition: iconview.cxx:92
virtual void CalcEntryHeight(SvTreeListEntry const *pEntry) override
Definition: iconview.cxx:55
virtual css::uno::Reference< css::accessibility::XAccessible > CreateAccessible() override
Creates and returns the accessible object of the Box.
Definition: iconview.cxx:231
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
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:1325
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(const char *pPropName, const OUString &rPropValue)
ScopedJsonWriterArray startArray(const char *)
ScopedJsonWriterStruct startStruct()
void SetColor(const Color &)
Definition: font/font.cxx:107
void SetFont(const vcl::Font &rNewFont)
Definition: window3.cxx:59
bool HasFocus() const
Definition: window.cxx:2988
vcl::Window * GetAccessibleParentWindow() const
WinBits GetStyle() const
Definition: window2.cxx:992
const vcl::Font & GetFont() const
Definition: window3.cxx:58
virtual css::uno::Reference< css::awt::XWindowPeer > GetComponentInterface(bool bCreate=true)
Definition: window.cxx:3152
virtual void DumpAsPropertyTree(tools::JsonWriter &)
Dumps itself and potentially its children to a property tree, to be written easily to JSON.
Definition: window.cxx:3365
Size GetOutputSizePixel() const
Definition: window3.cxx:89
constexpr ::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
int nCount
#define ERRCODE_NONE
static void lcl_DumpEntryAndSiblings(tools::JsonWriter &rJsonWriter, SvTreeListEntry *pEntry, const SvTreeListBox *pTabListBox)
Definition: iconview.cxx:273
static OString extractPngString(const SvLBoxContextBmp *pBmpItem)
Definition: iconview.cxx:257
std::function< std::unique_ptr< UIObject >(vcl::Window *)> FactoryFunction
Sequence< sal_Int8 > aSeq
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