1 /* GTK - The GTK+ 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_finalize (GObject *object);
48 static void gtk_text_tag_table_set_property (GObject *object,
52 static void gtk_text_tag_table_get_property (GObject *object,
57 static guint signals[LAST_SIGNAL] = { 0 };
59 G_DEFINE_TYPE (GtkTextTagTable, gtk_text_tag_table, G_TYPE_OBJECT)
62 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
64 GObjectClass *object_class = G_OBJECT_CLASS (klass);
66 object_class->set_property = gtk_text_tag_table_set_property;
67 object_class->get_property = gtk_text_tag_table_get_property;
69 object_class->finalize = gtk_text_tag_table_finalize;
71 signals[TAG_CHANGED] =
72 g_signal_new (I_("tag_changed"),
73 G_OBJECT_CLASS_TYPE (object_class),
75 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
77 _gtk_marshal_VOID__OBJECT_BOOLEAN,
84 g_signal_new (I_("tag_added"),
85 G_OBJECT_CLASS_TYPE (object_class),
87 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
89 _gtk_marshal_VOID__OBJECT,
94 signals[TAG_REMOVED] =
95 g_signal_new (I_("tag_removed"),
96 G_OBJECT_CLASS_TYPE (object_class),
98 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
100 _gtk_marshal_VOID__OBJECT,
107 gtk_text_tag_table_init (GtkTextTagTable *table)
109 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
113 * gtk_text_tag_table_new:
115 * Creates a new #GtkTextTagTable. The table contains no tags by
118 * Return value: a new #GtkTextTagTable
121 gtk_text_tag_table_new (void)
123 GtkTextTagTable *table;
125 table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
131 foreach_unref (GtkTextTag *tag, gpointer data)
135 /* We don't want to emit the remove signal here; so we just unparent
139 tmp = tag->table->buffers;
142 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
149 g_object_unref (tag);
153 gtk_text_tag_table_finalize (GObject *object)
155 GtkTextTagTable *table;
157 table = GTK_TEXT_TAG_TABLE (object);
159 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
161 g_hash_table_destroy (table->hash);
162 g_slist_free (table->anonymous);
164 g_slist_free (table->buffers);
166 (* G_OBJECT_CLASS (gtk_text_tag_table_parent_class)->finalize) (object);
169 gtk_text_tag_table_set_property (GObject *object,
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
185 gtk_text_tag_table_get_property (GObject *object,
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 * gtk_text_tag_table_add:
201 * @table: a #GtkTextTagTable
202 * @tag: a #GtkTextTag
204 * Add a tag to the table. The tag is assigned the highest priority
207 * @tag must not be in a tag table already, and may not have
208 * the same name as an already-added tag.
211 gtk_text_tag_table_add (GtkTextTagTable *table,
216 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
217 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
218 g_return_if_fail (tag->table == NULL);
220 if (tag->name && g_hash_table_lookup (table->hash, tag->name))
222 g_warning ("A tag named '%s' is already in the tag table.",
230 g_hash_table_insert (table->hash, tag->name, tag);
233 table->anonymous = g_slist_prepend (table->anonymous, tag);
234 table->anon_count += 1;
239 /* We get the highest tag priority, as the most-recently-added
240 tag. Note that we do NOT use gtk_text_tag_set_priority,
241 as it assumes the tag is already in the table. */
242 size = gtk_text_tag_table_get_size (table);
244 tag->priority = size - 1;
246 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
250 * gtk_text_tag_table_lookup:
251 * @table: a #GtkTextTagTable
252 * @name: name of a tag
254 * Look up a named tag.
256 * Return value: The tag, or %NULL if none by that name is in the table.
259 gtk_text_tag_table_lookup (GtkTextTagTable *table,
262 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
263 g_return_val_if_fail (name != NULL, NULL);
265 return g_hash_table_lookup (table->hash, name);
269 * gtk_text_tag_table_remove:
270 * @table: a #GtkTextTagTable
271 * @tag: a #GtkTextTag
273 * Remove a tag from the table. This will remove the table's
274 * reference to the tag, so be careful - the tag will end
275 * up destroyed if you don't have a reference to it.
278 gtk_text_tag_table_remove (GtkTextTagTable *table,
283 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
284 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
285 g_return_if_fail (tag->table == table);
287 /* Our little bad hack to be sure buffers don't still have the tag
288 * applied to text in the buffer
290 tmp = table->buffers;
293 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
299 /* Set ourselves to the highest priority; this means
300 when we're removed, there won't be any gaps in the
301 priorities of the tags in the table. */
302 gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
307 g_hash_table_remove (table->hash, tag->name);
310 table->anonymous = g_slist_remove (table->anonymous, tag);
311 table->anon_count -= 1;
314 g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
316 g_object_unref (tag);
321 GtkTextTagTableForeach func;
326 hash_foreach (gpointer key, gpointer value, gpointer data)
328 struct ForeachData *fd = data;
330 g_return_if_fail (GTK_IS_TEXT_TAG (value));
332 (* fd->func) (value, fd->data);
336 list_foreach (gpointer data, gpointer user_data)
338 struct ForeachData *fd = user_data;
340 g_return_if_fail (GTK_IS_TEXT_TAG (data));
342 (* fd->func) (data, fd->data);
346 * gtk_text_tag_table_foreach:
347 * @table: a #GtkTextTagTable
348 * @func: a function to call on each tag
351 * Calls @func on each tag in @table, with user data @data.
352 * Note that the table may not be modified while iterating
353 * over it (you can't add/remove tags).
356 gtk_text_tag_table_foreach (GtkTextTagTable *table,
357 GtkTextTagTableForeach func,
360 struct ForeachData d;
362 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
363 g_return_if_fail (func != NULL);
368 g_hash_table_foreach (table->hash, hash_foreach, &d);
369 g_slist_foreach (table->anonymous, list_foreach, &d);
373 * gtk_text_tag_table_get_size:
374 * @table: a #GtkTextTagTable
376 * Returns the size of the table (number of tags)
378 * Return value: number of tags in @table
381 gtk_text_tag_table_get_size (GtkTextTagTable *table)
383 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
385 return g_hash_table_size (table->hash) + table->anon_count;
389 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
392 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
394 table->buffers = g_slist_prepend (table->buffers, buffer);
398 foreach_remove_tag (GtkTextTag *tag, gpointer data)
400 GtkTextBuffer *buffer;
402 buffer = GTK_TEXT_BUFFER (data);
404 _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
408 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
411 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
413 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
415 table->buffers = g_slist_remove (table->buffers, buffer);
418 #define __GTK_TEXT_TAG_TABLE_C__
419 #include "gtkaliasdef.c"