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 
37 #include <vcl/window.hxx>
38 #include <svl/hint.hxx>
39 #include <svl/itemset.hxx>
40 #include <vcl/svapp.hxx>
42 #include <svl/style.hxx>
43 #include <editeng/editobj.hxx>
44 
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::accessibility;
47 
48 const sal_uInt8 MAX_AREAS = 3;
49 
50 ScAccessiblePageHeader::ScAccessiblePageHeader( const css::uno::Reference<css::accessibility::XAccessible>& rxParent,
51  ScPreviewShell* pViewShell, bool bHeader, sal_Int32 nIndex ) :
52 ScAccessibleContextBase( rxParent, bHeader ? AccessibleRole::HEADER : AccessibleRole::FOOTER ),
53  mpViewShell( pViewShell ),
54  mnIndex( nIndex ),
55  mbHeader( bHeader ),
57  mnChildCount(-1)
58 {
59  if (mpViewShell)
61 }
62 
64 {
65  if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
66  {
67  // increment refcount to prevent double call off dtor
68  osl_atomic_increment( &m_refCount );
69  dispose();
70  }
71 }
72 
74 {
75  SolarMutexGuard aGuard;
76  if (mpViewShell)
77  {
79  mpViewShell = nullptr;
80  }
81  for (auto & i : maAreas)
82  {
83  if (i.is())
84  {
85  i->dispose();
86  i.clear();
87  }
88  }
89 
91 }
92 
93 //===== SfxListener =====================================================
94 
96 {
97  // only notify if child exist, otherwise it is not necessary
98  if (rHint.GetId() == SfxHintId::ScDataChanged)
99  {
100  std::vector<rtl::Reference<ScAccessiblePageHeaderArea>> aOldAreas(maAreas);
101  mnChildCount = -1;
103  for (sal_uInt8 i = 0; i < MAX_AREAS; ++i)
104  {
105  if ((aOldAreas[i].is() && maAreas[i].is() && !ScGlobal::EETextObjEqual(aOldAreas[i]->GetEditTextObject(), maAreas[i]->GetEditTextObject())) ||
106  (aOldAreas[i].is() && !maAreas[i].is()) || (!aOldAreas[i].is() && maAreas[i].is()))
107  {
108  if (aOldAreas[i].is() && aOldAreas[i]->GetEditTextObject())
109  {
110  AccessibleEventObject aEvent;
111  aEvent.EventId = AccessibleEventId::CHILD;
112  aEvent.Source = uno::Reference< XAccessibleContext >(this);
113  aEvent.OldValue <<= uno::Reference<XAccessible>(aOldAreas[i].get());
114 
115  CommitChange(aEvent); // child gone - event
116  aOldAreas[i]->dispose();
117  }
118  if (maAreas[i].is() && maAreas[i]->GetEditTextObject())
119  {
120  AccessibleEventObject aEvent;
121  aEvent.EventId = AccessibleEventId::CHILD;
122  aEvent.Source = uno::Reference< XAccessibleContext >(this);
123  aEvent.NewValue <<= uno::Reference<XAccessible>(maAreas[i].get());
124 
125  CommitChange(aEvent); // new child - event
126  }
127  }
128  }
129  }
130  else if (rHint.GetId() == SfxHintId::ScAccVisAreaChanged)
131  {
132  AccessibleEventObject aEvent;
133  aEvent.EventId = AccessibleEventId::VISIBLE_DATA_CHANGED;
134  aEvent.Source = uno::Reference< XAccessibleContext >(this);
135  CommitChange(aEvent);
136  }
137 
139 }
140 
141 //===== XAccessibleComponent ============================================
142 
143 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleAtPoint( const awt::Point& aPoint )
144 {
145  uno::Reference<XAccessible> xRet;
146 
147  if (containsPoint(aPoint))
148  {
149  SolarMutexGuard aGuard;
150  IsObjectValid();
151 
152  sal_Int32 nCount(getAccessibleChildCount()); // fill the areas
153 
154  if (nCount)
155  {
156  // return the first with content, because they have all the same Bounding Box
157  sal_uInt8 i(0);
158  while(!xRet.is() && i < MAX_AREAS)
159  {
160  if (maAreas[i].is())
161  xRet = maAreas[i].get();
162  else
163  ++i;
164  }
165  }
166  }
167 
168  return xRet;
169 }
170 
172 {
173  SolarMutexGuard aGuard;
174  IsObjectValid();
175  if (getAccessibleParent().is())
176  {
177  uno::Reference<XAccessibleComponent> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY);
178  if (xAccessibleComponent.is())
179  xAccessibleComponent->grabFocus();
180  }
181 }
182 
183 //===== XAccessibleContext ==============================================
184 
186 {
187  SolarMutexGuard aGuard;
188  IsObjectValid();
189 
190  if((mnChildCount < 0) && mpViewShell)
191  {
192  mnChildCount = 0;
194  // find out how many regions (left,center, right) are with content
195 
196  SfxStyleSheetBase* pStyle = rDoc.GetStyleSheetPool()->Find(rDoc.GetPageStyle(mpViewShell->GetLocationData().GetPrintTab()), SfxStyleFamily::Page);
197  if (pStyle)
198  {
199  sal_uInt16 nPageWhichId(0);
200  if (mbHeader)
202  else
204 
205  const ScPageHFItem& rPageItem = static_cast<const ScPageHFItem&>(pStyle->GetItemSet().Get(nPageWhichId));
206  AddChild(rPageItem.GetLeftArea(), 0, SvxAdjust::Left);
207  AddChild(rPageItem.GetCenterArea(), 1, SvxAdjust::Center);
208  AddChild(rPageItem.GetRightArea(), 2, SvxAdjust::Right);
209  }
210  }
211 
212  return mnChildCount;
213 }
214 
215 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex )
216 {
217  SolarMutexGuard aGuard;
218  IsObjectValid();
219 
220  uno::Reference<XAccessible> xRet;
221 
222  if(mnChildCount < 0)
224 
225  if (nIndex >= 0)
226  for (const auto& rxArea : maAreas)
227  {
228  if (rxArea.is())
229  {
230  if (nIndex == 0)
231  {
232  xRet = rxArea.get();
233  break;
234  }
235  else
236  --nIndex;
237  }
238  }
239 
240  if ( !xRet.is() )
241  throw lang::IndexOutOfBoundsException();
242 
243  return xRet;
244 }
245 
247 {
248  return mnIndex;
249 }
250 
251 uno::Reference< XAccessibleStateSet > SAL_CALL ScAccessiblePageHeader::getAccessibleStateSet()
252 {
253  SolarMutexGuard aGuard;
254  uno::Reference<XAccessibleStateSet> xParentStates;
255  if (getAccessibleParent().is())
256  {
257  uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
258  xParentStates = xParentContext->getAccessibleStateSet();
259  }
261  if (IsDefunc(xParentStates))
262  pStateSet->AddState(AccessibleStateType::DEFUNC);
263  else
264  {
265  pStateSet->AddState(AccessibleStateType::ENABLED);
266  pStateSet->AddState(AccessibleStateType::OPAQUE);
267  if (isShowing())
268  pStateSet->AddState(AccessibleStateType::SHOWING);
269  if (isVisible())
270  pStateSet->AddState(AccessibleStateType::VISIBLE);
271  }
272  return pStateSet;
273 }
274 
275 //===== XServiceInfo ====================================================
276 
278 {
279  return "ScAccessiblePageHeader";
280 }
281 
282 uno::Sequence<OUString> SAL_CALL ScAccessiblePageHeader::getSupportedServiceNames()
283 {
284  uno::Sequence< OUString > aSequence = ScAccessibleContextBase::getSupportedServiceNames();
285  sal_Int32 nOldSize(aSequence.getLength());
286  aSequence.realloc(nOldSize + 1);
287 
288  aSequence[nOldSize] = "com.sun.star.text.AccessibleHeaderFooterView";
289 
290  return aSequence;
291 }
292 
293 //==== internal =========================================================
294 
296 {
297  OUString sDesc(mbHeader ? OUString(STR_ACC_HEADER_DESCR) : OUString(STR_ACC_FOOTER_DESCR));
298  return sDesc.replaceFirst("%1", ScResId(SCSTR_UNKNOWN));
299 }
300 
302 {
303  OUString sName(ScResId(mbHeader ? STR_ACC_HEADER_NAME : STR_ACC_FOOTER_NAME));
304  return sName.replaceFirst("%1", ScResId(SCSTR_UNKNOWN));
305 }
306 
308 {
309  tools::Rectangle aCellRect(GetBoundingBox());
310  if (mpViewShell)
311  {
312  vcl::Window* pWindow = mpViewShell->GetWindow();
313  if (pWindow)
314  {
315  tools::Rectangle aRect = pWindow->GetWindowExtentsRelative(nullptr);
316  aCellRect.setX(aCellRect.getX() + aRect.getX());
317  aCellRect.setY(aCellRect.getY() + aRect.getY());
318  }
319  }
320  return aCellRect;
321 }
322 
324 {
325  tools::Rectangle aRect;
326  if (mpViewShell)
327  {
329  if ( mbHeader )
330  rData.GetHeaderPosition( aRect );
331  else
332  rData.GetFooterPosition( aRect );
333 
334  // the Rectangle could contain negative coordinates so it should be clipped
335  tools::Rectangle aClipRect(Point(0, 0), aRect.GetSize());
336  vcl::Window* pWindow = mpViewShell->GetWindow();
337  if (pWindow)
338  aClipRect = pWindow->GetWindowExtentsRelative(pWindow->GetAccessibleParentWindow());
339  aRect = aClipRect.GetIntersection(aRect);
340  }
341  if (aRect.IsEmpty())
342  aRect.SetSize(Size(-1, -1));
343 
344  return aRect;
345 }
346 
347 bool ScAccessiblePageHeader::IsDefunc( const uno::Reference<XAccessibleStateSet>& rxParentStates )
348 {
349  return ScAccessibleContextBase::IsDefunc() || (mpViewShell == nullptr) || !getAccessibleParent().is() ||
350  (rxParentStates.is() && rxParentStates->contains(AccessibleStateType::DEFUNC));
351 }
352 
353 void ScAccessiblePageHeader::AddChild(const EditTextObject* pArea, sal_uInt32 nIndex, SvxAdjust eAdjust)
354 {
355  if (pArea && (!pArea->GetText(0).isEmpty() || (pArea->GetParagraphCount() > 1)))
356  {
357  if (maAreas[nIndex].is())
358  {
359  if (!ScGlobal::EETextObjEqual(maAreas[nIndex]->GetEditTextObject(), pArea))
360  {
361  maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, eAdjust);
362  }
363  }
364  else
365  {
366  maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, eAdjust);
367  }
368  ++mnChildCount;
369  }
370  else
371  {
372  maAreas[nIndex].clear();
373  }
374 }
375 
376 /* 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:748
ULONG m_refCount
const EditTextObject * GetCenterArea() const
Definition: attrib.hxx:186
virtual OUString createAccessibleName() override
Return the object's current name.
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()
long getY() const
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.
long getX() const
tools::Rectangle GetWindowExtentsRelative(const vcl::Window *pRelativeWindow) const
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.
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 setY(long y)
void AddChild(const EditTextObject *pArea, sal_uInt32 nIndex, SvxAdjust eAdjust)
void SetSize(const Size &rSize)
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
SC_DLLPUBLIC OUString GetPageStyle(SCTAB nTab) const
Definition: document.cxx:6186
constexpr TypedWhichId< ScPageHFItem > ATTR_PAGE_HEADERRIGHT(179)
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:6066
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 setX(long x)
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 =====================================================