gtktexttagtable.c 9.39 KB
Newer Older
1 2

#include "gtktexttagtable.h"
3
#include "gtkmarshalers.h"
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "gtksignal.h"

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

54 55 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 89 90 91 92
    g_signal_new ("tag_added",
                  GTK_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_added),
                  NULL, NULL,
93
                  _gtk_marshal_VOID__OBJECT,
94 95
                  GTK_TYPE_NONE,
                  1,
96
                  GTK_TYPE_TEXT_TAG);
97 98

  signals[TAG_REMOVED] =
99 100 101 102 103
    g_signal_new ("tag_removed",                   
                  GTK_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextTagTableClass, tag_removed),
                  NULL, NULL,
104
                  _gtk_marshal_VOID__OBJECT,
105 106
                  GTK_TYPE_NONE,
                  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;

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

131 132 133
  return table;
}

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

141
  tag->table = NULL;
142 143 144
  g_object_unref (G_OBJECT (tag));
}

145 146 147 148 149
static void
gtk_text_tag_table_finalize (GObject *object)
{
  GtkTextTagTable *table;

150
  table = GTK_TEXT_TAG_TABLE (object);
151

152
  gtk_text_tag_table_foreach (table, foreach_unref, NULL);
153

154
  g_hash_table_destroy (table->hash);
155 156
  g_slist_free (table->anonymous);

157
  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
158 159
}
static void
160 161 162
gtk_text_tag_table_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
Tim Janik's avatar
Tim Janik committed
163
                                 GParamSpec   *pspec)
164 165 166
{
  GtkTextTagTable *table;

167 168
  table = GTK_TEXT_TAG_TABLE (object);

169
  switch (prop_id)
170
    {
171

172
    default:
173
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 175 176 177
      break;
    }
}

178

179
static void
180 181 182
gtk_text_tag_table_get_property (GObject      *object,
                                 guint         prop_id,
                                 GValue       *value,
Tim Janik's avatar
Tim Janik committed
183
                                 GParamSpec   *pspec)
184 185 186
{
  GtkTextTagTable *table;

187
  table = GTK_TEXT_TAG_TABLE (object);
188

189
  switch (prop_id)
190 191 192
    {

    default:
193
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 195 196 197
      break;
    }
}

198 199 200 201 202 203 204
/**
 * 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
205 206 207
 *
 * @tag must not be in a tag table already, and may not have
 * the same name as an already-added tag.
208
 **/
209
void
210 211
gtk_text_tag_table_add (GtkTextTagTable *table,
                        GtkTextTag      *tag)
212 213
{
  guint size;
214

215 216 217 218
  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
219 220 221 222 223 224 225
  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;
    }
  
226
  g_object_ref (G_OBJECT (tag));
227 228

  if (tag->name)
229
    g_hash_table_insert (table->hash, tag->name, tag);
230 231 232 233 234
  else
    {
      table->anonymous = g_slist_prepend (table->anonymous, tag);
      table->anon_count += 1;
    }
235

236 237 238 239 240
  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. */
241
  size = gtk_text_tag_table_get_size (table);
242
  g_assert (size > 0);
243
  tag->priority = size - 1;
244

245
  g_signal_emit (G_OBJECT (table), signals[TAG_ADDED], 0, tag);
246 247
}

248 249 250 251 252 253 254 255 256
/**
 * 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.
 **/
257
GtkTextTag*
258 259
gtk_text_tag_table_lookup (GtkTextTagTable *table,
                           const gchar     *name)
260
{
261 262 263 264
  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);
265 266
}

267 268 269 270 271 272 273 274 275
/**
 * 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.
 **/
276
void
277 278
gtk_text_tag_table_remove (GtkTextTagTable *table,
                           GtkTextTag      *tag)
279
{
280
  g_return_if_fail (GTK_IS_TEXT_TAG_TABLE (table));
281
  g_return_if_fail (GTK_IS_TEXT_TAG (tag));
282
  g_return_if_fail (tag->table == table);
283 284 285 286

  /* 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. */
287
  gtk_text_tag_set_priority (tag, gtk_text_tag_table_get_size (table) - 1);
288

289 290
  tag->table = NULL;

291
  if (tag->name)
292
    g_hash_table_remove (table->hash, tag->name);
293 294 295 296 297
  else
    {
      table->anonymous = g_slist_remove (table->anonymous, tag);
      table->anon_count -= 1;
    }
298

299
  g_signal_emit (G_OBJECT (table), signals[TAG_REMOVED], 0, tag);
300

301
  g_object_unref (G_OBJECT (tag));
302 303
}

304 305 306 307 308 309 310 311 312 313 314
struct ForeachData
{
  GtkTextTagTableForeach func;
  gpointer data;
};

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

315
  g_return_if_fail (GTK_IS_TEXT_TAG (value));
316

317 318 319
  (* fd->func) (value, fd->data);
}

320 321 322 323 324 325
static void
list_foreach (gpointer data, gpointer user_data)
{
  struct ForeachData *fd = user_data;

  g_return_if_fail (GTK_IS_TEXT_TAG (data));
326

327 328 329
  (* fd->func) (data, fd->data);
}

330 331 332 333 334 335 336 337 338
/**
 * 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.
 * 
 **/
339
void
340 341 342
gtk_text_tag_table_foreach (GtkTextTagTable       *table,
                            GtkTextTagTableForeach func,
                            gpointer               data)
343
{
344
  struct ForeachData d;
345 346 347

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

349 350
  d.func = func;
  d.data = data;
351 352

  g_hash_table_foreach (table->hash, hash_foreach, &d);
353
  g_slist_foreach (table->anonymous, list_foreach, &d);
354 355
}

356
/**
357
 * gtk_text_tag_table_get_size:
358 359 360 361 362 363 364
 * @table: a #GtkTextTagTable
 * 
 * Returns the size of the table (number of tags)
 * 
 * Return value: number of tags in @table
 **/
gint
365
gtk_text_tag_table_get_size (GtkTextTagTable *table)
366
{
367
  g_return_val_if_fail (GTK_IS_TEXT_TAG_TABLE (table), 0);
368

369
  return g_hash_table_size (table->hash) + table->anon_count;
370
}