/src/libreoffice/toolkit/source/controls/stdtabcontroller.cxx
Line | Count | Source |
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 | | |
25 | | #include <controls/stdtabcontroller.hxx> |
26 | | #include <toolkit/awt/vclxwindow.hxx> |
27 | | #include <toolkit/helper/macros.hxx> |
28 | | #include <cppuhelper/supportsservice.hxx> |
29 | | #include <cppuhelper/queryinterface.hxx> |
30 | | |
31 | | #include <sal/log.hxx> |
32 | | #include <tools/debug.hxx> |
33 | | #include <vcl/svapp.hxx> |
34 | | #include <vcl/window.hxx> |
35 | | #include <comphelper/sequence.hxx> |
36 | | |
37 | | using namespace ::com::sun::star; |
38 | | using namespace ::com::sun::star::uno; |
39 | | using namespace ::com::sun::star::awt; |
40 | | using namespace ::com::sun::star::lang; |
41 | | using namespace ::com::sun::star::beans; |
42 | | |
43 | | |
44 | | |
45 | | StdTabController::StdTabController() |
46 | 0 | { |
47 | 0 | } |
48 | | |
49 | | StdTabController::~StdTabController() |
50 | 0 | { |
51 | 0 | } |
52 | | |
53 | | bool StdTabController::ImplCreateComponentSequence( |
54 | | Sequence< Reference< XControl > >& rControls, |
55 | | const Sequence< Reference< XControlModel > >& rModels, |
56 | | Sequence< Reference< XWindow > >& rComponents, |
57 | | Sequence< Any>* pTabStops, |
58 | | bool bPeerComponent ) |
59 | 0 | { |
60 | | // Get only the requested controls |
61 | 0 | sal_Int32 nModels = rModels.getLength(); |
62 | 0 | if (nModels != rControls.getLength()) |
63 | 0 | { |
64 | 0 | Sequence< Reference< XControl > > aSeq( nModels ); |
65 | 0 | auto aSeqRange = asNonConstRange(aSeq); |
66 | 0 | Reference< XControl > xCurrentControl; |
67 | |
|
68 | 0 | sal_Int32 nRealControls = 0; |
69 | 0 | for (const Reference< XControlModel >& rModel : rModels) |
70 | 0 | { |
71 | 0 | xCurrentControl = FindControl(rControls, rModel); |
72 | 0 | if (xCurrentControl.is()) |
73 | 0 | aSeqRange[nRealControls++] = xCurrentControl; |
74 | 0 | } |
75 | 0 | aSeq.realloc(nRealControls); |
76 | 0 | rControls = std::move(aSeq); |
77 | 0 | } |
78 | | |
79 | | // there may be less controls than models, but never more controls than models |
80 | 0 | assert(rControls.getLength() <= rModels.getLength()); |
81 | |
|
82 | 0 | sal_Int32 nCtrls = rControls.getLength(); |
83 | 0 | rComponents.realloc( nCtrls ); |
84 | 0 | Reference< XWindow > * pComps = rComponents.getArray(); |
85 | 0 | Any* pTabs = nullptr; |
86 | | |
87 | |
|
88 | 0 | if ( pTabStops ) |
89 | 0 | { |
90 | 0 | *pTabStops = Sequence< Any>( nCtrls ); |
91 | 0 | pTabs = pTabStops->getArray(); |
92 | 0 | } |
93 | |
|
94 | 0 | bool bOK = true; |
95 | 0 | for (const Reference<XControl>& xCtrl : rControls) |
96 | 0 | { |
97 | | // Get the matching control for this model |
98 | 0 | if ( !xCtrl.is() ) |
99 | 0 | { |
100 | 0 | SAL_WARN("toolkit", "Control not found" ); |
101 | 0 | bOK = false; |
102 | 0 | break; |
103 | 0 | } |
104 | | |
105 | 0 | if (bPeerComponent) |
106 | 0 | pComps->set(xCtrl->getPeer(), UNO_QUERY); |
107 | 0 | else |
108 | 0 | pComps->set(xCtrl, UNO_QUERY); |
109 | | |
110 | | // TabStop-Property |
111 | 0 | if ( pTabs ) |
112 | 0 | { |
113 | | // opt: Constant String for TabStop name |
114 | 0 | static constexpr OUString aTabStopName = u"Tabstop"_ustr; |
115 | |
|
116 | 0 | Reference< XPropertySet > xPSet( xCtrl->getModel(), UNO_QUERY ); |
117 | 0 | Reference< XPropertySetInfo > xInfo = xPSet->getPropertySetInfo(); |
118 | 0 | if( xInfo->hasPropertyByName( aTabStopName ) ) |
119 | 0 | *pTabs++ = xPSet->getPropertyValue( aTabStopName ); |
120 | 0 | else |
121 | 0 | ++pTabs; |
122 | 0 | } |
123 | |
|
124 | 0 | ++pComps; |
125 | 0 | } |
126 | 0 | return bOK; |
127 | 0 | } |
128 | | |
129 | | void StdTabController::ImplActivateControl( bool bFirst ) const |
130 | 0 | { |
131 | | // HACK due to bug #53688#, map controls onto an interface if remote controls may occur |
132 | 0 | Sequence< Reference< XControl > > aCtrls = const_cast<StdTabController*>(this)->getControls(); |
133 | 0 | const Reference< XControl > * pControls = aCtrls.getConstArray(); |
134 | 0 | sal_uInt32 nCount = aCtrls.getLength(); |
135 | |
|
136 | 0 | for ( sal_uInt32 n = bFirst ? 0 : nCount; bFirst ? n < nCount : n != 0; ) |
137 | 0 | { |
138 | 0 | sal_uInt32 nCtrl = bFirst ? n++ : --n; |
139 | 0 | DBG_ASSERT( pControls[nCtrl].is(), "Control not in Container!" ); |
140 | 0 | if ( pControls[nCtrl].is() ) |
141 | 0 | { |
142 | 0 | Reference< XWindowPeer > xCP = pControls[nCtrl]->getPeer(); |
143 | 0 | if ( xCP.is() ) |
144 | 0 | { |
145 | 0 | VCLXWindow* pC = dynamic_cast<VCLXWindow*>( xCP.get() ); |
146 | 0 | if ( pC && pC->GetWindow() && ( pC->GetWindow()->GetStyle() & WB_TABSTOP ) ) |
147 | 0 | { |
148 | 0 | pC->GetWindow()->GrabFocus(); |
149 | 0 | break; |
150 | 0 | } |
151 | 0 | } |
152 | 0 | } |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | | // XInterface |
157 | | Any StdTabController::queryAggregation( const Type & rType ) |
158 | 0 | { |
159 | 0 | Any aRet = ::cppu::queryInterface( rType, |
160 | 0 | static_cast< XTabController* >(this), |
161 | 0 | static_cast< XServiceInfo* >(this), |
162 | 0 | static_cast< XTypeProvider* >(this) ); |
163 | 0 | return (aRet.hasValue() ? aRet : OWeakAggObject::queryAggregation( rType )); |
164 | 0 | } |
165 | | |
166 | | IMPL_IMPLEMENTATION_ID( StdTabController ) |
167 | | |
168 | | // XTypeProvider |
169 | | css::uno::Sequence< css::uno::Type > StdTabController::getTypes() |
170 | 0 | { |
171 | 0 | static const css::uno::Sequence< css::uno::Type > aTypeList { |
172 | 0 | cppu::UnoType<css::lang::XTypeProvider>::get(), |
173 | 0 | cppu::UnoType<XTabController>::get(), |
174 | 0 | cppu::UnoType<XServiceInfo>::get() |
175 | 0 | }; |
176 | 0 | return aTypeList; |
177 | 0 | } |
178 | | |
179 | | void StdTabController::setModel( const Reference< XTabControllerModel >& Model ) |
180 | 0 | { |
181 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); |
182 | |
|
183 | 0 | mxModel = Model; |
184 | 0 | } |
185 | | |
186 | | Reference< XTabControllerModel > StdTabController::getModel( ) |
187 | 0 | { |
188 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); |
189 | |
|
190 | 0 | return mxModel; |
191 | 0 | } |
192 | | |
193 | | void StdTabController::setContainer( const Reference< XControlContainer >& Container ) |
194 | 0 | { |
195 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); |
196 | |
|
197 | 0 | mxControlContainer = Container; |
198 | 0 | } |
199 | | |
200 | | Reference< XControlContainer > StdTabController::getContainer( ) |
201 | 0 | { |
202 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); |
203 | |
|
204 | 0 | return mxControlContainer; |
205 | 0 | } |
206 | | |
207 | | Sequence< Reference< XControl > > StdTabController::getControls( ) |
208 | 0 | { |
209 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); |
210 | |
|
211 | 0 | Sequence< Reference< XControl > > aSeq; |
212 | |
|
213 | 0 | if ( mxControlContainer.is() ) |
214 | 0 | { |
215 | 0 | const Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels(); |
216 | |
|
217 | 0 | Sequence< Reference< XControl > > xCtrls = mxControlContainer->getControls(); |
218 | |
|
219 | 0 | sal_Int32 nCtrls = aModels.getLength(); |
220 | 0 | aSeq = Sequence< Reference< XControl > >( nCtrls ); |
221 | 0 | std::transform(aModels.begin(), aModels.end(), aSeq.getArray(), |
222 | 0 | [&xCtrls](const Reference< XControlModel >& xCtrlModel) -> Reference< XControl > { |
223 | 0 | return FindControl( xCtrls, xCtrlModel ); }); |
224 | 0 | } |
225 | 0 | return aSeq; |
226 | 0 | } |
227 | | |
228 | | namespace { |
229 | | |
230 | | struct ComponentEntry |
231 | | { |
232 | | css::awt::XWindow* pComponent; |
233 | | ::Point aPos; |
234 | | }; |
235 | | |
236 | | } |
237 | | |
238 | | void StdTabController::autoTabOrder( ) |
239 | 0 | { |
240 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); |
241 | |
|
242 | 0 | DBG_ASSERT( mxControlContainer.is(), "autoTabOrder: No ControlContainer!" ); |
243 | 0 | if ( !mxControlContainer.is() ) |
244 | 0 | return; |
245 | | |
246 | 0 | Sequence< Reference< XControlModel > > aSeq = mxModel->getControlModels(); |
247 | 0 | Sequence< Reference< XWindow > > aCompSeq; |
248 | | |
249 | | // This may return a TabController, which returns desired list of controls faster |
250 | 0 | 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 | 0 | if( !ImplCreateComponentSequence( aControls, aSeq, aCompSeq, nullptr, false ) ) |
255 | 0 | return; |
256 | | |
257 | 0 | sal_uInt32 nCtrls = aCompSeq.getLength(); |
258 | | |
259 | | // insert sort algorithm |
260 | 0 | std::vector< ComponentEntry > aCtrls; |
261 | 0 | aCtrls.reserve(nCtrls); |
262 | 0 | for (const Reference<XWindow>& rComponent : aCompSeq) |
263 | 0 | { |
264 | 0 | XWindow* pC = rComponent.get(); |
265 | 0 | ComponentEntry newEntry; |
266 | 0 | newEntry.pComponent = pC; |
267 | 0 | awt::Rectangle aPosSize = pC->getPosSize(); |
268 | 0 | newEntry.aPos.setX( aPosSize.X ); |
269 | 0 | newEntry.aPos.setY( aPosSize.Y ); |
270 | |
|
271 | 0 | decltype(aCtrls)::size_type nPos; |
272 | 0 | for ( nPos = 0; nPos < aCtrls.size(); nPos++ ) |
273 | 0 | { |
274 | 0 | ComponentEntry& rEntry = aCtrls[ nPos ]; |
275 | 0 | if ( ( rEntry.aPos.Y() > newEntry.aPos.Y() ) || |
276 | 0 | ( ( rEntry.aPos.Y() == newEntry.aPos.Y() ) && ( rEntry.aPos.X() > newEntry.aPos.X() ) ) ) |
277 | 0 | break; |
278 | 0 | } |
279 | 0 | if ( nPos < aCtrls.size() ) { |
280 | 0 | aCtrls.insert( aCtrls.begin() + nPos, newEntry ); |
281 | 0 | } else { |
282 | 0 | aCtrls.push_back( newEntry ); |
283 | 0 | } |
284 | 0 | } |
285 | |
|
286 | 0 | Sequence< Reference< XControlModel > > aNewSeq( nCtrls ); |
287 | 0 | std::transform(aCtrls.begin(), aCtrls.end(), aNewSeq.getArray(), |
288 | 0 | [](const ComponentEntry& rEntry) -> Reference< XControlModel > { |
289 | 0 | Reference< XControl > xUC( rEntry.pComponent, UNO_QUERY ); |
290 | 0 | return xUC->getModel(); |
291 | 0 | }); |
292 | |
|
293 | 0 | mxModel->setControlModels( aNewSeq ); |
294 | 0 | } |
295 | | |
296 | | void StdTabController::activateTabOrder( ) |
297 | 0 | { |
298 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); |
299 | | |
300 | | // Activate tab order for the control container |
301 | |
|
302 | 0 | Reference< XControl > xC( mxControlContainer, UNO_QUERY ); |
303 | 0 | Reference< XVclContainerPeer > xVclContainerPeer; |
304 | 0 | if ( xC.is() ) |
305 | 0 | xVclContainerPeer.set(xC->getPeer(), css::uno::UNO_QUERY); |
306 | 0 | if ( !xC.is() || !xVclContainerPeer.is() ) |
307 | 0 | 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 | 0 | Reference<XTabController> xTabController( static_cast<XTabController*>(this), UNO_QUERY ); |
312 | | |
313 | | // Get a flattened list of controls sequences |
314 | 0 | Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels(); |
315 | 0 | Sequence< Reference< XWindow > > aCompSeq; |
316 | 0 | 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 | 0 | 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 | 0 | if( !ImplCreateComponentSequence( aControls, aModels, aCompSeq, &aTabSeq, true ) ) |
326 | 0 | return; |
327 | | |
328 | 0 | xVclContainerPeer->setTabOrder( aCompSeq, aTabSeq, mxModel->getGroupControl() ); |
329 | |
|
330 | 0 | OUString aName; |
331 | 0 | Sequence< Reference< XControlModel > > aThisGroupModels; |
332 | 0 | Sequence< Reference< XWindow > > aControlComponents; |
333 | |
|
334 | 0 | sal_uInt32 nGroups = mxModel->getGroupCount(); |
335 | 0 | for ( sal_uInt32 nG = 0; nG < nGroups; nG++ ) |
336 | 0 | { |
337 | 0 | mxModel->getGroup( nG, aThisGroupModels, aName ); |
338 | |
|
339 | 0 | 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 | 0 | aControlComponents.realloc( 0 ); |
346 | |
|
347 | 0 | ImplCreateComponentSequence( aControls, aThisGroupModels, aControlComponents, nullptr, true ); |
348 | 0 | xVclContainerPeer->setGroup( aControlComponents ); |
349 | 0 | } |
350 | 0 | } |
351 | | |
352 | | void StdTabController::activateFirst( ) |
353 | 0 | { |
354 | 0 | SolarMutexGuard aSolarGuard; |
355 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary? |
356 | |
|
357 | 0 | ImplActivateControl( true ); |
358 | 0 | } |
359 | | |
360 | | void StdTabController::activateLast( ) |
361 | 0 | { |
362 | 0 | SolarMutexGuard aSolarGuard; |
363 | 0 | ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary? |
364 | |
|
365 | 0 | ImplActivateControl( false ); |
366 | 0 | } |
367 | | |
368 | | OUString StdTabController::getImplementationName() |
369 | 0 | { |
370 | 0 | return u"stardiv.Toolkit.StdTabController"_ustr; |
371 | 0 | } |
372 | | |
373 | | sal_Bool StdTabController::supportsService(OUString const & ServiceName) |
374 | 0 | { |
375 | 0 | return cppu::supportsService(this, ServiceName); |
376 | 0 | } |
377 | | |
378 | | css::uno::Sequence<OUString> StdTabController::getSupportedServiceNames() |
379 | 0 | { |
380 | 0 | return css::uno::Sequence<OUString>{ |
381 | 0 | u"com.sun.star.awt.TabController"_ustr, |
382 | 0 | u"stardiv.vcl.control.TabController"_ustr}; |
383 | 0 | } |
384 | | |
385 | | Reference< XControl > StdTabController::FindControl( Sequence< Reference< XControl > >& rCtrls, |
386 | | const Reference< XControlModel > & rxCtrlModel ) |
387 | 0 | { |
388 | 0 | if (!rxCtrlModel.is()) |
389 | 0 | throw lang::IllegalArgumentException(u"No valid XControlModel"_ustr, |
390 | 0 | uno::Reference<uno::XInterface>(), 0); |
391 | | |
392 | 0 | auto pCtrl = std::find_if(std::cbegin(rCtrls), std::cend(rCtrls), |
393 | 0 | [&rxCtrlModel](const Reference< XControl >& rCtrl) { |
394 | 0 | Reference< XControlModel > xModel(rCtrl.is() ? rCtrl->getModel() : Reference< XControlModel > ()); |
395 | 0 | return xModel.get() == rxCtrlModel.get(); |
396 | 0 | }); |
397 | 0 | if (pCtrl != std::cend(rCtrls)) |
398 | 0 | { |
399 | 0 | auto n = static_cast<sal_Int32>(std::distance(std::cbegin(rCtrls), pCtrl)); |
400 | 0 | Reference< XControl > xCtrl( *pCtrl ); |
401 | 0 | ::comphelper::removeElementAt( rCtrls, n ); |
402 | 0 | return xCtrl; |
403 | 0 | } |
404 | 0 | return Reference< XControl > (); |
405 | 0 | } |
406 | | |
407 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * |
408 | | stardiv_Toolkit_StdTabController_get_implementation( |
409 | | css::uno::XComponentContext *, |
410 | | css::uno::Sequence<css::uno::Any> const &) |
411 | 0 | { |
412 | 0 | return cppu::acquire(new StdTabController()); |
413 | 0 | } |
414 | | |
415 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |