Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / Accessibility / AccessiblePageHeader.cxx
bloba8f88fbdea213ffe42d0b2e9d8d28e7c8c8cf390
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 .
21 #include "AccessiblePageHeader.hxx"
22 #include "AccessiblePageHeaderArea.hxx"
23 #include "AccessibilityHints.hxx"
24 #include "prevwsh.hxx"
25 #include "miscuno.hxx"
26 #include "prevloc.hxx"
27 #include "document.hxx"
28 #include "stlpool.hxx"
29 #include "scitems.hxx"
30 #include "attrib.hxx"
31 #include "scresid.hxx"
32 #include "sc.hrc"
34 #include <com/sun/star/accessibility/AccessibleRole.hpp>
35 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
36 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
38 #include <vcl/window.hxx>
39 #include <svl/smplhint.hxx>
40 #include <vcl/svapp.hxx>
41 #include <unotools/accessiblestatesethelper.hxx>
42 #include <svl/style.hxx>
43 #include <svl/itempool.hxx>
44 #include <editeng/editobj.hxx>
45 #include <toolkit/helper/convert.hxx>
47 #include <algorithm>
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::accessibility;
52 const sal_uInt8 MAX_AREAS = 3;
54 //===== internal ============================================================
55 struct Acquire
57 void operator() (ScAccessiblePageHeaderArea* pArea)
59 if (pArea)
60 pArea->acquire();
64 struct Release
66 void operator() (ScAccessiblePageHeaderArea*& pArea)
68 if (pArea)
69 pArea->release();
73 struct Dispose
75 void operator() (ScAccessiblePageHeaderArea*& pArea)
77 if (pArea)
79 pArea->dispose();
80 pArea->release();
82 pArea = NULL;
86 ScAccessiblePageHeader::ScAccessiblePageHeader( const ::com::sun::star::uno::Reference<
87 ::com::sun::star::accessibility::XAccessible>& rxParent,
88 ScPreviewShell* pViewShell, bool bHeader, sal_Int32 nIndex ) :
89 ScAccessibleContextBase( rxParent, bHeader ? AccessibleRole::HEADER : AccessibleRole::FOOTER ),
90 mpViewShell( pViewShell ),
91 mnIndex( nIndex ),
92 mbHeader( bHeader ),
93 maAreas(MAX_AREAS, NULL),
94 mnChildCount(-1)
96 if (mpViewShell)
97 mpViewShell->AddAccessibilityObject(*this);
100 ScAccessiblePageHeader::~ScAccessiblePageHeader()
102 if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
104 // increment refcount to prevent double call off dtor
105 osl_atomic_increment( &m_refCount );
106 dispose();
110 void SAL_CALL ScAccessiblePageHeader::disposing()
112 SolarMutexGuard aGuard;
113 if (mpViewShell)
115 mpViewShell->RemoveAccessibilityObject(*this);
116 mpViewShell = NULL;
118 std::for_each(maAreas.begin(), maAreas.end(), Dispose());
120 ScAccessibleContextBase::disposing();
123 //===== SfxListener =====================================================
125 void ScAccessiblePageHeader::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
127 if (rHint.ISA( SfxSimpleHint ) )
129 const SfxSimpleHint& rRef = (const SfxSimpleHint&)rHint;
130 // only notify if child exist, otherwise it is not necessary
131 if ((rRef.GetId() == SC_HINT_DATACHANGED))
133 ScHFAreas aOldAreas(maAreas);
134 std::for_each(aOldAreas.begin(), aOldAreas.end(), Acquire());
135 mnChildCount = -1;
136 getAccessibleChildCount();
137 for (sal_uInt8 i = 0; i < MAX_AREAS; ++i)
139 if ((aOldAreas[i] && maAreas[i] && !ScGlobal::EETextObjEqual(aOldAreas[i]->GetEditTextObject(), maAreas[i]->GetEditTextObject())) ||
140 (aOldAreas[i] && !maAreas[i]) || (!aOldAreas[i] && maAreas[i]))
142 if (aOldAreas[i] && aOldAreas[i]->GetEditTextObject())
144 AccessibleEventObject aEvent;
145 aEvent.EventId = AccessibleEventId::CHILD;
146 aEvent.Source = uno::Reference< XAccessibleContext >(this);
147 aEvent.OldValue = uno::makeAny(uno::Reference<XAccessible>(aOldAreas[i]));
149 CommitChange(aEvent); // child gone - event
150 aOldAreas[i]->dispose();
152 if (maAreas[i] && maAreas[i]->GetEditTextObject())
154 AccessibleEventObject aEvent;
155 aEvent.EventId = AccessibleEventId::CHILD;
156 aEvent.Source = uno::Reference< XAccessibleContext >(this);
157 aEvent.NewValue = uno::makeAny(uno::Reference<XAccessible>(maAreas[i]));
159 CommitChange(aEvent); // new child - event
163 std::for_each(aOldAreas.begin(), aOldAreas.end(), Release());
165 else if (rRef.GetId() == SC_HINT_ACC_VISAREACHANGED)
167 AccessibleEventObject aEvent;
168 aEvent.EventId = AccessibleEventId::VISIBLE_DATA_CHANGED;
169 aEvent.Source = uno::Reference< XAccessibleContext >(this);
170 CommitChange(aEvent);
174 ScAccessibleContextBase::Notify(rBC, rHint);
177 //===== XAccessibleComponent ============================================
179 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleAtPoint( const awt::Point& aPoint )
180 throw (uno::RuntimeException, std::exception)
182 uno::Reference<XAccessible> xRet;
184 if (containsPoint(aPoint))
186 SolarMutexGuard aGuard;
187 IsObjectValid();
189 sal_Int32 nCount(getAccessibleChildCount()); // fill the areas
191 if (nCount)
193 // return the first with content, because they have all the same Bounding Box
194 sal_uInt8 i(0);
195 while(!xRet.is() && i < MAX_AREAS)
197 if (maAreas[i])
198 xRet = maAreas[i];
199 else
200 ++i;
205 return xRet;
208 void SAL_CALL ScAccessiblePageHeader::grabFocus() throw (uno::RuntimeException, std::exception)
210 SolarMutexGuard aGuard;
211 IsObjectValid();
212 if (getAccessibleParent().is())
214 uno::Reference<XAccessibleComponent> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY);
215 if (xAccessibleComponent.is())
216 xAccessibleComponent->grabFocus();
220 //===== XAccessibleContext ==============================================
222 sal_Int32 SAL_CALL ScAccessiblePageHeader::getAccessibleChildCount() throw (uno::RuntimeException, std::exception)
224 SolarMutexGuard aGuard;
225 IsObjectValid();
227 if((mnChildCount < 0) && mpViewShell)
229 mnChildCount = 0;
230 ScDocument* pDoc = mpViewShell->GetDocument();
231 if (pDoc)
233 // find out how many regions (left,center, right) are with content
235 SfxStyleSheetBase* pStyle = pDoc->GetStyleSheetPool()->Find(pDoc->GetPageStyle(mpViewShell->GetLocationData().GetPrintTab()), SFX_STYLE_FAMILY_PAGE);
236 if (pStyle)
238 sal_uInt16 nPageWhichId(0);
239 if (mbHeader)
240 nPageWhichId = mpViewShell->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT : ATTR_PAGE_HEADERRIGHT;
241 else
242 nPageWhichId = mpViewShell->GetLocationData().IsFooterLeft() ? ATTR_PAGE_FOOTERLEFT : ATTR_PAGE_FOOTERRIGHT;
244 const ScPageHFItem& rPageItem = static_cast<const ScPageHFItem&>(pStyle->GetItemSet().Get(nPageWhichId));
245 AddChild(rPageItem.GetLeftArea(), 0, SVX_ADJUST_LEFT);
246 AddChild(rPageItem.GetCenterArea(), 1, SVX_ADJUST_CENTER);
247 AddChild(rPageItem.GetRightArea(), 2, SVX_ADJUST_RIGHT);
252 return mnChildCount;
255 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex )
256 throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
258 SolarMutexGuard aGuard;
259 IsObjectValid();
261 uno::Reference<XAccessible> xRet;
263 if(mnChildCount < 0)
264 getAccessibleChildCount();
266 ScHFAreas::iterator aItr = maAreas.begin();
267 ScHFAreas::iterator aEndItr = maAreas.end();
268 while (!xRet.is() && (nIndex >= 0) && (aItr != aEndItr))
270 if (*aItr)
272 if (nIndex == 0)
273 xRet = *aItr;
274 else
275 --nIndex;
277 else
278 ++aItr;
281 if ( !xRet.is() )
282 throw lang::IndexOutOfBoundsException();
284 return xRet;
287 sal_Int32 SAL_CALL ScAccessiblePageHeader::getAccessibleIndexInParent() throw (uno::RuntimeException, std::exception)
289 return mnIndex;
292 uno::Reference< XAccessibleStateSet > SAL_CALL ScAccessiblePageHeader::getAccessibleStateSet()
293 throw (uno::RuntimeException, std::exception)
295 SolarMutexGuard aGuard;
296 uno::Reference<XAccessibleStateSet> xParentStates;
297 if (getAccessibleParent().is())
299 uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
300 xParentStates = xParentContext->getAccessibleStateSet();
302 utl::AccessibleStateSetHelper* pStateSet = new utl::AccessibleStateSetHelper();
303 if (IsDefunc(xParentStates))
304 pStateSet->AddState(AccessibleStateType::DEFUNC);
305 else
307 pStateSet->AddState(AccessibleStateType::ENABLED);
308 pStateSet->AddState(AccessibleStateType::OPAQUE);
309 if (isShowing())
310 pStateSet->AddState(AccessibleStateType::SHOWING);
311 if (isVisible())
312 pStateSet->AddState(AccessibleStateType::VISIBLE);
314 return pStateSet;
317 //===== XServiceInfo ====================================================
319 OUString SAL_CALL ScAccessiblePageHeader::getImplementationName() throw(uno::RuntimeException, std::exception)
321 return OUString("ScAccessiblePageHeader");
324 uno::Sequence<OUString> SAL_CALL ScAccessiblePageHeader::getSupportedServiceNames()
325 throw(uno::RuntimeException, std::exception)
327 uno::Sequence< OUString > aSequence = ScAccessibleContextBase::getSupportedServiceNames();
328 sal_Int32 nOldSize(aSequence.getLength());
329 aSequence.realloc(nOldSize + 1);
331 aSequence[nOldSize] = "com.sun.star.text.AccessibleHeaderFooterView";
333 return aSequence;
336 //==== internal =========================================================
338 OUString SAL_CALL ScAccessiblePageHeader::createAccessibleDescription(void)
339 throw (uno::RuntimeException)
341 OUString sDesc(SC_RESSTR(mbHeader ? STR_ACC_HEADER_DESCR : STR_ACC_FOOTER_DESCR));
342 return sDesc.replaceFirst("%1", SC_RESSTR(SCSTR_UNKNOWN));
345 OUString SAL_CALL ScAccessiblePageHeader::createAccessibleName(void)
346 throw (uno::RuntimeException, std::exception)
348 OUString sName(SC_RESSTR(mbHeader ? STR_ACC_HEADER_NAME : STR_ACC_FOOTER_NAME));
349 return sName.replaceFirst("%1", SC_RESSTR(SCSTR_UNKNOWN));
352 Rectangle ScAccessiblePageHeader::GetBoundingBoxOnScreen() const throw (uno::RuntimeException, std::exception)
354 Rectangle aCellRect(GetBoundingBox());
355 if (mpViewShell)
357 Window* pWindow = mpViewShell->GetWindow();
358 if (pWindow)
360 Rectangle aRect = pWindow->GetWindowExtentsRelative(NULL);
361 aCellRect.setX(aCellRect.getX() + aRect.getX());
362 aCellRect.setY(aCellRect.getY() + aRect.getY());
365 return aCellRect;
368 Rectangle ScAccessiblePageHeader::GetBoundingBox() const throw (uno::RuntimeException, std::exception)
370 Rectangle aRect;
371 if (mpViewShell)
373 const ScPreviewLocationData& rData = mpViewShell->GetLocationData();
374 if ( mbHeader )
375 rData.GetHeaderPosition( aRect );
376 else
377 rData.GetFooterPosition( aRect );
379 // the Rectangle could contain negative coordinates so it should be cliped
380 Rectangle aClipRect(Point(0, 0), aRect.GetSize());
381 Window* pWindow = mpViewShell->GetWindow();
382 if (pWindow)
383 aClipRect = pWindow->GetWindowExtentsRelative(pWindow->GetAccessibleParentWindow());
384 aRect = aClipRect.GetIntersection(aRect);
386 if (aRect.IsEmpty())
387 aRect.SetSize(Size(-1, -1));
389 return aRect;
392 bool ScAccessiblePageHeader::IsDefunc( const uno::Reference<XAccessibleStateSet>& rxParentStates )
394 return ScAccessibleContextBase::IsDefunc() || (mpViewShell == NULL) || !getAccessibleParent().is() ||
395 (rxParentStates.is() && rxParentStates->contains(AccessibleStateType::DEFUNC));
398 void ScAccessiblePageHeader::AddChild(const EditTextObject* pArea, sal_uInt32 nIndex, SvxAdjust eAdjust)
400 if (pArea && (!pArea->GetText(0).isEmpty() || (pArea->GetParagraphCount() > 1)))
402 if (maAreas[nIndex])
404 if (!ScGlobal::EETextObjEqual(maAreas[nIndex]->GetEditTextObject(), pArea))
406 maAreas[nIndex]->release();
407 maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, mbHeader, eAdjust);
408 maAreas[nIndex]->acquire();
411 else
413 maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, mbHeader, eAdjust);
414 maAreas[nIndex]->acquire();
416 ++mnChildCount;
418 else
420 if (maAreas[nIndex])
422 maAreas[nIndex]->release();
423 maAreas[nIndex] = NULL;
428 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */