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 "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"
30 #include "scresid.hxx"
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>
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::accessibility
;
51 const sal_uInt8 MAX_AREAS
= 3;
53 //===== internal ============================================================
56 void operator() (ScAccessiblePageHeaderArea
* pArea
)
65 void operator() (ScAccessiblePageHeaderArea
*& pArea
)
74 void operator() (ScAccessiblePageHeaderArea
*& pArea
)
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
),
92 maAreas(MAX_AREAS
, NULL
),
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
);
109 void SAL_CALL
ScAccessiblePageHeader::disposing()
111 SolarMutexGuard aGuard
;
114 mpViewShell
->RemoveAccessibilityObject(*this);
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
);
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());
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
;
188 sal_Int32
nCount(getAccessibleChildCount()); // fill the areas
192 // return the first with content, because they have all the same Bounding Box
194 while(!xRet
.is() && i
< MAX_AREAS
)
207 void SAL_CALL
ScAccessiblePageHeader::grabFocus() throw (uno::RuntimeException
, std::exception
)
209 SolarMutexGuard aGuard
;
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
;
226 if((mnChildCount
< 0) && mpViewShell
)
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
);
235 sal_uInt16
nPageWhichId(0);
237 nPageWhichId
= mpViewShell
->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT
: ATTR_PAGE_HEADERRIGHT
;
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
);
251 uno::Reference
< XAccessible
> SAL_CALL
ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex
)
252 throw (lang::IndexOutOfBoundsException
, uno::RuntimeException
, std::exception
)
254 SolarMutexGuard aGuard
;
257 uno::Reference
<XAccessible
> xRet
;
260 getAccessibleChildCount();
262 ScHFAreas::iterator aItr
= maAreas
.begin();
263 ScHFAreas::iterator aEndItr
= maAreas
.end();
264 while (!xRet
.is() && (nIndex
>= 0) && (aItr
!= aEndItr
))
278 throw lang::IndexOutOfBoundsException();
283 sal_Int32 SAL_CALL
ScAccessiblePageHeader::getAccessibleIndexInParent() throw (uno::RuntimeException
, std::exception
)
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
);
303 pStateSet
->AddState(AccessibleStateType::ENABLED
);
304 pStateSet
->AddState(AccessibleStateType::OPAQUE
);
306 pStateSet
->AddState(AccessibleStateType::SHOWING
);
308 pStateSet
->AddState(AccessibleStateType::VISIBLE
);
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";
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());
353 vcl::Window
* pWindow
= mpViewShell
->GetWindow();
356 Rectangle aRect
= pWindow
->GetWindowExtentsRelative(NULL
);
357 aCellRect
.setX(aCellRect
.getX() + aRect
.getX());
358 aCellRect
.setY(aCellRect
.getY() + aRect
.getY());
364 Rectangle
ScAccessiblePageHeader::GetBoundingBox() const throw (uno::RuntimeException
, std::exception
)
369 const ScPreviewLocationData
& rData
= mpViewShell
->GetLocationData();
371 rData
.GetHeaderPosition( aRect
);
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();
379 aClipRect
= pWindow
->GetWindowExtentsRelative(pWindow
->GetAccessibleParentWindow());
380 aRect
= aClipRect
.GetIntersection(aRect
);
383 aRect
.SetSize(Size(-1, -1));
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)))
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();
409 maAreas
[nIndex
] = new ScAccessiblePageHeaderArea(this, mpViewShell
, pArea
, mbHeader
, eAdjust
);
410 maAreas
[nIndex
]->acquire();
418 maAreas
[nIndex
]->release();
419 maAreas
[nIndex
] = NULL
;
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */