gtktexttagtable.c 10.8 KB
Newer Older
1 2

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

#include <stdlib.h>

enum {
  TAG_CHANGED,
  TAG_ADDED,
  TAG_REMOVED,
  LAST_SIGNAL
};

enum {
  LAST_ARG
};

20 21 22 23 24 25
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
26
                                             GParamSpec           *pspec);
27 28 29
static void gtk_text_tag_table_get_property (GObject              *object,
                                             guint                 prop_id,
                                             GValue               *value,
Tim Janik's avatar
Tim Janik committed
30
                                             GParamSpec           *pspec);
31 32

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

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

  if (our_type == 0)
    {
42
      static const GTypeInfo our_info =
43 44
      {
        sizeof (GtkTextTagTableClass),
45 46 47 48 49 50 51 52
        (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
53 54
      };

55 56 57 58
      our_type = g_type_register_static (G_TYPE_OBJECT,
                                         "GtkTextTagTable",
                                         &our_info,
                                         0);
59
    }
60 61 62 63 64 65 66

  return our_type;
}

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

69
  parent_class = g_type_class_peek_parent (klass);
70

71 72 73 74 75
  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;
  
76
  signals[TAG_CHANGED] =
77 78 79 80 81
    g_signal_new ("tag_changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
                  NULL, NULL,
82
                  _gtk_marshal_VOID__OBJECT_BOOLEAN,
83 84
                  G_TYPE_NONE,
                  2,
85
                  GTK_TYPE_TEXT_TAG,
86
                  G_TYPE_BOOLEAN);  
87 88

  signals[TAG_ADDED] =
89 90 91 92 93
    g_signal_new ("tag_added",
                  GTK_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
                  NULL, NULL,
94
                  _gtk_marshal_VOID__OBJECT,
95 96
                  GTK_TYPE_NONE,
                  1,
97
                  GTK_TYPE_TEXT_TAG);
98 99

  signals[TAG_REMOVED] =
100 101 102 103 104
    g_signal_new ("tag_removed",                   
                  GTK_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
                  NULL, NULL,
105
                  _gtk_marshal_VOID__OBJECT,
106 107
                  GTK_TYPE_NONE,
                  1,
108
                  GTK_TYPE_TEXT_TAG);
109 110
}

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

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

130
  table = GTK_TEXT_TAG_TABLE (g_object_new (gtk_text_tag_table_get_type (), NULL));
131

132 133 134
  return table;
}

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

144 145 146 147 148 149 150 151 152
  tmp = tag->table->buffers;
  while (tmp != NULL)
    {
      _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (tmp->data),
                                               tag);
      
      tmp = tmp->next;
    }
  
153
  tag->table = NULL;
154 155 156
  g_object_unref (G_OBJECT (tag));
}

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

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

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

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

181 182
  table = GTK_TEXT_TAG_TABLE (object);

183
  switch (prop_id)
184
    {
185

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

192

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

201
  table = GTK_TEXT_TAG_TABLE (object);
202

203
  switch (prop_id)
204 205 206
    {

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

212 213 214 215 216 217 218
/**
 * 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
219 220 221
 *
 * @tag must not be in a tag table already, and may not have
 * the same name as an already-added tag.
222
 **/
223
void
224 225
gtk_text_tag_table_add (GtkTextTagTable *table,
                        GtkTextTag      *tag)
226 227
{
  guint size;
228

229 230 231 232
  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
233 234 235 236 237 238 239
  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;
    }
  
240
  g_object_ref (G_OBJECT (tag));
241 242

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

250 251 252 253 254
  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. */
255
  size = gtk_text_tag_table_get_size (table);
256
  g_assert (size > 0);
257
  tag->priority = size - 1;
258

259
  g_signal_emit (G_OBJECT (table), signals[TAG_ADDED], 0, tag);
260 261
}

262 263 264 265 266 267 268 269 270
/**
 * 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.
 **/
271
GtkTextTag*
272 273
gtk_text_tag_table_lookup (GtkTextTagTable *table,
                           const gchar     *name)
274
{
275 276 277 278
  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);
279 280
}

281 282 283 284 285 286 287 288 289
/**
 * 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.
 **/
290
void
291 292
gtk_text_tag_table_remove (GtkTextTagTable *table,
                           GtkTextTag      *tag)
293
{
294 295
  GSList *tmp;
  
296
  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
297
  g_return_if_fail (GTK_IS_TEXT_TAG (tag));
298
  g_return_if_fail (tag->table == table);
299

300 301 302 303 304 305 306 307 308 309 310 311
  /* 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;
    }
  
312 313 314
  /* 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. */
315
  gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
316

317 318
  tag->table = NULL;

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

327
  g_signal_emit (G_OBJECT (table), signals[TAG_REMOVED], 0, tag);
328

329
  g_object_unref (G_OBJECT (tag));
330 331
}

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

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

343
  g_return_if_fail (GTK_IS_TEXT_TAG (value));
344

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

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

  g_return_if_fail (GTK_IS_TEXT_TAG (data));
354

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

358 359 360 361 362 363 364 365 366
/**
 * 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.
 * 
 **/
367
void
368 369 370
gtk_text_tag_table_foreach (GtkTextTagTable       *table,
                            GtkTextTagTableForeach func,
                            gpointer               data)
371
{
372
  struct ForeachData d;
373 374 375

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

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

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

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

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

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);
}

409 410 411 412 413 414 415 416 417 418
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);
}

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

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