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