LibreOffice Module sw (master)  1
accframebase.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/AccessibleStateType.hpp>
21 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
23 #include <osl/mutex.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/window.hxx>
26 #include <frmfmt.hxx>
27 #include <ndnotxt.hxx>
28 #include <flyfrm.hxx>
29 #include <cntfrm.hxx>
30 #include <fmtcntnt.hxx>
31 #include <ndindex.hxx>
32 #include <fesh.hxx>
33 #include <hints.hxx>
34 #include <accmap.hxx>
35 #include "accframebase.hxx"
36 
37 #include <crsrsh.hxx>
38 #include <txtfrm.hxx>
39 #include <notxtfrm.hxx>
40 #include <ndtxt.hxx>
41 #include <dcontact.hxx>
42 #include <fmtanchr.hxx>
43 
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::accessibility;
46 
48 {
49  bool bRet = false;
50 
51  assert(GetMap());
52  const SwViewShell *pVSh = GetMap()->GetShell();
53  assert(pVSh);
54  if( auto pFESh = dynamic_cast<const SwFEShell*>(pVSh) )
55  {
56  const SwFrame *pFlyFrame = pFESh->GetSelectedFlyFrame();
57  if( pFlyFrame == GetFrame() )
58  bRet = true;
59  }
60 
61  return bRet;
62 }
63 
65  ::utl::AccessibleStateSetHelper& rStateSet )
66 {
67  SwAccessibleContext::GetStates( rStateSet );
68 
69  const SwViewShell *pVSh = GetMap()->GetShell();
70  assert(pVSh);
71 
72  if (dynamic_cast<const SwFEShell*>(pVSh))
73  {
74  // SELECTABLE
75  rStateSet.AddState(AccessibleStateType::SELECTABLE);
76  // FOCUSABLE
77  rStateSet.AddState(AccessibleStateType::FOCUSABLE);
78  }
79 
80  // SELECTED and FOCUSED
81  if( IsSelected() )
82  {
83  rStateSet.AddState( AccessibleStateType::SELECTED );
84  SAL_WARN_IF(!m_bIsSelected, "sw.a11y", "bSelected out of sync");
86  GetMap()->SetCursorContext( xThis );
87 
88  vcl::Window *pWin = GetWindow();
89  if( pWin && pWin->HasFocus() )
90  rStateSet.AddState( AccessibleStateType::FOCUSED );
91  }
92  if( GetSelectedState() )
93  rStateSet.AddState( AccessibleStateType::SELECTED );
94 }
95 
97 {
99  if( pFlyFrame->Lower() )
100  {
101  if( pFlyFrame->Lower()->IsNoTextFrame() )
102  {
103  const SwNoTextFrame *const pContentFrame =
104  static_cast<const SwNoTextFrame *>(pFlyFrame->Lower());
105  nType = pContentFrame->GetNode()->GetNodeType();
106  }
107  }
108  else
109  {
110  const SwFrameFormat *pFrameFormat = pFlyFrame->GetFormat();
111  const SwFormatContent& rContent = pFrameFormat->GetContent();
112  const SwNodeIndex *pNdIdx = rContent.GetContentIdx();
113  if( pNdIdx )
114  {
115  const SwContentNode *pCNd =
116  (pNdIdx->GetNodes())[pNdIdx->GetIndex()+1]->GetContentNode();
117  if( pCNd )
118  nType = pCNd->GetNodeType();
119  }
120  }
121 
122  return nType;
123 }
124 
126  std::shared_ptr<SwAccessibleMap> const& pInitMap,
127  sal_Int16 nInitRole,
128  const SwFlyFrame* pFlyFrame ) :
129  SwAccessibleContext( pInitMap, nInitRole, pFlyFrame ),
130  m_bIsSelected( false )
131 {
132  const SwFrameFormat *pFrameFormat = pFlyFrame->GetFormat();
133  const_cast< SwFrameFormat * >( pFrameFormat )->Add( this );
134 
135  SetName( pFrameFormat->GetName() );
136 
138 }
139 
141 {
142  bool bNewSelected = IsSelected();
143  bool bOldSelected;
144 
145  {
146  osl::MutexGuard aGuard( m_Mutex );
147  bOldSelected = m_bIsSelected;
148  m_bIsSelected = bNewSelected;
149  }
150 
151  if( bNewSelected )
152  {
153  // remember that object as the one that has the caret. This is
154  // necessary to notify that object if the cursor leaves it.
156  GetMap()->SetCursorContext( xThis );
157  }
158 
159  if( bOldSelected != bNewSelected )
160  {
161  vcl::Window *pWin = GetWindow();
162  if( pWin && pWin->HasFocus() && bNewSelected )
163  FireStateChangedEvent( AccessibleStateType::FOCUSED, bNewSelected );
164  if( pWin && pWin->HasFocus() && !bNewSelected )
165  FireStateChangedEvent( AccessibleStateType::FOCUSED, bNewSelected );
166  if(bNewSelected)
167  {
168  uno::Reference< XAccessible > xParent( GetWeakParent() );
169  if( xParent.is() )
170  {
171  SwAccessibleContext *pAcc =
172  static_cast <SwAccessibleContext *>( xParent.get() );
173 
174  AccessibleEventObject aEvent;
175  aEvent.EventId = AccessibleEventId::SELECTION_CHANGED;
176  uno::Reference< XAccessible > xChild(this);
177  aEvent.NewValue <<= xChild;
178  pAcc->FireAccessibleEvent( aEvent );
179  }
180  }
181  }
182 }
183 
185 {
186  vcl::Window *pWin = GetWindow();
187  if( pWin )
188  {
189  bool bSelected;
190 
191  {
192  osl::MutexGuard aGuard( m_Mutex );
193  bSelected = m_bIsSelected;
194  }
195  assert(bSelected && "focus object should be selected");
196 
197  FireStateChangedEvent( AccessibleStateType::FOCUSED,
198  pWin->HasFocus() && bSelected );
199  }
200 }
201 
203 {
204  osl::MutexGuard aGuard( m_Mutex );
205  return m_bIsSelected;
206 }
207 
209 {
210 }
211 
213 {
214  sal_uInt16 nWhich = pOld ? pOld->Which() : pNew ? pNew->Which() : 0 ;
215  switch( nWhich )
216  {
217  case RES_NAME_CHANGED:
218  {
219  const SwFlyFrame *pFlyFrame = static_cast< const SwFlyFrame * >( GetFrame() );
220  if( pFlyFrame )
221  {
222  const SwFrameFormat *pFrameFormat = pFlyFrame->GetFormat();
223  assert(pFrameFormat == GetRegisteredIn() && "invalid frame");
224 
225  const OUString sOldName( GetName() );
226  assert( !pOld ||
227  static_cast<const SwStringMsgPoolItem *>(pOld)->GetString() == GetName());
228 
229  SetName( pFrameFormat->GetName() );
230  assert( !pNew ||
231  static_cast<const SwStringMsgPoolItem *>(pNew)->GetString() == GetName());
232 
233  if( sOldName != GetName() )
234  {
235  AccessibleEventObject aEvent;
236  aEvent.EventId = AccessibleEventId::NAME_CHANGED;
237  aEvent.OldValue <<= sOldName;
238  aEvent.NewValue <<= GetName();
239  FireAccessibleEvent( aEvent );
240  }
241  }
242  break;
243  }
244  case RES_OBJECTDYING:
245  // mba: it seems that this class intentionally does not call code in base class SwClient
246  if( pOld && ( GetRegisteredIn() == static_cast< SwModify *>( static_cast< const SwPtrMsgPoolItem * >( pOld )->pObject ) ) )
247  EndListeningAll();
248  break;
249 
250  case RES_FMT_CHG:
251  if( pOld &&
252  static_cast< const SwFormatChg * >(pNew)->pChangedFormat == GetRegisteredIn() &&
253  static_cast< const SwFormatChg * >(pOld)->pChangedFormat->IsFormatInDTOR() )
254  EndListeningAll();
255  break;
256 
257  default:
258  // mba: former call to base class method removed as it is meant to handle only RES_OBJECTDYING
259  break;
260  }
261 }
262 
263 void SwAccessibleFrameBase::Dispose(bool bRecursive, bool bCanSkipInvisible)
264 {
265  SolarMutexGuard aGuard;
266  EndListeningAll();
267  SwAccessibleContext::Dispose(bRecursive, bCanSkipInvisible);
268 }
269 
270 //Get the selection cursor of the document.
272 {
273  // get the cursor shell; if we don't have any, we don't have a
274  // cursor/selection either
275  SwPaM* pCursor = nullptr;
276  SwCursorShell* pCursorShell = GetCursorShell();
277  if( pCursorShell != nullptr && !pCursorShell->IsTableMode() )
278  {
279  SwFEShell *pFESh = dynamic_cast<const SwFEShell*>( pCursorShell) != nullptr
280  ? static_cast< SwFEShell * >( pCursorShell ) : nullptr;
281  if( !pFESh ||
282  !(pFESh->IsFrameSelected() || pFESh->IsObjSelected() > 0) )
283  {
284  // get the selection, and test whether it affects our text node
285  pCursor = pCursorShell->GetCursor( false /* ??? */ );
286  }
287  }
288 
289  return pCursor;
290 }
291 
292 //Return the selected state of the object.
293 //when the object's anchor are in the selection cursor, we should return true.
295 {
296  SolarMutexGuard aGuard;
297 
298  if(GetMap()->IsDocumentSelAll())
299  {
300  return true;
301  }
302 
303  // SELECTED.
304  SwFlyFrame* pFlyFrame = getFlyFrame();
305  const SwFrameFormat *pFrameFormat = pFlyFrame->GetFormat();
306  const SwFormatAnchor& rAnchor = pFrameFormat->GetAnchor();
307  const SwPosition *pPos = rAnchor.GetContentAnchor();
308  if( !pPos )
309  return false;
310  int nIndex = pPos->nContent.GetIndex();
311  if( pPos->nNode.GetNode().GetTextNode() )
312  {
313  SwPaM* pCursor = GetCursor();
314  if( pCursor != nullptr )
315  {
316  const SwTextNode* pNode = pPos->nNode.GetNode().GetTextNode();
317  sal_uLong nHere = pNode->GetIndex();
318 
319  // iterate over ring
320  SwPaM* pRingStart = pCursor;
321  do
322  {
323  // ignore, if no mark
324  if( pCursor->HasMark() )
325  {
326  // check whether nHere is 'inside' pCursor
327  SwPosition* pStart = pCursor->Start();
328  sal_uLong nStartIndex = pStart->nNode.GetIndex();
329  SwPosition* pEnd = pCursor->End();
330  sal_uLong nEndIndex = pEnd->nNode.GetIndex();
331  if( ( nHere >= nStartIndex ) && (nHere <= nEndIndex) )
332  {
333  if( rAnchor.GetAnchorId() == RndStdIds::FLY_AS_CHAR )
334  {
335  if( ((nHere == nStartIndex) && (nIndex >= pStart->nContent.GetIndex())) || (nHere > nStartIndex) )
336  if( ((nHere == nEndIndex) && (nIndex < pEnd->nContent.GetIndex())) || (nHere < nEndIndex) )
337  return true;
338  }
339  else if( rAnchor.GetAnchorId() == RndStdIds::FLY_AT_PARA )
340  {
341  if( ((nHere > nStartIndex) || pStart->nContent.GetIndex() ==0 )
342  && (nHere < nEndIndex ) )
343  return true;
344  }
345  break;
346  }
347  // else: this PaM doesn't point to this paragraph
348  }
349  // else: this PaM is collapsed and doesn't select anything
350 
351  // next PaM in ring
352  pCursor = pCursor->GetNext();
353  }
354  while( pCursor != pRingStart );
355  }
356  }
357  return false;
358 }
359 
361 {
362  SwFlyFrame* pFlyFrame = nullptr;
363 
364  const SwFrame* pFrame = GetFrame();
365  assert(pFrame);
366  if( pFrame->IsFlyFrame() )
367  {
368  pFlyFrame = static_cast<SwFlyFrame*>( const_cast<SwFrame*>( pFrame ) );
369  }
370 
371  return pFlyFrame;
372 }
373 
375 {
376  bool bParaSelected = GetSelectedState() || IsSelected();
377 
378  if (m_isSelectedInDoc != bParaSelected)
379  {
380  m_isSelectedInDoc = bParaSelected;
381  FireStateChangedEvent( AccessibleStateType::SELECTED, bParaSelected );
382  return true;
383  }
384  return false;
385 }
386 
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base class of the Writer layout elements.
Definition: frame.hxx:295
sal_uLong GetIndex() const
Definition: node.hxx:282
void FireAccessibleEvent(css::accessibility::AccessibleEventObject &rEvent)
Definition: acccontext.cxx:443
virtual const SwFlyFrameFormat * GetFormat() const override
Definition: fly.cxx:2814
virtual void Modify(const SfxPoolItem *pOld, const SfxPoolItem *pNew) override
Marks a position in the document model.
Definition: pam.hxx:35
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:185
virtual void GetStates(::utl::AccessibleStateSetHelper &rStateSet) override
virtual void InvalidateFocus_() override
SwNodeIndex nNode
Definition: pam.hxx:37
bool IsTableMode() const
Definition: crsrsh.hxx:643
SwAccessibleFrameBase(std::shared_ptr< SwAccessibleMap > const &pInitMap, sal_Int16 nInitRole, const SwFlyFrame *pFlyFrame)
sal_uIntPtr sal_uLong
vcl::Window * GetWindow()
Definition: acccontext.cxx:86
SwNode & GetNode() const
Definition: ndindex.hxx:118
Content, content of frame (header, footer, fly).
Definition: fmtcntnt.hxx:31
OUString GetString(int nId)
SwCursorShell * GetCursorShell()
convenience method to get SwCursorShell through accessibility map
Definition: acccontext.cxx:104
bool IsFlyFrame() const
Definition: frame.hxx:1186
SwViewShell * GetShell() const
Definition: accmap.hxx:169
FUNC_TYPE const nType
SwNodeType GetNodeType() const
Definition: node.hxx:144
SwIndex nContent
Definition: pam.hxx:38
const SwFrame * GetFrame() const
Definition: accframe.hxx:103
const OUString & GetName() const
Definition: format.hxx:111
sal_uLong GetIndex() const
Definition: ndindex.hxx:151
virtual bool HasCursor() override
SwPaM * GetNext()
Definition: pam.hxx:264
void EndListeningAll()
Definition: calbck.cxx:124
SwFlyFrame * getFlyFrame() const
void FireStateChangedEvent(sal_Int16 nState, bool bNewState)
Definition: acccontext.cxx:467
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
Style of a layout element.
Definition: frmfmt.hxx:57
SwNodeType
Definition: ndtyp.hxx:28
const SwFormatAnchor & GetAnchor(bool=true) const
Definition: fmtanchr.hxx:81
const OUString & GetName() const
Definition: acccontext.hxx:333
RndStdIds GetAnchorId() const
Definition: fmtanchr.hxx:65
const SwPosition * GetContentAnchor() const
Definition: fmtanchr.hxx:67
const SwFrame * Lower() const
Definition: layfrm.hxx:100
SwContentNode * GetContentNode()
Definition: node.hxx:615
FlyAnchors.
Definition: fmtanchr.hxx:34
bool HasMark() const
A PaM marks a selection if Point and Mark are distinct positions.
Definition: pam.hxx:205
Marks a node in the document model.
Definition: ndindex.hxx:31
#define RES_FMT_CHG
Definition: hintids.hxx:284
static SwNodeType GetNodeType(const SwFlyFrame *pFlyFrame)
const SwPosition * Start() const
Definition: pam.hxx:212
const SwNodeIndex * GetContentIdx() const
Definition: fmtcntnt.hxx:46
virtual void InvalidateCursorPos_() override
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
const SwContentNode * GetNode() const
Definition: notxtfrm.hxx:60
general base class for all free-flowing frames
Definition: flyfrm.hxx:60
virtual ~SwAccessibleFrameBase() override
#define SAL_WARN_IF(condition, area, stream)
virtual void Dispose(bool bRecursive, bool bCanSkipInvisible=true)
const SwNodes & GetNodes() const
Definition: ndindex.hxx:155
virtual void GetStates(::utl::AccessibleStateSetHelper &rStateSet)
Definition: acccontext.cxx:481
sal_Int32 GetIndex() const
Definition: index.hxx:95
bool IsNoTextFrame() const
Definition: frame.hxx:1214
const SwPosition * End() const
Definition: pam.hxx:217
const SwModify * GetRegisteredIn() const
Definition: calbck.hxx:157
#define RES_NAME_CHANGED
Definition: hintids.hxx:310
size_t IsObjSelected() const
Definition: feshview.cxx:1167
css::uno::Reference< css::accessibility::XAccessible > GetWeakParent() const
Definition: acccontext.cxx:78
SwAccessibleMap * GetMap()
Definition: acccontext.hxx:112
const SwFormatContent & GetContent(bool=true) const
Definition: fmtcntnt.hxx:55
virtual bool SetSelectedState(bool bSeleted) override
#define RES_OBJECTDYING
Definition: hintids.hxx:283
mutable::osl::Mutex m_Mutex
Definition: acccontext.hxx:64
void SetCursorContext(const ::rtl::Reference< SwAccessibleContext > &rCursorContext)
Definition: accmap.cxx:2740
bool HasFocus() const
void AddState(sal_Int16 aState)
void SetName(const OUString &rName)
Definition: acccontext.hxx:99
bool IsFrameSelected() const
Definition: feshview.cxx:1175
sal_uInt16 Which() const
virtual void Dispose(bool bRecursive, bool bCanSkipInvisible=true) override
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:843