2 #include "gtktexttagtable.h"
18 static void gtk_text_tag_table_init (GtkTextTagTable *table);
19 static void gtk_text_tag_table_class_init (GtkTextTagTableClass *klass);
20 static void gtk_text_tag_table_finalize (GObject *object);
21 static void gtk_text_tag_table_set_property (GObject *object,
25 const gchar *trailer);
26 static void gtk_text_tag_table_get_property (GObject *object,
30 const gchar *trailer);
32 static GObjectClass *parent_class = NULL;
33 static guint signals[LAST_SIGNAL] = { 0 };
36 gtk_text_tag_table_get_type (void)
38 static GType our_type = 0;
42 static const GTypeInfo our_info =
44 sizeof (GtkTextTagTableClass),
46 (GBaseFinalizeFunc) NULL,
47 (GClassInitFunc) gtk_text_tag_table_class_init,
48 NULL, /* class_finalize */
49 NULL, /* class_data */
50 sizeof (GtkTextTagTable),
52 (GInstanceInitFunc) gtk_text_tag_table_init
55 our_type = g_type_register_static (G_TYPE_OBJECT,
65 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
67 GObjectClass *object_class = G_OBJECT_CLASS (klass);
69 parent_class = g_type_class_peek_parent (klass);
71 object_class->set_property = gtk_text_tag_table_set_property;
72 object_class->get_property = gtk_text_tag_table_get_property;
74 object_class->finalize = gtk_text_tag_table_finalize;
76 signals[TAG_CHANGED] =
77 g_signal_newc ("tag_changed",
78 G_TYPE_FROM_CLASS (object_class),
80 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
82 gtk_marshal_VOID__OBJECT_BOOLEAN,
89 g_signal_newc ("tag_added",
90 GTK_CLASS_TYPE (object_class),
92 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
94 gtk_marshal_VOID__OBJECT,
99 signals[TAG_REMOVED] =
100 g_signal_newc ("tag_removed",
101 GTK_CLASS_TYPE (object_class),
103 G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
105 gtk_marshal_VOID__OBJECT,
112 gtk_text_tag_table_init (GtkTextTagTable *table)
114 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
118 * gtk_text_tag_table_new:
120 * Creates a new #GtkTextTagTable. The table contains no tags by
123 * Return value: a new #GtkTextTagTable
126 gtk_text_tag_table_new (void)
128 GtkTextTagTable *table;
130 table = GTK_TEXT_TAG_TABLE (g_object_new (gtk_text_tag_table_get_type (), NULL));
136 foreach_unref (GtkTextTag *tag, gpointer data)
138 /* We don't want to emit the remove signal here; so we just unparent
143 g_object_unref (G_OBJECT (tag));
147 gtk_text_tag_table_finalize (GObject *object)
149 GtkTextTagTable *table;
151 table = GTK_TEXT_TAG_TABLE (object);
153 gtk_text_tag_table_foreach (table, foreach_unref, NULL);
155 g_hash_table_destroy (table->hash);
156 g_slist_free (table->anonymous);
158 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
161 gtk_text_tag_table_set_property (GObject *object,
165 const gchar *trailer)
167 GtkTextTagTable *table;
169 table = GTK_TEXT_TAG_TABLE (object);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 gtk_text_tag_table_get_property (GObject *object,
186 const gchar *trailer)
188 GtkTextTagTable *table;
190 table = GTK_TEXT_TAG_TABLE (object);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 * gtk_text_tag_table_add:
203 * @table: a #GtkTextTagTable
204 * @tag: a #GtkTextTag
206 * Add a tag to the table. The tag is assigned the highest priority
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->name == NULL ||
219 g_hash_table_lookup (table->hash, tag->name) == NULL);
220 g_return_if_fail (tag->table == NULL);
222 g_object_ref (G_OBJECT (tag));
225 g_hash_table_insert (table->hash, tag->name, tag);
228 table->anonymous = g_slist_prepend (table->anonymous, tag);
229 table->anon_count += 1;
234 /* We get the highest tag priority, as the most-recently-added
235 tag. Note that we do NOT use gtk_text_tag_set_priority,
236 as it assumes the tag is already in the table. */
237 size = gtk_text_tag_table_size (table);
239 tag->priority = size - 1;
241 g_signal_emit (G_OBJECT (table), signals[TAG_ADDED], 0, tag);
245 * gtk_text_tag_table_lookup:
246 * @table: a #GtkTextTagTable
247 * @name: name of a tag
249 * Look up a named tag.
251 * Return value: The tag, or %NULL if none by that name is in the table.
254 gtk_text_tag_table_lookup (GtkTextTagTable *table,
257 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
258 g_return_val_if_fail (name != NULL, NULL);
260 return g_hash_table_lookup (table->hash, name);
264 * gtk_text_tag_table_remove:
265 * @table: a #GtkTextTagTable
266 * @tag: a #GtkTextTag
268 * Remove a tag from the table. This will remove the table's
269 * reference to the tag, so be careful - the tag will end
270 * up destroyed if you don't have a reference to it.
273 gtk_text_tag_table_remove (GtkTextTagTable *table,
276 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
277 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
278 g_return_if_fail (tag->table == table);
280 /* Set ourselves to the highest priority; this means
281 when we're removed, there won't be any gaps in the
282 priorities of the tags in the table. */
283 gtk_text_tag_set_priority (tag, gtk_text_tag_table_size (table) - 1);
288 g_hash_table_remove (table->hash, tag->name);
291 table->anonymous = g_slist_remove (table->anonymous, tag);
292 table->anon_count -= 1;
295 g_signal_emit (G_OBJECT (table), signals[TAG_REMOVED], 0, tag);
297 g_object_unref (G_OBJECT (tag));
302 GtkTextTagTableForeach func;
307 hash_foreach (gpointer key, gpointer value, gpointer data)
309 struct ForeachData *fd = data;
311 g_return_if_fail (GTK_IS_TEXT_TAG (value));
313 (* fd->func) (value, fd->data);
317 list_foreach (gpointer data, gpointer user_data)
319 struct ForeachData *fd = user_data;
321 g_return_if_fail (GTK_IS_TEXT_TAG (data));
323 (* fd->func) (data, fd->data);
327 * gtk_text_tag_table_foreach:
328 * @table: a #GtkTextTagTable
329 * @func: a function to call on each tag
332 * Calls @func on each tag in @table, with user data @data.
336 gtk_text_tag_table_foreach (GtkTextTagTable *table,
337 GtkTextTagTableForeach func,
340 struct ForeachData d;
342 g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
343 g_return_if_fail (func != NULL);
348 g_hash_table_foreach (table->hash, hash_foreach, &d);
349 g_slist_foreach (table->anonymous, list_foreach, &d);
353 * gtk_text_tag_table_size:
354 * @table: a #GtkTextTagTable
356 * Returns the size of the table (number of tags)
358 * Return value: number of tags in @table
361 gtk_text_tag_table_size (GtkTextTagTable *table)
363 g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
365 return g_hash_table_size (table->hash) + table->anon_count;