1*b0724fc6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b0724fc6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b0724fc6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b0724fc6SAndrew Rist * distributed with this work for additional information
6*b0724fc6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b0724fc6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b0724fc6SAndrew Rist * "License"); you may not use this file except in compliance
9*b0724fc6SAndrew Rist * with the License. You may obtain a copy of the License at
10*b0724fc6SAndrew Rist *
11*b0724fc6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b0724fc6SAndrew Rist *
13*b0724fc6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b0724fc6SAndrew Rist * software distributed under the License is distributed on an
15*b0724fc6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b0724fc6SAndrew Rist * KIND, either express or implied. See the License for the
17*b0724fc6SAndrew Rist * specific language governing permissions and limitations
18*b0724fc6SAndrew Rist * under the License.
19*b0724fc6SAndrew Rist *
20*b0724fc6SAndrew Rist *************************************************************/
21*b0724fc6SAndrew Rist
22*b0724fc6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "box.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include <tools/debug.hxx>
27cdf0e10cSrcweir #include <sal/macros.h>
28cdf0e10cSrcweir
29cdf0e10cSrcweir // fixed point precision for distributing error
30cdf0e10cSrcweir #define FIXED_PT 16
31cdf0e10cSrcweir
32cdf0e10cSrcweir namespace layoutimpl
33cdf0e10cSrcweir {
34cdf0e10cSrcweir
35cdf0e10cSrcweir using namespace css;
36cdf0e10cSrcweir
ChildProps(Box::ChildData * pData)37cdf0e10cSrcweir Box::ChildProps::ChildProps( Box::ChildData *pData )
38cdf0e10cSrcweir {
39cdf0e10cSrcweir addProp( RTL_CONSTASCII_USTRINGPARAM( "Expand" ),
40cdf0e10cSrcweir ::getCppuType( static_cast< const sal_Bool* >( NULL ) ),
41cdf0e10cSrcweir &(pData->mbExpand) );
42cdf0e10cSrcweir addProp( RTL_CONSTASCII_USTRINGPARAM( "Fill" ),
43cdf0e10cSrcweir ::getCppuType( static_cast< const sal_Bool* >( NULL ) ),
44cdf0e10cSrcweir &(pData->mbFill) );
45cdf0e10cSrcweir addProp( RTL_CONSTASCII_USTRINGPARAM( "Padding" ),
46cdf0e10cSrcweir ::getCppuType( static_cast< const sal_Int32* >( NULL ) ),
47cdf0e10cSrcweir &(pData->mnPadding) );
48cdf0e10cSrcweir }
49cdf0e10cSrcweir
ChildData(uno::Reference<awt::XLayoutConstrains> const & xChild)50cdf0e10cSrcweir Box::ChildData::ChildData( uno::Reference< awt::XLayoutConstrains > const& xChild )
51cdf0e10cSrcweir : Box_Base::ChildData( xChild )
52cdf0e10cSrcweir , mnPadding( 0 )
53cdf0e10cSrcweir , mbExpand( true )
54cdf0e10cSrcweir , mbFill( true )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir }
57cdf0e10cSrcweir
58cdf0e10cSrcweir Box::ChildData*
createChild(uno::Reference<awt::XLayoutConstrains> const & xChild)59cdf0e10cSrcweir Box::createChild( uno::Reference< awt::XLayoutConstrains > const& xChild )
60cdf0e10cSrcweir {
61cdf0e10cSrcweir return new ChildData( xChild );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
64cdf0e10cSrcweir Box::ChildProps*
createChildProps(Box_Base::ChildData * pData)65cdf0e10cSrcweir Box::createChildProps( Box_Base::ChildData *pData )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir return new ChildProps( static_cast<Box::ChildData*> ( pData ) );
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
Box(bool horizontal)70cdf0e10cSrcweir Box::Box( bool horizontal )
71cdf0e10cSrcweir : Box_Base()
72cdf0e10cSrcweir , mnSpacing( 0 )
73cdf0e10cSrcweir , mbHomogeneous( false )
74cdf0e10cSrcweir , mbHorizontal( horizontal )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir addProp( RTL_CONSTASCII_USTRINGPARAM( "Homogeneous" ),
77cdf0e10cSrcweir ::getCppuType( static_cast< const sal_Bool* >( NULL ) ),
78cdf0e10cSrcweir &mbHomogeneous );
79cdf0e10cSrcweir addProp( RTL_CONSTASCII_USTRINGPARAM( "Spacing" ),
80cdf0e10cSrcweir ::getCppuType( static_cast< const sal_Int32* >( NULL ) ),
81cdf0e10cSrcweir &mnSpacing );
82cdf0e10cSrcweir mbHasFlowChildren = false;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
85cdf0e10cSrcweir awt::Size
calculateSize(long nWidth)86cdf0e10cSrcweir Box::calculateSize( long nWidth )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir int nVisibleChildren = 0;
89cdf0e10cSrcweir // primary vs secundary axis (instead of a X and Y)
90cdf0e10cSrcweir int nPrimSize = 0;
91cdf0e10cSrcweir int nSecSize = 0;
92cdf0e10cSrcweir int nFlowMinWidth = 0; // in case the box only has flow children
93cdf0e10cSrcweir
94cdf0e10cSrcweir mbHasFlowChildren = false;
95cdf0e10cSrcweir
96cdf0e10cSrcweir for ( std::list<Box_Base::ChildData *>::const_iterator it
97cdf0e10cSrcweir = maChildren.begin(); it != maChildren.end(); it++ )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir ChildData *child = static_cast<Box::ChildData*> ( *it );
100cdf0e10cSrcweir if ( !child->isVisible() )
101cdf0e10cSrcweir continue;
102cdf0e10cSrcweir
103cdf0e10cSrcweir uno::Reference< awt::XLayoutContainer > xChildCont( child->mxChild, uno::UNO_QUERY );
104cdf0e10cSrcweir bool bFlow = xChildCont.is() && xChildCont->hasHeightForWidth();
105cdf0e10cSrcweir
106cdf0e10cSrcweir awt::Size aChildSize = child->maRequisition = child->mxChild->getMinimumSize();
107cdf0e10cSrcweir
108cdf0e10cSrcweir if ( !mbHorizontal /*vertical*/ && bFlow )
109cdf0e10cSrcweir {
110cdf0e10cSrcweir if ( nFlowMinWidth == 0 || nFlowMinWidth > aChildSize.Width )
111cdf0e10cSrcweir nFlowMinWidth = aChildSize.Width;
112cdf0e10cSrcweir mbHasFlowChildren = true;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir else
115cdf0e10cSrcweir {
116cdf0e10cSrcweir int size = primDim( aChildSize ) + child->mnPadding * 2;
117cdf0e10cSrcweir if ( mbHomogeneous )
118cdf0e10cSrcweir nPrimSize = SAL_MAX( nPrimSize, size );
119cdf0e10cSrcweir else
120cdf0e10cSrcweir nPrimSize += size;
121cdf0e10cSrcweir
122cdf0e10cSrcweir nSecSize = SAL_MAX( nSecSize, secDim( aChildSize ) );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir nVisibleChildren++;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir if ( nVisibleChildren )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir if ( mbHomogeneous )
130cdf0e10cSrcweir nPrimSize *= nVisibleChildren;
131cdf0e10cSrcweir nPrimSize += (nVisibleChildren - 1) * mnSpacing;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir
134cdf0e10cSrcweir if ( mbHasFlowChildren )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir if ( nWidth == 0 )
137cdf0e10cSrcweir nWidth = nSecSize ? nSecSize : nFlowMinWidth;
138cdf0e10cSrcweir for ( std::list<Box_Base::ChildData *>::const_iterator it
139cdf0e10cSrcweir = maChildren.begin(); it != maChildren.end(); it++ )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir ChildData *child = static_cast<Box::ChildData*> ( *it );
142cdf0e10cSrcweir if ( !child->isVisible() )
143cdf0e10cSrcweir continue;
144cdf0e10cSrcweir
145cdf0e10cSrcweir uno::Reference< awt::XLayoutContainer > xChildCont( child->mxChild, uno::UNO_QUERY );
146cdf0e10cSrcweir bool bFlow = xChildCont.is() && xChildCont->hasHeightForWidth();
147cdf0e10cSrcweir
148cdf0e10cSrcweir if ( bFlow )
149cdf0e10cSrcweir nPrimSize += xChildCont->getHeightForWidth( nWidth );
150cdf0e10cSrcweir }
151cdf0e10cSrcweir }
152cdf0e10cSrcweir
153cdf0e10cSrcweir nPrimSize += mnBorderWidth * 2;
154cdf0e10cSrcweir nSecSize += mnBorderWidth * 2;
155cdf0e10cSrcweir return awt::Size( mbHorizontal ? nPrimSize : nSecSize,
156cdf0e10cSrcweir mbHorizontal ? nSecSize : nPrimSize );
157cdf0e10cSrcweir }
158cdf0e10cSrcweir
159cdf0e10cSrcweir awt::Size SAL_CALL
getMinimumSize()160cdf0e10cSrcweir Box::getMinimumSize() throw(uno::RuntimeException)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir maRequisition = calculateSize();
163cdf0e10cSrcweir return maRequisition;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir sal_Bool SAL_CALL
hasHeightForWidth()167cdf0e10cSrcweir Box::hasHeightForWidth()
168cdf0e10cSrcweir throw(uno::RuntimeException)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir return mbHasFlowChildren;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir
173cdf0e10cSrcweir sal_Int32 SAL_CALL
getHeightForWidth(sal_Int32 nWidth)174cdf0e10cSrcweir Box::getHeightForWidth( sal_Int32 nWidth )
175cdf0e10cSrcweir throw(uno::RuntimeException)
176cdf0e10cSrcweir {
177cdf0e10cSrcweir if ( hasHeightForWidth() )
178cdf0e10cSrcweir return calculateSize( nWidth ).Height;
179cdf0e10cSrcweir return maRequisition.Height;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir
182cdf0e10cSrcweir void SAL_CALL
allocateArea(const awt::Rectangle & newArea)183cdf0e10cSrcweir Box::allocateArea( const awt::Rectangle &newArea )
184cdf0e10cSrcweir throw (uno::RuntimeException)
185cdf0e10cSrcweir {
186cdf0e10cSrcweir maAllocation = newArea;
187cdf0e10cSrcweir int nVisibleChildren = 0, nExpandChildren = 0;
188cdf0e10cSrcweir
189cdf0e10cSrcweir for ( std::list<Box_Base::ChildData *>::const_iterator it
190cdf0e10cSrcweir = maChildren.begin(); it != maChildren.end(); it++ )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir ChildData *child = static_cast<Box::ChildData*> ( *it );
193cdf0e10cSrcweir if ( child->isVisible() )
194cdf0e10cSrcweir {
195cdf0e10cSrcweir nVisibleChildren++;
196cdf0e10cSrcweir if ( child->mbExpand )
197cdf0e10cSrcweir nExpandChildren++;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir }
200cdf0e10cSrcweir if ( !nVisibleChildren )
201cdf0e10cSrcweir return;
202cdf0e10cSrcweir
203cdf0e10cSrcweir // split rectangle for dimension helpers
204cdf0e10cSrcweir awt::Point newPoint( newArea.X, newArea.Y );
205cdf0e10cSrcweir awt::Size newSize( newArea.Width, newArea.Height );
206cdf0e10cSrcweir
207cdf0e10cSrcweir int nExtraSpace;
208cdf0e10cSrcweir if ( mbHomogeneous )
209cdf0e10cSrcweir nExtraSpace = ( ( primDim( newSize ) - mnBorderWidth * 2 -
210cdf0e10cSrcweir ( nVisibleChildren - 1 ) * mnSpacing )) / nVisibleChildren;
211cdf0e10cSrcweir else if ( nExpandChildren )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir int reqSize = primDim( maRequisition );
214cdf0e10cSrcweir if ( !mbHorizontal && hasHeightForWidth() )
215cdf0e10cSrcweir reqSize = getHeightForWidth( newArea.Width );
216cdf0e10cSrcweir nExtraSpace = ( primDim( newSize ) - reqSize ) / nExpandChildren;
217cdf0e10cSrcweir }
218cdf0e10cSrcweir else
219cdf0e10cSrcweir nExtraSpace = 0;
220cdf0e10cSrcweir
221cdf0e10cSrcweir int nChildPrimPoint, nChildSecPoint, nChildPrimSize, nChildSecSize;
222cdf0e10cSrcweir
223cdf0e10cSrcweir int nStartPoint = primDim( newPoint ) + mnBorderWidth;
224cdf0e10cSrcweir int nBoxSecSize = SAL_MAX( 1, secDim( newSize ) - mnBorderWidth * 2 );
225cdf0e10cSrcweir
226cdf0e10cSrcweir for ( std::list<Box_Base::ChildData *>::const_iterator it
227cdf0e10cSrcweir = maChildren.begin(); it != maChildren.end(); it++ )
228cdf0e10cSrcweir {
229cdf0e10cSrcweir ChildData *child = static_cast<Box::ChildData*> ( *it );
230cdf0e10cSrcweir if ( !child->isVisible() )
231cdf0e10cSrcweir continue;
232cdf0e10cSrcweir
233cdf0e10cSrcweir awt::Point aChildPos;
234cdf0e10cSrcweir int nBoxPrimSize; // of the available box space
235cdf0e10cSrcweir
236cdf0e10cSrcweir if ( mbHomogeneous )
237cdf0e10cSrcweir nBoxPrimSize = nExtraSpace;
238cdf0e10cSrcweir else
239cdf0e10cSrcweir {
240cdf0e10cSrcweir uno::Reference< awt::XLayoutContainer > xChildCont( child->mxChild, uno::UNO_QUERY );
241cdf0e10cSrcweir bool bFlow = xChildCont.is() && xChildCont->hasHeightForWidth();
242cdf0e10cSrcweir if ( !mbHorizontal && bFlow )
243cdf0e10cSrcweir nBoxPrimSize = xChildCont->getHeightForWidth( newArea.Width );
244cdf0e10cSrcweir else
245cdf0e10cSrcweir nBoxPrimSize = primDim( child->maRequisition );
246cdf0e10cSrcweir nBoxPrimSize += child->mnPadding;
247cdf0e10cSrcweir if ( child->mbExpand )
248cdf0e10cSrcweir nBoxPrimSize += nExtraSpace;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir
251cdf0e10cSrcweir nChildPrimPoint = nStartPoint + child->mnPadding;
252cdf0e10cSrcweir nChildSecPoint = secDim( newPoint ) + mnBorderWidth;
253cdf0e10cSrcweir
254cdf0e10cSrcweir nChildSecSize = nBoxSecSize;
255cdf0e10cSrcweir if ( child->mbFill )
256cdf0e10cSrcweir nChildPrimSize = SAL_MAX( 1, nBoxPrimSize - child->mnPadding);
257cdf0e10cSrcweir else
258cdf0e10cSrcweir {
259cdf0e10cSrcweir nChildPrimSize = primDim( child->maRequisition );
260cdf0e10cSrcweir nChildPrimPoint += (nBoxPrimSize - nChildPrimSize) / 2;
261cdf0e10cSrcweir
262cdf0e10cSrcweir nChildSecPoint += (nBoxSecSize - nChildSecSize) / 2;
263cdf0e10cSrcweir }
264cdf0e10cSrcweir
265cdf0e10cSrcweir awt::Rectangle area;
266cdf0e10cSrcweir area.X = mbHorizontal ? nChildPrimPoint : nChildSecPoint;
267cdf0e10cSrcweir area.Y = mbHorizontal ? nChildSecPoint : nChildPrimPoint;
268cdf0e10cSrcweir area.Width = mbHorizontal ? nChildPrimSize : nChildSecSize;
269cdf0e10cSrcweir area.Height = mbHorizontal ? nChildSecSize : nChildPrimSize;
270cdf0e10cSrcweir
271cdf0e10cSrcweir allocateChildAt( child->mxChild, area );
272cdf0e10cSrcweir
273cdf0e10cSrcweir nStartPoint += nBoxPrimSize + mnSpacing + child->mnPadding;
274cdf0e10cSrcweir }
275cdf0e10cSrcweir }
276cdf0e10cSrcweir
277cdf0e10cSrcweir } // namespace layoutimpl
278