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 "timer.hxx"
25cdf0e10cSrcweir
26cdf0e10cSrcweir #include <vector>
27cdf0e10cSrcweir #include <list>
28cdf0e10cSrcweir #include <vcl/timer.hxx>
29cdf0e10cSrcweir #include <com/sun/star/awt/XLayoutContainer.hpp>
30cdf0e10cSrcweir
31cdf0e10cSrcweir namespace layoutimpl
32cdf0e10cSrcweir {
33cdf0e10cSrcweir using namespace ::com::sun::star;
34cdf0e10cSrcweir
35cdf0e10cSrcweir class AllocateTimer : public Timer
36cdf0e10cSrcweir {
37cdf0e10cSrcweir typedef std::list< uno::Reference< awt::XLayoutContainer > > ContainerList;
38cdf0e10cSrcweir ContainerList mxContainers;
39cdf0e10cSrcweir uno::Reference< awt::XLayoutContainer > mxLastAdded;
40cdf0e10cSrcweir
41cdf0e10cSrcweir public:
AllocateTimer()42cdf0e10cSrcweir AllocateTimer()
43cdf0e10cSrcweir {
44cdf0e10cSrcweir // timer set to 0 -- just process it as soon as it gets idle
45cdf0e10cSrcweir SetTimeout( 0 );
46cdf0e10cSrcweir }
47cdf0e10cSrcweir
isParentOf(uno::Reference<awt::XLayoutContainer> xParent,uno::Reference<awt::XLayoutContainer> xWidget)48cdf0e10cSrcweir static inline bool isParentOf( uno::Reference< awt::XLayoutContainer > xParent,
49cdf0e10cSrcweir uno::Reference< awt::XLayoutContainer > xWidget )
50cdf0e10cSrcweir {
51cdf0e10cSrcweir while ( xWidget.is() )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir if ( xWidget == xParent )
54cdf0e10cSrcweir return true;
55cdf0e10cSrcweir xWidget = uno::Reference< awt::XLayoutContainer >( xWidget->getParent(), uno::UNO_QUERY );
56cdf0e10cSrcweir }
57cdf0e10cSrcweir return false;
58cdf0e10cSrcweir }
59cdf0e10cSrcweir
eraseChildren(ContainerList::iterator & it,ContainerList & list)60cdf0e10cSrcweir static inline void eraseChildren( ContainerList::iterator &it, ContainerList &list )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir ContainerList::iterator jt = list.begin();
63cdf0e10cSrcweir while ( jt != list.end() )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir if ( it != jt && isParentOf( *it, *jt ) )
66cdf0e10cSrcweir jt = list.erase( jt );
67cdf0e10cSrcweir else
68cdf0e10cSrcweir jt++;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir }
71cdf0e10cSrcweir
isContainerDamaged(uno::Reference<awt::XLayoutContainer> xContainer)72cdf0e10cSrcweir static inline bool isContainerDamaged( uno::Reference< awt::XLayoutContainer > xContainer )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir uno::Reference< awt::XLayoutConstrains > xConstrains( xContainer, uno::UNO_QUERY );
75cdf0e10cSrcweir awt::Size lastReq( xContainer->getRequestedSize() );
76cdf0e10cSrcweir awt::Size curReq( xConstrains->getMinimumSize() );
77cdf0e10cSrcweir return lastReq.Width != curReq.Width || lastReq.Height != curReq.Height;
78cdf0e10cSrcweir }
79cdf0e10cSrcweir
add(const uno::Reference<awt::XLayoutContainer> & xContainer)80cdf0e10cSrcweir void add( const uno::Reference< awt::XLayoutContainer > &xContainer )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir // small optimization
83cdf0e10cSrcweir if ( mxLastAdded == xContainer )
84cdf0e10cSrcweir return;
85cdf0e10cSrcweir mxLastAdded = xContainer;
86cdf0e10cSrcweir
87cdf0e10cSrcweir mxContainers.push_back( xContainer );
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
Timeout()90cdf0e10cSrcweir virtual void Timeout()
91cdf0e10cSrcweir {
92cdf0e10cSrcweir mxLastAdded = uno::Reference< awt::XLayoutContainer >();
93cdf0e10cSrcweir
94cdf0e10cSrcweir // 1. remove duplications and children
95cdf0e10cSrcweir for ( ContainerList::iterator it = mxContainers.begin();
96cdf0e10cSrcweir it != mxContainers.end(); it++ )
97cdf0e10cSrcweir eraseChildren( it, mxContainers );
98cdf0e10cSrcweir
99cdf0e10cSrcweir // 2. check damage extent
100cdf0e10cSrcweir for ( ContainerList::iterator it = mxContainers.begin();
101cdf0e10cSrcweir it != mxContainers.end(); it++ )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir uno::Reference< awt::XLayoutContainer > xContainer = *it;
104cdf0e10cSrcweir while ( xContainer->getParent().is() && isContainerDamaged( xContainer ) )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir xContainer = uno::Reference< awt::XLayoutContainer >(
107cdf0e10cSrcweir xContainer->getParent(), uno::UNO_QUERY );
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
110cdf0e10cSrcweir if ( *it != xContainer )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir // 2.2 replace it with parent
113cdf0e10cSrcweir *it = xContainer;
114cdf0e10cSrcweir
115cdf0e10cSrcweir // 2.3 remove children of new parent
116cdf0e10cSrcweir eraseChildren( it, mxContainers );
117cdf0e10cSrcweir }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir // 3. force re-calculations
121cdf0e10cSrcweir for ( ContainerList::iterator it = mxContainers.begin();
122cdf0e10cSrcweir it != mxContainers.end(); it++ )
123cdf0e10cSrcweir (*it)->allocateArea( (*it)->getAllocatedArea() );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir };
126cdf0e10cSrcweir
AddResizeTimeout(const uno::Reference<awt::XLayoutContainer> & xCont)127cdf0e10cSrcweir static void AddResizeTimeout( const uno::Reference< awt::XLayoutContainer > &xCont )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir static AllocateTimer timer;
130cdf0e10cSrcweir timer.add( xCont );
131cdf0e10cSrcweir timer.Start();
132cdf0e10cSrcweir }
133cdf0e10cSrcweir
LayoutUnit()134cdf0e10cSrcweir LayoutUnit::LayoutUnit() : LayoutUnit_Base()
135cdf0e10cSrcweir {
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
queueResize(const uno::Reference<awt::XLayoutContainer> & xContainer)138cdf0e10cSrcweir void SAL_CALL LayoutUnit::queueResize( const uno::Reference< awt::XLayoutContainer > &xContainer )
139cdf0e10cSrcweir throw( uno::RuntimeException )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir AddResizeTimeout( xContainer );
142cdf0e10cSrcweir }
143cdf0e10cSrcweir
144cdf0e10cSrcweir }
145