bump product version to 5.0.4.1
[LibreOffice.git] / toolkit / source / controls / stdtabcontroller.cxx
blob3134587725846ad615ed301da405629f1130b435
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 <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>
31 #include <rtl/uuid.h>
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,
60 bool bPeerComponent )
62 bool bOK = true;
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);
80 rControls = aSeq;
82 #ifdef DBG_UTIL
83 DBG_ASSERT( rControls.getLength() <= rModels.getLength(), "StdTabController:ImplCreateComponentSequence: inconsistence!" );
84 // there may be less controls than models, but never more controls than models
85 #endif
88 const Reference< XControl > * pControls = rControls.getConstArray();
89 sal_uInt32 nCtrls = rControls.getLength();
90 rComponents.realloc( nCtrls );
91 Reference< XWindow > * pComps = rComponents.getArray();
92 Any* pTabs = NULL;
95 if ( pTabStops )
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]);
105 if ( xCtrl.is() )
107 if (bPeerComponent)
108 pComps[n] = Reference< XWindow > (xCtrl->getPeer(), UNO_QUERY);
109 else
110 pComps[n] = Reference< XWindow > (xCtrl, UNO_QUERY);
112 // TabStop-Property
113 if ( pTabs )
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 );
122 else
123 ++pTabs;
126 else
128 OSL_TRACE( "ImplCreateComponentSequence: Control not found" );
129 bOK = false;
132 return bOK;
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 nicht im Container!" );
147 if ( pControls[nCtrl].is() )
149 Reference< XWindowPeer > xCP = pControls[nCtrl]->getPeer();
150 if ( xCP.is() )
152 VCLXWindow* pC = VCLXWindow::GetImplementation( xCP );
153 if ( pC && pC->GetWindow() && ( pC->GetWindow()->GetStyle() & WB_TABSTOP ) )
155 pC->GetWindow()->GrabFocus();
156 break;
163 // XInterface
164 Any StdTabController::queryAggregation( const Type & rType ) throw(RuntimeException, std::exception)
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 ));
173 // XTypeProvider
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 ) throw(RuntimeException, std::exception)
181 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
183 mxModel = Model;
186 Reference< XTabControllerModel > StdTabController::getModel( ) throw(RuntimeException, std::exception)
188 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
190 return mxModel;
193 void StdTabController::setContainer( const Reference< XControlContainer >& Container ) throw(RuntimeException, std::exception)
195 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
197 mxControlContainer = Container;
200 Reference< XControlContainer > StdTabController::getContainer( ) throw(RuntimeException, std::exception)
202 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
204 return mxControlContainer;
207 Sequence< Reference< XControl > > StdTabController::getControls( ) throw(RuntimeException, std::exception)
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;
230 return aSeq;
233 void StdTabController::autoTabOrder( ) throw(RuntimeException, std::exception)
235 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
237 DBG_ASSERT( mxControlContainer.is(), "autoTabOrder: No ControlContainer!" );
238 if ( !mxControlContainer.is() )
239 return;
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, NULL, false ) )
251 return;
253 sal_uInt32 nCtrls = aCompSeq.getLength();
254 Reference< XWindow > * pComponents = aCompSeq.getArray();
256 // insert sort algorithm
257 ComponentEntryList aCtrls;
258 size_t n;
259 for ( n = 0; n < nCtrls; n++ )
261 XWindow* pC = (XWindow*)pComponents[n].get();
262 ComponentEntry* pE = new ComponentEntry;
263 pE->pComponent = pC;
264 awt::Rectangle aPosSize = pC->getPosSize();
265 pE->aPos.X() = aPosSize.X;
266 pE->aPos.Y() = aPosSize.Y;
268 sal_uInt16 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() ) ) )
274 break;
276 if ( nPos < aCtrls.size() ) {
277 ComponentEntryList::iterator it = aCtrls.begin();
278 ::std::advance( it, nPos );
279 aCtrls.insert( it, pE );
280 } else {
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();
291 delete pE;
293 aCtrls.clear();
295 mxModel->setControlModels( aNewSeq );
298 void StdTabController::activateTabOrder( ) throw(RuntimeException, std::exception)
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;
306 if ( xC.is() )
307 xVclContainerPeer.set(xC->getPeer(), css::uno::UNO_QUERY);
308 if ( !xC.is() || !xVclContainerPeer.is() )
309 return;
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 ) )
327 return;
329 xVclContainerPeer->setTabOrder( aCompSeq, aTabSeq, mxModel->getGroupControl() );
331 OUString aName;
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, NULL, true );
349 xVclContainerPeer->setGroup( aControlComponents );
353 void StdTabController::activateFirst( ) throw(RuntimeException, std::exception)
355 SolarMutexGuard aSolarGuard;
356 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?
358 ImplActivateControl( true );
361 void StdTabController::activateLast( ) throw(RuntimeException, std::exception)
363 SolarMutexGuard aSolarGuard;
364 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?
366 ImplActivateControl( false );
369 OUString StdTabController::getImplementationName()
370 throw (css::uno::RuntimeException, std::exception)
372 return OUString("stardiv.Toolkit.StdTabController");
375 sal_Bool StdTabController::supportsService(OUString const & ServiceName)
376 throw (css::uno::RuntimeException, std::exception)
378 return cppu::supportsService(this, ServiceName);
381 css::uno::Sequence<OUString> StdTabController::getSupportedServiceNames()
382 throw (css::uno::RuntimeException, std::exception)
384 return css::uno::Sequence<OUString>{
385 OUString::createFromAscii(szServiceName2_TabController),
386 "stardiv.vcl.control.TabController"};
389 Reference< XControl > StdTabController::FindControl( Sequence< Reference< XControl > >& rCtrls,
390 const Reference< XControlModel > & rxCtrlModel )
392 DBG_ASSERT( rxCtrlModel.is(), "ImplFindControl - welches ?!" );
394 const Reference< XControl > * pCtrls = rCtrls.getConstArray();
395 sal_Int32 nCtrls = rCtrls.getLength();
396 for ( sal_Int32 n = 0; n < nCtrls; n++ )
398 Reference< XControlModel > xModel(pCtrls[n].is() ? pCtrls[n]->getModel() : Reference< XControlModel > ());
399 if ( (XControlModel*)xModel.get() == (XControlModel*)rxCtrlModel.get() )
401 Reference< XControl > xCtrl( pCtrls[n] );
402 ::comphelper::removeElementAt( rCtrls, n );
403 return xCtrl;
406 return Reference< XControl > ();
409 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
410 stardiv_Toolkit_StdTabController_get_implementation(
411 css::uno::XComponentContext *,
412 css::uno::Sequence<css::uno::Any> const &)
414 return cppu::acquire(new StdTabController());
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */