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 :
21 : #include "tabbargeometry.hxx"
22 :
23 : #include <basegfx/range/b2drange.hxx>
24 : #include <basegfx/matrix/b2dhommatrix.hxx>
25 : #include <basegfx/numeric/ftools.hxx>
26 :
27 : #include <vcl/window.hxx>
28 :
29 : #include <algorithm>
30 :
31 : // the width (or height, depending on alignment) of the scroll buttons
32 : #define BUTTON_FLOW_WIDTH 20
33 : // the space between the scroll buttons and the items
34 : #define BUTTON_FLOW_SPACE 2
35 : // outer space to apply between the tab bar borders and any content. Note that those refer to a "normalized" geometry,
36 : // i.e. if the tab bar were aligned at the top
37 : #define OUTER_SPACE_LEFT 2
38 : #define OUTER_SPACE_TOP 4
39 : #define OUTER_SPACE_RIGHT 4
40 : #define OUTER_SPACE_BOTTOM 2
41 :
42 : // outer space to apply between the area for the items, and the actual items. They refer to a normalized geometry.
43 : #define ITEMS_INSET_LEFT 4
44 : #define ITEMS_INSET_TOP 3
45 : #define ITEMS_INSET_RIGHT 4
46 : #define ITEMS_INSET_BOTTOM 0
47 :
48 :
49 : namespace svt
50 : {
51 :
52 :
53 :
54 : //= helper
55 :
56 : namespace
57 : {
58 :
59 0 : static void lcl_transform( Rectangle& io_rRect, const ::basegfx::B2DHomMatrix& i_rTransformation )
60 : {
61 0 : ::basegfx::B2DRange aRect( io_rRect.Left(), io_rRect.Top(), io_rRect.Right(), io_rRect.Bottom() );
62 0 : aRect.transform( i_rTransformation );
63 0 : io_rRect.Left() = long( aRect.getMinX() );
64 0 : io_rRect.Top() = long( aRect.getMinY() );
65 0 : io_rRect.Right() = long( aRect.getMaxX() );
66 0 : io_rRect.Bottom() = long( aRect.getMaxY() );
67 0 : }
68 :
69 :
70 : /** transforms the given, possible rotated playground,
71 : */
72 0 : void lcl_rotate( const Rectangle& i_rReference, Rectangle& io_rArea, const bool i_bRight )
73 : {
74 : // step 1: move the to-be-upper-left corner (left/bottom) of the rectangle to (0,0)
75 0 : ::basegfx::B2DHomMatrix aTransformation;
76 : aTransformation.translate(
77 0 : i_bRight ? -i_rReference.Left() : -i_rReference.Right(),
78 0 : i_bRight ? -i_rReference.Bottom() : -i_rReference.Top()
79 0 : );
80 :
81 : // step 2: rotate by -90 degrees
82 0 : aTransformation.rotate( i_bRight ? +F_PI2 : -F_PI2 );
83 : // note:
84 : // on the screen, the ordinate goes top-down, while basegfx calculates in a system where the
85 : // ordinate goes bottom-up; thus the "wrong" sign before F_PI2 here
86 :
87 : // step 3: move back to original coordinates
88 0 : aTransformation.translate( i_rReference.Left(), i_rReference.Top() );
89 :
90 : // apply transformation
91 0 : lcl_transform( io_rArea, aTransformation );
92 0 : }
93 : }
94 :
95 :
96 0 : void lcl_mirrorHorizontally( const Rectangle& i_rReferenceArea, Rectangle& io_rArea )
97 : {
98 0 : io_rArea.Left() = i_rReferenceArea.Left() + i_rReferenceArea.Right() - io_rArea.Left();
99 0 : io_rArea.Right() = i_rReferenceArea.Left() + i_rReferenceArea.Right() - io_rArea.Right();
100 0 : ::std::swap( io_rArea.Left(), io_rArea.Right() );
101 0 : }
102 :
103 :
104 0 : void lcl_mirrorVertically( const Rectangle& i_rReferenceArea, Rectangle& io_rArea )
105 : {
106 0 : io_rArea.Top() = i_rReferenceArea.Top() + i_rReferenceArea.Bottom() - io_rArea.Top();
107 0 : io_rArea.Bottom() = i_rReferenceArea.Top() + i_rReferenceArea.Bottom() - io_rArea.Bottom();
108 0 : ::std::swap( io_rArea.Top(), io_rArea.Bottom() );
109 0 : }
110 :
111 :
112 : //= NormalizedArea
113 0 : NormalizedArea::NormalizedArea()
114 0 : :m_aReference()
115 : {
116 0 : }
117 :
118 0 : NormalizedArea::NormalizedArea( const Rectangle& i_rReference, const bool i_bIsVertical )
119 0 : : m_aReference(i_rReference)
120 : {
121 0 : if (i_bIsVertical)
122 : {
123 0 : const long nRotatedWidth = i_rReference.GetHeight();
124 0 : const long nRotatedHeight = i_rReference.GetWidth();
125 0 : m_aReference = Rectangle(i_rReference.TopLeft(), Size(nRotatedWidth, nRotatedHeight));
126 : }
127 0 : }
128 :
129 0 : Rectangle NormalizedArea::getTransformed( const Rectangle& i_rArea, const TabAlignment i_eTargetAlignment ) const
130 : {
131 0 : Rectangle aResult( i_rArea );
132 :
133 0 : if ( ( i_eTargetAlignment == TABS_RIGHT )
134 0 : || ( i_eTargetAlignment == TABS_LEFT )
135 : )
136 : {
137 0 : lcl_rotate( m_aReference, aResult, true );
138 :
139 0 : if ( i_eTargetAlignment == TABS_LEFT )
140 : {
141 0 : Rectangle aReference( m_aReference );
142 0 : aReference.Transpose();
143 0 : lcl_mirrorHorizontally( aReference, aResult );
144 0 : }
145 : }
146 0 : else if ( i_eTargetAlignment == TABS_BOTTOM )
147 : {
148 0 : lcl_mirrorVertically( m_aReference, aResult );
149 : }
150 :
151 0 : return aResult;
152 : }
153 :
154 :
155 0 : Rectangle NormalizedArea::getNormalized( const Rectangle& i_rArea, const TabAlignment i_eTargetAlignment ) const
156 : {
157 0 : Rectangle aResult( i_rArea );
158 :
159 0 : if ( ( i_eTargetAlignment == TABS_RIGHT )
160 0 : || ( i_eTargetAlignment == TABS_LEFT )
161 : )
162 : {
163 0 : Rectangle aReference( m_aReference );
164 0 : lcl_rotate( m_aReference, aReference, true );
165 :
166 0 : if ( i_eTargetAlignment == TABS_LEFT )
167 : {
168 0 : lcl_mirrorHorizontally( aReference, aResult );
169 : }
170 :
171 0 : lcl_rotate( aReference, aResult, false );
172 : }
173 0 : else if ( i_eTargetAlignment == TABS_BOTTOM )
174 : {
175 0 : lcl_mirrorVertically( m_aReference, aResult );
176 : }
177 0 : return aResult;
178 : }
179 :
180 :
181 : //= TabBarGeometry
182 :
183 :
184 0 : TabBarGeometry::TabBarGeometry( const TabItemContent i_eItemContent )
185 : :m_eTabItemContent( i_eItemContent )
186 : ,m_aItemsInset()
187 : ,m_aButtonBackRect()
188 : ,m_aItemsRect()
189 0 : ,m_aButtonForwardRect()
190 : {
191 0 : m_aItemsInset.Left() = ITEMS_INSET_LEFT;
192 0 : m_aItemsInset.Top() = ITEMS_INSET_TOP;
193 0 : m_aItemsInset.Right() = ITEMS_INSET_RIGHT;
194 0 : m_aItemsInset.Bottom() = ITEMS_INSET_BOTTOM;
195 0 : }
196 :
197 :
198 0 : TabBarGeometry::~TabBarGeometry()
199 : {
200 0 : }
201 :
202 :
203 0 : bool TabBarGeometry::impl_fitItems( ItemDescriptors& io_rItems ) const
204 : {
205 0 : if ( io_rItems.empty() )
206 : // nothing to do, "no items" perfectly fit into any space we have ...
207 0 : return true;
208 :
209 : // the available size
210 0 : Size aOutputSize( getItemsRect().GetSize() );
211 : // shrunk by the outer space
212 0 : aOutputSize.Width() -= m_aItemsInset.Right();
213 0 : aOutputSize.Height() -= m_aItemsInset.Bottom();
214 0 : const Rectangle aFitInto( Point( 0, 0 ), aOutputSize );
215 :
216 0 : TabItemContent eItemContent( getItemContent() );
217 0 : if ( eItemContent == TABITEM_AUTO )
218 : {
219 : // the "content modes" to try
220 : TabItemContent eTryThis[] =
221 : {
222 : TABITEM_IMAGE_ONLY, // assumed to have the smallest rects
223 : TABITEM_TEXT_ONLY,
224 : TABITEM_IMAGE_AND_TEXT // assumed to have the largest rects
225 0 : };
226 :
227 :
228 : // determine which of the different version fits
229 0 : eItemContent = eTryThis[0];
230 0 : size_t nTryIndex = 2;
231 0 : while ( nTryIndex > 0 )
232 : {
233 0 : const Point aBottomRight( io_rItems.rbegin()->GetRect( eTryThis[ nTryIndex ] ).BottomRight() );
234 0 : if ( aFitInto.IsInside( aBottomRight ) )
235 : {
236 0 : eItemContent = eTryThis[ nTryIndex ];
237 0 : break;
238 : }
239 0 : --nTryIndex;
240 : }
241 : }
242 :
243 : // propagate to the items
244 0 : for ( ItemDescriptors::iterator item = io_rItems.begin();
245 0 : item != io_rItems.end();
246 : ++item
247 : )
248 : {
249 0 : item->eContent = eItemContent;
250 : }
251 :
252 0 : const ItemDescriptor& rLastItem( *io_rItems.rbegin() );
253 0 : const Point aLastItemBottomRight( rLastItem.GetCurrentRect().BottomRight() );
254 0 : return aFitInto.Left() <= aLastItemBottomRight.X()
255 0 : && aFitInto.Right() >= aLastItemBottomRight.X();
256 : }
257 :
258 :
259 0 : Size TabBarGeometry::getOptimalSize(ItemDescriptors& io_rItems) const
260 : {
261 0 : if ( io_rItems.empty() )
262 : return Size(
263 0 : m_aItemsInset.Left() + m_aItemsInset.Right(),
264 0 : m_aItemsInset.Top() + m_aItemsInset.Bottom()
265 0 : );
266 :
267 : // the rect of the last item
268 0 : const Rectangle& rLastItemRect(io_rItems.rbegin()->aCompleteArea);
269 : return Size(
270 0 : rLastItemRect.Left() + 1 + m_aItemsInset.Right(),
271 0 : rLastItemRect.Top() + 1 + rLastItemRect.Bottom() + m_aItemsInset.Bottom()
272 0 : );
273 : }
274 :
275 :
276 0 : void TabBarGeometry::relayout( const Size& i_rActualOutputSize, ItemDescriptors& io_rItems )
277 : {
278 : // assume all items fit
279 0 : Point aButtonBackPos( OUTER_SPACE_LEFT, OUTER_SPACE_TOP );
280 0 : m_aButtonBackRect = Rectangle( aButtonBackPos, Size( 1, 1 ) );
281 0 : m_aButtonBackRect.SetEmpty();
282 :
283 0 : Point aButtonForwardPos( i_rActualOutputSize.Width(), OUTER_SPACE_TOP );
284 0 : m_aButtonForwardRect = Rectangle( aButtonForwardPos, Size( 1, 1 ) );
285 0 : m_aButtonForwardRect.SetEmpty();
286 :
287 0 : Point aItemsPos( OUTER_SPACE_LEFT, 0 );
288 0 : Size aItemsSize( i_rActualOutputSize.Width() - OUTER_SPACE_LEFT - OUTER_SPACE_RIGHT, i_rActualOutputSize.Height() );
289 0 : m_aItemsRect = Rectangle( aItemsPos, aItemsSize );
290 :
291 0 : if ( !impl_fitItems( io_rItems ) )
292 : {
293 : // assumption was wrong, the items do not fit => calculate rects for the scroll buttons
294 0 : const Size aButtonSize( BUTTON_FLOW_WIDTH, i_rActualOutputSize.Height() - OUTER_SPACE_TOP - OUTER_SPACE_BOTTOM );
295 :
296 0 : aButtonBackPos = Point( OUTER_SPACE_LEFT, OUTER_SPACE_TOP );
297 0 : m_aButtonBackRect = Rectangle( aButtonBackPos, aButtonSize );
298 :
299 0 : aButtonForwardPos = Point( i_rActualOutputSize.Width() - BUTTON_FLOW_WIDTH - OUTER_SPACE_RIGHT, OUTER_SPACE_TOP );
300 0 : m_aButtonForwardRect = Rectangle( aButtonForwardPos, aButtonSize );
301 :
302 0 : aItemsPos.X() = aButtonBackPos.X() + aButtonSize.Width() + BUTTON_FLOW_SPACE;
303 0 : aItemsSize.Width() = aButtonForwardPos.X() - BUTTON_FLOW_SPACE - aItemsPos.X();
304 0 : m_aItemsRect = Rectangle( aItemsPos, aItemsSize );
305 :
306 : // fit items, again. In the TABITEM_AUTO case, the smaller playground for the items might lead to another
307 : // item content.
308 0 : impl_fitItems( io_rItems );
309 : }
310 0 : }
311 :
312 :
313 0 : Point TabBarGeometry::getFirstItemPosition() const
314 : {
315 0 : return Point( m_aItemsInset.Left(), m_aItemsInset.Top() );
316 : }
317 :
318 :
319 : } // namespace svt
320 :
321 :
322 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|