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/svapp.hxx>
23 #include <vcl/settings.hxx>
24 #include <osl/diagnose.h>
25 
26 #include <svx/uiobject.hxx>
27 
28 SvxColorValueSet::SvxColorValueSet(std::unique_ptr<weld::ScrolledWindow> pWindow)
29  : ValueSet(std::move(pWindow))
30 {
31  SetEdgeBlending(true);
32 }
33 
35 {
37 }
38 
40 {
42 }
43 
45 {
46  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
47 
48  return rStyleSettings.GetListBoxPreviewDefaultPixelSize().Height() + 1;
49 }
50 
52 {
53  const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
54 
55  return rStyleSettings.GetColorValueSetColumnCount();
56 }
57 
58 void SvxColorValueSet::addEntriesForXColorList(const XColorList& rXColorList, sal_uInt32 nStartIndex)
59 {
60  const sal_uInt32 nColorCount(rXColorList.Count());
61 
62  for(sal_uInt32 nIndex(0); nIndex < nColorCount; nIndex++, nStartIndex++)
63  {
64  const XColorEntry* pEntry = rXColorList.GetColor(nIndex);
65 
66  if(pEntry)
67  {
68  InsertItem(nStartIndex, pEntry->GetColor(), pEntry->GetName());
69  }
70  else
71  {
72  OSL_ENSURE(false, "OOps, XColorList with empty entries (!)");
73  }
74  }
75 }
76 
77 void SvxColorValueSet::addEntriesForColorSet(const std::set<Color>& rColorSet, const OUString& rNamePrefix)
78 {
79  sal_uInt32 nStartIndex = 1;
80  if(rNamePrefix.getLength() != 0)
81  {
82  for(const auto& rColor : rColorSet)
83  {
84  InsertItem(nStartIndex, rColor, rNamePrefix + OUString::number(nStartIndex));
85  nStartIndex++;
86  }
87  }
88  else
89  {
90  for(const auto& rColor : rColorSet)
91  {
92  InsertItem(nStartIndex, rColor, "");
93  nStartIndex++;
94  }
95  }
96 }
97 
99 {
100  if(!nEntryCount)
101  {
102  nEntryCount++;
103  }
104 
105  const sal_uInt32 nRowCount(ceil(double(nEntryCount)/SvxColorValueSet::getColumnCount()));
107  const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
108 
109  if (nRowCount > SvxColorValueSet::getMaxRowCount())
110  {
111  SetStyle(aWinBits|WB_VSCROLL);
112  }
113  else
114  {
115  SetStyle(aWinBits);
116  }
117 
119  SetLineCount(std::min(nRowCount, SvxColorValueSet::getMaxRowCount()));
120  SetItemWidth(aItemSize.Width());
121  SetItemHeight(aItemSize.Height());
122 
123  return CalcWindowSizePixel(aItemSize);
124 }
125 
127 {
128  layoutToGivenHeight(GetOutputSizePixel().Height(), GetItemCount());
130 }
131 
132 Size SvxColorValueSet::layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount)
133 {
134  if(!nEntryCount)
135  {
136  nEntryCount++;
137  }
138 
140  const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
141 
142  // get size with all fields disabled
143  const WinBits aWinBitsNoScrollNoFields(GetStyle() & ~(WB_VSCROLL|WB_NAMEFIELD|WB_NONEFIELD));
144  SetStyle(aWinBitsNoScrollNoFields);
145  const Size aSizeNoScrollNoFields(CalcWindowSizePixel(aItemSize, SvxColorValueSet::getColumnCount()));
146 
147  // get size with all needed fields
148  SetStyle(aWinBits);
150 
151  const Size aItemSizePixel(CalcItemSizePixel(aItemSize));
152  // calculate field height and available height for requested height
153  const sal_uInt32 nFieldHeight(aNewSize.Height() - aSizeNoScrollNoFields.Height());
154  const sal_uInt32 nAvailableHeight(nHeight >= nFieldHeight ? nHeight - nFieldHeight + aItemSizePixel.Height() - 1 : 0);
155 
156  // calculate how many lines can be shown there
157  const sal_uInt32 nLineCount(nAvailableHeight / aItemSizePixel.Height());
158  const sal_uInt32 nLineMax(ceil(double(nEntryCount)/SvxColorValueSet::getColumnCount()));
159 
160  if(nLineMax > nLineCount)
161  {
162  SetStyle(aWinBits|WB_VSCROLL);
163  }
164 
165  // set height to wanted height
166  aNewSize.setHeight( nHeight );
167 
168  SetItemWidth(aItemSize.Width());
169  SetItemHeight(aItemSize.Height());
172 
173  return aNewSize;
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
const StyleSettings & GetStyleSettings() const
static const AllSettings & GetSettings()
virtual void Resize() override
std::function< std::unique_ptr< UIObject >vcl::Window *)> FactoryFunction
WinBits const WB_VSCROLL
#define WB_NONEFIELD
const Size & GetListBoxPreviewDefaultPixelSize() const
static sal_uInt32 getEntryEdgeLength()
void SetStyle(WinBits nStyle)
void SetLineCount(sal_uInt16 nNewLines=0)
static sal_uInt32 getColumnCount()
sal_uInt16 GetColorValueSetColumnCount() const
sal_Int64 WinBits
XColorEntry * GetColor(tools::Long nIndex) const
Definition: xtabcolr.cxx:50
void addEntriesForColorSet(const std::set< Color > &rColorSet, const OUString &rNamePrefix)
WinBits GetStyle() const
void SetItemHeight(tools::Long nLineHeight)
const OUString & GetName() const
void SetItemWidth(tools::Long nItemWidth)
tools::Long Width() const
void InsertItem(sal_uInt16 nItemId, const Image &rImage)
tools::Long Count() const
Definition: xtable.cxx:121
static sal_uInt16 GetColorValueSetMaximumRowCount()
Size CalcItemSizePixel(const Size &rSize) const
void SetEdgeBlending(bool bNew)
size_t GetItemCount() const
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)
SvxColorValueSet(std::unique_ptr< weld::ScrolledWindow > pWindow)
tools::Long Height() const
void addEntriesForXColorList(const XColorList &rXColorList, sal_uInt32 nStartIndex=1)
const Color & GetColor() const
Definition: xtable.hxx:50
void setHeight(tools::Long nHeight)
static sal_uInt32 getMaxRowCount()
virtual FactoryFunction GetUITestFactory() const override
static std::unique_ptr< UIObject > create(vcl::Window *pWindow)
Definition: sdrobject.cxx:183
const int nLineCount
Size layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount)
#define WB_NAMEFIELD