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 else // no selection
155 {
156 aWallpaper.SetColor(rEntry.GetBackColor());
157 }
158
159 Color aBackgroundColor = aWallpaper.GetColor();
160 if (aBackgroundColor != COL_TRANSPARENT)
161 {
162 rRenderContext.SetFillColor(aBackgroundColor);
163 bFillColorSet = true;
164 // this case may occur for smaller horizontal resizes
165 if (nTempEntryWidth > 1)
166 rRenderContext.DrawRect({ aEntryPos, Size(nTempEntryWidth, nTempEntryHeight) });
167 }
168 }
169
170 const size_t nItemCount = rEntry.ItemCount();
171 size_t nIconItem = nItemCount;
172
173 int nLabelHeight = 0;
174 std::vector<size_t> aTextItems;
175
176 for (size_t nCurItem = 0; nCurItem < nItemCount; ++nCurItem)
177 {
178 SvLBoxItem& rItem = rEntry.GetItem(nCurItem);
179 SvLBoxItemType nItemType = rItem.GetType();
180
181 if (nItemType == SvLBoxItemType::ContextBmp)
182 {
183 nIconItem = nCurItem;
184 continue;
185 }
186
187 aTextItems.push_back(nCurItem);
188 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
189 nLabelHeight += nItemHeight;
190 }
191
192 int nLabelYPos = nY + nTempEntryHeight - nLabelHeight - nSpacing; // padding from bottom
193 for (auto nCurItem : aTextItems)
194 {
195 aEntryPos.setY(nLabelYPos);
196
197 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nCurItem);
198 nLabelYPos += nItemHeight;
199
200 rEntry.GetItem(nCurItem).Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
201 }
202
203 if (bFillColorSet)
204 rRenderContext.SetFillColor(aBackupColor);
205
206 // draw icon
207 if (nIconItem < nItemCount)
208 {
209 SvLBoxItem& rItem = rEntry.GetItem(nIconItem);
210 auto nItemWidth = rItem.GetWidth(this, pViewDataEntry, nIconItem);
211 auto nItemHeight = SvLBoxItem::GetHeight(pViewDataEntry, nIconItem);
212
213 aEntryPos.setY(nY);
214
215 // center horizontally
216 aEntryPos.AdjustX((nTempEntryWidth - nItemWidth) / 2);
217 // center vertically
218 int nImageAreaHeight = nTempEntryHeight - nSpacing * 2; // spacings from top, from bottom
219 if (nLabelHeight > 0)
220 {
221 nImageAreaHeight -= nLabelHeight + nSpacing; // spacing between icon and label
222 }
223 aEntryPos.AdjustY((nImageAreaHeight - nItemHeight) / 2 + nSpacing);
224
225 rItem.Paint(aEntryPos, *this, rRenderContext, pViewDataEntry, rEntry);
226 }
227
228 if (bCurFontIsSel)
229 {
230 rRenderContext.SetTextColor(aBackupTextColor);
231 rRenderContext.SetFont(aBackupFont);
232 }
233}
234
235css::uno::Reference<css::accessibility::XAccessible> IconView::CreateAccessible()
236{
237 if (vcl::Window* pParent = GetAccessibleParentWindow())
238 {
239 if (auto xAccParent = pParent->GetAccessible())
240 {
241 // need to be done here to get the vclxwindow later on in the accessible
242 css::uno::Reference<css::awt::XWindowPeer> xHoldAlive(GetComponentInterface());
243 return pImpl->m_aFactoryAccess.getFactory().createAccessibleIconView(*this, xAccParent);
244 }
245 }
246 return {};
247}
248
250{
251 assert(pEntry);
252
255
257}
258
260
261static OString extractPngString(const SvLBoxContextBmp* pBmpItem)
262{
263 BitmapEx aImage = pBmpItem->GetBitmap1().GetBitmapEx();
264 SvMemoryStream aOStm(65535, 65535);
266 {
267 css::uno::Sequence<sal_Int8> aSeq(static_cast<sal_Int8 const*>(aOStm.GetData()),
268 aOStm.Tell());
269 OStringBuffer aBuffer("data:image/png;base64,");
271 return aBuffer.makeStringAndClear();
272 }
273
274 return "";
275}
276
278 const SvTreeListBox* pTabListBox)
279{
280 while (pEntry)
281 {
282 auto aNode = rJsonWriter.startStruct();
283
284 // simple listbox value
285 const SvLBoxItem* pIt = pEntry->GetFirstItem(SvLBoxItemType::String);
286 if (pIt)
287 rJsonWriter.put("text", static_cast<const SvLBoxString*>(pIt)->GetText());
288
290 if (pIt)
291 {
292 const SvLBoxContextBmp* pBmpItem = static_cast<const SvLBoxContextBmp*>(pIt);
293 if (pBmpItem)
294 rJsonWriter.put("image", extractPngString(pBmpItem));
295 }
296
297 if (const OUString tooltip = pTabListBox->GetEntryTooltip(pEntry); !tooltip.isEmpty())
298 rJsonWriter.put("tooltip", tooltip);
299
300 if (pTabListBox->IsSelected(pEntry))
301 rJsonWriter.put("selected", true);
302
304 rJsonWriter.put("separator", true);
305
306 rJsonWriter.put("row", pTabListBox->GetModel()->GetAbsPos(pEntry));
307
308 pEntry = pEntry->NextSibling();
309 }
310}
311
313{
315 rJsonWriter.put("type", "iconview");
316 rJsonWriter.put("singleclickactivate", GetActivateOnSingleClick());
317 auto aNode = rJsonWriter.startArray("entries");
318 lcl_DumpEntryAndSiblings(rJsonWriter, First(), this);
319}
320
321/* 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:259
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:249
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:312
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:235
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 Color & GetBackColor() 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:984
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:277
static OString extractPngString(const SvLBoxContextBmp *pBmpItem)
Definition: iconview.cxx:261
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