xref: /aoo41x/main/vcl/unx/gtk/a11y/atkcomponent.cxx (revision 9f62ea84)
1*9f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9f62ea84SAndrew Rist  * distributed with this work for additional information
6*9f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
9*9f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9f62ea84SAndrew Rist  *
11*9f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9f62ea84SAndrew Rist  *
13*9f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9f62ea84SAndrew Rist  * software distributed under the License is distributed on an
15*9f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9f62ea84SAndrew Rist  * specific language governing permissions and limitations
18*9f62ea84SAndrew Rist  * under the License.
19*9f62ea84SAndrew Rist  *
20*9f62ea84SAndrew Rist  *************************************************************/
21*9f62ea84SAndrew Rist 
22*9f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "atkwrapper.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/accessibility/XAccessibleComponent.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #ifdef ENABLE_TRACING
32cdf0e10cSrcweir #include <stdio.h>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir static accessibility::XAccessibleComponent*
getComponent(AtkComponent * pComponent)38cdf0e10cSrcweir     getComponent( AtkComponent *pComponent ) throw (uno::RuntimeException)
39cdf0e10cSrcweir {
40cdf0e10cSrcweir     AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pComponent );
41cdf0e10cSrcweir     if( pWrap )
42cdf0e10cSrcweir     {
43cdf0e10cSrcweir         if( !pWrap->mpComponent && pWrap->mpContext )
44cdf0e10cSrcweir         {
45cdf0e10cSrcweir             uno::Any any = pWrap->mpContext->queryInterface( accessibility::XAccessibleComponent::static_type(NULL) );
46cdf0e10cSrcweir             pWrap->mpComponent = reinterpret_cast< accessibility::XAccessibleComponent * > (any.pReserved);
47cdf0e10cSrcweir             pWrap->mpComponent->acquire();
48cdf0e10cSrcweir         }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         return pWrap->mpComponent;
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     return NULL;
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /*****************************************************************************/
57cdf0e10cSrcweir 
58cdf0e10cSrcweir static awt::Point
translatePoint(accessibility::XAccessibleComponent * pComponent,gint x,gint y,AtkCoordType t)59cdf0e10cSrcweir translatePoint( accessibility::XAccessibleComponent *pComponent,
60cdf0e10cSrcweir                 gint x, gint y, AtkCoordType t)
61cdf0e10cSrcweir {
62cdf0e10cSrcweir     awt::Point aOrigin( 0, 0 );
63cdf0e10cSrcweir     if( t == ATK_XY_SCREEN )
64cdf0e10cSrcweir         aOrigin = pComponent->getLocationOnScreen();
65cdf0e10cSrcweir 
66cdf0e10cSrcweir #ifdef ENABLE_TRACING
67cdf0e10cSrcweir     fprintf(stderr, "coordinates ( %u, %u ) translated to: ( %u, %u )\n",
68cdf0e10cSrcweir         x, y, x - aOrigin.X, y - aOrigin.Y);
69cdf0e10cSrcweir #endif
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     return awt::Point( x - aOrigin.X, y - aOrigin.Y );
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
74cdf0e10cSrcweir /*****************************************************************************/
75cdf0e10cSrcweir 
76cdf0e10cSrcweir extern "C" {
77cdf0e10cSrcweir 
78cdf0e10cSrcweir static gboolean
component_wrapper_grab_focus(AtkComponent * component)79cdf0e10cSrcweir component_wrapper_grab_focus (AtkComponent *component)
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     try
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         accessibility::XAccessibleComponent* pComponent = getComponent( component );
84cdf0e10cSrcweir         if( pComponent )
85cdf0e10cSrcweir         {
86cdf0e10cSrcweir             pComponent->grabFocus();
87cdf0e10cSrcweir             return TRUE;
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir     catch( const uno::Exception &e )
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         g_warning( "Exception in grabFocus()" );
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     return FALSE;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir /*****************************************************************************/
99cdf0e10cSrcweir 
100cdf0e10cSrcweir static gboolean
component_wrapper_contains(AtkComponent * component,gint x,gint y,AtkCoordType coord_type)101cdf0e10cSrcweir component_wrapper_contains (AtkComponent *component,
102cdf0e10cSrcweir                             gint          x,
103cdf0e10cSrcweir                             gint          y,
104cdf0e10cSrcweir                             AtkCoordType  coord_type)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     try
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         accessibility::XAccessibleComponent* pComponent = getComponent( component );
109cdf0e10cSrcweir         if( pComponent )
110cdf0e10cSrcweir             return pComponent->containsPoint( translatePoint( pComponent, x, y, coord_type ) );
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir     catch( const uno::Exception &e )
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir         g_warning( "Exception in containsPoint()" );
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     return FALSE;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir /*****************************************************************************/
121cdf0e10cSrcweir 
122cdf0e10cSrcweir static AtkObject *
component_wrapper_ref_accessible_at_point(AtkComponent * component,gint x,gint y,AtkCoordType coord_type)123cdf0e10cSrcweir component_wrapper_ref_accessible_at_point (AtkComponent *component,
124cdf0e10cSrcweir                                            gint          x,
125cdf0e10cSrcweir                                            gint          y,
126cdf0e10cSrcweir                                            AtkCoordType  coord_type)
127cdf0e10cSrcweir {
128cdf0e10cSrcweir     try
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         accessibility::XAccessibleComponent* pComponent = getComponent( component );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         if( pComponent )
133cdf0e10cSrcweir         {
134cdf0e10cSrcweir             uno::Reference< accessibility::XAccessible > xAccessible;
135cdf0e10cSrcweir             xAccessible = pComponent->getAccessibleAtPoint(
136cdf0e10cSrcweir                 translatePoint( pComponent, x, y, coord_type ) );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir #ifdef ENABLE_TRACING
139cdf0e10cSrcweir             fprintf(stderr, "getAccessibleAtPoint( %u, %u ) returned %p\n",
140cdf0e10cSrcweir               x, y, xAccessible.get());
141cdf0e10cSrcweir 
142cdf0e10cSrcweir             uno::Reference< accessibility::XAccessibleComponent > xComponent(
143cdf0e10cSrcweir                 xAccessible->getAccessibleContext(), uno::UNO_QUERY );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             if( xComponent.is() )
146cdf0e10cSrcweir             {
147cdf0e10cSrcweir                 awt::Rectangle rect = xComponent->getBounds();
148cdf0e10cSrcweir                 fprintf(stderr, "%p->getBounds() returned: ( %u, %u, %u, %u )\n",
149cdf0e10cSrcweir                     xAccessible.get(), rect.X, rect.Y, rect.Width, rect.Height );
150cdf0e10cSrcweir             }
151cdf0e10cSrcweir #endif
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             return atk_object_wrapper_ref( xAccessible );
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir     catch( const uno::Exception &e )
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         g_warning( "Exception in getAccessibleAtPoint()" );
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     return NULL;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir /*****************************************************************************/
165cdf0e10cSrcweir 
166cdf0e10cSrcweir static void
component_wrapper_get_position(AtkComponent * component,gint * x,gint * y,AtkCoordType coord_type)167cdf0e10cSrcweir component_wrapper_get_position (AtkComponent   *component,
168cdf0e10cSrcweir                                 gint           *x,
169cdf0e10cSrcweir                                 gint           *y,
170cdf0e10cSrcweir                                 AtkCoordType   coord_type)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     try
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         accessibility::XAccessibleComponent* pComponent = getComponent( component );
175cdf0e10cSrcweir         if( pComponent )
176cdf0e10cSrcweir         {
177cdf0e10cSrcweir             awt::Point aPos;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             if( coord_type == ATK_XY_SCREEN )
180cdf0e10cSrcweir                 aPos = pComponent->getLocationOnScreen();
181cdf0e10cSrcweir             else
182cdf0e10cSrcweir                 aPos = pComponent->getLocation();
183cdf0e10cSrcweir 
184cdf0e10cSrcweir             *x = aPos.X;
185cdf0e10cSrcweir             *y = aPos.Y;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir #ifdef ENABLE_TRACING
188cdf0e10cSrcweir             fprintf(stderr, "getLocation[OnScreen]() returned: ( %u, %u )\n", *x, *y );
189cdf0e10cSrcweir #endif
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir     catch( const uno::Exception &e )
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         g_warning( "Exception in getLocation[OnScreen]()" );
195cdf0e10cSrcweir     }
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir /*****************************************************************************/
199cdf0e10cSrcweir 
200cdf0e10cSrcweir static void
component_wrapper_get_size(AtkComponent * component,gint * width,gint * height)201cdf0e10cSrcweir component_wrapper_get_size (AtkComponent   *component,
202cdf0e10cSrcweir                             gint           *width,
203cdf0e10cSrcweir                             gint           *height)
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     try
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         accessibility::XAccessibleComponent* pComponent = getComponent( component );
208cdf0e10cSrcweir         if( pComponent )
209cdf0e10cSrcweir         {
210cdf0e10cSrcweir             awt::Size aSize = pComponent->getSize();
211cdf0e10cSrcweir             *width = aSize.Width;
212cdf0e10cSrcweir             *height = aSize.Height;
213cdf0e10cSrcweir 
214cdf0e10cSrcweir #ifdef ENABLE_TRACING
215cdf0e10cSrcweir             fprintf(stderr, "getSize() returned: ( %u, %u )\n", *width, *height );
216cdf0e10cSrcweir #endif
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir     catch( const uno::Exception &e )
220cdf0e10cSrcweir     {
221cdf0e10cSrcweir         g_warning( "Exception in getSize()" );
222cdf0e10cSrcweir     }
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir /*****************************************************************************/
226cdf0e10cSrcweir 
227cdf0e10cSrcweir static void
component_wrapper_get_extents(AtkComponent * component,gint * x,gint * y,gint * width,gint * height,AtkCoordType coord_type)228cdf0e10cSrcweir component_wrapper_get_extents (AtkComponent *component,
229cdf0e10cSrcweir                                gint         *x,
230cdf0e10cSrcweir                                gint         *y,
231cdf0e10cSrcweir                                gint         *width,
232cdf0e10cSrcweir                                gint         *height,
233cdf0e10cSrcweir                                AtkCoordType  coord_type)
234cdf0e10cSrcweir {
235cdf0e10cSrcweir     component_wrapper_get_position( component, x, y, coord_type );
236cdf0e10cSrcweir     component_wrapper_get_size( component, width, height );
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir /*****************************************************************************/
240cdf0e10cSrcweir 
241cdf0e10cSrcweir static gboolean
component_wrapper_set_extents(AtkComponent *,gint,gint,gint,gint,AtkCoordType)242cdf0e10cSrcweir component_wrapper_set_extents (AtkComponent *, gint, gint, gint, gint, AtkCoordType)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     g_warning( "AtkComponent::set_extents unimplementable" );
245cdf0e10cSrcweir     return FALSE;
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir /*****************************************************************************/
249cdf0e10cSrcweir 
250cdf0e10cSrcweir static gboolean
component_wrapper_set_position(AtkComponent *,gint,gint,AtkCoordType)251cdf0e10cSrcweir component_wrapper_set_position (AtkComponent *, gint, gint, AtkCoordType)
252cdf0e10cSrcweir {
253cdf0e10cSrcweir     g_warning( "AtkComponent::set_position unimplementable" );
254cdf0e10cSrcweir     return FALSE;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir 
257cdf0e10cSrcweir /*****************************************************************************/
258cdf0e10cSrcweir 
259cdf0e10cSrcweir static gboolean
component_wrapper_set_size(AtkComponent *,gint,gint)260cdf0e10cSrcweir component_wrapper_set_size (AtkComponent *, gint, gint)
261cdf0e10cSrcweir {
262cdf0e10cSrcweir     g_warning( "AtkComponent::set_size unimplementable" );
263cdf0e10cSrcweir     return FALSE;
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir /*****************************************************************************/
267cdf0e10cSrcweir 
268cdf0e10cSrcweir static AtkLayer
component_wrapper_get_layer(AtkComponent * component)269cdf0e10cSrcweir component_wrapper_get_layer (AtkComponent   *component)
270cdf0e10cSrcweir {
271cdf0e10cSrcweir     AtkRole role = atk_object_get_role( ATK_OBJECT( component ) );
272cdf0e10cSrcweir     AtkLayer layer = ATK_LAYER_WIDGET;
273cdf0e10cSrcweir 
274cdf0e10cSrcweir     switch (role)
275cdf0e10cSrcweir     {
276cdf0e10cSrcweir         case ATK_ROLE_POPUP_MENU:
277cdf0e10cSrcweir         case ATK_ROLE_MENU_ITEM:
278cdf0e10cSrcweir         case ATK_ROLE_CHECK_MENU_ITEM:
279cdf0e10cSrcweir         case ATK_ROLE_SEPARATOR:
280cdf0e10cSrcweir         case ATK_ROLE_LIST_ITEM:
281cdf0e10cSrcweir             layer = ATK_LAYER_POPUP;
282cdf0e10cSrcweir             break;
283cdf0e10cSrcweir         case ATK_ROLE_MENU:
284cdf0e10cSrcweir             {
285cdf0e10cSrcweir                 AtkObject * parent = atk_object_get_parent( ATK_OBJECT( component ) );
286cdf0e10cSrcweir                 if( atk_object_get_role( parent ) != ATK_ROLE_MENU_BAR )
287cdf0e10cSrcweir                     layer = ATK_LAYER_POPUP;
288cdf0e10cSrcweir             }
289cdf0e10cSrcweir             break;
290cdf0e10cSrcweir 
291cdf0e10cSrcweir         case ATK_ROLE_LIST:
292cdf0e10cSrcweir             {
293cdf0e10cSrcweir                 AtkObject * parent = atk_object_get_parent( ATK_OBJECT( component ) );
294cdf0e10cSrcweir                 if( atk_object_get_role( parent ) == ATK_ROLE_COMBO_BOX )
295cdf0e10cSrcweir                     layer = ATK_LAYER_POPUP;
296cdf0e10cSrcweir             }
297cdf0e10cSrcweir             break;
298cdf0e10cSrcweir 
299cdf0e10cSrcweir         default:
300cdf0e10cSrcweir             ;
301cdf0e10cSrcweir     }
302cdf0e10cSrcweir 
303cdf0e10cSrcweir     return layer;
304cdf0e10cSrcweir }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir /*****************************************************************************/
307cdf0e10cSrcweir 
308cdf0e10cSrcweir static gint
component_wrapper_get_mdi_zorder(AtkComponent *)309cdf0e10cSrcweir component_wrapper_get_mdi_zorder (AtkComponent   *)
310cdf0e10cSrcweir {
311cdf0e10cSrcweir     // only needed for ATK_LAYER_MDI (not used) or ATK_LAYER_WINDOW (inherited from GAIL)
312cdf0e10cSrcweir     return G_MININT;
313cdf0e10cSrcweir }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir /*****************************************************************************/
316cdf0e10cSrcweir 
317cdf0e10cSrcweir // This code is mostly stolen from libgail ..
318cdf0e10cSrcweir 
319cdf0e10cSrcweir static guint
component_wrapper_add_focus_handler(AtkComponent * component,AtkFocusHandler handler)320cdf0e10cSrcweir component_wrapper_add_focus_handler (AtkComponent    *component,
321cdf0e10cSrcweir                                      AtkFocusHandler  handler)
322cdf0e10cSrcweir {
323cdf0e10cSrcweir     GSignalMatchType match_type;
324cdf0e10cSrcweir     gulong ret;
325cdf0e10cSrcweir     guint signal_id;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir     match_type = (GSignalMatchType) (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC);
328cdf0e10cSrcweir     signal_id = g_signal_lookup( "focus-event", ATK_TYPE_OBJECT );
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     ret = g_signal_handler_find( component, match_type, signal_id, 0, NULL,
331cdf0e10cSrcweir                                  (gpointer) &handler, NULL);
332cdf0e10cSrcweir     if (!ret)
333cdf0e10cSrcweir     {
334cdf0e10cSrcweir         return g_signal_connect_closure_by_id (component,
335cdf0e10cSrcweir                                                signal_id, 0,
336cdf0e10cSrcweir                                                g_cclosure_new (
337cdf0e10cSrcweir                                                G_CALLBACK (handler), NULL,
338cdf0e10cSrcweir                                                (GClosureNotify) NULL),
339cdf0e10cSrcweir                                                FALSE);
340cdf0e10cSrcweir     }
341cdf0e10cSrcweir     else
342cdf0e10cSrcweir     {
343cdf0e10cSrcweir         return 0;
344cdf0e10cSrcweir     }
345cdf0e10cSrcweir }
346cdf0e10cSrcweir 
347cdf0e10cSrcweir /*****************************************************************************/
348cdf0e10cSrcweir 
349cdf0e10cSrcweir static void
component_wrapper_remove_focus_handler(AtkComponent * component,guint handler_id)350cdf0e10cSrcweir component_wrapper_remove_focus_handler (AtkComponent  *component,
351cdf0e10cSrcweir                                         guint         handler_id)
352cdf0e10cSrcweir {
353cdf0e10cSrcweir     g_signal_handler_disconnect (component, handler_id);
354cdf0e10cSrcweir }
355cdf0e10cSrcweir 
356cdf0e10cSrcweir /*****************************************************************************/
357cdf0e10cSrcweir 
358cdf0e10cSrcweir } // extern "C"
359cdf0e10cSrcweir 
360cdf0e10cSrcweir void
componentIfaceInit(AtkComponentIface * iface)361cdf0e10cSrcweir componentIfaceInit (AtkComponentIface *iface)
362cdf0e10cSrcweir {
363cdf0e10cSrcweir   g_return_if_fail (iface != NULL);
364cdf0e10cSrcweir 
365cdf0e10cSrcweir   iface->add_focus_handler = component_wrapper_add_focus_handler;
366cdf0e10cSrcweir   iface->contains = component_wrapper_contains;
367cdf0e10cSrcweir   iface->get_extents = component_wrapper_get_extents;
368cdf0e10cSrcweir   iface->get_layer = component_wrapper_get_layer;
369cdf0e10cSrcweir   iface->get_mdi_zorder = component_wrapper_get_mdi_zorder;
370cdf0e10cSrcweir   iface->get_position = component_wrapper_get_position;
371cdf0e10cSrcweir   iface->get_size = component_wrapper_get_size;
372cdf0e10cSrcweir   iface->grab_focus = component_wrapper_grab_focus;
373cdf0e10cSrcweir   iface->ref_accessible_at_point = component_wrapper_ref_accessible_at_point;
374cdf0e10cSrcweir   iface->remove_focus_handler = component_wrapper_remove_focus_handler;
375cdf0e10cSrcweir   iface->set_extents = component_wrapper_set_extents;
376cdf0e10cSrcweir   iface->set_position = component_wrapper_set_position;
377cdf0e10cSrcweir   iface->set_size = component_wrapper_set_size;
378cdf0e10cSrcweir }
379