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 :
114 :
115 0 : NormalizedArea::NormalizedArea()
116 0 : :m_aReference()
117 : {
118 0 : }
119 :
120 :
121 0 : NormalizedArea::NormalizedArea( const Rectangle& i_rReference, const bool i_bIsVertical )
122 0 : :m_aReference( i_bIsVertical ? Rectangle( i_rReference.TopLeft(), Size( i_rReference.GetHeight(), i_rReference.GetWidth() ) ) : i_rReference )
123 : {
124 0 : }
125 :
126 :
127 0 : Rectangle NormalizedArea::getTransformed( const Rectangle& i_rArea, const TabAlignment i_eTargetAlignment ) const
128 : {
129 0 : Rectangle aResult( i_rArea );
130 :
131 0 : if ( ( i_eTargetAlignment == TABS_RIGHT )
132 0 : || ( i_eTargetAlignment == TABS_LEFT )
133 : )
134 : {
135 0 : lcl_rotate( m_aReference, aResult, true );
136 :
137 0 : if ( i_eTargetAlignment == TABS_LEFT )
138 : {
139 0 : Rectangle aReference( m_aReference );
140 0 : aReference.Transpose();
141 0 : lcl_mirrorHorizontally( aReference, aResult );
142 0 : }
143 : }
144 0 : else if ( i_eTargetAlignment == TABS_BOTTOM )
145 : {
146 0 : lcl_mirrorVertically( m_aReference, aResult );
147 : }
148 :
149 0 : return aResult;
150 : }
151 :
152 :
153 0 : Rectangle NormalizedArea::getNormalized( const Rectangle& i_rArea, const TabAlignment i_eTargetAlignment ) const
154 : {
155 0 : Rectangle aResult( i_rArea );
156 :
157 0 : if ( ( i_eTargetAlignment == TABS_RIGHT )
158 0 : || ( i_eTargetAlignment == TABS_LEFT )
159 : )
160 : {
161 0 : Rectangle aReference( m_aReference );
162 0 : lcl_rotate( m_aReference, aReference, true );
163 :
164 0 : if ( i_eTargetAlignment == TABS_LEFT )
165 : {
166 0 : lcl_mirrorHorizontally( aReference, aResult );
167 : }
168 :
169 0 : lcl_rotate( aReference, aResult, false );
170 : }
171 0 : else if ( i_eTargetAlignment == TABS_BOTTOM )
172 : {
173 0 : lcl_mirrorVertically( m_aReference, aResult );
174 : }
175 0 : return aResult;
176 : }
177 :
178 :
179 : //= TabBarGeometry
180 :
181 :
182 0 : TabBarGeometry::TabBarGeometry( const TabItemContent i_eItemContent )
183 : :m_eTabItemContent( i_eItemContent )
184 : ,m_aItemsInset()
185 : ,m_aButtonBackRect()
186 : ,m_aItemsRect()
187 0 : ,m_aButtonForwardRect()
188 : {
189 0 : m_aItemsInset.Left() = ITEMS_INSET_LEFT;
190 0 : m_aItemsInset.Top() = ITEMS_INSET_TOP;
191 0 : m_aItemsInset.Right() = ITEMS_INSET_RIGHT;
192 0 : m_aItemsInset.Bottom() = ITEMS_INSET_BOTTOM;
193 0 : }
194 :
195 :
196 0 : TabBarGeometry::~TabBarGeometry()
197 : {
198 0 : }
199 :
200 :
201 0 : bool TabBarGeometry::impl_fitItems( ItemDescriptors& io_rItems ) const
202 : {
203 0 : if ( io_rItems.empty() )
204 : // nothing to do, "no items" perfectly fit into any space we have ...
205 0 : return true;
206 :
207 : // the available size
208 0 : Size aOutputSize( getItemsRect().GetSize() );
209 : // shrunk by the outer space
210 0 : aOutputSize.Width() -= m_aItemsInset.Right();
211 0 : aOutputSize.Height() -= m_aItemsInset.Bottom();
212 0 : const Rectangle aFitInto( Point( 0, 0 ), aOutputSize );
213 :
214 0 : TabItemContent eItemContent( getItemContent() );
215 0 : if ( eItemContent == TABITEM_AUTO )
216 : {
217 : // the "content modes" to try
218 : TabItemContent eTryThis[] =
219 : {
220 : TABITEM_IMAGE_ONLY, // assumed to have the smallest rects
221 : TABITEM_TEXT_ONLY,
222 : TABITEM_IMAGE_AND_TEXT // assumed to have the largest rects
223 0 : };
224 :
225 :
226 : // determine which of the different version fits
227 0 : eItemContent = eTryThis[0];
228 0 : size_t nTryIndex = 2;
229 0 : while ( nTryIndex > 0 )
230 : {
231 0 : const Point aBottomRight( io_rItems.rbegin()->GetRect( eTryThis[ nTryIndex ] ).BottomRight() );
232 0 : if ( aFitInto.IsInside( aBottomRight ) )
233 : {
234 0 : eItemContent = eTryThis[ nTryIndex ];
235 0 : break;
236 : }
237 0 : --nTryIndex;
238 : }
239 : }
240 :
241 : // propagate to the items
242 0 : for ( ItemDescriptors::iterator item = io_rItems.begin();
243 0 : item != io_rItems.end();
244 : ++item
245 : )
246 : {
247 0 : item->eContent = eItemContent;
248 : }
249 :
250 0 : const ItemDescriptor& rLastItem( *io_rItems.rbegin() );
251 0 : const Point aLastItemBottomRight( rLastItem.GetCurrentRect().BottomRight() );
252 0 : return aFitInto.Left() <= aLastItemBottomRight.X()
253 0 : && aFitInto.Right() >= aLastItemBottomRight.X();
254 : }
255 :
256 :
257 0 : Size TabBarGeometry::getOptimalSize(ItemDescriptors& io_rItems) const
258 : {
259 0 : if ( io_rItems.empty() )
260 : return Size(
261 0 : m_aItemsInset.Left() + m_aItemsInset.Right(),
262 0 : m_aItemsInset.Top() + m_aItemsInset.Bottom()
263 0 : );
264 :
265 : // the rect of the last item
266 0 : const Rectangle& rLastItemRect(io_rItems.rbegin()->aCompleteArea);
267 : return Size(
268 0 : rLastItemRect.Left() + 1 + m_aItemsInset.Right(),
269 0 : rLastItemRect.Top() + 1 + rLastItemRect.Bottom() + m_aItemsInset.Bottom()
270 0 : );
271 : }
272 :
273 :
274 0 : void TabBarGeometry::relayout( const Size& i_rActualOutputSize, ItemDescriptors& io_rItems )
275 : {
276 : // assume all items fit
277 0 : Point aButtonBackPos( OUTER_SPACE_LEFT, OUTER_SPACE_TOP );
278 0 : m_aButtonBackRect = Rectangle( aButtonBackPos, Size( 1, 1 ) );
279 0 : m_aButtonBackRect.SetEmpty();
280 :
281 0 : Point aButtonForwardPos( i_rActualOutputSize.Width(), OUTER_SPACE_TOP );
282 0 : m_aButtonForwardRect = Rectangle( aButtonForwardPos, Size( 1, 1 ) );
283 0 : m_aButtonForwardRect.SetEmpty();
284 :
285 0 : Point aItemsPos( OUTER_SPACE_LEFT, 0 );
286 0 : Size aItemsSize( i_rActualOutputSize.Width() - OUTER_SPACE_LEFT - OUTER_SPACE_RIGHT, i_rActualOutputSize.Height() );
287 0 : m_aItemsRect = Rectangle( aItemsPos, aItemsSize );
288 :
289 0 : if ( !impl_fitItems( io_rItems ) )
290 : {
291 : // assumption was wrong, the items do not fit => calculate rects for the scroll buttons
292 0 : const Size aButtonSize( BUTTON_FLOW_WIDTH, i_rActualOutputSize.Height() - OUTER_SPACE_TOP - OUTER_SPACE_BOTTOM );
293 :
294 0 : aButtonBackPos = Point( OUTER_SPACE_LEFT, OUTER_SPACE_TOP );
295 0 : m_aButtonBackRect = Rectangle( aButtonBackPos, aButtonSize );
296 :
297 0 : aButtonForwardPos = Point( i_rActualOutputSize.Width() - BUTTON_FLOW_WIDTH - OUTER_SPACE_RIGHT, OUTER_SPACE_TOP );
298 0 : m_aButtonForwardRect = Rectangle( aButtonForwardPos, aButtonSize );
299 :
300 0 : aItemsPos.X() = aButtonBackPos.X() + aButtonSize.Width() + BUTTON_FLOW_SPACE;
301 0 : aItemsSize.Width() = aButtonForwardPos.X() - BUTTON_FLOW_SPACE - aItemsPos.X();
302 0 : m_aItemsRect = Rectangle( aItemsPos, aItemsSize );
303 :
304 : // fit items, again. In the TABITEM_AUTO case, the smaller playground for the items might lead to another
305 : // item content.
306 0 : impl_fitItems( io_rItems );
307 : }
308 0 : }
309 :
310 :
311 0 : Point TabBarGeometry::getFirstItemPosition() const
312 : {
313 0 : return Point( m_aItemsInset.Left(), m_aItemsInset.Top() );
314 : }
315 :
316 :
317 : } // namespace svt
318 :
319 :
320 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|