summaryrefslogtreecommitdiffstats
path: root/sw/source/core/access/accselectionhelper.cxx
blob: be92d4a2c5aa365ba30f6f40f08185146a67d1f0 (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
/* -*- 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/accessibility/XAccessibleSelection.hpp>
#include "accselectionhelper.hxx"

#include "acccontext.hxx"
#include <accmap.hxx>
#include <o3tl/safeint.hxx>
#include <svx/AccessibleShape.hxx>
#include <viewsh.hxx>
#include <fesh.hxx>
#include <vcl/svapp.hxx>
#include <flyfrm.hxx>

#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/accessibility/AccessibleStateType.hpp>
#include <com/sun/star/accessibility/XAccessibleStateSet.hpp>
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
#include <fmtanchr.hxx>

using namespace ::com::sun::star::accessibility;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

using ::com::sun::star::accessibility::XAccessible;
using ::com::sun::star::accessibility::XAccessibleContext;
using ::com::sun::star::accessibility::XAccessibleSelection;

using namespace ::sw::access;

SwAccessibleSelectionHelper::SwAccessibleSelectionHelper(
    SwAccessibleContext& rContext ) :
        m_rContext( rContext )
{
}

SwAccessibleSelectionHelper::~SwAccessibleSelectionHelper()
{
}

SwFEShell* SwAccessibleSelectionHelper::GetFEShell()
{
    OSL_ENSURE( m_rContext.GetMap() != nullptr, "no map?" );
    SwViewShell* pViewShell = m_rContext.GetMap()->GetShell();
    OSL_ENSURE( pViewShell != nullptr,
                "No view shell? Then what are you looking at?" );

    SwFEShell* pFEShell = dynamic_cast<SwFEShell*>( pViewShell );

    return pFEShell;
}

void SwAccessibleSelectionHelper::throwIndexOutOfBoundsException()
{
    Reference < XAccessibleContext > xThis( &m_rContext );
    Reference < XAccessibleSelection >xSelThis( xThis, UNO_QUERY );
    lang::IndexOutOfBoundsException aExcept(
                "index out of bounds",
                xSelThis );
    throw aExcept;
}

// XAccessibleSelection
void SwAccessibleSelectionHelper::selectAccessibleChild(
    sal_Int32 nChildIndex )
{
    SolarMutexGuard aGuard;

    // Get the respective child as SwFrame (also do index checking), ...
    const SwAccessibleChild aChild = m_rContext.GetChild( *(m_rContext.GetMap()),
                                                        nChildIndex );
    if( !aChild.IsValid() )
        throwIndexOutOfBoundsException();

    // we can only select fly frames, so we ignore (should: return
    // false) all other attempts at child selection
    SwFEShell* pFEShell = GetFEShell();
    if( pFEShell != nullptr )
    {
        const SdrObject *pObj = aChild.GetDrawObject();
        if( pObj )
            m_rContext.Select( const_cast< SdrObject *>( pObj ), nullptr==aChild.GetSwFrame());
    }
    // no frame shell, or no frame, or no fly frame -> can't select
}

//When the selected state of the SwFrameOrObj is set, return true.
static bool lcl_getSelectedState(const SwAccessibleChild& aChild,
                                     SwAccessibleContext* pContext,
                                     SwAccessibleMap* pMap)
{
    Reference< XAccessible > xAcc;
    if ( aChild.GetSwFrame() )
    {
        xAcc = pMap->GetContext( aChild.GetSwFrame(), false );
    }
    else if ( aChild.GetDrawObject() )
    {
        xAcc = pMap->GetContext( aChild.GetDrawObject(), pContext, false );
    }

    if( xAcc.is() )
    {
        Reference< XAccessibleContext > pRContext = xAcc->getAccessibleContext();
        if(!pRContext.is())
            return false;
        Reference<XAccessibleStateSet> pRStateSet = pRContext->getAccessibleStateSet();
        if( pRStateSet.is() )
        {
            Sequence<short> aStates = pRStateSet->getStates();
            if (std::find(aStates.begin(), aStates.end(), AccessibleStateType::SELECTED) != aStates.end())
                return true;
        }
    }
    return false;
}

bool SwAccessibleSelectionHelper::isAccessibleChildSelected(
    sal_Int32 nChildIndex )
{
    SolarMutexGuard aGuard;

    // Get the respective child as SwFrame (also do index checking), ...
    const SwAccessibleChild aChild = m_rContext.GetChild( *(m_rContext.GetMap()),
                                                        nChildIndex );
    if( !aChild.IsValid() )
        throwIndexOutOfBoundsException();

    // ... and compare to the currently selected frame
    bool bRet = false;
    const SwFEShell* pFEShell = GetFEShell();
    if( pFEShell )
    {
        if ( aChild.GetSwFrame() != nullptr )
        {
            bRet = (pFEShell->GetSelectedFlyFrame() == aChild.GetSwFrame());
        }
        else if ( aChild.GetDrawObject() )
        {
            bRet = pFEShell->IsObjSelected( *aChild.GetDrawObject() );
        }
        //If the SwFrameOrObj is not selected directly in the UI, we should check whether it is selected in the selection cursor.
        if( !bRet )
        {
            if( lcl_getSelectedState( aChild, &m_rContext, m_rContext.GetMap() ) )
                bRet = true;
        }
    }

    return bRet;
}

void SwAccessibleSelectionHelper::selectAllAccessibleChildren(  )
{
    SolarMutexGuard aGuard;

    // We can select only one. So iterate over the children to find
    // the first we can select, and select it.

    SwFEShell* pFEShell = GetFEShell();
    if( pFEShell )
    {
        std::list< SwAccessibleChild > aChildren;
        m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );

        for( const SwAccessibleChild& rChild : aChildren )
        {
            const SdrObject* pObj = rChild.GetDrawObject();
            const SwFrame* pFrame = rChild.GetSwFrame();
            if( pObj && !(pFrame != nullptr && pFEShell->IsObjSelected()) )
            {
                m_rContext.Select( const_cast< SdrObject *>( pObj ), nullptr==pFrame );
                if( pFrame )
                    break;
            }
        }
    }
}

sal_Int32 SwAccessibleSelectionHelper::getSelectedAccessibleChildCount(  )
{
    SolarMutexGuard aGuard;

    sal_Int32 nCount = 0;
    // Only one frame can be selected at a time, and we only frames
    // for selectable children.
    const SwFEShell* pFEShell = GetFEShell();
    if( pFEShell != nullptr )
    {
        const SwFlyFrame* pFlyFrame = pFEShell->GetSelectedFlyFrame();
        if( pFlyFrame )
        {
            nCount = 1;
        }
        else
        {
            const size_t nSelObjs = pFEShell->IsObjSelected();
            if( nSelObjs > 0 )
            {
                std::list< SwAccessibleChild > aChildren;
                m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );

                for( const SwAccessibleChild& rChild : aChildren )
                {
                    if( rChild.GetDrawObject() && !rChild.GetSwFrame() &&
                        SwAccessibleFrame::GetParent(rChild, m_rContext.IsInPagePreview())
                           == m_rContext.GetFrame() &&
                        pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
                    {
                        nCount++;
                    }
                    if (o3tl::make_unsigned(nCount) >= nSelObjs)
                        break;
                }
            }
        }
        //If the SwFrameOrObj is not selected directly in the UI,
        //we should check whether it is selected in the selection cursor.
        if( nCount == 0 )
        {
            std::list< SwAccessibleChild > aChildren;
            m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
            nCount = static_cast<sal_Int32>(std::count_if(aChildren.begin(), aChildren.end(),
                [this](const SwAccessibleChild& aChild) { return lcl_getSelectedState(aChild, &m_rContext, m_rContext.GetMap()); }));
        }
    }
    return nCount;
}

Reference<XAccessible> SwAccessibleSelectionHelper::getSelectedAccessibleChild(
    sal_Int32 nSelectedChildIndex )
{
    SolarMutexGuard aGuard;

    // Since the index is relative to the selected children, and since
    // there can be at most one selected frame child, the index must
    // be 0, and a selection must exist, otherwise we have to throw an
    // lang::IndexOutOfBoundsException
    SwFEShell* pFEShell = GetFEShell();
    if( nullptr == pFEShell )
        throwIndexOutOfBoundsException();

    SwAccessibleChild aChild;
    const SwFlyFrame *pFlyFrame = pFEShell->GetSelectedFlyFrame();
    if( pFlyFrame )
    {
        if( 0 == nSelectedChildIndex )
        {
            if(SwAccessibleFrame::GetParent( SwAccessibleChild(pFlyFrame), m_rContext.IsInPagePreview()) == m_rContext.GetFrame() )
            {
                aChild = pFlyFrame;
            }
            else
            {
                const SwFrameFormat *pFrameFormat = pFlyFrame->GetFormat();
                if (pFrameFormat)
                {
                    const SwFormatAnchor& rAnchor = pFrameFormat->GetAnchor();
                    if( rAnchor.GetAnchorId() == RndStdIds::FLY_AS_CHAR )
                    {
                        const SwFrame *pParaFrame = SwAccessibleFrame::GetParent( SwAccessibleChild(pFlyFrame), m_rContext.IsInPagePreview() );
                        aChild = pParaFrame;
                    }
                }
            }
        }
    }
    else
    {
        const size_t nSelObjs = pFEShell->IsObjSelected();
        if( 0 == nSelObjs || o3tl::make_unsigned(nSelectedChildIndex) >= nSelObjs )
            throwIndexOutOfBoundsException();

        std::list< SwAccessibleChild > aChildren;
        m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );

        for( const SwAccessibleChild& rChild : aChildren )
        {
            if( rChild.GetDrawObject() && !rChild.GetSwFrame() &&
                SwAccessibleFrame::GetParent(rChild, m_rContext.IsInPagePreview()) ==
                    m_rContext.GetFrame() &&
                pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
            {
                if( 0 == nSelectedChildIndex )
                    aChild = rChild;
                else
                    --nSelectedChildIndex;
            }
            if (aChild.IsValid())
                break;
        }
    }

    if( !aChild.IsValid() )
        throwIndexOutOfBoundsException();

    OSL_ENSURE( m_rContext.GetMap() != nullptr, "We need the map." );
    Reference< XAccessible > xChild;
    if( aChild.GetSwFrame() )
    {
        ::rtl::Reference < SwAccessibleContext > xChildImpl(
                m_rContext.GetMap()->GetContextImpl( aChild.GetSwFrame() ) );
        if( xChildImpl.is() )
        {
            xChildImpl->SetParent( &m_rContext );
            xChild = xChildImpl.get();
        }
    }
    else if ( aChild.GetDrawObject() )
    {
        ::rtl::Reference < ::accessibility::AccessibleShape > xChildImpl(
                m_rContext.GetMap()->GetContextImpl( aChild.GetDrawObject(),
                                          &m_rContext )  );
        if( xChildImpl.is() )
            xChild = xChildImpl.get();
    }
    return xChild;
}

// index has to be treated as global child index.
void SwAccessibleSelectionHelper::deselectAccessibleChild(
    sal_Int32 nChildIndex )
{
    SolarMutexGuard g;

    if( nChildIndex < 0 ||
        nChildIndex >= m_rContext.GetChildCount( *(m_rContext.GetMap()) ) )
        throwIndexOutOfBoundsException();
}

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