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/window.hxx>
32 #include <comphelper/sequence.hxx>
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::awt
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::beans
;
40 // ----------------------------------------------------
41 // class StdTabController
42 // ----------------------------------------------------
43 StdTabController::StdTabController()
47 StdTabController::~StdTabController()
51 sal_Bool
StdTabController::ImplCreateComponentSequence(
52 Sequence
< Reference
< XControl
> >& rControls
,
53 const Sequence
< Reference
< XControlModel
> >& rModels
,
54 Sequence
< Reference
< XWindow
> >& rComponents
,
55 Sequence
< Any
>* pTabStops
,
56 sal_Bool bPeerComponent
) const
58 sal_Bool bOK
= sal_True
;
60 // Get only the requested controls
61 sal_Int32 nModels
= rModels
.getLength();
62 if (nModels
!= rControls
.getLength())
64 Sequence
< Reference
< XControl
> > aSeq( nModels
);
65 const Reference
< XControlModel
>* pModels
= rModels
.getConstArray();
66 Reference
< XControl
> xCurrentControl
;
68 sal_Int32 nRealControls
= 0;
69 for (sal_Int32 n
= 0; n
< nModels
; ++n
, ++pModels
)
71 xCurrentControl
= FindControl(rControls
, *pModels
);
72 if (xCurrentControl
.is())
73 aSeq
.getArray()[nRealControls
++] = xCurrentControl
;
75 aSeq
.realloc(nRealControls
);
79 DBG_ASSERT( rControls
.getLength() <= rModels
.getLength(), "StdTabController:ImplCreateComponentSequence: inconsistence!" );
80 // there may be less controls than models, but never more controls than models
84 const Reference
< XControl
> * pControls
= rControls
.getConstArray();
85 sal_uInt32 nCtrls
= rControls
.getLength();
86 rComponents
.realloc( nCtrls
);
87 Reference
< XWindow
> * pComps
= rComponents
.getArray();
93 *pTabStops
= Sequence
< Any
>( nCtrls
);
94 pTabs
= pTabStops
->getArray();
97 for ( sal_uInt32 n
= 0; bOK
&& ( n
< nCtrls
); n
++ )
99 // Get the matching control for this model
100 Reference
< XControl
> xCtrl(pControls
[n
]);
104 pComps
[n
] = Reference
< XWindow
> (xCtrl
->getPeer(), UNO_QUERY
);
106 pComps
[n
] = Reference
< XWindow
> (xCtrl
, UNO_QUERY
);
111 // opt: Constant String for TabStop name
112 static const ::rtl::OUString
aTabStopName( "Tabstop" );
114 Reference
< XPropertySet
> xPSet( xCtrl
->getModel(), UNO_QUERY
);
115 Reference
< XPropertySetInfo
> xInfo
= xPSet
->getPropertySetInfo();
116 if( xInfo
->hasPropertyByName( aTabStopName
) )
117 *pTabs
++ = xPSet
->getPropertyValue( aTabStopName
);
124 OSL_TRACE( "ImplCreateComponentSequence: Control not found" );
131 void StdTabController::ImplActivateControl( sal_Bool bFirst
) const
133 // HACK due to bug #53688#, map controls onto an interface if remote controls may occur
134 Reference
< XTabController
> xTabController(const_cast< ::cppu::OWeakObject
* >(static_cast< const ::cppu::OWeakObject
* >(this)), UNO_QUERY
);
135 Sequence
< Reference
< XControl
> > aCtrls
= xTabController
->getControls();
136 const Reference
< XControl
> * pControls
= aCtrls
.getConstArray();
137 sal_uInt32 nCount
= aCtrls
.getLength();
139 for ( sal_uInt32 n
= bFirst
? 0 : nCount
; bFirst
? ( n
< nCount
) : n
; )
141 sal_uInt32 nCtrl
= bFirst
? n
++ : --n
;
142 DBG_ASSERT( pControls
[nCtrl
].is(), "Control nicht im Container!" );
143 if ( pControls
[nCtrl
].is() )
145 Reference
< XWindowPeer
> xCP
= pControls
[nCtrl
]->getPeer();
148 VCLXWindow
* pC
= VCLXWindow::GetImplementation( xCP
);
149 if ( pC
&& pC
->GetWindow() && ( pC
->GetWindow()->GetStyle() & WB_TABSTOP
) )
151 pC
->GetWindow()->GrabFocus();
160 Any
StdTabController::queryAggregation( const Type
& rType
) throw(RuntimeException
)
162 Any aRet
= ::cppu::queryInterface( rType
,
163 (static_cast< XTabController
* >(this)),
164 (static_cast< XServiceInfo
* >(this)),
165 (static_cast< XTypeProvider
* >(this)) );
166 return (aRet
.hasValue() ? aRet
: OWeakAggObject::queryAggregation( rType
));
170 IMPL_XTYPEPROVIDER_START( StdTabController
)
171 getCppuType( ( Reference
< XTabController
>* ) NULL
),
172 getCppuType( ( Reference
< XServiceInfo
>* ) NULL
)
173 IMPL_XTYPEPROVIDER_END
175 void StdTabController::setModel( const Reference
< XTabControllerModel
>& Model
) throw(RuntimeException
)
177 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
182 Reference
< XTabControllerModel
> StdTabController::getModel( ) throw(RuntimeException
)
184 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
189 void StdTabController::setContainer( const Reference
< XControlContainer
>& Container
) throw(RuntimeException
)
191 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
193 mxControlContainer
= Container
;
196 Reference
< XControlContainer
> StdTabController::getContainer( ) throw(RuntimeException
)
198 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
200 return mxControlContainer
;
203 Sequence
< Reference
< XControl
> > StdTabController::getControls( ) throw(RuntimeException
)
205 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
207 Sequence
< Reference
< XControl
> > aSeq
;
209 if ( mxControlContainer
.is() )
211 Sequence
< Reference
< XControlModel
> > aModels
= mxModel
->getControlModels();
212 const Reference
< XControlModel
> * pModels
= aModels
.getConstArray();
214 Sequence
< Reference
< XControl
> > xCtrls
= mxControlContainer
->getControls();
216 sal_uInt32 nCtrls
= aModels
.getLength();
217 aSeq
= Sequence
< Reference
< XControl
> >( nCtrls
);
218 for ( sal_uInt32 n
= 0; n
< nCtrls
; n
++ )
220 Reference
< XControlModel
> xCtrlModel
= pModels
[n
];
221 // Search matching Control for this Model
222 Reference
< XControl
> xCtrl
= FindControl( xCtrls
, xCtrlModel
);
223 aSeq
.getArray()[n
] = xCtrl
;
229 void StdTabController::autoTabOrder( ) throw(RuntimeException
)
231 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
233 DBG_ASSERT( mxControlContainer
.is(), "autoTabOrder: No ControlContainer!" );
234 if ( !mxControlContainer
.is() )
237 Sequence
< Reference
< XControlModel
> > aSeq
= mxModel
->getControlModels();
238 Sequence
< Reference
< XWindow
> > aCompSeq
;
240 // This may return a TabController, which returns desired list of controls faster
241 Reference
< XTabController
> xTabController(static_cast< ::cppu::OWeakObject
* >(this), UNO_QUERY
);
242 Sequence
< Reference
< XControl
> > aControls
= xTabController
->getControls();
244 // #58317# Some Models may be missing from the Container. Plus there is a
245 // autoTabOrder call later on.
246 if( !ImplCreateComponentSequence( aControls
, aSeq
, aCompSeq
, NULL
, sal_False
) )
249 sal_uInt32 nCtrls
= aCompSeq
.getLength();
250 Reference
< XWindow
> * pComponents
= aCompSeq
.getArray();
252 // insert sort algorithm
253 ComponentEntryList aCtrls
;
255 for ( n
= 0; n
< nCtrls
; n
++ )
257 XWindow
* pC
= (XWindow
*)pComponents
[n
].get();
258 ComponentEntry
* pE
= new ComponentEntry
;
260 awt::Rectangle aPosSize
= pC
->getPosSize();
261 pE
->aPos
.X() = aPosSize
.X
;
262 pE
->aPos
.Y() = aPosSize
.Y
;
265 for ( nPos
= 0; nPos
< aCtrls
.size(); nPos
++ )
267 ComponentEntry
* pEntry
= aCtrls
[ nPos
];
268 if ( ( pEntry
->aPos
.Y() > pE
->aPos
.Y() ) ||
269 ( ( pEntry
->aPos
.Y() == pE
->aPos
.Y() ) && ( pEntry
->aPos
.X() > pE
->aPos
.X() ) ) )
272 if ( nPos
< aCtrls
.size() ) {
273 ComponentEntryList::iterator it
= aCtrls
.begin();
274 ::std::advance( it
, nPos
);
275 aCtrls
.insert( it
, pE
);
277 aCtrls
.push_back( pE
);
281 Sequence
< Reference
< XControlModel
> > aNewSeq( nCtrls
);
282 for ( n
= 0; n
< nCtrls
; n
++ )
284 ComponentEntry
* pE
= aCtrls
[ n
];
285 Reference
< XControl
> xUC( pE
->pComponent
, UNO_QUERY
);
286 aNewSeq
.getArray()[n
] = xUC
->getModel();
291 mxModel
->setControlModels( aNewSeq
);
294 void StdTabController::activateTabOrder( ) throw(RuntimeException
)
296 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
298 // Activate tab order for the control container
300 Reference
< XControl
> xC( mxControlContainer
, UNO_QUERY
);
301 Reference
< XVclContainerPeer
> xVclContainerPeer
;
303 xVclContainerPeer
= xVclContainerPeer
.query( xC
->getPeer() );
304 if ( !xC
.is() || !xVclContainerPeer
.is() )
307 // This may return a TabController, which returns desired list of controls faster
308 Reference
< XTabController
> xTabController(static_cast< ::cppu::OWeakObject
* >(this), UNO_QUERY
);
310 // Get a flattened list of controls sequences
311 Sequence
< Reference
< XControlModel
> > aModels
= mxModel
->getControlModels();
312 Sequence
< Reference
< XWindow
> > aCompSeq
;
313 Sequence
< Any
> aTabSeq
;
315 // DG: For the sake of optimization, retrieve Controls from getControls(),
316 // this may sound counterproductive, but leads to performance improvements
317 // in practical scenarios (Forms)
318 Sequence
< Reference
< XControl
> > aControls
= xTabController
->getControls();
320 // #58317# Some Models may be missing from the Container. Plus there is a
321 // autoTabOrder call later on.
322 if( !ImplCreateComponentSequence( aControls
, aModels
, aCompSeq
, &aTabSeq
, sal_True
) )
325 xVclContainerPeer
->setTabOrder( aCompSeq
, aTabSeq
, mxModel
->getGroupControl() );
327 ::rtl::OUString aName
;
328 Sequence
< Reference
< XControlModel
> > aThisGroupModels
;
329 Sequence
< Reference
< XWindow
> > aControlComponents
;
331 sal_uInt32 nGroups
= mxModel
->getGroupCount();
332 for ( sal_uInt32 nG
= 0; nG
< nGroups
; nG
++ )
334 mxModel
->getGroup( nG
, aThisGroupModels
, aName
);
336 aControls
= xTabController
->getControls();
337 // ImplCreateComponentSequence has a really strange semantics regarding it's first parameter:
338 // upon method entry, it expects a super set of the controls which it returns
339 // this means we need to completely fill this sequence with all available controls before
340 // calling into ImplCreateComponentSequence
342 aControlComponents
.realloc( 0 );
344 ImplCreateComponentSequence( aControls
, aThisGroupModels
, aControlComponents
, NULL
, sal_True
);
345 xVclContainerPeer
->setGroup( aControlComponents
);
349 void StdTabController::activateFirst( ) throw(RuntimeException
)
351 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
353 ImplActivateControl( sal_True
);
356 void StdTabController::activateLast( ) throw(RuntimeException
)
358 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
360 ImplActivateControl( sal_False
);
364 Reference
< XControl
> StdTabController::FindControl( Sequence
< Reference
< XControl
> >& rCtrls
,
365 const Reference
< XControlModel
> & rxCtrlModel
)
367 DBG_ASSERT( rxCtrlModel
.is(), "ImplFindControl - welches ?!" );
369 const Reference
< XControl
> * pCtrls
= rCtrls
.getConstArray();
370 sal_Int32 nCtrls
= rCtrls
.getLength();
371 for ( sal_Int32 n
= 0; n
< nCtrls
; n
++ )
373 Reference
< XControlModel
> xModel(pCtrls
[n
].is() ? pCtrls
[n
]->getModel() : Reference
< XControlModel
> ());
374 if ( (XControlModel
*)xModel
.get() == (XControlModel
*)rxCtrlModel
.get() )
376 Reference
< XControl
> xCtrl( pCtrls
[n
] );
377 ::comphelper::removeElementAt( rCtrls
, n
);
381 return Reference
< XControl
> ();
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */