tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / Accessibility / AccessiblePageHeader.cxx
blobdab5f2f89b3c260b95597f55d7a3e7e529014daa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 #include <attrib.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 ),
54 mnIndex( nIndex ),
55 mbHeader( bHeader ),
56 maAreas(MAX_AREAS, rtl::Reference<ScAccessiblePageHeaderArea>()),
57 mnChildCount(-1)
59 if (mpViewShell)
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 );
69 dispose();
73 void SAL_CALL ScAccessiblePageHeader::disposing()
75 SolarMutexGuard aGuard;
76 if (mpViewShell)
78 mpViewShell->RemoveAccessibilityObject(*this);
79 mpViewShell = nullptr;
81 for (auto & i : maAreas)
83 if (i.is())
85 i->dispose();
86 i.clear();
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);
101 mnChildCount = -1;
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;
152 IsObjectValid();
154 sal_Int64 nCount(getAccessibleChildCount()); // fill the areas
156 if (nCount)
158 // return the first with content, because they have all the same Bounding Box
159 sal_uInt8 i(0);
160 while(!xRet.is() && i < MAX_AREAS)
162 if (maAreas[i].is())
163 xRet = maAreas[i].get();
164 else
165 ++i;
170 return xRet;
173 void SAL_CALL ScAccessiblePageHeader::grabFocus()
175 SolarMutexGuard aGuard;
176 IsObjectValid();
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;
190 IsObjectValid();
192 if((mnChildCount < 0) && mpViewShell)
194 mnChildCount = 0;
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);
199 if (pStyle)
201 sal_uInt16 nPageWhichId(0);
202 if (mbHeader)
203 nPageWhichId = mpViewShell->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT : ATTR_PAGE_HEADERRIGHT;
204 else
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);
214 return mnChildCount;
217 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleChild( sal_Int64 nIndex )
219 SolarMutexGuard aGuard;
220 IsObjectValid();
222 uno::Reference<XAccessible> xRet;
224 if(mnChildCount < 0)
225 getAccessibleChildCount();
227 if (nIndex >= 0)
228 for (const auto& rxArea : maAreas)
230 if (rxArea.is())
232 if (nIndex == 0)
234 xRet = rxArea.get();
235 break;
237 else
238 --nIndex;
242 if ( !xRet.is() )
243 throw lang::IndexOutOfBoundsException();
245 return xRet;
248 sal_Int64 SAL_CALL ScAccessiblePageHeader::getAccessibleIndexInParent()
250 return mnIndex;
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;
265 else
267 nStateSet |= AccessibleStateType::ENABLED;
268 nStateSet |= AccessibleStateType::OPAQUE;
269 if (isShowing())
270 nStateSet |= AccessibleStateType::SHOWING;
271 if (isVisible())
272 nStateSet |= AccessibleStateType::VISIBLE;
274 return nStateSet;
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());
307 if (mpViewShell)
309 vcl::Window* pWindow = mpViewShell->GetWindow();
310 if (pWindow)
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;
322 if (mpViewShell)
324 const ScPreviewLocationData& rData = mpViewShell->GetLocationData();
325 if ( mbHeader )
326 rData.GetHeaderPosition( aRect );
327 else
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();
333 if (pWindow)
334 aClipRect = pWindow->GetWindowExtentsRelative(*pWindow->GetAccessibleParentWindow());
335 aRect = aClipRect.GetIntersection(aRect);
337 if (aRect.IsEmpty())
338 aRect.SetSize(Size(-1, -1));
340 return aRect;
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);
360 else
362 maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, eAdjust);
364 ++mnChildCount;
366 else
368 maAreas[nIndex].clear();
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */