| 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 | |
| 22 | #include <com/sun/star/accessibility/XAccessibleSelection.hpp> |
| 23 | #include <sal/log.hxx> |
| 24 | |
| 25 | using namespace ::com::sun::star; |
| 26 | |
| 27 | /// @throws uno::RuntimeException |
| 28 | static css::uno::Reference<css::accessibility::XAccessibleSelection> |
| 29 | getSelection( AtkSelection *pSelection ) |
| 30 | { |
| 31 | AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pSelection ); |
| 32 | if( pWrap ) |
| 33 | { |
| 34 | if( !pWrap->mpSelection.is() ) |
| 35 | { |
| 36 | pWrap->mpSelection.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 37 | } |
| 38 | |
| 39 | return pWrap->mpSelection; |
| 40 | } |
| 41 | |
| 42 | return css::uno::Reference<css::accessibility::XAccessibleSelection>(); |
| 43 | } |
| 44 | |
| 45 | extern "C" { |
| 46 | |
| 47 | static gboolean |
| 48 | selection_add_selection( AtkSelection *selection, |
| 49 | gint i ) |
| 50 | { |
| 51 | try { |
| 52 | css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection |
| 53 | = getSelection( selection ); |
| 54 | if( pSelection.is() ) |
| 55 | { |
| 56 | pSelection->selectAccessibleChild( i ); |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | catch(const uno::Exception&) { |
| 61 | g_warning( "Exception in selectAccessibleChild()" ); |
| 62 | } |
| 63 | |
| 64 | return FALSE; |
| 65 | } |
| 66 | |
| 67 | static gboolean |
| 68 | selection_clear_selection( AtkSelection *selection ) |
| 69 | { |
| 70 | try { |
| 71 | css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection |
| 72 | = getSelection( selection ); |
| 73 | if( pSelection.is() ) |
| 74 | { |
| 75 | pSelection->clearAccessibleSelection(); |
| 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | catch(const uno::Exception&) { |
| 80 | g_warning( "Exception in clearAccessibleSelection()" ); |
| 81 | } |
| 82 | |
| 83 | return FALSE; |
| 84 | } |
| 85 | |
| 86 | static AtkObject* |
| 87 | selection_ref_selection( AtkSelection *selection, |
| 88 | gint i ) |
| 89 | { |
| 90 | try { |
| 91 | css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection |
| 92 | = getSelection( selection ); |
| 93 | if( pSelection.is() ) |
| 94 | return atk_object_wrapper_ref( pSelection->getSelectedAccessibleChild( i ) ); |
| 95 | } |
| 96 | catch(const uno::Exception&) { |
| 97 | g_warning( "Exception in getSelectedAccessibleChild()" ); |
| 98 | } |
| 99 | |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
| 103 | static gint |
| 104 | selection_get_selection_count( AtkSelection *selection) |
| 105 | { |
| 106 | try { |
| 107 | css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection |
| 108 | = getSelection( selection ); |
| 109 | if( pSelection.is() ) |
| 110 | { |
| 111 | sal_Int64 nSelected = pSelection->getSelectedAccessibleChildCount(); |
| 112 | if (nSelected > std::numeric_limits<gint>::max()) |
| 113 | { |
| 114 | SAL_WARN("vcl.gtk", "selection_get_selection_count: Count exceeds maximum gint value, " |
| 115 | "using max gint."); |
| 116 | nSelected = std::numeric_limits<gint>::max(); |
| 117 | } |
| 118 | return nSelected; |
| 119 | } |
| 120 | } |
| 121 | catch(const uno::Exception&) { |
| 122 | g_warning( "Exception in getSelectedAccessibleChildCount()" ); |
| 123 | } |
| 124 | |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | static gboolean |
| 129 | selection_is_child_selected( AtkSelection *selection, |
| 130 | gint i) |
| 131 | { |
| 132 | try { |
| 133 | css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection |
| 134 | = getSelection( selection ); |
| 135 | if( pSelection.is() ) |
| 136 | return pSelection->isAccessibleChildSelected( i ); |
| 137 | } |
| 138 | catch(const uno::Exception&) { |
| 139 | g_warning( "Exception in isAccessibleChildSelected()" ); |
| 140 | } |
| 141 | |
| 142 | return FALSE; |
| 143 | } |
| 144 | |
| 145 | static gboolean |
| 146 | selection_remove_selection( AtkSelection *selection, |
| 147 | gint i ) |
| 148 | { |
| 149 | try { |
| 150 | css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection |
| 151 | = getSelection( selection ); |
| 152 | if( pSelection.is() ) |
| 153 | { |
| 154 | css::uno::Reference<css::accessibility::XAccessible> xAcc = pSelection->getSelectedAccessibleChild(i); |
| 155 | if (!xAcc.is()) |
| 156 | return false; |
| 157 | |
| 158 | css::uno::Reference<css::accessibility::XAccessibleContext> xAccContext = xAcc->getAccessibleContext(); |
| 159 | const sal_Int64 nChildIndex = xAccContext->getAccessibleIndexInParent(); |
| 160 | pSelection->deselectAccessibleChild(nChildIndex); |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | catch(const uno::Exception&) { |
| 165 | g_warning( "Exception in getSelectedAccessibleChild(), getAccessibleIndexInParent() or deselectAccessibleChild()" ); |
| 166 | } |
| 167 | |
| 168 | return FALSE; |
| 169 | } |
| 170 | |
| 171 | static gboolean |
| 172 | selection_select_all_selection( AtkSelection *selection) |
| 173 | { |
| 174 | try { |
| 175 | css::uno::Reference<css::accessibility::XAccessibleSelection> pSelection |
| 176 | = getSelection( selection ); |
| 177 | if( pSelection.is() ) |
| 178 | { |
| 179 | pSelection->selectAllAccessibleChildren(); |
| 180 | return true; |
| 181 | } |
| 182 | } |
| 183 | catch(const uno::Exception&) { |
| 184 | g_warning( "Exception in selectAllAccessibleChildren()" ); |
| 185 | } |
| 186 | |
| 187 | return FALSE; |
| 188 | } |
| 189 | |
| 190 | } // extern "C" |
| 191 | |
| 192 | void |
| 193 | selectionIfaceInit( gpointer iface_, gpointer) |
| 194 | { |
| 195 | auto const iface = static_cast<AtkSelectionIface *>(iface_); |
| 196 | g_return_if_fail (iface != nullptr); |
| 197 | |
| 198 | iface->add_selection = selection_add_selection; |
| 199 | iface->clear_selection = selection_clear_selection; |
| 200 | iface->ref_selection = selection_ref_selection; |
| 201 | iface->get_selection_count = selection_get_selection_count; |
| 202 | iface->is_child_selected = selection_is_child_selected; |
| 203 | iface->remove_selection = selection_remove_selection; |
| 204 | iface->select_all_selection = selection_select_all_selection; |
| 205 | } |
| 206 | |
| 207 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |