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 #include <cppuhelper/implbase3.hxx>
23 #include <cppuhelper/factory.hxx>
24 #include <cppuhelper/implementationentry.hxx>
25 
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/lang/XInitialization.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <com/sun/star/accessibility/XMSAAService.hpp>
30 
31 #include <com/sun/star/awt/XExtendedToolkit.hpp>
32 #include <vcl/svapp.hxx>
33 
34 using namespace ::rtl; // for OUString
35 using namespace ::com::sun::star; // for odk interfaces
36 using namespace ::com::sun::star::uno; // for basic types
37 using namespace ::com::sun::star::accessibility;
38 
39 using namespace ::com::sun::star::awt;
40 
41 typedef sal_Int32 HWND;
42 
43 #include "AccTopWindowListener.hxx"
44 #include "g_msacc.hxx"
45 
46 extern void FreeTopWindowListener();
47 extern long GetMSComPtr(long hWnd, long lParam, long wParam);
48 extern void handleWindowOpened_impl( long pAcc);
49 
50 
51 namespace my_sc_impl
52 {
53 
54 extern Sequence< OUString > SAL_CALL  getSupportedServiceNames_MSAAServiceImpl();
55 extern OUString SAL_CALL getImplementationName_MSAAServiceImpl();
56 extern Reference< XInterface > SAL_CALL create_MSAAServiceImpl(
57         Reference< XComponentContext > const & xContext )
58     SAL_THROW( () );
59 /**
60    * Method that returns the service name.
61    * @param
62    * @return Name sequence.
63    */
getSupportedServiceNames_MSAAServiceImpl()64 static Sequence< OUString > getSupportedServiceNames_MSAAServiceImpl()
65 {
66     static Sequence < OUString > *pNames = 0;
67     if( ! pNames )
68     {
69         //		MutexGuard guard( Mutex::getGlobalMutex() );
70         if( !pNames )
71         {
72             static Sequence< OUString > seqNames(1);
73             seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.accessibility.MSAAService"));
74             pNames = &seqNames;
75         }
76     }
77     return *pNames;
78 }
79 
80 /**
81    * Method that returns the service name.
82    * @param
83    * @return Name sequence.
84    */
getImplementationName_MSAAServiceImpl()85 static OUString getImplementationName_MSAAServiceImpl()
86 {
87     static OUString *pImplName = 0;
88     if( ! pImplName )
89     {
90         //		MutexGuard guard( Mutex::getGlobalMutex() );
91         if( ! pImplName )
92         {
93             static OUString implName( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.accessibility.my_sc_implementation.MSAAService") );
94             pImplName = &implName;
95         }
96     }
97     return *pImplName;
98 }
99 
100 class MSAAServiceImpl : public ::cppu::WeakImplHelper3<
101             XMSAAService, lang::XServiceInfo, lang::XInitialization >
102 {
103     OUString m_arg;
104 public:
105     // focus on three given interfaces,
106     // no need to implement XInterface, XTypeProvider, XWeak
107     MSAAServiceImpl ();
108     virtual ~MSAAServiceImpl( void );
109     // XInitialization will be called upon createInstanceWithArguments[AndContext]()
110     virtual void SAL_CALL initialize( Sequence< Any > const & args )
111     throw (Exception);
112     // XMSAAService
113     virtual sal_Int32 SAL_CALL getAccObjectPtr (long hWnd, long lParam, long wParam)
114     throw (RuntimeException);
115     virtual void SAL_CALL handleWindowOpened(sal_Int32)
116     throw (RuntimeException);
117     // XServiceInfo
118     virtual OUString SAL_CALL getImplementationName()
119     throw (RuntimeException);
120     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
121     throw (RuntimeException);
122     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
123     throw (RuntimeException);
124 };
125 
126 /**
127    * Implemention of XInitialization.
128    * @param
129    * @return.
130    */
initialize(Sequence<Any> const & args)131 void MSAAServiceImpl::initialize( Sequence< Any > const & args ) throw (Exception)
132 {
133     if (1 != args.getLength())
134     {
135         throw lang::IllegalArgumentException(
136             OUString( RTL_CONSTASCII_USTRINGPARAM("give a string instanciating this component!") ),
137             (::cppu::OWeakObject *)this, // resolve to XInterface reference
138             0 ); // argument pos
139     }
140     if (! (args[ 0 ] >>= m_arg))
141     {
142         throw lang::IllegalArgumentException(
143             OUString( RTL_CONSTASCII_USTRINGPARAM("no string given as argument!") ),
144             (::cppu::OWeakObject *)this, // resolve to XInterface reference
145             0 ); // argument pos
146     }
147 }
148 
149 /**
150    * Implemention of getAccObjectPtr.
151    * @param
152    * @return Com interface.
153    */
getAccObjectPtr(long hWnd,long lParam,long wParam)154 sal_Int32 MSAAServiceImpl::getAccObjectPtr ( long hWnd, long lParam, long wParam) throw (RuntimeException)
155 {
156     return GetMSComPtr(hWnd, lParam, wParam);
157 }
158 
159 /**
160    * Implemention of handleWindowOpened,the method will be invoked when a top window
161    * opened and AT starts up.
162    * @param
163    * @return
164    */
handleWindowOpened(sal_Int32 pAcc)165 void MSAAServiceImpl::handleWindowOpened( sal_Int32 pAcc)
166 {
167     handleWindowOpened_impl(pAcc);
168 }
169 
170 /**
171    * Implemention of XServiceInfo.
172    * @param
173    * @return Implementataion name.
174    */
getImplementationName()175 OUString MSAAServiceImpl::getImplementationName() throw (RuntimeException)
176 {
177     // unique implementation name
178     return OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.accessibility.my_sc_impl.MSAAService") );
179 }
180 
181 /**
182    * Implemention of XServiceInfo,return support service name.
183    * @param Service name.
184    * @return If the service name is supported.
185    */
supportsService(OUString const & serviceName)186 sal_Bool MSAAServiceImpl::supportsService( OUString const & serviceName ) throw (RuntimeException)
187 {
188     // this object only supports one service, so the test is simple
189     return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("com.sun.star.accessibility.MSAAService") );
190 }
191 
192 /**
193    * Implemention of XServiceInfo,return all service names.
194    * @param.
195    * @return service name sequence.
196    */
getSupportedServiceNames()197 Sequence< OUString > MSAAServiceImpl::getSupportedServiceNames() throw (RuntimeException)
198 {
199     return getSupportedServiceNames_MSAAServiceImpl();
200 }
201 
202 /**
203    * Static method that can create an entity of our MSAA Service
204    * @param xContext No use here.
205    * @return The object interface.
206    */
create_MSAAServiceImpl(Reference<XComponentContext> const &)207 Reference< XInterface > SAL_CALL create_MSAAServiceImpl( Reference< XComponentContext > const & /*xContext*/ ) SAL_THROW( () )
208 {
209     MSAAServiceImpl* xxx = new MSAAServiceImpl();
210     //return static_cast< lang::XTypeProvider * >(  xxx );
211     Reference< XMSAAService > p( xxx );
212     return p;
213 }
214 
215 /**
216    * Constructor.
217    * @param
218    * @return
219    */
MSAAServiceImpl()220 MSAAServiceImpl::MSAAServiceImpl()
221 {
222     Reference< XExtendedToolkit > xToolkit =
223         Reference< XExtendedToolkit >(Application::GetVCLToolkit(), UNO_QUERY);
224 
225     if(xToolkit.is())
226     {
227         AccTopWindowListener *accListener;
228         accListener = new AccTopWindowListener();
229         g_pTop = accListener;
230         Reference< XTopWindowListener> x(accListener);
231         xToolkit->addTopWindowListener(x);
232     }
233 }
234 
235 /**
236    * Static method that can create an entity of our MSAA Service
237    * @param Destructor
238    * @return
239    */
~MSAAServiceImpl()240 MSAAServiceImpl::~MSAAServiceImpl()
241 {
242 
243     // As all folders and streams contain references to their parents,
244     // we must remove these references so that they will be deleted when
245     // the hash_map of the root folder is cleared, releasing all subfolders
246     // and substreams which in turn release theirs, etc. When xRootFolder is
247     // released when this destructor completes, the folder tree should be
248     // deleted fully (and automagically).
249     FreeTopWindowListener();
250 
251 
252 }
253 
254 }
255 
256 /* shared lib exports implemented without helpers in service_impl1.cxx */
257 namespace my_sc_impl
258 {
259 static struct ::cppu::ImplementationEntry s_component_entries [] =
260     {
261         {
262             create_MSAAServiceImpl, getImplementationName_MSAAServiceImpl,
263             getSupportedServiceNames_MSAAServiceImpl, ::cppu::createSingleComponentFactory,
264             0, 0
265         },
266         {
267             create_MSAAServiceImpl, getImplementationName_MSAAServiceImpl,
268             getSupportedServiceNames_MSAAServiceImpl, ::cppu::createSingleComponentFactory,
269             0, 0
270         },
271         { 0, 0, 0, 0, 0, 0 }
272     };
273 }
274 
275 extern "C"
276 {
component_getImplementationEnvironment(sal_Char const ** ppEnvTypeName,uno_Environment **)277     void SAL_CALL component_getImplementationEnvironment(
278         sal_Char const ** ppEnvTypeName, uno_Environment ** /*ppEnv*/ )
279     {
280         *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
281     }
component_getFactory(sal_Char const * implName,lang::XMultiServiceFactory * xMgr,registry::XRegistryKey * xRegistry)282     void * SAL_CALL component_getFactory(
283         sal_Char const * implName, lang::XMultiServiceFactory * xMgr,
284         registry::XRegistryKey * xRegistry )
285     {
286         return ::cppu::component_getFactoryHelper(
287                    implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries );
288     }
289 }
290