1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/uno/XComponentContext.hpp>
22 #include <com/sun/star/awt/XVclContainerPeer.hpp>
24 #include <toolkit/controls/stdtabcontroller.hxx>
25 #include <toolkit/controls/stdtabcontrollermodel.hxx>
26 #include <toolkit/awt/vclxwindow.hxx>
27 #include <toolkit/helper/macros.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <cppuhelper/typeprovider.hxx>
30 #include <cppuhelper/queryinterface.hxx>
33 #include <tools/debug.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/window.hxx>
36 #include <comphelper/sequence.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::awt
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::beans
;
45 // class StdTabController
47 StdTabController::StdTabController()
51 StdTabController::~StdTabController()
55 bool StdTabController::ImplCreateComponentSequence(
56 Sequence
< Reference
< XControl
> >& rControls
,
57 const Sequence
< Reference
< XControlModel
> >& rModels
,
58 Sequence
< Reference
< XWindow
> >& rComponents
,
59 Sequence
< Any
>* pTabStops
,
64 // Get only the requested controls
65 sal_Int32 nModels
= rModels
.getLength();
66 if (nModels
!= rControls
.getLength())
68 Sequence
< Reference
< XControl
> > aSeq( nModels
);
69 const Reference
< XControlModel
>* pModels
= rModels
.getConstArray();
70 Reference
< XControl
> xCurrentControl
;
72 sal_Int32 nRealControls
= 0;
73 for (sal_Int32 n
= 0; n
< nModels
; ++n
, ++pModels
)
75 xCurrentControl
= FindControl(rControls
, *pModels
);
76 if (xCurrentControl
.is())
77 aSeq
.getArray()[nRealControls
++] = xCurrentControl
;
79 aSeq
.realloc(nRealControls
);
83 DBG_ASSERT( rControls
.getLength() <= rModels
.getLength(), "StdTabController:ImplCreateComponentSequence: inconsistence!" );
84 // there may be less controls than models, but never more controls than models
88 const Reference
< XControl
> * pControls
= rControls
.getConstArray();
89 sal_uInt32 nCtrls
= rControls
.getLength();
90 rComponents
.realloc( nCtrls
);
91 Reference
< XWindow
> * pComps
= rComponents
.getArray();
97 *pTabStops
= Sequence
< Any
>( nCtrls
);
98 pTabs
= pTabStops
->getArray();
101 for ( sal_uInt32 n
= 0; bOK
&& ( n
< nCtrls
); n
++ )
103 // Get the matching control for this model
104 Reference
< XControl
> xCtrl(pControls
[n
]);
108 pComps
[n
].set(xCtrl
->getPeer(), UNO_QUERY
);
110 pComps
[n
].set(xCtrl
, UNO_QUERY
);
115 // opt: Constant String for TabStop name
116 static const char aTabStopName
[] = "Tabstop";
118 Reference
< XPropertySet
> xPSet( xCtrl
->getModel(), UNO_QUERY
);
119 Reference
< XPropertySetInfo
> xInfo
= xPSet
->getPropertySetInfo();
120 if( xInfo
->hasPropertyByName( aTabStopName
) )
121 *pTabs
++ = xPSet
->getPropertyValue( aTabStopName
);
128 SAL_WARN("toolkit", "Control not found" );
135 void StdTabController::ImplActivateControl( bool bFirst
) const
137 // HACK due to bug #53688#, map controls onto an interface if remote controls may occur
138 Reference
< XTabController
> xTabController(const_cast< ::cppu::OWeakObject
* >(static_cast< const ::cppu::OWeakObject
* >(this)), UNO_QUERY
);
139 Sequence
< Reference
< XControl
> > aCtrls
= xTabController
->getControls();
140 const Reference
< XControl
> * pControls
= aCtrls
.getConstArray();
141 sal_uInt32 nCount
= aCtrls
.getLength();
143 for ( sal_uInt32 n
= bFirst
? 0 : nCount
; bFirst
? n
< nCount
: n
!= 0; )
145 sal_uInt32 nCtrl
= bFirst
? n
++ : --n
;
146 DBG_ASSERT( pControls
[nCtrl
].is(), "Control not in Container!" );
147 if ( pControls
[nCtrl
].is() )
149 Reference
< XWindowPeer
> xCP
= pControls
[nCtrl
]->getPeer();
152 VCLXWindow
* pC
= VCLXWindow::GetImplementation( xCP
);
153 if ( pC
&& pC
->GetWindow() && ( pC
->GetWindow()->GetStyle() & WB_TABSTOP
) )
155 pC
->GetWindow()->GrabFocus();
164 Any
StdTabController::queryAggregation( const Type
& rType
)
166 Any aRet
= ::cppu::queryInterface( rType
,
167 static_cast< XTabController
* >(this),
168 static_cast< XServiceInfo
* >(this),
169 static_cast< XTypeProvider
* >(this) );
170 return (aRet
.hasValue() ? aRet
: OWeakAggObject::queryAggregation( rType
));
174 IMPL_XTYPEPROVIDER_START( StdTabController
)
175 cppu::UnoType
<XTabController
>::get(),
176 cppu::UnoType
<XServiceInfo
>::get()
177 IMPL_XTYPEPROVIDER_END
179 void StdTabController::setModel( const Reference
< XTabControllerModel
>& Model
)
181 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
186 Reference
< XTabControllerModel
> StdTabController::getModel( )
188 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
193 void StdTabController::setContainer( const Reference
< XControlContainer
>& Container
)
195 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
197 mxControlContainer
= Container
;
200 Reference
< XControlContainer
> StdTabController::getContainer( )
202 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
204 return mxControlContainer
;
207 Sequence
< Reference
< XControl
> > StdTabController::getControls( )
209 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
211 Sequence
< Reference
< XControl
> > aSeq
;
213 if ( mxControlContainer
.is() )
215 Sequence
< Reference
< XControlModel
> > aModels
= mxModel
->getControlModels();
216 const Reference
< XControlModel
> * pModels
= aModels
.getConstArray();
218 Sequence
< Reference
< XControl
> > xCtrls
= mxControlContainer
->getControls();
220 sal_uInt32 nCtrls
= aModels
.getLength();
221 aSeq
= Sequence
< Reference
< XControl
> >( nCtrls
);
222 for ( sal_uInt32 n
= 0; n
< nCtrls
; n
++ )
224 Reference
< XControlModel
> xCtrlModel
= pModels
[n
];
225 // Search matching Control for this Model
226 Reference
< XControl
> xCtrl
= FindControl( xCtrls
, xCtrlModel
);
227 aSeq
.getArray()[n
] = xCtrl
;
233 void StdTabController::autoTabOrder( )
235 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
237 DBG_ASSERT( mxControlContainer
.is(), "autoTabOrder: No ControlContainer!" );
238 if ( !mxControlContainer
.is() )
241 Sequence
< Reference
< XControlModel
> > aSeq
= mxModel
->getControlModels();
242 Sequence
< Reference
< XWindow
> > aCompSeq
;
244 // This may return a TabController, which returns desired list of controls faster
245 Reference
< XTabController
> xTabController(static_cast< ::cppu::OWeakObject
* >(this), UNO_QUERY
);
246 Sequence
< Reference
< XControl
> > aControls
= xTabController
->getControls();
248 // #58317# Some Models may be missing from the Container. Plus there is a
249 // autoTabOrder call later on.
250 if( !ImplCreateComponentSequence( aControls
, aSeq
, aCompSeq
, nullptr, false ) )
253 sal_uInt32 nCtrls
= aCompSeq
.getLength();
254 Reference
< XWindow
> * pComponents
= aCompSeq
.getArray();
256 // insert sort algorithm
257 ComponentEntryList aCtrls
;
259 for ( n
= 0; n
< nCtrls
; n
++ )
261 XWindow
* pC
= pComponents
[n
].get();
262 ComponentEntry
* pE
= new ComponentEntry
;
264 awt::Rectangle aPosSize
= pC
->getPosSize();
265 pE
->aPos
.setX( aPosSize
.X
);
266 pE
->aPos
.setY( aPosSize
.Y
);
268 ComponentEntryList::size_type nPos
;
269 for ( nPos
= 0; nPos
< aCtrls
.size(); nPos
++ )
271 ComponentEntry
* pEntry
= aCtrls
[ nPos
];
272 if ( ( pEntry
->aPos
.Y() > pE
->aPos
.Y() ) ||
273 ( ( pEntry
->aPos
.Y() == pE
->aPos
.Y() ) && ( pEntry
->aPos
.X() > pE
->aPos
.X() ) ) )
276 if ( nPos
< aCtrls
.size() ) {
277 ComponentEntryList::iterator it
= aCtrls
.begin();
278 ::std::advance( it
, nPos
);
279 aCtrls
.insert( it
, pE
);
281 aCtrls
.push_back( pE
);
285 Sequence
< Reference
< XControlModel
> > aNewSeq( nCtrls
);
286 for ( n
= 0; n
< nCtrls
; n
++ )
288 ComponentEntry
* pE
= aCtrls
[ n
];
289 Reference
< XControl
> xUC( pE
->pComponent
, UNO_QUERY
);
290 aNewSeq
.getArray()[n
] = xUC
->getModel();
295 mxModel
->setControlModels( aNewSeq
);
298 void StdTabController::activateTabOrder( )
300 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
302 // Activate tab order for the control container
304 Reference
< XControl
> xC( mxControlContainer
, UNO_QUERY
);
305 Reference
< XVclContainerPeer
> xVclContainerPeer
;
307 xVclContainerPeer
.set(xC
->getPeer(), css::uno::UNO_QUERY
);
308 if ( !xC
.is() || !xVclContainerPeer
.is() )
311 // This may return a TabController, which returns desired list of controls faster
312 Reference
< XTabController
> xTabController(static_cast< ::cppu::OWeakObject
* >(this), UNO_QUERY
);
314 // Get a flattened list of controls sequences
315 Sequence
< Reference
< XControlModel
> > aModels
= mxModel
->getControlModels();
316 Sequence
< Reference
< XWindow
> > aCompSeq
;
317 Sequence
< Any
> aTabSeq
;
319 // DG: For the sake of optimization, retrieve Controls from getControls(),
320 // this may sound counterproductive, but leads to performance improvements
321 // in practical scenarios (Forms)
322 Sequence
< Reference
< XControl
> > aControls
= xTabController
->getControls();
324 // #58317# Some Models may be missing from the Container. Plus there is a
325 // autoTabOrder call later on.
326 if( !ImplCreateComponentSequence( aControls
, aModels
, aCompSeq
, &aTabSeq
, true ) )
329 xVclContainerPeer
->setTabOrder( aCompSeq
, aTabSeq
, mxModel
->getGroupControl() );
332 Sequence
< Reference
< XControlModel
> > aThisGroupModels
;
333 Sequence
< Reference
< XWindow
> > aControlComponents
;
335 sal_uInt32 nGroups
= mxModel
->getGroupCount();
336 for ( sal_uInt32 nG
= 0; nG
< nGroups
; nG
++ )
338 mxModel
->getGroup( nG
, aThisGroupModels
, aName
);
340 aControls
= xTabController
->getControls();
341 // ImplCreateComponentSequence has a really strange semantics regarding it's first parameter:
342 // upon method entry, it expects a super set of the controls which it returns
343 // this means we need to completely fill this sequence with all available controls before
344 // calling into ImplCreateComponentSequence
346 aControlComponents
.realloc( 0 );
348 ImplCreateComponentSequence( aControls
, aThisGroupModels
, aControlComponents
, nullptr, true );
349 xVclContainerPeer
->setGroup( aControlComponents
);
353 void StdTabController::activateFirst( )
355 SolarMutexGuard aSolarGuard
;
356 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() ); //TODO: necessary?
358 ImplActivateControl( true );
361 void StdTabController::activateLast( )
363 SolarMutexGuard aSolarGuard
;
364 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() ); //TODO: necessary?
366 ImplActivateControl( false );
369 OUString
StdTabController::getImplementationName()
371 return OUString("stardiv.Toolkit.StdTabController");
374 sal_Bool
StdTabController::supportsService(OUString
const & ServiceName
)
376 return cppu::supportsService(this, ServiceName
);
379 css::uno::Sequence
<OUString
> StdTabController::getSupportedServiceNames()
381 return css::uno::Sequence
<OUString
>{
382 OUString::createFromAscii(szServiceName2_TabController
),
383 "stardiv.vcl.control.TabController"};
386 Reference
< XControl
> StdTabController::FindControl( Sequence
< Reference
< XControl
> >& rCtrls
,
387 const Reference
< XControlModel
> & rxCtrlModel
)
389 DBG_ASSERT( rxCtrlModel
.is(), "ImplFindControl - which one ?!" );
391 const Reference
< XControl
> * pCtrls
= rCtrls
.getConstArray();
392 sal_Int32 nCtrls
= rCtrls
.getLength();
393 for ( sal_Int32 n
= 0; n
< nCtrls
; n
++ )
395 Reference
< XControlModel
> xModel(pCtrls
[n
].is() ? pCtrls
[n
]->getModel() : Reference
< XControlModel
> ());
396 if ( xModel
.get() == rxCtrlModel
.get() )
398 Reference
< XControl
> xCtrl( pCtrls
[n
] );
399 ::comphelper::removeElementAt( rCtrls
, n
);
403 return Reference
< XControl
> ();
406 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
407 stardiv_Toolkit_StdTabController_get_implementation(
408 css::uno::XComponentContext
*,
409 css::uno::Sequence
<css::uno::Any
> const &)
411 return cppu::acquire(new StdTabController());
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */