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