LibreOffice Module UnoControls (master) 1
basecontainercontrol.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21
24
25#include <com/sun/star/container/ContainerEvent.hpp>
26#include <com/sun/star/container/XContainerListener.hpp>
27#include <com/sun/star/awt/XControlContainer.hpp>
28
29// namespaces
30
31using namespace ::cppu;
32using namespace ::osl;
33using namespace ::com::sun::star::uno;
34using namespace ::com::sun::star::lang;
35using namespace ::com::sun::star::awt;
36using namespace ::com::sun::star::container;
37
38namespace unocontrols {
39
40// construct/destruct
41
42BaseContainerControl::BaseContainerControl( const Reference< XComponentContext >& rxContext )
43 : BaseControl ( rxContext )
45{
46}
47
49{
50}
51
52// XInterface
53
55{
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 )
61 )
62 );
63
64 // If searched interface supported by this class ...
65 if ( aReturn.hasValue() )
66 {
67 // ... return this information.
68 return aReturn;
69 }
70 else
71 {
72 // Else; ... ask baseclass for interfaces!
73 return BaseControl::queryInterface( rType );
74 }
75}
76
77// XTypeProvider
78
79Sequence< Type > SAL_CALL BaseContainerControl::getTypes()
80{
81 static OTypeCollection ourTypeCollection(
85
86 return ourTypeCollection.getTypes();
87}
88
89// XControl
90
91void SAL_CALL BaseContainerControl::createPeer( const Reference< XToolkit >& xToolkit ,
92 const Reference< XWindowPeer >& xParent )
93{
94 if ( getPeer().is() )
95 return;
96
97 // create own peer
98 BaseControl::createPeer( xToolkit, xParent );
99
100 // create peers at all children
101 Sequence< Reference< XControl > > seqControlList = getControls();
102
103 for ( auto& rxControl : asNonConstRange(seqControlList) )
104 {
105 rxControl->createPeer( xToolkit, getPeer() );
106 }
107}
108
109// XControl
110
111sal_Bool SAL_CALL BaseContainerControl::setModel( const Reference< XControlModel >& )
112{
113 // This object has NO model.
114 return false;
115}
116
117// XControl
118
119Reference< XControlModel > SAL_CALL BaseContainerControl::getModel()
120{
121 // This object has NO model.
122 // return (XControlModel*)this;
123 return Reference< XControlModel >();
124}
125
126// XComponent
127
129{
130 // Tell everything that this container is now gone.
131 // It's faster if you listen to both the control and the container.
132
133 // Ready for multithreading
134 MutexGuard aGuard( m_aMutex );
135
136 // remove listeners
137 EventObject aObject;
138
139 aObject.Source.set( static_cast<XControlContainer*>(this), UNO_QUERY );
140 m_aListeners.disposeAndClear( aObject );
141
142 // remove controls
143 const Sequence< Reference< XControl > > seqCtrls = getControls();
144
145 maControlInfoList.clear();
146
147 for ( Reference< XControl > const & control : seqCtrls )
148 {
149 control->removeEventListener ( static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) ) );
150 control->dispose ( );
151 }
152
153 // call baseclass
155}
156
157// XEventListener
158
159void SAL_CALL BaseContainerControl::disposing( const EventObject& rEvent )
160{
161 Reference< XControl > xControl( rEvent.Source, UNO_QUERY );
162
163 // "removeControl" remove only, when control is an active control
164 removeControl( xControl );
165}
166
167// XControlContainer
168
169void SAL_CALL BaseContainerControl::addControl ( const OUString& rName, const Reference< XControl > & rControl )
170{
171 if ( !rControl.is () )
172 return;
173
174 // take memory for new item
175 IMPL_ControlInfo aNewControl;
176
177 // Ready for multithreading
178 MutexGuard aGuard (m_aMutex);
179
180 // set control
181 aNewControl.sName = rName;
182 aNewControl.xControl = rControl;
183
184 // and insert in list
185 maControlInfoList.emplace_back( aNewControl );
186
187 // initialize new control
188 aNewControl.xControl->setContext (getXWeak());
189 aNewControl.xControl->addEventListener ( static_cast< XEventListener* >( static_cast< XWindowListener* >( this ) ) );
190
191 // when container has a peer...
192 if (getPeer().is())
193 {
194 // ... then create a peer on child
195 aNewControl.xControl->createPeer ( getPeer()->getToolkit(), getPeer() );
196 }
197
198 // Send message to all listener
200
201 if (!pInterfaceContainer)
202 return;
203
204 // Build event
205 ContainerEvent aEvent;
206
207 aEvent.Source = *this;
208 aEvent.Element <<= rControl;
209
210 // Get all listener
211 comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer);
212
213 // Send event
214 while ( aIterator.hasMoreElements() )
215 {
216 static_cast<XContainerListener*>(aIterator.next())->elementInserted (aEvent);
217 }
218}
219
220// XControlContainer
221
222void SAL_CALL BaseContainerControl::removeControl ( const Reference< XControl > & rControl )
223{
224 if ( !rControl.is() )
225 return;
226
227 // Ready for multithreading
228 MutexGuard aGuard (m_aMutex);
229
230 size_t nControls = maControlInfoList.size();
231
232 for ( size_t n = 0; n < nControls; n++ )
233 {
234 // Search for right control
235 IMPL_ControlInfo* pControl = &maControlInfoList[ n ];
236 if ( rControl == pControl->xControl )
237 {
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 > () );
241
242 // ... free memory
243 maControlInfoList.erase(maControlInfoList.begin() + n);
244
245 // Send message to all other listener
247
248 if (pInterfaceContainer)
249 {
250 ContainerEvent aEvent;
251
252 aEvent.Source = *this;
253 aEvent.Element <<= rControl;
254
255 comphelper::OInterfaceIteratorHelper2 aIterator (*pInterfaceContainer);
256
257 while ( aIterator.hasMoreElements() )
258 {
259 static_cast<XContainerListener*>(aIterator.next())->elementRemoved (aEvent);
260 }
261 }
262 // Break "for" !
263 break;
264 }
265 }
266}
267
268// XControlContainer
269
270void SAL_CALL BaseContainerControl::setStatusText ( const OUString& rStatusText )
271{
272 // go down to each parent
273 Reference< XControlContainer > xContainer ( getContext(), UNO_QUERY );
274
275 if ( xContainer.is () )
276 {
277 xContainer->setStatusText ( rStatusText );
278 }
279}
280
281// XControlContainer
282
283Reference< XControl > SAL_CALL BaseContainerControl::getControl ( const OUString& rName )
284{
285 // Ready for multithreading
286 MutexGuard aGuard ( Mutex::getGlobalMutex() );
287
288 // Search for right control
289 for( const IMPL_ControlInfo& rSearchControl : maControlInfoList )
290 {
291 if ( rSearchControl.sName == rName )
292 {
293 // We have found it ...
294 // Break operation and return.
295 return rSearchControl.xControl;
296 }
297 }
298
299 // We have not found it ... return NULL.
300 return Reference< XControl > ();
301}
302
303// XControlContainer
304
305Sequence< Reference< XControl > > SAL_CALL BaseContainerControl::getControls ()
306{
307 // Ready for multithreading
308 MutexGuard aGuard ( Mutex::getGlobalMutex() );
309
310 size_t nControls = maControlInfoList.size();
311 size_t nCount = 0;
312 Sequence< Reference< XControl > > aDescriptor ( nControls );
313 Reference< XControl > * pDestination = aDescriptor.getArray ();
314
315 // Copy controls to sequence
316 for( const IMPL_ControlInfo& rCopyControl : maControlInfoList )
317 {
318 pDestination [ nCount++ ] = rCopyControl.xControl;
319 }
320
321 // Return sequence
322 return aDescriptor;
323}
324
325// XWindow
326
328{
329 // override baseclass definition
331
332 // is it a top window ?
333 if ( !getContext().is() && bVisible )
334 {
335 // then show it automatically
336 createPeer ( Reference< XToolkit > (), Reference< XWindowPeer > () );
337 }
338}
339
340// protected method
341
342WindowDescriptor BaseContainerControl::impl_getWindowDescriptor ( const Reference< XWindowPeer > & rParentPeer )
343{
344 WindowDescriptor aDescriptor;
345
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;
352
353 return aDescriptor;
354}
355
356// protected method
357
358void BaseContainerControl::impl_paint ( sal_Int32 /*nX*/, sal_Int32 /*nY*/, const Reference< XGraphics > & /*rGraphics*/ )
359{
360}
361
362} // namespace unocontrols
363
364/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
css::uno::XInterface * next()
OInterfaceContainerHelper2 * getContainer(const css::uno::Type &rKey) const
void disposeAndClear(const css::lang::EventObject &rEvt)
mutable::osl::Mutex m_aMutex
virtual css::uno::Reference< css::awt::XControl > SAL_CALL getControl(const OUString &sName) override
virtual void SAL_CALL dispose() override
virtual void impl_paint(sal_Int32 nX, sal_Int32 nY, const css::uno::Reference< css::awt::XGraphics > &xGraphics) override
virtual css::awt::WindowDescriptor impl_getWindowDescriptor(const css::uno::Reference< css::awt::XWindowPeer > &xParentPeer) override
comphelper::OMultiTypeInterfaceContainerHelper2 m_aListeners
BaseContainerControl(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual css::uno::Reference< css::awt::XControlModel > SAL_CALL getModel() override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces @seealso XTypeProvider
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
give answer, if interface is supported @descr The interfaces are searched by type.
virtual void SAL_CALL disposing(const css::lang::EventObject &rEvent) override
virtual void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &xToolkit, const css::uno::Reference< css::awt::XWindowPeer > &xParent) override
virtual sal_Bool SAL_CALL setModel(const css::uno::Reference< css::awt::XControlModel > &xModel) override
virtual void SAL_CALL setVisible(sal_Bool bVisible) override
::std::vector< IMPL_ControlInfo > maControlInfoList
virtual void SAL_CALL setStatusText(const OUString &sStatusText) override
virtual css::uno::Sequence< css::uno::Reference< css::awt::XControl > > SAL_CALL getControls() override
virtual void SAL_CALL removeControl(const css::uno::Reference< css::awt::XControl > &xControl) override
virtual void SAL_CALL addControl(const OUString &sName, const css::uno::Reference< css::awt::XControl > &xControl) override
virtual void SAL_CALL createPeer(const css::uno::Reference< css::awt::XToolkit > &xToolkit, const css::uno::Reference< css::awt::XWindowPeer > &xParent) override
virtual void SAL_CALL setVisible(sal_Bool bVisible) override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
get information about supported interfaces @seealso XTypeProvider
virtual css::uno::Reference< css::awt::XWindowPeer > SAL_CALL getPeer() override
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) override
give answer, if interface is supported @descr The interfaces are searched by type.
Definition: basecontrol.cxx:70
virtual void SAL_CALL dispose() override
virtual css::awt::Rectangle SAL_CALL getPosSize() override
virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getContext() override
int nCount
std::vector< Reference< css::datatransfer::clipboard::XClipboardListener > > m_aListeners
std::mutex m_aMutex
sal_Int64 n
void SAL_CALL elementRemoved(const css::container::ContainerEvent &Event) override
DECL_LISTENERMULTIPLEXER_END void SAL_CALL elementInserted(const css::container::ContainerEvent &Event) override
Type
css::uno::Reference< css::awt::XControl > xControl
bool bVisible
unsigned char sal_Bool