[gtk+] text view: Improve text rendering



commit 9d9088304c5386c32f6a6e304ff16feb0a13e33e
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Nov 10 21:49:42 2015 -0500

    text view: Improve text rendering
    
    Use the text CSS node for rendering text, and the selection node
    for rendering selected text, avoid gtk_style_context_save, update
    states of all CSS nodes, and use the proper states when querying
    style properties.

 gtk/gtktextdisplay.c |   28 +++++++++++++++-------------
 gtk/gtktextview.c    |   21 ++++++++++++++-------
 2 files changed, 29 insertions(+), 20 deletions(-)
---
diff --git a/gtk/gtktextdisplay.c b/gtk/gtktextdisplay.c
index 24372d2..dedf611 100644
--- a/gtk/gtktextdisplay.c
+++ b/gtk/gtktextdisplay.c
@@ -166,12 +166,14 @@ get_item_appearance (PangoItem *item)
   return NULL;
 }
 
+extern GtkCssNode *gtk_text_view_get_text_node      (GtkTextView *text_view);
+extern GtkCssNode *gtk_text_view_get_selection_node (GtkTextView *text_view);
+
 static void
 gtk_text_renderer_prepare_run (PangoRenderer  *renderer,
                               PangoLayoutRun *run)
 {
   GtkStyleContext *context;
-  GtkStateFlags state;
   GtkTextRenderer *text_renderer = GTK_TEXT_RENDERER (renderer);
   GdkRGBA *bg_rgba = NULL;
   GdkRGBA *fg_rgba = NULL;
@@ -183,29 +185,30 @@ gtk_text_renderer_prepare_run (PangoRenderer  *renderer,
   g_assert (appearance != NULL);
 
   context = gtk_widget_get_style_context (text_renderer->widget);
-  state   = gtk_style_context_get_state (context);
 
   if (appearance->draw_bg && text_renderer->state == NORMAL)
     bg_rgba = appearance->rgba[0];
   else
     bg_rgba = NULL;
-  
+
   text_renderer_set_rgba (text_renderer, PANGO_RENDER_PART_BACKGROUND, bg_rgba);
 
   if (text_renderer->state == SELECTED)
     {
-      gtk_style_context_save (context);
+      GtkCssNode *selection_node;
 
-      state |= GTK_STATE_FLAG_SELECTED;
-      gtk_style_context_set_state (context, state);
+      selection_node = gtk_text_view_get_selection_node ((GtkTextView *)text_renderer->widget);
+      gtk_style_context_save_to_node (context, selection_node);
 
-      gtk_style_context_get (context, state, "color", &fg_rgba, NULL);
+      gtk_style_context_get (context, gtk_style_context_get_state (context),
+                             "color", &fg_rgba,
+                             NULL);
 
       gtk_style_context_restore (context);
     }
   else if (text_renderer->state == CURSOR && gtk_widget_has_focus (text_renderer->widget))
     {
-      gtk_style_context_get (context, state,
+      gtk_style_context_get (context, gtk_style_context_get_state (context),
                              "background-color", &fg_rgba,
                               NULL);
     }
@@ -243,7 +246,7 @@ gtk_text_renderer_prepare_run (PangoRenderer  *renderer,
 
          if (color)
            {
-             GdkRGBA   rgba;
+             GdkRGBA rgba;
 
              rgba.red = color->red / 65535.;
              rgba.green = color->green / 65535.;
@@ -519,14 +522,15 @@ text_renderer_begin (GtkTextRenderer *text_renderer,
   GtkStyleContext *context;
   GtkStateFlags state;
   GdkRGBA color;
+  GtkCssNode *text_node;
 
   text_renderer->widget = widget;
   text_renderer->cr = cr;
 
   context = gtk_widget_get_style_context (widget);
 
-  gtk_style_context_save (context);
-  gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
+  text_node = gtk_text_view_get_text_node ((GtkTextView *)widget);
+  gtk_style_context_save_to_node (context, text_node);
 
   state = gtk_style_context_get_state (context);
   gtk_style_context_get_color (context, state, &color);
@@ -596,8 +600,6 @@ get_selected_clip (GtkTextRenderer    *text_renderer,
   return clip_region;
 }
 
-extern GtkCssNode *gtk_text_view_get_selection_node (GtkTextView *text_view);
-
 static void
 render_para (GtkTextRenderer    *text_renderer,
              GtkTextLineDisplay *line_display,
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index b2db5ce..8c27593 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -1748,6 +1748,12 @@ gtk_text_view_init (GtkTextView *text_view)
 }
 
 GtkCssNode *
+gtk_text_view_get_text_node (GtkTextView *text_view)
+{
+  return text_view->priv->text_window->css_node;
+}
+
+GtkCssNode *
 gtk_text_view_get_selection_node (GtkTextView *text_view)
 {
   return text_view->priv->selection_node;
@@ -4854,6 +4860,7 @@ gtk_text_view_state_flags_changed (GtkWidget     *widget,
   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
   GtkTextViewPrivate *priv = text_view->priv;
   GdkCursor *cursor;
+  GtkStateFlags state;
 
   if (gtk_widget_get_realized (widget))
     {
@@ -4876,17 +4883,17 @@ gtk_text_view_state_flags_changed (GtkWidget     *widget,
       gtk_text_view_unselect (text_view);
     }
 
-  gtk_css_node_set_state (priv->text_window->css_node, gtk_widget_get_state (widget));
-  gtk_css_node_set_state (priv->selection_node, gtk_widget_get_state (widget));
-
+  state = gtk_widget_get_state_flags (widget);
+  gtk_css_node_set_state (priv->text_window->css_node, state);
+  gtk_css_node_set_state (priv->selection_node, state);
   if (priv->left_window)
-    gtk_css_node_set_state (priv->left_window->css_node, gtk_widget_get_state (widget));
+    gtk_css_node_set_state (priv->left_window->css_node, state);
   if (priv->right_window)
-    gtk_css_node_set_state (priv->right_window->css_node, gtk_widget_get_state (widget));
+    gtk_css_node_set_state (priv->right_window->css_node, state);
   if (priv->top_window)
-    gtk_css_node_set_state (priv->top_window->css_node, gtk_widget_get_state (widget));
+    gtk_css_node_set_state (priv->top_window->css_node, state);
   if (priv->bottom_window)
-    gtk_css_node_set_state (priv->bottom_window->css_node, gtk_widget_get_state (widget));
+    gtk_css_node_set_state (priv->bottom_window->css_node, state);
 
   gtk_widget_queue_draw (widget);
 }


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