gtktexttagtable.c 10.6 KB
Newer Older
1

2
#include <config.h>
3
#include "gtktexttagtable.h"
4
#include "gtkmarshalers.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
      };

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

  return our_type;
}

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

67
  parent_class = g_type_class_peek_parent (klass);
68

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

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

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

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

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

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

130 131 132
  return table;
}

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

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

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

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

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

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

179 180
  table = GTK_TEXT_TAG_TABLE (object);

181
  switch (prop_id)
182
    {
183

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

190

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

199
  table = GTK_TEXT_TAG_TABLE (object);
200

201
  switch (prop_id)
202 203 204
    {

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

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

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

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

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

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

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

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

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

315 316
  tag->table = NULL;

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

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

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

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

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

341
  g_return_if_fail (GTK_IS_TEXT_TAG (value));
342

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

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

  g_return_if_fail (GTK_IS_TEXT_TAG (data));
352

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

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

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

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

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

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

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

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

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

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

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