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