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/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>
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
);
80 DBG_ASSERT( rControls
.getLength() <= rModels
.getLength(), "StdTabController:ImplCreateComponentSequence: inconsistence!" );
81 // there may be less controls than models, but never more controls than models
85 const Reference
< XControl
> * pControls
= rControls
.getConstArray();
86 sal_uInt32 nCtrls
= rControls
.getLength();
87 rComponents
.realloc( nCtrls
);
88 Reference
< XWindow
> * pComps
= rComponents
.getArray();
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
]);
105 pComps
[n
] = Reference
< XWindow
> (xCtrl
->getPeer(), UNO_QUERY
);
107 pComps
[n
] = Reference
< XWindow
> (xCtrl
, UNO_QUERY
);
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
);
125 OSL_TRACE( "ImplCreateComponentSequence: Control not found" );
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();
149 VCLXWindow
* pC
= VCLXWindow::GetImplementation( xCP
);
150 if ( pC
&& pC
->GetWindow() && ( pC
->GetWindow()->GetStyle() & WB_TABSTOP
) )
152 pC
->GetWindow()->GrabFocus();
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
));
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() );
183 Reference
< XTabControllerModel
> StdTabController::getModel( ) throw(RuntimeException
)
185 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
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
;
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() )
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
) )
250 sal_uInt32 nCtrls
= aCompSeq
.getLength();
251 Reference
< XWindow
> * pComponents
= aCompSeq
.getArray();
253 // insert sort algorithm
254 ComponentEntryList aCtrls
;
256 for ( n
= 0; n
< nCtrls
; n
++ )
258 XWindow
* pC
= (XWindow
*)pComponents
[n
].get();
259 ComponentEntry
* pE
= new ComponentEntry
;
261 awt::Rectangle aPosSize
= pC
->getPosSize();
262 pE
->aPos
.X() = aPosSize
.X
;
263 pE
->aPos
.Y() = aPosSize
.Y
;
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() ) ) )
273 if ( nPos
< aCtrls
.size() ) {
274 ComponentEntryList::iterator it
= aCtrls
.begin();
275 ::std::advance( it
, nPos
);
276 aCtrls
.insert( it
, pE
);
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();
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
;
304 xVclContainerPeer
= xVclContainerPeer
.query( xC
->getPeer() );
305 if ( !xC
.is() || !xVclContainerPeer
.is() )
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
) )
326 xVclContainerPeer
->setTabOrder( aCompSeq
, aTabSeq
, mxModel
->getGroupControl() );
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
);
384 return Reference
< XControl
> ();
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */