LibreOffice Module sw (master) 1
accselectionhelper.cxx
Go to the documentation of this file.
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/accessibility/XAccessibleSelection.hpp>
22
23#include "acccontext.hxx"
24#include <accmap.hxx>
25#include <o3tl/safeint.hxx>
27#include <viewsh.hxx>
28#include <fesh.hxx>
29#include <vcl/svapp.hxx>
30#include <flyfrm.hxx>
31
32#include <com/sun/star/uno/Reference.hxx>
33#include <com/sun/star/accessibility/AccessibleStateType.hpp>
34#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
35#include <fmtanchr.hxx>
36
37using namespace ::com::sun::star::accessibility;
38using namespace ::com::sun::star;
39using namespace ::com::sun::star::uno;
40
41using ::com::sun::star::accessibility::XAccessible;
42using ::com::sun::star::accessibility::XAccessibleContext;
43using ::com::sun::star::accessibility::XAccessibleSelection;
44
45using namespace ::sw::access;
46
48 SwAccessibleContext& rContext ) :
49 m_rContext( rContext )
50{
51}
52
54{
55 OSL_ENSURE( m_rContext.GetMap() != nullptr, "no map?" );
56 SwViewShell* pViewShell = m_rContext.GetMap()->GetShell();
57 OSL_ENSURE( pViewShell != nullptr,
58 "No view shell? Then what are you looking at?" );
59
60 SwFEShell* pFEShell = dynamic_cast<SwFEShell*>( pViewShell );
61
62 return pFEShell;
63}
64
66{
67 Reference < XAccessibleContext > xThis( &m_rContext );
68 Reference < XAccessibleSelection >xSelThis( xThis, UNO_QUERY );
69 lang::IndexOutOfBoundsException aExcept(
70 "index out of bounds",
71 xSelThis );
72 throw aExcept;
73}
74
75// XAccessibleSelection
77 sal_Int64 nChildIndex )
78{
79 SolarMutexGuard aGuard;
80
81 if (nChildIndex < 0 || nChildIndex >= m_rContext.GetChildCount(*(m_rContext.GetMap())))
83
84 // Get the respective child as SwFrame (also do index checking), ...
85 const SwAccessibleChild aChild = m_rContext.GetChild( *(m_rContext.GetMap()),
86 nChildIndex );
87 if( !aChild.IsValid() )
89
90 // we can only select fly frames, so we ignore (should: return
91 // false) all other attempts at child selection
92 if (GetFEShell())
93 {
94 const SdrObject *pObj = aChild.GetDrawObject();
95 if( pObj )
96 m_rContext.Select( const_cast< SdrObject *>( pObj ), nullptr==aChild.GetSwFrame());
97 }
98 // no frame shell, or no frame, or no fly frame -> can't select
99}
100
101//When the selected state of the SwFrameOrObj is set, return true.
102static bool lcl_getSelectedState(const SwAccessibleChild& aChild,
103 SwAccessibleContext* pContext,
104 SwAccessibleMap* pMap)
105{
106 Reference< XAccessible > xAcc;
107 if ( aChild.GetSwFrame() )
108 {
109 xAcc = pMap->GetContext( aChild.GetSwFrame(), false );
110 }
111 else if ( aChild.GetDrawObject() )
112 {
113 xAcc = pMap->GetContext( aChild.GetDrawObject(), pContext, false );
114 }
115
116 if( xAcc.is() )
117 {
118 Reference< XAccessibleContext > pRContext = xAcc->getAccessibleContext();
119 if(!pRContext.is())
120 return false;
121 sal_Int64 nRStateSet = pRContext->getAccessibleStateSet();
122 if(nRStateSet & AccessibleStateType::SELECTED)
123 return true;
124 }
125 return false;
126}
127
129 sal_Int64 nChildIndex )
130{
131 SolarMutexGuard aGuard;
132
133 if (nChildIndex < 0 || nChildIndex >= m_rContext.GetChildCount(*(m_rContext.GetMap())))
135
136 // Get the respective child as SwFrame (also do index checking), ...
137 const SwAccessibleChild aChild = m_rContext.GetChild( *(m_rContext.GetMap()),
138 nChildIndex );
139 if( !aChild.IsValid() )
141
142 // ... and compare to the currently selected frame
143 bool bRet = false;
144 if (const SwFEShell* pFEShell = GetFEShell())
145 {
146 if ( aChild.GetSwFrame() != nullptr )
147 {
148 bRet = (pFEShell->GetSelectedFlyFrame() == aChild.GetSwFrame());
149 }
150 else if ( aChild.GetDrawObject() )
151 {
152 bRet = pFEShell->IsObjSelected( *aChild.GetDrawObject() );
153 }
154 //If the SwFrameOrObj is not selected directly in the UI, we should check whether it is selected in the selection cursor.
155 if( !bRet )
156 {
158 bRet = true;
159 }
160 }
161
162 return bRet;
163}
164
166{
167 SolarMutexGuard aGuard;
168
169 // We can select only one. So iterate over the children to find
170 // the first we can select, and select it.
171
172 SwFEShell* pFEShell = GetFEShell();
173 if (!pFEShell)
174 return;
175
176 std::list< SwAccessibleChild > aChildren;
177 m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
178
179 for( const SwAccessibleChild& rChild : aChildren )
180 {
181 const SdrObject* pObj = rChild.GetDrawObject();
182 const SwFrame* pFrame = rChild.GetSwFrame();
183 if( pObj && !(pFrame != nullptr && pFEShell->IsObjSelected()) )
184 {
185 m_rContext.Select( const_cast< SdrObject *>( pObj ), nullptr==pFrame );
186 if( pFrame )
187 break;
188 }
189 }
190}
191
193{
194 SolarMutexGuard aGuard;
195
196 sal_Int64 nCount = 0;
197 // Only one frame can be selected at a time, and we only frames
198 // for selectable children.
199 if (const SwFEShell* pFEShell = GetFEShell())
200 {
201 const SwFlyFrame* pFlyFrame = pFEShell->GetSelectedFlyFrame();
202 if( pFlyFrame )
203 {
204 nCount = 1;
205 }
206 else
207 {
208 const size_t nSelObjs = pFEShell->IsObjSelected();
209 if( nSelObjs > 0 )
210 {
211 std::list< SwAccessibleChild > aChildren;
212 m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
213
214 for( const SwAccessibleChild& rChild : aChildren )
215 {
216 if( rChild.GetDrawObject() && !rChild.GetSwFrame() &&
218 == m_rContext.GetFrame() &&
219 pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
220 {
221 nCount++;
222 }
223 if (o3tl::make_unsigned(nCount) >= nSelObjs)
224 break;
225 }
226 }
227 }
228 //If the SwFrameOrObj is not selected directly in the UI,
229 //we should check whether it is selected in the selection cursor.
230 if( nCount == 0 )
231 {
232 std::list< SwAccessibleChild > aChildren;
233 m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
234 nCount = static_cast<sal_Int32>(std::count_if(aChildren.begin(), aChildren.end(),
235 [this](const SwAccessibleChild& aChild) { return lcl_getSelectedState(aChild, &m_rContext, m_rContext.GetMap()); }));
236 }
237 }
238 return nCount;
239}
240
242 sal_Int64 nSelectedChildIndex )
243{
244 SolarMutexGuard aGuard;
245
246 // Since the index is relative to the selected children, and since
247 // there can be at most one selected frame child, the index must
248 // be 0, and a selection must exist, otherwise we have to throw an
249 // lang::IndexOutOfBoundsException
250 SwFEShell* pFEShell = GetFEShell();
251 if (!pFEShell)
253
254 SwAccessibleChild aChild;
255 const SwFlyFrame *pFlyFrame = pFEShell->GetSelectedFlyFrame();
256 if( pFlyFrame )
257 {
258 if( 0 == nSelectedChildIndex )
259 {
260 if(SwAccessibleFrame::GetParent( SwAccessibleChild(pFlyFrame), m_rContext.IsInPagePreview()) == m_rContext.GetFrame() )
261 {
262 aChild = pFlyFrame;
263 }
264 else
265 {
266 const SwFrameFormat *pFrameFormat = pFlyFrame->GetFormat();
267 if (pFrameFormat)
268 {
269 const SwFormatAnchor& rAnchor = pFrameFormat->GetAnchor();
270 if( rAnchor.GetAnchorId() == RndStdIds::FLY_AS_CHAR )
271 {
272 const SwFrame *pParaFrame = SwAccessibleFrame::GetParent( SwAccessibleChild(pFlyFrame), m_rContext.IsInPagePreview() );
273 aChild = pParaFrame;
274 }
275 }
276 }
277 }
278 }
279 else
280 {
281 const size_t nSelObjs = pFEShell->IsObjSelected();
282 if( 0 == nSelObjs || o3tl::make_unsigned(nSelectedChildIndex) >= nSelObjs )
284
285 std::list< SwAccessibleChild > aChildren;
286 m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
287
288 for( const SwAccessibleChild& rChild : aChildren )
289 {
290 if( rChild.GetDrawObject() && !rChild.GetSwFrame() &&
293 pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
294 {
295 if( 0 == nSelectedChildIndex )
296 aChild = rChild;
297 else
298 --nSelectedChildIndex;
299 }
300 if (aChild.IsValid())
301 break;
302 }
303 }
304
305 if( !aChild.IsValid() )
307
308 OSL_ENSURE( m_rContext.GetMap() != nullptr, "We need the map." );
309 Reference< XAccessible > xChild;
310 if( aChild.GetSwFrame() )
311 {
313 m_rContext.GetMap()->GetContextImpl( aChild.GetSwFrame() ) );
314 if( xChildImpl.is() )
315 {
316 xChildImpl->SetParent( &m_rContext );
317 xChild = xChildImpl.get();
318 }
319 }
320 else if ( aChild.GetDrawObject() )
321 {
323 m_rContext.GetMap()->GetContextImpl( aChild.GetDrawObject(),
324 &m_rContext ) );
325 if( xChildImpl.is() )
326 xChild = xChildImpl.get();
327 }
328 return xChild;
329}
330
331// index has to be treated as global child index.
333 sal_Int64 nChildIndex )
334{
336
337 if( nChildIndex < 0 ||
338 nChildIndex >= m_rContext.GetChildCount( *(m_rContext.GetMap()) ) )
340}
341
342/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool lcl_getSelectedState(const SwAccessibleChild &aChild, SwAccessibleContext *pContext, SwAccessibleMap *pMap)
SwAccessibleMap * GetMap()
Definition: acccontext.hxx:112
bool Select(SwPaM *pPaM, SdrObject *pObj, bool bAdd)
bool IsInPagePreview() const
Definition: accframe.hxx:86
static void GetChildren(SwAccessibleMap &rAccMap, const SwRect &rVisArea, const SwFrame &rFrame, std::list< sw::access::SwAccessibleChild > &rChildren, bool bInPagePreview)
Definition: accframe.cxx:277
static sal_Int32 GetChildCount(SwAccessibleMap &rAccMap, const SwRect &rVisArea, const SwFrame *pFrame, bool bInPagePreviewr)
Definition: accframe.cxx:41
static sw::access::SwAccessibleChild GetChild(SwAccessibleMap &rAccMap, const SwRect &rVisArea, const SwFrame &rFrame, sal_Int32 &rPos, bool bInPagePreview)
Definition: accframe.cxx:71
const SwFrame * GetFrame() const
Definition: accframe.hxx:102
const SwFrame * GetParent() const
Definition: accframe.hxx:154
::rtl::Reference< SwAccessibleContext > GetContextImpl(const SwFrame *pFrame, bool bCreate=true)
Definition: accmap.cxx:1914
css::uno::Reference< css::accessibility::XAccessible > GetContext(const SwFrame *pFrame, bool bCreate=true)
Definition: accmap.cxx:1789
SwViewShell * GetShell() const
Definition: accmap.hxx:173
SwAccessibleContext & m_rContext
the context on which this helper works
SwAccessibleSelectionHelper(SwAccessibleContext &rContext)
bool isAccessibleChildSelected(sal_Int64 nChildIndex)
void selectAccessibleChild(sal_Int64 nChildIndex)
SwFEShell * GetFEShell()
get FE-Shell
css::uno::Reference< css::accessibility::XAccessible > getSelectedAccessibleChild(sal_Int64 nSelectedChildIndex)
void deselectAccessibleChild(sal_Int64 nChildIndex)
SwFlyFrame * GetSelectedFlyFrame() const
Definition: fefly1.cxx:277
size_t IsObjSelected() const
Definition: feshview.cxx:1125
general base class for all free-flowing frames
Definition: flyfrm.hxx:79
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:3119
FlyAnchors.
Definition: fmtanchr.hxx:37
RndStdIds GetAnchorId() const
Definition: fmtanchr.hxx:67
const SwFormatAnchor & GetAnchor(bool=true) const
Definition: fmtanchr.hxx:88
Style of a layout element.
Definition: frmfmt.hxx:72
Base class of the Writer layout elements.
Definition: frame.hxx:315
int nCount
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
const Reference< XComponentContext > & m_rContext