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/.
28 #include "gtktexttagtable.h"
29 #include "gtkmarshalers.h"
30 #include "gtktextbuffer.h" /* just for the lame notify_will_remove_tag hack */
37 * SECTION:gtktexttagtable
38 * @Short_description: Collection of tags that can be used together
39 * @Title: GtkTextTagTable
41 * You may wish to begin by reading the <link linkend="TextWidget">text widget
42 * conceptual overview</link> which gives an overview of all the objects and data
43 * types related to the text widget and how they work together.
47 struct _GtkTextTagTablePrivate
68 static void gtk_text_tag_table_finalize (GObject *object);
69 static void gtk_text_tag_table_set_property (GObject *object,
73 static void gtk_text_tag_table_get_property (GObject *object,
78 static guint signals[LAST_SIGNAL] = { 0 };
80 G_DEFINE_TYPE (GtkTextTagTable, gtk_text_tag_table, G_TYPE_OBJECT)
83 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
85 GObjectClass *object_class = G_OBJECT_CLASS (klass);
87 object_class->set_property = gtk_text_tag_table_set_property;
88 object_class->get_property = gtk_text_tag_table_get_property;
90 object_class->finalize = gtk_text_tag_table_finalize;
93 * GtkTextTagTable::tag-changed:
94 * @texttagtable: the object which received the signal.
95 * @tag: the changed tag.
96 * @size_changed: whether the size has been changed.
98 signals[TAG_CHANGED] =
99 g_signal_new (I_("tag-changed"),
100 G_OBJECT_CLASS_TYPE (object_class),
102 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
104 _gtk_marshal_VOID__OBJECT_BOOLEAN,
111 * GtkTextTagTable::tag-added:
112 * @texttagtable: the object which received the signal.
113 * @tag: the added tag.
116 g_signal_new (I_("tag-added"),
117 G_OBJECT_CLASS_TYPE (object_class),
119 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
121 _gtk_marshal_VOID__OBJECT,
127 * GtkTextTagTable::tag-removed:
128 * @texttagtable: the object which received the signal.
129 * @tag: the removed tag.
131 signals[TAG_REMOVED] =
132 g_signal_new (I_("tag-removed"),
133 G_OBJECT_CLASS_TYPE (object_class),
135 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
137 _gtk_marshal_VOID__OBJECT,
142 g_type_class_add_private (klass, sizeof (GtkTextTagTablePrivate));
146 gtk_text_tag_table_init (GtkTextTagTable *table)
148 GtkTextTagTablePrivate *priv;
150 table->priv = G_TYPE_INSTANCE_GET_PRIVATE (table,
151 GTK_TYPE_TEXT_TAG_TABLE,
152 GtkTextTagTablePrivate);
155 priv->hash = g_hash_table_new (g_str_hash, g_str_equal);
159 * gtk_text_tag_table_new:
161 * Creates a new #GtkTextTagTable. The table contains no tags by
164 * Return value: a new #GtkTextTagTable
167 gtk_text_tag_table_new (void)
169 GtkTextTagTable *table;
171 table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
177 foreach_unref (GtkTextTag *tag, gpointer data)
179 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (tag->table);
180 GtkTextTagTablePrivate *priv = table->priv;
183 /* We don't want to emit the remove signal here; so we just unparent
190 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
197 g_object_unref (tag);
201 gtk_text_tag_table_finalize (GObject *object)
203 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (object);
204 GtkTextTagTablePrivate *priv = table->priv;
206 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
208 g_hash_table_destroy (priv->hash);
209 g_slist_free (priv->anonymous);
211 g_slist_free (priv->buffers);
213 G_OBJECT_CLASS (gtk_text_tag_table_parent_class)->finalize (object);
216 gtk_text_tag_table_set_property (GObject *object,
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 gtk_text_tag_table_get_property (GObject *object,
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 * gtk_text_tag_table_add:
248 * @table: a #GtkTextTagTable
249 * @tag: a #GtkTextTag
251 * Add a tag to the table. The tag is assigned the highest priority
254 * @tag must not be in a tag table already, and may not have
255 * the same name as an already-added tag.
258 gtk_text_tag_table_add (GtkTextTagTable *table,
261 GtkTextTagTablePrivate *priv;
264 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
265 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
266 g_return_if_fail (tag->table == NULL);
270 if (tag->name && g_hash_table_lookup (priv->hash, tag->name))
272 g_warning ("A tag named '%s' is already in the tag table.",
280 g_hash_table_insert (priv->hash, tag->name, tag);
283 priv->anonymous = g_slist_prepend (priv->anonymous, tag);
284 priv->anon_count += 1;
289 /* We get the highest tag priority, as the most-recently-added
290 tag. Note that we do NOT use gtk_text_tag_set_priority,
291 as it assumes the tag is already in the table. */
292 size = gtk_text_tag_table_get_size (table);
294 tag->priority = size - 1;
296 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
300 * gtk_text_tag_table_lookup:
301 * @table: a #GtkTextTagTable
302 * @name: name of a tag
304 * Look up a named tag.
306 * Return value: (transfer none): The tag, or %NULL if none by that name is in the table.
309 gtk_text_tag_table_lookup (GtkTextTagTable *table,
312 GtkTextTagTablePrivate *priv;
314 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
315 g_return_val_if_fail (name != NULL, NULL);
319 return g_hash_table_lookup (priv->hash, name);
323 * gtk_text_tag_table_remove:
324 * @table: a #GtkTextTagTable
325 * @tag: a #GtkTextTag
327 * Remove a tag from the table. This will remove the table's
328 * reference to the tag, so be careful - the tag will end
329 * up destroyed if you don't have a reference to it.
332 gtk_text_tag_table_remove (GtkTextTagTable *table,
335 GtkTextTagTablePrivate *priv;
338 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
339 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
340 g_return_if_fail (tag->table == table);
344 /* Our little bad hack to be sure buffers don't still have the tag
345 * applied to text in the buffer
350 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
356 /* Set ourselves to the highest priority; this means
357 when we're removed, there won't be any gaps in the
358 priorities of the tags in the table. */
359 gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
364 g_hash_table_remove (priv->hash, tag->name);
367 priv->anonymous = g_slist_remove (priv->anonymous, tag);
368 priv->anon_count -= 1;
371 g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
373 g_object_unref (tag);
378 GtkTextTagTableForeach func;
383 hash_foreach (gpointer key, gpointer value, gpointer data)
385 struct ForeachData *fd = data;
387 g_return_if_fail (GTK_IS_TEXT_TAG (value));
389 (* fd->func) (value, fd->data);
393 list_foreach (gpointer data, gpointer user_data)
395 struct ForeachData *fd = user_data;
397 g_return_if_fail (GTK_IS_TEXT_TAG (data));
399 (* fd->func) (data, fd->data);
403 * gtk_text_tag_table_foreach:
404 * @table: a #GtkTextTagTable
405 * @func: (scope call): a function to call on each tag
408 * Calls @func on each tag in @table, with user data @data.
409 * Note that the table may not be modified while iterating
410 * over it (you can't add/remove tags).
413 gtk_text_tag_table_foreach (GtkTextTagTable *table,
414 GtkTextTagTableForeach func,
417 GtkTextTagTablePrivate *priv;
418 struct ForeachData d;
420 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
421 g_return_if_fail (func != NULL);
428 g_hash_table_foreach (priv->hash, hash_foreach, &d);
429 g_slist_foreach (priv->anonymous, list_foreach, &d);
433 * gtk_text_tag_table_get_size:
434 * @table: a #GtkTextTagTable
436 * Returns the size of the table (number of tags)
438 * Return value: number of tags in @table
441 gtk_text_tag_table_get_size (GtkTextTagTable *table)
443 GtkTextTagTablePrivate *priv;
445 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
449 return g_hash_table_size (priv->hash) + priv->anon_count;
453 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
456 GtkTextTagTablePrivate *priv = table->priv;
458 priv->buffers = g_slist_prepend (priv->buffers, buffer);
462 foreach_remove_tag (GtkTextTag *tag, gpointer data)
464 GtkTextBuffer *buffer;
466 buffer = GTK_TEXT_BUFFER (data);
468 _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
472 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
475 GtkTextTagTablePrivate *priv = table->priv;
477 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
479 priv->buffers = g_slist_remove (priv->buffers, buffer);