gimptextbuffer.c 48.2 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

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

#include "libgimpbase/gimpbase.h"
29
#include "libgimpcolor/gimpcolor.h"
30 31 32 33

#include "widgets-types.h"

#include "gimptextbuffer.h"
34
#include "gimptextbuffer-serialize.h"
35
#include "gimptexttag.h"
36
#include "gimpwidgets-utils.h"
37 38 39 40

#include "gimp-intl.h"


41 42 43 44 45 46 47
enum
{
  COLOR_APPLIED,
  LAST_SIGNAL
};


48 49
/*  local function prototypes  */

50 51 52
static void   gimp_text_buffer_constructed (GObject           *object);
static void   gimp_text_buffer_dispose     (GObject           *object);
static void   gimp_text_buffer_finalize    (GObject           *object);
53

54 55 56
static void   gimp_text_buffer_mark_set    (GtkTextBuffer     *buffer,
                                            const GtkTextIter *location,
                                            GtkTextMark       *mark);
57

58 59 60 61 62

G_DEFINE_TYPE (GimpTextBuffer, gimp_text_buffer, GTK_TYPE_TEXT_BUFFER)

#define parent_class gimp_text_buffer_parent_class

63 64
static guint buffer_signals[LAST_SIGNAL] = { 0, };

65 66 67 68

static void
gimp_text_buffer_class_init (GimpTextBufferClass *klass)
{
69 70
  GObjectClass       *object_class = G_OBJECT_CLASS (klass);
  GtkTextBufferClass *buffer_class = GTK_TEXT_BUFFER_CLASS (klass);
71

72
  object_class->constructed = gimp_text_buffer_constructed;
73 74
  object_class->dispose     = gimp_text_buffer_dispose;
  object_class->finalize    = gimp_text_buffer_finalize;
75 76

  buffer_class->mark_set    = gimp_text_buffer_mark_set;
77 78 79 80 81 82 83 84 85 86

  buffer_signals[COLOR_APPLIED] =
    g_signal_new ("color-applied",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpTextBufferClass, color_applied),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__BOXED,
                  G_TYPE_NONE, 1,
                  GIMP_TYPE_RGB);
87 88 89 90 91
}

static void
gimp_text_buffer_init (GimpTextBuffer *buffer)
{
92 93 94 95 96 97 98 99 100 101
  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);
102 103
}

104 105
static void
gimp_text_buffer_constructed (GObject *object)
106
{
107
  GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object);
108

109
  G_OBJECT_CLASS (parent_class)->constructed (object);
110 111 112

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

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  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);

128 129 130 131 132
  buffer->preedit_underline_tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                                              "preedit-underline",
                                                              "underline", PANGO_UNDERLINE_SINGLE,
                                                              NULL);

133 134 135 136
  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
}

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)
{
150 151
  GimpTextBuffer *buffer = GIMP_TEXT_BUFFER (object);

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

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

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

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

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

182
  if (buffer->preedit_color_tags)
183
    {
184 185 186 187 188 189 190 191
      g_list_free (buffer->preedit_color_tags);
      buffer->preedit_color_tags = NULL;
    }

  if (buffer->preedit_bg_color_tags)
    {
      g_list_free (buffer->preedit_bg_color_tags);
      buffer->preedit_bg_color_tags = NULL;
192 193
    }

194
  gimp_text_buffer_clear_insert_tags (buffer);
195 196 197 198

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

199 200 201 202 203 204 205 206 207 208
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);
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

/*  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);
228 229

  gimp_text_buffer_clear_insert_tags (buffer);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
}

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

245 246 247 248 249 250 251 252 253 254
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)
    {
255 256 257 258
      GtkTextTagTable *tag_table;
      GtkTextBuffer   *content;
      GtkTextIter      insert;
      GError          *error = NULL;
259

260 261
      tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (buffer));
      content = gtk_text_buffer_new (tag_table);
262 263

      gtk_text_buffer_get_start_iter (content, &insert);
264 265

      if (! gtk_text_buffer_deserialize (GTK_TEXT_BUFFER (buffer),
266
                                         content,
267
                                         buffer->markup_atom,
268
                                         &insert,
269 270 271 272 273 274
                                         (const guint8 *) markup, -1,
                                         &error))
        {
          g_printerr ("EEK: %s\n", error->message);
          g_clear_error (&error);
        }
275 276 277 278
      else
        {
          GtkTextIter start, end;

279 280
          gimp_text_buffer_post_deserialize (buffer, content);

281 282 283 284 285 286 287 288
          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);
289
    }
290 291

  gimp_text_buffer_clear_insert_tags (buffer);
292 293 294 295 296
}

gchar *
gimp_text_buffer_get_markup (GimpTextBuffer *buffer)
{
297 298 299 300 301 302
  GtkTextTagTable *tag_table;
  GtkTextBuffer   *content;
  GtkTextIter      insert;
  GtkTextIter      start, end;
  gchar           *markup;
  gsize            length;
303 304 305

  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);

306 307
  tag_table = gtk_text_buffer_get_tag_table (GTK_TEXT_BUFFER (buffer));
  content = gtk_text_buffer_new (tag_table);
308

309
  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
310 311 312 313
  gtk_text_buffer_get_start_iter (content, &insert);

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

314 315
  gimp_text_buffer_pre_serialize (buffer, content);

316 317 318 319 320 321 322 323 324
  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);
325

326
  return markup;
327 328
}

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
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;
}

353 354 355 356 357 358 359 360 361 362 363 364 365 366
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)
367
            *size = gimp_text_tag_get_size (tag);
368 369 370 371 372 373 374 375 376 377 378

          return tag;
        }
    }

  if (size)
    *size = 0;

  return NULL;
}

379
GtkTextTag *
380 381 382 383 384 385 386 387 388 389 390
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;

391
      if (size == gimp_text_tag_get_size (tag))
392 393 394 395 396 397 398
        return tag;
    }

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

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
399
                                    GIMP_TEXT_PROP_NAME_SIZE, size,
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 502 503 504 505 506 507 508 509 510 511 512
                                    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));
}

513 514 515 516
GtkTextTag *
gimp_text_buffer_get_iter_baseline (GimpTextBuffer    *buffer,
                                    const GtkTextIter *iter,
                                    gint              *baseline)
517 518 519 520 521 522 523 524 525
{
  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))
        {
526
          if (baseline)
527
            *baseline = gimp_text_tag_get_baseline (tag);
528

529
          return tag;
530 531 532
        }
    }

533 534
  if (baseline)
    *baseline = 0;
535

536
  return NULL;
537 538 539
}

static GtkTextTag *
540 541
gimp_text_buffer_get_baseline_tag (GimpTextBuffer *buffer,
                                   gint            baseline)
542 543 544 545 546 547 548 549 550
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

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

551
      if (baseline == gimp_text_tag_get_baseline (tag))
552 553 554 555 556 557 558
        return tag;
    }

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

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
559
                                    GIMP_TEXT_PROP_NAME_BASELINE, baseline,
560 561 562 563 564 565 566
                                    NULL);

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

  return tag;
}

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 592 593 594 595 596 597 598 599 600 601 602
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));
}

603 604 605 606 607 608 609 610 611 612
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;
613
  gint         span_baseline;
614 615 616 617 618

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

619
  if (gtk_text_iter_equal (start, end))
620 621
    return;

622 623 624 625
  iter       = *start;
  span_start = *start;
  span_tag   = gimp_text_buffer_get_iter_baseline (buffer, &iter,
                                                   &span_baseline);
626

627 628
  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

629 630 631
  do
    {
      GtkTextTag *iter_tag;
632
      gint        iter_baseline;
633 634 635

      gtk_text_iter_forward_char (&iter);

636 637
      iter_tag = gimp_text_buffer_get_iter_baseline (buffer, &iter,
                                                     &iter_baseline);
638 639 640 641

      span_end = iter;

      if (iter_baseline != span_baseline ||
642
          gtk_text_iter_compare (&iter, end) >= 0)
643 644 645 646 647 648 649 650 651
        {
          if (span_baseline != 0)
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

          if (span_baseline + count != 0)
            {
652 653
              span_tag = gimp_text_buffer_get_baseline_tag (buffer,
                                                            span_baseline + count);
654 655 656 657 658 659 660 661 662

              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;
        }
663 664 665 666

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

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
671 672
}

673
GtkTextTag *
674
gimp_text_buffer_get_iter_kerning (GimpTextBuffer    *buffer,
675
                                   const GtkTextIter *iter,
676
                                   gint              *kerning)
677 678 679
{
  GList *list;

680
  for (list = buffer->kerning_tags; list; list = g_list_next (list))
681 682 683 684 685
    {
      GtkTextTag *tag = list->data;

      if (gtk_text_iter_has_tag (iter, tag))
        {
686
          if (kerning)
687
            *kerning = gimp_text_tag_get_kerning (tag);
688

689
          return tag;
690 691 692
        }
    }

693 694
  if (kerning)
    *kerning = 0;
695

696
  return NULL;
697 698 699
}

static GtkTextTag *
700 701
gimp_text_buffer_get_kerning_tag (GimpTextBuffer *buffer,
                                  gint            kerning)
702 703 704 705 706
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[32];

707
  for (list = buffer->kerning_tags; list; list = g_list_next (list))
708 709 710
    {
      tag = list->data;

711
      if (kerning == gimp_text_tag_get_kerning (tag))
712 713 714
        return tag;
    }

715
  g_snprintf (name, sizeof (name), "kerning-%d", kerning);
716 717 718

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
719
                                    GIMP_TEXT_PROP_NAME_KERNING, kerning,
720 721
                                    NULL);

722
  buffer->kerning_tags = g_list_prepend (buffer->kerning_tags, tag);
723 724 725 726

  return tag;
}

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 752 753 754 755 756 757 758 759 760 761 762
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));
}

763
void
764
gimp_text_buffer_change_kerning (GimpTextBuffer    *buffer,
765 766 767 768
                                 const GtkTextIter *start,
                                 const GtkTextIter *end,
                                 gint               count)
{
769 770 771 772
  GtkTextIter  iter;
  GtkTextIter  span_start;
  GtkTextIter  span_end;
  GtkTextTag  *span_tag;
773
  gint         span_kerning;
774

775 776 777
  g_return_if_fail (GIMP_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (start != NULL);
  g_return_if_fail (end != NULL);
778

779 780 781
  if (gtk_text_iter_equal (start, end))
    return;

782 783
  iter       = *start;
  span_start = *start;
784 785
  span_tag   = gimp_text_buffer_get_iter_kerning (buffer, &iter,
                                                  &span_kerning);
786

787 788
  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));

789 790 791
  do
    {
      GtkTextTag *iter_tag;
792
      gint        iter_kerning;
793 794 795

      gtk_text_iter_forward_char (&iter);

796 797
      iter_tag = gimp_text_buffer_get_iter_kerning (buffer, &iter,
                                                    &iter_kerning);
798 799 800

      span_end = iter;

801
      if (iter_kerning != span_kerning ||
802 803
          gtk_text_iter_compare (&iter, end) >= 0)
        {
804
          if (span_kerning != 0)
805 806 807 808 809
            {
              gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), span_tag,
                                          &span_start, &span_end);
            }

810
          if (span_kerning + count != 0)
811
            {
812 813
              span_tag = gimp_text_buffer_get_kerning_tag (buffer,
                                                           span_kerning + count);
814 815 816 817 818 819

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

          span_start   = iter;
820
          span_kerning = iter_kerning;
821 822 823 824 825 826 827 828
          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));
829 830

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
831 832
}

833 834 835 836
GtkTextTag *
gimp_text_buffer_get_iter_font (GimpTextBuffer     *buffer,
                                const GtkTextIter  *iter,
                                gchar             **font)
837 838 839 840 841 842 843 844 845
{
  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))
        {
846
          if (font)
847
            *font = gimp_text_tag_get_font (tag);
848

849
          return tag;
850 851 852
        }
    }

853 854
  if (font)
    *font = NULL;
855 856 857 858

  return NULL;
}

859
GtkTextTag *
860 861 862 863 864 865
gimp_text_buffer_get_font_tag (GimpTextBuffer *buffer,
                               const gchar    *font)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
866 867 868 869
  PangoFontDescription *pfd = pango_font_description_from_string (font);
  char *description = pango_font_description_to_string (pfd);

  pango_font_description_free (pfd);
870 871 872 873 874 875 876

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

      tag = list->data;

877
      tag_font = gimp_text_tag_get_font (tag);
878

879
      if (! strcmp (description, tag_font))
880 881
        {
          g_free (tag_font);
882
          g_free (description);
883 884 885 886 887 888
          return tag;
        }

      g_free (tag_font);
    }

889
  g_snprintf (name, sizeof (name), "font-%s", description);
890 891 892

  tag = gtk_text_buffer_create_tag (GTK_TEXT_BUFFER (buffer),
                                    name,
893
                                    "font", description,
894
                                    NULL);
895
  gtk_text_tag_set_priority (tag, 0);
896
  g_free (description);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
  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));
}

936 937 938 939 940 941 942 943 944 945 946 947 948 949
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)
950
            gimp_text_tag_get_fg_color (tag, color);
951 952 953 954 955 956 957 958

          return tag;
        }
    }

  return NULL;
}

959
GtkTextTag *
960 961 962 963 964 965 966
gimp_text_buffer_get_color_tag (GimpTextBuffer *buffer,
                                const GimpRGB  *color)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
  GdkColor    gdk_color;
967 968 969
  guchar      r, g, b;

  gimp_rgb_get_uchar (color, &r, &g, &b);
970 971 972 973

  for (list = buffer->color_tags; list; list = g_list_next (list))
    {
      GimpRGB tag_color;
974
      guchar  tag_r, tag_g, tag_b;
975 976 977

      tag = list->data;

978
      gimp_text_tag_get_fg_color (tag, &tag_color);
979

980 981
      gimp_rgb_get_uchar (&tag_color, &tag_r, &tag_g, &tag_b);

982
      /* Do not compare the alpha channel, since it's unused */
983 984 985
      if (tag_r == r &&
          tag_g == g &&
          tag_b == b)
986 987 988 989 990
        {
          return tag;
        }
    }

991 992
  g_snprintf (name, sizeof (name), "color-#%02x%02x%02x",
              r, g, b);
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 1027 1028 1029 1030 1031 1032 1033 1034 1035

  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);
1036 1037

      g_signal_emit (buffer, buffer_signals[COLOR_APPLIED], 0, color);
1038 1039 1040 1041 1042
    }

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

1043
GtkTextTag *
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
gimp_text_buffer_get_preedit_color_tag (GimpTextBuffer *buffer,
                                        const GimpRGB  *color)
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
  GdkColor    gdk_color;
  guchar      r, g, b;

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

  for (list = buffer->preedit_color_tags; list; list = g_list_next (list))
    {
      GimpRGB tag_color;
      guchar  tag_r, tag_g, tag_b;

      tag = list->data;

1062
      gimp_text_tag_get_fg_color (tag, &tag_color);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127

      gimp_rgb_get_uchar (&tag_color, &tag_r, &tag_g, &tag_b);

      /* Do not compare the alpha channel, since it's unused */
      if (tag_r == r &&
          tag_g == g &&
          tag_b == b)
        {
          return tag;
        }
    }

  g_snprintf (name, sizeof (name), "preedit-color-#%02x%02x%02x",
              r, g, 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->preedit_color_tags = g_list_prepend (buffer->preedit_color_tags, tag);

  return tag;
}

void
gimp_text_buffer_set_preedit_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->preedit_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_preedit_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));
}

GtkTextTag *
gimp_text_buffer_get_preedit_bg_color_tag (GimpTextBuffer *buffer,
                                           const GimpRGB  *color)
1128 1129 1130 1131 1132 1133 1134 1135 1136
{
  GList      *list;
  GtkTextTag *tag;
  gchar       name[256];
  GdkColor    gdk_color;
  guchar      r, g, b;

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

1137
  for (list = buffer->preedit_bg_color_tags; list; list = g_list_next (list))
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    {
      GimpRGB tag_color;
      guchar  tag_r, tag_g, tag_b;

      tag = list->data;

      gimp_text_tag_get_bg_color (tag, &tag_color);

      gimp_rgb_get_uchar (&tag_color, &tag_r, &tag_g, &tag_b);

      /* Do not compare the alpha channel, since it's unused */
      if (tag_r == r &&
          tag_g == g &&
          tag_b == b)
        {
          return tag;
        }
    }

  g_snprintf (name, sizeof (name), "bg-color-#%02x%02x%02x",
              r, g, b);

  gimp_rgb_get_gdk_color (color, &gdk_color);

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

1168
  buffer->preedit_bg_color_tags = g_list_prepend (buffer->preedit_bg_color_tags, tag);
1169 1170 1171 1172 1173

  return tag;
}

void
1174 1175 1176 1177
gimp_text_buffer_set_preedit_bg_color (GimpTextBuffer    *buffer,
                                       const GtkTextIter *start,
                                       const GtkTextIter *end,
                                       const GimpRGB     *color)
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
{
  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));

1190
  for (list = buffer->preedit_bg_color_tags; list; list = g_list_next (list))
1191 1192 1193 1194 1195 1196 1197
    {
      gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (buffer), list->data,
                                  start, end);
    }

  if (color)
    {
1198
      GtkTextTag *tag = gimp_text_buffer_get_preedit_bg_color_tag (buffer, color);
1199 1200 1201 1202 1203 1204 1205 1206

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

  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
}

1207 1208
/*  Pango markup attribute names  */

1209 1210 1211 1212 1213 1214
#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"
#define GIMP_TEXT_ATTR_NAME_STYLE     "style"
#define GIMP_TEXT_ATTR_NAME_COLOR     "foreground"
1215
#define GIMP_TEXT_ATTR_NAME_FG_COLOR  "fgcolor"
1216 1217
#define GIMP_TEXT_ATTR_NAME_BG_COLOR  "background"
#define GIMP_TEXT_ATTR_NAME_UNDERLINE "underline"
1218

1219
const gchar *
1220 1221 1222 1223
gimp_text_buffer_tag_to_name (GimpTextBuffer  *buffer,
                              GtkTextTag      *tag,
                              const gchar    **attribute,
                              gchar          **value)
1224 1225 1226 1227
{
  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), NULL);

1228 1229 1230 1231 1232 1233
  if (attribute)
    *attribute = NULL;

  if (value)
    *value = NULL;

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  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";
    }
1250 1251 1252
  else if (g_list_find (buffer->size_tags, tag))
    {
      if (attribute)
1253
        *attribute = GIMP_TEXT_ATTR_NAME_SIZE;
1254 1255

      if (value)
1256
        *value = g_strdup_printf ("%d", gimp_text_tag_get_size (tag));
1257 1258 1259

      return "span";
    }
1260 1261 1262
  else if (g_list_find (buffer->baseline_tags, tag))
    {
      if (attribute)
1263
        *attribute = GIMP_TEXT_ATTR_NAME_BASELINE;
1264 1265

      if (value)
1266
        *value = g_strdup_printf ("%d", gimp_text_tag_get_baseline (tag));
1267 1268 1269

      return "span";
    }
1270
  else if (g_list_find (buffer->kerning_tags, tag))
1271 1272
    {
      if (attribute)
1273
        *attribute = GIMP_TEXT_ATTR_NAME_KERNING;
1274 1275

      if (value)
1276
        *value = g_strdup_printf ("%d", gimp_text_tag_get_kerning (tag));
1277 1278 1279

      return "span";
    }