]> Pileus Git - ~andy/gtk/blob - gtk/gtktexttagtable.c
Various cleanups. (#315360, Kjartan Maraas)
[~andy/gtk] / gtk / gtktexttagtable.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include <config.h>
28 #include "gtktexttagtable.h"
29 #include "gtkmarshalers.h"
30 #include "gtktextbuffer.h" /* just for the lame notify_will_remove_tag hack */
31 #include "gtkintl.h"
32 #include "gtkalias.h"
33
34 #include <stdlib.h>
35
36 enum {
37   TAG_CHANGED,
38   TAG_ADDED,
39   TAG_REMOVED,
40   LAST_SIGNAL
41 };
42
43 enum {
44   LAST_ARG
45 };
46
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,
51                                              guint                 prop_id,
52                                              const GValue         *value,
53                                              GParamSpec           *pspec);
54 static void gtk_text_tag_table_get_property (GObject              *object,
55                                              guint                 prop_id,
56                                              GValue               *value,
57                                              GParamSpec           *pspec);
58
59 static GObjectClass *parent_class = NULL;
60 static guint signals[LAST_SIGNAL] = { 0 };
61
62 GType
63 gtk_text_tag_table_get_type (void)
64 {
65   static GType our_type = 0;
66
67   if (our_type == 0)
68     {
69       static const GTypeInfo our_info =
70       {
71         sizeof (GtkTextTagTableClass),
72         (GBaseInitFunc) NULL,
73         (GBaseFinalizeFunc) NULL,
74         (GClassInitFunc) gtk_text_tag_table_class_init,
75         NULL,           /* class_finalize */
76         NULL,           /* class_data */
77         sizeof (GtkTextTagTable),
78         0,              /* n_preallocs */
79         (GInstanceInitFunc) gtk_text_tag_table_init
80       };
81
82       our_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkTextTagTable"),
83                                          &our_info, 0);
84     }
85
86   return our_type;
87 }
88
89 static void
90 gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
91 {
92   GObjectClass *object_class = G_OBJECT_CLASS (klass);
93
94   parent_class = g_type_class_peek_parent (klass);
95
96   object_class->set_property = gtk_text_tag_table_set_property;
97   object_class->get_property = gtk_text_tag_table_get_property;
98   
99   object_class->finalize = gtk_text_tag_table_finalize;
100   
101   signals[TAG_CHANGED] =
102     g_signal_new (I_("tag_changed"),
103                   G_OBJECT_CLASS_TYPE (object_class),
104                   G_SIGNAL_RUN_LAST,
105                   G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
106                   NULL, NULL,
107                   _gtk_marshal_VOID__OBJECT_BOOLEAN,
108                   G_TYPE_NONE,
109                   2,
110                   GTK_TYPE_TEXT_TAG,
111                   G_TYPE_BOOLEAN);  
112
113   signals[TAG_ADDED] =
114     g_signal_new (I_("tag_added"),
115                   G_OBJECT_CLASS_TYPE (object_class),
116                   G_SIGNAL_RUN_LAST,
117                   G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
118                   NULL, NULL,
119                   _gtk_marshal_VOID__OBJECT,
120                   G_TYPE_NONE,
121                   1,
122                   GTK_TYPE_TEXT_TAG);
123
124   signals[TAG_REMOVED] =
125     g_signal_new (I_("tag_removed"),  
126                   G_OBJECT_CLASS_TYPE (object_class),
127                   G_SIGNAL_RUN_LAST,
128                   G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
129                   NULL, NULL,
130                   _gtk_marshal_VOID__OBJECT,
131                   G_TYPE_NONE,
132                   1,
133                   GTK_TYPE_TEXT_TAG);
134 }
135
136 static void
137 gtk_text_tag_table_init (GtkTextTagTable *table)
138 {
139   table->hash = g_hash_table_new (g_str_hash, g_str_equal);
140 }
141
142 /**
143  * gtk_text_tag_table_new:
144  * 
145  * Creates a new #GtkTextTagTable. The table contains no tags by
146  * default.
147  * 
148  * Return value: a new #GtkTextTagTable
149  **/
150 GtkTextTagTable*
151 gtk_text_tag_table_new (void)
152 {
153   GtkTextTagTable *table;
154
155   table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
156
157   return table;
158 }
159
160 static void
161 foreach_unref (GtkTextTag *tag, gpointer data)
162 {
163   GSList *tmp;
164   
165   /* We don't want to emit the remove signal here; so we just unparent
166    * and unref the tag.
167    */
168
169   tmp = tag->table->buffers;
170   while (tmp != NULL)
171     {
172       _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
173                                                tag);
174       
175       tmp = tmp->next;
176     }
177   
178   tag->table = NULL;
179   g_object_unref (tag);
180 }
181
182 static void
183 gtk_text_tag_table_finalize (GObject *object)
184 {
185   GtkTextTagTable *table;
186
187   table = GTK_TEXT_TAG_TABLE (object);
188   
189   gtk_text_tag_table_foreach (table, foreach_unref, NULL);
190
191   g_hash_table_destroy (table->hash);
192   g_slist_free (table->anonymous);
193
194   g_slist_free (table->buffers);
195   
196   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
197 }
198 static void
199 gtk_text_tag_table_set_property (GObject      *object,
200                                  guint         prop_id,
201                                  const GValue *value,
202                                  GParamSpec   *pspec)
203 {
204   switch (prop_id)
205     {
206
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210     }
211 }
212
213
214 static void
215 gtk_text_tag_table_get_property (GObject      *object,
216                                  guint         prop_id,
217                                  GValue       *value,
218                                  GParamSpec   *pspec)
219 {
220   switch (prop_id)
221     {
222
223     default:
224       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225       break;
226     }
227 }
228
229 /**
230  * gtk_text_tag_table_add:
231  * @table: a #GtkTextTagTable
232  * @tag: a #GtkTextTag
233  *
234  * Add a tag to the table. The tag is assigned the highest priority
235  * in the table.
236  *
237  * @tag must not be in a tag table already, and may not have
238  * the same name as an already-added tag.
239  **/
240 void
241 gtk_text_tag_table_add (GtkTextTagTable *table,
242                         GtkTextTag      *tag)
243 {
244   guint size;
245
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);
249
250   if (tag->name && g_hash_table_lookup (table->hash, tag->name))
251     {
252       g_warning ("A tag named '%s' is already in the tag table.",
253                  tag->name);
254       return;
255     }
256   
257   g_object_ref (tag);
258
259   if (tag->name)
260     g_hash_table_insert (table->hash, tag->name, tag);
261   else
262     {
263       table->anonymous = g_slist_prepend (table->anonymous, tag);
264       table->anon_count += 1;
265     }
266
267   tag->table = table;
268
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);
273   g_assert (size > 0);
274   tag->priority = size - 1;
275
276   g_signal_emit (table, signals[TAG_ADDED], 0, tag);
277 }
278
279 /**
280  * gtk_text_tag_table_lookup:
281  * @table: a #GtkTextTagTable 
282  * @name: name of a tag
283  * 
284  * Look up a named tag.
285  * 
286  * Return value: The tag, or %NULL if none by that name is in the table.
287  **/
288 GtkTextTag*
289 gtk_text_tag_table_lookup (GtkTextTagTable *table,
290                            const gchar     *name)
291 {
292   g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
293   g_return_val_if_fail (name != NULL, NULL);
294
295   return g_hash_table_lookup (table->hash, name);
296 }
297
298 /**
299  * gtk_text_tag_table_remove:
300  * @table: a #GtkTextTagTable
301  * @tag: a #GtkTextTag
302  * 
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.
306  **/
307 void
308 gtk_text_tag_table_remove (GtkTextTagTable *table,
309                            GtkTextTag      *tag)
310 {
311   GSList *tmp;
312   
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);
316
317   /* Our little bad hack to be sure buffers don't still have the tag
318    * applied to text in the buffer
319    */
320   tmp = table->buffers;
321   while (tmp != NULL)
322     {
323       _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
324                                                tag);
325       
326       tmp = tmp->next;
327     }
328   
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);
333
334   tag->table = NULL;
335
336   if (tag->name)
337     g_hash_table_remove (table->hash, tag->name);
338   else
339     {
340       table->anonymous = g_slist_remove (table->anonymous, tag);
341       table->anon_count -= 1;
342     }
343
344   g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
345
346   g_object_unref (tag);
347 }
348
349 struct ForeachData
350 {
351   GtkTextTagTableForeach func;
352   gpointer data;
353 };
354
355 static void
356 hash_foreach (gpointer key, gpointer value, gpointer data)
357 {
358   struct ForeachData *fd = data;
359
360   g_return_if_fail (GTK_IS_TEXT_TAG (value));
361
362   (* fd->func) (value, fd->data);
363 }
364
365 static void
366 list_foreach (gpointer data, gpointer user_data)
367 {
368   struct ForeachData *fd = user_data;
369
370   g_return_if_fail (GTK_IS_TEXT_TAG (data));
371
372   (* fd->func) (data, fd->data);
373 }
374
375 /**
376  * gtk_text_tag_table_foreach:
377  * @table: a #GtkTextTagTable
378  * @func: a function to call on each tag
379  * @data: user data
380  *
381  * Calls @func on each tag in @table, with user data @data.
382  * 
383  **/
384 void
385 gtk_text_tag_table_foreach (GtkTextTagTable       *table,
386                             GtkTextTagTableForeach func,
387                             gpointer               data)
388 {
389   struct ForeachData d;
390
391   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
392   g_return_if_fail (func != NULL);
393
394   d.func = func;
395   d.data = data;
396
397   g_hash_table_foreach (table->hash, hash_foreach, &d);
398   g_slist_foreach (table->anonymous, list_foreach, &d);
399 }
400
401 /**
402  * gtk_text_tag_table_get_size:
403  * @table: a #GtkTextTagTable
404  * 
405  * Returns the size of the table (number of tags)
406  * 
407  * Return value: number of tags in @table
408  **/
409 gint
410 gtk_text_tag_table_get_size (GtkTextTagTable *table)
411 {
412   g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
413
414   return g_hash_table_size (table->hash) + table->anon_count;
415 }
416
417 void
418 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
419                                 gpointer         buffer)
420 {
421   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
422
423   table->buffers = g_slist_prepend (table->buffers, buffer);
424 }
425
426 static void
427 foreach_remove_tag (GtkTextTag *tag, gpointer data)
428 {
429   GtkTextBuffer *buffer;
430
431   buffer = GTK_TEXT_BUFFER (data);
432
433   _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
434 }
435
436 void
437 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
438                                    gpointer         buffer)
439 {
440   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
441
442   gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
443   
444   table->buffers = g_slist_remove (table->buffers, buffer);
445 }
446
447 #define __GTK_TEXT_TAG_TABLE_C__
448 #include "gtkaliasdef.c"