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,
204 GtkTextTagTable *table;
206 table = GTK_TEXT_TAG_TABLE (object);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 gtk_text_tag_table_get_property (GObject *object,
224 GtkTextTagTable *table;
226 table = GTK_TEXT_TAG_TABLE (object);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 * gtk_text_tag_table_add:
239 * @table: a #GtkTextTagTable
240 * @tag: a #GtkTextTag
242 * Add a tag to the table. The tag is assigned the highest priority
245 * @tag must not be in a tag table already, and may not have
246 * the same name as an already-added tag.
249 gtk_text_tag_table_add (GtkTextTagTable *table,
254 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
255 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
256 g_return_if_fail (tag->table == NULL);
258 if (tag->name && g_hash_table_lookup (table->hash, tag->name))
260 g_warning ("A tag named '%s' is already in the tag table.",
268 g_hash_table_insert (table->hash, tag->name, tag);
271 table->anonymous = g_slist_prepend (table->anonymous, tag);
272 table->anon_count += 1;
277 /* We get the highest tag priority, as the most-recently-added
278 tag. Note that we do NOT use gtk_text_tag_set_priority,
279 as it assumes the tag is already in the table. */
280 size = gtk_text_tag_table_get_size (table);
282 tag->priority = size - 1;
284 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
288 * gtk_text_tag_table_lookup:
289 * @table: a #GtkTextTagTable
290 * @name: name of a tag
292 * Look up a named tag.
294 * Return value: The tag, or %NULL if none by that name is in the table.
297 gtk_text_tag_table_lookup (GtkTextTagTable *table,
300 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
301 g_return_val_if_fail (name != NULL, NULL);
303 return g_hash_table_lookup (table->hash, name);
307 * gtk_text_tag_table_remove:
308 * @table: a #GtkTextTagTable
309 * @tag: a #GtkTextTag
311 * Remove a tag from the table. This will remove the table's
312 * reference to the tag, so be careful - the tag will end
313 * up destroyed if you don't have a reference to it.
316 gtk_text_tag_table_remove (GtkTextTagTable *table,
321 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
322 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
323 g_return_if_fail (tag->table == table);
325 /* Our little bad hack to be sure buffers don't still have the tag
326 * applied to text in the buffer
328 tmp = table->buffers;
331 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
337 /* Set ourselves to the highest priority; this means
338 when we're removed, there won't be any gaps in the
339 priorities of the tags in the table. */
340 gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
345 g_hash_table_remove (table->hash, tag->name);
348 table->anonymous = g_slist_remove (table->anonymous, tag);
349 table->anon_count -= 1;
352 g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
354 g_object_unref (tag);
359 GtkTextTagTableForeach func;
364 hash_foreach (gpointer key, gpointer value, gpointer data)
366 struct ForeachData *fd = data;
368 g_return_if_fail (GTK_IS_TEXT_TAG (value));
370 (* fd->func) (value, fd->data);
374 list_foreach (gpointer data, gpointer user_data)
376 struct ForeachData *fd = user_data;
378 g_return_if_fail (GTK_IS_TEXT_TAG (data));
380 (* fd->func) (data, fd->data);
384 * gtk_text_tag_table_foreach:
385 * @table: a #GtkTextTagTable
386 * @func: a function to call on each tag
389 * Calls @func on each tag in @table, with user data @data.
393 gtk_text_tag_table_foreach (GtkTextTagTable *table,
394 GtkTextTagTableForeach func,
397 struct ForeachData d;
399 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
400 g_return_if_fail (func != NULL);
405 g_hash_table_foreach (table->hash, hash_foreach, &d);
406 g_slist_foreach (table->anonymous, list_foreach, &d);
410 * gtk_text_tag_table_get_size:
411 * @table: a #GtkTextTagTable
413 * Returns the size of the table (number of tags)
415 * Return value: number of tags in @table
418 gtk_text_tag_table_get_size (GtkTextTagTable *table)
420 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
422 return g_hash_table_size (table->hash) + table->anon_count;
426 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
429 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
431 table->buffers = g_slist_prepend (table->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 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
450 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
452 table->buffers = g_slist_remove (table->buffers, buffer);
455 #define __GTK_TEXT_TAG_TABLE_C__
456 #include "gtkaliasdef.c"