[gtksourceview] StyleScheme: fix bug in CSS generation for cursors



commit 261b15f295f2362274282eee08ceb498fa1d39dc
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Mar 26 14:50:56 2016 +0100

    StyleScheme: fix bug in CSS generation for cursors
    
    Allow setting a secondary cursor color without setting a primary cursor
    color. In that case the primary cursor color is taken from the GTK+
    theme (CSS is not generated for it).

 gtksourceview/gtksourcestylescheme.c |   56 +++++++++++++++++++++-------------
 1 files changed, 35 insertions(+), 21 deletions(-)
---
diff --git a/gtksourceview/gtksourcestylescheme.c b/gtksourceview/gtksourcestylescheme.c
index 7e47523..c5297b2 100644
--- a/gtksourceview/gtksourcestylescheme.c
+++ b/gtksourceview/gtksourcestylescheme.c
@@ -659,22 +659,41 @@ get_cursors_css_style (GtkSourceStyleScheme *scheme,
        GtkSourceStyle *secondary_style;
        GdkRGBA primary_color = { 0 };
        GdkRGBA secondary_color = { 0 };
-       gchar *primary_color_str;
+       gboolean primary_color_set;
+       gboolean secondary_color_set;
        gchar *secondary_color_str;
-       gchar *css;
+       GString *css;
 
        primary_style = gtk_source_style_scheme_get_style (scheme, STYLE_CURSOR);
        secondary_style = gtk_source_style_scheme_get_style (scheme, STYLE_SECONDARY_CURSOR);
 
-       if (!get_color (primary_style, TRUE, &primary_color))
+       primary_color_set = get_color (primary_style, TRUE, &primary_color);
+       secondary_color_set = get_color (secondary_style, TRUE, &secondary_color);
+
+       if (!primary_color_set && !secondary_color_set)
        {
                return NULL;
        }
 
-       if (!get_color (secondary_style, TRUE, &secondary_color))
+       css = g_string_new ("textview text {\n");
+
+       if (primary_color_set)
+       {
+               gchar *primary_color_str;
+
+               primary_color_str = gdk_rgba_to_string (&primary_color);
+               g_string_append_printf (css,
+                                       "\tcaret-color: %s;\n",
+                                       primary_color_str);
+               g_free (primary_color_str);
+       }
+
+       if (!secondary_color_set)
        {
                GtkStyleContext *context;
-               GdkRGBA *rgba;
+               GdkRGBA *background_color;
+
+               g_assert (primary_color_set);
 
                context = gtk_widget_get_style_context (widget);
 
@@ -683,34 +702,29 @@ get_cursors_css_style (GtkSourceStyleScheme *scheme,
 
                gtk_style_context_get (context,
                                       gtk_style_context_get_state (context),
-                                      "background-color", &rgba,
+                                      "background-color", &background_color,
                                       NULL);
 
                gtk_style_context_restore (context);
 
                /* shade the secondary cursor */
-               secondary_color.red = rgba->red * 0.5;
-               secondary_color.green = rgba->green * 0.5;
-               secondary_color.blue = rgba->blue * 0.5;
+               secondary_color.red = background_color->red * 0.5;
+               secondary_color.green = background_color->green * 0.5;
+               secondary_color.blue = background_color->blue * 0.5;
                secondary_color.alpha = 1.0;
 
-               gdk_rgba_free (rgba);
+               gdk_rgba_free (background_color);
        }
 
-       primary_color_str = gdk_rgba_to_string (&primary_color);
        secondary_color_str = gdk_rgba_to_string (&secondary_color);
-
-       css = g_strdup_printf ("textview text {\n"
-                              "\tcaret-color: %s;\n"
-                              "\t-gtk-secondary-caret-color: %s;\n"
-                              "}\n",
-                              primary_color_str,
-                              secondary_color_str);
-
-       g_free (primary_color_str);
+       g_string_append_printf (css,
+                               "\t-gtk-secondary-caret-color: %s;\n",
+                               secondary_color_str);
        g_free (secondary_color_str);
 
-       return css;
+       g_string_append_printf (css, "}\n");
+
+       return g_string_free (css, FALSE);
 }
 
 /* The CssProvider for the cursors depends only on @scheme, but it needs a


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