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