merge the formfield patch from ooo-build
[ooovba.git] / toolkit / source / layout / core / flow.cxx
blobb004336104d3768090b9eb7830263183d082ea5a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile$
11 * $Revision$
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "flow.hxx"
34 #include <sal/macros.h>
36 namespace layoutimpl
39 using namespace css;
41 bool Flow::ChildData::isVisible()
43 return xChild.is();
46 Flow::Flow()
47 : Container()
48 , mnSpacing( 0 )
49 , mbHomogeneous( false )
51 addProp( RTL_CONSTASCII_USTRINGPARAM( "Homogeneous" ),
52 ::getCppuType( static_cast< const sal_Bool* >( NULL ) ),
53 &mbHomogeneous );
54 addProp( RTL_CONSTASCII_USTRINGPARAM( "Spacing" ),
55 ::getCppuType( static_cast< const sal_Int32* >( NULL ) ),
56 &mnSpacing );
59 bool
60 Flow::emptyVisible ()
62 return true;
65 void SAL_CALL
66 Flow::addChild( const uno::Reference< awt::XLayoutConstrains >& xChild )
67 throw (uno::RuntimeException, css::awt::MaxChildrenException)
69 if ( xChild.is() )
71 ChildData *pData = new ChildData();
72 pData->xChild = xChild;
73 maChildren.push_back( pData );
75 setChildParent( xChild );
76 queueResize();
80 void SAL_CALL
81 Flow::removeChild( const css::uno::Reference< css::awt::XLayoutConstrains >& xChild )
82 throw (css::uno::RuntimeException)
84 for ( std::list< ChildData * >::iterator it = maChildren.begin();
85 it != maChildren.end(); it++ )
87 if ( (*it)->xChild == xChild )
89 delete *it;
90 maChildren.erase( it );
92 unsetChildParent( xChild );
93 queueResize();
94 break;
99 css::uno::Sequence< css::uno::Reference < css::awt::XLayoutConstrains > > SAL_CALL
100 Flow::getChildren()
101 throw (css::uno::RuntimeException)
103 uno::Sequence< uno::Reference< awt::XLayoutConstrains > > children( maChildren.size() );
104 unsigned int i = 0;
105 for ( std::list< ChildData * >::iterator it = maChildren.begin();
106 it != maChildren.end(); it++, i++ )
107 children[i] = (*it)->xChild;
109 return children;
112 uno::Reference< beans::XPropertySet > SAL_CALL
113 Flow::getChildProperties( const uno::Reference< awt::XLayoutConstrains >& /*xChild*/ )
114 throw (uno::RuntimeException)
116 return uno::Reference< beans::XPropertySet >();
119 css::awt::Size
120 Flow::calculateSize( long nMaxWidth )
122 long nNeedHeight = 0;
124 std::list<ChildData *>::const_iterator it;
125 mnEachWidth = 0;
126 // first pass, for homogeneous property
127 for (it = maChildren.begin(); it != maChildren.end(); it++)
129 if ( !(*it)->isVisible() )
130 continue;
131 (*it)->aRequisition = (*it)->xChild->getMinimumSize();
132 if ( mbHomogeneous )
133 mnEachWidth = SAL_MAX( mnEachWidth, (*it)->aRequisition.Width );
136 long nRowWidth = 0, nRowHeight = 0;
137 for (it = maChildren.begin(); it != maChildren.end(); it++)
139 if ( !(*it)->isVisible() )
140 continue;
142 awt::Size aChildSize = (*it)->aRequisition;
143 if ( mbHomogeneous )
144 aChildSize.Width = mnEachWidth;
146 if ( nMaxWidth && nRowWidth > 0 && nRowWidth + aChildSize.Width > nMaxWidth )
148 nRowWidth = 0;
149 nNeedHeight += nRowHeight;
150 nRowHeight = 0;
152 nRowHeight = SAL_MAX( nRowHeight, aChildSize.Height );
153 nRowWidth += aChildSize.Width;
155 nNeedHeight += nRowHeight;
157 return awt::Size( nRowWidth, nNeedHeight );
160 awt::Size SAL_CALL
161 Flow::getMinimumSize() throw(uno::RuntimeException)
163 return maRequisition = calculateSize( 0 );
166 sal_Bool SAL_CALL
167 Flow::hasHeightForWidth()
168 throw(css::uno::RuntimeException)
170 return true;
173 sal_Int32 SAL_CALL
174 Flow::getHeightForWidth( sal_Int32 nWidth )
175 throw(css::uno::RuntimeException)
177 return calculateSize( nWidth ).Height;
180 void SAL_CALL
181 Flow::allocateArea( const css::awt::Rectangle &rArea )
182 throw (css::uno::RuntimeException)
184 maAllocation = rArea;
186 std::list<ChildData *>::const_iterator it;
187 long nX = 0, nY = 0, nRowHeight = 0;
188 for (it = maChildren.begin(); it != maChildren.end(); it++)
190 ChildData *child = *it;
191 if ( !child->isVisible() )
192 continue;
194 awt::Size aChildSize( child->aRequisition );
195 if ( mbHomogeneous )
196 aChildSize.Width = mnEachWidth;
198 if ( nX > 0 && nX + aChildSize.Width > rArea.Width )
200 nX = 0;
201 nY += nRowHeight;
202 nRowHeight = 0;
204 nRowHeight = SAL_MAX( nRowHeight, aChildSize.Height );
206 allocateChildAt( child->xChild,
207 awt::Rectangle( rArea.X + nX, rArea.Y + nY, aChildSize.Width, aChildSize.Height ) );
209 nX += aChildSize.Width;
213 } // namespace layoutimpl