xref: /AOO41X/main/toolkit/source/layout/core/flow.cxx (revision b0724fc6948542b2496e16ea247f985ee5987cfe)
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
10cdf0e10cSrcweir  *
11*b0724fc6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
20*b0724fc6SAndrew Rist  *************************************************************/
21*b0724fc6SAndrew Rist 
22*b0724fc6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "flow.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <sal/macros.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir namespace layoutimpl
29cdf0e10cSrcweir {
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace css;
32cdf0e10cSrcweir 
isVisible()33cdf0e10cSrcweir bool Flow::ChildData::isVisible()
34cdf0e10cSrcweir {
35cdf0e10cSrcweir     return xChild.is();
36cdf0e10cSrcweir }
37cdf0e10cSrcweir 
Flow()38cdf0e10cSrcweir Flow::Flow()
39cdf0e10cSrcweir     : Container()
40cdf0e10cSrcweir     , mnSpacing( 0 )
41cdf0e10cSrcweir     , mbHomogeneous( false )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     addProp( RTL_CONSTASCII_USTRINGPARAM( "Homogeneous" ),
44cdf0e10cSrcweir              ::getCppuType( static_cast< const sal_Bool* >( NULL ) ),
45cdf0e10cSrcweir              &mbHomogeneous );
46cdf0e10cSrcweir     addProp( RTL_CONSTASCII_USTRINGPARAM( "Spacing" ),
47cdf0e10cSrcweir              ::getCppuType( static_cast< const sal_Int32* >( NULL ) ),
48cdf0e10cSrcweir              &mnSpacing );
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir bool
emptyVisible()52cdf0e10cSrcweir Flow::emptyVisible ()
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     return true;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir void SAL_CALL
addChild(const uno::Reference<awt::XLayoutConstrains> & xChild)58cdf0e10cSrcweir Flow::addChild( const uno::Reference< awt::XLayoutConstrains >& xChild )
59cdf0e10cSrcweir     throw (uno::RuntimeException, css::awt::MaxChildrenException)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     if ( xChild.is() )
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         ChildData *pData = new ChildData();
64cdf0e10cSrcweir         pData->xChild = xChild;
65cdf0e10cSrcweir         maChildren.push_back( pData );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         setChildParent( xChild );
68cdf0e10cSrcweir         queueResize();
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir void SAL_CALL
removeChild(const css::uno::Reference<css::awt::XLayoutConstrains> & xChild)73cdf0e10cSrcweir Flow::removeChild( const css::uno::Reference< css::awt::XLayoutConstrains >& xChild )
74cdf0e10cSrcweir     throw (css::uno::RuntimeException)
75cdf0e10cSrcweir {
76cdf0e10cSrcweir     for ( std::list< ChildData * >::iterator it = maChildren.begin();
77cdf0e10cSrcweir           it != maChildren.end(); it++ )
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         if ( (*it)->xChild == xChild )
80cdf0e10cSrcweir         {
81cdf0e10cSrcweir             delete *it;
82cdf0e10cSrcweir             maChildren.erase( it );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             unsetChildParent( xChild );
85cdf0e10cSrcweir             queueResize();
86cdf0e10cSrcweir             break;
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir     }
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir css::uno::Sequence< css::uno::Reference < css::awt::XLayoutConstrains > > SAL_CALL
getChildren()92cdf0e10cSrcweir Flow::getChildren()
93cdf0e10cSrcweir     throw (css::uno::RuntimeException)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir     uno::Sequence< uno::Reference< awt::XLayoutConstrains > > children( maChildren.size() );
96cdf0e10cSrcweir     unsigned int i = 0;
97cdf0e10cSrcweir     for ( std::list< ChildData * >::iterator it = maChildren.begin();
98cdf0e10cSrcweir           it != maChildren.end(); it++, i++ )
99cdf0e10cSrcweir         children[i] = (*it)->xChild;
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     return children;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir uno::Reference< beans::XPropertySet > SAL_CALL
getChildProperties(const uno::Reference<awt::XLayoutConstrains> &)105cdf0e10cSrcweir Flow::getChildProperties( const uno::Reference< awt::XLayoutConstrains >& /*xChild*/ )
106cdf0e10cSrcweir     throw (uno::RuntimeException)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     return uno::Reference< beans::XPropertySet >();
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir css::awt::Size
calculateSize(long nMaxWidth)112cdf0e10cSrcweir Flow::calculateSize( long nMaxWidth )
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     long nNeedHeight = 0;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     std::list<ChildData *>::const_iterator it;
117cdf0e10cSrcweir     mnEachWidth = 0;
118cdf0e10cSrcweir     // first pass, for homogeneous property
119cdf0e10cSrcweir     for (it = maChildren.begin(); it != maChildren.end(); it++)
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir         if ( !(*it)->isVisible() )
122cdf0e10cSrcweir             continue;
123cdf0e10cSrcweir         (*it)->aRequisition = (*it)->xChild->getMinimumSize();
124cdf0e10cSrcweir         if ( mbHomogeneous )
125cdf0e10cSrcweir             mnEachWidth = SAL_MAX( mnEachWidth, (*it)->aRequisition.Width );
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir     long nRowWidth = 0, nRowHeight = 0;
129cdf0e10cSrcweir     for (it = maChildren.begin(); it != maChildren.end(); it++)
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         if ( !(*it)->isVisible() )
132cdf0e10cSrcweir             continue;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir         awt::Size aChildSize = (*it)->aRequisition;
135cdf0e10cSrcweir         if ( mbHomogeneous )
136cdf0e10cSrcweir             aChildSize.Width = mnEachWidth;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         if ( nMaxWidth && nRowWidth > 0 && nRowWidth + aChildSize.Width > nMaxWidth )
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             nRowWidth = 0;
141cdf0e10cSrcweir             nNeedHeight += nRowHeight;
142cdf0e10cSrcweir             nRowHeight = 0;
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir         nRowHeight = SAL_MAX( nRowHeight, aChildSize.Height );
145cdf0e10cSrcweir         nRowWidth += aChildSize.Width;
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir     nNeedHeight += nRowHeight;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     return awt::Size( nRowWidth, nNeedHeight );
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir awt::Size SAL_CALL
getMinimumSize()153cdf0e10cSrcweir Flow::getMinimumSize() throw(uno::RuntimeException)
154cdf0e10cSrcweir {
155cdf0e10cSrcweir     return maRequisition = calculateSize( 0 );
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir sal_Bool SAL_CALL
hasHeightForWidth()159cdf0e10cSrcweir Flow::hasHeightForWidth()
160cdf0e10cSrcweir     throw(css::uno::RuntimeException)
161cdf0e10cSrcweir {
162cdf0e10cSrcweir     return true;
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir sal_Int32 SAL_CALL
getHeightForWidth(sal_Int32 nWidth)166cdf0e10cSrcweir Flow::getHeightForWidth( sal_Int32 nWidth )
167cdf0e10cSrcweir     throw(css::uno::RuntimeException)
168cdf0e10cSrcweir {
169cdf0e10cSrcweir     return calculateSize( nWidth ).Height;
170cdf0e10cSrcweir }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir void SAL_CALL
allocateArea(const css::awt::Rectangle & rArea)173cdf0e10cSrcweir Flow::allocateArea( const css::awt::Rectangle &rArea )
174cdf0e10cSrcweir     throw (css::uno::RuntimeException)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     maAllocation = rArea;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     std::list<ChildData *>::const_iterator it;
179cdf0e10cSrcweir     long nX = 0, nY = 0, nRowHeight = 0;
180cdf0e10cSrcweir     for (it = maChildren.begin(); it != maChildren.end(); it++)
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir         ChildData *child = *it;
183cdf0e10cSrcweir         if ( !child->isVisible() )
184cdf0e10cSrcweir             continue;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir         awt::Size aChildSize( child->aRequisition );
187cdf0e10cSrcweir         if ( mbHomogeneous )
188cdf0e10cSrcweir             aChildSize.Width = mnEachWidth;
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         if ( nX > 0 && nX + aChildSize.Width > rArea.Width )
191cdf0e10cSrcweir         {
192cdf0e10cSrcweir             nX = 0;
193cdf0e10cSrcweir             nY += nRowHeight;
194cdf0e10cSrcweir             nRowHeight = 0;
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir         nRowHeight = SAL_MAX( nRowHeight, aChildSize.Height );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         allocateChildAt( child->xChild,
199cdf0e10cSrcweir                          awt::Rectangle( rArea.X + nX, rArea.Y + nY, aChildSize.Width, aChildSize.Height ) );
200cdf0e10cSrcweir 
201cdf0e10cSrcweir         nX += aChildSize.Width;
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir } // namespace layoutimpl
206