[gtksourceview/wip/gutter-style: 6/6] Apply line-numbers style in GtkSourceGutter



commit a1b91bb93e3cf33a8e2247877aaa203c9e81844c
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Aug 13 19:08:53 2014 +0200

    Apply line-numbers style in GtkSourceGutter
    
    The line-numbers style was applied to the whole GtkTextView widget with
    gtk_widget_override_color() and gtk_widget_override_background_color().
    It worked because the 'text' style is also applied to the main
    GtkTextView region with CSS, which seems to override the colors set with
    gtk_widget_override_color() and gtk_widget_override_background_color().
    
    Now the line-numbers style is applied directly in GtkSourceGutter, which
    is more logical.
    
    If a style scheme wanted to define 'line-numbers' without defining
    'text', it didn't work. Now it works, and it'll be used for classic and
    tango (which don't define the 'text' style).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=733606

 gtksourceview/gtksourcegutter.c      |   88 ++++++++++++++++++++++++++++++----
 gtksourceview/gtksourcestylescheme.c |   33 +------------
 2 files changed, 80 insertions(+), 41 deletions(-)
---
diff --git a/gtksourceview/gtksourcegutter.c b/gtksourceview/gtksourcegutter.c
index 1cff5c7..5e42297 100644
--- a/gtksourceview/gtksourcegutter.c
+++ b/gtksourceview/gtksourcegutter.c
@@ -25,6 +25,9 @@
 #include "gtksourceview-i18n.h"
 #include "gtksourcegutterrenderer.h"
 #include "gtksourcegutterrenderer-private.h"
+#include "gtksourcebuffer.h"
+#include "gtksourcestyle.h"
+#include "gtksourcestylescheme.h"
 
 /**
  * SECTION:gutter
@@ -929,6 +932,70 @@ get_lines (GtkTextView  *text_view,
        return total_height;
 }
 
+static void
+get_colors (GtkSourceView *view,
+           GdkRGBA       *fg_color,
+           GdkRGBA       *bg_color)
+{
+       GtkStyleContext *style_context;
+       GtkStateFlags state;
+       GtkSourceBuffer *buffer;
+       GtkSourceStyleScheme *style_scheme;
+       GtkSourceStyle *style;
+       gboolean fg_set;
+       gboolean bg_set;
+       gchar *fg_str;
+       gchar *bg_str;
+
+       g_assert (fg_color != NULL);
+       g_assert (bg_color != NULL);
+
+       /* Take by default the colors set on the widget. */
+
+       style_context = gtk_widget_get_style_context (GTK_WIDGET (view));
+       state = gtk_widget_get_state_flags (GTK_WIDGET (view));
+
+       gtk_style_context_get_color (style_context, state, fg_color);
+       gtk_style_context_get_background_color (style_context, state, bg_color);
+
+       /* Override with the 'line-numbers' style, if set. */
+
+       buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
+       style_scheme = gtk_source_buffer_get_style_scheme (buffer);
+
+       if (style_scheme == NULL)
+       {
+               return;
+       }
+
+       style = gtk_source_style_scheme_get_style (style_scheme, "line-numbers");
+
+       if (style == NULL)
+       {
+               return;
+       }
+
+       g_object_get (style,
+                     "foreground-set", &fg_set,
+                     "background-set", &bg_set,
+                     "foreground", &fg_str,
+                     "background", &bg_str,
+                     NULL);
+
+       if (fg_set)
+       {
+               gdk_rgba_parse (fg_color, fg_str);
+       }
+
+       if (bg_set)
+       {
+               gdk_rgba_parse (bg_color, bg_str);
+       }
+
+       g_free (fg_str);
+       g_free (bg_str);
+}
+
 static gboolean
 on_view_draw (GtkSourceView   *view,
               cairo_t         *cr,
@@ -956,8 +1023,8 @@ on_view_draw (GtkSourceView   *view,
        GtkTextIter selection_end;
        gboolean has_selection;
        gint idx;
-       GtkStyleContext *style_context;
        GdkRGBA fg_color;
+       GdkRGBA bg_color;
 
        window = get_window (gutter);
 
@@ -975,6 +1042,17 @@ on_view_draw (GtkSourceView   *view,
 
        gutter->priv->is_drawing = TRUE;
 
+       get_colors (view, &fg_color, &bg_color);
+
+       /* draw background color */
+       cairo_save (cr);
+       gdk_cairo_set_source_rgba (cr, &bg_color);
+       gdk_cairo_rectangle (cr, &clip);
+       cairo_fill (cr);
+       cairo_restore (cr);
+
+       gdk_cairo_set_source_rgba (cr, &fg_color);
+
        text_view = GTK_TEXT_VIEW (view);
        buffer = gtk_text_view_get_buffer (text_view);
 
@@ -1026,14 +1104,6 @@ on_view_draw (GtkSourceView   *view,
 
        cell_area.y = background_area.y;
 
-       style_context = gtk_widget_get_style_context (GTK_WIDGET (view));
-
-       gtk_style_context_get_color (style_context,
-                                    gtk_widget_get_state_flags (GTK_WIDGET (view)),
-                                    &fg_color);
-
-       gdk_cairo_set_source_rgba (cr, &fg_color);
-
        for (item = gutter->priv->renderers, idx = 0;
             item != NULL;
             item = g_list_next (item), idx++)
diff --git a/gtksourceview/gtksourcestylescheme.c b/gtksourceview/gtksourcestylescheme.c
index 357f0ad..c162114 100644
--- a/gtksourceview/gtksourcestylescheme.c
+++ b/gtksourceview/gtksourcestylescheme.c
@@ -52,7 +52,7 @@
 #define STYLE_CURSOR                   "cursor"
 #define STYLE_SECONDARY_CURSOR         "secondary-cursor"
 #define STYLE_CURRENT_LINE             "current-line"
-#define STYLE_LINE_NUMBERS             "line-numbers"
+#define STYLE_LINE_NUMBERS             "line-numbers" /* used in GtkSourceGutter */
 #define STYLE_RIGHT_MARGIN             "right-margin"
 #define STYLE_DRAW_SPACES              "draw-spaces"
 
@@ -636,33 +636,6 @@ _gtk_source_style_scheme_get_current_line_color (GtkSourceStyleScheme *scheme,
 }
 
 static void
-set_line_numbers_style (GtkWidget      *widget,
-                       GtkSourceStyle *style)
-{
-       GdkRGBA *fg_ptr = NULL;
-       GdkRGBA *bg_ptr = NULL;
-       GdkRGBA fg;
-       GdkRGBA bg;
-       GtkStateFlags flags;
-
-       if (get_color (style, TRUE, &fg))
-       {
-               fg_ptr = &fg;
-       }
-
-       if (get_color (style, FALSE, &bg))
-       {
-               bg_ptr = &bg;
-       }
-
-       /* Override the color no matter what the state is */
-       flags = GTK_STATE_FLAG_NORMAL;
-
-       gtk_widget_override_color (widget, flags, fg_ptr);
-       gtk_widget_override_background_color (widget, flags, bg_ptr);
-}
-
-static void
 update_cursor_colors (GtkWidget      *widget,
                      GtkSourceStyle *style_primary,
                      GtkSourceStyle *style_secondary)
@@ -738,9 +711,6 @@ _gtk_source_style_scheme_apply (GtkSourceStyleScheme *scheme,
        gtk_style_context_invalidate (context);
        G_GNUC_END_IGNORE_DEPRECATIONS;
 
-       style = gtk_source_style_scheme_get_style (scheme, STYLE_LINE_NUMBERS);
-       set_line_numbers_style (widget, style);
-
        style = gtk_source_style_scheme_get_style (scheme, STYLE_CURSOR);
        style2 = gtk_source_style_scheme_get_style (scheme, STYLE_SECONDARY_CURSOR);
        update_cursor_colors (widget, style, style2);
@@ -773,7 +743,6 @@ _gtk_source_style_scheme_unapply (GtkSourceStyleScheme *scheme,
        gtk_style_context_invalidate (context);
        G_GNUC_END_IGNORE_DEPRECATIONS;
 
-       set_line_numbers_style (widget, NULL);
        update_cursor_colors (widget, NULL, NULL);
 }
 


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