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