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 */
36 struct _GtkTextTagTablePriv
57 static void gtk_text_tag_table_finalize (GObject *object);
58 static void gtk_text_tag_table_set_property (GObject *object,
62 static void gtk_text_tag_table_get_property (GObject *object,
67 static guint signals[LAST_SIGNAL] = { 0 };
69 G_DEFINE_TYPE (GtkTextTagTable, gtk_text_tag_table, G_TYPE_OBJECT)
72 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
74 GObjectClass *object_class = G_OBJECT_CLASS (klass);
76 object_class->set_property = gtk_text_tag_table_set_property;
77 object_class->get_property = gtk_text_tag_table_get_property;
79 object_class->finalize = gtk_text_tag_table_finalize;
81 signals[TAG_CHANGED] =
82 g_signal_new (I_("tag-changed"),
83 G_OBJECT_CLASS_TYPE (object_class),
85 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
87 _gtk_marshal_VOID__OBJECT_BOOLEAN,
94 g_signal_new (I_("tag-added"),
95 G_OBJECT_CLASS_TYPE (object_class),
97 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
99 _gtk_marshal_VOID__OBJECT,
104 signals[TAG_REMOVED] =
105 g_signal_new (I_("tag-removed"),
106 G_OBJECT_CLASS_TYPE (object_class),
108 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
110 _gtk_marshal_VOID__OBJECT,
115 g_type_class_add_private (klass, sizeof (GtkTextTagTablePriv));
119 gtk_text_tag_table_init (GtkTextTagTable *table)
121 GtkTextTagTablePriv *priv;
123 table->priv = G_TYPE_INSTANCE_GET_PRIVATE (table,
124 GTK_TYPE_TEXT_TAG_TABLE,
125 GtkTextTagTablePriv);
128 priv->hash = g_hash_table_new (g_str_hash, g_str_equal);
132 * gtk_text_tag_table_new:
134 * Creates a new #GtkTextTagTable. The table contains no tags by
137 * Return value: a new #GtkTextTagTable
140 gtk_text_tag_table_new (void)
142 GtkTextTagTable *table;
144 table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
150 foreach_unref (GtkTextTag *tag, gpointer data)
152 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (tag->table);
153 GtkTextTagTablePriv *priv = table->priv;
156 /* We don't want to emit the remove signal here; so we just unparent
163 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
170 g_object_unref (tag);
174 gtk_text_tag_table_finalize (GObject *object)
176 GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (object);
177 GtkTextTagTablePriv *priv = table->priv;
179 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
181 g_hash_table_destroy (priv->hash);
182 g_slist_free (priv->anonymous);
184 g_slist_free (priv->buffers);
186 G_OBJECT_CLASS (gtk_text_tag_table_parent_class)->finalize (object);
189 gtk_text_tag_table_set_property (GObject *object,
198 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
205 gtk_text_tag_table_get_property (GObject *object,
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 * gtk_text_tag_table_add:
221 * @table: a #GtkTextTagTable
222 * @tag: a #GtkTextTag
224 * Add a tag to the table. The tag is assigned the highest priority
227 * @tag must not be in a tag table already, and may not have
228 * the same name as an already-added tag.
231 gtk_text_tag_table_add (GtkTextTagTable *table,
234 GtkTextTagTablePriv *priv;
237 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
238 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
239 g_return_if_fail (tag->table == NULL);
243 if (tag->name && g_hash_table_lookup (priv->hash, tag->name))
245 g_warning ("A tag named '%s' is already in the tag table.",
253 g_hash_table_insert (priv->hash, tag->name, tag);
256 priv->anonymous = g_slist_prepend (priv->anonymous, tag);
257 priv->anon_count += 1;
262 /* We get the highest tag priority, as the most-recently-added
263 tag. Note that we do NOT use gtk_text_tag_set_priority,
264 as it assumes the tag is already in the table. */
265 size = gtk_text_tag_table_get_size (table);
267 tag->priority = size - 1;
269 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
273 * gtk_text_tag_table_lookup:
274 * @table: a #GtkTextTagTable
275 * @name: name of a tag
277 * Look up a named tag.
279 * Return value: (transfer none): The tag, or %NULL if none by that name is in the table.
282 gtk_text_tag_table_lookup (GtkTextTagTable *table,
285 GtkTextTagTablePriv *priv;
287 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
288 g_return_val_if_fail (name != NULL, NULL);
292 return g_hash_table_lookup (priv->hash, name);
296 * gtk_text_tag_table_remove:
297 * @table: a #GtkTextTagTable
298 * @tag: a #GtkTextTag
300 * Remove a tag from the table. This will remove the table's
301 * reference to the tag, so be careful - the tag will end
302 * up destroyed if you don't have a reference to it.
305 gtk_text_tag_table_remove (GtkTextTagTable *table,
308 GtkTextTagTablePriv *priv;
311 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
312 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
313 g_return_if_fail (tag->table == table);
317 /* Our little bad hack to be sure buffers don't still have the tag
318 * applied to text in the buffer
323 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
329 /* Set ourselves to the highest priority; this means
330 when we're removed, there won't be any gaps in the
331 priorities of the tags in the table. */
332 gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
337 g_hash_table_remove (priv->hash, tag->name);
340 priv->anonymous = g_slist_remove (priv->anonymous, tag);
341 priv->anon_count -= 1;
344 g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
346 g_object_unref (tag);
351 GtkTextTagTableForeach func;
356 hash_foreach (gpointer key, gpointer value, gpointer data)
358 struct ForeachData *fd = data;
360 g_return_if_fail (GTK_IS_TEXT_TAG (value));
362 (* fd->func) (value, fd->data);
366 list_foreach (gpointer data, gpointer user_data)
368 struct ForeachData *fd = user_data;
370 g_return_if_fail (GTK_IS_TEXT_TAG (data));
372 (* fd->func) (data, fd->data);
376 * gtk_text_tag_table_foreach:
377 * @table: a #GtkTextTagTable
378 * @func: a function to call on each tag
381 * Calls @func on each tag in @table, with user data @data.
382 * Note that the table may not be modified while iterating
383 * over it (you can't add/remove tags).
386 gtk_text_tag_table_foreach (GtkTextTagTable *table,
387 GtkTextTagTableForeach func,
390 GtkTextTagTablePriv *priv;
391 struct ForeachData d;
393 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
394 g_return_if_fail (func != NULL);
401 g_hash_table_foreach (priv->hash, hash_foreach, &d);
402 g_slist_foreach (priv->anonymous, list_foreach, &d);
406 * gtk_text_tag_table_get_size:
407 * @table: a #GtkTextTagTable
409 * Returns the size of the table (number of tags)
411 * Return value: number of tags in @table
414 gtk_text_tag_table_get_size (GtkTextTagTable *table)
416 GtkTextTagTablePriv *priv;
418 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
422 return g_hash_table_size (priv->hash) + priv->anon_count;
426 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
429 GtkTextTagTablePriv *priv = table->priv;
431 priv->buffers = g_slist_prepend (priv->buffers, buffer);
435 foreach_remove_tag (GtkTextTag *tag, gpointer data)
437 GtkTextBuffer *buffer;
439 buffer = GTK_TEXT_BUFFER (data);
441 _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
445 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
448 GtkTextTagTablePriv *priv = table->priv;
450 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
452 priv->buffers = g_slist_remove (priv->buffers, buffer);