fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / Accessibility / AccessiblePageHeader.cxx
blob936a8fce756125694edaa41faeb1fdd9459eeb72
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 "AccessibilityHints.hxx"
23 #include "prevwsh.hxx"
24 #include "miscuno.hxx"
25 #include "prevloc.hxx"
26 #include "document.hxx"
27 #include "stlpool.hxx"
28 #include "scitems.hxx"
29 #include "attrib.hxx"
30 #include "scresid.hxx"
31 #include "sc.hrc"
33 #include <com/sun/star/accessibility/AccessibleRole.hpp>
34 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
35 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
37 #include <vcl/window.hxx>
38 #include <svl/smplhint.hxx>
39 #include <vcl/svapp.hxx>
40 #include <unotools/accessiblestatesethelper.hxx>
41 #include <svl/style.hxx>
42 #include <svl/itempool.hxx>
43 #include <editeng/editobj.hxx>
44 #include <toolkit/helper/convert.hxx>
46 #include <algorithm>
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::accessibility;
51 const sal_uInt8 MAX_AREAS = 3;
53 //===== internal ============================================================
54 struct Acquire
56 void operator() (ScAccessiblePageHeaderArea* pArea)
58 if (pArea)
59 pArea->acquire();
63 struct Release
65 void operator() (ScAccessiblePageHeaderArea*& pArea)
67 if (pArea)
68 pArea->release();
72 struct Dispose
74 void operator() (ScAccessiblePageHeaderArea*& pArea)
76 if (pArea)
78 pArea->dispose();
79 pArea->release();
81 pArea = NULL;
85 ScAccessiblePageHeader::ScAccessiblePageHeader( const ::com::sun::star::uno::Reference<
86 ::com::sun::star::accessibility::XAccessible>& rxParent,
87 ScPreviewShell* pViewShell, bool bHeader, sal_Int32 nIndex ) :
88 ScAccessibleContextBase( rxParent, bHeader ? AccessibleRole::HEADER : AccessibleRole::FOOTER ),
89 mpViewShell( pViewShell ),
90 mnIndex( nIndex ),
91 mbHeader( bHeader ),
92 maAreas(MAX_AREAS, NULL),
93 mnChildCount(-1)
95 if (mpViewShell)
96 mpViewShell->AddAccessibilityObject(*this);
99 ScAccessiblePageHeader::~ScAccessiblePageHeader()
101 if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
103 // increment refcount to prevent double call off dtor
104 osl_atomic_increment( &m_refCount );
105 dispose();
109 void SAL_CALL ScAccessiblePageHeader::disposing()
111 SolarMutexGuard aGuard;
112 if (mpViewShell)
114 mpViewShell->RemoveAccessibilityObject(*this);
115 mpViewShell = NULL;
117 std::for_each(maAreas.begin(), maAreas.end(), Dispose());
119 ScAccessibleContextBase::disposing();
122 //===== SfxListener =====================================================
124 void ScAccessiblePageHeader::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
126 const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
127 if (pSimpleHint)
129 // only notify if child exist, otherwise it is not necessary
130 if (pSimpleHint->GetId() == SC_HINT_DATACHANGED)
132 ScHFAreas aOldAreas(maAreas);
133 std::for_each(aOldAreas.begin(), aOldAreas.end(), Acquire());
134 mnChildCount = -1;
135 getAccessibleChildCount();
136 for (sal_uInt8 i = 0; i < MAX_AREAS; ++i)
138 if ((aOldAreas[i] && maAreas[i] && !ScGlobal::EETextObjEqual(aOldAreas[i]->GetEditTextObject(), maAreas[i]->GetEditTextObject())) ||
139 (aOldAreas[i] && !maAreas[i]) || (!aOldAreas[i] && maAreas[i]))
141 if (aOldAreas[i] && aOldAreas[i]->GetEditTextObject())
143 AccessibleEventObject aEvent;
144 aEvent.EventId = AccessibleEventId::CHILD;
145 aEvent.Source = uno::Reference< XAccessibleContext >(this);
146 aEvent.OldValue = uno::makeAny(uno::Reference<XAccessible>(aOldAreas[i]));
148 CommitChange(aEvent); // child gone - event
149 aOldAreas[i]->dispose();
151 if (maAreas[i] && maAreas[i]->GetEditTextObject())
153 AccessibleEventObject aEvent;
154 aEvent.EventId = AccessibleEventId::CHILD;
155 aEvent.Source = uno::Reference< XAccessibleContext >(this);
156 aEvent.NewValue = uno::makeAny(uno::Reference<XAccessible>(maAreas[i]));
158 CommitChange(aEvent); // new child - event
162 std::for_each(aOldAreas.begin(), aOldAreas.end(), Release());
164 else if (pSimpleHint->GetId() == SC_HINT_ACC_VISAREACHANGED)
166 AccessibleEventObject aEvent;
167 aEvent.EventId = AccessibleEventId::VISIBLE_DATA_CHANGED;
168 aEvent.Source = uno::Reference< XAccessibleContext >(this);
169 CommitChange(aEvent);
173 ScAccessibleContextBase::Notify(rBC, rHint);
176 //===== XAccessibleComponent ============================================
178 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleAtPoint( const awt::Point& aPoint )
179 throw (uno::RuntimeException, std::exception)
181 uno::Reference<XAccessible> xRet;
183 if (containsPoint(aPoint))
185 SolarMutexGuard aGuard;
186 IsObjectValid();
188 sal_Int32 nCount(getAccessibleChildCount()); // fill the areas
190 if (nCount)
192 // return the first with content, because they have all the same Bounding Box
193 sal_uInt8 i(0);
194 while(!xRet.is() && i < MAX_AREAS)
196 if (maAreas[i])
197 xRet = maAreas[i];
198 else
199 ++i;
204 return xRet;
207 void SAL_CALL ScAccessiblePageHeader::grabFocus() throw (uno::RuntimeException, std::exception)
209 SolarMutexGuard aGuard;
210 IsObjectValid();
211 if (getAccessibleParent().is())
213 uno::Reference<XAccessibleComponent> xAccessibleComponent(getAccessibleParent()->getAccessibleContext(), uno::UNO_QUERY);
214 if (xAccessibleComponent.is())
215 xAccessibleComponent->grabFocus();
219 //===== XAccessibleContext ==============================================
221 sal_Int32 SAL_CALL ScAccessiblePageHeader::getAccessibleChildCount() throw (uno::RuntimeException, std::exception)
223 SolarMutexGuard aGuard;
224 IsObjectValid();
226 if((mnChildCount < 0) && mpViewShell)
228 mnChildCount = 0;
229 ScDocument& rDoc = mpViewShell->GetDocument();
230 // find out how many regions (left,center, right) are with content
232 SfxStyleSheetBase* pStyle = rDoc.GetStyleSheetPool()->Find(rDoc.GetPageStyle(mpViewShell->GetLocationData().GetPrintTab()), SFX_STYLE_FAMILY_PAGE);
233 if (pStyle)
235 sal_uInt16 nPageWhichId(0);
236 if (mbHeader)
237 nPageWhichId = mpViewShell->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT : ATTR_PAGE_HEADERRIGHT;
238 else
239 nPageWhichId = mpViewShell->GetLocationData().IsFooterLeft() ? ATTR_PAGE_FOOTERLEFT : ATTR_PAGE_FOOTERRIGHT;
241 const ScPageHFItem& rPageItem = static_cast<const ScPageHFItem&>(pStyle->GetItemSet().Get(nPageWhichId));
242 AddChild(rPageItem.GetLeftArea(), 0, SVX_ADJUST_LEFT);
243 AddChild(rPageItem.GetCenterArea(), 1, SVX_ADJUST_CENTER);
244 AddChild(rPageItem.GetRightArea(), 2, SVX_ADJUST_RIGHT);
248 return mnChildCount;
251 uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex )
252 throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
254 SolarMutexGuard aGuard;
255 IsObjectValid();
257 uno::Reference<XAccessible> xRet;
259 if(mnChildCount < 0)
260 getAccessibleChildCount();
262 ScHFAreas::iterator aItr = maAreas.begin();
263 ScHFAreas::iterator aEndItr = maAreas.end();
264 while (!xRet.is() && (nIndex >= 0) && (aItr != aEndItr))
266 if (*aItr)
268 if (nIndex == 0)
269 xRet = *aItr;
270 else
271 --nIndex;
273 else
274 ++aItr;
277 if ( !xRet.is() )
278 throw lang::IndexOutOfBoundsException();
280 return xRet;
283 sal_Int32 SAL_CALL ScAccessiblePageHeader::getAccessibleIndexInParent() throw (uno::RuntimeException, std::exception)
285 return mnIndex;
288 uno::Reference< XAccessibleStateSet > SAL_CALL ScAccessiblePageHeader::getAccessibleStateSet()
289 throw (uno::RuntimeException, std::exception)
291 SolarMutexGuard aGuard;
292 uno::Reference<XAccessibleStateSet> xParentStates;
293 if (getAccessibleParent().is())
295 uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
296 xParentStates = xParentContext->getAccessibleStateSet();
298 utl::AccessibleStateSetHelper* pStateSet = new utl::AccessibleStateSetHelper();
299 if (IsDefunc(xParentStates))
300 pStateSet->AddState(AccessibleStateType::DEFUNC);
301 else
303 pStateSet->AddState(AccessibleStateType::ENABLED);
304 pStateSet->AddState(AccessibleStateType::OPAQUE);
305 if (isShowing())
306 pStateSet->AddState(AccessibleStateType::SHOWING);
307 if (isVisible())
308 pStateSet->AddState(AccessibleStateType::VISIBLE);
310 return pStateSet;
313 //===== XServiceInfo ====================================================
315 OUString SAL_CALL ScAccessiblePageHeader::getImplementationName() throw(uno::RuntimeException, std::exception)
317 return OUString("ScAccessiblePageHeader");
320 uno::Sequence<OUString> SAL_CALL ScAccessiblePageHeader::getSupportedServiceNames()
321 throw(uno::RuntimeException, std::exception)
323 uno::Sequence< OUString > aSequence = ScAccessibleContextBase::getSupportedServiceNames();
324 sal_Int32 nOldSize(aSequence.getLength());
325 aSequence.realloc(nOldSize + 1);
327 aSequence[nOldSize] = "com.sun.star.text.AccessibleHeaderFooterView";
329 return aSequence;
332 //==== internal =========================================================
334 OUString SAL_CALL ScAccessiblePageHeader::createAccessibleDescription()
335 throw (uno::RuntimeException)
337 OUString sDesc(SC_RESSTR(mbHeader ? STR_ACC_HEADER_DESCR : STR_ACC_FOOTER_DESCR));
338 return sDesc.replaceFirst("%1", SC_RESSTR(SCSTR_UNKNOWN));
341 OUString SAL_CALL ScAccessiblePageHeader::createAccessibleName()
342 throw (uno::RuntimeException, std::exception)
344 OUString sName(SC_RESSTR(mbHeader ? STR_ACC_HEADER_NAME : STR_ACC_FOOTER_NAME));
345 return sName.replaceFirst("%1", SC_RESSTR(SCSTR_UNKNOWN));
348 Rectangle ScAccessiblePageHeader::GetBoundingBoxOnScreen() const throw (uno::RuntimeException, std::exception)
350 Rectangle aCellRect(GetBoundingBox());
351 if (mpViewShell)
353 vcl::Window* pWindow = mpViewShell->GetWindow();
354 if (pWindow)
356 Rectangle aRect = pWindow->GetWindowExtentsRelative(NULL);
357 aCellRect.setX(aCellRect.getX() + aRect.getX());
358 aCellRect.setY(aCellRect.getY() + aRect.getY());
361 return aCellRect;
364 Rectangle ScAccessiblePageHeader::GetBoundingBox() const throw (uno::RuntimeException, std::exception)
366 Rectangle aRect;
367 if (mpViewShell)
369 const ScPreviewLocationData& rData = mpViewShell->GetLocationData();
370 if ( mbHeader )
371 rData.GetHeaderPosition( aRect );
372 else
373 rData.GetFooterPosition( aRect );
375 // the Rectangle could contain negative coordinates so it should be cliped
376 Rectangle aClipRect(Point(0, 0), aRect.GetSize());
377 vcl::Window* pWindow = mpViewShell->GetWindow();
378 if (pWindow)
379 aClipRect = pWindow->GetWindowExtentsRelative(pWindow->GetAccessibleParentWindow());
380 aRect = aClipRect.GetIntersection(aRect);
382 if (aRect.IsEmpty())
383 aRect.SetSize(Size(-1, -1));
385 return aRect;
388 bool ScAccessiblePageHeader::IsDefunc( const uno::Reference<XAccessibleStateSet>& rxParentStates )
390 return ScAccessibleContextBase::IsDefunc() || (mpViewShell == NULL) || !getAccessibleParent().is() ||
391 (rxParentStates.is() && rxParentStates->contains(AccessibleStateType::DEFUNC));
394 void ScAccessiblePageHeader::AddChild(const EditTextObject* pArea, sal_uInt32 nIndex, SvxAdjust eAdjust)
396 if (pArea && (!pArea->GetText(0).isEmpty() || (pArea->GetParagraphCount() > 1)))
398 if (maAreas[nIndex])
400 if (!ScGlobal::EETextObjEqual(maAreas[nIndex]->GetEditTextObject(), pArea))
402 maAreas[nIndex]->release();
403 maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, mbHeader, eAdjust);
404 maAreas[nIndex]->acquire();
407 else
409 maAreas[nIndex] = new ScAccessiblePageHeaderArea(this, mpViewShell, pArea, mbHeader, eAdjust);
410 maAreas[nIndex]->acquire();
412 ++mnChildCount;
414 else
416 if (maAreas[nIndex])
418 maAreas[nIndex]->release();
419 maAreas[nIndex] = NULL;
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */