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