b42d618374c01525ffb5acfea00c385b4a2e2b1f
[platform/core/uifw/eail.git] / eail / eail / eail_text.c
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * @file eail_text.c
22  * @brief EailText implementation
23  */
24
25 #include <Elementary.h>
26
27 #include "eail_text.h"
28 #include "eail_factory.h"
29 #include "eail_utils.h"
30
31 /*
32  * Implementation of the *AtkObject* interface
33  */
34
35 static void atk_text_interface_init(AtkTextIface *iface);
36
37 /**
38  * @brief EailText type definition
39  */
40 G_DEFINE_TYPE_WITH_CODE(EailText,
41                         eail_text,
42                         EAIL_TYPE_WIDGET,
43                         G_IMPLEMENT_INTERFACE(ATK_TYPE_TEXT,
44                                               atk_text_interface_init));
45
46 /**
47  * @brief EailText initializer
48  *
49  * @param obj an AtkObject
50  * @param data initialization data
51  */
52 static void
53 eail_text_initialize(AtkObject *obj, gpointer data)
54 {
55    ATK_OBJECT_CLASS(eail_text_parent_class)->initialize(obj, data);
56
57    obj->role = ATK_ROLE_TEXT;
58 }
59
60 /**
61  * @brief Gets obj name
62  *
63  * Returns obj accessible name if assigned, obj text content otherwise
64  *
65  * @param obj an AtkObject
66  * @returns accessible obj name if assigned, obj text content otherwise
67  */
68 static const gchar*
69 eail_text_get_name(AtkObject *obj)
70 {
71    const gchar *name;
72    Evas_Object *widget;
73
74    name = ATK_OBJECT_CLASS(eail_text_parent_class)->get_name(obj);
75    if (NULL != name)
76      return name;
77
78    widget = eail_widget_get_widget(EAIL_WIDGET(obj));
79    if (widget)
80      name = (const gchar*)elm_object_text_get(widget);
81
82    return name;
83 }
84
85 /**
86  * @brief EailText instance initializer
87  *
88  * @param text an EailText
89  */
90 static void
91 eail_text_init(EailText *text)
92 {
93 }
94
95 /**
96  * @brief EailText class initializer
97  *
98  * @param klass an EailTextClass
99  */
100 static void
101 eail_text_class_init(EailTextClass *klass)
102 {
103    AtkObjectClass *class = ATK_OBJECT_CLASS(klass);
104
105    class->initialize = eail_text_initialize;
106    class->get_name = eail_text_get_name;
107 }
108
109 /*
110  * Implementation of the *AtkText* interface
111  */
112
113 /**
114  * @brief Gets text bounded by start_offset and end_offset
115  *
116  * Use g_free() to free the returned string
117  *
118  * @param text an AtkText
119  * @param start_offset start position
120  * @param end_offset end position, -1 for the end of the string
121  * @return string containing text from start_offset up to, but not including
122  * end_offset
123  */
124 static gchar*
125 eail_text_get_text(AtkText   *text,
126                    gint       start_offset,
127                    gint       end_offset)
128 {
129    gchar *string = NULL;
130    Evas_Object *widget = eail_widget_get_widget(EAIL_WIDGET(text));
131
132    if (widget)
133      string = (gchar *)elm_object_text_get(widget);
134
135    return eail_get_substring(string, start_offset, end_offset);
136 }
137
138 /**
139  * @brief Gets character at offset
140  *
141  * @param text an AtkText
142  * @param offset character offset
143  * @return character at offset
144  */
145 static gunichar
146 eail_text_get_character_at_offset(AtkText    *text,
147                                   gint        offset)
148 {
149    gunichar character = '\0';
150    Evas_Object *widget = eail_widget_get_widget(EAIL_WIDGET(text));
151
152    if (widget)
153      character = g_utf8_get_char(
154          g_utf8_offset_to_pointer(elm_object_text_get(widget), offset));
155
156    return character;
157 }
158
159 /**
160  * @brief Gets text length
161  *
162  * @param text an AtkText
163  * @return text length
164  */
165 static gint
166 eail_text_get_character_count(AtkText *text)
167 {
168    gint count = 0;
169    const gchar *string_text = NULL;
170
171    Evas_Object *widget = eail_widget_get_widget(EAIL_WIDGET(text));
172
173    if (!widget) return count;
174
175    string_text = elm_object_text_get(widget);
176    if (!string_text) return count;
177
178    count = g_utf8_strlen(string_text, -1);
179
180    return count;
181 }
182
183 /**
184  * @brief AtkText interface initializer
185  *
186  * @param iface an AtkTextIface
187  */
188 static void
189 atk_text_interface_init(AtkTextIface *iface)
190 {
191    iface->get_text = eail_text_get_text;
192    iface->get_character_at_offset = eail_text_get_character_at_offset;
193    iface->get_character_count = eail_text_get_character_count;
194 }