gtktexttagtable.c 10.6 KB
Newer Older
1 2

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

#include <stdlib.h>

enum {
  TAG_CHANGED,
  TAG_ADDED,
  TAG_REMOVED,
  LAST_SIGNAL
};

enum {
  LAST_ARG
};

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

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

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

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

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

  return our_type;
}

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

66
  parent_class = g_type_class_peek_parent (klass);
67

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

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

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

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

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

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

129 130 131
  return table;
}

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

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

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

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

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

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

178 179
  table = GTK_TEXT_TAG_TABLE (object);

180
  switch (prop_id)
181
    {
182

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

189

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

198
  table = GTK_TEXT_TAG_TABLE (object);
199

200
  switch (prop_id)
201 202 203
    {

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

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

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

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

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

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

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

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

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

314 315
  tag->table = NULL;

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

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

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

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

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

340
  g_return_if_fail (GTK_IS_TEXT_TAG (value));
341

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

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

  g_return_if_fail (GTK_IS_TEXT_TAG (data));
351

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

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

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

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

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

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

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

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

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

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

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