Updated core
[LibreOffice.git] / toolkit / source / controls / stdtabcontroller.cxx
blob2ffdea5ef272f4482792fbd80c266891743c969d
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/awt/XVclContainerPeer.hpp>
23 #include <toolkit/controls/stdtabcontroller.hxx>
24 #include <toolkit/controls/stdtabcontrollermodel.hxx>
25 #include <toolkit/awt/vclxwindow.hxx>
26 #include <toolkit/helper/macros.hxx>
27 #include <cppuhelper/typeprovider.hxx>
28 #include <rtl/uuid.h>
30 #include <tools/debug.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/window.hxx>
33 #include <comphelper/sequence.hxx>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::awt;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::beans;
41 // ----------------------------------------------------
42 // class StdTabController
43 // ----------------------------------------------------
44 StdTabController::StdTabController()
48 StdTabController::~StdTabController()
52 sal_Bool StdTabController::ImplCreateComponentSequence(
53 Sequence< Reference< XControl > >& rControls,
54 const Sequence< Reference< XControlModel > >& rModels,
55 Sequence< Reference< XWindow > >& rComponents,
56 Sequence< Any>* pTabStops,
57 sal_Bool bPeerComponent ) const
59 sal_Bool bOK = sal_True;
61 // Get only the requested controls
62 sal_Int32 nModels = rModels.getLength();
63 if (nModels != rControls.getLength())
65 Sequence< Reference< XControl > > aSeq( nModels );
66 const Reference< XControlModel >* pModels = rModels.getConstArray();
67 Reference< XControl > xCurrentControl;
69 sal_Int32 nRealControls = 0;
70 for (sal_Int32 n = 0; n < nModels; ++n, ++pModels)
72 xCurrentControl = FindControl(rControls, *pModels);
73 if (xCurrentControl.is())
74 aSeq.getArray()[nRealControls++] = xCurrentControl;
76 aSeq.realloc(nRealControls);
77 rControls = aSeq;
79 #ifdef DBG_UTIL
80 DBG_ASSERT( rControls.getLength() <= rModels.getLength(), "StdTabController:ImplCreateComponentSequence: inconsistence!" );
81 // there may be less controls than models, but never more controls than models
82 #endif
85 const Reference< XControl > * pControls = rControls.getConstArray();
86 sal_uInt32 nCtrls = rControls.getLength();
87 rComponents.realloc( nCtrls );
88 Reference< XWindow > * pComps = rComponents.getArray();
89 Any* pTabs = NULL;
92 if ( pTabStops )
94 *pTabStops = Sequence< Any>( nCtrls );
95 pTabs = pTabStops->getArray();
98 for ( sal_uInt32 n = 0; bOK && ( n < nCtrls ); n++ )
100 // Get the matching control for this model
101 Reference< XControl > xCtrl(pControls[n]);
102 if ( xCtrl.is() )
104 if (bPeerComponent)
105 pComps[n] = Reference< XWindow > (xCtrl->getPeer(), UNO_QUERY);
106 else
107 pComps[n] = Reference< XWindow > (xCtrl, UNO_QUERY);
109 // TabStop-Property
110 if ( pTabs )
112 // opt: Constant String for TabStop name
113 static const OUString aTabStopName( "Tabstop" );
115 Reference< XPropertySet > xPSet( xCtrl->getModel(), UNO_QUERY );
116 Reference< XPropertySetInfo > xInfo = xPSet->getPropertySetInfo();
117 if( xInfo->hasPropertyByName( aTabStopName ) )
118 *pTabs++ = xPSet->getPropertyValue( aTabStopName );
119 else
120 ++pTabs;
123 else
125 OSL_TRACE( "ImplCreateComponentSequence: Control not found" );
126 bOK = sal_False;
129 return bOK;
132 void StdTabController::ImplActivateControl( sal_Bool bFirst ) const
134 // HACK due to bug #53688#, map controls onto an interface if remote controls may occur
135 Reference< XTabController > xTabController(const_cast< ::cppu::OWeakObject* >(static_cast< const ::cppu::OWeakObject* >(this)), UNO_QUERY);
136 Sequence< Reference< XControl > > aCtrls = xTabController->getControls();
137 const Reference< XControl > * pControls = aCtrls.getConstArray();
138 sal_uInt32 nCount = aCtrls.getLength();
140 for ( sal_uInt32 n = bFirst ? 0 : nCount; bFirst ? ( n < nCount ) : n; )
142 sal_uInt32 nCtrl = bFirst ? n++ : --n;
143 DBG_ASSERT( pControls[nCtrl].is(), "Control nicht im Container!" );
144 if ( pControls[nCtrl].is() )
146 Reference< XWindowPeer > xCP = pControls[nCtrl]->getPeer();
147 if ( xCP.is() )
149 VCLXWindow* pC = VCLXWindow::GetImplementation( xCP );
150 if ( pC && pC->GetWindow() && ( pC->GetWindow()->GetStyle() & WB_TABSTOP ) )
152 pC->GetWindow()->GrabFocus();
153 break;
160 // XInterface
161 Any StdTabController::queryAggregation( const Type & rType ) throw(RuntimeException)
163 Any aRet = ::cppu::queryInterface( rType,
164 (static_cast< XTabController* >(this)),
165 (static_cast< XServiceInfo* >(this)),
166 (static_cast< XTypeProvider* >(this)) );
167 return (aRet.hasValue() ? aRet : OWeakAggObject::queryAggregation( rType ));
170 // XTypeProvider
171 IMPL_XTYPEPROVIDER_START( StdTabController )
172 getCppuType( ( Reference< XTabController>* ) NULL ),
173 getCppuType( ( Reference< XServiceInfo>* ) NULL )
174 IMPL_XTYPEPROVIDER_END
176 void StdTabController::setModel( const Reference< XTabControllerModel >& Model ) throw(RuntimeException)
178 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
180 mxModel = Model;
183 Reference< XTabControllerModel > StdTabController::getModel( ) throw(RuntimeException)
185 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
187 return mxModel;
190 void StdTabController::setContainer( const Reference< XControlContainer >& Container ) throw(RuntimeException)
192 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
194 mxControlContainer = Container;
197 Reference< XControlContainer > StdTabController::getContainer( ) throw(RuntimeException)
199 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
201 return mxControlContainer;
204 Sequence< Reference< XControl > > StdTabController::getControls( ) throw(RuntimeException)
206 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
208 Sequence< Reference< XControl > > aSeq;
210 if ( mxControlContainer.is() )
212 Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels();
213 const Reference< XControlModel > * pModels = aModels.getConstArray();
215 Sequence< Reference< XControl > > xCtrls = mxControlContainer->getControls();
217 sal_uInt32 nCtrls = aModels.getLength();
218 aSeq = Sequence< Reference< XControl > >( nCtrls );
219 for ( sal_uInt32 n = 0; n < nCtrls; n++ )
221 Reference< XControlModel > xCtrlModel = pModels[n];
222 // Search matching Control for this Model
223 Reference< XControl > xCtrl = FindControl( xCtrls, xCtrlModel );
224 aSeq.getArray()[n] = xCtrl;
227 return aSeq;
230 void StdTabController::autoTabOrder( ) throw(RuntimeException)
232 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
234 DBG_ASSERT( mxControlContainer.is(), "autoTabOrder: No ControlContainer!" );
235 if ( !mxControlContainer.is() )
236 return;
238 Sequence< Reference< XControlModel > > aSeq = mxModel->getControlModels();
239 Sequence< Reference< XWindow > > aCompSeq;
241 // This may return a TabController, which returns desired list of controls faster
242 Reference< XTabController > xTabController(static_cast< ::cppu::OWeakObject* >(this), UNO_QUERY);
243 Sequence< Reference< XControl > > aControls = xTabController->getControls();
245 // #58317# Some Models may be missing from the Container. Plus there is a
246 // autoTabOrder call later on.
247 if( !ImplCreateComponentSequence( aControls, aSeq, aCompSeq, NULL, sal_False ) )
248 return;
250 sal_uInt32 nCtrls = aCompSeq.getLength();
251 Reference< XWindow > * pComponents = aCompSeq.getArray();
253 // insert sort algorithm
254 ComponentEntryList aCtrls;
255 size_t n;
256 for ( n = 0; n < nCtrls; n++ )
258 XWindow* pC = (XWindow*)pComponents[n].get();
259 ComponentEntry* pE = new ComponentEntry;
260 pE->pComponent = pC;
261 awt::Rectangle aPosSize = pC->getPosSize();
262 pE->aPos.X() = aPosSize.X;
263 pE->aPos.Y() = aPosSize.Y;
265 sal_uInt16 nPos;
266 for ( nPos = 0; nPos < aCtrls.size(); nPos++ )
268 ComponentEntry* pEntry = aCtrls[ nPos ];
269 if ( ( pEntry->aPos.Y() > pE->aPos.Y() ) ||
270 ( ( pEntry->aPos.Y() == pE->aPos.Y() ) && ( pEntry->aPos.X() > pE->aPos.X() ) ) )
271 break;
273 if ( nPos < aCtrls.size() ) {
274 ComponentEntryList::iterator it = aCtrls.begin();
275 ::std::advance( it, nPos );
276 aCtrls.insert( it, pE );
277 } else {
278 aCtrls.push_back( pE );
282 Sequence< Reference< XControlModel > > aNewSeq( nCtrls );
283 for ( n = 0; n < nCtrls; n++ )
285 ComponentEntry* pE = aCtrls[ n ];
286 Reference< XControl > xUC( pE->pComponent, UNO_QUERY );
287 aNewSeq.getArray()[n] = xUC->getModel();
288 delete pE;
290 aCtrls.clear();
292 mxModel->setControlModels( aNewSeq );
295 void StdTabController::activateTabOrder( ) throw(RuntimeException)
297 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
299 // Activate tab order for the control container
301 Reference< XControl > xC( mxControlContainer, UNO_QUERY );
302 Reference< XVclContainerPeer > xVclContainerPeer;
303 if ( xC.is() )
304 xVclContainerPeer = xVclContainerPeer.query( xC->getPeer() );
305 if ( !xC.is() || !xVclContainerPeer.is() )
306 return;
308 // This may return a TabController, which returns desired list of controls faster
309 Reference< XTabController > xTabController(static_cast< ::cppu::OWeakObject* >(this), UNO_QUERY);
311 // Get a flattened list of controls sequences
312 Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels();
313 Sequence< Reference< XWindow > > aCompSeq;
314 Sequence< Any> aTabSeq;
316 // DG: For the sake of optimization, retrieve Controls from getControls(),
317 // this may sound counterproductive, but leads to performance improvements
318 // in practical scenarios (Forms)
319 Sequence< Reference< XControl > > aControls = xTabController->getControls();
321 // #58317# Some Models may be missing from the Container. Plus there is a
322 // autoTabOrder call later on.
323 if( !ImplCreateComponentSequence( aControls, aModels, aCompSeq, &aTabSeq, sal_True ) )
324 return;
326 xVclContainerPeer->setTabOrder( aCompSeq, aTabSeq, mxModel->getGroupControl() );
328 OUString aName;
329 Sequence< Reference< XControlModel > > aThisGroupModels;
330 Sequence< Reference< XWindow > > aControlComponents;
332 sal_uInt32 nGroups = mxModel->getGroupCount();
333 for ( sal_uInt32 nG = 0; nG < nGroups; nG++ )
335 mxModel->getGroup( nG, aThisGroupModels, aName );
337 aControls = xTabController->getControls();
338 // ImplCreateComponentSequence has a really strange semantics regarding it's first parameter:
339 // upon method entry, it expects a super set of the controls which it returns
340 // this means we need to completely fill this sequence with all available controls before
341 // calling into ImplCreateComponentSequence
343 aControlComponents.realloc( 0 );
345 ImplCreateComponentSequence( aControls, aThisGroupModels, aControlComponents, NULL, sal_True );
346 xVclContainerPeer->setGroup( aControlComponents );
350 void StdTabController::activateFirst( ) throw(RuntimeException)
352 SolarMutexGuard aSolarGuard;
353 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?
355 ImplActivateControl( sal_True );
358 void StdTabController::activateLast( ) throw(RuntimeException)
360 SolarMutexGuard aSolarGuard;
361 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?
363 ImplActivateControl( sal_False );
367 Reference< XControl > StdTabController::FindControl( Sequence< Reference< XControl > >& rCtrls,
368 const Reference< XControlModel > & rxCtrlModel )
370 DBG_ASSERT( rxCtrlModel.is(), "ImplFindControl - welches ?!" );
372 const Reference< XControl > * pCtrls = rCtrls.getConstArray();
373 sal_Int32 nCtrls = rCtrls.getLength();
374 for ( sal_Int32 n = 0; n < nCtrls; n++ )
376 Reference< XControlModel > xModel(pCtrls[n].is() ? pCtrls[n]->getModel() : Reference< XControlModel > ());
377 if ( (XControlModel*)xModel.get() == (XControlModel*)rxCtrlModel.get() )
379 Reference< XControl > xCtrl( pCtrls[n] );
380 ::comphelper::removeElementAt( rCtrls, n );
381 return xCtrl;
384 return Reference< XControl > ();
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */