Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / UnoControls / source / base / basecontainercontrol.cxx
blobbf638a9c56a4075a2c2508af5630ef7ac310ee59
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <basecontainercontrol.hxx>
22 #include <cppuhelper/queryinterface.hxx>
23 #include <cppuhelper/typeprovider.hxx>
25 #include <com/sun/star/container/ContainerEvent.hpp>
26 #include <com/sun/star/container/XContainerListener.hpp>
27 #include <com/sun/star/awt/XControlContainer.hpp>
29 // namespaces
31 using namespace ::cppu;
32 using namespace ::osl;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::awt;
36 using namespace ::com::sun::star::container;
38 namespace unocontrols {
40 // construct/destruct
42 BaseContainerControl::BaseContainerControl( const Reference< XComponentContext >& rxContext )
43 : BaseControl ( rxContext )
44 , m_aListeners ( m_aMutex )
48 BaseContainerControl::~BaseContainerControl()
52 // XInterface
54 Any SAL_CALL BaseContainerControl::queryInterface( const Type& rType )
56 // Ask for my own supported interfaces ...
57 // Attention: XTypeProvider and XInterface are supported by WeakComponentImplHelper!
58 Any aReturn ( ::cppu::queryInterface( rType ,
59 static_cast< XControlModel* > ( this ) ,
60 static_cast< XControlContainer* > ( this )
64 // If searched interface supported by this class ...
65 if ( aReturn.hasValue() )
67 // ... return this information.
68 return aReturn;
70 else
72 // Else; ... ask baseclass for interfaces!
73 return BaseControl::queryInterface( rType );
77 // XTypeProvider
79 Sequence< Type > SAL_CALL BaseContainerControl::getTypes()
81 static OTypeCollection ourTypeCollection(
82 cppu::UnoType<XControlModel>::get(),
83 cppu::UnoType<XControlContainer>::get(),
84 BaseControl::getTypes() );
86 return ourTypeCollection.getTypes();
89 // XControl
91 void SAL_CALL BaseContainerControl::createPeer( const Reference< XToolkit >& xToolkit ,
92 const Reference< XWindowPeer >& xParent )
94 if ( getPeer().is() )
95 return;
97 // create own peer
98 BaseControl::createPeer( xToolkit, xParent );
100 // create peers at all children
101 Sequence< Reference< XControl > > seqControlList = getControls();
103 for ( auto& rxControl : asNonConstRange(seqControlList) )
105 rxControl->createPeer( xToolkit, getPeer() );
109 // XControl
111 sal_Bool SAL_CALL BaseContainerControl::setModel( const Reference< XControlModel >& )
113 // This object has NO model.
114 return false;
117 // XControl
119 Reference< XControlModel > SAL_CALL BaseContainerControl::getModel()
121 // This object has NO model.
122 // return (XControlModel*)this;
123 return Reference< XControlModel >();
126 // XComponent
128 void SAL_CALL BaseContainerControl::dispose()
130 // Tell everything that this container is now gone.
131 // It's faster if you listen to both the control and the container.
133 // Ready for multithreading
134 MutexGuard aGuard( m_aMutex );
136 // remove listeners
137 EventObject aObject;
139 aObject.Source.set( static_cast<XControlContainer*>(this), UNO_QUERY );
140 m_aListeners.disposeAndClear( aObject );
142 // remove controls
143 const Sequence< Reference< XControl > > seqCtrls = getControls();
145 maControlInfoList.clear();
147 for ( Reference< XControl > const & control : seqCtrls )
149 control->removeEventListener ( static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) ) );
150 control->dispose ( );
153 // call baseclass
154 BaseControl::dispose();
157 // XEventListener
159 void SAL_CALL BaseContainerControl::disposing( const EventObject& rEvent )
161 Reference< XControl > xControl( rEvent.Source, UNO_QUERY );
163 // "removeControl" remove only, when control is an active control
164 removeControl( xControl );
167 // XControlContainer
169 void SAL_CALL BaseContainerControl::addControl ( const OUString& rName, const Reference< XControl > & rControl )
171 if ( !rControl.is () )
172 return;
174 // take memory for new item
175 IMPL_ControlInfo aNewControl;
177 // Ready for multithreading
178 MutexGuard aGuard (m_aMutex);
180 // set control
181 aNewControl.sName = rName;
182 aNewControl.xControl = rControl;
184 // and insert in list
185 maControlInfoList.emplace_back( aNewControl );
187 // initialize new control
188 aNewControl.xControl->setContext (getXWeak());
189 aNewControl.xControl->addEventListener ( static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) ) );
191 // when container has a peer...
192 if (getPeer().is())
194 // ... then create a peer on child
195 aNewControl.xControl->createPeer ( getPeer()->getToolkit(), getPeer() );
198 // Send message to all listener
199 comphelper::OInterfaceContainerHelper2* pInterfaceContainer = m_aListeners.getContainer( cppu::UnoType<XContainerListener>::get());
201 if (!pInterfaceContainer)
202 return;
204 // Build event
205 ContainerEvent aEvent;
207 aEvent.Source = *this;
208 aEvent.Element <<= rControl;
210 // Get all listener
211 comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer);
213 // Send event
214 while ( aIterator.hasMoreElements() )
216 static_cast<XContainerListener*>(aIterator.next())->elementInserted (aEvent);
220 // XControlContainer
222 void SAL_CALL BaseContainerControl::removeControl ( const Reference< XControl > & rControl )
224 if ( !rControl.is() )
225 return;
227 // Ready for multithreading
228 MutexGuard aGuard (m_aMutex);
230 size_t nControls = maControlInfoList.size();
232 for ( size_t n = 0; n < nControls; n++ )
234 // Search for right control
235 IMPL_ControlInfo* pControl = &maControlInfoList[ n ];
236 if ( rControl == pControl->xControl )
238 //.is it found ... remove listener from control
239 pControl->xControl->removeEventListener (static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) ));
240 pControl->xControl->setContext ( Reference< XInterface > () );
242 // ... free memory
243 maControlInfoList.erase(maControlInfoList.begin() + n);
245 // Send message to all other listener
246 comphelper::OInterfaceContainerHelper2 * pInterfaceContainer = m_aListeners.getContainer( cppu::UnoType<XContainerListener>::get());
248 if (pInterfaceContainer)
250 ContainerEvent aEvent;
252 aEvent.Source = *this;
253 aEvent.Element <<= rControl;
255 comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer);
257 while ( aIterator.hasMoreElements() )
259 static_cast<XContainerListener*>(aIterator.next())->elementRemoved (aEvent);
262 // Break "for" !
263 break;
268 // XControlContainer
270 void SAL_CALL BaseContainerControl::setStatusText ( const OUString& rStatusText )
272 // go down to each parent
273 Reference< XControlContainer > xContainer ( getContext(), UNO_QUERY );
275 if ( xContainer.is () )
277 xContainer->setStatusText ( rStatusText );
281 // XControlContainer
283 Reference< XControl > SAL_CALL BaseContainerControl::getControl ( const OUString& rName )
285 // Ready for multithreading
286 MutexGuard aGuard ( Mutex::getGlobalMutex() );
288 // Search for right control
289 for( const IMPL_ControlInfo& rSearchControl : maControlInfoList )
291 if ( rSearchControl.sName == rName )
293 // We have found it ...
294 // Break operation and return.
295 return rSearchControl.xControl;
299 // We have not found it ... return NULL.
300 return Reference< XControl > ();
303 // XControlContainer
305 Sequence< Reference< XControl > > SAL_CALL BaseContainerControl::getControls ()
307 // Ready for multithreading
308 MutexGuard aGuard ( Mutex::getGlobalMutex() );
310 size_t nControls = maControlInfoList.size();
311 size_t nCount = 0;
312 Sequence< Reference< XControl > > aDescriptor ( nControls );
313 Reference< XControl > * pDestination = aDescriptor.getArray ();
315 // Copy controls to sequence
316 for( const IMPL_ControlInfo& rCopyControl : maControlInfoList )
318 pDestination [ nCount++ ] = rCopyControl.xControl;
321 // Return sequence
322 return aDescriptor;
325 // XWindow
327 void SAL_CALL BaseContainerControl::setVisible ( sal_Bool bVisible )
329 // override baseclass definition
330 BaseControl::setVisible ( bVisible );
332 // is it a top window ?
333 if ( !getContext().is() && bVisible )
335 // then show it automatically
336 createPeer ( Reference< XToolkit > (), Reference< XWindowPeer > () );
340 // protected method
342 WindowDescriptor BaseContainerControl::impl_getWindowDescriptor ( const Reference< XWindowPeer > & rParentPeer )
344 WindowDescriptor aDescriptor;
346 aDescriptor.Type = WindowClass_CONTAINER;
347 aDescriptor.WindowServiceName = "window";
348 aDescriptor.ParentIndex = -1;
349 aDescriptor.Parent = rParentPeer;
350 aDescriptor.Bounds = getPosSize ();
351 aDescriptor.WindowAttributes = 0;
353 return aDescriptor;
356 // protected method
358 void BaseContainerControl::impl_paint ( sal_Int32 /*nX*/, sal_Int32 /*nY*/, const Reference< XGraphics > & /*rGraphics*/ )
362 } // namespace unocontrols
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */