LibreOffice Module svx (master)  1
SvxColorValueSet.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 
20 #include <svx/SvxColorValueSet.hxx>
21 #include <svx/xtable.hxx>
22 #include <vcl/builderfactory.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/settings.hxx>
25 #include <osl/diagnose.h>
26 
28 : ValueSet(_pParent, nWinStyle)
29 {
30  SetEdgeBlending(true);
31 }
32 
33 ColorValueSet::ColorValueSet(std::unique_ptr<weld::ScrolledWindow> pWindow)
34  : SvtValueSet(std::move(pWindow))
35 {
36  SetEdgeBlending(true);
37 }
38 
40 
41 sal_uInt32 SvxColorValueSet::getMaxRowCount()
42 {
44 }
45 
47 {
48  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
49 
50  return rStyleSettings.GetListBoxPreviewDefaultPixelSize().Height() + 1;
51 }
52 
54 {
55  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
56 
57  return rStyleSettings.GetColorValueSetColumnCount();
58 }
59 
60 void SvxColorValueSet::addEntriesForXColorList(const XColorList& rXColorList, sal_uInt32 nStartIndex)
61 {
62  const sal_uInt32 nColorCount(rXColorList.Count());
63 
64  for(sal_uInt32 nIndex(0); nIndex < nColorCount; nIndex++, nStartIndex++)
65  {
66  const XColorEntry* pEntry = rXColorList.GetColor(nIndex);
67 
68  if(pEntry)
69  {
70  InsertItem(nStartIndex, pEntry->GetColor(), pEntry->GetName());
71  }
72  else
73  {
74  OSL_ENSURE(false, "OOps, XColorList with empty entries (!)");
75  }
76  }
77 }
78 
79 void ColorValueSet::addEntriesForXColorList(const XColorList& rXColorList, sal_uInt32 nStartIndex)
80 {
81  const sal_uInt32 nColorCount(rXColorList.Count());
82 
83  for(sal_uInt32 nIndex(0); nIndex < nColorCount; nIndex++, nStartIndex++)
84  {
85  const XColorEntry* pEntry = rXColorList.GetColor(nIndex);
86 
87  if(pEntry)
88  {
89  InsertItem(nStartIndex, pEntry->GetColor(), pEntry->GetName());
90  }
91  else
92  {
93  OSL_ENSURE(false, "OOps, XColorList with empty entries (!)");
94  }
95  }
96 }
97 
98 void ColorValueSet::addEntriesForColorSet(const std::set<Color>& rColorSet, const OUString& rNamePrefix)
99 {
100  sal_uInt32 nStartIndex = 1;
101  if(rNamePrefix.getLength() != 0)
102  {
103  for(const auto& rColor : rColorSet)
104  {
105  InsertItem(nStartIndex, rColor, rNamePrefix + OUString::number(nStartIndex));
106  nStartIndex++;
107  }
108  }
109  else
110  {
111  for(const auto& rColor : rColorSet)
112  {
113  InsertItem(nStartIndex, rColor, "");
114  nStartIndex++;
115  }
116  }
117 }
118 
119 void SvxColorValueSet::addEntriesForColorSet(const std::set<Color>& rColorSet, const OUString& rNamePrefix)
120 {
121  sal_uInt32 nStartIndex = 1;
122  if(rNamePrefix.getLength() != 0)
123  {
124  for(const auto& rColor : rColorSet)
125  {
126  InsertItem(nStartIndex, rColor, rNamePrefix + OUString::number(nStartIndex));
127  nStartIndex++;
128  }
129  }
130  else
131  {
132  for(const auto& rColor : rColorSet)
133  {
134  InsertItem(nStartIndex, rColor, "");
135  nStartIndex++;
136  }
137  }
138 }
139 
140 Size ColorValueSet::layoutAllVisible(sal_uInt32 nEntryCount)
141 {
142  if(!nEntryCount)
143  {
144  nEntryCount++;
145  }
146 
147  const sal_uInt32 nRowCount(ceil(double(nEntryCount)/SvxColorValueSet::getColumnCount()));
149  const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
150 
151  if (nRowCount > SvxColorValueSet::getMaxRowCount())
152  {
153  SetStyle(aWinBits|WB_VSCROLL);
154  }
155  else
156  {
157  SetStyle(aWinBits);
158  }
159 
161  SetLineCount(std::min(nRowCount, SvxColorValueSet::getMaxRowCount()));
162  SetItemWidth(aItemSize.Width());
163  SetItemHeight(aItemSize.Height());
164 
165  return CalcWindowSizePixel(aItemSize);
166 }
167 
169 {
170  if(!nEntryCount)
171  {
172  nEntryCount++;
173  }
174 
175  const sal_uInt32 nRowCount(ceil(double(nEntryCount)/getColumnCount()));
176  const Size aItemSize(getEntryEdgeLength() - 2, getEntryEdgeLength() - 2);
177  const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
178 
179  if(nRowCount > getMaxRowCount())
180  {
181  SetStyle(aWinBits|WB_VSCROLL);
182  }
183  else
184  {
185  SetStyle(aWinBits);
186  }
187 
189  SetLineCount(std::min(nRowCount, getMaxRowCount()));
190  SetItemWidth(aItemSize.Width());
191  SetItemHeight(aItemSize.Height());
192 
193  return CalcWindowSizePixel(aItemSize);
194 }
195 
197 {
198  layoutToGivenHeight(GetSizePixel().Height(), GetItemCount());
200 }
201 
203 {
204  layoutToGivenHeight(GetOutputSizePixel().Height(), GetItemCount());
206 }
207 
208 Size SvxColorValueSet::layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount)
209 {
210  if(!nEntryCount)
211  {
212  nEntryCount++;
213  }
214 
215  const Size aItemSize(getEntryEdgeLength() - 2, getEntryEdgeLength() - 2);
216  const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
217 
218  // get size with all fields disabled
219  const WinBits aWinBitsNoScrollNoFields(GetStyle() & ~(WB_VSCROLL|WB_NAMEFIELD|WB_NONEFIELD));
220  SetStyle(aWinBitsNoScrollNoFields);
221  const Size aSizeNoScrollNoFields(CalcWindowSizePixel(aItemSize, getColumnCount()));
222 
223  // get size with all needed fields
224  SetStyle(aWinBits);
225  Size aNewSize(CalcWindowSizePixel(aItemSize, getColumnCount()));
226 
227  const Size aItemSizePixel(CalcItemSizePixel(aItemSize));
228  // calculate field height and available height for requested height
229  const sal_uInt32 nFieldHeight(aNewSize.Height() - aSizeNoScrollNoFields.Height());
230  const sal_uInt32 nAvailableHeight(nHeight >= nFieldHeight ? nHeight - nFieldHeight + aItemSizePixel.Height() - 1 : 0);
231 
232  // calculate how many lines can be shown there
233  const sal_uInt32 nLineCount(nAvailableHeight / aItemSizePixel.Height());
234  const sal_uInt32 nLineMax(ceil(double(nEntryCount)/getColumnCount()));
235 
236  if(nLineMax > nLineCount)
237  {
238  SetStyle(aWinBits|WB_VSCROLL);
239  }
240 
241  // set height to wanted height
242  aNewSize.setHeight( nHeight );
243 
244  SetItemWidth(aItemSize.Width());
245  SetItemHeight(aItemSize.Height());
248 
249  return aNewSize;
250 }
251 
252 Size ColorValueSet::layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount)
253 {
254  if(!nEntryCount)
255  {
256  nEntryCount++;
257  }
258 
260  const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
261 
262  // get size with all fields disabled
263  const WinBits aWinBitsNoScrollNoFields(GetStyle() & ~(WB_VSCROLL|WB_NAMEFIELD|WB_NONEFIELD));
264  SetStyle(aWinBitsNoScrollNoFields);
265  const Size aSizeNoScrollNoFields(CalcWindowSizePixel(aItemSize, SvxColorValueSet::getColumnCount()));
266 
267  // get size with all needed fields
268  SetStyle(aWinBits);
270 
271  const Size aItemSizePixel(CalcItemSizePixel(aItemSize));
272  // calculate field height and available height for requested height
273  const sal_uInt32 nFieldHeight(aNewSize.Height() - aSizeNoScrollNoFields.Height());
274  const sal_uInt32 nAvailableHeight(nHeight >= nFieldHeight ? nHeight - nFieldHeight + aItemSizePixel.Height() - 1 : 0);
275 
276  // calculate how many lines can be shown there
277  const sal_uInt32 nLineCount(nAvailableHeight / aItemSizePixel.Height());
278  const sal_uInt32 nLineMax(ceil(double(nEntryCount)/SvxColorValueSet::getColumnCount()));
279 
280  if(nLineMax > nLineCount)
281  {
282  SetStyle(aWinBits|WB_VSCROLL);
283  }
284 
285  // set height to wanted height
286  aNewSize.setHeight( nHeight );
287 
288  SetItemWidth(aItemSize.Width());
289  SetItemHeight(aItemSize.Height());
292 
293  return aNewSize;
294 }
295 
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
sal_Int32 nIndex
void SetItemWidth(long nItemWidth)
void SetItemWidth(long nItemWidth)
long Height() const
Size layoutAllVisible(sal_uInt32 nEntryCount)
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
virtual void Resize() override
WinBits const WB_VSCROLL
#define WB_NONEFIELD
const Size & GetListBoxPreviewDefaultPixelSize() const
static sal_uInt32 getEntryEdgeLength()
void SetLineCount(sal_uInt16 nNewLines=0)
void SetItemHeight(long nLineHeight)
virtual void Resize() override
WinBits GetStyle() const
static sal_uInt32 getColumnCount()
sal_uInt16 GetColorValueSetColumnCount() const
sal_Int64 WinBits
void SetEdgeBlending(bool bNew)
void addEntriesForColorSet(const std::set< Color > &rColorSet, const OUString &rNamePrefix)
void addEntriesForXColorList(const XColorList &rXColorList, sal_uInt32 nStartIndex=1)
void SetItemHeight(long nLineHeight)
const OUString & GetName() const
void SetStyle(WinBits nStyle)
XColorEntry * GetColor(long nIndex) const
Definition: xtabcolr.cxx:51
void addEntriesForColorSet(const std::set< Color > &rColorSet, const OUString &rNamePrefix)
void InsertItem(sal_uInt16 nItemId, const Image &rImage)
#define VCL_BUILDER_FACTORY_CONSTRUCTOR(typeName, arg2)
static sal_uInt16 GetColorValueSetMaximumRowCount()
Size layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount)
Size CalcItemSizePixel(const Size &rSize) const
ColorValueSet(std::unique_ptr< weld::ScrolledWindow > pWindow)
void SetEdgeBlending(bool bNew)
size_t GetItemCount() const
void SetLineCount(sal_uInt16 nNewLines=0)
Size layoutAllVisible(sal_uInt32 nEntryCount)
Size CalcWindowSizePixel(const Size &rItemSize, sal_uInt16 nCalcCols=0, sal_uInt16 nCalcLines=0) const
virtual void Resize() override
void SetColCount(sal_uInt16 nNewCols=1)
void addEntriesForXColorList(const XColorList &rXColorList, sal_uInt32 nStartIndex=1)
const Color & GetColor() const
Definition: xtable.hxx:50
long Count() const
Definition: xtable.cxx:124
Size CalcWindowSizePixel(const Size &rItemSize, sal_uInt16 nCalcCols=0, sal_uInt16 nCalcLines=0) const
void SetColCount(sal_uInt16 nNewCols=1)
void InsertItem(sal_uInt16 nItemId, const Image &rImage)
Size CalcItemSizePixel(const Size &rSize) const
static sal_uInt32 getMaxRowCount()
virtual void Resize() override
const int nLineCount
size_t GetItemCount() const
Size layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount)
SvxColorValueSet(vcl::Window *pParent, WinBits nWinStyle)
void setHeight(long nHeight)
#define WB_NAMEFIELD