| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | /* |
| 3 | * This file is part of the LibreOffice project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * This file incorporates work covered by the following license notice: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 | * contributor license agreements. See the NOTICE file distributed |
| 13 | * with this work for additional information regarding copyright |
| 14 | * ownership. The ASF licenses this file to you under the Apache |
| 15 | * License, Version 2.0 (the "License"); you may not use this file |
| 16 | * except in compliance with the License. You may obtain a copy of |
| 17 | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 | */ |
| 19 | |
| 20 | #include "atkwrapper.hxx" |
| 21 | #include <com/sun/star/accessibility/XAccessibleComponent.hpp> |
| 22 | #include <sal/log.hxx> |
| 23 | #include <gtk/gtk.h> |
| 24 | |
| 25 | using namespace ::com::sun::star; |
| 26 | |
| 27 | static AtkObjectWrapper* getObjectWrapper(AtkComponent *pComponent) |
| 28 | { |
| 29 | AtkObjectWrapper *pWrap = nullptr; |
| 30 | if (ATK_IS_OBJECT_WRAPPER(pComponent)) |
| 31 | pWrap = ATK_OBJECT_WRAPPER(pComponent); |
| 32 | else if (GTK_IS_DRAWING_AREA(pComponent)) //when using a GtkDrawingArea as a custom widget in welded gtk3 |
| 33 | { |
| 34 | GtkWidget* pDrawingArea = GTK_WIDGET(pComponent); |
| 35 | AtkObject* pAtkObject = gtk_widget_get_accessible(pDrawingArea); |
| 36 | pWrap = ATK_IS_OBJECT_WRAPPER(pAtkObject) ? ATK_OBJECT_WRAPPER(pAtkObject) : nullptr; |
| 37 | } |
| 38 | return pWrap; |
| 39 | } |
| 40 | |
| 41 | /// @throws uno::RuntimeException |
| 42 | static css::uno::Reference<css::accessibility::XAccessibleComponent> |
| 43 | getComponent(AtkObjectWrapper *pWrap) |
| 44 | { |
| 45 | if (pWrap) |
| 46 | { |
| 47 | if (!pWrap->mpComponent.is()) |
| 48 | pWrap->mpComponent.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 49 | return pWrap->mpComponent; |
| 50 | } |
| 51 | |
| 52 | return css::uno::Reference<css::accessibility::XAccessibleComponent>(); |
| 53 | } |
| 54 | |
| 55 | static awt::Point |
| 56 | lcl_getLocationInWindow(AtkComponent* pAtkComponent, |
| 57 | css::uno::Reference<accessibility::XAccessibleComponent> const& xComponent) |
| 58 | { |
| 59 | // calculate position in window by adding the component's position in the parent |
| 60 | // to the parent's position in the window (unless parent is a window itself) |
| 61 | awt::Point aPos = xComponent->getLocation(); |
| 62 | AtkObject* pParent = atk_object_get_parent(ATK_OBJECT(pAtkComponent)); |
| 63 | if (ATK_IS_COMPONENT(pParent) && pParent->role != AtkRole::ATK_ROLE_DIALOG |
| 64 | && pParent->role != AtkRole::ATK_ROLE_FILE_CHOOSER |
| 65 | && pParent->role != AtkRole::ATK_ROLE_FRAME |
| 66 | && pParent->role != AtkRole::ATK_ROLE_WINDOW) |
| 67 | { |
| 68 | int nX; |
| 69 | int nY; |
| 70 | atk_component_get_extents(ATK_COMPONENT(pParent), &nX, &nY, nullptr, nullptr, ATK_XY_WINDOW); |
| 71 | aPos.X += nX; |
| 72 | aPos.Y += nY; |
| 73 | } |
| 74 | |
| 75 | return aPos; |
| 76 | } |
| 77 | |
| 78 | /*****************************************************************************/ |
| 79 | |
| 80 | static awt::Point |
| 81 | translatePoint( AtkComponent* pAtkComponent, |
| 82 | css::uno::Reference<accessibility::XAccessibleComponent> const & pComponent, |
| 83 | gint x, gint y, AtkCoordType t) |
| 84 | { |
| 85 | awt::Point aOrigin( 0, 0 ); |
| 86 | if( t == ATK_XY_SCREEN ) |
| 87 | aOrigin = pComponent->getLocationOnScreen(); |
| 88 | else if (t == ATK_XY_WINDOW) |
| 89 | aOrigin = lcl_getLocationInWindow(pAtkComponent, pComponent); |
| 90 | return awt::Point( x - aOrigin.X, y - aOrigin.Y ); |
| 91 | } |
| 92 | |
| 93 | /*****************************************************************************/ |
| 94 | |
| 95 | extern "C" { |
| 96 | |
| 97 | static gboolean |
| 98 | component_wrapper_grab_focus (AtkComponent *component) |
| 99 | { |
| 100 | AtkObjectWrapper* obj = getObjectWrapper(component); |
| 101 | //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y |
| 102 | if (obj && obj->mpOrig) |
| 103 | return atk_component_grab_focus(ATK_COMPONENT(obj->mpOrig)); |
| 104 | |
| 105 | try |
| 106 | { |
| 107 | css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent |
| 108 | = getComponent(obj); |
| 109 | if( pComponent.is() ) |
| 110 | { |
| 111 | pComponent->grabFocus(); |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | catch( const uno::Exception & ) |
| 116 | { |
| 117 | g_warning( "Exception in grabFocus()" ); |
| 118 | } |
| 119 | |
| 120 | return FALSE; |
| 121 | } |
| 122 | |
| 123 | /*****************************************************************************/ |
| 124 | |
| 125 | static gboolean |
| 126 | component_wrapper_contains (AtkComponent *component, |
| 127 | gint x, |
| 128 | gint y, |
| 129 | AtkCoordType coord_type) |
| 130 | { |
| 131 | AtkObjectWrapper* obj = getObjectWrapper(component); |
| 132 | //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y |
| 133 | if (obj && obj->mpOrig) |
| 134 | return atk_component_contains(ATK_COMPONENT(obj->mpOrig), x, y, coord_type); |
| 135 | |
| 136 | try |
| 137 | { |
| 138 | css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent |
| 139 | = getComponent(obj); |
| 140 | if( pComponent.is() ) |
| 141 | return pComponent->containsPoint( |
| 142 | translatePoint(component, pComponent, x, y, coord_type)); |
| 143 | } |
| 144 | catch( const uno::Exception & ) |
| 145 | { |
| 146 | g_warning( "Exception in containsPoint()" ); |
| 147 | } |
| 148 | |
| 149 | return FALSE; |
| 150 | } |
| 151 | |
| 152 | /*****************************************************************************/ |
| 153 | |
| 154 | static AtkObject * |
| 155 | component_wrapper_ref_accessible_at_point (AtkComponent *component, |
| 156 | gint x, |
| 157 | gint y, |
| 158 | AtkCoordType coord_type) |
| 159 | { |
| 160 | AtkObjectWrapper* obj = getObjectWrapper(component); |
| 161 | //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y |
| 162 | if (obj && obj->mpOrig) |
| 163 | return atk_component_ref_accessible_at_point(ATK_COMPONENT(obj->mpOrig), x, y, coord_type); |
| 164 | |
| 165 | try |
| 166 | { |
| 167 | css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent |
| 168 | = getComponent(obj); |
| 169 | |
| 170 | if( pComponent.is() ) |
| 171 | { |
| 172 | uno::Reference< accessibility::XAccessible > xAccessible = pComponent->getAccessibleAtPoint( |
| 173 | translatePoint(component, pComponent, x, y, coord_type)); |
| 174 | if (xAccessible.is()) |
| 175 | return atk_object_wrapper_ref(xAccessible); |
| 176 | } |
| 177 | } |
| 178 | catch( const uno::Exception & ) |
| 179 | { |
| 180 | g_warning( "Exception in getAccessibleAtPoint()" ); |
| 181 | } |
| 182 | |
| 183 | return nullptr; |
| 184 | } |
| 185 | |
| 186 | /*****************************************************************************/ |
| 187 | |
| 188 | static void |
| 189 | component_wrapper_get_position (AtkComponent *component, |
| 190 | gint *x, |
| 191 | gint *y, |
| 192 | AtkCoordType coord_type) |
| 193 | { |
| 194 | AtkObjectWrapper* obj = getObjectWrapper(component); |
| 195 | //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y |
| 196 | if (obj && obj->mpOrig) |
| 197 | { |
| 198 | atk_component_get_extents(ATK_COMPONENT(obj->mpOrig), x, y, nullptr, nullptr, coord_type); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | *x = *y = -1; |
| 203 | |
| 204 | try |
| 205 | { |
| 206 | css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent |
| 207 | = getComponent(obj); |
| 208 | if( pComponent.is() ) |
| 209 | { |
| 210 | awt::Point aPos; |
| 211 | |
| 212 | if( coord_type == ATK_XY_SCREEN ) |
| 213 | aPos = pComponent->getLocationOnScreen(); |
| 214 | else if (coord_type == ATK_XY_WINDOW) |
| 215 | aPos = lcl_getLocationInWindow(component, pComponent); |
| 216 | else if (coord_type == ATK_XY_PARENT) |
| 217 | aPos = pComponent->getLocation(); |
| 218 | else |
| 219 | { |
| 220 | SAL_WARN("vcl.gtk", |
| 221 | "component_wrapper_get_position called with unknown AtkCoordType " |
| 222 | << coord_type); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | *x = aPos.X; |
| 227 | *y = aPos.Y; |
| 228 | } |
| 229 | } |
| 230 | catch( const uno::Exception & ) |
| 231 | { |
| 232 | g_warning( "Exception in getLocation[OnScreen]()" ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /*****************************************************************************/ |
| 237 | |
| 238 | static void |
| 239 | component_wrapper_get_size (AtkComponent *component, |
| 240 | gint *width, |
| 241 | gint *height) |
| 242 | { |
| 243 | AtkObjectWrapper* obj = getObjectWrapper(component); |
| 244 | //if we're a native GtkDrawingArea with custom a11y, use the default toolkit a11y |
| 245 | if (obj && obj->mpOrig) |
| 246 | { |
| 247 | atk_component_get_extents(ATK_COMPONENT(obj->mpOrig), nullptr, nullptr, width, height, ATK_XY_WINDOW); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | *width = *height = -1; |
| 252 | |
| 253 | try |
| 254 | { |
| 255 | css::uno::Reference<css::accessibility::XAccessibleComponent> pComponent |
| 256 | = getComponent(obj); |
| 257 | if( pComponent.is() ) |
| 258 | { |
| 259 | awt::Size aSize = pComponent->getSize(); |
| 260 | *width = aSize.Width; |
| 261 | *height = aSize.Height; |
| 262 | } |
| 263 | } |
| 264 | catch( const uno::Exception & ) |
| 265 | { |
| 266 | g_warning( "Exception in getSize()" ); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /*****************************************************************************/ |
| 271 | |
| 272 | static void |
| 273 | component_wrapper_get_extents (AtkComponent *component, |
| 274 | gint *x, |
| 275 | gint *y, |
| 276 | gint *width, |
| 277 | gint *height, |
| 278 | AtkCoordType coord_type) |
| 279 | { |
| 280 | component_wrapper_get_position( component, x, y, coord_type ); |
| 281 | component_wrapper_get_size( component, width, height ); |
| 282 | } |
| 283 | |
| 284 | /*****************************************************************************/ |
| 285 | |
| 286 | static gboolean |
| 287 | component_wrapper_set_extents (AtkComponent *, gint, gint, gint, gint, AtkCoordType) |
| 288 | { |
| 289 | g_warning( "AtkComponent::set_extents unimplementable" ); |
| 290 | return FALSE; |
| 291 | } |
| 292 | |
| 293 | /*****************************************************************************/ |
| 294 | |
| 295 | static gboolean |
| 296 | component_wrapper_set_position (AtkComponent *, gint, gint, AtkCoordType) |
| 297 | { |
| 298 | g_warning( "AtkComponent::set_position unimplementable" ); |
| 299 | return FALSE; |
| 300 | } |
| 301 | |
| 302 | /*****************************************************************************/ |
| 303 | |
| 304 | static gboolean |
| 305 | component_wrapper_set_size (AtkComponent *, gint, gint) |
| 306 | { |
| 307 | g_warning( "AtkComponent::set_size unimplementable" ); |
| 308 | return FALSE; |
| 309 | } |
| 310 | |
| 311 | /*****************************************************************************/ |
| 312 | |
| 313 | static AtkLayer |
| 314 | component_wrapper_get_layer (AtkComponent *component) |
| 315 | { |
| 316 | AtkRole role = atk_object_get_role( ATK_OBJECT( component ) ); |
| 317 | AtkLayer layer = ATK_LAYER_WIDGET; |
| 318 | |
| 319 | switch (role) |
| 320 | { |
| 321 | case ATK_ROLE_POPUP_MENU: |
| 322 | case ATK_ROLE_MENU_ITEM: |
| 323 | case ATK_ROLE_CHECK_MENU_ITEM: |
| 324 | case ATK_ROLE_SEPARATOR: |
| 325 | case ATK_ROLE_LIST_ITEM: |
| 326 | layer = ATK_LAYER_POPUP; |
| 327 | break; |
| 328 | case ATK_ROLE_MENU: |
| 329 | { |
| 330 | AtkObject * parent = atk_object_get_parent( ATK_OBJECT( component ) ); |
| 331 | if( atk_object_get_role( parent ) != ATK_ROLE_MENU_BAR ) |
| 332 | layer = ATK_LAYER_POPUP; |
| 333 | } |
| 334 | break; |
| 335 | |
| 336 | case ATK_ROLE_LIST: |
| 337 | { |
| 338 | AtkObject * parent = atk_object_get_parent( ATK_OBJECT( component ) ); |
| 339 | if( atk_object_get_role( parent ) == ATK_ROLE_COMBO_BOX ) |
| 340 | layer = ATK_LAYER_POPUP; |
| 341 | } |
| 342 | break; |
| 343 | |
| 344 | default: |
| 345 | ; |
| 346 | } |
| 347 | |
| 348 | return layer; |
| 349 | } |
| 350 | |
| 351 | /*****************************************************************************/ |
| 352 | |
| 353 | static gint |
| 354 | component_wrapper_get_mdi_zorder (AtkComponent *) |
| 355 | { |
| 356 | // only needed for ATK_LAYER_MDI (not used) or ATK_LAYER_WINDOW (inherited from GAIL) |
| 357 | return G_MININT; |
| 358 | } |
| 359 | |
| 360 | /*****************************************************************************/ |
| 361 | |
| 362 | // This code is mostly stolen from libgail .. |
| 363 | |
| 364 | static guint |
| 365 | component_wrapper_add_focus_handler (AtkComponent *component, |
| 366 | AtkFocusHandler handler) |
| 367 | { |
| 368 | GSignalMatchType match_type; |
| 369 | gulong ret; |
| 370 | guint signal_id; |
| 371 | |
| 372 | match_type = GSignalMatchType(G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC); |
| 373 | signal_id = g_signal_lookup( "focus-event", ATK_TYPE_OBJECT ); |
| 374 | |
| 375 | ret = g_signal_handler_find( component, match_type, signal_id, 0, nullptr, |
| 376 | static_cast<gpointer>(&handler), nullptr); |
| 377 | if (!ret) |
| 378 | { |
| 379 | return g_signal_connect_closure_by_id (component, |
| 380 | signal_id, 0, |
| 381 | g_cclosure_new ( |
| 382 | G_CALLBACK (handler), nullptr, |
| 383 | nullptr), |
| 384 | FALSE); |
| 385 | } |
| 386 | else |
| 387 | { |
| 388 | return 0; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /*****************************************************************************/ |
| 393 | |
| 394 | static void |
| 395 | component_wrapper_remove_focus_handler (AtkComponent *component, |
| 396 | guint handler_id) |
| 397 | { |
| 398 | g_signal_handler_disconnect (component, handler_id); |
| 399 | } |
| 400 | |
| 401 | /*****************************************************************************/ |
| 402 | |
| 403 | } // extern "C" |
| 404 | |
| 405 | void |
| 406 | componentIfaceInit (gpointer iface_, gpointer) |
| 407 | { |
| 408 | auto const iface = static_cast<AtkComponentIface *>(iface_); |
| 409 | g_return_if_fail (iface != nullptr); |
| 410 | |
| 411 | iface->add_focus_handler = component_wrapper_add_focus_handler; |
| 412 | iface->contains = component_wrapper_contains; |
| 413 | iface->get_extents = component_wrapper_get_extents; |
| 414 | iface->get_layer = component_wrapper_get_layer; |
| 415 | iface->get_mdi_zorder = component_wrapper_get_mdi_zorder; |
| 416 | iface->get_position = component_wrapper_get_position; |
| 417 | iface->get_size = component_wrapper_get_size; |
| 418 | iface->grab_focus = component_wrapper_grab_focus; |
| 419 | iface->ref_accessible_at_point = component_wrapper_ref_accessible_at_point; |
| 420 | iface->remove_focus_handler = component_wrapper_remove_focus_handler; |
| 421 | iface->set_extents = component_wrapper_set_extents; |
| 422 | iface->set_position = component_wrapper_set_position; |
| 423 | iface->set_size = component_wrapper_set_size; |
| 424 | } |
| 425 | |
| 426 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |