1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svx.hxx"
24 
25 #include <svx/SvxColorValueSet.hxx>
26 #include <svx/xtable.hxx>
27 #include <svtools/accessibilityoptions.hxx>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 SvxColorValueSet::SvxColorValueSet(Window* _pParent, WinBits nWinStyle)
32 :   ValueSet(_pParent, nWinStyle)
33 {
34 }
35 
36 SvxColorValueSet::SvxColorValueSet(Window* _pParent, const ResId& rResId)
37 :   ValueSet(_pParent, rResId)
38 {
39 }
40 
41 sal_uInt32 SvxColorValueSet::getMaxRowCount() const
42 {
43     const SvtAccessibilityOptions aOptions;
44 
45     return aOptions.GetColorValueSetMaximumRowCount();
46 }
47 
48 sal_uInt32 SvxColorValueSet::getEntryEdgeLength() const
49 {
50     const SvtAccessibilityOptions aOptions;
51 
52     return aOptions.GetColorValueSetEntryEdgeLength();
53 }
54 
55 sal_uInt32 SvxColorValueSet::getColumnCount() const
56 {
57     const SvtAccessibilityOptions aOptions;
58 
59     return aOptions.GetColorValueSetColumnCount();
60 }
61 
62 void SvxColorValueSet::addEntriesForXColorList(const XColorList& rXColorList, sal_uInt32 nStartIndex)
63 {
64     const sal_uInt32 nColorCount(rXColorList.Count());
65 
66     for(sal_uInt32 nIndex(0); nIndex < nColorCount; nIndex++, nStartIndex++)
67     {
68         const XColorEntry* pEntry = rXColorList.GetColor(nIndex);
69 
70         if(pEntry)
71         {
72             InsertItem(nStartIndex, pEntry->GetColor(), pEntry->GetName());
73         }
74         else
75         {
76             OSL_ENSURE(false, "OOps, XColorList with empty entries (!)");
77         }
78     }
79 }
80 
81 Size SvxColorValueSet::layoutAllVisible(sal_uInt32 nEntryCount)
82 {
83     if(!nEntryCount)
84     {
85         nEntryCount++;
86     }
87 
88     const sal_uInt32 nRowCount(ceil(double(nEntryCount)/getColumnCount()));
89     const Size aItemSize(getEntryEdgeLength() - 2, getEntryEdgeLength() - 2);
90     const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
91 
92     if(nRowCount > getMaxRowCount())
93     {
94         SetStyle(aWinBits|WB_VSCROLL);
95     }
96     else
97     {
98         SetStyle(aWinBits);
99     }
100 
101     SetColCount(getColumnCount());
102     SetLineCount(std::min(nRowCount, getMaxRowCount()));
103     SetItemWidth(aItemSize.Width());
104     SetItemHeight(aItemSize.Height());
105 
106     return CalcWindowSizePixel(aItemSize);
107 }
108 
109 Size SvxColorValueSet::layoutToGivenHeight(sal_uInt32 nHeight, sal_uInt32 nEntryCount)
110 {
111     if(!nEntryCount)
112     {
113         nEntryCount++;
114     }
115 
116     const Size aItemSize(getEntryEdgeLength(), getEntryEdgeLength());
117     const WinBits aWinBits(GetStyle() & ~WB_VSCROLL);
118 
119     // get size whith all fields disabled
120     const WinBits aWinBitsNoScrollNoFields(GetStyle() & ~(WB_VSCROLL|WB_NAMEFIELD|WB_NONEFIELD));
121     SetStyle(aWinBitsNoScrollNoFields);
122     const Size aSizeNoScrollNoFields(CalcWindowSizePixel(aItemSize, getColumnCount()));
123 
124     // get size with all needed fields
125     SetStyle(aWinBits);
126     Size aNewSize(CalcWindowSizePixel(aItemSize, getColumnCount()));
127 
128     // evtl. activate vertical scroll
129     const bool bAdaptHeight(static_cast< sal_uInt32 >(aNewSize.Height()) > nHeight);
130 
131     if(bAdaptHeight)
132     {
133         SetStyle(aWinBits|WB_VSCROLL);
134         aNewSize = CalcWindowSizePixel(aItemSize, getColumnCount());
135     }
136 
137     // calculate field height and available height for requested height
138     const sal_uInt32 nFieldHeight(aNewSize.Height() - aSizeNoScrollNoFields.Height());
139     const sal_uInt32 nAvailableHeight(nHeight >= nFieldHeight ? nHeight - nFieldHeight : 0);
140 
141     // calculate how many lines can be shown there
142     const Size aItemSizePixel(CalcItemSizePixel(aItemSize));
143     const sal_uInt32 nLineCount((nAvailableHeight + aItemSizePixel.Height() - 1) / aItemSizePixel.Height());
144 
145     // set height to wanted height
146     aNewSize.Height() = nHeight;
147 
148     SetItemWidth(aItemSize.Width());
149     SetItemHeight(aItemSize.Height());
150     SetColCount(getColumnCount());
151     SetLineCount(nLineCount);
152 
153     return aNewSize;
154 }
155 
156 //////////////////////////////////////////////////////////////////////////////
157 // eof
158