gimptextbuffer.c 40.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * GimpTextBuffer
 * Copyright (C) 2010  Michael Natterer <mitch@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

23
#include <stdlib.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <glib/gstdio.h>

#include <glib.h>

#ifdef G_OS_WIN32
#include "libgimpbase/gimpwin32-io.h"
#endif

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpbase/gimpbase.h"
45
#include "libgimpcolor/gimpcolor.h"
46 47 48 49

#include "widgets-types.h"

#include "gimptextbuffer.h"
50
#include "gimptextbuffer-serialize.h"
51
#include "gimptexttag.h"
52
#include "gimpwidgets-utils.h"
53 54 55 56 57 58 59 60 61 62 63 64

#include "gimp-intl.h"


/*  local function prototypes  */

static GObject * gimp_text_buffer_constructor (GType                  type,
                                               guint                  n_params,
                                               GObjectConstructParam *params);
static void      gimp_text_buffer_dispose     (GObject               *object);
static void      gimp_text_buffer_finalize    (GObject               *object);

65 66 67 68
static void      gimp_text_buffer_mark_set    (GtkTextBuffer         *buffer,
                                               const GtkTextIter     *location,
                                               GtkTextMark           *mark);

69 70 71 72 73 74 75 76 77

G_DEFINE_TYPE (GimpTextBuffer, gimp_text_buffer, GTK_TYPE_TEXT_BUFFER)

#define parent_class gimp_text_buffer_parent_class


static void
gimp_text_buffer_class_init (GimpTextBufferClass *klass)
{
78 79
  GObjectClass       *object_class = G_OBJECT_CLASS (klass);
  GtkTextBufferClass *buffer_class = GTK_TEXT_BUFFER_CLASS (klass);
80 81 82 83

  object_class->constructor = gimp_text_buffer_constructor;
  object_class->dispose     = gimp_text_buffer_dispose;
  object_class->finalize    = gimp_text_buffer_finalize;
84 85

  buffer_class->mark_set    = gimp_text_buffer_mark_set;
86 87 88 89 90
}

static void
gimp_text_buffer_init (GimpTextBuffer *buffer)
{
91 92 93 94 95 96 97 98 99 100
  buffer->markup_atom =
    gtk_text_buffer_register_serialize_format (GTK_TEXT_BUFFER (buffer),
                                               "application/x-gimp-pango-markup",
                                               gimp_text_buffer_serialize,
                                               NULL, NULL);

  gtk_text_buffer_register_deserialize_format (GTK_TEXT_BUFFER (buffer),
                                               "application/x-gimp-pango-markup",
                                               gimp_text_buffer_deserialize,
                                               NULL, NULL);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}

static GObject *
gimp_text_buffer_constructor (GType                  type,
                              guint                  n_params,
                              GObjectConstructParam *params)
{
  GObject        *object;
  GimpTextBuffer *buffer;

  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);

  buffer = GIMP_TEXT_BUFFER (object);

  gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), "", -1);

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  buffer->bold_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                 "bold",
                                                 "weight", PANGO_WEIGHT_BOLD,
                                                 NULL);

  buffer->italic_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                   "italic",
                                                   "style", PANGO_STYLE_ITALIC,
                                                   NULL);

  buffer->underline_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                      "underline",
                                                      "underline", PANGO_UNDERLINE_SINGLE,
                                                      NULL);

  buffer->strikethrough_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                          "strikethrough",
                                                          "strikethrough", TRUE,
                                                          NULL);

137 138 139 140 141 142 143 144 145 146 147 148 149 150
  return object;
}

static void
gimp_text_buffer_dispose (GObject *object)
{
  /* GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object); */

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
gimp_text_buffer_finalize (GObject *object)
{
151 152
  GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object);

153 154 155 156 157 158
  if (buffer->size_tags)
    {
      g_list_free (buffer->size_tags);
      buffer->size_tags = NULL;
    }

159 160 161 162 163 164
  if (buffer->baseline_tags)
    {
      g_list_free (buffer->baseline_tags);
      buffer->baseline_tags = NULL;
    }

165
  if (buffer->kerning_tags)
166
    {
167 168
      g_list_free (buffer->kerning_tags);
      buffer->kerning_tags = NULL;
169 170
    }

171 172 173 174 175 176
  if (buffer->font_tags)
    {
      g_list_free (buffer->font_tags);
      buffer->font_tags = NULL;
    }

177 178 179 180 181 182
  if (buffer->color_tags)
    {
      g_list_free (buffer->color_tags);
      buffer->color_tags = NULL;
    }

183
  gimp_text_buffer_clear_insert_tags (buffer);
184 185 186 187

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

188 189 190 191 192 193 194 195 196 197
static void
gimp_text_buffer_mark_set (GtkTextBuffer     *buffer,
                           const GtkTextIter *location,
                           GtkTextMark       *mark)
{
  gimp_text_buffer_clear_insert_tags (GIMP_TEXT_BUFFER (buffer));

  GTK_TEXT_BUFFER_CLASS (parent_class)->mark_set (buffer, location, mark);
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

/*  public functions  */

GimpTextBuffer *
gimp_text_buffer_new (void)
{
  return g_object_new (GIMP_TYPE_TEXT_BUFFER, NULL);
}

void
gimp_text_buffer_set_text (GimpTextBuffer *buffer,
                           const gchar    *text)
{
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));

  if (text == NULL)
    text = "";

  gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
217 218

  gimp_text_buffer_clear_insert_tags (buffer);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
}

gchar *
gimp_text_buffer_get_text (GimpTextBuffer *buffer)
{
  GtkTextIter start, end;

  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);

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

  return gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer),
                                   &start, &end, TRUE);
}

234 235 236 237 238 239 240 241 242 243
void
gimp_text_buffer_set_markup (GimpTextBuffer *buffer,
                             const gchar    *markup)
{
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));

  gimp_text_buffer_set_text (buffer, NULL);

  if (markup)
    {
244 245 246 247
      GtkTextTagTable *tag_table;
      GtkTextBuffer   *content;
      GtkTextIter      insert;
      GError          *error = NULL;
248

249 250
      tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (buffer));
      content = gtk_text_buffer_new (tag_table);
251 252

      gtk_text_buffer_get_start_iter (content, &insert);
253 254

      if (! gtk_text_buffer_deserialize (GTK_TEXT_BUFFER (buffer),
255
                                         content,
256
                                         buffer->markup_atom,
257
                                         &insert,
258 259 260 261 262 263
                                         (const guint8 *) markup, -1,
                                         &error))
        {
          g_printerr ("EEK: %s\n", error->message);
          g_clear_error (&error);
        }
264 265 266 267
      else
        {
          GtkTextIter start, end;

268 269
          gimp_text_buffer_post_deserialize (buffer, content);

270 271 272 273 274 275 276 277
          gtk_text_buffer_get_bounds (content, &start, &end);
          gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &insert);

          gtk_text_buffer_insert_range (GTK_TEXT_BUFFER (buffer),
                                        &insert, &start, &end);
        }

      g_object_unref (content);
278
    }
279 280

  gimp_text_buffer_clear_insert_tags (buffer);
281 282 283 284 285
}

gchar *
gimp_text_buffer_get_markup (GimpTextBuffer *buffer)
{
286 287 288 289 290 291
  GtkTextTagTable *tag_table;
  GtkTextBuffer   *content;
  GtkTextIter      insert;
  GtkTextIter      start, end;
  gchar           *markup;
  gsize            length;
292 293 294

  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);

295 296
  tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (buffer));
  content = gtk_text_buffer_new (tag_table);
297

298
  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
299 300 301 302
  gtk_text_buffer_get_start_iter (content, &insert);

  gtk_text_buffer_insert_range (content, &insert, &start, &end);

303 304
  gimp_text_buffer_pre_serialize (buffer, content);

305 306 307 308 309 310 311 312 313
  gtk_text_buffer_get_bounds (content, &start, &end);

  markup = (gchar *) gtk_text_buffer_serialize (GTK_TEXT_BUFFER (buffer),
                                                content,
                                                buffer->markup_atom,
                                                &start, &end,
                                                &length);

  g_object_unref (content);
314

315
  return markup;
316 317
}

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
gboolean
gimp_text_buffer_has_markup (GimpTextBuffer *buffer)
{
  GtkTextIter iter;

  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), FALSE);

  gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);

  do
    {
      GSList *tags = gtk_text_iter_get_tags (&iter);

      if (tags)
        {
          g_slist_free (tags);
          return TRUE;
        }
    }
  while (gtk_text_iter_forward_char (&iter));

  return FALSE;
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355
GtkTextTag *
gimp_text_buffer_get_iter_size (GimpTextBuffer    *buffer,
                                const GtkTextIter *iter,
                                gint              *size)
{
  GList *list;

  for (list = buffer->size_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
          if (size)
356
            *size = gimp_text_tag_get_size (tag);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

          return tag;
        }
    }

  if (size)
    *size = 0;

  return NULL;
}

static GtkTextTag *
gimp_text_buffer_get_size_tag (GimpTextBuffer *buffer,
                               gint            size)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

  for (list = buffer->size_tags; list; list = g_list_next (list))
    {
      tag = list->data;

380
      if (size == gimp_text_tag_get_size (tag))
381 382 383 384 385 386 387
        return tag;
    }

  g_snprintf (name, sizeof (name), "size-%d", size);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
388
                                    GIMP_TEXT_PROP_NAME_SIZE, size,
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
                                    NULL);

  buffer->size_tags = g_list_prepend (buffer->size_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_size (GimpTextBuffer    *buffer,
                           const GtkTextIter *start,
                           const GtkTextIter *end,
                           gint               size)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->size_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (size != 0)
    {
      GtkTextTag *tag;

      tag = gimp_text_buffer_get_size_tag (buffer, size);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

void
gimp_text_buffer_change_size (GimpTextBuffer    *buffer,
                              const GtkTextIter *start,
                              const GtkTextIter *end,
                              gint               count)
{
  GtkTextIter  iter;
  GtkTextIter  span_start;
  GtkTextIter  span_end;
  GtkTextTag  *span_tag;
  gint         span_size;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  iter       = *start;
  span_start = *start;
  span_tag   = gimp_text_buffer_get_iter_size (buffer, &iter,
                                               &span_size);

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  do
    {
      GtkTextTag *iter_tag;
      gint        iter_size;

      gtk_text_iter_forward_char (&iter);

      iter_tag = gimp_text_buffer_get_iter_size (buffer, &iter,
                                                 &iter_size);

      span_end = iter;

      if (iter_size != span_size ||
          gtk_text_iter_compare (&iter, end) >= 0)
        {
          if (span_size != 0)
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

          if ((span_size + count) > 0)
            {
              span_tag = gimp_text_buffer_get_size_tag (buffer,
                                                        span_size + count);

              gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                         &span_start, &span_end);
            }

          span_start = iter;
          span_size  = iter_size;
          span_tag   = iter_tag;
        }

      /* We might have moved too far */
      if (gtk_text_iter_compare (&iter, end) > 0)
        iter = *end;
    }
  while (! gtk_text_iter_equal (&iter, end));

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

502 503 504 505
GtkTextTag *
gimp_text_buffer_get_iter_baseline (GimpTextBuffer    *buffer,
                                    const GtkTextIter *iter,
                                    gint              *baseline)
506 507 508 509 510 511 512 513 514
{
  GList *list;

  for (list = buffer->baseline_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
515
          if (baseline)
516
            *baseline = gimp_text_tag_get_baseline (tag);
517

518
          return tag;
519 520 521
        }
    }

522 523
  if (baseline)
    *baseline = 0;
524

525
  return NULL;
526 527 528
}

static GtkTextTag *
529 530
gimp_text_buffer_get_baseline_tag (GimpTextBuffer *buffer,
                                   gint            baseline)
531 532 533 534 535 536 537 538 539
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

  for (list = buffer->baseline_tags; list; list = g_list_next (list))
    {
      tag = list->data;

540
      if (baseline == gimp_text_tag_get_baseline (tag))
541 542 543 544 545 546 547
        return tag;
    }

  g_snprintf (name, sizeof (name), "baseline-%d", baseline);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
548
                                    GIMP_TEXT_PROP_NAME_BASELINE, baseline,
549 550 551 552 553 554 555
                                    NULL);

  buffer->baseline_tags = g_list_prepend (buffer->baseline_tags, tag);

  return tag;
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
void
gimp_text_buffer_set_baseline (GimpTextBuffer    *buffer,
                               const GtkTextIter *start,
                               const GtkTextIter *end,
                               gint               baseline)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->baseline_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (baseline != 0)
    {
      GtkTextTag *tag;

      tag = gimp_text_buffer_get_baseline_tag (buffer, baseline);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

592 593 594 595 596 597 598 599 600 601
void
gimp_text_buffer_change_baseline (GimpTextBuffer    *buffer,
                                  const GtkTextIter *start,
                                  const GtkTextIter *end,
                                  gint               count)
{
  GtkTextIter  iter;
  GtkTextIter  span_start;
  GtkTextIter  span_end;
  GtkTextTag  *span_tag;
602
  gint         span_baseline;
603 604 605 606 607

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

608
  if (gtk_text_iter_equal (start, end))
609 610
    return;

611 612 613 614
  iter       = *start;
  span_start = *start;
  span_tag   = gimp_text_buffer_get_iter_baseline (buffer, &iter,
                                                   &span_baseline);
615

616 617
  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

618 619 620
  do
    {
      GtkTextTag *iter_tag;
621
      gint        iter_baseline;
622 623 624

      gtk_text_iter_forward_char (&iter);

625 626
      iter_tag = gimp_text_buffer_get_iter_baseline (buffer, &iter,
                                                     &iter_baseline);
627 628 629 630

      span_end = iter;

      if (iter_baseline != span_baseline ||
631
          gtk_text_iter_compare (&iter, end) >= 0)
632 633 634 635 636 637 638 639 640
        {
          if (span_baseline != 0)
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

          if (span_baseline + count != 0)
            {
641 642
              span_tag = gimp_text_buffer_get_baseline_tag (buffer,
                                                            span_baseline + count);
643 644 645 646 647 648 649 650 651

              gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                         &span_start, &span_end);
            }

          span_start    = iter;
          span_baseline = iter_baseline;
          span_tag      = iter_tag;
        }
652 653 654 655

      /* We might have moved too far */
      if (gtk_text_iter_compare (&iter, end) > 0)
        iter = *end;
656 657
    }
  while (! gtk_text_iter_equal (&iter, end));
658 659

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
660 661
}

662
GtkTextTag *
663
gimp_text_buffer_get_iter_kerning (GimpTextBuffer    *buffer,
664
                                   const GtkTextIter *iter,
665
                                   gint              *kerning)
666 667 668
{
  GList *list;

669
  for (list = buffer->kerning_tags; list; list = g_list_next (list))
670 671 672 673 674
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
675
          if (kerning)
676
            *kerning = gimp_text_tag_get_kerning (tag);
677

678
          return tag;
679 680 681
        }
    }

682 683
  if (kerning)
    *kerning = 0;
684

685
  return NULL;
686 687 688
}

static GtkTextTag *
689 690
gimp_text_buffer_get_kerning_tag (GimpTextBuffer *buffer,
                                  gint            kerning)
691 692 693 694 695
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

696
  for (list = buffer->kerning_tags; list; list = g_list_next (list))
697 698 699
    {
      tag = list->data;

700
      if (kerning == gimp_text_tag_get_kerning (tag))
701 702 703
        return tag;
    }

704
  g_snprintf (name, sizeof (name), "kerning-%d", kerning);
705 706 707

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
708
                                    GIMP_TEXT_PROP_NAME_KERNING, kerning,
709 710
                                    NULL);

711
  buffer->kerning_tags = g_list_prepend (buffer->kerning_tags, tag);
712 713 714 715

  return tag;
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
void
gimp_text_buffer_set_kerning (GimpTextBuffer    *buffer,
                              const GtkTextIter *start,
                              const GtkTextIter *end,
                              gint               kerning)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->kerning_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (kerning != 0)
    {
      GtkTextTag *tag;

      tag = gimp_text_buffer_get_kerning_tag (buffer, kerning);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

752
void
753
gimp_text_buffer_change_kerning (GimpTextBuffer    *buffer,
754 755 756 757
                                 const GtkTextIter *start,
                                 const GtkTextIter *end,
                                 gint               count)
{
758 759 760 761
  GtkTextIter  iter;
  GtkTextIter  span_start;
  GtkTextIter  span_end;
  GtkTextTag  *span_tag;
762
  gint         span_kerning;
763

764 765 766
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);
767

768 769 770
  if (gtk_text_iter_equal (start, end))
    return;

771 772
  iter       = *start;
  span_start = *start;
773 774
  span_tag   = gimp_text_buffer_get_iter_kerning (buffer, &iter,
                                                  &span_kerning);
775

776 777
  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

778 779 780
  do
    {
      GtkTextTag *iter_tag;
781
      gint        iter_kerning;
782 783 784

      gtk_text_iter_forward_char (&iter);

785 786
      iter_tag = gimp_text_buffer_get_iter_kerning (buffer, &iter,
                                                    &iter_kerning);
787 788 789

      span_end = iter;

790
      if (iter_kerning != span_kerning ||
791 792
          gtk_text_iter_compare (&iter, end) >= 0)
        {
793
          if (span_kerning != 0)
794 795 796 797 798
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

799
          if (span_kerning + count != 0)
800
            {
801 802
              span_tag = gimp_text_buffer_get_kerning_tag (buffer,
                                                           span_kerning + count);
803 804 805 806 807 808

              gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                         &span_start, &span_end);
            }

          span_start   = iter;
809
          span_kerning = iter_kerning;
810 811 812 813 814 815 816 817
          span_tag     = iter_tag;
        }

      /* We might have moved too far */
      if (gtk_text_iter_compare (&iter, end) > 0)
        iter = *end;
    }
  while (! gtk_text_iter_equal (&iter, end));
818 819

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
820 821
}

822 823 824 825
GtkTextTag *
gimp_text_buffer_get_iter_font (GimpTextBuffer     *buffer,
                                const GtkTextIter  *iter,
                                gchar             **font)
826 827 828 829 830 831 832 833 834
{
  GList *list;

  for (list = buffer->font_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
835
          if (font)
836
            *font = gimp_text_tag_get_font (tag);
837

838
          return tag;
839 840 841
        }
    }

842 843
  if (font)
    *font = NULL;
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861

  return NULL;
}

static GtkTextTag *
gimp_text_buffer_get_font_tag (GimpTextBuffer *buffer,
                               const gchar    *font)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];

  for (list = buffer->font_tags; list; list = g_list_next (list))
    {
      gchar *tag_font;

      tag = list->data;

862
      tag_font = gimp_text_tag_get_font (tag);
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 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918

      if (! strcmp (font, tag_font))
        {
          g_free (tag_font);
          return tag;
        }

      g_free (tag_font);
    }

  g_snprintf (name, sizeof (name), "font-%s", font);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
                                    "font", font,
                                    NULL);

  buffer->font_tags = g_list_prepend (buffer->font_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_font (GimpTextBuffer    *buffer,
                           const GtkTextIter *start,
                           const GtkTextIter *end,
                           const gchar       *font)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->font_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (font)
    {
      GtkTextTag *tag = gimp_text_buffer_get_font_tag (buffer, font);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 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 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
GtkTextTag *
gimp_text_buffer_get_iter_color (GimpTextBuffer    *buffer,
                                 const GtkTextIter *iter,
                                 GimpRGB           *color)
{
  GList *list;

  for (list = buffer->color_tags; list; list = g_list_next (list))
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
          if (color)
            gimp_text_tag_get_color (tag, color);

          return tag;
        }
    }

  return NULL;
}

static GtkTextTag *
gimp_text_buffer_get_color_tag (GimpTextBuffer *buffer,
                                const GimpRGB  *color)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
  GdkColor    gdk_color;

  for (list = buffer->color_tags; list; list = g_list_next (list))
    {
      GimpRGB tag_color;

      tag = list->data;

      gimp_text_tag_get_color (tag, &tag_color);

      /* Do not compare the alpha channel, since it's unused */
      if (tag_color.r == color->r &&
          tag_color.g == color->g &&
          tag_color.b == color->b)
        {
          return tag;
        }
    }

  g_snprintf (name, sizeof (name), "color-(%0.f,%0.f,%0.f)",
              color->r, color->g, color->b);

  gimp_rgb_get_gdk_color (color, &gdk_color);

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
                                    "foreground-gdk", &gdk_color,
                                    "foreground-set", TRUE,
                                    NULL);

  buffer->color_tags = g_list_prepend (buffer->color_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_color (GimpTextBuffer    *buffer,
                            const GtkTextIter *start,
                            const GtkTextIter *end,
                            const GimpRGB     *color)
{
  GList *list;

  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);

  if (gtk_text_iter_equal (start, end))
    return;

  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

  for (list = buffer->color_tags; list; list = g_list_next (list))
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (color)
    {
      GtkTextTag *tag = gimp_text_buffer_get_color_tag (buffer, color);

      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
                                 start, end);
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

1018 1019 1020 1021 1022 1023
/*  Pango markup attribute names  */

#define GIMP_TEXT_ATTR_NAME_SIZE     "size"
#define GIMP_TEXT_ATTR_NAME_BASELINE "rise"
#define GIMP_TEXT_ATTR_NAME_KERNING  "letter_spacing"
#define GIMP_TEXT_ATTR_NAME_FONT     "font"
1024
#define GIMP_TEXT_ATTR_NAME_COLOR    "foreground"
1025

1026
const gchar *
1027 1028 1029 1030
gimp_text_buffer_tag_to_name (GimpTextBuffer  *buffer,
                              GtkTextTag      *tag,
                              const gchar    **attribute,
                              gchar          **value)
1031 1032 1033 1034
{
  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), NULL);

1035 1036 1037 1038 1039 1040
  if (attribute)
    *attribute = NULL;

  if (value)
    *value = NULL;

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  if (tag == buffer->bold_tag)
    {
      return "b";
    }
  else if (tag == buffer->italic_tag)
    {
      return "i";
    }
  else if (tag == buffer->underline_tag)
    {
      return "u";
    }
  else if (tag == buffer->strikethrough_tag)
    {
      return "s";
    }
1057 1058 1059
  else if (g_list_find (buffer->size_tags, tag))
    {
      if (attribute)
1060
        *attribute = GIMP_TEXT_ATTR_NAME_SIZE;
1061 1062

      if (value)
1063
        *value = g_strdup_printf ("%d", gimp_text_tag_get_size (tag));
1064 1065 1066

      return "span";
    }
1067 1068 1069
  else if (g_list_find (buffer->baseline_tags, tag))
    {
      if (attribute)
1070
        *attribute = GIMP_TEXT_ATTR_NAME_BASELINE;
1071 1072

      if (value)
1073
        *value = g_strdup_printf ("%d", gimp_text_tag_get_baseline (tag));
1074 1075 1076

      return "span";
    }
1077
  else if (g_list_find (buffer->kerning_tags, tag))
1078 1079
    {
      if (attribute)
1080
        *attribute = GIMP_TEXT_ATTR_NAME_KERNING;
1081 1082

      if (value)
1083
        *value = g_strdup_printf ("%d", gimp_text_tag_get_kerning (tag));
1084 1085 1086

      return "span";
    }
1087 1088 1089
  else if (g_list_find (buffer->font_tags, tag))
    {
      if (attribute)
1090
        *attribute = GIMP_TEXT_ATTR_NAME_FONT;
1091 1092

      if (value)
1093
        *value = gimp_text_tag_get_font (tag);
1094 1095 1096

      return "span";
    }
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  else if (g_list_find (buffer->color_tags, tag))
    {
      if (attribute)
        *attribute = GIMP_TEXT_ATTR_NAME_COLOR;

      if (value)
        {
          GimpRGB color;
          guchar  r, g, b;

          gimp_text_tag_get_color (tag, &color);
          gimp_rgb_get_uchar (&color, &r, &g, &b);

          *value = g_strdup_printf ("#%02x%02x%02x", r, g, b);
        }

      return "span";
    }
1115 1116 1117 1118 1119 1120

  return NULL;
}

GtkTextTag *
gimp_text_buffer_name_to_tag (GimpTextBuffer *buffer,
1121 1122 1123
                              const gchar    *name,
                              const gchar    *attribute,
                              const gchar    *value)
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
{
  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  if (! strcmp (name, "b"))
    {
      return buffer->bold_tag;
    }
  else if (! strcmp (name, "i"))
    {
      return buffer->italic_tag;
    }
  else if (! strcmp (name, "u"))
    {
      return buffer->underline_tag;
    }
  else if (! strcmp (name, "s"))
    {
      return buffer->strikethrough_tag;
    }
1144 1145 1146
  else if (! strcmp (name, "span") &&
           attribute != NULL       &&
           value     != NULL)
1147
    {
1148
      if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_SIZE))
1149 1150 1151
        {
          return gimp_text_buffer_get_size_tag (buffer, atoi (value));
        }
1152
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_BASELINE))
1153
        {
1154
          return gimp_text_buffer_get_baseline_tag (buffer, atoi (value));
1155
        }
1156
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_KERNING))
1157
        {
1158
          return gimp_text_buffer_get_kerning_tag (buffer, atoi (value));
1159
        }
1160
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_FONT))
1161 1162
        {
          return gimp_text_buffer_get_font_tag (buffer, value);
1163
        }
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
      else if (! strcmp (attribute, GIMP_TEXT_ATTR_NAME_COLOR))
        {
          GimpRGB color;
          guint   r, g, b;

          sscanf (value, "#%02x%02x%02x", &r, &g, &b);

          gimp_rgb_set_uchar (&color, r, g, b);

          return gimp_text_buffer_get_color_tag (buffer, &color);
        }
1175
    }
1176 1177 1178 1179

  return NULL;
}

1180 1181
void
gimp_text_buffer_set_insert_tags (GimpTextBuffer *buffer,
1182 1183
                                  GList          *insert_tags,
                                  GList          *remove_tags)
1184
{
1185 1186
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));

1187
  buffer->insert_tags_set = TRUE;
1188 1189 1190 1191 1192

  g_list_free (buffer->insert_tags);
  g_list_free (buffer->remove_tags);
  buffer->insert_tags = insert_tags;
  buffer->remove_tags = remove_tags;
1193 1194 1195 1196 1197 1198 1199
}

void
gimp_text_buffer_clear_insert_tags (GimpTextBuffer *buffer)
{
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));

1200 1201
  buffer->insert_tags_set = FALSE;

1202
  g_list_free (buffer->insert_tags);
1203
  g_list_free (buffer->remove_tags);
1204
  buffer->insert_tags = NULL;
1205
  buffer->remove_tags = NULL;
1206 1207
}

1208 1209 1210 1211
void
gimp_text_buffer_insert (GimpTextBuffer *buffer,
                         const gchar    *text)
{
1212 1213
  GtkTextIter  iter, start;
  gint         start_offset;
1214
  gboolean     insert_tags_set;
1215 1216
  GList       *insert_tags;
  GList       *remove_tags;
1217
  GSList      *tags_off = NULL;
1218

1219 1220
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));

1221 1222 1223 1224 1225
  gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer), &iter,
                                    gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (buffer)));

  start_offset = gtk_text_iter_get_offset (&iter);

1226
  insert_tags_set = buffer->insert_tags_set;
1227 1228 1229
  insert_tags     = buffer->insert_tags;
  remove_tags     = buffer->remove_tags;

1230
  buffer->insert_tags_set = FALSE;
1231 1232
  buffer->insert_tags     = NULL;
  buffer->remove_tags     = NULL;
1233

1234
  tags_off = gtk_text_iter_get_toggled_tags (&iter, FALSE);
1235

1236 1237
  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

1238 1239 1240 1241 1242
  gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &iter, text, -1);

  gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &start,
                                      start_offset);

1243
  if (insert_tags_set)
1244
    {
1245 1246
      GList *list;

1247 1248 1249 1250 1251 1252 1253
      for (list = remove_tags; list; list = g_list_next (list))
        {
          GtkTextTag *tag = list->data;

          gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), tag,
                                      &start, &iter);
        }
1254 1255 1256

      for (list = insert_tags; list; list = g_list_next (list))
        {
1257 1258 1259
          GtkTextTag *tag = list->data;

          gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag,
1260 1261
                                     &start, &iter);
        }
1262
    }
1263 1264

  if (tags_off)
1265
    {
1266
      GSList *slist;
1267

1268
      for (slist = tags_off; slist; slist = g_slist_next (slist))
1269
        {
1270 1271 1272
          GtkTextTag *tag = slist->data;

          if (! g_list_find (remove_tags, tag) &&
1273
              ! g_list_find (buffer->kerning_tags, tag))