1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <AccessiblePageHeader.hxx>
21 #include <AccessiblePageHeaderArea.hxx>
22 #include <prevwsh.hxx>
23 #include <prevloc.hxx>
24 #include <document.hxx>
25 #include <stlpool.hxx>
26 #include <scitems.hxx>
28 #include <scresid.hxx>
29 #include <strings.hrc>
30 #include <strings.hxx>
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>
37 #include <vcl/window.hxx>
38 #include <svl/hint.hxx>
39 #include <svl/itemset.hxx>
40 #include <vcl/svapp.hxx>
41 #include <unotools/accessiblestatesethelper.hxx>
42 #include <svl/style.hxx>
43 #include <editeng/editobj.hxx>
45 using namespace ::com::sun::star
;
46 using namespace ::com::sun::star::accessibility
;
48 const sal_uInt8 MAX_AREAS
= 3;
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
),
56 maAreas(MAX_AREAS
, rtl::Reference
<ScAccessiblePageHeaderArea
>()),
60 mpViewShell
->AddAccessibilityObject(*this);
63 ScAccessiblePageHeader::~ScAccessiblePageHeader()
65 if (!ScAccessibleContextBase::IsDefunc() && !rBHelper
.bInDispose
)
67 // increment refcount to prevent double call off dtor
68 osl_atomic_increment( &m_refCount
);
73 void SAL_CALL
ScAccessiblePageHeader::disposing()
75 SolarMutexGuard aGuard
;
78 mpViewShell
->RemoveAccessibilityObject(*this);
79 mpViewShell
= nullptr;
81 for (auto & i
: maAreas
)
90 ScAccessibleContextBase::disposing();
93 //===== SfxListener =====================================================
95 void ScAccessiblePageHeader::Notify( SfxBroadcaster
& rBC
, const SfxHint
& rHint
)
97 // only notify if child exist, otherwise it is not necessary
98 if (rHint
.GetId() == SfxHintId::ScDataChanged
)
100 std::vector
<rtl::Reference
<ScAccessiblePageHeaderArea
>> aOldAreas(maAreas
);
102 getAccessibleChildCount();
103 for (sal_uInt8 i
= 0; i
< MAX_AREAS
; ++i
)
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()))
108 if (aOldAreas
[i
].is() && aOldAreas
[i
]->GetEditTextObject())
110 AccessibleEventObject aEvent
;
111 aEvent
.EventId
= AccessibleEventId::CHILD
;
112 aEvent
.Source
= uno::Reference
< XAccessibleContext
>(this);
113 aEvent
.OldValue
<<= uno::Reference
<XAccessible
>(aOldAreas
[i
].get());
115 CommitChange(aEvent
); // child gone - event
116 aOldAreas
[i
]->dispose();
118 if (maAreas
[i
].is() && maAreas
[i
]->GetEditTextObject())
120 AccessibleEventObject aEvent
;
121 aEvent
.EventId
= AccessibleEventId::CHILD
;
122 aEvent
.Source
= uno::Reference
< XAccessibleContext
>(this);
123 aEvent
.NewValue
<<= uno::Reference
<XAccessible
>(maAreas
[i
].get());
125 CommitChange(aEvent
); // new child - event
130 else if (rHint
.GetId() == SfxHintId::ScAccVisAreaChanged
)
132 AccessibleEventObject aEvent
;
133 aEvent
.EventId
= AccessibleEventId::VISIBLE_DATA_CHANGED
;
134 aEvent
.Source
= uno::Reference
< XAccessibleContext
>(this);
135 CommitChange(aEvent
);
138 ScAccessibleContextBase::Notify(rBC
, rHint
);
141 //===== XAccessibleComponent ============================================
143 uno::Reference
< XAccessible
> SAL_CALL
ScAccessiblePageHeader::getAccessibleAtPoint( const awt::Point
& aPoint
)
145 uno::Reference
<XAccessible
> xRet
;
147 if (containsPoint(aPoint
))
149 SolarMutexGuard aGuard
;
152 sal_Int32
nCount(getAccessibleChildCount()); // fill the areas
156 // return the first with content, because they have all the same Bounding Box
158 while(!xRet
.is() && i
< MAX_AREAS
)
161 xRet
= maAreas
[i
].get();
171 void SAL_CALL
ScAccessiblePageHeader::grabFocus()
173 SolarMutexGuard aGuard
;
175 if (getAccessibleParent().is())
177 uno::Reference
<XAccessibleComponent
> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY
);
178 if (xAccessibleComponent
.is())
179 xAccessibleComponent
->grabFocus();
183 //===== XAccessibleContext ==============================================
185 sal_Int32 SAL_CALL
ScAccessiblePageHeader::getAccessibleChildCount()
187 SolarMutexGuard aGuard
;
190 if((mnChildCount
< 0) && mpViewShell
)
193 ScDocument
& rDoc
= mpViewShell
->GetDocument();
194 // find out how many regions (left,center, right) are with content
196 SfxStyleSheetBase
* pStyle
= rDoc
.GetStyleSheetPool()->Find(rDoc
.GetPageStyle(mpViewShell
->GetLocationData().GetPrintTab()), SfxStyleFamily::Page
);
199 sal_uInt16
nPageWhichId(0);
201 nPageWhichId
= mpViewShell
->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT
: ATTR_PAGE_HEADERRIGHT
;
203 nPageWhichId
= mpViewShell
->GetLocationData().IsFooterLeft() ? ATTR_PAGE_FOOTERLEFT
: ATTR_PAGE_FOOTERRIGHT
;
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
);
215 uno::Reference
< XAccessible
> SAL_CALL
ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex
)
217 SolarMutexGuard aGuard
;
220 uno::Reference
<XAccessible
> xRet
;
223 getAccessibleChildCount();
226 for (const auto& rxArea
: maAreas
)
241 throw lang::IndexOutOfBoundsException();
246 sal_Int32 SAL_CALL
ScAccessiblePageHeader::getAccessibleIndexInParent()
251 uno::Reference
< XAccessibleStateSet
> SAL_CALL
ScAccessiblePageHeader::getAccessibleStateSet()
253 SolarMutexGuard aGuard
;
254 uno::Reference
<XAccessibleStateSet
> xParentStates
;
255 if (getAccessibleParent().is())
257 uno::Reference
<XAccessibleContext
> xParentContext
= getAccessibleParent()->getAccessibleContext();
258 xParentStates
= xParentContext
->getAccessibleStateSet();
260 utl::AccessibleStateSetHelper
* pStateSet
= new utl::AccessibleStateSetHelper();
261 if (IsDefunc(xParentStates
))
262 pStateSet
->AddState(AccessibleStateType::DEFUNC
);
265 pStateSet
->AddState(AccessibleStateType::ENABLED
);
266 pStateSet
->AddState(AccessibleStateType::OPAQUE
);
268 pStateSet
->AddState(AccessibleStateType::SHOWING
);
270 pStateSet
->AddState(AccessibleStateType::VISIBLE
);
275 //===== XServiceInfo ====================================================
277 OUString SAL_CALL
ScAccessiblePageHeader::getImplementationName()
279 return "ScAccessiblePageHeader";
282 uno::Sequence
<OUString
> SAL_CALL
ScAccessiblePageHeader::getSupportedServiceNames()
284 uno::Sequence
< OUString
> aSequence
= ScAccessibleContextBase::getSupportedServiceNames();
285 sal_Int32
nOldSize(aSequence
.getLength());
286 aSequence
.realloc(nOldSize
+ 1);
288 aSequence
[nOldSize
] = "com.sun.star.text.AccessibleHeaderFooterView";
293 //==== internal =========================================================
295 OUString
ScAccessiblePageHeader::createAccessibleDescription()
297 OUString
sDesc(mbHeader
? OUString(STR_ACC_HEADER_DESCR
) : OUString(STR_ACC_FOOTER_DESCR
));
298 return sDesc
.replaceFirst("%1", ScResId(SCSTR_UNKNOWN
));
301 OUString
ScAccessiblePageHeader::createAccessibleName()
303 OUString
sName(ScResId(mbHeader
? STR_ACC_HEADER_NAME
: STR_ACC_FOOTER_NAME
));
304 return sName
.replaceFirst("%1", ScResId(SCSTR_UNKNOWN
));
307 tools::Rectangle
ScAccessiblePageHeader::GetBoundingBoxOnScreen() const
309 tools::Rectangle
aCellRect(GetBoundingBox());
312 vcl::Window
* pWindow
= mpViewShell
->GetWindow();
315 tools::Rectangle aRect
= pWindow
->GetWindowExtentsRelative(nullptr);
316 aCellRect
.setX(aCellRect
.getX() + aRect
.getX());
317 aCellRect
.setY(aCellRect
.getY() + aRect
.getY());
323 tools::Rectangle
ScAccessiblePageHeader::GetBoundingBox() const
325 tools::Rectangle aRect
;
328 const ScPreviewLocationData
& rData
= mpViewShell
->GetLocationData();
330 rData
.GetHeaderPosition( aRect
);
332 rData
.GetFooterPosition( aRect
);
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();
338 aClipRect
= pWindow
->GetWindowExtentsRelative(pWindow
->GetAccessibleParentWindow());
339 aRect
= aClipRect
.GetIntersection(aRect
);
342 aRect
.SetSize(Size(-1, -1));
347 bool ScAccessiblePageHeader::IsDefunc( const uno::Reference
<XAccessibleStateSet
>& rxParentStates
)
349 return ScAccessibleContextBase::IsDefunc() || (mpViewShell
== nullptr) || !getAccessibleParent().is() ||
350 (rxParentStates
.is() && rxParentStates
->contains(AccessibleStateType::DEFUNC
));
353 void ScAccessiblePageHeader::AddChild(const EditTextObject
* pArea
, sal_uInt32 nIndex
, SvxAdjust eAdjust
)
355 if (pArea
&& (!pArea
->GetText(0).isEmpty() || (pArea
->GetParagraphCount() > 1)))
357 if (maAreas
[nIndex
].is())
359 if (!ScGlobal::EETextObjEqual(maAreas
[nIndex
]->GetEditTextObject(), pArea
))
361 maAreas
[nIndex
] = new ScAccessiblePageHeaderArea(this, mpViewShell
, pArea
, eAdjust
);
366 maAreas
[nIndex
] = new ScAccessiblePageHeaderArea(this, mpViewShell
, pArea
, eAdjust
);
372 maAreas
[nIndex
].clear();
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */