1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "wrapper.hxx"
29 
30 #include <com/sun/star/awt/XLayoutRoot.hpp>
31 #include <com/sun/star/awt/XLayoutContainer.hpp>
32 #include <comphelper/processfactory.hxx>
33 #include <layout/core/helper.hxx>
34 #include <tools/debug.hxx>
35 
36 using namespace ::com::sun::star;
37 
38 namespace layout
39 {
40 
41 Container::Container( Context const* context, char const* pId )
42     : mxContainer( context->GetPeerHandle( pId ), uno::UNO_QUERY )
43 {
44     if ( !mxContainer.is() )
45     {
46         DBG_ERROR1( "Error: failed to associate container with '%s'", pId );
47     }
48 }
49 
50 Container::Container( rtl::OUString const& rName, sal_Int32 nBorder )
51 {
52     mxContainer = layoutimpl::WidgetFactory::createContainer( rName );
53 
54     uno::Reference< beans::XPropertySet > xProps( mxContainer, uno::UNO_QUERY_THROW );
55     xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Border" ) ),
56                               uno::Any( nBorder ) );
57 }
58 
59 void Container::Add( Window *pChild )
60 {
61     if ( pChild )
62     {
63         uno::Reference< awt::XLayoutConstrains > xChild( pChild->GetPeer(), uno::UNO_QUERY );
64         mxContainer->addChild( xChild );
65     }
66 }
67 
68 void Container::Add( Container *pChild )
69 {
70     if ( pChild )
71     {
72         uno::Reference< awt::XLayoutConstrains > xChild( pChild->getImpl(), uno::UNO_QUERY );
73         mxContainer->addChild( xChild );
74     }
75 }
76 
77 void Container::Remove( Window *pChild )
78 {
79     if ( pChild )
80     {
81         uno::Reference< awt::XLayoutConstrains > xChild( pChild->GetPeer(), uno::UNO_QUERY );
82         mxContainer->removeChild( xChild );
83     }
84 }
85 
86 void Container::Remove( Container *pChild )
87 {
88     if ( pChild )
89     {
90         uno::Reference< awt::XLayoutConstrains > xChild( pChild->getImpl(), uno::UNO_QUERY );
91         mxContainer->removeChild( xChild );
92     }
93 }
94 
95 void Container::Clear()
96 {
97     css::uno::Sequence< css::uno::Reference < css::awt::XLayoutConstrains > > children;
98     children = mxContainer->getChildren();
99     for (int i = 0; i < children.getLength(); i++)
100         mxContainer->removeChild( children[i] );
101 }
102 
103 void Container::ShowAll( bool bShow )
104 {
105     struct inner
106     {
107         static void setChildrenVisible( uno::Reference < awt::XLayoutContainer > xCont,
108                                         bool bVisible )  /* auxiliary */
109         {
110             if ( xCont.is() )
111             {
112                 uno::Sequence< uno::Reference < awt::XLayoutConstrains > > aChildren;
113                 aChildren = xCont->getChildren();
114                 for (int i = 0; i < aChildren.getLength(); i++)
115                 {
116                     uno::Reference < awt::XLayoutConstrains > xChild( aChildren[ i ] );
117 
118                     uno::Reference< awt::XWindow > xWin( xChild, uno::UNO_QUERY);
119                     if ( xWin.is() )
120                         xWin->setVisible( bVisible );
121 
122                     uno::Reference < awt::XLayoutContainer > xChildCont(
123                         xChild, uno::UNO_QUERY );
124                     setChildrenVisible( xChildCont, bVisible );
125                 }
126             }
127         }
128     };
129 
130     inner::setChildrenVisible( mxContainer, bShow );
131 }
132 
133 void Container::Show()
134 {
135     ShowAll( true );
136 }
137 
138 void Container::Hide()
139 {
140     ShowAll( false );
141 }
142 
143 Table::Table( sal_Int32 nBorder, sal_Int32 nColumns )
144     : Container( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "table" ) ), nBorder )
145 {
146     uno::Reference< beans::XPropertySet > xProps( mxContainer, uno::UNO_QUERY_THROW );
147     xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Columns" ) ),
148                               uno::Any( nColumns ) );
149 }
150 
151 void Table::Add( Window *window, bool bXExpand, bool bYExpand,
152                  sal_Int32 nXSpan, sal_Int32 nYSpan )
153 {
154     if ( !window )
155         return;
156     WindowImpl &rImpl = window->getImpl();
157     uno::Reference< awt::XLayoutConstrains > xChild( rImpl.mxWindow,
158                                                      uno::UNO_QUERY );
159     mxContainer->addChild( xChild );
160     setProps( xChild, bXExpand, bYExpand, nXSpan, nYSpan );
161 }
162 
163 void Table::Add( Container *pContainer, bool bXExpand, bool bYExpand,
164                  sal_Int32 nXSpan, sal_Int32 nYSpan )
165 {
166     if ( !pContainer )
167         return;
168     uno::Reference< awt::XLayoutConstrains > xChild( pContainer->getImpl(),
169                                                      uno::UNO_QUERY );
170     mxContainer->addChild( xChild );
171     setProps( xChild, bXExpand, bYExpand, nXSpan, nYSpan );
172 }
173 
174 void Table::setProps( uno::Reference< awt::XLayoutConstrains > xChild,
175                       bool bXExpand, bool bYExpand, sal_Int32 nXSpan, sal_Int32 nYSpan )
176 {
177     uno::Reference< beans::XPropertySet > xProps
178         ( mxContainer->getChildProperties( xChild ), uno::UNO_QUERY_THROW );
179     xProps->setPropertyValue( rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM( "XExpand" ) ),
180                               uno::Any( bXExpand ) );
181     xProps->setPropertyValue( rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM( "YExpand" ) ),
182                               uno::Any( bYExpand ) );
183     xProps->setPropertyValue( rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM( "ColSpan" ) ),
184                               uno::Any( nXSpan ) );
185     xProps->setPropertyValue( rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM( "RowSpan" ) ),
186                               uno::Any( nYSpan ) );
187 }
188 
189 Box::Box( rtl::OUString const& rName, sal_Int32 nBorder, bool bHomogeneous )
190     : Container( rName, nBorder )
191 {
192     uno::Reference< beans::XPropertySet > xProps( mxContainer, uno::UNO_QUERY_THROW );
193     xProps->setPropertyValue( rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM( "Homogeneous" ) ),
194                               uno::Any( bHomogeneous ) );
195 }
196 
197 void Box::Add( Window *window, bool bExpand, bool bFill, sal_Int32 nPadding)
198 {
199     if ( !window )
200         return;
201     WindowImpl &rImpl = window->getImpl();
202     uno::Reference< awt::XLayoutConstrains > xChild( rImpl.mxWindow,
203                                                      uno::UNO_QUERY );
204 
205     mxContainer->addChild( xChild );
206     setProps( xChild, bExpand, bFill, nPadding );
207 }
208 
209 void Box::Add( Container *pContainer, bool bExpand, bool bFill, sal_Int32 nPadding)
210 {
211     if ( !pContainer )
212         return;
213 
214     uno::Reference< awt::XLayoutConstrains > xChild( pContainer->getImpl(),
215                                                      uno::UNO_QUERY );
216     mxContainer->addChild( xChild );
217     setProps( xChild, bExpand, bFill, nPadding );
218 }
219 
220 void Box::setProps( uno::Reference< awt::XLayoutConstrains > xChild,
221                     bool bExpand, bool bFill, sal_Int32 nPadding )
222 {
223     uno::Reference< beans::XPropertySet > xProps
224         ( mxContainer->getChildProperties( xChild ), uno::UNO_QUERY_THROW );
225 
226     xProps->setPropertyValue( rtl::OUString ( RTL_CONSTASCII_USTRINGPARAM( "Expand" ) ),
227                               uno::Any( bExpand ) );
228     xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Fill" ) ),
229                               uno::Any( bFill ) );
230     xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Padding" ) ),
231                               uno::Any( nPadding ) );
232 }
233 
234 Table::Table( Context const* context, char const* pId )
235     : Container( context, pId )
236 {
237 }
238 
239 Box::Box( Context const* context, char const* pId )
240     : Container( context, pId )
241 {
242 }
243 
244 HBox::HBox( sal_Int32 nBorder, bool bHomogeneous )
245     : Box( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "hbox" ) ),
246            nBorder, bHomogeneous )
247 {
248 }
249 
250 HBox::HBox( Context const* context, char const* pId )
251     : Box( context, pId )
252 {
253 }
254 
255 VBox::VBox( sal_Int32 nBorder, bool bHomogeneous )
256     : Box( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vbox" ) ),
257            nBorder, bHomogeneous )
258 {
259 }
260 
261 VBox::VBox( Context const* context, char const* pId )
262     : Box( context, pId )
263 {
264 }
265 
266 } // namespace layout
267