gtktextbuffer.c 111 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* GTK - The GIMP Toolkit
 * gtktextbuffer.c Copyright (C) 2000 Red Hat, Inc.
 *
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 13 14 15 16 17 18 19 20 21 22 23
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * 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
24
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 26
 */

27

28
#include <string.h>
29
#include <stdarg.h>
30

31
#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
32
#include "gtkclipboard.h"
33
#include "gtkinvisible.h"
34
#include "gtkmarshalers.h"
35 36 37
#include "gtktextbuffer.h"
#include "gtktextbtree.h"
#include "gtktextiterprivate.h"
38
#include "gtkintl.h"
39

40 41 42 43 44 45 46 47
typedef struct _ClipboardRequest ClipboardRequest;

struct _ClipboardRequest
{
  GtkTextBuffer *buffer;
  gboolean interactive;
  gboolean default_editable;
  gboolean is_clipboard;
48
  gboolean replace_selection;
49 50
};

51 52
enum {
  INSERT_TEXT,
53
  INSERT_PIXBUF,
54
  INSERT_CHILD_ANCHOR,
55
  DELETE_RANGE,
56 57 58 59 60 61
  CHANGED,
  MODIFIED_CHANGED,
  MARK_SET,
  MARK_DELETED,
  APPLY_TAG,
  REMOVE_TAG,
62 63
  BEGIN_USER_ACTION,
  END_USER_ACTION,
64 65 66 67
  LAST_SIGNAL
};

enum {
68 69 70 71
  PROP_0,

  /* Construct */
  PROP_TAG_TABLE
72 73 74 75 76 77
};

enum {
  TARGET_STRING,
  TARGET_TEXT,
  TARGET_COMPOUND_TEXT,
78 79
  TARGET_UTF8_STRING,
  TARGET_TEXT_BUFFER_CONTENTS
80 81 82 83 84 85 86 87 88 89
};

static void gtk_text_buffer_init       (GtkTextBuffer      *tkxt_buffer);
static void gtk_text_buffer_class_init (GtkTextBufferClass *klass);
static void gtk_text_buffer_finalize   (GObject            *object);


static void gtk_text_buffer_real_insert_text           (GtkTextBuffer     *buffer,
                                                        GtkTextIter       *iter,
                                                        const gchar       *text,
90
                                                        gint               len);
91 92 93
static void gtk_text_buffer_real_insert_pixbuf         (GtkTextBuffer     *buffer,
                                                        GtkTextIter       *iter,
                                                        GdkPixbuf         *pixbuf);
94 95 96
static void gtk_text_buffer_real_insert_anchor         (GtkTextBuffer     *buffer,
                                                        GtkTextIter       *iter,
                                                        GtkTextChildAnchor *anchor);
97
static void gtk_text_buffer_real_delete_range          (GtkTextBuffer     *buffer,
98
                                                        GtkTextIter       *start,
99
                                                        GtkTextIter       *end);
100 101 102 103 104 105 106 107
static void gtk_text_buffer_real_apply_tag             (GtkTextBuffer     *buffer,
                                                        GtkTextTag        *tag,
                                                        const GtkTextIter *start_char,
                                                        const GtkTextIter *end_char);
static void gtk_text_buffer_real_remove_tag            (GtkTextBuffer     *buffer,
                                                        GtkTextTag        *tag,
                                                        const GtkTextIter *start_char,
                                                        const GtkTextIter *end_char);
108
static void gtk_text_buffer_real_changed               (GtkTextBuffer     *buffer);
109

110
static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
111
static void          free_log_attr_cache (GtkTextLogAttrCache *cache);
112

113 114 115 116
static void remove_all_clipboard_contents_buffers (GtkTextBuffer *buffer);
static void remove_all_selection_clipboards       (GtkTextBuffer *buffer);
static void update_selection_clipboards           (GtkTextBuffer *buffer);

117
static GObjectClass *parent_class = NULL;
118 119
static guint signals[LAST_SIGNAL] = { 0 };

120 121 122 123 124 125 126 127 128 129
static void gtk_text_buffer_set_property (GObject         *object,
				          guint            prop_id,
				          const GValue    *value,
				          GParamSpec      *pspec);
static void gtk_text_buffer_get_property (GObject         *object,
				          guint            prop_id,
				          GValue          *value,
				          GParamSpec      *pspec);


130
GType
131 132
gtk_text_buffer_get_type (void)
{
133
  static GType our_type = 0;
134 135 136

  if (our_type == 0)
    {
137
      static const GTypeInfo our_info =
138 139
      {
        sizeof (GtkTextBufferClass),
140 141 142 143 144 145 146 147
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_text_buffer_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkTextBuffer),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_text_buffer_init
148 149
      };

Manish Singh's avatar
Manish Singh committed
150 151
      our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextBuffer",
                                         &our_info, 0);
152 153 154 155 156 157 158 159
    }

  return our_type;
}

static void
gtk_text_buffer_class_init (GtkTextBufferClass *klass)
{
160
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
161

162
  parent_class = g_type_class_peek_parent (klass);
163

164
  object_class->finalize = gtk_text_buffer_finalize;
165 166 167
  object_class->set_property = gtk_text_buffer_set_property;
  object_class->get_property = gtk_text_buffer_get_property;
 
168
  klass->insert_text = gtk_text_buffer_real_insert_text;
169
  klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
Havoc Pennington's avatar
Havoc Pennington committed
170
  klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
171
  klass->delete_range = gtk_text_buffer_real_delete_range;
172 173 174 175
  klass->apply_tag = gtk_text_buffer_real_apply_tag;
  klass->remove_tag = gtk_text_buffer_real_remove_tag;
  klass->changed = gtk_text_buffer_real_changed;

176 177 178 179 180 181 182 183 184
  /* Construct */
  g_object_class_install_property (object_class,
                                   PROP_TAG_TABLE,
                                   g_param_spec_object ("tag_table",
                                                        _("Tag Table"),
                                                        _("Text Tag Table"),
                                                        GTK_TYPE_TEXT_TAG_TABLE,
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

185
  signals[INSERT_TEXT] =
186 187 188 189 190
    g_signal_new ("insert_text",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
                  NULL, NULL,
191
                  _gtk_marshal_VOID__BOXED_STRING_INT,
Manish Singh's avatar
Manish Singh committed
192
                  G_TYPE_NONE,
193 194
                  3,
                  GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
Manish Singh's avatar
Manish Singh committed
195 196
                  G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
                  G_TYPE_INT);
197

198
  signals[INSERT_PIXBUF] =
199 200 201 202 203
    g_signal_new ("insert_pixbuf",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
                  NULL, NULL,
204
                  _gtk_marshal_VOID__BOXED_OBJECT,
Manish Singh's avatar
Manish Singh committed
205
                  G_TYPE_NONE,
206 207 208
                  2,
                  GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
                  GDK_TYPE_PIXBUF);
209 210

  signals[INSERT_CHILD_ANCHOR] =
211 212 213 214 215
    g_signal_new ("insert_child_anchor",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
                  NULL, NULL,
216
                  _gtk_marshal_VOID__BOXED_OBJECT,
Manish Singh's avatar
Manish Singh committed
217
                  G_TYPE_NONE,
218 219 220
                  2,
                  GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
                  GTK_TYPE_TEXT_CHILD_ANCHOR);
221 222
  
  signals[DELETE_RANGE] =
223 224 225 226 227
    g_signal_new ("delete_range",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
                  NULL, NULL,
228
                  _gtk_marshal_VOID__BOXED_BOXED,
Manish Singh's avatar
Manish Singh committed
229
                  G_TYPE_NONE,
230 231 232
                  2,
                  GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
                  GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
233 234

  signals[CHANGED] =
235 236 237 238 239
    g_signal_new ("changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,                   
                  G_STRUCT_OFFSET (GtkTextBufferClass, changed),
                  NULL, NULL,
240
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
241
                  G_TYPE_NONE,
242
                  0);
243 244

  signals[MODIFIED_CHANGED] =
245 246 247 248 249
    g_signal_new ("modified_changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
                  NULL, NULL,
250
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
251
                  G_TYPE_NONE,
252
                  0);
253

254
  signals[MARK_SET] =
255 256 257 258 259
    g_signal_new ("mark_set",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,                   
                  G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
                  NULL, NULL,
260
                  _gtk_marshal_VOID__BOXED_OBJECT,
Manish Singh's avatar
Manish Singh committed
261
                  G_TYPE_NONE,
262 263 264
                  2,
                  GTK_TYPE_TEXT_ITER,
                  GTK_TYPE_TEXT_MARK);
265 266

  signals[MARK_DELETED] =
267 268 269 270 271
    g_signal_new ("mark_deleted",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,                   
                  G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
                  NULL, NULL,
272
                  _gtk_marshal_VOID__OBJECT,
Manish Singh's avatar
Manish Singh committed
273
                  G_TYPE_NONE,
274 275
                  1,
                  GTK_TYPE_TEXT_MARK);
276
  
277
  signals[APPLY_TAG] =
278 279 280 281 282
    g_signal_new ("apply_tag",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
                  NULL, NULL,
283
                  _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
Manish Singh's avatar
Manish Singh committed
284
                  G_TYPE_NONE,
285 286 287 288
                  3,
                  GTK_TYPE_TEXT_TAG,
                  GTK_TYPE_TEXT_ITER,
                  GTK_TYPE_TEXT_ITER);
289

290
  signals[REMOVE_TAG] =
291 292 293 294 295
    g_signal_new ("remove_tag",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
                  NULL, NULL,
296
                  _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
Manish Singh's avatar
Manish Singh committed
297
                  G_TYPE_NONE,
298 299 300 301
                  3,
                  GTK_TYPE_TEXT_TAG,
                  GTK_TYPE_TEXT_ITER,
                  GTK_TYPE_TEXT_ITER);
302 303

  signals[BEGIN_USER_ACTION] =
304 305 306 307 308
    g_signal_new ("begin_user_action",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,                   
                  G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
                  NULL, NULL,
309
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
310
                  G_TYPE_NONE,
311
                  0);
312 313

  signals[END_USER_ACTION] =
314 315 316 317 318
    g_signal_new ("end_user_action",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,                   
                  G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
                  NULL, NULL,
319
                  _gtk_marshal_VOID__VOID,
Manish Singh's avatar
Manish Singh committed
320
                  G_TYPE_NONE,
321
                  0);  
322 323
}

324
static void
325 326
gtk_text_buffer_init (GtkTextBuffer *buffer)
{
327
  buffer->clipboard_contents_buffers = NULL;
328 329 330 331 332 333 334 335 336 337 338
  buffer->tag_table = NULL;
}

static void
set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
{
  g_return_if_fail (buffer->tag_table == NULL);

  if (table)
    {
      buffer->tag_table = table;
339
      g_object_ref (buffer->tag_table);
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
      _gtk_text_tag_table_add_buffer (table, buffer);
    }
}

static GtkTextTagTable*
get_table (GtkTextBuffer *buffer)
{
  if (buffer->tag_table == NULL)
    {
      buffer->tag_table = gtk_text_tag_table_new ();
      _gtk_text_tag_table_add_buffer (buffer->tag_table, buffer);
    }

  return buffer->tag_table;
}

static void
gtk_text_buffer_set_property (GObject         *object,
                              guint            prop_id,
                              const GValue    *value,
                              GParamSpec      *pspec)
{
  GtkTextBuffer *text_buffer;

  text_buffer = GTK_TEXT_BUFFER (object);

  switch (prop_id)
    {
    case PROP_TAG_TABLE:
      set_table (text_buffer, g_value_get_object (value));
      break;

    default:
      break;
    }
}

static void
gtk_text_buffer_get_property (GObject         *object,
                              guint            prop_id,
                              GValue          *value,
                              GParamSpec      *pspec)
{
  GtkTextBuffer *text_buffer;

  text_buffer = GTK_TEXT_BUFFER (object);

  switch (prop_id)
    {
    case PROP_TAG_TABLE:
      g_value_set_object (value, get_table (text_buffer));
      break;

    default:
      break;
    }
396 397
}

Havoc Pennington's avatar
Havoc Pennington committed
398 399 400
/**
 * gtk_text_buffer_new:
 * @table: a tag table, or NULL to create a new one
401
 *
Havoc Pennington's avatar
Havoc Pennington committed
402
 * Creates a new text buffer.
403
 *
Havoc Pennington's avatar
Havoc Pennington committed
404 405
 * Return value: a new text buffer
 **/
406 407 408 409
GtkTextBuffer*
gtk_text_buffer_new (GtkTextTagTable *table)
{
  GtkTextBuffer *text_buffer;
410

Manish Singh's avatar
Manish Singh committed
411
  text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag_table", table, NULL);
412

413 414 415 416
  return text_buffer;
}

static void
417
gtk_text_buffer_finalize (GObject *object)
418 419 420 421 422
{
  GtkTextBuffer *buffer;

  buffer = GTK_TEXT_BUFFER (object);

423 424 425
  remove_all_clipboard_contents_buffers (buffer);
  remove_all_selection_clipboards (buffer);

426 427
  if (buffer->tag_table)
    {
428
      _gtk_text_tag_table_remove_buffer (buffer->tag_table, buffer);
Manish Singh's avatar
Manish Singh committed
429
      g_object_unref (buffer->tag_table);
430 431 432 433 434
      buffer->tag_table = NULL;
    }

  if (buffer->btree)
    {
435
      _gtk_text_btree_unref (buffer->btree);
436 437
      buffer->btree = NULL;
    }
438 439 440 441 442

  if (buffer->log_attr_cache)
    free_log_attr_cache (buffer->log_attr_cache);

  buffer->log_attr_cache = NULL;
443
  
444 445 446
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

447 448 449 450
static GtkTextBTree*
get_btree (GtkTextBuffer *buffer)
{
  if (buffer->btree == NULL)
451
    buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
452
                                         buffer);
453

454 455 456 457 458 459 460 461 462
  return buffer->btree;
}

GtkTextBTree*
_gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
{
  return get_btree (buffer);
}

Havoc Pennington's avatar
Havoc Pennington committed
463 464 465
/**
 * gtk_text_buffer_get_tag_table:
 * @buffer: a #GtkTextBuffer
466
 *
Havoc Pennington's avatar
Havoc Pennington committed
467
 * Get the #GtkTextTagTable associated with this buffer.
468
 *
Havoc Pennington's avatar
Havoc Pennington committed
469 470
 * Return value: the buffer's tag table
 **/
471 472 473
GtkTextTagTable*
gtk_text_buffer_get_tag_table (GtkTextBuffer  *buffer)
{
474
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
475 476 477 478

  return get_table (buffer);
}

479 480 481 482 483
/**
 * gtk_text_buffer_set_text:
 * @buffer: a #GtkTextBuffer
 * @text: UTF-8 text to insert
 * @len: length of @text in bytes
484
 *
485 486
 * Deletes current contents of @buffer, and inserts @text instead. If
 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
487 488 489 490 491 492 493
 **/
void
gtk_text_buffer_set_text (GtkTextBuffer *buffer,
                          const gchar   *text,
                          gint           len)
{
  GtkTextIter start, end;
494 495

  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
496 497 498 499 500 501
  g_return_if_fail (text != NULL);

  if (len < 0)
    len = strlen (text);

  gtk_text_buffer_get_bounds (buffer, &start, &end);
502

503 504 505 506 507 508 509 510 511
  gtk_text_buffer_delete (buffer, &start, &end);

  if (len > 0)
    {
      gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
      gtk_text_buffer_insert (buffer, &start, text, len);
    }
}

512 513 514 515 516
/*
 * Insertion
 */

static void
517 518 519
gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
                                  GtkTextIter *iter,
                                  const gchar *text,
520
                                  gint len)
521
{
522 523
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (iter != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
524
  
525
  _gtk_text_btree_insert (iter, text, len);
526

Manish Singh's avatar
Manish Singh committed
527
  g_signal_emit (buffer, signals[CHANGED], 0);
528 529 530
}

static void
531
gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
532 533
                             GtkTextIter   *iter,
                             const gchar   *text,
534
                             gint           len)
535
{
536 537 538
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (iter != NULL);
  g_return_if_fail (text != NULL);
539 540

  if (len < 0)
541 542
    len = strlen (text);

543
  g_return_if_fail (g_utf8_validate (text, len, NULL));
544
  
545 546
  if (len > 0)
    {
Manish Singh's avatar
Manish Singh committed
547
      g_signal_emit (buffer, signals[INSERT_TEXT], 0,
548
                     iter, text, len);
549 550 551
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
552 553 554 555 556 557
/**
 * gtk_text_buffer_insert:
 * @buffer: a #GtkTextBuffer
 * @iter: a position in the buffer
 * @text: UTF-8 format text to insert
 * @len: length of text in bytes, or -1
558
 *
Havoc Pennington's avatar
Havoc Pennington committed
559 560 561 562 563 564 565
 * Inserts @len bytes of @text at position @iter.  If @len is -1,
 * @text must be nul-terminated and will be inserted in its
 * entirety. Emits the "insert_text" signal; insertion actually occurs
 * in the default handler for the signal. @iter is invalidated when
 * insertion occurs (because the buffer contents change), but the
 * default signal handler revalidates it to point to the end of the
 * inserted text.
566
 *
Havoc Pennington's avatar
Havoc Pennington committed
567
 **/
568 569 570 571 572 573
void
gtk_text_buffer_insert (GtkTextBuffer *buffer,
                        GtkTextIter *iter,
                        const gchar *text,
                        gint len)
{
574 575 576
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (iter != NULL);
  g_return_if_fail (text != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
577 578
  g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
  
579
  gtk_text_buffer_emit_insert (buffer, iter, text, len);
580 581
}

Havoc Pennington's avatar
Havoc Pennington committed
582 583 584 585 586
/**
 * gtk_text_buffer_insert_at_cursor:
 * @buffer: a #GtkTextBuffer
 * @text: some text in UTF-8 format
 * @len: length of text, in bytes
587 588
 *
 * Simply calls gtk_text_buffer_insert (), using the current
Havoc Pennington's avatar
Havoc Pennington committed
589 590
 * cursor position as the insertion point.
 **/
591 592 593 594 595 596 597
void
gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
                                  const gchar *text,
                                  gint len)
{
  GtkTextIter iter;

598 599
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (text != NULL);
600

601 602 603
  gtk_text_buffer_get_iter_at_mark (buffer, &iter,
                                    gtk_text_buffer_get_mark (buffer,
                                                              "insert"));
604

605
  gtk_text_buffer_insert (buffer, &iter, text, len);
606 607
}

Havoc Pennington's avatar
Havoc Pennington committed
608 609 610 611 612 613
/**
 * gtk_text_buffer_insert_interactive:
 * @buffer: a #GtkTextBuffer
 * @iter: a position in @buffer
 * @text: some UTF-8 text
 * @len: length of text in bytes, or -1
614
 * @default_editable: default editability of buffer
615 616
 *
 * Like gtk_text_buffer_insert (), but the insertion will not occur if
Havoc Pennington's avatar
Havoc Pennington committed
617 618 619
 * @iter is at a non-editable location in the buffer.  Usually you
 * want to prevent insertions at ineditable locations if the insertion
 * results from a user action (is interactive).
620
 *
621 622 623 624
 * @default_editable indicates the editability of text that doesn't
 * have a tag affecting editability applied to it. Typically the
 * result of gtk_text_view_get_editable() is appropriate here.
 *
Havoc Pennington's avatar
Havoc Pennington committed
625 626
 * Return value: whether text was actually inserted
 **/
627
gboolean
628 629 630 631 632 633 634 635
gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
                                    GtkTextIter   *iter,
                                    const gchar   *text,
                                    gint           len,
                                    gboolean       default_editable)
{
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
  g_return_val_if_fail (text != NULL, FALSE);
Havoc Pennington's avatar
Havoc Pennington committed
636
  g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
637

638
  if (gtk_text_iter_can_insert (iter, default_editable))
639
    {
640 641 642
      gtk_text_buffer_begin_user_action (buffer);
      gtk_text_buffer_emit_insert (buffer, iter, text, len);
      gtk_text_buffer_end_user_action (buffer);
643 644 645 646
      return TRUE;
    }
  else
    return FALSE;
647 648
}

Havoc Pennington's avatar
Havoc Pennington committed
649 650 651 652 653 654
/**
 * gtk_text_buffer_insert_interactive_at_cursor:
 * @buffer: a #GtkTextBuffer
 * @text: text in UTF-8 format
 * @len: length of text in bytes, or -1
 * @default_editable: default editability of buffer
655 656
 *
 * Calls gtk_text_buffer_insert_interactive () at the cursor
Havoc Pennington's avatar
Havoc Pennington committed
657
 * position.
658
 *
659 660 661 662
 * @default_editable indicates the editability of text that doesn't
 * have a tag affecting editability applied to it. Typically the
 * result of gtk_text_view_get_editable() is appropriate here.
 * 
Havoc Pennington's avatar
Havoc Pennington committed
663 664
 * Return value: whether text was actually inserted
 **/
665 666 667 668 669
gboolean
gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
                                              const gchar   *text,
                                              gint           len,
                                              gboolean       default_editable)
670
{
671
  GtkTextIter iter;
672

673 674 675 676 677 678
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
  g_return_val_if_fail (text != NULL, FALSE);

  gtk_text_buffer_get_iter_at_mark (buffer, &iter,
                                    gtk_text_buffer_get_mark (buffer,
                                                              "insert"));
679

680 681
  return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
                                             default_editable);
682 683
}

684 685 686 687
static gboolean
possibly_not_text (gunichar ch,
                   gpointer user_data)
{
688
  return ch == GTK_TEXT_UNKNOWN_CHAR;
689 690 691 692 693 694 695 696 697 698 699 700 701
}

static void
insert_text_range (GtkTextBuffer     *buffer,
                   GtkTextIter       *iter,
                   const GtkTextIter *orig_start,
                   const GtkTextIter *orig_end,
                   gboolean           interactive)
{
  gchar *text;

  text = gtk_text_iter_get_text (orig_start, orig_end);

702
  gtk_text_buffer_emit_insert (buffer, iter, text, -1);
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

  g_free (text);
}

typedef struct _Range Range;
struct _Range
{
  GtkTextBuffer *buffer;
  GtkTextMark *start_mark;
  GtkTextMark *end_mark;
  GtkTextMark *whole_end_mark;
  GtkTextIter *range_start;
  GtkTextIter *range_end;
  GtkTextIter *whole_end;
};

static Range*
save_range (GtkTextIter *range_start,
            GtkTextIter *range_end,
            GtkTextIter *whole_end)
{
  Range *r;

  r = g_new (Range, 1);

  r->buffer = gtk_text_iter_get_buffer (range_start);
Manish Singh's avatar
Manish Singh committed
729
  g_object_ref (r->buffer);
730 731 732 733 734
  
  r->start_mark = 
    gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
                                 NULL,
                                 range_start,
735
                                 FALSE);
736 737 738 739
  r->end_mark = 
    gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
                                 NULL,
                                 range_end,
740
                                 TRUE);
741 742 743 744 745

  r->whole_end_mark = 
    gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
                                 NULL,
                                 whole_end,
746
                                 TRUE);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767

  r->range_start = range_start;
  r->range_end = range_end;
  r->whole_end = whole_end;

  return r;
}

static void
restore_range (Range *r)
{
  gtk_text_buffer_get_iter_at_mark (r->buffer,
                                    r->range_start,
                                    r->start_mark);
      
  gtk_text_buffer_get_iter_at_mark (r->buffer,
                                    r->range_end,
                                    r->end_mark);
      
  gtk_text_buffer_get_iter_at_mark (r->buffer,
                                    r->whole_end,
768 769
                                    r->whole_end_mark);  
  
770 771 772 773
  gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
  gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
  gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);

774 775 776 777 778 779 780 781 782 783 784
  /* Due to the gravities on the marks, the ordering could have
   * gotten mangled; we switch to an empty range in that
   * case
   */
  
  if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
    *r->range_start = *r->range_end;

  if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
    *r->range_end = *r->whole_end;
  
Manish Singh's avatar
Manish Singh committed
785
  g_object_unref (r->buffer);
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
  g_free (r); 
}

static void
insert_range_untagged (GtkTextBuffer     *buffer,
                       GtkTextIter       *iter,
                       const GtkTextIter *orig_start,
                       const GtkTextIter *orig_end,
                       gboolean           interactive)
{
  GtkTextIter range_start;
  GtkTextIter range_end;
  GtkTextIter start, end;
  GtkTextBuffer *src_buffer;
  Range *r;
  
  if (gtk_text_iter_equal (orig_start, orig_end))
    return;

  start = *orig_start;
  end = *orig_end;
  
  src_buffer = gtk_text_iter_get_buffer (&start);
  
  range_start = start;
  range_end = start;
  
  while (TRUE)
    {
      if (gtk_text_iter_equal (&range_start, &range_end))
        {
          /* Figure out how to move forward */

          g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
          
          if (gtk_text_iter_equal (&range_end, &end))
            {
              /* nothing left to do */
              break;
            }
826
          else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
            {
              GdkPixbuf *pixbuf = NULL;
              GtkTextChildAnchor *anchor = NULL;
              pixbuf = gtk_text_iter_get_pixbuf (&range_end);
              anchor = gtk_text_iter_get_child_anchor (&range_end);

              if (pixbuf)
                {
                  r = save_range (&range_start,
                                  &range_end,
                                  &end);

                  gtk_text_buffer_insert_pixbuf (buffer,
                                                 iter,
                                                 pixbuf);

                  restore_range (r);
                  r = NULL;
                  
846
                  gtk_text_iter_forward_char (&range_end);
847 848 849 850 851 852 853
                  
                  range_start = range_end;
                }
              else if (anchor)
                {
                  /* Just skip anchors */

854
                  gtk_text_iter_forward_char (&range_end);
855 856 857 858
                  range_start = range_end;
                }
              else
                {
859
                  /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
                   * keep going. 
                   */
                  gtk_text_iter_forward_find_char (&range_end,
                                                   possibly_not_text, NULL,
                                                   &end);
                  
                  g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
                }
            }
          else
            {
              /* Text segment starts here, so forward search to
               * find its possible endpoint
               */
              gtk_text_iter_forward_find_char (&range_end,
                                               possibly_not_text, NULL,
                                               &end);
              
              g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
            }
        }
      else
        {
          r = save_range (&range_start,
                          &range_end,
                          &end);
          
          insert_text_range (buffer,
                             iter,
                             &range_start,
                             &range_end,
                             interactive);

          restore_range (r);
          r = NULL;
          
          range_start = range_end;
        }
    }
}

static void
902 903 904 905 906
insert_range_not_inside_self (GtkTextBuffer     *buffer,
                              GtkTextIter       *iter,
                              const GtkTextIter *orig_start,
                              const GtkTextIter *orig_end,
                              gboolean           interactive)
907 908 909 910 911 912 913 914 915 916 917 918
{
  /* Find each range of uniformly-tagged text, insert it,
   * then apply the tags.
   */
  GtkTextIter start = *orig_start;
  GtkTextIter end = *orig_end;
  GtkTextIter range_start;
  GtkTextIter range_end;
  GtkTextBuffer *src_buffer;
  
  if (gtk_text_iter_equal (orig_start, orig_end))
    return;
919
  
920 921
  src_buffer = gtk_text_iter_get_buffer (orig_start);
  
922
  gtk_text_iter_order (&start, &end);
923 924

  range_start = start;
925
  range_end = start;  
926
  
927 928 929 930 931 932
  while (TRUE)
    {
      gint start_offset;
      GtkTextIter start_iter;
      GSList *tags;
      GSList *tmp_list;
933
      Range *r;
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
      
      if (gtk_text_iter_equal (&range_start, &end))
        break; /* All done */

      g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
      
      gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);

      g_assert (!gtk_text_iter_equal (&range_start, &range_end));

      /* Clamp to the end iterator */
      if (gtk_text_iter_compare (&range_end, &end) > 0)
        range_end = end;
      
      /* We have a range with unique tags; insert it, and
       * apply all tags.
       */
      start_offset = gtk_text_iter_get_offset (iter);

      r = save_range (&range_start, &range_end, &end);
      
      insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);

      restore_range (r);
      r = NULL;
      
      gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
      
      tags = gtk_text_iter_get_tags (&range_start);
      tmp_list = tags;
      while (tmp_list != NULL)
        {
          gtk_text_buffer_apply_tag (buffer,
                                     tmp_list->data,
                                     &start_iter,
                                     iter);
          
          tmp_list = g_slist_next (tmp_list);
        }
      g_slist_free (tags);

      range_start = range_end;
    }
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
}

static void
gtk_text_buffer_real_insert_range (GtkTextBuffer     *buffer,
                                   GtkTextIter       *iter,
                                   const GtkTextIter *orig_start,
                                   const GtkTextIter *orig_end,
                                   gboolean           interactive)
{
  GtkTextBuffer *src_buffer;
  
  /* Find each range of uniformly-tagged text, insert it,
   * then apply the tags.
   */  
  if (gtk_text_iter_equal (orig_start, orig_end))
    return;

  if (interactive)
    gtk_text_buffer_begin_user_action (buffer);
  
  src_buffer = gtk_text_iter_get_buffer (orig_start);
  
  if (gtk_text_iter_get_buffer (iter) != src_buffer ||
      !gtk_text_iter_in_range (iter, orig_start, orig_end))
    {
      insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
    }
  else
    {
      /* If you insert a range into itself, it could loop infinitely
       * because the region being copied keeps growing as we insert. So
       * we have to separately copy the range before and after
       * the insertion point.
       */
      GtkTextIter start = *orig_start;
      GtkTextIter end = *orig_end;
      GtkTextIter range_start;
      GtkTextIter range_end;
      Range *first_half;
      Range *second_half;

      gtk_text_iter_order (&start, &end);
      
      range_start = start;
      range_end = *iter;
      first_half = save_range (&range_start, &range_end, &end);

      range_start = *iter;
      range_end = end;
      second_half = save_range (&range_start, &range_end, &end);
1027

1028 1029 1030 1031 1032 1033 1034
      restore_range (first_half);
      insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);

      restore_range (second_half);
      insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
    }
  
1035 1036
  if (interactive)
    gtk_text_buffer_end_user_action (buffer);
1037
}
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

/**
 * gtk_text_buffer_insert_range:
 * @buffer: a #GtkTextBuffer
 * @iter: a position in @buffer
 * @start: a position in a #GtkTextBuffer
 * @end: another position in the same buffer as @start
 *
 * Copies text, tags, and pixbufs between @start and @end (the order
 * of @start and @end doesn't matter) and inserts the copy at @iter.
 * Used instead of simply getting/inserting text because it preserves
1049 1050
 * images and tags. If @start and @end are in a different buffer from
 * @buffer, the two buffers must share the same tag table.
1051
 *
1052 1053
 * Implemented via emissions of the insert_text and apply_tag signals,
 * so expect those.
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
 **/
void
gtk_text_buffer_insert_range (GtkTextBuffer     *buffer,
                              GtkTextIter       *iter,
                              const GtkTextIter *start,
                              const GtkTextIter *end)
{
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (iter != NULL);
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);
1065
  g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1066
                    gtk_text_iter_get_buffer (end));
1067
  g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1068
                    buffer->tag_table);  
Havoc Pennington's avatar
Havoc Pennington committed
1069 1070
  g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
  
1071
  gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1072 1073
}

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
/**
 * gtk_text_buffer_insert_range_interactive:
 * @buffer: a #GtkTextBuffer
 * @iter: a position in @buffer
 * @start: a position in a #GtkTextBuffer
 * @end: another position in the same buffer as @start
 * @default_editable: default editability of the buffer
 *
 * Same as gtk_text_buffer_insert_range(), but does nothing if the
 * insertion point isn't editable. The @default_editable parameter
 * indicates whether the text is editable at @iter if no tags
 * enclosing @iter affect editability. Typically the result of
 * gtk_text_view_get_editable() is appropriate here.
 *
 * Returns: whether an insertion was possible at @iter
 **/
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
gboolean
gtk_text_buffer_insert_range_interactive (GtkTextBuffer     *buffer,
                                          GtkTextIter       *iter,
                                          const GtkTextIter *start,
                                          const GtkTextIter *end,
                                          gboolean           default_editable)
{
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (start != NULL, FALSE);
  g_return_val_if_fail (end != NULL, FALSE);
1101
  g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1102
                        gtk_text_iter_get_buffer (end), FALSE);
1103
  g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1104 1105 1106
                        buffer->tag_table, FALSE);


1107
  if (gtk_text_iter_can_insert (iter, default_editable))
1108 1109 1110 1111 1112 1113
    {
      gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
      return TRUE;
    }
  else
    return FALSE;
1114 1115 1116 1117 1118 1119 1120
}

/**
 * gtk_text_buffer_insert_with_tags:
 * @buffer: a #GtkTextBuffer
 * @iter: an iterator in @buffer
 * @text: UTF-8 text
1121
 * @len: length of @text, or -1
1122
 * @first_tag: first tag to apply to @text
1123 1124
 * @Varargs: NULL-terminated list of tags to apply
 *
1125 1126
 * Inserts @text into @buffer at @iter, applying the list of tags to
 * the newly-inserted text. The last tag specified must be NULL to
1127 1128 1129
 * terminate the list. Equivalent to calling gtk_text_buffer_insert (),
 * then gtk_text_buffer_apply_tag () on the inserted text;
 * gtk_text_buffer_insert_with_tags () is just a convenience function.
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
 **/
void
gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
                                  GtkTextIter   *iter,
                                  const gchar   *text,
                                  gint           len,
                                  GtkTextTag    *first_tag,
                                  ...)
{
  gint start_offset;
  GtkTextIter start;
  va_list args;
  GtkTextTag *tag;
1143 1144

  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1145 1146
  g_return_if_fail (iter != NULL);
  g_return_if_fail (text != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1147 1148
  g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
  
1149
  start_offset = gtk_text_iter_get_offset (iter);
1150

1151 1152 1153 1154
  gtk_text_buffer_insert (buffer, iter, text, len);

  if (first_tag == NULL)
    return;
1155

1156
  gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1157

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
  va_start (args, first_tag);
  tag = first_tag;
  while (tag)
    {
      gtk_text_buffer_apply_tag (buffer, tag, &start, iter);

      tag = va_arg (args, GtkTextTag*);
    }

  va_end (args);
}

/**
 * gtk_text_buffer_insert_with_tags_by_name:
 * @buffer: a #GtkTextBuffer
 * @iter: position in @buffer
 * @text: UTF-8 text
 * @len: length of @text, or -1
 * @first_tag_name: name of a tag to apply to @text
 * @Varargs: more tag names
1178 1179
 *
 * Same as gtk_text_buffer_insert_with_tags (), but allows you
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
 * to pass in tag names instead of tag objects.
 **/
void
gtk_text_buffer_insert_with_tags_by_name  (GtkTextBuffer *buffer,
                                           GtkTextIter   *iter,
                                           const gchar   *text,
                                           gint           len,
                                           const gchar   *first_tag_name,
                                           ...)
{
  gint start_offset;
  GtkTextIter start;
  va_list args;
  const gchar *tag_name;
1194 1195

  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1196 1197
  g_return_if_fail (iter != NULL);
  g_return_if_fail (text != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1198 1199
  g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
  
1200
  start_offset = gtk_text_iter_get_offset (iter);
1201

1202 1203 1204 1205
  gtk_text_buffer_insert (buffer, iter, text, len);

  if (first_tag_name == NULL)
    return;
1206

1207
  gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1208

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
  va_start (args, first_tag_name);
  tag_name = first_tag_name;
  while (tag_name)
    {
      GtkTextTag *tag;

      tag = gtk_text_tag_table_lookup (buffer->tag_table,
                                       tag_name);

      if (tag == NULL)
        {
          g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
          return;
        }
1223

1224 1225 1226 1227 1228 1229 1230 1231 1232
      gtk_text_buffer_apply_tag (buffer, tag, &start, iter);

      tag_name = va_arg (args, const gchar*);
    }

  va_end (args);
}


1233 1234 1235 1236 1237
/*
 * Deletion
 */

static void
1238 1239 1240
gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
                                   GtkTextIter   *start,
                                   GtkTextIter   *end)
1241
{
1242 1243 1244 1245
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

1246
  _gtk_text_btree_delete (start, end);
1247 1248

  /* may have deleted the selection... */
1249
  update_selection_clipboards (buffer);
1250

Manish Singh's avatar
Manish Singh committed
1251
  g_signal_emit (buffer, signals[CHANGED], 0);
1252 1253 1254
}

static void
1255 1256
gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
                             GtkTextIter *start,
1257
                             GtkTextIter *end)
1258
{
1259 1260 1261
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);
1262

1263
  if (gtk_text_iter_equal (start, end))
1264 1265
    return;

1266
  gtk_text_iter_order (start, end);
Havoc Pennington's avatar
Havoc Pennington committed
1267

Manish Singh's avatar
Manish Singh committed
1268
  g_signal_emit (buffer,
1269
                 signals[DELETE_RANGE],
1270
                 0,
1271
                 start, end);
1272 1273
}

Havoc Pennington's avatar
Havoc Pennington committed
1274 1275 1276 1277 1278 1279 1280
/**
 * gtk_text_buffer_delete:
 * @buffer: a #GtkTextBuffer
 * @start: a position in @buffer
 * @end: another position in @buffer
 *
 * Deletes text between @start and @end. The order of @start and @end
1281
 * is not actually relevant; gtk_text_buffer_delete () will reorder
1282
 * them. This function actually emits the "delete_range" signal, and
Havoc Pennington's avatar
Havoc Pennington committed
1283 1284 1285 1286 1287 1288
 * the default handler of that signal deletes the text. Because the
 * buffer is modified, all outstanding iterators become invalid after
 * calling this function; however, the @start and @end will be
 * re-initialized to point to the location where text was deleted.
 *
 **/
1289 1290
void
gtk_text_buffer_delete (GtkTextBuffer *buffer,
1291 1292
                        GtkTextIter   *start,
                        GtkTextIter   *end)
1293
{
1294 1295 1296
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1297 1298 1299
  g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
  g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
  
1300
  gtk_text_buffer_emit_delete (buffer, start, end);
1301 1302
}

Havoc Pennington's avatar
Havoc Pennington committed
1303 1304 1305 1306 1307
/**
 * gtk_text_buffer_delete_interactive:
 * @buffer: a #GtkTextBuffer
 * @start_iter: start of range to delete
 * @end_iter: end of range
1308 1309
 * @default_editable: whether the buffer is editable by default
 *
Havoc Pennington's avatar
Havoc Pennington committed
1310
 * Deletes all <emphasis>editable</emphasis> text in the given range.
1311
 * Calls gtk_text_buffer_delete () for each editable sub-range of
1312 1313 1314
 * [@start,@end). @start and @end are revalidated to point to
 * the location of the last deleted range, or left untouched if
 * no text was deleted.
1315
 *
Havoc Pennington's avatar
Havoc Pennington committed
1316 1317
 * Return value: whether some text was actually deleted
 **/
1318 1319 1320 1321 1322
gboolean
gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
                                    GtkTextIter   *start_iter,
                                    GtkTextIter   *end_iter,
                                    gboolean       default_editable)
1323
{
1324 1325 1326 1327 1328 1329 1330
  GtkTextMark *end_mark;
  GtkTextMark *start_mark;
  GtkTextIter iter;
  gboolean current_state;
  gboolean deleted_stuff = FALSE;

  /* Delete all editable text in the range start_iter, end_iter */
1331 1332

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1333 1334
  g_return_val_if_fail (start_iter != NULL, FALSE);
  g_return_val_if_fail (end_iter != NULL, FALSE);
Havoc Pennington's avatar
Havoc Pennington committed
1335 1336
  g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
  g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1337

Havoc Pennington's avatar
Havoc Pennington committed
1338
  
1339 1340
  gtk_text_buffer_begin_user_action (buffer);
  
1341
  gtk_text_iter_order (start_iter, end_iter);
1342 1343

  start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1344
                                            start_iter, TRUE);
1345
  end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1346 1347
                                          end_iter, FALSE);
  iter = *start_iter;
1348

1349
  current_state = gtk_text_iter_editable (&iter, default_editable);
1350

1351 1352 1353 1354 1355
  while (TRUE)
    {
      gboolean new_state;
      gboolean done = FALSE;
      GtkTextIter end;
1356

1357
      gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1358

1359
      gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1360

1361 1362 1363 1364 1365
      if (gtk_text_iter_compare (&iter, &end) >= 0)
        {
          done = TRUE;
          iter = end; /* clamp to the last boundary */
        }
1366

1367
      new_state = gtk_text_iter_editable (&iter, default_editable);
1368

1369 1370 1371 1372 1373 1374 1375 1376
      if (current_state == new_state)
        {
          if (done)
            {
              if (current_state)
                {
                  /* We're ending an editable region. Delete said region. */
                  GtkTextIter start;
1377

1378
                  gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1379

1380
                  gtk_text_buffer_emit_delete (buffer, &start, &iter);
1381

1382
                  deleted_stuff = TRUE;
1383 1384 1385 1386

                  /* revalidate user's iterators. */
                  *start_iter = start;
                  *end_iter = iter;
1387 1388 1389 1390 1391 1392 1393
                }

              break;
            }
          else
            continue;
        }
1394

1395 1396 1397 1398
      if (current_state && !new_state)
        {
          /* End of an editable region. Delete it. */
          GtkTextIter start;
1399

1400
          gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1401

1402
          gtk_text_buffer_emit_delete (buffer, &start, &iter);
1403

1404 1405
          current_state = FALSE;
          deleted_stuff = TRUE;
1406 1407 1408 1409

          /* revalidate user's iterators. */
          *start_iter = start;
          *end_iter = iter;
1410 1411 1412 1413 1414 1415
        }
      else
        {
          /* We are at the start of an editable region. We won't be deleting
           * the previous region. Move start mark to start of this region.
           */
1416

1417 1418
          g_assert (!current_state && new_state);

1419 1420
          gtk_text_buffer_move_mark (buffer, start_mark,
                                     &iter);
1421 1422


1423 1424 1425 1426 1427 1428 1429
          current_state = TRUE;
        }

      if (done)
        break;
    }

1430

1431 1432
  gtk_text_buffer_delete_mark (buffer, start_mark);
  gtk_text_buffer_delete_mark (buffer, end_mark);
1433

1434 1435
  gtk_text_buffer_end_user_action (buffer);
  
1436
  return deleted_stuff;
1437 1438 1439 1440 1441 1442
}

/*
 * Extracting textual buffer contents
 */

Havoc Pennington's avatar
Havoc Pennington committed
1443 1444 1445 1446 1447 1448
/**
 * gtk_text_buffer_get_text:
 * @buffer: a #GtkTextBuffer
 * @start: start of a range
 * @end: end of a range
 * @include_hidden_chars: whether to include invisible text
1449
 *
Havoc Pennington's avatar
Havoc Pennington committed
1450 1451 1452 1453 1454 1455
 * Returns the text in the range [@start,@end). Excludes undisplayed
 * text (text marked with tags that set the invisibility attribute) if
 * @include_hidden_chars is FALSE. Does not include characters
 * representing embedded images, so byte and character indexes into
 * the returned string do <emphasis>not</emphasis> correspond to byte
 * and character indexes into the buffer. Contrast with
1456 1457
 * gtk_text_buffer_get_slice ().
 *
Havoc Pennington's avatar
Havoc Pennington committed
1458 1459
 * Return value: an allocated UTF-8 string
 **/
1460 1461 1462 1463 1464 1465
gchar*
gtk_text_buffer_get_text (GtkTextBuffer      *buffer,
                          const GtkTextIter *start,
                          const GtkTextIter *end,
                          gboolean             include_hidden_chars)
{
1466 1467 1468
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (start != NULL, NULL);
  g_return_val_if_fail (end != NULL, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1469 1470 1471
  g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
  g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
  
1472
  if (include_hidden_chars)
1473
    return gtk_text_iter_get_text (start, end);
1474
  else
1475
    return gtk_text_iter_get_visible_text (start, end);
1476 1477
}

Havoc Pennington's avatar
Havoc Pennington committed
1478 1479 1480 1481 1482
/**
 * gtk_text_buffer_get_slice:
 * @buffer: a #GtkTextBuffer
 * @start: start of a range
 * @end: end of a range
1483
 * @include_hidden_chars: whether to include invisible text
Havoc Pennington's avatar
Havoc Pennington committed
1484 1485 1486 1487
 *
 * Returns the text in the range [@start,@end). Excludes undisplayed
 * text (text marked with tags that set the invisibility attribute) if
 * @include_hidden_chars is FALSE. The returned string includes a
1488
 * 0xFFFC character whenever the buffer contains
Havoc Pennington's avatar
Havoc Pennington committed
1489 1490 1491
 * embedded images, so byte and character indexes into
 * the returned string <emphasis>do</emphasis> correspond to byte
 * and character indexes into the buffer. Contrast with
1492
 * gtk_text_buffer_get_text (). Note that 0xFFFC can occur in normal
1493 1494
 * text as well, so it is not a reliable indicator that a pixbuf or
 * widget is in the buffer.
1495
 *
Havoc Pennington's avatar
Havoc Pennington committed
1496 1497
 * Return value: an allocated UTF-8 string
 **/
1498 1499 1500 1501 1502 1503
gchar*
gtk_text_buffer_get_slice (GtkTextBuffer      *buffer,
                           const GtkTextIter *start,
                           const GtkTextIter *end,
                           gboolean             include_hidden_chars)
{
1504 1505 1506
  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (start != NULL, NULL);
  g_return_val_if_fail (end != NULL, NULL);
Havoc Pennington's avatar
Havoc Pennington committed
1507 1508 1509
  g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
  g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
  
1510
  if (include_hidden_chars)
1511
    return gtk_text_iter_get_slice (start, end);
1512
  else
1513
    return gtk_text_iter_get_visible_slice (start, end);
1514 1515 1516
}

/*
1517
 * Pixbufs
1518 1519
 */

1520 1521 1522 1523
static void
gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer     *buffer,
                                    GtkTextIter       *iter,
                                    GdkPixbuf         *pixbuf)
Havoc Pennington's avatar
Havoc Pennington committed
1524
{ 
1525 1526
  _gtk_text_btree_insert_pixbuf (iter, pixbuf);

Manish Singh's avatar
Manish Singh committed
1527
  g_signal_emit (buffer, signals[CHANGED], 0);
1528 1529
}

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
/**
 * gtk_text_buffer_insert_pixbuf:
 * @buffer: a #GtkTextBuffer
 * @iter: location to insert the pixbuf
 * @pixbuf: a #GdkPixbuf
 *
 * Inserts an image into the text buffer at @iter. The image will be
 * counted as one character in character counts, and when obtaining
 * the buffer contents as a string, will be represented by the Unicode
 * "object replacement character" 0xFFFC. Note that the "slice"
 * variants for obtaining portions of the buffer as a string include
 * this character for pixbufs, but the "text" variants do
 * not. e.g. see gtk_text_buffer_get_slice() and
 * gtk_text_buffer_get_text().
 * 
 **/
1546
void
Havoc Pennington's avatar
Havoc Pennington committed
1547 1548 1549
gtk_text_buffer