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