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 #include "precompiled_sd.hxx"
23 
24 #include "PreviewValueSet.hxx"
25 #include <vcl/image.hxx>
26 
27 
28 namespace sd { namespace sidebar {
29 
30 
31 PreviewValueSet::PreviewValueSet (::Window* pParent)
32     : ValueSet (pParent, WB_TABSTOP),
33       maPreviewSize(10,10),
34       mnBorderWidth(3),
35       mnBorderHeight(3),
36       mnMaxColumnCount(-1)
37 {
38 	SetStyle (
39         GetStyle()
40         & ~(WB_ITEMBORDER)// | WB_MENUSTYLEVALUESET)
41         //        | WB_FLATVALUESET);
42         );
43 	SetColCount(2);
44 	SetExtraSpacing (2);
45 }
46 
47 
48 
49 
50 PreviewValueSet::~PreviewValueSet (void)
51 {
52 }
53 
54 
55 
56 
57 void PreviewValueSet::SetPreviewSize (const Size& rSize)
58 {
59     maPreviewSize = rSize;
60 }
61 
62 
63 
64 
65 void PreviewValueSet::SetRightMouseClickHandler (const Link& rLink)
66 {
67     maRightMouseClickHandler = rLink;
68 }
69 
70 
71 
72 
73 void PreviewValueSet::MouseButtonDown (const MouseEvent& rEvent)
74 {
75     if (rEvent.IsRight())
76         maRightMouseClickHandler.Call(reinterpret_cast<void*>(
77             &const_cast<MouseEvent&>(rEvent)));
78     else
79         ValueSet::MouseButtonDown (rEvent);
80 
81 }
82 
83 
84 
85 
86 void PreviewValueSet::Resize (void)
87 {
88     ValueSet::Resize ();
89 
90     Size aWindowSize (GetOutputSizePixel());
91     if (aWindowSize.Width()>0 && aWindowSize.Height()>0)
92     {
93         Rearrange();
94     }
95 }
96 
97 
98 
99 
100 void PreviewValueSet::Rearrange (bool bForceRequestResize)
101 {
102     sal_uInt16 nOldColumnCount (GetColCount());
103     sal_uInt16 nOldRowCount (GetLineCount());
104 
105     sal_uInt16 nNewColumnCount (CalculateColumnCount (
106         GetOutputSizePixel().Width()));
107     sal_uInt16 nNewRowCount (CalculateRowCount (nNewColumnCount));
108 
109     SetColCount(nNewColumnCount);
110     SetLineCount(nNewRowCount);
111 }
112 
113 
114 
115 
116 sal_uInt16 PreviewValueSet::CalculateColumnCount (int nWidth) const
117 {
118     int nColumnCount = 0;
119     if (nWidth > 0)
120     {
121         nColumnCount = nWidth / (maPreviewSize.Width() + 2*mnBorderWidth);
122         if (nColumnCount < 1)
123             nColumnCount = 1;
124         else if (mnMaxColumnCount>0 && nColumnCount>mnMaxColumnCount)
125             nColumnCount = mnMaxColumnCount;
126     }
127     return (sal_uInt16)nColumnCount;
128 }
129 
130 
131 
132 
133 sal_uInt16 PreviewValueSet::CalculateRowCount (sal_uInt16 nColumnCount) const
134 {
135     int nRowCount = 0;
136     int nItemCount = GetItemCount();
137     if (nColumnCount > 0)
138     {
139         nRowCount = (nItemCount+nColumnCount-1) / nColumnCount;
140         if (nRowCount < 1)
141             nRowCount = 1;
142     }
143 
144     return (sal_uInt16)nRowCount;
145 }
146 
147 
148 
149 
150 sal_Int32 PreviewValueSet::GetPreferredWidth (sal_Int32 nHeight)
151 {
152     int nPreferredWidth (maPreviewSize.Width() + 2*mnBorderWidth);
153 
154     // Get height of each row.
155     int nItemHeight (maPreviewSize.Height() + 2*mnBorderHeight);
156 
157     // Calculate the row- and column count and from the later the preferred
158     // width.
159     int nRowCount = nHeight / nItemHeight;
160     if (nRowCount > 0)
161     {
162         int nColumnCount = (GetItemCount()+nRowCount-1) / nRowCount;
163         if (nColumnCount > 0)
164             nPreferredWidth = (maPreviewSize.Width() + 2*mnBorderWidth)
165                 * nColumnCount;
166     }
167 
168     return nPreferredWidth;
169 }
170 
171 
172 
173 
174 sal_Int32 PreviewValueSet::GetPreferredHeight (sal_Int32 nWidth)
175 {
176     int nRowCount (CalculateRowCount(CalculateColumnCount(nWidth)));
177     int nItemHeight (maPreviewSize.Height());
178 
179     return nRowCount * (nItemHeight + 2*mnBorderHeight);
180 }
181 
182 
183 
184 
185 } } // end of namespace sd::sidebar
186