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 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Sun Microsystems, Inc.
20 : * Portions created by the Initial Developer are Copyright (C) 2002
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Bolian Yin (bolian.yin@sun.com)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsIURI.h"
41 : #include "nsMaiHyperlink.h"
42 :
43 : /* MaiAtkHyperlink */
44 :
45 : #define MAI_TYPE_ATK_HYPERLINK (mai_atk_hyperlink_get_type ())
46 : #define MAI_ATK_HYPERLINK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),\
47 : MAI_TYPE_ATK_HYPERLINK, MaiAtkHyperlink))
48 : #define MAI_ATK_HYPERLINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),\
49 : MAI_TYPE_ATK_HYPERLINK, MaiAtkHyperlinkClass))
50 : #define MAI_IS_ATK_HYPERLINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),\
51 : MAI_TYPE_ATK_HYPERLINK))
52 : #define MAI_IS_ATK_HYPERLINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),\
53 : MAI_TYPE_ATK_HYPERLINK))
54 : #define MAI_ATK_HYPERLINK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),\
55 : MAI_TYPE_ATK_HYPERLINK, MaiAtkHyperlinkClass))
56 :
57 : /**
58 : * This MaiAtkHyperlink is a thin wrapper, in the MAI namespace,
59 : * for AtkHyperlink
60 : */
61 :
62 : struct MaiAtkHyperlink
63 : {
64 : AtkHyperlink parent;
65 :
66 : /*
67 : * The MaiHyperlink whose properties and features are exported via this
68 : * hyperlink instance.
69 : */
70 : MaiHyperlink *maiHyperlink;
71 : };
72 :
73 : struct MaiAtkHyperlinkClass
74 : {
75 : AtkHyperlinkClass parent_class;
76 : };
77 :
78 : GType mai_atk_hyperlink_get_type(void);
79 :
80 : G_BEGIN_DECLS
81 : /* callbacks for AtkHyperlink */
82 : static void classInitCB(AtkHyperlinkClass *aClass);
83 : static void finalizeCB(GObject *aObj);
84 :
85 : /* callbacks for AtkHyperlink virtual functions */
86 : static gchar *getUriCB(AtkHyperlink *aLink, gint aLinkIndex);
87 : static AtkObject *getObjectCB(AtkHyperlink *aLink, gint aLinkIndex);
88 : static gint getEndIndexCB(AtkHyperlink *aLink);
89 : static gint getStartIndexCB(AtkHyperlink *aLink);
90 : static gboolean isValidCB(AtkHyperlink *aLink);
91 : static gint getAnchorCountCB(AtkHyperlink *aLink);
92 : G_END_DECLS
93 :
94 : static gpointer parent_class = NULL;
95 : static nsAccessible*
96 : get_accessible_hyperlink(AtkHyperlink *aHyperlink);
97 :
98 : GType
99 0 : mai_atk_hyperlink_get_type(void)
100 : {
101 : static GType type = 0;
102 :
103 0 : if (!type) {
104 : static const GTypeInfo tinfo = {
105 : sizeof(MaiAtkHyperlinkClass),
106 : (GBaseInitFunc)NULL,
107 : (GBaseFinalizeFunc)NULL,
108 : (GClassInitFunc)classInitCB,
109 : (GClassFinalizeFunc)NULL,
110 : NULL, /* class data */
111 : sizeof(MaiAtkHyperlink), /* instance size */
112 : 0, /* nb preallocs */
113 : (GInstanceInitFunc)NULL,
114 : NULL /* value table */
115 : };
116 :
117 : type = g_type_register_static(ATK_TYPE_HYPERLINK,
118 : "MaiAtkHyperlink",
119 0 : &tinfo, GTypeFlags(0));
120 : }
121 0 : return type;
122 : }
123 :
124 0 : MaiHyperlink::MaiHyperlink(nsAccessible* aHyperLink) :
125 : mHyperlink(aHyperLink),
126 0 : mMaiAtkHyperlink(nsnull)
127 : {
128 0 : }
129 :
130 0 : MaiHyperlink::~MaiHyperlink()
131 : {
132 0 : if (mMaiAtkHyperlink) {
133 0 : MAI_ATK_HYPERLINK(mMaiAtkHyperlink)->maiHyperlink = nsnull;
134 0 : g_object_unref(mMaiAtkHyperlink);
135 : }
136 0 : }
137 :
138 : AtkHyperlink*
139 0 : MaiHyperlink::GetAtkHyperlink(void)
140 : {
141 0 : NS_ENSURE_TRUE(mHyperlink, nsnull);
142 :
143 0 : if (mMaiAtkHyperlink)
144 0 : return mMaiAtkHyperlink;
145 :
146 0 : if (!mHyperlink->IsLink())
147 0 : return nsnull;
148 :
149 : mMaiAtkHyperlink =
150 : reinterpret_cast<AtkHyperlink *>
151 0 : (g_object_new(mai_atk_hyperlink_get_type(), NULL));
152 0 : NS_ASSERTION(mMaiAtkHyperlink, "OUT OF MEMORY");
153 0 : NS_ENSURE_TRUE(mMaiAtkHyperlink, nsnull);
154 :
155 : /* be sure to initialize it with "this" */
156 0 : MaiHyperlink::Initialize(mMaiAtkHyperlink, this);
157 :
158 0 : return mMaiAtkHyperlink;
159 : }
160 :
161 : /* static */
162 :
163 : /* remember to call this static function when a MaiAtkHyperlink
164 : * is created
165 : */
166 :
167 : nsresult
168 0 : MaiHyperlink::Initialize(AtkHyperlink *aObj, MaiHyperlink *aHyperlink)
169 : {
170 0 : NS_ENSURE_ARG(MAI_IS_ATK_HYPERLINK(aObj));
171 0 : NS_ENSURE_ARG(aHyperlink);
172 :
173 : /* initialize hyperlink */
174 0 : MAI_ATK_HYPERLINK(aObj)->maiHyperlink = aHyperlink;
175 0 : return NS_OK;
176 : }
177 :
178 : /* static functions for ATK callbacks */
179 :
180 : void
181 0 : classInitCB(AtkHyperlinkClass *aClass)
182 : {
183 0 : GObjectClass *gobject_class = G_OBJECT_CLASS(aClass);
184 :
185 0 : parent_class = g_type_class_peek_parent(aClass);
186 :
187 0 : aClass->get_uri = getUriCB;
188 0 : aClass->get_object = getObjectCB;
189 0 : aClass->get_end_index = getEndIndexCB;
190 0 : aClass->get_start_index = getStartIndexCB;
191 0 : aClass->is_valid = isValidCB;
192 0 : aClass->get_n_anchors = getAnchorCountCB;
193 :
194 0 : gobject_class->finalize = finalizeCB;
195 0 : }
196 :
197 : void
198 0 : finalizeCB(GObject *aObj)
199 : {
200 0 : NS_ASSERTION(MAI_IS_ATK_HYPERLINK(aObj), "Invalid MaiAtkHyperlink");
201 0 : if (!MAI_IS_ATK_HYPERLINK(aObj))
202 0 : return;
203 :
204 0 : MaiAtkHyperlink *maiAtkHyperlink = MAI_ATK_HYPERLINK(aObj);
205 0 : maiAtkHyperlink->maiHyperlink = nsnull;
206 :
207 : /* call parent finalize function */
208 0 : if (G_OBJECT_CLASS (parent_class)->finalize)
209 0 : G_OBJECT_CLASS (parent_class)->finalize(aObj);
210 : }
211 :
212 : gchar *
213 0 : getUriCB(AtkHyperlink *aLink, gint aLinkIndex)
214 : {
215 0 : nsAccessible* hyperlink = get_accessible_hyperlink(aLink);
216 0 : NS_ENSURE_TRUE(hyperlink, nsnull);
217 :
218 0 : nsCOMPtr<nsIURI> uri = hyperlink->AnchorURIAt(aLinkIndex);
219 0 : if (!uri)
220 0 : return nsnull;
221 :
222 0 : nsCAutoString cautoStr;
223 0 : nsresult rv = uri->GetSpec(cautoStr);
224 0 : NS_ENSURE_SUCCESS(rv, nsnull);
225 :
226 0 : return g_strdup(cautoStr.get());
227 : }
228 :
229 : AtkObject *
230 0 : getObjectCB(AtkHyperlink *aLink, gint aLinkIndex)
231 : {
232 0 : nsAccessible* hyperlink = get_accessible_hyperlink(aLink);
233 0 : NS_ENSURE_TRUE(hyperlink, nsnull);
234 :
235 0 : nsAccessible* anchor = hyperlink->AnchorAt(aLinkIndex);
236 0 : NS_ENSURE_TRUE(anchor, nsnull);
237 :
238 0 : AtkObject *atkObj = nsAccessibleWrap::GetAtkObject(anchor);
239 : //no need to add ref it, because it is "get" not "ref"
240 0 : return atkObj;
241 : }
242 :
243 : gint
244 0 : getEndIndexCB(AtkHyperlink *aLink)
245 : {
246 0 : nsAccessible* hyperlink = get_accessible_hyperlink(aLink);
247 0 : NS_ENSURE_TRUE(hyperlink, -1);
248 :
249 0 : return static_cast<gint>(hyperlink->EndOffset());
250 : }
251 :
252 : gint
253 0 : getStartIndexCB(AtkHyperlink *aLink)
254 : {
255 0 : nsAccessible* hyperlink = get_accessible_hyperlink(aLink);
256 0 : NS_ENSURE_TRUE(hyperlink, -1);
257 :
258 0 : return static_cast<gint>(hyperlink->StartOffset());
259 : }
260 :
261 : gboolean
262 0 : isValidCB(AtkHyperlink *aLink)
263 : {
264 0 : nsAccessible* hyperlink = get_accessible_hyperlink(aLink);
265 0 : NS_ENSURE_TRUE(hyperlink, FALSE);
266 :
267 0 : return static_cast<gboolean>(hyperlink->IsLinkValid());
268 : }
269 :
270 : gint
271 0 : getAnchorCountCB(AtkHyperlink *aLink)
272 : {
273 0 : nsAccessible* hyperlink = get_accessible_hyperlink(aLink);
274 0 : NS_ENSURE_TRUE(hyperlink, -1);
275 :
276 0 : return static_cast<gint>(hyperlink->AnchorCount());
277 : }
278 :
279 : // Check if aHyperlink is a valid MaiHyperlink, and return the
280 : // HyperLinkAccessible related.
281 : nsAccessible*
282 0 : get_accessible_hyperlink(AtkHyperlink *aHyperlink)
283 : {
284 0 : NS_ENSURE_TRUE(MAI_IS_ATK_HYPERLINK(aHyperlink), nsnull);
285 : MaiHyperlink * maiHyperlink =
286 0 : MAI_ATK_HYPERLINK(aHyperlink)->maiHyperlink;
287 0 : NS_ENSURE_TRUE(maiHyperlink != nsnull, nsnull);
288 0 : NS_ENSURE_TRUE(maiHyperlink->GetAtkHyperlink() == aHyperlink, nsnull);
289 0 : return maiHyperlink->GetAccHyperlink();
290 : }
|