[gimp] app: GimpText can be set background colors.



commit f1dbd57d49ecf9ce05c17dbf7a29dbabad6b7a0d
Author: Jehan <jehan girinstud io>
Date:   Mon May 9 00:30:26 2016 +0200

    app: GimpText can be set background colors.
    
    There is nothing in our UI to set background color to text, but this
    can be used internally, for instance to display input method's preedit
    texts with specific colors.

 app/widgets/gimptextbuffer.c |  107 ++++++++++++++++++++++++++++++++++++++++++
 app/widgets/gimptextbuffer.h |    8 +++
 app/widgets/gimptexttag.c    |   15 ++++++
 app/widgets/gimptexttag.h    |    3 +
 4 files changed, 133 insertions(+), 0 deletions(-)
---
diff --git a/app/widgets/gimptextbuffer.c b/app/widgets/gimptextbuffer.c
index 6c5da93..a6f040c 100644
--- a/app/widgets/gimptextbuffer.c
+++ b/app/widgets/gimptextbuffer.c
@@ -155,6 +155,12 @@ gimp_text_buffer_finalize (GObject *object)
       buffer->color_tags = NULL;
     }
 
+  if (buffer->bg_color_tags)
+    {
+      g_list_free (buffer->bg_color_tags);
+      buffer->bg_color_tags = NULL;
+    }
+
   gimp_text_buffer_clear_insert_tags (buffer);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -1001,6 +1007,88 @@ gimp_text_buffer_set_color (GimpTextBuffer    *buffer,
   gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
 }
 
+GtkTextTag *
+gimp_text_buffer_get_bg_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->bg_color_tags; list; list = g_list_next (list))
+    {
+      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);
+
+  buffer->bg_color_tags = g_list_prepend (buffer->bg_color_tags, tag);
+
+  return tag;
+}
+
+void
+gimp_text_buffer_set_bg_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->bg_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_bg_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));
+}
+
 /*  Pango markup attribute names  */
 
 #define GIMP_TEXT_ATTR_NAME_SIZE     "size"
@@ -1008,6 +1096,7 @@ gimp_text_buffer_set_color (GimpTextBuffer    *buffer,
 #define GIMP_TEXT_ATTR_NAME_KERNING  "letter_spacing"
 #define GIMP_TEXT_ATTR_NAME_FONT     "font"
 #define GIMP_TEXT_ATTR_NAME_COLOR    "foreground"
+#define GIMP_TEXT_ATTR_NAME_BG_COLOR "background"
 
 const gchar *
 gimp_text_buffer_tag_to_name (GimpTextBuffer  *buffer,
@@ -1098,6 +1187,24 @@ gimp_text_buffer_tag_to_name (GimpTextBuffer  *buffer,
 
       return "span";
     }
+  else if (g_list_find (buffer->bg_color_tags, tag))
+    {
+      if (attribute)
+        *attribute = GIMP_TEXT_ATTR_NAME_BG_COLOR;
+
+      if (value)
+        {
+          GimpRGB color;
+          guchar  r, g, b;
+
+          gimp_text_tag_get_bg_color (tag, &color);
+          gimp_rgb_get_uchar (&color, &r, &g, &b);
+
+          *value = g_strdup_printf ("#%02x%02x%02x", r, g, b);
+        }
+
+      return "span";
+    }
 
   return NULL;
 }
diff --git a/app/widgets/gimptextbuffer.h b/app/widgets/gimptextbuffer.h
index 1cc8f4e..dc6fa63 100644
--- a/app/widgets/gimptextbuffer.h
+++ b/app/widgets/gimptextbuffer.h
@@ -45,6 +45,7 @@ struct _GimpTextBuffer
   GList         *kerning_tags;
   GList         *font_tags;
   GList         *color_tags;
+  GList         *bg_color_tags;
 
   gboolean       insert_tags_set;
   GList         *insert_tags;
@@ -131,6 +132,13 @@ void             gimp_text_buffer_set_color         (GimpTextBuffer    *buffer,
                                                      const GtkTextIter *end,
                                                      const GimpRGB     *color);
 
+GtkTextTag     * gimp_text_buffer_get_bg_color_tag  (GimpTextBuffer    *buffer,
+                                                     const GimpRGB     *color);
+void             gimp_text_buffer_set_bg_color      (GimpTextBuffer    *buffer,
+                                                     const GtkTextIter *start,
+                                                     const GtkTextIter *end,
+                                                     const GimpRGB     *color);
+
 const gchar    * gimp_text_buffer_tag_to_name       (GimpTextBuffer    *buffer,
                                                      GtkTextTag        *tag,
                                                      const gchar      **attribute,
diff --git a/app/widgets/gimptexttag.c b/app/widgets/gimptexttag.c
index dee775b..893fe71 100644
--- a/app/widgets/gimptexttag.c
+++ b/app/widgets/gimptexttag.c
@@ -95,3 +95,18 @@ gimp_text_tag_get_color (GtkTextTag *tag,
 
   gdk_color_free (gdk_color);
 }
+
+void
+gimp_text_tag_get_bg_color (GtkTextTag *tag,
+                            GimpRGB    *color)
+{
+  GdkColor *gdk_color;
+
+  g_object_get (tag,
+                GIMP_TEXT_PROP_NAME_BG_COLOR, &gdk_color,
+                NULL);
+
+  gimp_rgb_set_gdk_color (color, gdk_color);
+
+  gdk_color_free (gdk_color);
+}
diff --git a/app/widgets/gimptexttag.h b/app/widgets/gimptexttag.h
index e0c967a..183f4f0 100644
--- a/app/widgets/gimptexttag.h
+++ b/app/widgets/gimptexttag.h
@@ -29,6 +29,7 @@
 #define GIMP_TEXT_PROP_NAME_KERNING  "rise" /* FIXME */
 #define GIMP_TEXT_PROP_NAME_FONT     "font"
 #define GIMP_TEXT_PROP_NAME_COLOR    "foreground-gdk"
+#define GIMP_TEXT_PROP_NAME_BG_COLOR "background-gdk"
 
 
 gint    gimp_text_tag_get_size     (GtkTextTag *tag);
@@ -37,6 +38,8 @@ gint    gimp_text_tag_get_kerning  (GtkTextTag *tag);
 gchar * gimp_text_tag_get_font     (GtkTextTag *tag);
 void    gimp_text_tag_get_color    (GtkTextTag *tag,
                                     GimpRGB    *color);
+void    gimp_text_tag_get_bg_color (GtkTextTag *tag,
+                                    GimpRGB    *color);
 
 
 #endif /* __GIMP_TEXT_TAG_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]