LibreOffice Module sc (master)  1
AccessiblePageHeader.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 <AccessiblePageHeader.hxx>
22 #include <prevwsh.hxx>
23 #include <prevloc.hxx>
24 #include <document.hxx>
25 #include <stlpool.hxx>
26 #include <scitems.hxx>
27 #include <attrib.hxx>
28 #include <scresid.hxx>
29 #include <strings.hrc>
30 #include <strings.hxx>
31 
32 #include <com/sun/star/accessibility/AccessibleRole.hpp>
33 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
34 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
35 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
36 #include <comphelper/sequence.hxx>
37 
38 #include <vcl/window.hxx>
39 #include <svl/hint.hxx>
40 #include <svl/itemset.hxx>
41 #include <vcl/svapp.hxx>
43 #include <svl/style.hxx>
44 #include <editeng/editobj.hxx>
45 
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::accessibility;
48 
49 const sal_uInt8 MAX_AREAS = 3;
50 
51 ScAccessiblePageHeader::ScAccessiblePageHeader( const css::uno::Reference<css::accessibility::XAccessible>& rxParent,
52  ScPreviewShell* pViewShell, bool bHeader, sal_Int32 nIndex ) :
53 ScAccessibleContextBase( rxParent, bHeader ? AccessibleRole::HEADER : AccessibleRole::FOOTER ),
54  mpViewShell( pViewShell ),
55  mnIndex( nIndex ),
56  mbHeader( bHeader ),
58  mnChildCount(-1)
59 {
60  if (mpViewShell)
62 }
63 
65 {
66  if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
67  {
68  // increment refcount to prevent double call off dtor
69  osl_atomic_increment( &m_refCount );
70  dispose();
71  }
72 }
73 
75 {
76  SolarMutexGuard aGuard;
77  if (mpViewShell)
78  {
80  mpViewShell = nullptr;
81  }
82  for (auto & i : maAreas)
83  {
84  if (i.is())
85  {
86  i->dispose();
87  i.clear();
88  }
89  }
90 
92 }
93 
94 //===== SfxListener =====================================================
95 
97 {
98  // only notify if child exist, otherwise it is not necessary
99  if (rHint.GetId() == SfxHintId::ScDataChanged)
100  {
101  std::vector<rtl::Reference<ScAccessiblePageHeaderArea>> aOldAreas(maAreas);
102  mnChildCount = -1;
104  for (sal_uInt8 i = 0; i < MAX_AREAS; ++i)
105  {
106  if ((aOldAreas[i].is() && maAreas[i].is() && !ScGlobal::EETextObjEqual(aOldAreas[i]->GetEditTextObject(), maAreas[i]->GetEditTextObject())) ||
107  (aOldAreas[i].is() && !maAreas[i].is()) || (!aOldAreas[i].is() && maAreas[i].is()))
108  {
109  if (aOldAreas[i].is() && aOldAreas[i]->GetEditTextObject())
110  {
111  AccessibleEventObject aEvent;
112  aEvent.EventId = AccessibleEventId::CHILD;
113  aEvent.Source = uno::Reference< XAccessibleContext >(this);
114  aEvent.OldValue <<= uno::Reference<XAccessible>(aOldAreas[i].get());
115 
116  CommitChange(aEvent); // child gone - event
117  aOldAreas[i]->dispose();
118  }
119  if (maAreas[i].is() && maAreas[i]->GetEditTextObject())
120  {
121  AccessibleEventObject aEvent;
122  aEvent.EventId = AccessibleEventId::CHILD;
123  aEvent.Source = uno::Reference< XAccessibleContext >(this);
124  aEvent.NewValue <<= uno::Reference<XAccessible>(maAreas[i].get());
125 
126  CommitChange(aEvent); // new child - event
127  }
128  }
129  }
130  }
131  else if (rHint.GetId() == SfxHintId::ScAccVisAreaChanged)
132  {
133  AccessibleEventObject aEvent;
134  aEvent.EventId = AccessibleEventId::VISIBLE_DATA_CHANGED;
135  aEvent.Source = uno::Reference< XAccessibleContext >(this);
136  CommitChange(aEvent);
137  }
138 
140 }
141 
142 //===== XAccessibleComponent ============================================
143 
144 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleAtPoint( const awt::Point& aPoint )
145 {
146  uno::Reference<XAccessible> xRet;
147 
148  if (containsPoint(aPoint))
149  {
150  SolarMutexGuard aGuard;
151  IsObjectValid();
152 
153  sal_Int32 nCount(getAccessibleChildCount()); // fill the areas
154 
155  if (nCount)
156  {
157  // return the first with content, because they have all the same Bounding Box
158  sal_uInt8 i(0);
159  while(!xRet.is() && i < MAX_AREAS)
160  {
161  if (maAreas[i].is())
162  xRet = maAreas[i].get();
163  else
164  ++i;
165  }
166  }
167  }
168 
169  return xRet;
170 }
171 
173 {
174  SolarMutexGuard aGuard;
175  IsObjectValid();
176  if (getAccessibleParent().is())
177  {
178  uno::Reference<XAccessibleComponent> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY);
179  if (xAccessibleComponent.is())
180  xAccessibleComponent->grabFocus();
181  }
182 }
183 
184 //===== XAccessibleContext ==============================================
185 
187 {
188  SolarMutexGuard aGuard;
189  IsObjectValid();
190 
191  if((mnChildCount < 0) && mpViewShell)
192  {
193  mnChildCount = 0;
195  // find out how many regions (left,center, right) are with content
196 
197  SfxStyleSheetBase* pStyle = rDoc.GetStyleSheetPool()->Find(rDoc.GetPageStyle(mpViewShell->GetLocationData().GetPrintTab()), SfxStyleFamily::Page);
198  if (pStyle)
199  {
200  sal_uInt16 nPageWhichId(0);
201  if (mbHeader)
203  else
205 
206  const ScPageHFItem& rPageItem = static_cast<const ScPageHFItem&>(pStyle->GetItemSet().Get(nPageWhichId));
207  AddChild(rPageItem.GetLeftArea(), 0, SvxAdjust::Left);
208  AddChild(rPageItem.GetCenterArea(), 1, SvxAdjust::Center);
209  AddChild(rPageItem.GetRightArea(), 2, SvxAdjust::Right);
210  }
211  }
212 
213  return mnChildCount;
214 }
215 
216 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex )
217 {
218  SolarMutexGuard aGuard;
219  IsObjectValid();
220 
221  uno::Reference<XAccessible> xRet;
222 
223  if(mnChildCount < 0)
225 
226  if (nIndex >= 0)
227  for (const auto& rxArea : maAreas)
228  {
229  if (rxArea.is())
230  {
231  if (nIndex == 0)
232  {
233  xRet = rxArea.get();
234  break;
235  }
236  else
237  --nIndex;
238  }
239  }
240 
241  if ( !xRet.is() )
242  throw lang::IndexOutOfBoundsException();
243 
244  return xRet;
245 }
246 
248 {
249  return mnIndex;
250 }
251 
252 uno::Reference< XAccessibleStateSet > SAL_CALL ScAccessiblePageHeader::getAccessibleStateSet()
253 {
254  SolarMutexGuard aGuard;
255  uno::Reference<XAccessibleStateSet> xParentStates;
256  if (getAccessibleParent().is())
257  {
258  uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
259  xParentStates = xParentContext->getAccessibleStateSet();
260  }
262  if (IsDefunc(xParentStates))
263  pStateSet->AddState(AccessibleStateType::DEFUNC);
264  else
265  {
266  pStateSet->AddState(AccessibleStateType::ENABLED);
267  pStateSet->AddState(AccessibleStateType::OPAQUE);
268  if (isShowing())
269  pStateSet->AddState(AccessibleStateType::SHOWING);
270  if (isVisible())
271  pStateSet->AddState(AccessibleStateType::VISIBLE);
272  }
273  return pStateSet;
274 }
275 
276 //===== XServiceInfo ====================================================
277 
279 {
280  return "ScAccessiblePageHeader";
281 }
282 
283 uno::Sequence<OUString> SAL_CALL ScAccessiblePageHeader::getSupportedServiceNames()
284 {
285  const css::uno::Sequence<OUString> vals { "com.sun.star.text.AccessibleHeaderFooterView" };
287 }
288 
289 //==== internal =========================================================
290 
292 {
293  OUString sDesc(mbHeader ? OUString(STR_ACC_HEADER_DESCR) : OUString(STR_ACC_FOOTER_DESCR));
294  return sDesc.replaceFirst("%1", ScResId(SCSTR_UNKNOWN));
295 }
296 
298 {
299  OUString sName(ScResId(mbHeader ? STR_ACC_HEADER_NAME : STR_ACC_FOOTER_NAME));
300  return sName.replaceFirst("%1", ScResId(SCSTR_UNKNOWN));
301 }
302 
304 {
305  tools::Rectangle aCellRect(GetBoundingBox());
306  if (mpViewShell)
307  {
308  vcl::Window* pWindow = mpViewShell->GetWindow();
309  if (pWindow)
310  {
311  tools::Rectangle aRect = pWindow->GetWindowExtentsRelative(nullptr);
312  aCellRect.setX(aCellRect.getX() + aRect.getX());
313  aCellRect.setY(aCellRect.getY() + aRect.getY());
314  }
315  }
316  return aCellRect;
317 }
318 
320 {
321  tools::Rectangle aRect;
322  if (mpViewShell)
323  {
325  if ( mbHeader )
326  rData.GetHeaderPosition( aRect );
327  else
328  rData.GetFooterPosition( aRect );
329 
330  // the Rectangle could contain negative coordinates so it should be clipped
331  tools::Rectangle aClipRect(Point(0, 0), aRect.GetSize());
332  vcl::Window* pWindow = mpViewShell->GetWindow();
333  if (pWindow)
334  aClipRect = pWindow->GetWindowExtentsRelative(pWindow->GetAccessibleParentWindow());
335  aRect = aClipRect.GetIntersection(aRect);
336  }
337  if (aRect.IsEmpty())
338  aRect.SetSize(Size(-1, -1));
339 
340  return aRect;
341 }
342 
343 bool ScAccessiblePageHeader::IsDefunc( const uno::Reference<XAccessibleStateSet>& rxParentStates )
344 {
345  return ScAccessibleContextBase::IsDefunc() || (mpViewShell == nullptr) || !getAccessibleParent().is() ||
346  (rxParentStates.is() && rxParentStates->contains(AccessibleStateType::DEFUNC));
347 }
348 
349 void ScAccessiblePageHeader::AddChild(const EditTextObject* pArea, sal_uInt32 nIndex, SvxAdjust eAdjust)
350 {
351  if (pArea && (!pArea->GetText(0).isEmpty() || (pArea->GetParagraphCount() > 1)))
352  {
353  if (maAreas[nIndex].is())
354  {
355  if (!ScGlobal::EETextObjEqual(maAreas[nIndex]->GetEditTextObject(), pArea))
356  {
357  maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, eAdjust);
358  }
359  }
360  else
361  {
362  maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, eAdjust);
363  }
364  ++mnChildCount;
365  }
366  else
367  {
368  maAreas[nIndex].clear();
369  }
370 }
371 
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
===== SfxListener =====================================================
const ScPreviewLocationData & GetLocationData()
Definition: prevwsh.cxx:1177
void AddAccessibilityObject(SfxListener &rObject)
Definition: prevwsh.cxx:1148
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Returns a list of all supported services.
static bool EETextObjEqual(const EditTextObject *pObj1, const EditTextObject *pObj2)
Definition: global.cxx:747
ULONG m_refCount
const EditTextObject * GetCenterArea() const
Definition: attrib.hxx:186
virtual OUString createAccessibleName() override
Return the object's current name.
tools::Long getY() const
Reference
const ContentProperties & rData
virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleParent() override
Return a reference to the parent.
#define STR_ACC_HEADER_DESCR
Definition: strings.hxx:23
virtual SfxItemSet & GetItemSet()
virtual sal_Bool SAL_CALL containsPoint(const css::awt::Point &rPoint) override
===== XAccessibleComponent ============================================
SfxHintId GetId() const
virtual sal_Int32 SAL_CALL getAccessibleIndexInParent() override
Return this objects index among the parents children.
bool IsEmpty() const
virtual ~ScAccessiblePageHeader() override
int nCount
SCTAB GetPrintTab() const
Definition: prevloc.hxx:118
tools::Rectangle GetIntersection(const tools::Rectangle &rRect) const
virtual void Notify(SfxBroadcaster &rBC, const SfxHint &rHint) override
===== SfxListener =====================================================
virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleChild(sal_Int32 i) override
Return the specified child or NULL if index is invalid.
tools::Long getX() const
tools::Rectangle GetWindowExtentsRelative(const vcl::Window *pRelativeWindow) const
void setX(tools::Long x)
const char * sName
const sal_uInt8 MAX_AREAS
OUString GetText(sal_Int32 nPara) const
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_FOOTERLEFT(178)
ScAccessiblePageHeader(const css::uno::Reference< css::accessibility::XAccessible > &rxParent, ScPreviewShell *pViewShell, bool bHeader, sal_Int32 nIndex)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Returns a list of all supported services.
void setY(tools::Long y)
bool GetFooterPosition(tools::Rectangle &rFooterRect) const
Definition: prevloc.cxx:347
bool IsFooterLeft() const
Definition: prevloc.cxx:375
virtual tools::Rectangle GetBoundingBoxOnScreen() const override
Return the object's current bounding box relative to the desktop.
SvxAdjust
int i
virtual void SAL_CALL disposing() override
void AddChild(const EditTextObject *pArea, sal_uInt32 nIndex, SvxAdjust eAdjust)
void SetSize(const Size &rSize)
OUString ScResId(const char *pId)
Definition: scdll.cxx:89
SC_DLLPUBLIC OUString GetPageStyle(SCTAB nTab) const
Definition: document.cxx:6179
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_HEADERRIGHT(179)
css::uno::Sequence< T > concatSequences(const css::uno::Sequence< T > &rS1, const Ss &...rSn)
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_FOOTERRIGHT(180)
bool IsHeaderLeft() const
Definition: prevloc.cxx:361
Size GetSize() const
virtual sal_Int32 SAL_CALL getAccessibleChildCount() override
===== XAccessibleContext ==============================================
sal_Int32 GetParagraphCount() const
std::vector< rtl::Reference< ScAccessiblePageHeaderArea > > maAreas
virtual void SAL_CALL disposing() override
const SfxPoolItem & Get(sal_uInt16 nWhich, bool bSrchInParent=true) const
void RemoveAccessibilityObject(SfxListener &rObject)
Definition: prevwsh.cxx:1156
unsigned char sal_uInt8
const EditTextObject * GetRightArea() const
Definition: attrib.hxx:187
virtual OUString createAccessibleDescription() override
Return this object's description.
const EditTextObject * GetLeftArea() const
Definition: attrib.hxx:185
virtual css::uno::Reference< css::accessibility::XAccessibleStateSet > SAL_CALL getAccessibleStateSet() override
Return the set of current states.
ScDocument & GetDocument()
Definition: prevwsh.cxx:1182
vcl::Window * GetWindow() const
SC_DLLPUBLIC ScStyleSheetPool * GetStyleSheetPool() const
Definition: document.cxx:6059
void CommitChange(const css::accessibility::AccessibleEventObject &rEvent) const
Calls all Listener to tell they the change.
#define STR_ACC_FOOTER_DESCR
Definition: strings.hxx:24
virtual void SAL_CALL grabFocus() override
void dispose()
int mnIndex
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All)
void AddState(sal_Int16 aState)
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_HEADERLEFT(177)
virtual OUString SAL_CALL getImplementationName() override
===== XServiceInfo ====================================================
AnyEventRef aEvent
virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleAtPoint(const css::awt::Point &aPoint) override
virtual tools::Rectangle GetBoundingBox() const override
Return the object's current bounding box relative to the parent object.
bool GetHeaderPosition(tools::Rectangle &rHeaderRect) const
Definition: prevloc.cxx:333
virtual css::uno::Reference< css::accessibility::XAccessibleContext > SAL_CALL getAccessibleContext() override
===== XAccessible =====================================================