xref: /trunk/main/toolkit/source/layout/core/flow.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "flow.hxx"
25 
26 #include <sal/macros.h>
27 
28 namespace layoutimpl
29 {
30 
31 using namespace css;
32 
isVisible()33 bool Flow::ChildData::isVisible()
34 {
35     return xChild.is();
36 }
37 
Flow()38 Flow::Flow()
39     : Container()
40     , mnSpacing( 0 )
41     , mbHomogeneous( false )
42 {
43     addProp( RTL_CONSTASCII_USTRINGPARAM( "Homogeneous" ),
44              ::getCppuType( static_cast< const sal_Bool* >( NULL ) ),
45              &mbHomogeneous );
46     addProp( RTL_CONSTASCII_USTRINGPARAM( "Spacing" ),
47              ::getCppuType( static_cast< const sal_Int32* >( NULL ) ),
48              &mnSpacing );
49 }
50 
51 bool
emptyVisible()52 Flow::emptyVisible ()
53 {
54     return true;
55 }
56 
57 void SAL_CALL
addChild(const uno::Reference<awt::XLayoutConstrains> & xChild)58 Flow::addChild( const uno::Reference< awt::XLayoutConstrains >& xChild )
59 {
60     if ( xChild.is() )
61     {
62         ChildData *pData = new ChildData();
63         pData->xChild = xChild;
64         maChildren.push_back( pData );
65 
66         setChildParent( xChild );
67         queueResize();
68     }
69 }
70 
71 void SAL_CALL
removeChild(const css::uno::Reference<css::awt::XLayoutConstrains> & xChild)72 Flow::removeChild( const css::uno::Reference< css::awt::XLayoutConstrains >& xChild )
73 {
74     for ( std::list< ChildData * >::iterator it = maChildren.begin();
75           it != maChildren.end(); it++ )
76     {
77         if ( (*it)->xChild == xChild )
78         {
79             delete *it;
80             maChildren.erase( it );
81 
82             unsetChildParent( xChild );
83             queueResize();
84             break;
85         }
86     }
87 }
88 
89 css::uno::Sequence< css::uno::Reference < css::awt::XLayoutConstrains > > SAL_CALL
getChildren()90 Flow::getChildren()
91 {
92     uno::Sequence< uno::Reference< awt::XLayoutConstrains > > children( maChildren.size() );
93     unsigned int i = 0;
94     for ( std::list< ChildData * >::iterator it = maChildren.begin();
95           it != maChildren.end(); it++, i++ )
96         children[i] = (*it)->xChild;
97 
98     return children;
99 }
100 
101 uno::Reference< beans::XPropertySet > SAL_CALL
getChildProperties(const uno::Reference<awt::XLayoutConstrains> &)102 Flow::getChildProperties( const uno::Reference< awt::XLayoutConstrains >& /*xChild*/ )
103 {
104     return uno::Reference< beans::XPropertySet >();
105 }
106 
107 css::awt::Size
calculateSize(long nMaxWidth)108 Flow::calculateSize( long nMaxWidth )
109 {
110     long nNeedHeight = 0;
111 
112     std::list<ChildData *>::const_iterator it;
113     mnEachWidth = 0;
114     // first pass, for homogeneous property
115     for (it = maChildren.begin(); it != maChildren.end(); it++)
116     {
117         if ( !(*it)->isVisible() )
118             continue;
119         (*it)->aRequisition = (*it)->xChild->getMinimumSize();
120         if ( mbHomogeneous )
121             mnEachWidth = SAL_MAX( mnEachWidth, (*it)->aRequisition.Width );
122     }
123 
124     long nRowWidth = 0, nRowHeight = 0;
125     for (it = maChildren.begin(); it != maChildren.end(); it++)
126     {
127         if ( !(*it)->isVisible() )
128             continue;
129 
130         awt::Size aChildSize = (*it)->aRequisition;
131         if ( mbHomogeneous )
132             aChildSize.Width = mnEachWidth;
133 
134         if ( nMaxWidth && nRowWidth > 0 && nRowWidth + aChildSize.Width > nMaxWidth )
135         {
136             nRowWidth = 0;
137             nNeedHeight += nRowHeight;
138             nRowHeight = 0;
139         }
140         nRowHeight = SAL_MAX( nRowHeight, aChildSize.Height );
141         nRowWidth += aChildSize.Width;
142     }
143     nNeedHeight += nRowHeight;
144 
145     return awt::Size( nRowWidth, nNeedHeight );
146 }
147 
148 awt::Size SAL_CALL
getMinimumSize()149 Flow::getMinimumSize()
150 {
151     return maRequisition = calculateSize( 0 );
152 }
153 
154 sal_Bool SAL_CALL
hasHeightForWidth()155 Flow::hasHeightForWidth()
156 {
157     return true;
158 }
159 
160 sal_Int32 SAL_CALL
getHeightForWidth(sal_Int32 nWidth)161 Flow::getHeightForWidth( sal_Int32 nWidth )
162 {
163     return calculateSize( nWidth ).Height;
164 }
165 
166 void SAL_CALL
allocateArea(const css::awt::Rectangle & rArea)167 Flow::allocateArea( const css::awt::Rectangle &rArea )
168 {
169     maAllocation = rArea;
170 
171     std::list<ChildData *>::const_iterator it;
172     long nX = 0, nY = 0, nRowHeight = 0;
173     for (it = maChildren.begin(); it != maChildren.end(); it++)
174     {
175         ChildData *child = *it;
176         if ( !child->isVisible() )
177             continue;
178 
179         awt::Size aChildSize( child->aRequisition );
180         if ( mbHomogeneous )
181             aChildSize.Width = mnEachWidth;
182 
183         if ( nX > 0 && nX + aChildSize.Width > rArea.Width )
184         {
185             nX = 0;
186             nY += nRowHeight;
187             nRowHeight = 0;
188         }
189         nRowHeight = SAL_MAX( nRowHeight, aChildSize.Height );
190 
191         allocateChildAt( child->xChild,
192                          awt::Rectangle( rArea.X + nX, rArea.Y + nY, aChildSize.Width, aChildSize.Height ) );
193 
194         nX += aChildSize.Width;
195     }
196 }
197 
198 } // namespace layoutimpl
199