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 .
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"
31 #include "scresid.hxx"
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>
49 using namespace ::com::sun::star
;
50 using namespace ::com::sun::star::accessibility
;
52 const sal_uInt8 MAX_AREAS
= 3;
54 //===== internal ============================================================
57 void operator() (ScAccessiblePageHeaderArea
* pArea
)
66 void operator() (ScAccessiblePageHeaderArea
*& pArea
)
75 void operator() (ScAccessiblePageHeaderArea
*& pArea
)
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
),
93 maAreas(MAX_AREAS
, NULL
),
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
);
110 void SAL_CALL
ScAccessiblePageHeader::disposing()
112 SolarMutexGuard aGuard
;
115 mpViewShell
->RemoveAccessibilityObject(*this);
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());
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
;
189 sal_Int32
nCount(getAccessibleChildCount()); // fill the areas
193 // return the first with content, because they have all the same Bounding Box
195 while(!xRet
.is() && i
< MAX_AREAS
)
208 void SAL_CALL
ScAccessiblePageHeader::grabFocus() throw (uno::RuntimeException
, std::exception
)
210 SolarMutexGuard aGuard
;
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
;
227 if((mnChildCount
< 0) && mpViewShell
)
230 ScDocument
* pDoc
= mpViewShell
->GetDocument();
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
);
238 sal_uInt16
nPageWhichId(0);
240 nPageWhichId
= mpViewShell
->GetLocationData().IsHeaderLeft() ? ATTR_PAGE_HEADERLEFT
: ATTR_PAGE_HEADERRIGHT
;
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
);
255 uno::Reference
< XAccessible
> SAL_CALL
ScAccessiblePageHeader::getAccessibleChild( sal_Int32 nIndex
)
256 throw (lang::IndexOutOfBoundsException
, uno::RuntimeException
, std::exception
)
258 SolarMutexGuard aGuard
;
261 uno::Reference
<XAccessible
> xRet
;
264 getAccessibleChildCount();
266 ScHFAreas::iterator aItr
= maAreas
.begin();
267 ScHFAreas::iterator aEndItr
= maAreas
.end();
268 while (!xRet
.is() && (nIndex
>= 0) && (aItr
!= aEndItr
))
282 throw lang::IndexOutOfBoundsException();
287 sal_Int32 SAL_CALL
ScAccessiblePageHeader::getAccessibleIndexInParent() throw (uno::RuntimeException
, std::exception
)
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
);
307 pStateSet
->AddState(AccessibleStateType::ENABLED
);
308 pStateSet
->AddState(AccessibleStateType::OPAQUE
);
310 pStateSet
->AddState(AccessibleStateType::SHOWING
);
312 pStateSet
->AddState(AccessibleStateType::VISIBLE
);
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";
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());
357 Window
* pWindow
= mpViewShell
->GetWindow();
360 Rectangle aRect
= pWindow
->GetWindowExtentsRelative(NULL
);
361 aCellRect
.setX(aCellRect
.getX() + aRect
.getX());
362 aCellRect
.setY(aCellRect
.getY() + aRect
.getY());
368 Rectangle
ScAccessiblePageHeader::GetBoundingBox() const throw (uno::RuntimeException
, std::exception
)
373 const ScPreviewLocationData
& rData
= mpViewShell
->GetLocationData();
375 rData
.GetHeaderPosition( aRect
);
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();
383 aClipRect
= pWindow
->GetWindowExtentsRelative(pWindow
->GetAccessibleParentWindow());
384 aRect
= aClipRect
.GetIntersection(aRect
);
387 aRect
.SetSize(Size(-1, -1));
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)))
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();
413 maAreas
[nIndex
] = new ScAccessiblePageHeaderArea(this, mpViewShell
, pArea
, mbHeader
, eAdjust
);
414 maAreas
[nIndex
]->acquire();
422 maAreas
[nIndex
]->release();
423 maAreas
[nIndex
] = NULL
;
428 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */