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 : || ( 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 : else
145 0 : if ( i_eTargetAlignment == TABS_BOTTOM )
146 : {
147 0 : lcl_mirrorVertically( m_aReference, aResult );
148 : }
149 :
150 0 : return aResult;
151 : }
152 :
153 : //------------------------------------------------------------------------------------------------------------------
154 0 : Rectangle NormalizedArea::getNormalized( const Rectangle& i_rArea, const TabAlignment i_eTargetAlignment ) const
155 : {
156 0 : Rectangle aResult( i_rArea );
157 :
158 0 : if ( ( i_eTargetAlignment == TABS_RIGHT )
159 : || ( i_eTargetAlignment == TABS_LEFT )
160 : )
161 : {
162 0 : Rectangle aReference( m_aReference );
163 0 : lcl_rotate( m_aReference, aReference, true );
164 :
165 0 : if ( i_eTargetAlignment == TABS_LEFT )
166 : {
167 0 : lcl_mirrorHorizontally( aReference, aResult );
168 : }
169 :
170 0 : lcl_rotate( aReference, aResult, false );
171 : }
172 : else
173 0 : 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 : 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 bool i_bMinimalSize ) 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( i_bMinimalSize ? io_rItems.rbegin()->aIconOnlyArea : 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: */
|