LibreOffice Module toolkit (master) 1
stdtabcontroller.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <com/sun/star/beans/XPropertySet.hpp>
21#include <com/sun/star/uno/XComponentContext.hpp>
22#include <com/sun/star/awt/XVclContainerPeer.hpp>
23#include <com/sun/star/lang/IllegalArgumentException.hpp>
24
30
31#include <sal/log.hxx>
32#include <tools/debug.hxx>
33#include <vcl/svapp.hxx>
34#include <vcl/window.hxx>
36
37using namespace ::com::sun::star;
38using namespace ::com::sun::star::uno;
39using namespace ::com::sun::star::awt;
40using namespace ::com::sun::star::lang;
41using namespace ::com::sun::star::beans;
42
43
44
46{
47}
48
50{
51}
52
54 Sequence< Reference< XControl > >& rControls,
55 const Sequence< Reference< XControlModel > >& rModels,
56 Sequence< Reference< XWindow > >& rComponents,
57 Sequence< Any>* pTabStops,
58 bool bPeerComponent )
59{
60 // Get only the requested controls
61 sal_Int32 nModels = rModels.getLength();
62 if (nModels != rControls.getLength())
63 {
64 Sequence< Reference< XControl > > aSeq( nModels );
65 auto aSeqRange = asNonConstRange(aSeq);
66 Reference< XControl > xCurrentControl;
67
68 sal_Int32 nRealControls = 0;
69 for (const Reference< XControlModel >& rModel : rModels)
70 {
71 xCurrentControl = FindControl(rControls, rModel);
72 if (xCurrentControl.is())
73 aSeqRange[nRealControls++] = xCurrentControl;
74 }
75 aSeq.realloc(nRealControls);
76 rControls = aSeq;
77 }
78
79 // there may be less controls than models, but never more controls than models
80 assert(rControls.getLength() <= rModels.getLength());
81
82 sal_Int32 nCtrls = rControls.getLength();
83 rComponents.realloc( nCtrls );
84 Reference< XWindow > * pComps = rComponents.getArray();
85 Any* pTabs = nullptr;
86
87
88 if ( pTabStops )
89 {
90 *pTabStops = Sequence< Any>( nCtrls );
91 pTabs = pTabStops->getArray();
92 }
93
94 bool bOK = true;
95 for ( const Reference< XControl >& xCtrl : std::as_const(rControls) )
96 {
97 // Get the matching control for this model
98 if ( !xCtrl.is() )
99 {
100 SAL_WARN("toolkit", "Control not found" );
101 bOK = false;
102 break;
103 }
104
105 if (bPeerComponent)
106 pComps->set(xCtrl->getPeer(), UNO_QUERY);
107 else
108 pComps->set(xCtrl, UNO_QUERY);
109
110 // TabStop-Property
111 if ( pTabs )
112 {
113 // opt: Constant String for TabStop name
114 static constexpr OUStringLiteral aTabStopName = u"Tabstop";
115
116 Reference< XPropertySet > xPSet( xCtrl->getModel(), UNO_QUERY );
117 Reference< XPropertySetInfo > xInfo = xPSet->getPropertySetInfo();
118 if( xInfo->hasPropertyByName( aTabStopName ) )
119 *pTabs++ = xPSet->getPropertyValue( aTabStopName );
120 else
121 ++pTabs;
122 }
123
124 ++pComps;
125 }
126 return bOK;
127}
128
130{
131 // HACK due to bug #53688#, map controls onto an interface if remote controls may occur
132 Sequence< Reference< XControl > > aCtrls = const_cast<StdTabController*>(this)->getControls();
133 const Reference< XControl > * pControls = aCtrls.getConstArray();
134 sal_uInt32 nCount = aCtrls.getLength();
135
136 for ( sal_uInt32 n = bFirst ? 0 : nCount; bFirst ? n < nCount : n != 0; )
137 {
138 sal_uInt32 nCtrl = bFirst ? n++ : --n;
139 DBG_ASSERT( pControls[nCtrl].is(), "Control not in Container!" );
140 if ( pControls[nCtrl].is() )
141 {
142 Reference< XWindowPeer > xCP = pControls[nCtrl]->getPeer();
143 if ( xCP.is() )
144 {
145 VCLXWindow* pC = dynamic_cast<VCLXWindow*>( xCP.get() );
146 if ( pC && pC->GetWindow() && ( pC->GetWindow()->GetStyle() & WB_TABSTOP ) )
147 {
148 pC->GetWindow()->GrabFocus();
149 break;
150 }
151 }
152 }
153 }
154}
155
156// XInterface
158{
159 Any aRet = ::cppu::queryInterface( rType,
160 static_cast< XTabController* >(this),
161 static_cast< XServiceInfo* >(this),
162 static_cast< XTypeProvider* >(this) );
163 return (aRet.hasValue() ? aRet : OWeakAggObject::queryAggregation( rType ));
164}
165
167
168// XTypeProvider
169css::uno::Sequence< css::uno::Type > StdTabController::getTypes()
170{
171 static const css::uno::Sequence< css::uno::Type > aTypeList {
175 };
176 return aTypeList;
177}
178
179void StdTabController::setModel( const Reference< XTabControllerModel >& Model )
180{
181 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
182
183 mxModel = Model;
184}
185
186Reference< XTabControllerModel > StdTabController::getModel( )
187{
188 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
189
190 return mxModel;
191}
192
193void StdTabController::setContainer( const Reference< XControlContainer >& Container )
194{
195 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
196
197 mxControlContainer = Container;
198}
199
200Reference< XControlContainer > StdTabController::getContainer( )
201{
202 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
203
204 return mxControlContainer;
205}
206
207Sequence< Reference< XControl > > StdTabController::getControls( )
208{
209 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
210
211 Sequence< Reference< XControl > > aSeq;
212
213 if ( mxControlContainer.is() )
214 {
215 const Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels();
216
217 Sequence< Reference< XControl > > xCtrls = mxControlContainer->getControls();
218
219 sal_Int32 nCtrls = aModels.getLength();
220 aSeq = Sequence< Reference< XControl > >( nCtrls );
221 std::transform(aModels.begin(), aModels.end(), aSeq.getArray(),
222 [&xCtrls](const Reference< XControlModel >& xCtrlModel) -> Reference< XControl > {
223 return FindControl( xCtrls, xCtrlModel ); });
224 }
225 return aSeq;
226}
227
228namespace {
229
230struct ComponentEntry
231{
232 css::awt::XWindow* pComponent;
233 ::Point aPos;
234};
235
236}
237
239{
240 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
241
242 DBG_ASSERT( mxControlContainer.is(), "autoTabOrder: No ControlContainer!" );
243 if ( !mxControlContainer.is() )
244 return;
245
246 Sequence< Reference< XControlModel > > aSeq = mxModel->getControlModels();
247 Sequence< Reference< XWindow > > aCompSeq;
248
249 // This may return a TabController, which returns desired list of controls faster
250 Sequence< Reference< XControl > > aControls = getControls();
251
252 // #58317# Some Models may be missing from the Container. Plus there is a
253 // autoTabOrder call later on.
254 if( !ImplCreateComponentSequence( aControls, aSeq, aCompSeq, nullptr, false ) )
255 return;
256
257 sal_uInt32 nCtrls = aCompSeq.getLength();
258
259 // insert sort algorithm
260 std::vector< ComponentEntry > aCtrls;
261 aCtrls.reserve(nCtrls);
262 for ( const Reference< XWindow >& rComponent : std::as_const(aCompSeq) )
263 {
264 XWindow* pC = rComponent.get();
265 ComponentEntry newEntry;
266 newEntry.pComponent = pC;
267 awt::Rectangle aPosSize = pC->getPosSize();
268 newEntry.aPos.setX( aPosSize.X );
269 newEntry.aPos.setY( aPosSize.Y );
270
271 decltype(aCtrls)::size_type nPos;
272 for ( nPos = 0; nPos < aCtrls.size(); nPos++ )
273 {
274 ComponentEntry& rEntry = aCtrls[ nPos ];
275 if ( ( rEntry.aPos.Y() > newEntry.aPos.Y() ) ||
276 ( ( rEntry.aPos.Y() == newEntry.aPos.Y() ) && ( rEntry.aPos.X() > newEntry.aPos.X() ) ) )
277 break;
278 }
279 if ( nPos < aCtrls.size() ) {
280 aCtrls.insert( aCtrls.begin() + nPos, newEntry );
281 } else {
282 aCtrls.push_back( newEntry );
283 }
284 }
285
286 Sequence< Reference< XControlModel > > aNewSeq( nCtrls );
287 std::transform(aCtrls.begin(), aCtrls.end(), aNewSeq.getArray(),
288 [](const ComponentEntry& rEntry) -> Reference< XControlModel > {
289 Reference< XControl > xUC( rEntry.pComponent, UNO_QUERY );
290 return xUC->getModel();
291 });
292
293 mxModel->setControlModels( aNewSeq );
294}
295
297{
298 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
299
300 // Activate tab order for the control container
301
302 Reference< XControl > xC( mxControlContainer, UNO_QUERY );
303 Reference< XVclContainerPeer > xVclContainerPeer;
304 if ( xC.is() )
305 xVclContainerPeer.set(xC->getPeer(), css::uno::UNO_QUERY);
306 if ( !xC.is() || !xVclContainerPeer.is() )
307 return;
308
309 // This may return a TabController, which returns desired list of controls faster
310 // (the dreaded UNO aggregation, retrieve the thing that we are part of)
311 Reference<XTabController> xTabController( static_cast<XTabController*>(this), UNO_QUERY );
312
313 // Get a flattened list of controls sequences
314 Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels();
315 Sequence< Reference< XWindow > > aCompSeq;
316 Sequence< Any> aTabSeq;
317
318 // DG: For the sake of optimization, retrieve Controls from getControls(),
319 // this may sound counterproductive, but leads to performance improvements
320 // in practical scenarios (Forms)
321 Sequence< Reference< XControl > > aControls = xTabController->getControls();
322
323 // #58317# Some Models may be missing from the Container. Plus there is a
324 // autoTabOrder call later on.
325 if( !ImplCreateComponentSequence( aControls, aModels, aCompSeq, &aTabSeq, true ) )
326 return;
327
328 xVclContainerPeer->setTabOrder( aCompSeq, aTabSeq, mxModel->getGroupControl() );
329
330 OUString aName;
331 Sequence< Reference< XControlModel > > aThisGroupModels;
332 Sequence< Reference< XWindow > > aControlComponents;
333
334 sal_uInt32 nGroups = mxModel->getGroupCount();
335 for ( sal_uInt32 nG = 0; nG < nGroups; nG++ )
336 {
337 mxModel->getGroup( nG, aThisGroupModels, aName );
338
339 aControls = xTabController->getControls();
340 // ImplCreateComponentSequence has a really strange semantics regarding it's first parameter:
341 // upon method entry, it expects a super set of the controls which it returns
342 // this means we need to completely fill this sequence with all available controls before
343 // calling into ImplCreateComponentSequence
344
345 aControlComponents.realloc( 0 );
346
347 ImplCreateComponentSequence( aControls, aThisGroupModels, aControlComponents, nullptr, true );
348 xVclContainerPeer->setGroup( aControlComponents );
349 }
350}
351
353{
354 SolarMutexGuard aSolarGuard;
355 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?
356
357 ImplActivateControl( true );
358}
359
361{
362 SolarMutexGuard aSolarGuard;
363 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?
364
365 ImplActivateControl( false );
366}
367
369{
370 return "stardiv.Toolkit.StdTabController";
371}
372
373sal_Bool StdTabController::supportsService(OUString const & ServiceName)
374{
376}
377
378css::uno::Sequence<OUString> StdTabController::getSupportedServiceNames()
379{
380 return css::uno::Sequence<OUString>{
381 "com.sun.star.awt.TabController",
382 "stardiv.vcl.control.TabController"};
383}
384
385Reference< XControl > StdTabController::FindControl( Sequence< Reference< XControl > >& rCtrls,
386 const Reference< XControlModel > & rxCtrlModel )
387{
388 if (!rxCtrlModel.is())
389 throw lang::IllegalArgumentException("No valid XControlModel",
390 uno::Reference<uno::XInterface>(), 0);
391
392 auto pCtrl = std::find_if(std::cbegin(rCtrls), std::cend(rCtrls),
393 [&rxCtrlModel](const Reference< XControl >& rCtrl) {
394 Reference< XControlModel > xModel(rCtrl.is() ? rCtrl->getModel() : Reference< XControlModel > ());
395 return xModel.get() == rxCtrlModel.get();
396 });
397 if (pCtrl != std::cend(rCtrls))
398 {
399 auto n = static_cast<sal_Int32>(std::distance(std::cbegin(rCtrls), pCtrl));
400 Reference< XControl > xCtrl( *pCtrl );
401 ::comphelper::removeElementAt( rCtrls, n );
402 return xCtrl;
403 }
404 return Reference< XControl > ();
405}
406
407extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
409 css::uno::XComponentContext *,
410 css::uno::Sequence<css::uno::Any> const &)
411{
412 return cppu::acquire(new StdTabController());
413}
414
415/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< css::uno::Reference< css::awt::XControl > > SAL_CALL getControls() override
OUString SAL_CALL getImplementationName() override
css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &rType) override
void SAL_CALL activateFirst() override
css::uno::Reference< css::awt::XTabControllerModel > mxModel
css::uno::Reference< css::awt::XTabControllerModel > SAL_CALL getModel() override
css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
static css::uno::Reference< css::awt::XControl > FindControl(css::uno::Sequence< css::uno::Reference< css::awt::XControl > > &rCtrls, const css::uno::Reference< css::awt::XControlModel > &rxCtrlModel)
css::uno::Reference< css::awt::XControlContainer > mxControlContainer
void ImplActivateControl(bool bFirst) const
void SAL_CALL autoTabOrder() override
void SAL_CALL activateTabOrder() override
::osl::Mutex & GetMutex()
virtual ~StdTabController() override
static bool ImplCreateComponentSequence(css::uno::Sequence< css::uno::Reference< css::awt::XControl > > &rControls, const css::uno::Sequence< css::uno::Reference< css::awt::XControlModel > > &rModels, css::uno::Sequence< css::uno::Reference< css::awt::XWindow > > &rComponents, css::uno::Sequence< css::uno::Any > *pTabStops, bool bPeerComponent)
sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
css::uno::Reference< css::awt::XControlContainer > SAL_CALL getContainer() override
void SAL_CALL activateLast() override
void SAL_CALL setContainer(const css::uno::Reference< css::awt::XControlContainer > &Container) override
void SAL_CALL setModel(const css::uno::Reference< css::awt::XTabControllerModel > &Model) override
vcl::Window * GetWindow() const
Definition: vclxwindow.hxx:131
css::uno::Type const & get()
void GrabFocus()
WinBits GetStyle() const
int nCount
#define DBG_ASSERT(sCon, aError)
float u
OUString aName
sal_Int64 n
sal_uInt16 nPos
Sequence< sal_Int8 > aSeq
#define SAL_WARN(area, stream)
#define IMPL_IMPLEMENTATION_ID(ClassName)
Definition: macros.hxx:27
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * stardiv_Toolkit_StdTabController_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Reference< XModel > xModel
unsigned char sal_Bool
WinBits const WB_TABSTOP