1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* IBus - The Input Bus
4 * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
5 * Copyright (C) 2008-2010 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 /* functions prototype */
25 static void ibus_text_destroy (IBusText *text);
26 static gboolean ibus_text_serialize (IBusText *text,
27 GVariantBuilder *builder);
28 static int ibus_text_deserialize (IBusText *text,
30 static gboolean ibus_text_copy (IBusText *dest,
33 G_DEFINE_TYPE (IBusText, ibus_text, IBUS_TYPE_SERIALIZABLE)
36 ibus_text_class_init (IBusTextClass *class)
38 IBusObjectClass *object_class = IBUS_OBJECT_CLASS (class);
39 IBusSerializableClass *serializable_class = IBUS_SERIALIZABLE_CLASS (class);
41 ibus_text_parent_class = (IBusSerializableClass *) g_type_class_peek_parent (class);
43 object_class->destroy = (IBusObjectDestroyFunc) ibus_text_destroy;
45 serializable_class->serialize = (IBusSerializableSerializeFunc) ibus_text_serialize;
46 serializable_class->deserialize = (IBusSerializableDeserializeFunc) ibus_text_deserialize;
47 serializable_class->copy = (IBusSerializableCopyFunc) ibus_text_copy;
51 ibus_text_init (IBusText *text)
53 text->is_static = TRUE;
59 ibus_text_destroy (IBusText *text)
61 if (text->text != NULL && text->is_static == FALSE) {
67 g_object_unref (text->attrs);
71 IBUS_OBJECT_CLASS (ibus_text_parent_class)->destroy ((IBusObject *)text);
75 ibus_text_serialize (IBusText *text,
76 GVariantBuilder *builder)
80 retval = IBUS_SERIALIZABLE_CLASS (ibus_text_parent_class)->serialize (
81 (IBusSerializable *)text, builder);
82 g_return_val_if_fail (retval, FALSE);
84 g_variant_builder_add (builder, "s", text->text);
86 if (text->attrs == NULL) {
87 text->attrs = ibus_attr_list_new ();
88 g_object_ref_sink (text->attrs);
90 g_variant_builder_add (builder, "v", ibus_serializable_serialize ((IBusSerializable *)text->attrs));
96 ibus_text_deserialize (IBusText *text,
100 retval = IBUS_SERIALIZABLE_CLASS (ibus_text_parent_class)->deserialize (
101 (IBusSerializable *)text, variant);
103 if (text->is_static == FALSE)
105 g_variant_get_child (variant, retval++, "s", &text->text);
106 text->is_static = FALSE;
109 g_object_unref (text->attrs);
111 GVariant *var = g_variant_get_child_value (variant, retval++);
112 text->attrs = IBUS_ATTR_LIST (ibus_serializable_deserialize (var));
113 g_variant_unref (var);
114 g_object_ref_sink (text->attrs);
120 ibus_text_copy (IBusText *dest,
125 retval = IBUS_SERIALIZABLE_CLASS (ibus_text_parent_class)->copy (
126 (IBusSerializable *)dest,
127 (IBusSerializable *)src);
128 g_return_val_if_fail (retval, FALSE);
130 g_return_val_if_fail (IBUS_IS_TEXT (dest), FALSE);
131 g_return_val_if_fail (IBUS_IS_TEXT (src), FALSE);
133 dest->text = g_strdup (src->text);
134 dest->is_static = FALSE;
136 dest->attrs = (IBusAttrList *)ibus_serializable_copy ((IBusSerializable *)src->attrs);
137 g_object_ref_sink (dest->attrs);
144 ibus_text_new_from_string (const gchar *str)
150 text= g_object_new (IBUS_TYPE_TEXT, NULL);
152 text->is_static = FALSE;
153 text->text = g_strdup (str);
159 ibus_text_new_from_ucs4 (const gunichar *str)
163 gchar *buf = g_ucs4_to_utf8 (str, -1, NULL, NULL, NULL);
164 g_return_val_if_fail (buf != NULL, NULL);
166 IBusText *text= g_object_new (IBUS_TYPE_TEXT, NULL);
168 text->is_static = FALSE;
175 ibus_text_new_from_static_string (const gchar *str)
181 text= g_object_new (IBUS_TYPE_TEXT, NULL);
183 text->is_static = TRUE;
184 text->text = (gchar *)str;
190 ibus_text_new_from_printf (const gchar *format,
199 va_start (args, format);
200 str = g_strdup_vprintf (format, args);
203 g_return_val_if_fail (str != NULL, NULL);
205 text= g_object_new (IBUS_TYPE_TEXT, NULL);
206 text->is_static = FALSE;
207 text->text = (gchar *)str;
213 ibus_text_new_from_unichar (gunichar c)
218 g_return_val_if_fail (g_unichar_validate (c), NULL);
220 text= g_object_new (IBUS_TYPE_TEXT, NULL);
222 text->is_static = FALSE;
223 text->text = (gchar *)g_malloc (12);
224 len = g_unichar_to_utf8 (c, text->text);
231 ibus_text_append_attribute (IBusText *text,
237 g_assert (IBUS_IS_TEXT (text));
242 end_index += g_utf8_strlen(text->text, -1) + 1;
245 if (end_index <= 0) {
249 if (text->attrs == NULL) {
250 text->attrs = ibus_attr_list_new ();
253 attr = ibus_attribute_new (type, value, start_index, end_index);
254 ibus_attr_list_append (text->attrs, attr);
258 ibus_text_get_length (IBusText *text)
260 return g_utf8_strlen (text->text, -1);
264 ibus_text_get_is_static (IBusText *text)
266 return text->is_static;
270 ibus_text_get_text (IBusText *text)
276 ibus_text_get_attributes (IBusText *text)
282 ibus_text_set_attributes (IBusText *text,
286 g_object_unref (text->attrs);
288 g_object_ref_sink (text->attrs);