]> Pileus Git - ~andy/gtk/blob - gtk/gtktexttagtable.c
Intern some more strings.
[~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   GtkTextTagTable *table;
205
206   table = GTK_TEXT_TAG_TABLE (object);
207
208   switch (prop_id)
209     {
210
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214     }
215 }
216
217
218 static void
219 gtk_text_tag_table_get_property (GObject      *object,
220                                  guint         prop_id,
221                                  GValue       *value,
222                                  GParamSpec   *pspec)
223 {
224   GtkTextTagTable *table;
225
226   table = GTK_TEXT_TAG_TABLE (object);
227
228   switch (prop_id)
229     {
230
231     default:
232       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233       break;
234     }
235 }
236
237 /**
238  * gtk_text_tag_table_add:
239  * @table: a #GtkTextTagTable
240  * @tag: a #GtkTextTag
241  *
242  * Add a tag to the table. The tag is assigned the highest priority
243  * in the table.
244  *
245  * @tag must not be in a tag table already, and may not have
246  * the same name as an already-added tag.
247  **/
248 void
249 gtk_text_tag_table_add (GtkTextTagTable *table,
250                         GtkTextTag      *tag)
251 {
252   guint size;
253
254   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
255   g_return_if_fail (GTK_IS_TEXT_TAG (tag));
256   g_return_if_fail (tag->table == NULL);
257
258   if (tag->name && g_hash_table_lookup (table->hash, tag->name))
259     {
260       g_warning ("A tag named '%s' is already in the tag table.",
261                  tag->name);
262       return;
263     }
264   
265   g_object_ref (tag);
266
267   if (tag->name)
268     g_hash_table_insert (table->hash, tag->name, tag);
269   else
270     {
271       table->anonymous = g_slist_prepend (table->anonymous, tag);
272       table->anon_count += 1;
273     }
274
275   tag->table = table;
276
277   /* We get the highest tag priority, as the most-recently-added
278      tag. Note that we do NOT use gtk_text_tag_set_priority,
279      as it assumes the tag is already in the table. */
280   size = gtk_text_tag_table_get_size (table);
281   g_assert (size > 0);
282   tag->priority = size - 1;
283
284   g_signal_emit (table, signals[TAG_ADDED], 0, tag);
285 }
286
287 /**
288  * gtk_text_tag_table_lookup:
289  * @table: a #GtkTextTagTable 
290  * @name: name of a tag
291  * 
292  * Look up a named tag.
293  * 
294  * Return value: The tag, or %NULL if none by that name is in the table.
295  **/
296 GtkTextTag*
297 gtk_text_tag_table_lookup (GtkTextTagTable *table,
298                            const gchar     *name)
299 {
300   g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
301   g_return_val_if_fail (name != NULL, NULL);
302
303   return g_hash_table_lookup (table->hash, name);
304 }
305
306 /**
307  * gtk_text_tag_table_remove:
308  * @table: a #GtkTextTagTable
309  * @tag: a #GtkTextTag
310  * 
311  * Remove a tag from the table. This will remove the table's
312  * reference to the tag, so be careful - the tag will end
313  * up destroyed if you don't have a reference to it.
314  **/
315 void
316 gtk_text_tag_table_remove (GtkTextTagTable *table,
317                            GtkTextTag      *tag)
318 {
319   GSList *tmp;
320   
321   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
322   g_return_if_fail (GTK_IS_TEXT_TAG (tag));
323   g_return_if_fail (tag->table == table);
324
325   /* Our little bad hack to be sure buffers don't still have the tag
326    * applied to text in the buffer
327    */
328   tmp = table->buffers;
329   while (tmp != NULL)
330     {
331       _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
332                                                tag);
333       
334       tmp = tmp->next;
335     }
336   
337   /* Set ourselves to the highest priority; this means
338      when we're removed, there won't be any gaps in the
339      priorities of the tags in the table. */
340   gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
341
342   tag->table = NULL;
343
344   if (tag->name)
345     g_hash_table_remove (table->hash, tag->name);
346   else
347     {
348       table->anonymous = g_slist_remove (table->anonymous, tag);
349       table->anon_count -= 1;
350     }
351
352   g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
353
354   g_object_unref (tag);
355 }
356
357 struct ForeachData
358 {
359   GtkTextTagTableForeach func;
360   gpointer data;
361 };
362
363 static void
364 hash_foreach (gpointer key, gpointer value, gpointer data)
365 {
366   struct ForeachData *fd = data;
367
368   g_return_if_fail (GTK_IS_TEXT_TAG (value));
369
370   (* fd->func) (value, fd->data);
371 }
372
373 static void
374 list_foreach (gpointer data, gpointer user_data)
375 {
376   struct ForeachData *fd = user_data;
377
378   g_return_if_fail (GTK_IS_TEXT_TAG (data));
379
380   (* fd->func) (data, fd->data);
381 }
382
383 /**
384  * gtk_text_tag_table_foreach:
385  * @table: a #GtkTextTagTable
386  * @func: a function to call on each tag
387  * @data: user data
388  *
389  * Calls @func on each tag in @table, with user data @data.
390  * 
391  **/
392 void
393 gtk_text_tag_table_foreach (GtkTextTagTable       *table,
394                             GtkTextTagTableForeach func,
395                             gpointer               data)
396 {
397   struct ForeachData d;
398
399   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
400   g_return_if_fail (func != NULL);
401
402   d.func = func;
403   d.data = data;
404
405   g_hash_table_foreach (table->hash, hash_foreach, &d);
406   g_slist_foreach (table->anonymous, list_foreach, &d);
407 }
408
409 /**
410  * gtk_text_tag_table_get_size:
411  * @table: a #GtkTextTagTable
412  * 
413  * Returns the size of the table (number of tags)
414  * 
415  * Return value: number of tags in @table
416  **/
417 gint
418 gtk_text_tag_table_get_size (GtkTextTagTable *table)
419 {
420   g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
421
422   return g_hash_table_size (table->hash) + table->anon_count;
423 }
424
425 void
426 _gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
427                                 gpointer         buffer)
428 {
429   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
430
431   table->buffers = g_slist_prepend (table->buffers, buffer);
432 }
433
434 static void
435 foreach_remove_tag (GtkTextTag *tag, gpointer data)
436 {
437   GtkTextBuffer *buffer;
438
439   buffer = GTK_TEXT_BUFFER (data);
440
441   _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
442 }
443
444 void
445 _gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
446                                    gpointer         buffer)
447 {
448   g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
449
450   gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
451   
452   table->buffers = g_slist_remove (table->buffers, buffer);
453 }
454
455 #define __GTK_TEXT_TAG_TABLE_C__
456 #include "gtkaliasdef.c"