1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 #include <comphelper/accimplaccess.hxx>
27 #include <com/sun/star/accessibility/XAccessible.hpp>
28 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
29 #include <cppuhelper/typeprovider.hxx>
30 
31 #include <set>
32 
33 //.........................................................................
34 namespace comphelper
35 {
36 //.........................................................................
37 
38 #define BITFIELDSIZE	( sizeof( sal_Int64 ) * 8 )
39 	// maximum number of bits we have in a sal_Int64
40 
41 	using ::com::sun::star::uno::Reference;
42 	using ::com::sun::star::uno::Sequence;
43 	using ::com::sun::star::uno::Exception;
44 	using ::com::sun::star::uno::UNO_QUERY;
45 	using ::com::sun::star::uno::RuntimeException;
46 	using ::com::sun::star::lang::XUnoTunnel;
47 	using ::com::sun::star::accessibility::XAccessible;
48 	using ::com::sun::star::accessibility::XAccessibleContext;
49 
50 	//=====================================================================
51 	//= OAccImpl_Impl
52 	//=====================================================================
53 	struct OAccImpl_Impl
54 	{
55 		Reference< XAccessible >	m_xAccParent;
56 		sal_Int64					m_nForeignControlledStates;
57 	};
58 
59 
60 	//=====================================================================
61 	//= OAccessibleImplementationAccess
62 	//=====================================================================
63 	//---------------------------------------------------------------------
OAccessibleImplementationAccess()64 	OAccessibleImplementationAccess::OAccessibleImplementationAccess( )
65 		:m_pImpl( new OAccImpl_Impl	)
66 	{
67 	}
68 
69 	//---------------------------------------------------------------------
~OAccessibleImplementationAccess()70 	OAccessibleImplementationAccess::~OAccessibleImplementationAccess( )
71 	{
72 		delete m_pImpl;
73 		m_pImpl = NULL;
74 	}
75 
76 	//---------------------------------------------------------------------
implGetForeignControlledParent() const77 	Reference< XAccessible > OAccessibleImplementationAccess::implGetForeignControlledParent( ) const
78 	{
79 		return m_pImpl->m_xAccParent;
80 	}
81 
82 	//---------------------------------------------------------------------
setAccessibleParent(const Reference<XAccessible> & _rxAccParent)83 	void OAccessibleImplementationAccess::setAccessibleParent( const Reference< XAccessible >& _rxAccParent )
84 	{
85 		m_pImpl->m_xAccParent = _rxAccParent;
86 	}
87 
88 	//---------------------------------------------------------------------
implGetForeignControlledStates() const89 	sal_Int64 OAccessibleImplementationAccess::implGetForeignControlledStates( ) const
90 	{
91 		return m_pImpl->m_nForeignControlledStates;
92 	}
93 
94 	//---------------------------------------------------------------------
setStateBit(const sal_Int16 _nState,const sal_Bool _bSet)95 	void OAccessibleImplementationAccess::setStateBit( const sal_Int16 _nState, const sal_Bool _bSet )
96 	{
97 		OSL_ENSURE( _nState >= 0 && static_cast< sal_uInt16 >(_nState) < BITFIELDSIZE, "OAccessibleImplementationAccess::setStateBit: no more bits (shutting down the universe now)!" );
98 
99 		sal_uInt64 nBitMask( 1 );
100 		nBitMask <<= _nState;
101 		if ( _bSet )
102 			m_pImpl->m_nForeignControlledStates |= nBitMask;
103 		else
104 			m_pImpl->m_nForeignControlledStates &= ~nBitMask;
105 	}
106 
107 	//---------------------------------------------------------------------
setForeignControlledState(const Reference<XAccessibleContext> & _rxComponent,const sal_Int16 _nState,const sal_Bool _bSet)108 	sal_Bool OAccessibleImplementationAccess::setForeignControlledState( const Reference< XAccessibleContext >& _rxComponent, const sal_Int16 _nState,
109 		const sal_Bool	_bSet )
110 	{
111 		OAccessibleImplementationAccess* pImplementation = getImplementation( _rxComponent );
112 
113 		if ( pImplementation )
114 			pImplementation->setStateBit( _nState, _bSet );
115 
116 		return ( NULL != pImplementation );
117 	}
118 
119 	//---------------------------------------------------------------------
getUnoTunnelImplementationId()120 	const Sequence< sal_Int8 >& OAccessibleImplementationAccess::getUnoTunnelImplementationId()
121 	{
122 		static Sequence< sal_Int8 > aId;
123 		if ( !aId.getLength() )
124 		{
125 			::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
126 			if ( !aId.getLength() )
127 			{
128 				static ::cppu::OImplementationId aImplId;
129 				// unfortunately, the OImplementationId::getImplementationId returns a copy, not a static reference ...
130 				aId = aImplId.getImplementationId();
131 			}
132 		}
133 		return aId;
134 	}
135 
136 	//---------------------------------------------------------------------
getSomething(const Sequence<sal_Int8> & _rIdentifier)137 	sal_Int64 SAL_CALL OAccessibleImplementationAccess::getSomething( const Sequence< sal_Int8 >& _rIdentifier ) throw (RuntimeException)
138 	{
139 		sal_Int64 nReturn( 0 );
140 
141 		if	(	( _rIdentifier.getLength() == 16 )
142 			&&	( 0 == rtl_compareMemory( getUnoTunnelImplementationId().getConstArray(), _rIdentifier.getConstArray(), 16 ) )
143 			)
144 			nReturn = reinterpret_cast< sal_Int64 >( this );
145 
146 		return nReturn;
147 	}
148 
149 	//---------------------------------------------------------------------
getImplementation(const Reference<XAccessibleContext> & _rxComponent)150 	OAccessibleImplementationAccess* OAccessibleImplementationAccess::getImplementation( const Reference< XAccessibleContext >& _rxComponent )
151 	{
152 		OAccessibleImplementationAccess* pImplementation = NULL;
153 		try
154 		{
155 			Reference< XUnoTunnel > xTunnel( _rxComponent, UNO_QUERY );
156 			if ( xTunnel.is() )
157 			{
158 				pImplementation = reinterpret_cast< OAccessibleImplementationAccess* >(
159 						xTunnel->getSomething( getUnoTunnelImplementationId() ) );
160 			}
161 		}
162 		catch( const Exception& )
163 		{
164 			OSL_ENSURE( sal_False, "OAccessibleImplementationAccess::setAccessibleParent: caught an exception while retrieving the implementation!" );
165 		}
166 		return pImplementation;
167 	}
168 
169 	//---------------------------------------------------------------------
setAccessibleParent(const Reference<XAccessibleContext> & _rxComponent,const Reference<XAccessible> & _rxNewParent)170 	sal_Bool OAccessibleImplementationAccess::setAccessibleParent(
171 			const Reference< XAccessibleContext >& _rxComponent, const Reference< XAccessible >& _rxNewParent )
172 	{
173 		OAccessibleImplementationAccess* pImplementation = getImplementation( _rxComponent );
174 
175 		if ( pImplementation )
176 			pImplementation->setAccessibleParent( _rxNewParent );
177 
178 		return ( NULL != pImplementation );
179 	}
180 
181 //.........................................................................
182 }	// namespace comphelper
183 //.........................................................................
184 
185 
186