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