2 * Copyright 2008 Codethink Ltd.
3 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "my-atk-object.h"
27 #include "my-atk-hypertext.h"
28 #include "my-atk-hyperlink.h"
30 typedef struct _MyAtkHypertextInfo MyAtkHypertextInfo;
32 static void atk_hypertext_interface_init (AtkHypertextIface *iface);
33 static void GDestroyNotifyGOBJptrArray (gpointer data);
35 G_DEFINE_TYPE_WITH_CODE (MyAtkHypertext,
38 G_IMPLEMENT_INTERFACE (ATK_TYPE_HYPERTEXT,
39 atk_hypertext_interface_init));
42 my_atk_set_hypertext (AtkHypertext *obj, const gchar *text)
44 MyAtkHypertext *self = MY_ATK_HYPERTEXT (obj);
47 const gchar *fstr= "href=";
48 gint len = strlen (fstr);
49 gint text_len = strlen (text);
52 g_return_val_if_fail (MY_IS_ATK_HYPERTEXT (obj), -1);
58 ptr = g_strstr_len (text + index, text_len - index, fstr);
59 index = ptr - text + len;
61 if (text_len < index || index < len)
63 if (text[index] == '\'') {
64 start_offset = index + 1;
65 while (++index < text_len && text[index] != '\'');
66 if (text[index] != '\'')
68 link = new_MyAtkHyperlink ();
69 my_atk_set_hyperlink (ATK_HYPERLINK (link),
70 g_strndup (text + start_offset, index - start_offset),
73 g_ptr_array_add (self->array, link);
79 return self->array->len > 0 ? 0 : -1;
83 my_atk_hypertext_get_n_links (AtkHypertext *obj)
85 MyAtkHypertext *self = MY_ATK_HYPERTEXT (obj);
86 g_return_val_if_fail (MY_IS_ATK_HYPERTEXT (obj), -1);
87 return self->array->len;
92 my_atk_hypertext_get_link (AtkHypertext *obj, gint link_index)
94 MyAtkHypertext *self = MY_ATK_HYPERTEXT (obj);
95 AtkHyperlink *link = NULL;
96 g_return_val_if_fail (MY_IS_ATK_HYPERTEXT (obj), NULL);
97 if (0 <= link_index && link_index < self->array->len)
98 link = g_ptr_array_index (self->array, link_index);
103 my_atk_hypertext_get_link_index (AtkHypertext *obj, gint char_index)
105 MyAtkHypertext *self = MY_ATK_HYPERTEXT (obj);
107 MyAtkHyperlink *link;
108 g_return_val_if_fail (MY_IS_ATK_HYPERTEXT (obj), -1);
110 for (i = 0; i < self->array->len; i++) {
111 link = g_ptr_array_index (self->array, i);
112 if (link->start <= char_index && char_index <= link->end)
119 my_atk_hypertext_link_selected (AtkHypertext *obj, gint link_index)
121 g_return_if_fail (MY_IS_ATK_HYPERTEXT (obj));
125 GDestroyNotifyGOBJptrArray (gpointer data)
127 g_object_unref (data);
131 atk_hypertext_interface_init (AtkHypertextIface *iface)
134 iface->get_n_links = my_atk_hypertext_get_n_links;
135 iface->get_link = my_atk_hypertext_get_link;
136 iface->get_link_index = my_atk_hypertext_get_link_index;
137 iface->link_selected = my_atk_hypertext_link_selected;
141 my_atk_hypertext_init (MyAtkHypertext *self)
143 self->array = g_ptr_array_new_full (2, GDestroyNotifyGOBJptrArray);
147 my_atk_hypertext_class_initialize (AtkObject *obj, gpointer data)
152 my_atk_hypertext_class_finalize (GObject *obj)
157 my_atk_hypertext_class_init(MyAtkHypertextClass *my_class)
159 AtkObjectClass *atk_class = ATK_OBJECT_CLASS(my_class);
160 GObjectClass *gobject_class = G_OBJECT_CLASS(my_class);
162 gobject_class->finalize = my_atk_hypertext_class_finalize;
164 atk_class->initialize = my_atk_hypertext_class_initialize;