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