1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b3f79822SAndrew Rist  * distributed with this work for additional information
6*b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*b3f79822SAndrew Rist  *
11*b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist  *
13*b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b3f79822SAndrew Rist  * specific language governing permissions and limitations
18*b3f79822SAndrew Rist  * under the License.
19*b3f79822SAndrew Rist  *
20*b3f79822SAndrew Rist  *************************************************************/
21*b3f79822SAndrew Rist 
22*b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir #include <tools/gen.hxx>
27cdf0e10cSrcweir #include "AccessiblePageHeaderArea.hxx"
28cdf0e10cSrcweir #include "AccessibleText.hxx"
29cdf0e10cSrcweir #include "AccessibilityHints.hxx"
30cdf0e10cSrcweir #include "unoguard.hxx"
31cdf0e10cSrcweir #include "editsrc.hxx"
32cdf0e10cSrcweir #include "prevwsh.hxx"
33cdf0e10cSrcweir #include "prevloc.hxx"
34cdf0e10cSrcweir #include "scresid.hxx"
35cdf0e10cSrcweir #ifndef SC_SC_HRC
36cdf0e10cSrcweir #include "sc.hrc"
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #ifndef _COM_SUN_STAR_ACCESSIBILITY_XACCESSIBLEROLE_HPP_
40cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleRole.hpp>
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir #ifndef _COM_SUN_STAR_ACCESSIBILITY_XACCESSIBLESTATETYPE_HPP_
43cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleStateType.hpp>
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleEventId.hpp>
46cdf0e10cSrcweir #include <editeng/editobj.hxx>
47cdf0e10cSrcweir #include <svx/AccessibleTextHelper.hxx>
48cdf0e10cSrcweir #include <rtl/uuid.h>
49cdf0e10cSrcweir #include <unotools/accessiblestatesethelper.hxx>
50cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
51cdf0e10cSrcweir #include <toolkit/helper/convert.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir using namespace	::com::sun::star;
54cdf0e10cSrcweir using namespace	::com::sun::star::accessibility;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	//=====  internal  ========================================================
57cdf0e10cSrcweir 
ScAccessiblePageHeaderArea(const uno::Reference<XAccessible> & rxParent,ScPreviewShell * pViewShell,const EditTextObject * pEditObj,sal_Bool bHeader,SvxAdjust eAdjust)58cdf0e10cSrcweir ScAccessiblePageHeaderArea::ScAccessiblePageHeaderArea(
59cdf0e10cSrcweir         const uno::Reference<XAccessible>& rxParent,
60cdf0e10cSrcweir         ScPreviewShell* pViewShell,
61cdf0e10cSrcweir         const EditTextObject* pEditObj,
62cdf0e10cSrcweir         sal_Bool bHeader,
63cdf0e10cSrcweir         SvxAdjust eAdjust)
64cdf0e10cSrcweir         : ScAccessibleContextBase(rxParent, AccessibleRole::TEXT),
65cdf0e10cSrcweir         mpEditObj(pEditObj->Clone()),
66cdf0e10cSrcweir     	mpTextHelper(NULL),
67cdf0e10cSrcweir         mpViewShell(pViewShell),
68cdf0e10cSrcweir         mbHeader(bHeader),
69cdf0e10cSrcweir         meAdjust(eAdjust)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	if (mpViewShell)
72cdf0e10cSrcweir 		mpViewShell->AddAccessibilityObject(*this);
73cdf0e10cSrcweir }
74cdf0e10cSrcweir 
~ScAccessiblePageHeaderArea(void)75cdf0e10cSrcweir ScAccessiblePageHeaderArea::~ScAccessiblePageHeaderArea(void)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	if (!ScAccessibleContextBase::IsDefunc() && !rBHelper.bInDispose)
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		// increment refcount to prevent double call off dtor
80cdf0e10cSrcweir 		osl_incrementInterlockedCount( &m_refCount );
81cdf0e10cSrcweir 		dispose();
82cdf0e10cSrcweir 	}
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
disposing()85cdf0e10cSrcweir void SAL_CALL ScAccessiblePageHeaderArea::disposing()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     ScUnoGuard aGuard;
88cdf0e10cSrcweir 	if (mpViewShell)
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 		mpViewShell->RemoveAccessibilityObject(*this);
91cdf0e10cSrcweir 		mpViewShell = NULL;
92cdf0e10cSrcweir 	}
93cdf0e10cSrcweir 	if (mpTextHelper)
94cdf0e10cSrcweir 		DELETEZ(mpTextHelper);
95cdf0e10cSrcweir     if (mpEditObj)
96cdf0e10cSrcweir         DELETEZ(mpEditObj);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	ScAccessibleContextBase::disposing();
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir //=====  SfxListener  =====================================================
102cdf0e10cSrcweir 
Notify(SfxBroadcaster & rBC,const SfxHint & rHint)103cdf0e10cSrcweir void ScAccessiblePageHeaderArea::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir 	if (rHint.ISA( SfxSimpleHint ) )
106cdf0e10cSrcweir 	{
107cdf0e10cSrcweir 		const SfxSimpleHint& rRef = (const SfxSimpleHint&)rHint;
108cdf0e10cSrcweir 		// only notify if child exist, otherwise it is not necessary
109cdf0e10cSrcweir         if (rRef.GetId() == SC_HINT_ACC_VISAREACHANGED)
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             if (mpTextHelper)
112cdf0e10cSrcweir                 mpTextHelper->UpdateChildren();
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             AccessibleEventObject aEvent;
115cdf0e10cSrcweir             aEvent.EventId = AccessibleEventId::VISIBLE_DATA_CHANGED;
116cdf0e10cSrcweir 			aEvent.Source = uno::Reference< XAccessibleContext >(this);
117cdf0e10cSrcweir             CommitChange(aEvent);
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 	ScAccessibleContextBase::Notify(rBC, rHint);
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 	//=====  XAccessibleComponent  ============================================
123cdf0e10cSrcweir 
getAccessibleAtPoint(const awt::Point & rPoint)124cdf0e10cSrcweir uno::Reference< XAccessible > SAL_CALL ScAccessiblePageHeaderArea::getAccessibleAtPoint(
125cdf0e10cSrcweir 		const awt::Point& rPoint )
126cdf0e10cSrcweir 		throw (uno::RuntimeException)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     uno::Reference<XAccessible> xRet;
129cdf0e10cSrcweir     if (containsPoint(rPoint))
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir  	    ScUnoGuard aGuard;
132cdf0e10cSrcweir         IsObjectValid();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     	if(!mpTextHelper)
135cdf0e10cSrcweir 	    	CreateTextHelper();
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         xRet = mpTextHelper->GetAt(rPoint);
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     return xRet;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	//=====  XAccessibleContext  ==============================================
144cdf0e10cSrcweir 
145cdf0e10cSrcweir sal_Int32 SAL_CALL
getAccessibleChildCount(void)146cdf0e10cSrcweir 	ScAccessiblePageHeaderArea::getAccessibleChildCount(void)
147cdf0e10cSrcweir     				throw (uno::RuntimeException)
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	ScUnoGuard aGuard;
150cdf0e10cSrcweir     IsObjectValid();
151cdf0e10cSrcweir 	if (!mpTextHelper)
152cdf0e10cSrcweir 		CreateTextHelper();
153cdf0e10cSrcweir 	return mpTextHelper->GetChildCount();
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir uno::Reference< XAccessible > SAL_CALL
getAccessibleChild(sal_Int32 nIndex)157cdf0e10cSrcweir 	ScAccessiblePageHeaderArea::getAccessibleChild(sal_Int32 nIndex)
158cdf0e10cSrcweir         throw (uno::RuntimeException,
159cdf0e10cSrcweir 		lang::IndexOutOfBoundsException)
160cdf0e10cSrcweir {
161cdf0e10cSrcweir 	ScUnoGuard aGuard;
162cdf0e10cSrcweir     IsObjectValid();
163cdf0e10cSrcweir 	if (!mpTextHelper)
164cdf0e10cSrcweir 		CreateTextHelper();
165cdf0e10cSrcweir 	return mpTextHelper->GetChild(nIndex);
166cdf0e10cSrcweir }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir uno::Reference<XAccessibleStateSet> SAL_CALL
getAccessibleStateSet(void)169cdf0e10cSrcweir 	ScAccessiblePageHeaderArea::getAccessibleStateSet(void)
170cdf0e10cSrcweir     throw (uno::RuntimeException)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir 	ScUnoGuard aGuard;
173cdf0e10cSrcweir 	uno::Reference<XAccessibleStateSet> xParentStates;
174cdf0e10cSrcweir 	if (getAccessibleParent().is())
175cdf0e10cSrcweir 	{
176cdf0e10cSrcweir 		uno::Reference<XAccessibleContext> xParentContext = getAccessibleParent()->getAccessibleContext();
177cdf0e10cSrcweir 		xParentStates = xParentContext->getAccessibleStateSet();
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir 	utl::AccessibleStateSetHelper* pStateSet = new utl::AccessibleStateSetHelper();
180cdf0e10cSrcweir 	if (IsDefunc())
181cdf0e10cSrcweir 		pStateSet->AddState(AccessibleStateType::DEFUNC);
182cdf0e10cSrcweir     else
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir 	    pStateSet->AddState(AccessibleStateType::ENABLED);
185cdf0e10cSrcweir 	    pStateSet->AddState(AccessibleStateType::MULTI_LINE);
186cdf0e10cSrcweir 	    if (isShowing())
187cdf0e10cSrcweir 		    pStateSet->AddState(AccessibleStateType::SHOWING);
188cdf0e10cSrcweir 	    if (isVisible())
189cdf0e10cSrcweir 		    pStateSet->AddState(AccessibleStateType::VISIBLE);
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir 	return pStateSet;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir //=====  XServiceInfo  ========================================================
195cdf0e10cSrcweir 
196cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getImplementationName(void)197cdf0e10cSrcweir    	ScAccessiblePageHeaderArea::getImplementationName(void)
198cdf0e10cSrcweir     throw (uno::RuntimeException)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir     return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM ("ScAccessiblePageHeaderArea"));
201cdf0e10cSrcweir }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir uno::Sequence< ::rtl::OUString> SAL_CALL
getSupportedServiceNames(void)204cdf0e10cSrcweir    	ScAccessiblePageHeaderArea::getSupportedServiceNames(void)
205cdf0e10cSrcweir     throw (uno::RuntimeException)
206cdf0e10cSrcweir {
207cdf0e10cSrcweir 	uno::Sequence< ::rtl::OUString > aSequence = ScAccessibleContextBase::getSupportedServiceNames();
208cdf0e10cSrcweir     sal_Int32 nOldSize(aSequence.getLength());
209cdf0e10cSrcweir     aSequence.realloc(nOldSize + 1);
210cdf0e10cSrcweir     ::rtl::OUString* pNames = aSequence.getArray();
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 	pNames[nOldSize] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sheet.AccessiblePageHeaderFooterAreasView"));
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	return aSequence;
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir //=====  XTypeProvider  =======================================================
218cdf0e10cSrcweir 
219cdf0e10cSrcweir uno::Sequence<sal_Int8> SAL_CALL
getImplementationId(void)220cdf0e10cSrcweir 	ScAccessiblePageHeaderArea::getImplementationId(void)
221cdf0e10cSrcweir     throw (uno::RuntimeException)
222cdf0e10cSrcweir {
223cdf0e10cSrcweir     ScUnoGuard aGuard;
224cdf0e10cSrcweir     IsObjectValid();
225cdf0e10cSrcweir 	static uno::Sequence<sal_Int8> aId;
226cdf0e10cSrcweir 	if (aId.getLength() == 0)
227cdf0e10cSrcweir 	{
228cdf0e10cSrcweir 		aId.realloc (16);
229cdf0e10cSrcweir 		rtl_createUuid (reinterpret_cast<sal_uInt8 *>(aId.getArray()), 0, sal_True);
230cdf0e10cSrcweir 	}
231cdf0e10cSrcweir 	return aId;
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir //===== internal ==============================================================
createAccessibleDescription(void)235cdf0e10cSrcweir rtl::OUString SAL_CALL ScAccessiblePageHeaderArea::createAccessibleDescription(void)
236cdf0e10cSrcweir     throw(uno::RuntimeException)
237cdf0e10cSrcweir {
238cdf0e10cSrcweir     rtl::OUString sDesc;
239cdf0e10cSrcweir     switch (meAdjust)
240cdf0e10cSrcweir     {
241cdf0e10cSrcweir     case SVX_ADJUST_LEFT :
242cdf0e10cSrcweir         sDesc = String(ScResId(STR_ACC_LEFTAREA_DESCR));
243cdf0e10cSrcweir         break;
244cdf0e10cSrcweir     case SVX_ADJUST_RIGHT:
245cdf0e10cSrcweir         sDesc = String(ScResId(STR_ACC_RIGHTAREA_DESCR));
246cdf0e10cSrcweir         break;
247cdf0e10cSrcweir     case SVX_ADJUST_CENTER:
248cdf0e10cSrcweir         sDesc = String(ScResId(STR_ACC_CENTERAREA_DESCR));
249cdf0e10cSrcweir         break;
250cdf0e10cSrcweir     default:
251cdf0e10cSrcweir         DBG_ERRORFILE("wrong adjustment found");
252cdf0e10cSrcweir     }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     return sDesc;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir 
createAccessibleName(void)257cdf0e10cSrcweir rtl::OUString SAL_CALL ScAccessiblePageHeaderArea::createAccessibleName(void)
258cdf0e10cSrcweir     throw (uno::RuntimeException)
259cdf0e10cSrcweir {
260cdf0e10cSrcweir     rtl::OUString sName;
261cdf0e10cSrcweir     switch (meAdjust)
262cdf0e10cSrcweir     {
263cdf0e10cSrcweir     case SVX_ADJUST_LEFT :
264cdf0e10cSrcweir         sName = String(ScResId(STR_ACC_LEFTAREA_NAME));
265cdf0e10cSrcweir         break;
266cdf0e10cSrcweir     case SVX_ADJUST_RIGHT:
267cdf0e10cSrcweir         sName = String(ScResId(STR_ACC_RIGHTAREA_NAME));
268cdf0e10cSrcweir         break;
269cdf0e10cSrcweir     case SVX_ADJUST_CENTER:
270cdf0e10cSrcweir         sName = String(ScResId(STR_ACC_CENTERAREA_NAME));
271cdf0e10cSrcweir         break;
272cdf0e10cSrcweir     default:
273cdf0e10cSrcweir         DBG_ERRORFILE("wrong adjustment found");
274cdf0e10cSrcweir     }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir     return sName;
277cdf0e10cSrcweir }
278cdf0e10cSrcweir 
GetBoundingBoxOnScreen(void) const279cdf0e10cSrcweir Rectangle ScAccessiblePageHeaderArea::GetBoundingBoxOnScreen(void) const
280cdf0e10cSrcweir     throw(::com::sun::star::uno::RuntimeException)
281cdf0e10cSrcweir {
282cdf0e10cSrcweir 	Rectangle aRect;
283cdf0e10cSrcweir     if (mxParent.is())
284cdf0e10cSrcweir     {
285cdf0e10cSrcweir         uno::Reference<XAccessibleContext> xContext = mxParent->getAccessibleContext();
286cdf0e10cSrcweir         uno::Reference<XAccessibleComponent> xComp(xContext, uno::UNO_QUERY);
287cdf0e10cSrcweir         if (xComp.is())
288cdf0e10cSrcweir         {
289cdf0e10cSrcweir             // has the same size and position on screen like the parent
290cdf0e10cSrcweir             aRect = Rectangle(VCLPoint(xComp->getLocationOnScreen()), VCLRectangle(xComp->getBounds()).GetSize());
291cdf0e10cSrcweir         }
292cdf0e10cSrcweir     }
293cdf0e10cSrcweir 	return aRect;
294cdf0e10cSrcweir }
295cdf0e10cSrcweir 
GetBoundingBox(void) const296cdf0e10cSrcweir Rectangle ScAccessiblePageHeaderArea::GetBoundingBox(void) const
297cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
298cdf0e10cSrcweir {
299cdf0e10cSrcweir 	Rectangle aRect;
300cdf0e10cSrcweir     if (mxParent.is())
301cdf0e10cSrcweir     {
302cdf0e10cSrcweir         uno::Reference<XAccessibleContext> xContext = mxParent->getAccessibleContext();
303cdf0e10cSrcweir         uno::Reference<XAccessibleComponent> xComp(xContext, uno::UNO_QUERY);
304cdf0e10cSrcweir         if (xComp.is())
305cdf0e10cSrcweir         {
306cdf0e10cSrcweir             // has the same size and position on screen like the parent and so the pos is (0, 0)
307cdf0e10cSrcweir             Rectangle aNewRect(Point(0, 0), VCLRectangle(xComp->getBounds()).GetSize());
308cdf0e10cSrcweir             aRect = aNewRect;
309cdf0e10cSrcweir         }
310cdf0e10cSrcweir     }
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	return aRect;
313cdf0e10cSrcweir }
314cdf0e10cSrcweir 
CreateTextHelper()315cdf0e10cSrcweir void ScAccessiblePageHeaderArea::CreateTextHelper()
316cdf0e10cSrcweir {
317cdf0e10cSrcweir 	if (!mpTextHelper)
318cdf0e10cSrcweir 	{
319cdf0e10cSrcweir 		::std::auto_ptr < ScAccessibleTextData > pAccessibleHeaderTextData
320cdf0e10cSrcweir 			(new ScAccessibleHeaderTextData(mpViewShell, mpEditObj, mbHeader, meAdjust));
321cdf0e10cSrcweir 		::std::auto_ptr< SvxEditSource > pEditSource (new ScAccessibilityEditSource(pAccessibleHeaderTextData));
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 		mpTextHelper = new ::accessibility::AccessibleTextHelper(pEditSource );
324cdf0e10cSrcweir         mpTextHelper->SetEventSource(this);
325cdf0e10cSrcweir 	}
326cdf0e10cSrcweir }
327