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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gtktexttagtable.h"
31 #include "gtkbuildable.h"
32 #include "gtktexttagprivate.h"
33 #include "gtkmarshalers.h"
34 #include "gtktextbuffer.h" /* just for the lame notify_will_remove_tag hack */
41 * SECTION:gtktexttagtable
42 * @Short_description: Collection of tags that can be used together
43 * @Title: GtkTextTagTable
45 * You may wish to begin by reading the <link linkend="TextWidget">text widget
46 * conceptual overview</link> which gives an overview of all the objects and
47 * data types related to the text widget and how they work together.
49 * <refsect2 id="GtkTextTagTable-BUILDER-UI">
50 * <title>GtkTextTagTables as GtkBuildable</title>
52 * The GtkTextTagTable implementation of the GtkBuildable interface
53 * supports adding tags by specifying "tag" as the "type"
54 * attribute of a <child> element.
57 * <title>A UI definition fragment specifying tags</title>
58 * <programlisting><![CDATA[
59 * <object class="GtkTextTagTable">
61 * <object class="GtkTextTag"/>
64 * ]]></programlisting>
70 struct _GtkTextTagTablePrivate
91 static void gtk_text_tag_table_finalize (GObject *object);
92 static void gtk_text_tag_table_set_property (GObject *object,
96 static void gtk_text_tag_table_get_property (GObject *object,
101 static void gtk_text_tag_table_buildable_interface_init (GtkBuildableIface *iface);
102 static void gtk_text_tag_table_buildable_add_child (GtkBuildable *buildable,
107 static guint signals[LAST_SIGNAL] = { 0 };
109 G_DEFINE_TYPE_WITH_CODE (GtkTextTagTable, gtk_text_tag_table, G_TYPE_OBJECT,
110 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
111 gtk_text_tag_table_buildable_interface_init))
114 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
116 GObjectClass *object_class = G_OBJECT_CLASS (klass);
118 object_class->set_property = gtk_text_tag_table_set_property;
119 object_class->get_property = gtk_text_tag_table_get_property;
121 object_class->finalize = gtk_text_tag_table_finalize;
124 * GtkTextTagTable::tag-changed:
125 * @texttagtable: the object which received the signal.
126 * @tag: the changed tag.
127 * @size_changed: whether the size has been changed.
129 signals[TAG_CHANGED] =
130 g_signal_new (I_("tag-changed"),
131 G_OBJECT_CLASS_TYPE (object_class),
133 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
135 _gtk_marshal_VOID__OBJECT_BOOLEAN,
142 * GtkTextTagTable::tag-added:
143 * @texttagtable: the object which received the signal.
144 * @tag: the added tag.
147 g_signal_new (I_("tag-added"),
148 G_OBJECT_CLASS_TYPE (object_class),
150 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
152 _gtk_marshal_VOID__OBJECT,
158 * GtkTextTagTable::tag-removed:
159 * @texttagtable: the object which received the signal.
160 * @tag: the removed tag.
162 signals[TAG_REMOVED] =
163 g_signal_new (I_("tag-removed"),
164 G_OBJECT_CLASS_TYPE (object_class),
166 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
168 _gtk_marshal_VOID__OBJECT,
173 g_type_class_add_private (klass, sizeof (GtkTextTagTablePrivate));
177 gtk_text_tag_table_init (GtkTextTagTable *table)
179 GtkTextTagTablePrivate *priv;
181 table->priv = G_TYPE_INSTANCE_GET_PRIVATE (table,
182 GTK_TYPE_TEXT_TAG_TABLE,
183 GtkTextTagTablePrivate);
186 priv->hash = g_hash_table_new (g_str_hash, g_str_equal);
190 * gtk_text_tag_table_new:
192 * Creates a new #GtkTextTagTable. The table contains no tags by
195 * Return value: a new #GtkTextTagTable
198 gtk_text_tag_table_new (void)
200 GtkTextTagTable *table;
202 table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
208 foreach_unref (GtkTextTag *tag, gpointer data)
210 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (tag->priv->table);
211 GtkTextTagTablePrivate *priv = table->priv;
214 /* We don't want to emit the remove signal here; so we just unparent
221 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
227 tag->priv->table = NULL;
228 g_object_unref (tag);
232 gtk_text_tag_table_finalize (GObject *object)
234 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (object);
235 GtkTextTagTablePrivate *priv = table->priv;
237 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
239 g_hash_table_destroy (priv->hash);
240 g_slist_free (priv->anonymous);
242 g_slist_free (priv->buffers);
244 G_OBJECT_CLASS (gtk_text_tag_table_parent_class)->finalize (object);
247 gtk_text_tag_table_set_property (GObject *object,
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
263 gtk_text_tag_table_get_property (GObject *object,
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gtk_text_tag_table_buildable_interface_init (GtkBuildableIface *iface)
280 iface->add_child = gtk_text_tag_table_buildable_add_child;
284 gtk_text_tag_table_buildable_add_child (GtkBuildable *buildable,
289 if (type && strcmp (type, "tag") == 0)
290 gtk_text_tag_table_add (GTK_TEXT_TAG_TABLE (buildable),
291 GTK_TEXT_TAG (child));
295 * gtk_text_tag_table_add:
296 * @table: a #GtkTextTagTable
297 * @tag: a #GtkTextTag
299 * Add a tag to the table. The tag is assigned the highest priority
302 * @tag must not be in a tag table already, and may not have
303 * the same name as an already-added tag.
306 gtk_text_tag_table_add (GtkTextTagTable *table,
309 GtkTextTagTablePrivate *priv;
312 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
313 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
314 g_return_if_fail (tag->priv->table == NULL);
318 if (tag->priv->name && g_hash_table_lookup (priv->hash, tag->priv->name))
320 g_warning ("A tag named '%s' is already in the tag table.",
328 g_hash_table_insert (priv->hash, tag->priv->name, tag);
331 priv->anonymous = g_slist_prepend (priv->anonymous, tag);
332 priv->anon_count += 1;
335 tag->priv->table = table;
337 /* We get the highest tag priority, as the most-recently-added
338 tag. Note that we do NOT use gtk_text_tag_set_priority,
339 as it assumes the tag is already in the table. */
340 size = gtk_text_tag_table_get_size (table);
342 tag->priv->priority = size - 1;
344 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
348 * gtk_text_tag_table_lookup:
349 * @table: a #GtkTextTagTable
350 * @name: name of a tag
352 * Look up a named tag.
354 * Return value: (transfer none): The tag, or %NULL if none by that name is in the table.
357 gtk_text_tag_table_lookup (GtkTextTagTable *table,
360 GtkTextTagTablePrivate *priv;
362 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
363 g_return_val_if_fail (name != NULL, NULL);
367 return g_hash_table_lookup (priv->hash, name);
371 * gtk_text_tag_table_remove:
372 * @table: a #GtkTextTagTable
373 * @tag: a #GtkTextTag
375 * Remove a tag from the table. This will remove the table's
376 * reference to the tag, so be careful - the tag will end
377 * up destroyed if you don't have a reference to it.
380 gtk_text_tag_table_remove (GtkTextTagTable *table,
383 GtkTextTagTablePrivate *priv;
386 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
387 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
388 g_return_if_fail (tag->priv->table == table);
392 /* Our little bad hack to be sure buffers don't still have the tag
393 * applied to text in the buffer
398 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
404 /* Set ourselves to the highest priority; this means
405 when we're removed, there won't be any gaps in the
406 priorities of the tags in the table. */
407 gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
409 tag->priv->table = NULL;
412 g_hash_table_remove (priv->hash, tag->priv->name);
415 priv->anonymous = g_slist_remove (priv->anonymous, tag);
416 priv->anon_count -= 1;
419 g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
421 g_object_unref (tag);
426 GtkTextTagTableForeach func;
431 hash_foreach (gpointer key, gpointer value, gpointer data)
433 struct ForeachData *fd = data;
435 g_return_if_fail (GTK_IS_TEXT_TAG (value));
437 (* fd->func) (value, fd->data);
441 list_foreach (gpointer data, gpointer user_data)
443 struct ForeachData *fd = user_data;
445 g_return_if_fail (GTK_IS_TEXT_TAG (data));
447 (* fd->func) (data, fd->data);
451 * gtk_text_tag_table_foreach:
452 * @table: a #GtkTextTagTable
453 * @func: (scope call): a function to call on each tag
456 * Calls @func on each tag in @table, with user data @data.
457 * Note that the table may not be modified while iterating
458 * over it (you can't add/remove tags).
461 gtk_text_tag_table_foreach (GtkTextTagTable *table,
462 GtkTextTagTableForeach func,
465 GtkTextTagTablePrivate *priv;
466 struct ForeachData d;
468 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
469 g_return_if_fail (func != NULL);
476 g_hash_table_foreach (priv->hash, hash_foreach, &d);
477 g_slist_foreach (priv->anonymous, list_foreach, &d);
481 * gtk_text_tag_table_get_size:
482 * @table: a #GtkTextTagTable
484 * Returns the size of the table (number of tags)
486 * Return value: number of tags in @table
489 gtk_text_tag_table_get_size (GtkTextTagTable *table)
491 GtkTextTagTablePrivate *priv;
493 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
497 return g_hash_table_size (priv->hash) + priv->anon_count;
501 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
504 GtkTextTagTablePrivate *priv = table->priv;
506 priv->buffers = g_slist_prepend (priv->buffers, buffer);
510 foreach_remove_tag (GtkTextTag *tag, gpointer data)
512 GtkTextBuffer *buffer;
514 buffer = GTK_TEXT_BUFFER (data);
516 _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
520 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
523 GtkTextTagTablePrivate *priv = table->priv;
525 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
527 priv->buffers = g_slist_remove (priv->buffers, buffer);