2.34.1
[platform/upstream/at-spi2-atk.git] / tests / dummyatk / my-atk-hypertext.c
1 /*
2  * Copyright 2008 Codethink Ltd.
3  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include <glib.h>
22 #include <string.h>
23 #include <atk/atk.h>
24 #include <stdio.h>
25
26 #include "my-atk-object.h"
27 #include "my-atk-hypertext.h"
28 #include "my-atk-hyperlink.h"
29
30 typedef struct _MyAtkHypertextInfo MyAtkHypertextInfo;
31
32 static void atk_hypertext_interface_init (AtkHypertextIface *iface);
33 static void GDestroyNotifyGOBJptrArray (gpointer data);
34
35 G_DEFINE_TYPE_WITH_CODE (MyAtkHypertext,
36                          my_atk_hypertext,
37                          MY_TYPE_ATK_OBJECT,
38                          G_IMPLEMENT_INTERFACE (ATK_TYPE_HYPERTEXT,
39                              atk_hypertext_interface_init));
40
41 gint
42 my_atk_set_hypertext (AtkHypertext *obj, const gchar *text)
43 {
44   MyAtkHypertext *self = MY_ATK_HYPERTEXT (obj);
45   MyAtkHyperlink *link;
46   gchar *ptr;
47   const gchar *fstr= "href=";
48   gint len = strlen (fstr);
49   gint text_len = strlen (text);
50   gint index = 0;
51   gint start_offset;
52   g_return_val_if_fail (MY_IS_ATK_HYPERTEXT (obj), -1);
53
54   if (text_len < len)
55     return -1;
56
57   while (text) {
58     ptr = g_strstr_len (text + index, text_len - index, fstr);
59     index = ptr - text + len;
60     if (ptr) {
61       if (text_len < index || index < len)
62         break;
63       if (text[index] == '\'') {
64         start_offset = index + 1;
65         while (++index < text_len && text[index] != '\'');
66         if (text[index] != '\'')
67           break;
68         link = new_MyAtkHyperlink ();
69         my_atk_set_hyperlink (ATK_HYPERLINK (link),
70                               g_strndup (text + start_offset, index - start_offset),
71                               start_offset,
72                               index);
73         g_ptr_array_add (self->array, link);
74       }
75     } else
76       break;
77   }
78
79   return self->array->len > 0 ? 0 : -1;
80 }
81
82 static gint
83 my_atk_hypertext_get_n_links (AtkHypertext *obj)
84 {
85   MyAtkHypertext *self = MY_ATK_HYPERTEXT (obj);
86   g_return_val_if_fail (MY_IS_ATK_HYPERTEXT (obj), -1);
87   return self->array->len;
88
89 }
90
91 static AtkHyperlink *
92 my_atk_hypertext_get_link (AtkHypertext *obj, gint link_index)
93 {
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);
99   return link;
100 }
101
102 static gint
103 my_atk_hypertext_get_link_index (AtkHypertext *obj, gint char_index)
104 {
105   MyAtkHypertext *self = MY_ATK_HYPERTEXT (obj);
106   gint i;
107   MyAtkHyperlink *link;
108   g_return_val_if_fail (MY_IS_ATK_HYPERTEXT (obj), -1);
109
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)
113       return i;
114   }
115   return -1;
116 }
117
118 static void
119 my_atk_hypertext_link_selected (AtkHypertext *obj, gint link_index)
120 {
121   g_return_if_fail (MY_IS_ATK_HYPERTEXT (obj));
122 }
123
124 static void
125 GDestroyNotifyGOBJptrArray (gpointer data)
126 {
127   g_object_unref (data);
128 }
129
130 static void
131 atk_hypertext_interface_init (AtkHypertextIface *iface)
132 {
133   if (!iface) return;
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;
138 }
139
140 static void
141 my_atk_hypertext_init (MyAtkHypertext *self)
142 {
143   self->array = g_ptr_array_new_full (2, GDestroyNotifyGOBJptrArray);
144 }
145
146 static void
147 my_atk_hypertext_class_initialize (AtkObject *obj, gpointer data)
148 {
149 }
150
151 static void
152 my_atk_hypertext_class_finalize (GObject *obj)
153 {
154 }
155
156 static void
157 my_atk_hypertext_class_init(MyAtkHypertextClass *my_class)
158 {
159   AtkObjectClass *atk_class = ATK_OBJECT_CLASS(my_class);
160   GObjectClass *gobject_class = G_OBJECT_CLASS(my_class);
161
162   gobject_class->finalize = my_atk_hypertext_class_finalize;
163
164   atk_class->initialize = my_atk_hypertext_class_initialize;
165 }