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 */
47 static void gtk_text_tag_table_init (GtkTextTagTable *table);
48 static void gtk_text_tag_table_class_init (GtkTextTagTableClass *klass);
49 static void gtk_text_tag_table_finalize (GObject *object);
50 static void gtk_text_tag_table_set_property (GObject *object,
54 static void gtk_text_tag_table_get_property (GObject *object,
59 static GObjectClass *parent_class = NULL;
60 static guint signals[LAST_SIGNAL] = { 0 };
63 gtk_text_tag_table_get_type (void)
65 static GType our_type = 0;
69 static const GTypeInfo our_info =
71 sizeof (GtkTextTagTableClass),
73 (GBaseFinalizeFunc) NULL,
74 (GClassInitFunc) gtk_text_tag_table_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
77 sizeof (GtkTextTagTable),
79 (GInstanceInitFunc) gtk_text_tag_table_init
82 our_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkTextTagTable"),
90 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
92 GObjectClass *object_class = G_OBJECT_CLASS (klass);
94 parent_class = g_type_class_peek_parent (klass);
96 object_class->set_property = gtk_text_tag_table_set_property;
97 object_class->get_property = gtk_text_tag_table_get_property;
99 object_class->finalize = gtk_text_tag_table_finalize;
101 signals[TAG_CHANGED] =
102 g_signal_new (I_("tag_changed"),
103 G_OBJECT_CLASS_TYPE (object_class),
105 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
107 _gtk_marshal_VOID__OBJECT_BOOLEAN,
114 g_signal_new (I_("tag_added"),
115 G_OBJECT_CLASS_TYPE (object_class),
117 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
119 _gtk_marshal_VOID__OBJECT,
124 signals[TAG_REMOVED] =
125 g_signal_new (I_("tag_removed"),
126 G_OBJECT_CLASS_TYPE (object_class),
128 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
130 _gtk_marshal_VOID__OBJECT,
137 gtk_text_tag_table_init (GtkTextTagTable *table)
139 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
143 * gtk_text_tag_table_new:
145 * Creates a new #GtkTextTagTable. The table contains no tags by
148 * Return value: a new #GtkTextTagTable
151 gtk_text_tag_table_new (void)
153 GtkTextTagTable *table;
155 table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
161 foreach_unref (GtkTextTag *tag, gpointer data)
165 /* We don't want to emit the remove signal here; so we just unparent
169 tmp = tag->table->buffers;
172 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
179 g_object_unref (tag);
183 gtk_text_tag_table_finalize (GObject *object)
185 GtkTextTagTable *table;
187 table = GTK_TEXT_TAG_TABLE (object);
189 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
191 g_hash_table_destroy (table->hash);
192 g_slist_free (table->anonymous);
194 g_slist_free (table->buffers);
196 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
199 gtk_text_tag_table_set_property (GObject *object,
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gtk_text_tag_table_get_property (GObject *object,
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 * gtk_text_tag_table_add:
231 * @table: a #GtkTextTagTable
232 * @tag: a #GtkTextTag
234 * Add a tag to the table. The tag is assigned the highest priority
237 * @tag must not be in a tag table already, and may not have
238 * the same name as an already-added tag.
241 gtk_text_tag_table_add (GtkTextTagTable *table,
246 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
247 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
248 g_return_if_fail (tag->table == NULL);
250 if (tag->name && g_hash_table_lookup (table->hash, tag->name))
252 g_warning ("A tag named '%s' is already in the tag table.",
260 g_hash_table_insert (table->hash, tag->name, tag);
263 table->anonymous = g_slist_prepend (table->anonymous, tag);
264 table->anon_count += 1;
269 /* We get the highest tag priority, as the most-recently-added
270 tag. Note that we do NOT use gtk_text_tag_set_priority,
271 as it assumes the tag is already in the table. */
272 size = gtk_text_tag_table_get_size (table);
274 tag->priority = size - 1;
276 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
280 * gtk_text_tag_table_lookup:
281 * @table: a #GtkTextTagTable
282 * @name: name of a tag
284 * Look up a named tag.
286 * Return value: The tag, or %NULL if none by that name is in the table.
289 gtk_text_tag_table_lookup (GtkTextTagTable *table,
292 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
293 g_return_val_if_fail (name != NULL, NULL);
295 return g_hash_table_lookup (table->hash, name);
299 * gtk_text_tag_table_remove:
300 * @table: a #GtkTextTagTable
301 * @tag: a #GtkTextTag
303 * Remove a tag from the table. This will remove the table's
304 * reference to the tag, so be careful - the tag will end
305 * up destroyed if you don't have a reference to it.
308 gtk_text_tag_table_remove (GtkTextTagTable *table,
313 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
314 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
315 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
320 tmp = table->buffers;
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 (table->hash, tag->name);
340 table->anonymous = g_slist_remove (table->anonymous, tag);
341 table->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.
385 gtk_text_tag_table_foreach (GtkTextTagTable *table,
386 GtkTextTagTableForeach func,
389 struct ForeachData d;
391 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
392 g_return_if_fail (func != NULL);
397 g_hash_table_foreach (table->hash, hash_foreach, &d);
398 g_slist_foreach (table->anonymous, list_foreach, &d);
402 * gtk_text_tag_table_get_size:
403 * @table: a #GtkTextTagTable
405 * Returns the size of the table (number of tags)
407 * Return value: number of tags in @table
410 gtk_text_tag_table_get_size (GtkTextTagTable *table)
412 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
414 return g_hash_table_size (table->hash) + table->anon_count;
418 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
421 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
423 table->buffers = g_slist_prepend (table->buffers, buffer);
427 foreach_remove_tag (GtkTextTag *tag, gpointer data)
429 GtkTextBuffer *buffer;
431 buffer = GTK_TEXT_BUFFER (data);
433 _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
437 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
440 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
442 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
444 table->buffers = g_slist_remove (table->buffers, buffer);
447 #define __GTK_TEXT_TAG_TABLE_C__
448 #include "gtkaliasdef.c"