xref: /AOO41X/main/unotools/source/accessibility/accessiblestatesethelper.cxx (revision b5088357f810cb81479bbbd0e021cd3c9835ca0d)
1*b5088357SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*b5088357SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*b5088357SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*b5088357SAndrew Rist  * distributed with this work for additional information
6*b5088357SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*b5088357SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*b5088357SAndrew Rist  * "License"); you may not use this file except in compliance
9*b5088357SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*b5088357SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*b5088357SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*b5088357SAndrew Rist  * software distributed under the License is distributed on an
15*b5088357SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b5088357SAndrew Rist  * KIND, either express or implied.  See the License for the
17*b5088357SAndrew Rist  * specific language governing permissions and limitations
18*b5088357SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*b5088357SAndrew Rist  *************************************************************/
21*b5088357SAndrew Rist 
22*b5088357SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_unotools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "unotools/accessiblestatesethelper.hxx"
29cdf0e10cSrcweir #include <rtl/uuid.h>
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #if 0
33cdf0e10cSrcweir #include <bitset>
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir 
36cdf0e10cSrcweir // defines how many states the bitfield can contain
37cdf0e10cSrcweir // it has the size of 64 because I use a uInt64
38cdf0e10cSrcweir #define BITFIELDSIZE 64
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace	::utl;
41cdf0e10cSrcweir using namespace	::rtl;
42cdf0e10cSrcweir using namespace	::com::sun::star;
43cdf0e10cSrcweir using namespace	::com::sun::star::accessibility;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir class AccessibleStateSetHelperImpl
46cdf0e10cSrcweir {
47cdf0e10cSrcweir public:
48cdf0e10cSrcweir 	AccessibleStateSetHelperImpl();
49cdf0e10cSrcweir 	AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl);
50cdf0e10cSrcweir 	~AccessibleStateSetHelperImpl();
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	sal_Bool IsEmpty ()
53cdf0e10cSrcweir 		throw (uno::RuntimeException);
54cdf0e10cSrcweir 	sal_Bool Contains (sal_Int16 aState)
55cdf0e10cSrcweir 		throw (uno::RuntimeException);
56cdf0e10cSrcweir     uno::Sequence<sal_Int16> GetStates()
57cdf0e10cSrcweir         throw (uno::RuntimeException);
58cdf0e10cSrcweir 	void AddState(sal_Int16 aState)
59cdf0e10cSrcweir 		throw (uno::RuntimeException);
60cdf0e10cSrcweir 	void RemoveState(sal_Int16 aState)
61cdf0e10cSrcweir 		throw (uno::RuntimeException);
62cdf0e10cSrcweir 	sal_Bool Compare(const AccessibleStateSetHelperImpl* pComparativeValue,
63cdf0e10cSrcweir 						AccessibleStateSetHelperImpl* pOldStates,
64cdf0e10cSrcweir 						AccessibleStateSetHelperImpl* pNewStates)
65cdf0e10cSrcweir 		throw (uno::RuntimeException);
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	inline void	AddStates( const sal_Int64 _nStates ) SAL_THROW( ( ) );
68cdf0e10cSrcweir 
69cdf0e10cSrcweir private:
70cdf0e10cSrcweir #if 0
71cdf0e10cSrcweir 	::std::bitset<BITFIELDSIZE> maStates; //Bitfield
72cdf0e10cSrcweir #endif
73cdf0e10cSrcweir 	sal_uInt64 maStates;
74cdf0e10cSrcweir };
75cdf0e10cSrcweir 
AccessibleStateSetHelperImpl()76cdf0e10cSrcweir AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl()
77cdf0e10cSrcweir 	: maStates(0)
78cdf0e10cSrcweir {
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl & rImpl)81cdf0e10cSrcweir AccessibleStateSetHelperImpl::AccessibleStateSetHelperImpl(const AccessibleStateSetHelperImpl& rImpl)
82cdf0e10cSrcweir 	: maStates(rImpl.maStates)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
~AccessibleStateSetHelperImpl()86cdf0e10cSrcweir AccessibleStateSetHelperImpl::~AccessibleStateSetHelperImpl()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
IsEmpty()90cdf0e10cSrcweir inline sal_Bool AccessibleStateSetHelperImpl::IsEmpty ()
91cdf0e10cSrcweir 	throw (uno::RuntimeException)
92cdf0e10cSrcweir {
93cdf0e10cSrcweir #if 0
94cdf0e10cSrcweir 	return maStates.none();
95cdf0e10cSrcweir #endif
96cdf0e10cSrcweir 	return maStates == 0;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
Contains(sal_Int16 aState)99cdf0e10cSrcweir inline sal_Bool AccessibleStateSetHelperImpl::Contains (sal_Int16 aState)
100cdf0e10cSrcweir 	throw (uno::RuntimeException)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir 	DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
103cdf0e10cSrcweir #if 0
104cdf0e10cSrcweir 	return maStates.test(aState);
105cdf0e10cSrcweir #endif
106cdf0e10cSrcweir 	sal_uInt64 aTempBitSet(1);
107cdf0e10cSrcweir 	aTempBitSet <<= aState;
108cdf0e10cSrcweir 	return ((aTempBitSet & maStates) != 0);
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
GetStates()111cdf0e10cSrcweir inline uno::Sequence<sal_Int16> AccessibleStateSetHelperImpl::GetStates()
112cdf0e10cSrcweir     throw (uno::RuntimeException)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir     uno::Sequence<sal_Int16> aRet(BITFIELDSIZE);
115cdf0e10cSrcweir     sal_Int16* pSeq = aRet.getArray();
116cdf0e10cSrcweir     sal_Int16 nStateCount(0);
117cdf0e10cSrcweir     for (sal_Int16 i = 0; i < BITFIELDSIZE; ++i)
118cdf0e10cSrcweir         if (Contains(i))
119cdf0e10cSrcweir         {
120cdf0e10cSrcweir             *pSeq = i;
121cdf0e10cSrcweir             ++pSeq;
122cdf0e10cSrcweir             ++nStateCount;
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir     aRet.realloc(nStateCount);
125cdf0e10cSrcweir     return aRet;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
AddStates(const sal_Int64 _nStates)128cdf0e10cSrcweir inline void AccessibleStateSetHelperImpl::AddStates( const sal_Int64 _nStates ) SAL_THROW( ( ) )
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 	maStates |= _nStates;
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
AddState(sal_Int16 aState)133cdf0e10cSrcweir inline void AccessibleStateSetHelperImpl::AddState(sal_Int16 aState)
134cdf0e10cSrcweir 	throw (uno::RuntimeException)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
137cdf0e10cSrcweir #if 0
138cdf0e10cSrcweir 	maStates.set(aState);
139cdf0e10cSrcweir #endif
140cdf0e10cSrcweir 	sal_uInt64 aTempBitSet(1);
141cdf0e10cSrcweir 	aTempBitSet <<= aState;
142cdf0e10cSrcweir 	maStates |= aTempBitSet;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
RemoveState(sal_Int16 aState)145cdf0e10cSrcweir inline void AccessibleStateSetHelperImpl::RemoveState(sal_Int16 aState)
146cdf0e10cSrcweir 	throw (uno::RuntimeException)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir 	DBG_ASSERT(aState < BITFIELDSIZE, "the statesset is too small");
149cdf0e10cSrcweir #if 0
150cdf0e10cSrcweir 	maStates.set(aState, 0);
151cdf0e10cSrcweir #endif
152cdf0e10cSrcweir 	sal_uInt64 aTempBitSet(1);
153cdf0e10cSrcweir 	aTempBitSet <<= aState;
154cdf0e10cSrcweir 	aTempBitSet = ~aTempBitSet;
155cdf0e10cSrcweir 	maStates &= aTempBitSet;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
Compare(const AccessibleStateSetHelperImpl * pComparativeValue,AccessibleStateSetHelperImpl * pOldStates,AccessibleStateSetHelperImpl * pNewStates)158cdf0e10cSrcweir inline sal_Bool AccessibleStateSetHelperImpl::Compare(
159cdf0e10cSrcweir 	const AccessibleStateSetHelperImpl* pComparativeValue,
160cdf0e10cSrcweir 		AccessibleStateSetHelperImpl* pOldStates,
161cdf0e10cSrcweir 		AccessibleStateSetHelperImpl* pNewStates)
162cdf0e10cSrcweir 	throw (uno::RuntimeException)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir 	sal_Bool bResult(sal_False);
165cdf0e10cSrcweir 	if (pComparativeValue && pOldStates && pNewStates)
166cdf0e10cSrcweir 	{
167cdf0e10cSrcweir 		if (maStates == pComparativeValue->maStates)
168cdf0e10cSrcweir 			bResult = sal_True;
169cdf0e10cSrcweir 		else
170cdf0e10cSrcweir 		{
171cdf0e10cSrcweir #if 0
172cdf0e10cSrcweir 			std::bitset<BITFIELDSIZE> aTempBitSet(maStates);
173cdf0e10cSrcweir #endif
174cdf0e10cSrcweir 			sal_uInt64 aTempBitSet(maStates);
175cdf0e10cSrcweir 			aTempBitSet ^= pComparativeValue->maStates;
176cdf0e10cSrcweir 			pOldStates->maStates = aTempBitSet;
177cdf0e10cSrcweir 			pOldStates->maStates &= maStates;
178cdf0e10cSrcweir 			pNewStates->maStates = aTempBitSet;
179cdf0e10cSrcweir 			pNewStates->maStates &= pComparativeValue->maStates;
180cdf0e10cSrcweir 		}
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir 	return bResult;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 
186cdf0e10cSrcweir //=====  internal  ============================================================
187cdf0e10cSrcweir 
AccessibleStateSetHelper()188cdf0e10cSrcweir AccessibleStateSetHelper::AccessibleStateSetHelper ()
189cdf0e10cSrcweir 	: mpHelperImpl(NULL)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir 	mpHelperImpl = new AccessibleStateSetHelperImpl();
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
AccessibleStateSetHelper(const sal_Int64 _nInitialStates)194cdf0e10cSrcweir AccessibleStateSetHelper::AccessibleStateSetHelper ( const sal_Int64 _nInitialStates )
195cdf0e10cSrcweir 	: mpHelperImpl(NULL)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir 	mpHelperImpl = new AccessibleStateSetHelperImpl();
198cdf0e10cSrcweir 	mpHelperImpl->AddStates( _nInitialStates );
199cdf0e10cSrcweir }
200cdf0e10cSrcweir 
AccessibleStateSetHelper(const AccessibleStateSetHelper & rHelper)201cdf0e10cSrcweir AccessibleStateSetHelper::AccessibleStateSetHelper (const AccessibleStateSetHelper& rHelper)
202cdf0e10cSrcweir 	: cppu::WeakImplHelper1<XAccessibleStateSet>()
203cdf0e10cSrcweir     , mpHelperImpl(NULL)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir 	if (rHelper.mpHelperImpl)
206cdf0e10cSrcweir 		mpHelperImpl = new AccessibleStateSetHelperImpl(*rHelper.mpHelperImpl);
207cdf0e10cSrcweir 	else
208cdf0e10cSrcweir 		mpHelperImpl = new AccessibleStateSetHelperImpl();
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
~AccessibleStateSetHelper(void)211cdf0e10cSrcweir AccessibleStateSetHelper::~AccessibleStateSetHelper(void)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	delete mpHelperImpl;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir //=====  XAccessibleStateSet  ==============================================
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     /** Checks whether the current state set is empty.
219cdf0e10cSrcweir 
220cdf0e10cSrcweir         @return
221cdf0e10cSrcweir             Returns <TRUE/> if there is no state in this state set and
222cdf0e10cSrcweir             <FALSE/> if there is at least one state set in it.
223cdf0e10cSrcweir     */
isEmpty()224cdf0e10cSrcweir sal_Bool SAL_CALL AccessibleStateSetHelper::isEmpty ()
225cdf0e10cSrcweir 	throw (uno::RuntimeException)
226cdf0e10cSrcweir {
227cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
228cdf0e10cSrcweir 	return mpHelperImpl->IsEmpty();
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
231cdf0e10cSrcweir     /** Checks if the given state is a member of the state set of this
232cdf0e10cSrcweir         object.
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         @param aState
235cdf0e10cSrcweir             The state for which to check membership.  This has to be one of
236cdf0e10cSrcweir             the constants of <type>AccessibleStateType</type>.
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         @return
239cdf0e10cSrcweir             Returns <TRUE/> if the given state is a memeber of this object's
240cdf0e10cSrcweir             state set and <FALSE/> otherwise.
241cdf0e10cSrcweir     */
contains(sal_Int16 aState)242cdf0e10cSrcweir sal_Bool SAL_CALL AccessibleStateSetHelper::contains (sal_Int16 aState)
243cdf0e10cSrcweir 	throw (uno::RuntimeException)
244cdf0e10cSrcweir {
245cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
246cdf0e10cSrcweir 	return mpHelperImpl->Contains(aState);
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249cdf0e10cSrcweir     /** Checks if all of the given states are in this object's state
250cdf0e10cSrcweir         set.
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         @param aStateSet
253cdf0e10cSrcweir             This sequence of states is interpreted as set and every of its
254cdf0e10cSrcweir             members, duplicates are ignored, is checked for membership in
255cdf0e10cSrcweir             this object's state set.  Each state has to be one of the
256cdf0e10cSrcweir             constants of <type>AccessibleStateType</type>.
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         @return
259cdf0e10cSrcweir             Returns <TRUE/> if all states of the given state set are members
260cdf0e10cSrcweir             of this object's state set.  <FALSE/> is returned if at least
261cdf0e10cSrcweir             one of the states in the given state is not a member of this
262cdf0e10cSrcweir             object's state set.
263cdf0e10cSrcweir     */
containsAll(const uno::Sequence<sal_Int16> & rStateSet)264cdf0e10cSrcweir sal_Bool SAL_CALL AccessibleStateSetHelper::containsAll
265cdf0e10cSrcweir 	(const uno::Sequence<sal_Int16>& rStateSet)
266cdf0e10cSrcweir 	throw (uno::RuntimeException)
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
269cdf0e10cSrcweir 	sal_Int32 nCount(rStateSet.getLength());
270cdf0e10cSrcweir 	const sal_Int16* pStates = rStateSet.getConstArray();
271cdf0e10cSrcweir 	sal_Int32 i = 0;
272cdf0e10cSrcweir 	sal_Bool bFound(sal_True);
273cdf0e10cSrcweir 	while (i < nCount)
274cdf0e10cSrcweir 	{
275cdf0e10cSrcweir 		bFound = mpHelperImpl->Contains(pStates[i]);
276cdf0e10cSrcweir 		i++;
277cdf0e10cSrcweir 	}
278cdf0e10cSrcweir 	return bFound;
279cdf0e10cSrcweir }
280cdf0e10cSrcweir 
getStates()281cdf0e10cSrcweir uno::Sequence<sal_Int16> SAL_CALL AccessibleStateSetHelper::getStates()
282cdf0e10cSrcweir     throw (uno::RuntimeException)
283cdf0e10cSrcweir {
284cdf0e10cSrcweir     ::vos::OGuard aGuard(maMutex);
285cdf0e10cSrcweir     return mpHelperImpl->GetStates();
286cdf0e10cSrcweir }
287cdf0e10cSrcweir 
AddState(sal_Int16 aState)288cdf0e10cSrcweir void AccessibleStateSetHelper::AddState(sal_Int16 aState)
289cdf0e10cSrcweir 	throw (uno::RuntimeException)
290cdf0e10cSrcweir {
291cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
292cdf0e10cSrcweir 	mpHelperImpl->AddState(aState);
293cdf0e10cSrcweir }
294cdf0e10cSrcweir 
RemoveState(sal_Int16 aState)295cdf0e10cSrcweir void AccessibleStateSetHelper::RemoveState(sal_Int16 aState)
296cdf0e10cSrcweir 	throw (uno::RuntimeException)
297cdf0e10cSrcweir {
298cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
299cdf0e10cSrcweir 	mpHelperImpl->RemoveState(aState);
300cdf0e10cSrcweir }
301cdf0e10cSrcweir 
Compare(const AccessibleStateSetHelper & rComparativeValue,AccessibleStateSetHelper & rOldStates,AccessibleStateSetHelper & rNewStates)302cdf0e10cSrcweir sal_Bool AccessibleStateSetHelper::Compare(
303cdf0e10cSrcweir 	const AccessibleStateSetHelper& rComparativeValue,
304cdf0e10cSrcweir 		AccessibleStateSetHelper& rOldStates,
305cdf0e10cSrcweir 		AccessibleStateSetHelper& rNewStates)
306cdf0e10cSrcweir 	throw (uno::RuntimeException)
307cdf0e10cSrcweir {
308cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
309cdf0e10cSrcweir 	return mpHelperImpl->Compare(rComparativeValue.mpHelperImpl,
310cdf0e10cSrcweir 		rOldStates.mpHelperImpl, rNewStates.mpHelperImpl);
311cdf0e10cSrcweir }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir //=====  XTypeProvider  =======================================================
314cdf0e10cSrcweir 
315cdf0e10cSrcweir uno::Sequence< ::com::sun::star::uno::Type>
getTypes(void)316cdf0e10cSrcweir 	AccessibleStateSetHelper::getTypes (void)
317cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
318cdf0e10cSrcweir {
319cdf0e10cSrcweir 	const ::com::sun::star::uno::Type aTypeList[] = {
320cdf0e10cSrcweir     	::getCppuType((const uno::Reference<
321cdf0e10cSrcweir         	XAccessibleStateSet>*)0),
322cdf0e10cSrcweir     	::getCppuType((const uno::Reference<
323cdf0e10cSrcweir         	lang::XTypeProvider>*)0)
324cdf0e10cSrcweir 		};
325cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type>
326cdf0e10cSrcweir     	aTypeSequence (aTypeList, 2);
327cdf0e10cSrcweir 	return aTypeSequence;
328cdf0e10cSrcweir }
329cdf0e10cSrcweir 
330cdf0e10cSrcweir uno::Sequence<sal_Int8> SAL_CALL
getImplementationId(void)331cdf0e10cSrcweir 	AccessibleStateSetHelper::getImplementationId (void)
332cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
333cdf0e10cSrcweir {
334cdf0e10cSrcweir     ::vos::OGuard aGuard (maMutex);
335cdf0e10cSrcweir 	static uno::Sequence<sal_Int8> aId;
336cdf0e10cSrcweir 	if (aId.getLength() == 0)
337cdf0e10cSrcweir 	{
338cdf0e10cSrcweir 		aId.realloc (16);
339cdf0e10cSrcweir 		rtl_createUuid ((sal_uInt8 *)aId.getArray(), 0, sal_True);
340cdf0e10cSrcweir 	}
341cdf0e10cSrcweir 	return aId;
342cdf0e10cSrcweir }
343