1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gtktexttagtable.h"
29 #include "gtkbuildable.h"
30 #include "gtktexttagprivate.h"
31 #include "gtkmarshalers.h"
32 #include "gtktextbuffer.h" /* just for the lame notify_will_remove_tag hack */
39 * SECTION:gtktexttagtable
40 * @Short_description: Collection of tags that can be used together
41 * @Title: GtkTextTagTable
43 * You may wish to begin by reading the <link linkend="TextWidget">text widget
44 * conceptual overview</link> which gives an overview of all the objects and
45 * data types related to the text widget and how they work together.
47 * <refsect2 id="GtkTextTagTable-BUILDER-UI">
48 * <title>GtkTextTagTables as GtkBuildable</title>
50 * The GtkTextTagTable implementation of the GtkBuildable interface
51 * supports adding tags by specifying "tag" as the "type"
52 * attribute of a <child> element.
55 * <title>A UI definition fragment specifying tags</title>
56 * <programlisting><![CDATA[
57 * <object class="GtkTextTagTable">
59 * <object class="GtkTextTag"/>
62 * ]]></programlisting>
68 struct _GtkTextTagTablePrivate
89 static void gtk_text_tag_table_finalize (GObject *object);
90 static void gtk_text_tag_table_set_property (GObject *object,
94 static void gtk_text_tag_table_get_property (GObject *object,
99 static void gtk_text_tag_table_buildable_interface_init (GtkBuildableIface *iface);
100 static void gtk_text_tag_table_buildable_add_child (GtkBuildable *buildable,
105 static guint signals[LAST_SIGNAL] = { 0 };
107 G_DEFINE_TYPE_WITH_CODE (GtkTextTagTable, gtk_text_tag_table, G_TYPE_OBJECT,
108 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
109 gtk_text_tag_table_buildable_interface_init))
112 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
114 GObjectClass *object_class = G_OBJECT_CLASS (klass);
116 object_class->set_property = gtk_text_tag_table_set_property;
117 object_class->get_property = gtk_text_tag_table_get_property;
119 object_class->finalize = gtk_text_tag_table_finalize;
122 * GtkTextTagTable::tag-changed:
123 * @texttagtable: the object which received the signal.
124 * @tag: the changed tag.
125 * @size_changed: whether the size has been changed.
127 signals[TAG_CHANGED] =
128 g_signal_new (I_("tag-changed"),
129 G_OBJECT_CLASS_TYPE (object_class),
131 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
133 _gtk_marshal_VOID__OBJECT_BOOLEAN,
140 * GtkTextTagTable::tag-added:
141 * @texttagtable: the object which received the signal.
142 * @tag: the added tag.
145 g_signal_new (I_("tag-added"),
146 G_OBJECT_CLASS_TYPE (object_class),
148 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
150 _gtk_marshal_VOID__OBJECT,
156 * GtkTextTagTable::tag-removed:
157 * @texttagtable: the object which received the signal.
158 * @tag: the removed tag.
160 signals[TAG_REMOVED] =
161 g_signal_new (I_("tag-removed"),
162 G_OBJECT_CLASS_TYPE (object_class),
164 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
166 _gtk_marshal_VOID__OBJECT,
171 g_type_class_add_private (klass, sizeof (GtkTextTagTablePrivate));
175 gtk_text_tag_table_init (GtkTextTagTable *table)
177 GtkTextTagTablePrivate *priv;
179 table->priv = G_TYPE_INSTANCE_GET_PRIVATE (table,
180 GTK_TYPE_TEXT_TAG_TABLE,
181 GtkTextTagTablePrivate);
184 priv->hash = g_hash_table_new (g_str_hash, g_str_equal);
188 * gtk_text_tag_table_new:
190 * Creates a new #GtkTextTagTable. The table contains no tags by
193 * Return value: a new #GtkTextTagTable
196 gtk_text_tag_table_new (void)
198 GtkTextTagTable *table;
200 table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
206 foreach_unref (GtkTextTag *tag, gpointer data)
208 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (tag->priv->table);
209 GtkTextTagTablePrivate *priv = table->priv;
212 /* We don't want to emit the remove signal here; so we just unparent
219 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
225 tag->priv->table = NULL;
226 g_object_unref (tag);
230 gtk_text_tag_table_finalize (GObject *object)
232 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (object);
233 GtkTextTagTablePrivate *priv = table->priv;
235 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
237 g_hash_table_destroy (priv->hash);
238 g_slist_free (priv->anonymous);
240 g_slist_free (priv->buffers);
242 G_OBJECT_CLASS (gtk_text_tag_table_parent_class)->finalize (object);
245 gtk_text_tag_table_set_property (GObject *object,
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 gtk_text_tag_table_get_property (GObject *object,
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 gtk_text_tag_table_buildable_interface_init (GtkBuildableIface *iface)
278 iface->add_child = gtk_text_tag_table_buildable_add_child;
282 gtk_text_tag_table_buildable_add_child (GtkBuildable *buildable,
287 if (type && strcmp (type, "tag") == 0)
288 gtk_text_tag_table_add (GTK_TEXT_TAG_TABLE (buildable),
289 GTK_TEXT_TAG (child));
293 * gtk_text_tag_table_add:
294 * @table: a #GtkTextTagTable
295 * @tag: a #GtkTextTag
297 * Add a tag to the table. The tag is assigned the highest priority
300 * @tag must not be in a tag table already, and may not have
301 * the same name as an already-added tag.
304 gtk_text_tag_table_add (GtkTextTagTable *table,
307 GtkTextTagTablePrivate *priv;
310 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
311 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
312 g_return_if_fail (tag->priv->table == NULL);
316 if (tag->priv->name && g_hash_table_lookup (priv->hash, tag->priv->name))
318 g_warning ("A tag named '%s' is already in the tag table.",
326 g_hash_table_insert (priv->hash, tag->priv->name, tag);
329 priv->anonymous = g_slist_prepend (priv->anonymous, tag);
330 priv->anon_count += 1;
333 tag->priv->table = table;
335 /* We get the highest tag priority, as the most-recently-added
336 tag. Note that we do NOT use gtk_text_tag_set_priority,
337 as it assumes the tag is already in the table. */
338 size = gtk_text_tag_table_get_size (table);
340 tag->priv->priority = size - 1;
342 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
346 * gtk_text_tag_table_lookup:
347 * @table: a #GtkTextTagTable
348 * @name: name of a tag
350 * Look up a named tag.
352 * Return value: (transfer none): The tag, or %NULL if none by that name is in the table.
355 gtk_text_tag_table_lookup (GtkTextTagTable *table,
358 GtkTextTagTablePrivate *priv;
360 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
361 g_return_val_if_fail (name != NULL, NULL);
365 return g_hash_table_lookup (priv->hash, name);
369 * gtk_text_tag_table_remove:
370 * @table: a #GtkTextTagTable
371 * @tag: a #GtkTextTag
373 * Remove a tag from the table. This will remove the table's
374 * reference to the tag, so be careful - the tag will end
375 * up destroyed if you don't have a reference to it.
378 gtk_text_tag_table_remove (GtkTextTagTable *table,
381 GtkTextTagTablePrivate *priv;
384 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
385 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
386 g_return_if_fail (tag->priv->table == table);
390 /* Our little bad hack to be sure buffers don't still have the tag
391 * applied to text in the buffer
396 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
402 /* Set ourselves to the highest priority; this means
403 when we're removed, there won't be any gaps in the
404 priorities of the tags in the table. */
405 gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
407 tag->priv->table = NULL;
410 g_hash_table_remove (priv->hash, tag->priv->name);
413 priv->anonymous = g_slist_remove (priv->anonymous, tag);
414 priv->anon_count -= 1;
417 g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
419 g_object_unref (tag);
424 GtkTextTagTableForeach func;
429 hash_foreach (gpointer key, gpointer value, gpointer data)
431 struct ForeachData *fd = data;
433 g_return_if_fail (GTK_IS_TEXT_TAG (value));
435 (* fd->func) (value, fd->data);
439 list_foreach (gpointer data, gpointer user_data)
441 struct ForeachData *fd = user_data;
443 g_return_if_fail (GTK_IS_TEXT_TAG (data));
445 (* fd->func) (data, fd->data);
449 * gtk_text_tag_table_foreach:
450 * @table: a #GtkTextTagTable
451 * @func: (scope call): a function to call on each tag
454 * Calls @func on each tag in @table, with user data @data.
455 * Note that the table may not be modified while iterating
456 * over it (you can't add/remove tags).
459 gtk_text_tag_table_foreach (GtkTextTagTable *table,
460 GtkTextTagTableForeach func,
463 GtkTextTagTablePrivate *priv;
464 struct ForeachData d;
466 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
467 g_return_if_fail (func != NULL);
474 g_hash_table_foreach (priv->hash, hash_foreach, &d);
475 g_slist_foreach (priv->anonymous, list_foreach, &d);
479 * gtk_text_tag_table_get_size:
480 * @table: a #GtkTextTagTable
482 * Returns the size of the table (number of tags)
484 * Return value: number of tags in @table
487 gtk_text_tag_table_get_size (GtkTextTagTable *table)
489 GtkTextTagTablePrivate *priv;
491 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
495 return g_hash_table_size (priv->hash) + priv->anon_count;
499 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
502 GtkTextTagTablePrivate *priv = table->priv;
504 priv->buffers = g_slist_prepend (priv->buffers, buffer);
508 foreach_remove_tag (GtkTextTag *tag, gpointer data)
510 GtkTextBuffer *buffer;
512 buffer = GTK_TEXT_BUFFER (data);
514 _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
518 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
521 GtkTextTagTablePrivate *priv = table->priv;
523 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
525 priv->buffers = g_slist_remove (priv->buffers, buffer);