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  sal_Int32 count = aStates.getLength();
128  for( sal_Int32 i = 0; i < count; i++ )
129  {
130  if( aStates[i] == AccessibleStateType::SELECTED)
131  return true;
132  }
133  }
134  }
135  return false;
136 }
137 
139  sal_Int32 nChildIndex )
140 {
141  SolarMutexGuard aGuard;
142 
143  // Get the respective child as SwFrame (also do index checking), ...
144  const SwAccessibleChild aChild = m_rContext.GetChild( *(m_rContext.GetMap()),
145  nChildIndex );
146  if( !aChild.IsValid() )
148 
149  // ... and compare to the currently selected frame
150  bool bRet = false;
151  const SwFEShell* pFEShell = GetFEShell();
152  if( pFEShell )
153  {
154  if ( aChild.GetSwFrame() != nullptr )
155  {
156  bRet = (pFEShell->GetSelectedFlyFrame() == aChild.GetSwFrame());
157  }
158  else if ( aChild.GetDrawObject() )
159  {
160  bRet = pFEShell->IsObjSelected( *aChild.GetDrawObject() );
161  }
162  //If the SwFrameOrObj is not selected directly in the UI, we should check whether it is selected in the selection cursor.
163  if( !bRet )
164  {
165  if( lcl_getSelectedState( aChild, &m_rContext, m_rContext.GetMap() ) )
166  bRet = true;
167  }
168  }
169 
170  return bRet;
171 }
172 
174 {
175  SolarMutexGuard aGuard;
176 
177  // We can select only one. So iterate over the children to find
178  // the first we can select, and select it.
179 
180  SwFEShell* pFEShell = GetFEShell();
181  if( pFEShell )
182  {
183  std::list< SwAccessibleChild > aChildren;
184  m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
185 
186  for( const SwAccessibleChild& rChild : aChildren )
187  {
188  const SdrObject* pObj = rChild.GetDrawObject();
189  const SwFrame* pFrame = rChild.GetSwFrame();
190  if( pObj && !(pFrame != nullptr && pFEShell->IsObjSelected()) )
191  {
192  m_rContext.Select( const_cast< SdrObject *>( pObj ), nullptr==pFrame );
193  if( pFrame )
194  break;
195  }
196  }
197  }
198 }
199 
201 {
202  SolarMutexGuard aGuard;
203 
204  sal_Int32 nCount = 0;
205  // Only one frame can be selected at a time, and we only frames
206  // for selectable children.
207  const SwFEShell* pFEShell = GetFEShell();
208  if( pFEShell != nullptr )
209  {
210  const SwFlyFrame* pFlyFrame = pFEShell->GetSelectedFlyFrame();
211  if( pFlyFrame )
212  {
213  nCount = 1;
214  }
215  else
216  {
217  const size_t nSelObjs = pFEShell->IsObjSelected();
218  if( nSelObjs > 0 )
219  {
220  std::list< SwAccessibleChild > aChildren;
221  m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
222 
223  for( const SwAccessibleChild& rChild : aChildren )
224  {
225  if( rChild.GetDrawObject() && !rChild.GetSwFrame() &&
227  == m_rContext.GetFrame() &&
228  pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
229  {
230  nCount++;
231  }
232  if (static_cast<size_t>(nCount) >= nSelObjs)
233  break;
234  }
235  }
236  }
237  //If the SwFrameOrObj is not selected directly in the UI,
238  //we should check whether it is selected in the selection cursor.
239  if( nCount == 0 )
240  {
241  std::list< SwAccessibleChild > aChildren;
242  m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
243  nCount = static_cast<sal_Int32>(std::count_if(aChildren.begin(), aChildren.end(),
244  [this](const SwAccessibleChild& aChild) { return lcl_getSelectedState(aChild, &m_rContext, m_rContext.GetMap()); }));
245  }
246  }
247  return nCount;
248 }
249 
251  sal_Int32 nSelectedChildIndex )
252 {
253  SolarMutexGuard aGuard;
254 
255  // Since the index is relative to the selected children, and since
256  // there can be at most one selected frame child, the index must
257  // be 0, and a selection must exist, otherwise we have to throw an
258  // lang::IndexOutOfBoundsException
259  SwFEShell* pFEShell = GetFEShell();
260  if( nullptr == pFEShell )
262 
263  SwAccessibleChild aChild;
264  const SwFlyFrame *pFlyFrame = pFEShell->GetSelectedFlyFrame();
265  if( pFlyFrame )
266  {
267  if( 0 == nSelectedChildIndex )
268  {
269  if(SwAccessibleFrame::GetParent( SwAccessibleChild(pFlyFrame), m_rContext.IsInPagePreview()) == m_rContext.GetFrame() )
270  {
271  aChild = pFlyFrame;
272  }
273  else
274  {
275  const SwFrameFormat *pFrameFormat = pFlyFrame->GetFormat();
276  if (pFrameFormat)
277  {
278  const SwFormatAnchor& rAnchor = pFrameFormat->GetAnchor();
279  if( rAnchor.GetAnchorId() == RndStdIds::FLY_AS_CHAR )
280  {
281  const SwFrame *pParaFrame = SwAccessibleFrame::GetParent( SwAccessibleChild(pFlyFrame), m_rContext.IsInPagePreview() );
282  aChild = pParaFrame;
283  }
284  }
285  }
286  }
287  }
288  else
289  {
290  const size_t nSelObjs = pFEShell->IsObjSelected();
291  if( 0 == nSelObjs || static_cast<size_t>(nSelectedChildIndex) >= nSelObjs )
293 
294  std::list< SwAccessibleChild > aChildren;
295  m_rContext.GetChildren( *(m_rContext.GetMap()), aChildren );
296 
297  for( const SwAccessibleChild& rChild : aChildren )
298  {
299  if( rChild.GetDrawObject() && !rChild.GetSwFrame() &&
301  m_rContext.GetFrame() &&
302  pFEShell->IsObjSelected( *rChild.GetDrawObject() ) )
303  {
304  if( 0 == nSelectedChildIndex )
305  aChild = rChild;
306  else
307  --nSelectedChildIndex;
308  }
309  if (aChild.IsValid())
310  break;
311  }
312  }
313 
314  if( !aChild.IsValid() )
316 
317  OSL_ENSURE( m_rContext.GetMap() != nullptr, "We need the map." );
318  Reference< XAccessible > xChild;
319  if( aChild.GetSwFrame() )
320  {
322  m_rContext.GetMap()->GetContextImpl( aChild.GetSwFrame() ) );
323  if( xChildImpl.is() )
324  {
325  xChildImpl->SetParent( &m_rContext );
326  xChild = xChildImpl.get();
327  }
328  }
329  else if ( aChild.GetDrawObject() )
330  {
332  m_rContext.GetMap()->GetContextImpl( aChild.GetDrawObject(),
333  &m_rContext ) );
334  if( xChildImpl.is() )
335  xChild = xChildImpl.get();
336  }
337  return xChild;
338 }
339 
340 // index has to be treated as global child index.
342  sal_Int32 nChildIndex )
343 {
344  SolarMutexGuard g;
345 
346  if( nChildIndex < 0 ||
347  nChildIndex >= m_rContext.GetChildCount( *(m_rContext.GetMap()) ) )
349 }
350 
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base class of the Writer layout elements.
Definition: frame.hxx:295
const size_t count(pCandidateA->getBorderLines().size())
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2814
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:1924
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
int i
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:1796
SwAccessibleMap * GetMap()
Definition: acccontext.hxx:112
void deselectAccessibleChild(sal_Int32 nChildIndex)
SwAccessibleContext & m_rContext
the context on which this helper works