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