1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "InterfaceInitFuncs.h"
8 
9 #include "Accessible-inl.h"
10 #include "HyperTextAccessible.h"
11 #include "nsMai.h"
12 #include "nsMaiHyperlink.h"
13 #include "ProxyAccessible.h"
14 #include "mozilla/Likely.h"
15 
16 using namespace mozilla::a11y;
17 
18 extern "C" {
19 
getLinkCB(AtkHypertext * aText,gint aLinkIndex)20 static AtkHyperlink* getLinkCB(AtkHypertext* aText, gint aLinkIndex) {
21   AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
22   AtkObject* atkHyperLink = nullptr;
23   if (accWrap) {
24     HyperTextAccessible* hyperText = accWrap->AsHyperText();
25     NS_ENSURE_TRUE(hyperText, nullptr);
26 
27     Accessible* hyperLink = hyperText->LinkAt(aLinkIndex);
28     if (!hyperLink || !hyperLink->IsLink()) {
29       return nullptr;
30     }
31 
32     atkHyperLink = AccessibleWrap::GetAtkObject(hyperLink);
33   } else if (ProxyAccessible* proxy = GetProxy(ATK_OBJECT(aText))) {
34     ProxyAccessible* proxyLink = proxy->LinkAt(aLinkIndex);
35     if (!proxyLink) return nullptr;
36 
37     atkHyperLink = GetWrapperFor(proxyLink);
38   }
39 
40   NS_ENSURE_TRUE(IS_MAI_OBJECT(atkHyperLink), nullptr);
41   return MAI_ATK_OBJECT(atkHyperLink)->GetAtkHyperlink();
42 }
43 
getLinkCountCB(AtkHypertext * aText)44 static gint getLinkCountCB(AtkHypertext* aText) {
45   AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
46   if (accWrap) {
47     HyperTextAccessible* hyperText = accWrap->AsHyperText();
48     NS_ENSURE_TRUE(hyperText, -1);
49     return hyperText->LinkCount();
50   }
51 
52   if (ProxyAccessible* proxy = GetProxy(ATK_OBJECT(aText))) {
53     return proxy->LinkCount();
54   }
55 
56   return -1;
57 }
58 
getLinkIndexCB(AtkHypertext * aText,gint aCharIndex)59 static gint getLinkIndexCB(AtkHypertext* aText, gint aCharIndex) {
60   AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aText));
61   if (accWrap) {
62     HyperTextAccessible* hyperText = accWrap->AsHyperText();
63     NS_ENSURE_TRUE(hyperText, -1);
64 
65     return hyperText->LinkIndexAtOffset(aCharIndex);
66   }
67 
68   if (ProxyAccessible* proxy = GetProxy(ATK_OBJECT(aText))) {
69     return proxy->LinkIndexAtOffset(aCharIndex);
70   }
71 
72   return -1;
73 }
74 }
75 
hypertextInterfaceInitCB(AtkHypertextIface * aIface)76 void hypertextInterfaceInitCB(AtkHypertextIface* aIface) {
77   NS_ASSERTION(aIface, "no interface!");
78   if (MOZ_UNLIKELY(!aIface)) return;
79 
80   aIface->get_link = getLinkCB;
81   aIface->get_n_links = getLinkCountCB;
82   aIface->get_link_index = getLinkIndexCB;
83 }
84