summaryrefslogtreecommitdiff
path: root/toolkit/source/controls/stdtabcontroller.cxx
blob: 8ef6d84302f358118c43bcc48de8a8732c15e3f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/awt/XVclContainerPeer.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>

#include <toolkit/controls/stdtabcontroller.hxx>
#include <toolkit/helper/servicenames.hxx>
#include <toolkit/awt/vclxwindow.hxx>
#include <toolkit/helper/macros.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <cppuhelper/queryinterface.hxx>

#include <sal/log.hxx>
#include <tools/debug.hxx>
#include <vcl/svapp.hxx>
#include <vcl/window.hxx>
#include <comphelper/sequence.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::awt;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;



StdTabController::StdTabController()
{
}

StdTabController::~StdTabController()
{
}

bool StdTabController::ImplCreateComponentSequence(
        Sequence< Reference< XControl > >&              rControls,
        const Sequence< Reference< XControlModel > >&   rModels,
        Sequence< Reference< XWindow > >&               rComponents,
        Sequence< Any>*                                 pTabStops,
        bool bPeerComponent )
{
    // Get only the requested controls
    sal_Int32 nModels = rModels.getLength();
    if (nModels != rControls.getLength())
    {
        Sequence< Reference< XControl > > aSeq( nModels );
        Reference< XControl >  xCurrentControl;

        sal_Int32 nRealControls = 0;
        for (const Reference< XControlModel >& rModel : rModels)
        {
            xCurrentControl = FindControl(rControls, rModel);
            if (xCurrentControl.is())
                aSeq[nRealControls++] = xCurrentControl;
        }
        aSeq.realloc(nRealControls);
        rControls = aSeq;
    }

    // there may be less controls than models, but never more controls than models
    assert(rControls.getLength() <= rModels.getLength());

    sal_Int32 nCtrls = rControls.getLength();
    rComponents.realloc( nCtrls );
    Reference< XWindow > * pComps = rComponents.getArray();
    Any* pTabs = nullptr;


    if ( pTabStops )
    {
        *pTabStops = Sequence< Any>( nCtrls );
        pTabs = pTabStops->getArray();
    }

    bool bOK = true;
    for ( const Reference< XControl >& xCtrl : std::as_const(rControls) )
    {
        // Get the matching control for this model
        if ( !xCtrl.is() )
        {
            SAL_WARN("toolkit", "Control not found" );
            bOK = false;
            break;
        }

        if (bPeerComponent)
            pComps->set(xCtrl->getPeer(), UNO_QUERY);
        else
            pComps->set(xCtrl, UNO_QUERY);

        // TabStop-Property
        if ( pTabs )
        {
            // opt: Constant String for TabStop name
            static const char aTabStopName[] = "Tabstop";

            Reference< XPropertySet >  xPSet( xCtrl->getModel(), UNO_QUERY );
            Reference< XPropertySetInfo >  xInfo = xPSet->getPropertySetInfo();
            if( xInfo->hasPropertyByName( aTabStopName ) )
                *pTabs++ = xPSet->getPropertyValue( aTabStopName );
            else
                ++pTabs;
        }

        ++pComps;
    }
    return bOK;
}

void StdTabController::ImplActivateControl( bool bFirst ) const
{
    // HACK due to bug #53688#, map controls onto an interface if remote controls may occur
    Sequence< Reference< XControl > > aCtrls = const_cast<StdTabController*>(this)->getControls();
    const Reference< XControl > * pControls = aCtrls.getConstArray();
    sal_uInt32 nCount = aCtrls.getLength();

    for ( sal_uInt32 n = bFirst ? 0 : nCount; bFirst ? n < nCount : n != 0; )
    {
        sal_uInt32 nCtrl = bFirst ? n++ : --n;
        DBG_ASSERT( pControls[nCtrl].is(), "Control not in Container!" );
        if ( pControls[nCtrl].is() )
        {
            Reference< XWindowPeer >  xCP = pControls[nCtrl]->getPeer();
            if ( xCP.is() )
            {
                VCLXWindow* pC = comphelper::getUnoTunnelImplementation<VCLXWindow>( xCP );
                if ( pC && pC->GetWindow() && ( pC->GetWindow()->GetStyle() & WB_TABSTOP ) )
                {
                    pC->GetWindow()->GrabFocus();
                    break;
                }
            }
        }
    }
}

// XInterface
Any StdTabController::queryAggregation( const Type & rType )
{
    Any aRet = ::cppu::queryInterface( rType,
                                        static_cast< XTabController* >(this),
                                        static_cast< XServiceInfo* >(this),
                                        static_cast< XTypeProvider* >(this) );
    return (aRet.hasValue() ? aRet : OWeakAggObject::queryAggregation( rType ));
}

IMPL_IMPLEMENTATION_ID( StdTabController )

// XTypeProvider
css::uno::Sequence< css::uno::Type > StdTabController::getTypes()
{
    static const css::uno::Sequence< css::uno::Type > aTypeList {
        cppu::UnoType<css::lang::XTypeProvider>::get(),
        cppu::UnoType<XTabController>::get(),
        cppu::UnoType<XServiceInfo>::get()
    };
    return aTypeList;
}

void StdTabController::setModel( const Reference< XTabControllerModel >& Model )
{
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );

    mxModel = Model;
}

Reference< XTabControllerModel > StdTabController::getModel(  )
{
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );

    return mxModel;
}

void StdTabController::setContainer( const Reference< XControlContainer >& Container )
{
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );

    mxControlContainer = Container;
}

Reference< XControlContainer > StdTabController::getContainer(  )
{
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );

    return mxControlContainer;
}

Sequence< Reference< XControl > > StdTabController::getControls(  )
{
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );

    Sequence< Reference< XControl > > aSeq;

    if ( mxControlContainer.is() )
    {
        Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels();

        Sequence< Reference< XControl > > xCtrls = mxControlContainer->getControls();

        sal_Int32 nCtrls = aModels.getLength();
        aSeq = Sequence< Reference< XControl > >( nCtrls );
        std::transform(aModels.begin(), aModels.end(), aSeq.begin(),
            [&xCtrls](const Reference< XControlModel >& xCtrlModel) -> Reference< XControl > {
                return FindControl( xCtrls, xCtrlModel ); });
    }
    return aSeq;
}

namespace {

struct ComponentEntry
{
    css::awt::XWindow*  pComponent;
    ::Point             aPos;
};

}

void StdTabController::autoTabOrder(  )
{
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );

    DBG_ASSERT( mxControlContainer.is(), "autoTabOrder: No ControlContainer!" );
    if ( !mxControlContainer.is() )
        return;

    Sequence< Reference< XControlModel > > aSeq = mxModel->getControlModels();
    Sequence< Reference< XWindow > > aCompSeq;

    // This may return a TabController, which returns desired list of controls faster
    Sequence< Reference< XControl > > aControls = getControls();

    // #58317# Some Models may be missing from the Container. Plus there is a
    // autoTabOrder call later on.
    if( !ImplCreateComponentSequence( aControls, aSeq, aCompSeq, nullptr, false ) )
        return;

    sal_uInt32 nCtrls = aCompSeq.getLength();

    // insert sort algorithm
    std::vector< ComponentEntry > aCtrls;
    aCtrls.reserve(nCtrls);
    for ( const Reference< XWindow >& rComponent : std::as_const(aCompSeq) )
    {
        XWindow* pC = rComponent.get();
        ComponentEntry newEntry;
        newEntry.pComponent = pC;
        awt::Rectangle aPosSize = pC->getPosSize();
        newEntry.aPos.setX( aPosSize.X );
        newEntry.aPos.setY( aPosSize.Y );

        decltype(aCtrls)::size_type nPos;
        for ( nPos = 0; nPos < aCtrls.size(); nPos++ )
        {
            ComponentEntry& rEntry = aCtrls[ nPos ];
            if ( ( rEntry.aPos.Y() > newEntry.aPos.Y() ) ||
                 ( ( rEntry.aPos.Y() == newEntry.aPos.Y() ) && ( rEntry.aPos.X() > newEntry.aPos.X() ) ) )
                    break;
        }
        if ( nPos < aCtrls.size() ) {
            aCtrls.insert( aCtrls.begin() + nPos, newEntry );
        } else {
            aCtrls.push_back( newEntry );
        }
    }

    Sequence< Reference< XControlModel > > aNewSeq( nCtrls );
    std::transform(aCtrls.begin(), aCtrls.end(), aNewSeq.begin(),
        [](const ComponentEntry& rEntry) -> Reference< XControlModel > {
            Reference< XControl >  xUC( rEntry.pComponent, UNO_QUERY );
            return xUC->getModel();
        });

    mxModel->setControlModels( aNewSeq );
}

void StdTabController::activateTabOrder(  )
{
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );

    // Activate tab order for the control container

    Reference< XControl >  xC( mxControlContainer, UNO_QUERY );
    Reference< XVclContainerPeer >  xVclContainerPeer;
    if ( xC.is() )
        xVclContainerPeer.set(xC->getPeer(), css::uno::UNO_QUERY);
    if ( !xC.is() || !xVclContainerPeer.is() )
        return;

    // Get a flattened list of controls sequences
    Sequence< Reference< XControlModel > > aModels = mxModel->getControlModels();
    Sequence< Reference< XWindow > > aCompSeq;
    Sequence< Any> aTabSeq;

    // Previously used aControls = xTabController->getControls() "for the sake of optimization",
    // but that list isn't valid during the creation phase (missing last created control) because
    // listenermultiplexer.cxx handles fmvwimp::elementinserted before formcontroller::elementInserted
    // Perhaps other places using the same optimization need to be reviewed?  (tdf#125609)
    Sequence< Reference< XControl > > aCachedControls = getControls();
    Sequence< Reference< XControl > > aControls = aCachedControls;

    // #58317# Some Models may be missing from the Container. Plus there is a
    // autoTabOrder call later on.
    if( !ImplCreateComponentSequence( aControls, aModels, aCompSeq, &aTabSeq, true ) )
        return;

    xVclContainerPeer->setTabOrder( aCompSeq, aTabSeq, mxModel->getGroupControl() );

    OUString aName;
    Sequence< Reference< XControlModel > >  aThisGroupModels;
    Sequence< Reference< XWindow > >        aControlComponents;

    sal_uInt32 nGroups = mxModel->getGroupCount();
    for ( sal_uInt32 nG = 0; nG < nGroups; nG++ )
    {
        mxModel->getGroup( nG, aThisGroupModels, aName );

        aControls = aCachedControls;
            // ImplCreateComponentSequence has a really strange semantics regarding it's first parameter:
            // upon method entry, it expects a super set of the controls which it returns
            // this means we need to completely fill this sequence with all available controls before
            // calling into ImplCreateComponentSequence

        aControlComponents.realloc( 0 );

        ImplCreateComponentSequence( aControls, aThisGroupModels, aControlComponents, nullptr, true );
        xVclContainerPeer->setGroup( aControlComponents );
    }
}

void StdTabController::activateFirst(  )
{
    SolarMutexGuard aSolarGuard;
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?

    ImplActivateControl( true );
}

void StdTabController::activateLast(  )
{
    SolarMutexGuard aSolarGuard;
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() ); //TODO: necessary?

    ImplActivateControl( false );
}

OUString StdTabController::getImplementationName()
{
    return "stardiv.Toolkit.StdTabController";
}

sal_Bool StdTabController::supportsService(OUString const & ServiceName)
{
    return cppu::supportsService(this, ServiceName);
}

css::uno::Sequence<OUString> StdTabController::getSupportedServiceNames()
{
    return css::uno::Sequence<OUString>{
        OUString::createFromAscii(szServiceName2_TabController),
        "stardiv.vcl.control.TabController"};
}

Reference< XControl >  StdTabController::FindControl( Sequence< Reference< XControl > >& rCtrls,
 const Reference< XControlModel > & rxCtrlModel )
{
    if (!rxCtrlModel.is())
        throw lang::IllegalArgumentException("No valid XControlModel",
                                             uno::Reference<uno::XInterface>(), 0);

    auto pCtrl = std::find_if(rCtrls.begin(), rCtrls.end(),
        [&rxCtrlModel](const Reference< XControl >& rCtrl) {
            Reference< XControlModel >  xModel(rCtrl.is() ? rCtrl->getModel() : Reference< XControlModel > ());
            return xModel.get() == rxCtrlModel.get();
        });
    if (pCtrl != rCtrls.end())
    {
        auto n = static_cast<sal_Int32>(std::distance(rCtrls.begin(), pCtrl));
        Reference< XControl >  xCtrl( *pCtrl );
        ::comphelper::removeElementAt( rCtrls, n );
        return xCtrl;
    }
    return Reference< XControl > ();
}

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
stardiv_Toolkit_StdTabController_get_implementation(
    css::uno::XComponentContext *,
    css::uno::Sequence<css::uno::Any> const &)
{
    return cppu::acquire(new StdTabController());
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */