| 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/XAccessibleHypertext.hpp> |
| 23 | |
| 24 | using namespace ::com::sun::star; |
| 25 | |
| 26 | // ---------------------- AtkHyperlink ---------------------- |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | struct HyperLink { |
| 31 | AtkHyperlink const atk_hyper_link; |
| 32 | |
| 33 | uno::Reference< accessibility::XAccessibleHyperlink > xLink; |
| 34 | }; |
| 35 | |
| 36 | } |
| 37 | |
| 38 | static uno::Reference< accessibility::XAccessibleHyperlink > const & |
| 39 | getHyperlink( AtkHyperlink *pHyperlink ) |
| 40 | { |
| 41 | HyperLink *pLink = reinterpret_cast<HyperLink *>(pHyperlink); |
| 42 | return pLink->xLink; |
| 43 | } |
| 44 | |
| 45 | static GObjectClass *hyper_parent_class = nullptr; |
| 46 | |
| 47 | extern "C" { |
| 48 | |
| 49 | static void |
| 50 | hyper_link_finalize (GObject *obj) |
| 51 | { |
| 52 | HyperLink *hl = reinterpret_cast<HyperLink *>(obj); |
| 53 | hl->xLink.clear(); |
| 54 | hyper_parent_class->finalize (obj); |
| 55 | } |
| 56 | |
| 57 | static gchar * |
| 58 | hyper_link_get_uri( AtkHyperlink *pLink, |
| 59 | gint i ) |
| 60 | { |
| 61 | try { |
| 62 | uno::Any aAny = getHyperlink( pLink )->getAccessibleActionObject( i ); |
| 63 | OUString aUri = aAny.get< OUString > (); |
| 64 | return OUStringToGChar(aUri); |
| 65 | } |
| 66 | catch(const uno::Exception&) { |
| 67 | g_warning( "Exception in hyper_link_get_uri" ); |
| 68 | } |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | static AtkObject * |
| 73 | hyper_link_get_object( AtkHyperlink *, |
| 74 | gint ) |
| 75 | { |
| 76 | g_warning( "FIXME: hyper_link_get_object unimplemented" ); |
| 77 | return nullptr; |
| 78 | } |
| 79 | static gint |
| 80 | hyper_link_get_end_index( AtkHyperlink *pLink ) |
| 81 | { |
| 82 | try { |
| 83 | return getHyperlink( pLink )->getEndIndex(); |
| 84 | } |
| 85 | catch(const uno::Exception&) { |
| 86 | } |
| 87 | return -1; |
| 88 | } |
| 89 | static gint |
| 90 | hyper_link_get_start_index( AtkHyperlink *pLink ) |
| 91 | { |
| 92 | try { |
| 93 | return getHyperlink( pLink )->getStartIndex(); |
| 94 | } |
| 95 | catch(const uno::Exception&) { |
| 96 | } |
| 97 | return -1; |
| 98 | } |
| 99 | static gboolean |
| 100 | hyper_link_is_valid( AtkHyperlink *pLink ) |
| 101 | { |
| 102 | try { |
| 103 | return getHyperlink( pLink )->isValid(); |
| 104 | } |
| 105 | catch(const uno::Exception&) { |
| 106 | } |
| 107 | return FALSE; |
| 108 | } |
| 109 | static gint |
| 110 | hyper_link_get_n_anchors( AtkHyperlink *pLink ) |
| 111 | { |
| 112 | try { |
| 113 | return getHyperlink( pLink )->getAccessibleActionCount(); |
| 114 | } |
| 115 | catch(const uno::Exception&) { |
| 116 | } |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static guint |
| 121 | hyper_link_link_state( AtkHyperlink * ) |
| 122 | { |
| 123 | g_warning( "FIXME: hyper_link_link_state unimplemented" ); |
| 124 | return 0; |
| 125 | } |
| 126 | static gboolean |
| 127 | hyper_link_is_selected_link( AtkHyperlink * ) |
| 128 | { |
| 129 | g_warning( "FIXME: hyper_link_is_selected_link unimplemented" ); |
| 130 | return FALSE; |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | hyper_link_class_init (gpointer klass_, gpointer) |
| 135 | { |
| 136 | auto const klass = static_cast<AtkHyperlinkClass *>(klass_); |
| 137 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 138 | |
| 139 | gobject_class->finalize = hyper_link_finalize; |
| 140 | |
| 141 | hyper_parent_class = static_cast<GObjectClass *>(g_type_class_peek_parent (klass)); |
| 142 | |
| 143 | klass->get_uri = hyper_link_get_uri; |
| 144 | klass->get_object = hyper_link_get_object; |
| 145 | klass->get_end_index = hyper_link_get_end_index; |
| 146 | klass->get_start_index = hyper_link_get_start_index; |
| 147 | klass->is_valid = hyper_link_is_valid; |
| 148 | klass->get_n_anchors = hyper_link_get_n_anchors; |
| 149 | klass->link_state = hyper_link_link_state; |
| 150 | klass->is_selected_link = hyper_link_is_selected_link; |
| 151 | } |
| 152 | |
| 153 | static GType |
| 154 | hyper_link_get_type() |
| 155 | { |
| 156 | static GType type = 0; |
| 157 | |
| 158 | if (!type) { |
| 159 | static const GTypeInfo tinfo = { |
| 160 | sizeof (AtkHyperlinkClass), |
| 161 | nullptr, /* base init */ |
| 162 | nullptr, /* base finalize */ |
| 163 | hyper_link_class_init, |
| 164 | nullptr, /* class finalize */ |
| 165 | nullptr, /* class data */ |
| 166 | sizeof (HyperLink), /* instance size */ |
| 167 | 0, /* nb preallocs */ |
| 168 | nullptr, /* instance init */ |
| 169 | nullptr /* value table */ |
| 170 | }; |
| 171 | |
| 172 | static const GInterfaceInfo atk_action_info = { |
| 173 | actionIfaceInit, |
| 174 | nullptr, |
| 175 | nullptr |
| 176 | }; |
| 177 | |
| 178 | type = g_type_register_static (ATK_TYPE_HYPERLINK, |
| 179 | "OOoAtkObjHyperLink", &tinfo, |
| 180 | GTypeFlags(0)); |
| 181 | g_type_add_interface_static (type, ATK_TYPE_ACTION, |
| 182 | &atk_action_info); |
| 183 | } |
| 184 | |
| 185 | return type; |
| 186 | } |
| 187 | |
| 188 | // ---------------------- AtkHyperText ---------------------- |
| 189 | |
| 190 | /// @throws uno::RuntimeException |
| 191 | static css::uno::Reference<css::accessibility::XAccessibleHypertext> |
| 192 | getHypertext( AtkHypertext *pHypertext ) |
| 193 | { |
| 194 | AtkObjectWrapper *pWrap = ATK_OBJECT_WRAPPER( pHypertext ); |
| 195 | if( pWrap ) |
| 196 | { |
| 197 | if( !pWrap->mpHypertext.is() ) |
| 198 | { |
| 199 | pWrap->mpHypertext.set(pWrap->mpContext, css::uno::UNO_QUERY); |
| 200 | } |
| 201 | |
| 202 | return pWrap->mpHypertext; |
| 203 | } |
| 204 | |
| 205 | return css::uno::Reference<css::accessibility::XAccessibleHypertext>(); |
| 206 | } |
| 207 | |
| 208 | static AtkHyperlink * |
| 209 | hypertext_get_link( AtkHypertext *hypertext, |
| 210 | gint link_index) |
| 211 | { |
| 212 | try { |
| 213 | css::uno::Reference<css::accessibility::XAccessibleHypertext> pHypertext |
| 214 | = getHypertext( hypertext ); |
| 215 | if( pHypertext.is() ) |
| 216 | { |
| 217 | HyperLink *pLink = static_cast<HyperLink *>(g_object_new( hyper_link_get_type(), nullptr )); |
| 218 | pLink->xLink = pHypertext->getHyperLink( link_index ); |
| 219 | if( !pLink->xLink.is() ) { |
| 220 | g_object_unref( G_OBJECT( pLink ) ); |
| 221 | pLink = nullptr; |
| 222 | } |
| 223 | return ATK_HYPERLINK( pLink ); |
| 224 | } |
| 225 | } |
| 226 | catch(const uno::Exception&) { |
| 227 | g_warning( "Exception in getHyperLink()" ); |
| 228 | } |
| 229 | |
| 230 | return nullptr; |
| 231 | } |
| 232 | |
| 233 | static gint |
| 234 | hypertext_get_n_links( AtkHypertext *hypertext ) |
| 235 | { |
| 236 | try { |
| 237 | css::uno::Reference<css::accessibility::XAccessibleHypertext> pHypertext |
| 238 | = getHypertext( hypertext ); |
| 239 | if( pHypertext.is() ) |
| 240 | return pHypertext->getHyperLinkCount(); |
| 241 | } |
| 242 | catch(const uno::Exception&) { |
| 243 | g_warning( "Exception in getHyperLinkCount()" ); |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static gint |
| 250 | hypertext_get_link_index( AtkHypertext *hypertext, |
| 251 | gint index) |
| 252 | { |
| 253 | try { |
| 254 | css::uno::Reference<css::accessibility::XAccessibleHypertext> pHypertext |
| 255 | = getHypertext( hypertext ); |
| 256 | if( pHypertext.is() ) |
| 257 | return pHypertext->getHyperLinkIndex( index ); |
| 258 | } |
| 259 | catch(const uno::Exception&) { |
| 260 | g_warning( "Exception in getHyperLinkIndex()" ); |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | } // extern "C" |
| 267 | |
| 268 | void |
| 269 | hypertextIfaceInit (gpointer iface_, gpointer) |
| 270 | { |
| 271 | auto const iface = static_cast<AtkHypertextIface *>(iface_); |
| 272 | g_return_if_fail (iface != nullptr); |
| 273 | |
| 274 | iface->get_link = hypertext_get_link; |
| 275 | iface->get_n_links = hypertext_get_n_links; |
| 276 | iface->get_link_index = hypertext_get_link_index; |
| 277 | } |
| 278 | |
| 279 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |