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 */
46 static void gtk_text_tag_table_finalize (GObject *object);
47 static void gtk_text_tag_table_set_property (GObject *object,
51 static void gtk_text_tag_table_get_property (GObject *object,
56 static guint signals[LAST_SIGNAL] = { 0 };
58 G_DEFINE_TYPE (GtkTextTagTable, gtk_text_tag_table, G_TYPE_OBJECT)
61 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
63 GObjectClass *object_class = G_OBJECT_CLASS (klass);
65 object_class->set_property = gtk_text_tag_table_set_property;
66 object_class->get_property = gtk_text_tag_table_get_property;
68 object_class->finalize = gtk_text_tag_table_finalize;
70 signals[TAG_CHANGED] =
71 g_signal_new (I_("tag-changed"),
72 G_OBJECT_CLASS_TYPE (object_class),
74 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
76 _gtk_marshal_VOID__OBJECT_BOOLEAN,
83 g_signal_new (I_("tag-added"),
84 G_OBJECT_CLASS_TYPE (object_class),
86 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
88 _gtk_marshal_VOID__OBJECT,
93 signals[TAG_REMOVED] =
94 g_signal_new (I_("tag-removed"),
95 G_OBJECT_CLASS_TYPE (object_class),
97 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
99 _gtk_marshal_VOID__OBJECT,
106 gtk_text_tag_table_init (GtkTextTagTable *table)
108 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
112 * gtk_text_tag_table_new:
114 * Creates a new #GtkTextTagTable. The table contains no tags by
117 * Return value: a new #GtkTextTagTable
120 gtk_text_tag_table_new (void)
122 GtkTextTagTable *table;
124 table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
130 foreach_unref (GtkTextTag *tag, gpointer data)
134 /* We don't want to emit the remove signal here; so we just unparent
138 tmp = tag->table->buffers;
141 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
148 g_object_unref (tag);
152 gtk_text_tag_table_finalize (GObject *object)
154 GtkTextTagTable *table;
156 table = GTK_TEXT_TAG_TABLE (object);
158 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
160 g_hash_table_destroy (table->hash);
161 g_slist_free (table->anonymous);
163 g_slist_free (table->buffers);
165 G_OBJECT_CLASS (gtk_text_tag_table_parent_class)->finalize (object);
168 gtk_text_tag_table_set_property (GObject *object,
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184 gtk_text_tag_table_get_property (GObject *object,
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 * gtk_text_tag_table_add:
200 * @table: a #GtkTextTagTable
201 * @tag: a #GtkTextTag
203 * Add a tag to the table. The tag is assigned the highest priority
206 * @tag must not be in a tag table already, and may not have
207 * the same name as an already-added tag.
210 gtk_text_tag_table_add (GtkTextTagTable *table,
215 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
216 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
217 g_return_if_fail (tag->table == NULL);
219 if (tag->name && g_hash_table_lookup (table->hash, tag->name))
221 g_warning ("A tag named '%s' is already in the tag table.",
229 g_hash_table_insert (table->hash, tag->name, tag);
232 table->anonymous = g_slist_prepend (table->anonymous, tag);
233 table->anon_count += 1;
238 /* We get the highest tag priority, as the most-recently-added
239 tag. Note that we do NOT use gtk_text_tag_set_priority,
240 as it assumes the tag is already in the table. */
241 size = gtk_text_tag_table_get_size (table);
243 tag->priority = size - 1;
245 g_signal_emit (table, signals[TAG_ADDED], 0, tag);
249 * gtk_text_tag_table_lookup:
250 * @table: a #GtkTextTagTable
251 * @name: name of a tag
253 * Look up a named tag.
255 * Return value: (transfer none): The tag, or %NULL if none by that name is in the table.
258 gtk_text_tag_table_lookup (GtkTextTagTable *table,
261 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
262 g_return_val_if_fail (name != NULL, NULL);
264 return g_hash_table_lookup (table->hash, name);
268 * gtk_text_tag_table_remove:
269 * @table: a #GtkTextTagTable
270 * @tag: a #GtkTextTag
272 * Remove a tag from the table. This will remove the table's
273 * reference to the tag, so be careful - the tag will end
274 * up destroyed if you don't have a reference to it.
277 gtk_text_tag_table_remove (GtkTextTagTable *table,
282 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
283 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
284 g_return_if_fail (tag->table == table);
286 /* Our little bad hack to be sure buffers don't still have the tag
287 * applied to text in the buffer
289 tmp = table->buffers;
292 _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
298 /* Set ourselves to the highest priority; this means
299 when we're removed, there won't be any gaps in the
300 priorities of the tags in the table. */
301 gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
306 g_hash_table_remove (table->hash, tag->name);
309 table->anonymous = g_slist_remove (table->anonymous, tag);
310 table->anon_count -= 1;
313 g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
315 g_object_unref (tag);
320 GtkTextTagTableForeach func;
325 hash_foreach (gpointer key, gpointer value, gpointer data)
327 struct ForeachData *fd = data;
329 g_return_if_fail (GTK_IS_TEXT_TAG (value));
331 (* fd->func) (value, fd->data);
335 list_foreach (gpointer data, gpointer user_data)
337 struct ForeachData *fd = user_data;
339 g_return_if_fail (GTK_IS_TEXT_TAG (data));
341 (* fd->func) (data, fd->data);
345 * gtk_text_tag_table_foreach:
346 * @table: a #GtkTextTagTable
347 * @func: a function to call on each tag
350 * Calls @func on each tag in @table, with user data @data.
351 * Note that the table may not be modified while iterating
352 * over it (you can't add/remove tags).
355 gtk_text_tag_table_foreach (GtkTextTagTable *table,
356 GtkTextTagTableForeach func,
359 struct ForeachData d;
361 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
362 g_return_if_fail (func != NULL);
367 g_hash_table_foreach (table->hash, hash_foreach, &d);
368 g_slist_foreach (table->anonymous, list_foreach, &d);
372 * gtk_text_tag_table_get_size:
373 * @table: a #GtkTextTagTable
375 * Returns the size of the table (number of tags)
377 * Return value: number of tags in @table
380 gtk_text_tag_table_get_size (GtkTextTagTable *table)
382 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
384 return g_hash_table_size (table->hash) + table->anon_count;
388 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
391 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
393 table->buffers = g_slist_prepend (table->buffers, buffer);
397 foreach_remove_tag (GtkTextTag *tag, gpointer data)
399 GtkTextBuffer *buffer;
401 buffer = GTK_TEXT_BUFFER (data);
403 _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
407 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
410 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
412 gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
414 table->buffers = g_slist_remove (table->buffers, buffer);