gtktexttagtable.c 12.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */
24

25
#include "config.h"
26

27
#include "gtktexttagtable.h"
28

29
#include "gtkbuildable.h"
30
#include "gtktexttagprivate.h"
31
#include "gtkmarshalers.h"
32
#include "gtktextbufferprivate.h" /* just for the lame notify_will_remove_tag hack */
33
#include "gtkintl.h"
34 35 36

#include <stdlib.h>

37

38 39 40 41 42
/**
 * SECTION:gtktexttagtable
 * @Short_description: Collection of tags that can be used together
 * @Title: GtkTextTagTable
 *
43 44 45
 * You may wish to begin by reading the
 * [text widget conceptual overview][TextWidget]
 * which gives an overview of all the objects and
Matthias Clasen's avatar
Matthias Clasen committed
46
 * data types related to the text widget and how they work together.
47
 *
48
 * # GtkTextTagTables as GtkBuildable
49
 *
50
 * The GtkTextTagTable implementation of the GtkBuildable interface
51 52
 * supports adding tags by specifying “tag” as the “type” attribute
 * of a <child> element.
Matthias Clasen's avatar
Matthias Clasen committed
53
 *
54
 * An example of a UI definition fragment specifying tags:
55
 * |[
56 57 58 59 60
 * <object class="GtkTextTagTable">
 *  <child type="tag">
 *    <object class="GtkTextTag"/>
 *  </child>
 * </object>
61
 * ]|
62 63
 */

64
struct _GtkTextTagTablePrivate
65 66 67 68 69 70 71 72
{
  GHashTable *hash;
  GSList     *anonymous;
  GSList     *buffers;

  gint anon_count;
};

73 74 75 76 77 78 79
enum {
  TAG_CHANGED,
  TAG_ADDED,
  TAG_REMOVED,
  LAST_SIGNAL
};

80
static void gtk_text_tag_table_finalize                 (GObject             *object);
81

82 83 84 85 86 87
static void gtk_text_tag_table_buildable_interface_init (GtkBuildableIface   *iface);
static void gtk_text_tag_table_buildable_add_child      (GtkBuildable        *buildable,
							 GtkBuilder          *builder,
							 GObject             *child,
							 const gchar         *type);

88 89
static guint signals[LAST_SIGNAL] = { 0 };

90
G_DEFINE_TYPE_WITH_CODE (GtkTextTagTable, gtk_text_tag_table, G_TYPE_OBJECT,
91
                         G_ADD_PRIVATE (GtkTextTagTable)
92 93
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_text_tag_table_buildable_interface_init))
94 95 96 97

static void
gtk_text_tag_table_class_init (GtkTextTagTableClass *klass)
{
98
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
99

100
  object_class->finalize = gtk_text_tag_table_finalize;
101 102 103 104 105

  /**
   * GtkTextTagTable::tag-changed:
   * @texttagtable: the object which received the signal.
   * @tag: the changed tag.
106
   * @size_changed: whether the change affects the #GtkTextView layout.
107
   */
108
  signals[TAG_CHANGED] =
109
    g_signal_new (I_("tag-changed"),
110 111 112 113
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_changed),
                  NULL, NULL,
114
                  _gtk_marshal_VOID__OBJECT_BOOLEAN,
115 116
                  G_TYPE_NONE,
                  2,
117
                  GTK_TYPE_TEXT_TAG,
118
                  G_TYPE_BOOLEAN);  
119

120 121 122 123 124
  /**
   * GtkTextTagTable::tag-added:
   * @texttagtable: the object which received the signal.
   * @tag: the added tag.
   */
125
  signals[TAG_ADDED] =
126
    g_signal_new (I_("tag-added"),
Manish Singh's avatar
Manish Singh committed
127
                  G_OBJECT_CLASS_TYPE (object_class),
128 129 130
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
                  NULL, NULL,
131
                  _gtk_marshal_VOID__OBJECT,
Manish Singh's avatar
Manish Singh committed
132
                  G_TYPE_NONE,
133
                  1,
134
                  GTK_TYPE_TEXT_TAG);
135

136 137 138 139 140
  /**
   * GtkTextTagTable::tag-removed:
   * @texttagtable: the object which received the signal.
   * @tag: the removed tag.
   */
141
  signals[TAG_REMOVED] =
142
    g_signal_new (I_("tag-removed"),  
Manish Singh's avatar
Manish Singh committed
143
                  G_OBJECT_CLASS_TYPE (object_class),
144 145 146
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
                  NULL, NULL,
147
                  _gtk_marshal_VOID__OBJECT,
Manish Singh's avatar
Manish Singh committed
148
                  G_TYPE_NONE,
149
                  1,
150
                  GTK_TYPE_TEXT_TAG);
151 152
}

153
static void
154 155
gtk_text_tag_table_init (GtkTextTagTable *table)
{
156 157
  table->priv = gtk_text_tag_table_get_instance_private (table);
  table->priv->hash = g_hash_table_new (g_str_hash, g_str_equal);
158 159
}

160 161
/**
 * gtk_text_tag_table_new:
162
 * 
163 164 165
 * Creates a new #GtkTextTagTable. The table contains no tags by
 * default.
 * 
166
 * Returns: a new #GtkTextTagTable
167
 **/
168 169 170 171 172
GtkTextTagTable*
gtk_text_tag_table_new (void)
{
  GtkTextTagTable *table;

Manish Singh's avatar
Manish Singh committed
173
  table = g_object_new (GTK_TYPE_TEXT_TAG_TABLE, NULL);
174

175 176 177
  return table;
}

178 179 180
static void
foreach_unref (GtkTextTag *tag, gpointer data)
{
181
  GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (tag->priv->table);
182
  GtkTextTagTablePrivate *priv = table->priv;
183 184
  GSList *l;

185 186 187
  /* We don't want to emit the remove signal here; so we just unparent
   * and unref the tag.
   */
188

189 190 191 192
  for (l = priv->buffers; l != NULL; l = l->next)
    _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (l->data),
                                             tag);

193
  tag->priv->table = NULL;
Manish Singh's avatar
Manish Singh committed
194
  g_object_unref (tag);
195 196
}

197 198 199
static void
gtk_text_tag_table_finalize (GObject *object)
{
200
  GtkTextTagTable *table = GTK_TEXT_TAG_TABLE (object);
201
  GtkTextTagTablePrivate *priv = table->priv;
202

203
  gtk_text_tag_table_foreach (table, foreach_unref, NULL);
204

205 206 207
  g_hash_table_destroy (priv->hash);
  g_slist_free (priv->anonymous);
  g_slist_free (priv->buffers);
208 209

  G_OBJECT_CLASS (gtk_text_tag_table_parent_class)->finalize (object);
210 211
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static void
gtk_text_tag_table_buildable_interface_init (GtkBuildableIface   *iface)
{
  iface->add_child = gtk_text_tag_table_buildable_add_child;
}

static void
gtk_text_tag_table_buildable_add_child (GtkBuildable        *buildable,
					GtkBuilder          *builder,
					GObject             *child,
					const gchar         *type)
{
  if (type && strcmp (type, "tag") == 0)
    gtk_text_tag_table_add (GTK_TEXT_TAG_TABLE (buildable),
			    GTK_TEXT_TAG (child));
}

229 230 231 232 233 234 235
/**
 * 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
236 237 238
 *
 * @tag must not be in a tag table already, and may not have
 * the same name as an already-added tag.
239 240
 *
 * Returns: %TRUE on success.
241
 **/
242
gboolean
243 244
gtk_text_tag_table_add (GtkTextTagTable *table,
                        GtkTextTag      *tag)
245
{
246
  GtkTextTagTablePrivate *priv;
247
  guint size;
248

249 250 251
  g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), FALSE);
  g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), FALSE);
  g_return_val_if_fail (tag->priv->table == NULL, FALSE);
252

253 254
  priv = table->priv;

255
  if (tag->priv->name && g_hash_table_lookup (priv->hash, tag->priv->name))
Havoc Pennington's avatar
Havoc Pennington committed
256 257
    {
      g_warning ("A tag named '%s' is already in the tag table.",
258
                 tag->priv->name);
259
      return FALSE;
Havoc Pennington's avatar
Havoc Pennington committed
260 261
    }
  
Manish Singh's avatar
Manish Singh committed
262
  g_object_ref (tag);
263

264 265
  if (tag->priv->name)
    g_hash_table_insert (priv->hash, tag->priv->name, tag);
266 267
  else
    {
268
      priv->anonymous = g_slist_prepend (priv->anonymous, tag);
269
      priv->anon_count++;
270
    }
271

272
  tag->priv->table = table;
273 274 275 276

  /* 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. */
277
  size = gtk_text_tag_table_get_size (table);
278
  g_assert (size > 0);
279
  tag->priv->priority = size - 1;
280

Manish Singh's avatar
Manish Singh committed
281
  g_signal_emit (table, signals[TAG_ADDED], 0, tag);
282
  return TRUE;
283 284
}

285 286 287 288 289 290 291
/**
 * gtk_text_tag_table_lookup:
 * @table: a #GtkTextTagTable 
 * @name: name of a tag
 * 
 * Look up a named tag.
 * 
292 293
 * Returns: (nullable) (transfer none): The tag, or %NULL if none by that
 * name is in the table.
294
 **/
295
GtkTextTag*
296 297
gtk_text_tag_table_lookup (GtkTextTagTable *table,
                           const gchar     *name)
298
{
299
  GtkTextTagTablePrivate *priv;
300

301 302 303
  g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), NULL);
  g_return_val_if_fail (name != NULL, NULL);

304 305 306
  priv = table->priv;

  return g_hash_table_lookup (priv->hash, name);
307 308
}

309 310 311 312
/**
 * gtk_text_tag_table_remove:
 * @table: a #GtkTextTagTable
 * @tag: a #GtkTextTag
313 314
 *
 * Remove a tag from the table. If a #GtkTextBuffer has @table as its tag table,
315
 * the tag is removed from the buffer. The table’s reference to the tag is
316
 * removed, so the tag will end up destroyed if you don’t have a reference to
317
 * it.
318
 **/
319
void
320 321
gtk_text_tag_table_remove (GtkTextTagTable *table,
                           GtkTextTag      *tag)
322
{
323
  GtkTextTagTablePrivate *priv;
324 325
  GSList *l;

326
  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
327
  g_return_if_fail (GTK_IS_TEXT_TAG (tag));
328
  g_return_if_fail (tag->priv->table == table);
329

330 331
  priv = table->priv;

332 333 334
  /* Our little bad hack to be sure buffers don't still have the tag
   * applied to text in the buffer
   */
335 336 337 338
  for (l = priv->buffers; l != NULL; l = l->next)
    _gtk_text_buffer_notify_will_remove_tag (GTK_TEXT_BUFFER (l->data),
                                             tag);

339 340 341
  /* 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. */
342
  gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
343

344
  tag->priv->table = NULL;
345

346 347
  if (tag->priv->name)
    g_hash_table_remove (priv->hash, tag->priv->name);
348 349
  else
    {
350
      priv->anonymous = g_slist_remove (priv->anonymous, tag);
351
      priv->anon_count--;
352
    }
353

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

Manish Singh's avatar
Manish Singh committed
356
  g_object_unref (tag);
357 358
}

359 360 361 362 363 364 365 366 367 368 369
struct ForeachData
{
  GtkTextTagTableForeach func;
  gpointer data;
};

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

370
  g_return_if_fail (GTK_IS_TEXT_TAG (value));
371

372 373 374
  (* fd->func) (value, fd->data);
}

375 376 377 378 379 380
static void
list_foreach (gpointer data, gpointer user_data)
{
  struct ForeachData *fd = user_data;

  g_return_if_fail (GTK_IS_TEXT_TAG (data));
381

382 383 384
  (* fd->func) (data, fd->data);
}

385 386 387
/**
 * gtk_text_tag_table_foreach:
 * @table: a #GtkTextTagTable
388
 * @func: (scope call): a function to call on each tag
389 390 391
 * @data: user data
 *
 * Calls @func on each tag in @table, with user data @data.
392
 * Note that the table may not be modified while iterating 
393
 * over it (you can’t add/remove tags).
394
 **/
395
void
396 397 398
gtk_text_tag_table_foreach (GtkTextTagTable       *table,
                            GtkTextTagTableForeach func,
                            gpointer               data)
399
{
400
  GtkTextTagTablePrivate *priv;
401
  struct ForeachData d;
402 403 404

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

406 407
  priv = table->priv;

408 409
  d.func = func;
  d.data = data;
410

411 412
  g_hash_table_foreach (priv->hash, hash_foreach, &d);
  g_slist_foreach (priv->anonymous, list_foreach, &d);
413 414
}

415
/**
416
 * gtk_text_tag_table_get_size:
417 418 419 420
 * @table: a #GtkTextTagTable
 * 
 * Returns the size of the table (number of tags)
 * 
421
 * Returns: number of tags in @table
422 423
 **/
gint
424
gtk_text_tag_table_get_size (GtkTextTagTable *table)
425
{
426
  GtkTextTagTablePrivate *priv;
427

428
  g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
429

430 431 432
  priv = table->priv;

  return g_hash_table_size (priv->hash) + priv->anon_count;
433
}
434 435 436 437 438

void
_gtk_text_tag_table_add_buffer (GtkTextTagTable *table,
                                gpointer         buffer)
{
439
  GtkTextTagTablePrivate *priv = table->priv;
440

441
  priv->buffers = g_slist_prepend (priv->buffers, buffer);
442 443
}

444 445 446 447 448 449 450 451 452 453
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);
}

454 455 456 457
void
_gtk_text_tag_table_remove_buffer (GtkTextTagTable *table,
                                   gpointer         buffer)
{
458
  GtkTextTagTablePrivate *priv = table->priv;
459

460
  gtk_text_tag_table_foreach (table, foreach_remove_tag, buffer);
461 462

  priv->buffers = g_slist_remove (priv->buffers, buffer);
463
}