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