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>
36 #include <comphelper/sequence.hxx>
38 #include <vcl/window.hxx>
39 #include <svl/hint.hxx>
40 #include <svl/itemset.hxx>
41 #include <vcl/svapp.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
]);
114 aEvent
.IndexHint
= -1;
116 CommitChange(aEvent
); // child gone - event
117 aOldAreas
[i
]->dispose();
119 if (maAreas
[i
].is() && maAreas
[i
]->GetEditTextObject())
121 AccessibleEventObject aEvent
;
122 aEvent
.EventId
= AccessibleEventId::CHILD
;
123 aEvent
.Source
= uno::Reference
< XAccessibleContext
>(this);
124 aEvent
.NewValue
<<= uno::Reference
<XAccessible
>(maAreas
[i
]);
125 aEvent
.IndexHint
= -1;
127 CommitChange(aEvent
); // new child - event
132 else if (rHint
.GetId() == SfxHintId::ScAccVisAreaChanged
)
134 AccessibleEventObject aEvent
;
135 aEvent
.EventId
= AccessibleEventId::VISIBLE_DATA_CHANGED
;
136 aEvent
.Source
= uno::Reference
< XAccessibleContext
>(this);
137 CommitChange(aEvent
);
140 ScAccessibleContextBase::Notify(rBC
, rHint
);
143 //===== XAccessibleComponent ============================================
145 uno::Reference
< XAccessible
> SAL_CALL
ScAccessiblePageHeader::getAccessibleAtPoint( const awt::Point
& aPoint
)
147 uno::Reference
<XAccessible
> xRet
;
149 if (containsPoint(aPoint
))
151 SolarMutexGuard aGuard
;
154 sal_Int64
nCount(getAccessibleChildCount()); // fill the areas
158 // return the first with content, because they have all the same Bounding Box
160 while(!xRet
.is() && i
< MAX_AREAS
)
163 xRet
= maAreas
[i
].get();
173 void SAL_CALL
ScAccessiblePageHeader::grabFocus()
175 SolarMutexGuard aGuard
;
177 if (getAccessibleParent().is())
179 uno::Reference
<XAccessibleComponent
> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY
);
180 if (xAccessibleComponent
.is())
181 xAccessibleComponent
->grabFocus();
185 //===== XAccessibleContext ==============================================
187 sal_Int64 SAL_CALL
ScAccessiblePageHeader::getAccessibleChildCount()
189 SolarMutexGuard aGuard
;
192 if((mnChildCount
< 0) && mpViewShell
)
195 ScDocument
& rDoc
= mpViewShell
->GetDocument();
196 // find out how many regions (left,center, right) are with content
198 SfxStyleSheetBase
* pStyle
= rDoc
.GetStyleSheetPool()->Find(rDoc
.GetPageStyle(mpViewShell
->GetLocationData().GetPrintTab()), SfxStyleFamily::Page
);
201 sal_uInt16
nPageWhichId(0);
203 nPageWhichId
= mpViewShell
->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT
: ATTR_PAGE_HEADERRIGHT
;
205 nPageWhichId
= mpViewShell
->GetLocationData().IsFooterLeft() ? ATTR_PAGE_FOOTERLEFT
: ATTR_PAGE_FOOTERRIGHT
;
207 const ScPageHFItem
& rPageItem
= static_cast<const ScPageHFItem
&>(pStyle
->GetItemSet().Get(nPageWhichId
));
208 AddChild(rPageItem
.GetLeftArea(), 0, SvxAdjust::Left
);
209 AddChild(rPageItem
.GetCenterArea(), 1, SvxAdjust::Center
);
210 AddChild(rPageItem
.GetRightArea(), 2, SvxAdjust::Right
);
217 uno::Reference
< XAccessible
> SAL_CALL
ScAccessiblePageHeader::getAccessibleChild( sal_Int64 nIndex
)
219 SolarMutexGuard aGuard
;
222 uno::Reference
<XAccessible
> xRet
;
225 getAccessibleChildCount();
228 for (const auto& rxArea
: maAreas
)
243 throw lang::IndexOutOfBoundsException();
248 sal_Int64 SAL_CALL
ScAccessiblePageHeader::getAccessibleIndexInParent()
253 sal_Int64 SAL_CALL
ScAccessiblePageHeader::getAccessibleStateSet()
255 SolarMutexGuard aGuard
;
256 sal_Int64 nParentStates
= 0;
257 if (getAccessibleParent().is())
259 uno::Reference
<XAccessibleContext
> xParentContext
= getAccessibleParent()->getAccessibleContext();
260 nParentStates
= xParentContext
->getAccessibleStateSet();
262 sal_Int64 nStateSet
= 0;
263 if (IsDefunc(nParentStates
))
264 nStateSet
|= AccessibleStateType::DEFUNC
;
267 nStateSet
|= AccessibleStateType::ENABLED
;
268 nStateSet
|= AccessibleStateType::OPAQUE
;
270 nStateSet
|= AccessibleStateType::SHOWING
;
272 nStateSet
|= AccessibleStateType::VISIBLE
;
277 //===== XServiceInfo ====================================================
279 OUString SAL_CALL
ScAccessiblePageHeader::getImplementationName()
281 return u
"ScAccessiblePageHeader"_ustr
;
284 uno::Sequence
<OUString
> SAL_CALL
ScAccessiblePageHeader::getSupportedServiceNames()
286 const css::uno::Sequence
<OUString
> vals
{ u
"com.sun.star.text.AccessibleHeaderFooterView"_ustr
};
287 return comphelper::concatSequences(ScAccessibleContextBase::getSupportedServiceNames(), vals
);
290 //==== internal =========================================================
292 OUString
ScAccessiblePageHeader::createAccessibleDescription()
294 OUString
sDesc(mbHeader
? STR_ACC_HEADER_DESCR
: STR_ACC_FOOTER_DESCR
);
295 return sDesc
.replaceFirst("%1", ScResId(SCSTR_UNKNOWN
));
298 OUString
ScAccessiblePageHeader::createAccessibleName()
300 OUString
sName(ScResId(mbHeader
? STR_ACC_HEADER_NAME
: STR_ACC_FOOTER_NAME
));
301 return sName
.replaceFirst("%1", ScResId(SCSTR_UNKNOWN
));
304 AbsoluteScreenPixelRectangle
ScAccessiblePageHeader::GetBoundingBoxOnScreen() const
306 tools::Rectangle
aCellRect(GetBoundingBox());
309 vcl::Window
* pWindow
= mpViewShell
->GetWindow();
312 AbsoluteScreenPixelRectangle aRect
= pWindow
->GetWindowExtentsAbsolute();
313 aCellRect
.Move(aRect
.Left(), aRect
.Top());
316 return AbsoluteScreenPixelRectangle(aCellRect
);
319 tools::Rectangle
ScAccessiblePageHeader::GetBoundingBox() const
321 tools::Rectangle aRect
;
324 const ScPreviewLocationData
& rData
= mpViewShell
->GetLocationData();
326 rData
.GetHeaderPosition( aRect
);
328 rData
.GetFooterPosition( aRect
);
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();
334 aClipRect
= pWindow
->GetWindowExtentsRelative(*pWindow
->GetAccessibleParentWindow());
335 aRect
= aClipRect
.GetIntersection(aRect
);
338 aRect
.SetSize(Size(-1, -1));
343 bool ScAccessiblePageHeader::IsDefunc( sal_Int64 nParentStates
)
345 return ScAccessibleContextBase::IsDefunc() || (mpViewShell
== nullptr) || !getAccessibleParent().is() ||
346 (nParentStates
& AccessibleStateType::DEFUNC
);
349 void ScAccessiblePageHeader::AddChild(const EditTextObject
* pArea
, sal_uInt32 nIndex
, SvxAdjust eAdjust
)
351 if (pArea
&& ((pArea
->GetParagraphCount() > 1) || pArea
->HasText(0)))
353 if (maAreas
[nIndex
].is())
355 if (!ScGlobal::EETextObjEqual(maAreas
[nIndex
]->GetEditTextObject(), pArea
))
357 maAreas
[nIndex
] = new ScAccessiblePageHeaderArea(this, mpViewShell
, pArea
, eAdjust
);
362 maAreas
[nIndex
] = new ScAccessiblePageHeaderArea(this, mpViewShell
, pArea
, eAdjust
);
368 maAreas
[nIndex
].clear();
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */