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