gtktexttagtable.c 10.7 KB
Newer Older
1

2
#include <config.h>
3
#include "gtkalias.h"
4
#include "gtktexttagtable.h"
5
#include "gtkmarshalers.h"
6
#include "gtktextbuffer.h" /* just for the lame notify_will_remove_tag hack */
7 8 9 10 11 12 13 14 15 16 17 18 19 20

#include <stdlib.h>

enum {
  TAG_CHANGED,
  TAG_ADDED,
  TAG_REMOVED,
  LAST_SIGNAL
};

enum {
  LAST_ARG
};

21 22 23 24 25 26
static void gtk_text_tag_table_init         (GtkTextTagTable      *table);
static void gtk_text_tag_table_class_init   (GtkTextTagTableClass *klass);
static void gtk_text_tag_table_finalize     (GObject              *object);
static void gtk_text_tag_table_set_property (GObject              *object,
                                             guint                 prop_id,
                                             const GValue         *value,
Tim Janik's avatar
Tim Janik committed
27
                                             GParamSpec           *pspec);
28 29 30
static void gtk_text_tag_table_get_property (GObject              *object,
                                             guint                 prop_id,
                                             GValue               *value,
Tim Janik's avatar
Tim Janik committed
31
                                             GParamSpec           *pspec);
32 33

static GObjectClass *parent_class = NULL;
34 35
static guint signals[LAST_SIGNAL] = { 0 };

36
GType
37 38
gtk_text_tag_table_get_type (void)
{
39
  static GType our_type = 0;
40 41 42

  if (our_type == 0)
    {
43
      static const GTypeInfo our_info =
44 45
      {
        sizeof (GtkTextTagTableClass),
46 47 48 49 50 51 52 53
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_text_tag_table_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkTextTagTable),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_text_tag_table_init
54 55
      };

Manish Singh's avatar
Manish Singh committed
56 57
      our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextTagTable",
                                         &our_info, 0);
58
    }
59 60 61 62 63 64 65

  return our_type;
}

static void
gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
{
66
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
67

68
  parent_class = g_type_class_peek_parent (klass);
69

70 71 72 73 74
  object_class->set_property = gtk_text_tag_table_set_property;
  object_class->get_property = gtk_text_tag_table_get_property;
  
  object_class->finalize = gtk_text_tag_table_finalize;
  
75
  signals[TAG_CHANGED] =
76 77 78 79 80
    g_signal_new ("tag_changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
                  NULL, NULL,
81
                  _gtk_marshal_VOID__OBJECT_BOOLEAN,
82 83
                  G_TYPE_NONE,
                  2,
84
                  GTK_TYPE_TEXT_TAG,
85
                  G_TYPE_BOOLEAN);  
86 87

  signals[TAG_ADDED] =
88
    g_signal_new ("tag_added",
Manish Singh's avatar
Manish Singh committed
89
                  G_OBJECT_CLASS_TYPE (object_class),
90 91 92
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
                  NULL, NULL,
93
                  _gtk_marshal_VOID__OBJECT,
Manish Singh's avatar
Manish Singh committed
94
                  G_TYPE_NONE,
95
                  1,
96
                  GTK_TYPE_TEXT_TAG);
97 98

  signals[TAG_REMOVED] =
99
    g_signal_new ("tag_removed",                   
Manish Singh's avatar
Manish Singh committed
100
                  G_OBJECT_CLASS_TYPE (object_class),
101 102 103
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
                  NULL, NULL,
104
                  _gtk_marshal_VOID__OBJECT,
Manish Singh's avatar
Manish Singh committed
105
                  G_TYPE_NONE,
106
                  1,
107
                  GTK_TYPE_TEXT_TAG);
108 109
}

110
static void
111 112
gtk_text_tag_table_init (GtkTextTagTable *table)
{
113
  table->hash = g_hash_table_new (g_str_hash, g_str_equal);
114 115
}

116 117
/**
 * gtk_text_tag_table_new:
118
 * 
119 120 121 122 123
 * Creates a new #GtkTextTagTable. The table contains no tags by
 * default.
 * 
 * Return value: a new #GtkTextTagTable
 **/
124 125 126 127 128
GtkTextTagTable*
gtk_text_tag_table_new (void)
{
  GtkTextTagTable *table;

Manish Singh's avatar
Manish Singh committed
129
  table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
130

131 132 133
  return table;
}

134 135 136
static void
foreach_unref (GtkTextTag *tag, gpointer data)
{
137 138
  GSList *tmp;
  
139 140 141
  /* We don't want to emit the remove signal here; so we just unparent
   * and unref the tag.
   */
142

143 144 145 146 147 148 149 150 151
  tmp = tag->table->buffers;
  while (tmp != NULL)
    {
      _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
                                               tag);
      
      tmp = tmp->next;
    }
  
152
  tag->table = NULL;
Manish Singh's avatar
Manish Singh committed
153
  g_object_unref (tag);
154 155
}

156 157 158 159 160
static void
gtk_text_tag_table_finalize (GObject *object)
{
  GtkTextTagTable *table;

161
  table = GTK_TEXT_TAG_TABLE (object);
162
  
163
  gtk_text_tag_table_foreach (table, foreach_unref, NULL);
164

165
  g_hash_table_destroy (table->hash);
166 167
  g_slist_free (table->anonymous);

168 169
  g_slist_free (table->buffers);
  
170
  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
171 172
}
static void
173 174 175
gtk_text_tag_table_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
Tim Janik's avatar
Tim Janik committed
176
                                 GParamSpec   *pspec)
177 178 179
{
  GtkTextTagTable *table;

180 181
  table = GTK_TEXT_TAG_TABLE (object);

182
  switch (prop_id)
183
    {
184

185
    default:
186
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 188 189 190
      break;
    }
}

191

192
static void
193 194 195
gtk_text_tag_table_get_property (GObject      *object,
                                 guint         prop_id,
                                 GValue       *value,
Tim Janik's avatar
Tim Janik committed
196
                                 GParamSpec   *pspec)
197 198 199
{
  GtkTextTagTable *table;

200
  table = GTK_TEXT_TAG_TABLE (object);
201

202
  switch (prop_id)
203 204 205
    {

    default:
206
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 208 209 210
      break;
    }
}

211 212 213 214 215 216 217
/**
 * gtk_text_tag_table_add:
 * @table: a #GtkTextTagTable
 * @tag: a #GtkTextTag
 *
 * Add a tag to the table. The tag is assigned the highest priority
 * in the table.
Havoc Pennington's avatar
Havoc Pennington committed
218 219 220
 *
 * @tag must not be in a tag table already, and may not have
 * the same name as an already-added tag.
221
 **/
222
void
223 224
gtk_text_tag_table_add (GtkTextTagTable *table,
                        GtkTextTag      *tag)
225 226
{
  guint size;
227

228 229 230 231
  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
  g_return_if_fail (GTK_IS_TEXT_TAG (tag));
  g_return_if_fail (tag->table == NULL);

Havoc Pennington's avatar
Havoc Pennington committed
232 233 234 235 236 237 238
  if (tag->name && g_hash_table_lookup (table->hash, tag->name))
    {
      g_warning ("A tag named '%s' is already in the tag table.",
                 tag->name);
      return;
    }
  
Manish Singh's avatar
Manish Singh committed
239
  g_object_ref (tag);
240 241

  if (tag->name)
242
    g_hash_table_insert (table->hash, tag->name, tag);
243 244 245 246 247
  else
    {
      table->anonymous = g_slist_prepend (table->anonymous, tag);
      table->anon_count += 1;
    }
248

249 250 251 252 253
  tag->table = table;

  /* We get the highest tag priority, as the most-recently-added
     tag. Note that we do NOT use gtk_text_tag_set_priority,
     as it assumes the tag is already in the table. */
254
  size = gtk_text_tag_table_get_size (table);
255
  g_assert (size > 0);
256
  tag->priority = size - 1;
257

Manish Singh's avatar
Manish Singh committed
258
  g_signal_emit (table, signals[TAG_ADDED], 0, tag);
259 260
}

261 262 263 264 265 266 267 268 269
/**
 * gtk_text_tag_table_lookup:
 * @table: a #GtkTextTagTable 
 * @name: name of a tag
 * 
 * Look up a named tag.
 * 
 * Return value: The tag, or %NULL if none by that name is in the table.
 **/
270
GtkTextTag*
271 272
gtk_text_tag_table_lookup (GtkTextTagTable *table,
                           const gchar     *name)
273
{
274 275 276 277
  g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  return g_hash_table_lookup (table->hash, name);
278 279
}

280 281 282 283 284 285 286 287 288
/**
 * gtk_text_tag_table_remove:
 * @table: a #GtkTextTagTable
 * @tag: a #GtkTextTag
 * 
 * Remove a tag from the table. This will remove the table's
 * reference to the tag, so be careful - the tag will end
 * up destroyed if you don't have a reference to it.
 **/
289
void
290 291
gtk_text_tag_table_remove (GtkTextTagTable *table,
                           GtkTextTag      *tag)
292
{
293 294
  GSList *tmp;
  
295
  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
296
  g_return_if_fail (GTK_IS_TEXT_TAG (tag));
297
  g_return_if_fail (tag->table == table);
298

299 300 301 302 303 304 305 306 307 308 309 310
  /* Our little bad hack to be sure buffers don't still have the tag
   * applied to text in the buffer
   */
  tmp = table->buffers;
  while (tmp != NULL)
    {
      _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
                                               tag);
      
      tmp = tmp->next;
    }
  
311 312 313
  /* Set ourselves to the highest priority; this means
     when we're removed, there won't be any gaps in the
     priorities of the tags in the table. */
314
  gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
315

316 317
  tag->table = NULL;

318
  if (tag->name)
319
    g_hash_table_remove (table->hash, tag->name);
320 321 322 323 324
  else
    {
      table->anonymous = g_slist_remove (table->anonymous, tag);
      table->anon_count -= 1;
    }
325

Manish Singh's avatar
Manish Singh committed
326
  g_signal_emit (table, signals[TAG_REMOVED], 0, tag);
327

Manish Singh's avatar
Manish Singh committed
328
  g_object_unref (tag);
329 330
}

331 332 333 334 335 336 337 338 339 340 341
struct ForeachData
{
  GtkTextTagTableForeach func;
  gpointer data;
};

static void
hash_foreach (gpointer key, gpointer value, gpointer data)
{
  struct ForeachData *fd = data;

342
  g_return_if_fail (GTK_IS_TEXT_TAG (value));
343

344 345 346
  (* fd->func) (value, fd->data);
}

347 348 349 350 351 352
static void
list_foreach (gpointer data, gpointer user_data)
{
  struct ForeachData *fd = user_data;

  g_return_if_fail (GTK_IS_TEXT_TAG (data));
353

354 355 356
  (* fd->func) (data, fd->data);
}

357 358 359 360 361 362 363 364 365
/**
 * gtk_text_tag_table_foreach:
 * @table: a #GtkTextTagTable
 * @func: a function to call on each tag
 * @data: user data
 *
 * Calls @func on each tag in @table, with user data @data.
 * 
 **/
366
void
367 368 369
gtk_text_tag_table_foreach (GtkTextTagTable       *table,
                            GtkTextTagTableForeach func,
                            gpointer               data)
370
{
371
  struct ForeachData d;
372 373 374

  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
  g_return_if_fail (func != NULL);
375

376 377
  d.func = func;
  d.data = data;
378 379

  g_hash_table_foreach (table->hash, hash_foreach, &d);
380
  g_slist_foreach (table->anonymous, list_foreach, &d);
381 382
}

383
/**
384
 * gtk_text_tag_table_get_size:
385 386 387 388 389 390 391
 * @table: a #GtkTextTagTable
 * 
 * Returns the size of the table (number of tags)
 * 
 * Return value: number of tags in @table
 **/
gint
392
gtk_text_tag_table_get_size (GtkTextTagTable *table)
393
{
394
  g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
395

396
  return g_hash_table_size (table->hash) + table->anon_count;
397
}
398 399 400 401 402 403 404 405 406 407

void
_gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
                                gpointer         buffer)
{
  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));

  table->buffers = g_slist_prepend (table->buffers, buffer);
}

408 409 410 411 412 413 414 415 416 417
static void
foreach_remove_tag (GtkTextTag *tag, gpointer data)
{
  GtkTextBuffer *buffer;

  buffer = GTK_TEXT_BUFFER (data);

  _gtk_text_buffer_notify_will_remove_tag (buffer, tag);
}

418 419 420 421 422 423
void
_gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
                                   gpointer         buffer)
{
  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));

424 425
  gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
  
426 427
  table->buffers = g_slist_remove (table->buffers, buffer);
}