[gtk/text-fixes: 8/8] textivew: Use gtk_widget_update_pango_context




commit 5f92a3b671a1c8af5c00c1fa1fe888f98db46921
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Sep 4 15:35:44 2021 -0400

    textivew: Use gtk_widget_update_pango_context
    
    This makes it less likely that we forget to update
    some things, causing textview-specific breakage
    in the future.

 gtk/gtktextview.c | 46 ++++++++++++++++++++++++++++------------------
 1 file changed, 28 insertions(+), 18 deletions(-)
---
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index 39398bdd92..bb449e9e2b 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -575,6 +575,7 @@ static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
 static void gtk_text_view_insert_emoji (GtkTextView *text_view);
 
 static void update_node_ordering (GtkWidget    *widget);
+static void gtk_text_view_update_pango_contexts (GtkTextView *text_view);
 
 /* GtkTextHandle handlers */
 static void gtk_text_view_handle_drag_started  (GtkTextHandle         *handle,
@@ -4987,7 +4988,6 @@ gtk_text_view_css_changed (GtkWidget         *widget,
 {
   GtkTextView *text_view;
   GtkTextViewPrivate *priv;
-  PangoContext *ltr_context, *rtl_context;
 
   text_view = GTK_TEXT_VIEW (widget);
   priv = text_view->priv;
@@ -5004,16 +5004,13 @@ gtk_text_view_css_changed (GtkWidget         *widget,
       gtk_text_view_set_attributes_from_style (text_view,
                                                priv->layout->default_style);
       gtk_text_layout_default_style_changed (priv->layout);
+    }
 
-      ltr_context = gtk_widget_create_pango_context (widget);
-      pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
-      rtl_context = gtk_widget_create_pango_context (widget);
-      pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
-
-      gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
-
-      g_object_unref (ltr_context);
-      g_object_unref (rtl_context);
+  if ((change == NULL ||
+       gtk_css_style_change_affects (change, GTK_CSS_AFFECTS_TEXT)) &&
+      priv->layout)
+    {
+      gtk_text_view_update_pango_contexts (text_view);
     }
 }
 
@@ -5034,21 +5031,25 @@ gtk_text_view_direction_changed (GtkWidget        *widget,
 static void
 gtk_text_view_update_pango_contexts (GtkTextView *text_view)
 {
+  GtkWidget *widget = GTK_WIDGET (text_view);
   GtkTextViewPrivate *priv = text_view->priv;
-  PangoContext *ltr_context, *rtl_context;
+  gboolean update_ltr, update_rtl;
 
   if (!priv->layout)
     return;
 
-  ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
-  pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
-  rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
-  pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
+ update_ltr = gtk_widget_update_pango_context (widget, priv->layout->ltr_context, GTK_TEXT_DIR_LTR);
 
-  gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
+ update_rtl = gtk_widget_update_pango_context (widget, priv->layout->rtl_context, GTK_TEXT_DIR_RTL);
 
-  g_object_unref (ltr_context);
-  g_object_unref (rtl_context);
+  if (update_ltr || update_rtl)
+    {
+      GtkTextIter start, end;
+
+      gtk_text_buffer_get_bounds (get_buffer (text_view), &start, &end);
+      gtk_text_layout_invalidate (priv->layout, &start, &end);
+      gtk_widget_queue_draw (widget);
+    }
 }
 
 static void
@@ -7845,6 +7846,7 @@ gtk_text_view_ensure_layout (GtkTextView *text_view)
     {
       GtkTextAttributes *style;
       const GList *iter;
+      PangoContext *ltr_context, *rtl_context;
 
       DV(g_print(G_STRLOC"\n"));
       
@@ -7876,6 +7878,14 @@ gtk_text_view_ensure_layout (GtkTextView *text_view)
       gtk_text_layout_set_overwrite_mode (priv->layout,
                                          priv->overwrite_mode && priv->editable);
 
+      ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
+      rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
+      pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
+      pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
+      gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
+      g_object_unref (ltr_context);
+      g_object_unref (rtl_context);
+
       gtk_text_view_update_pango_contexts (text_view);
 
       gtk_text_view_check_keymap_direction (text_view);


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