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