[gtk+] text view: Improve tag pointer tracking



commit a3a5cf1087278e3e727a74c75f2532bcb14ddca6
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Feb 10 00:01:54 2016 -0500

    text view: Improve tag pointer tracking
    
    A problem that has been observed in polari is that links in tags
    are clickable all the way into the margin. This problem is caused
    by gtk_text_view_get_iter_at_position ignoring the return value
    of pango_layout_xy_to_index. Instead, pass it back as a boolean
    return value. This is technically an API break, but we've allowed
    ourselves to change return types from void to gboolean before.

 gtk/gtktextlayout.c |   39 ++++++++++++++++++++++++---------------
 gtk/gtktextlayout.h |   18 +++++++++---------
 gtk/gtktextview.c   |   48 +++++++++++++++++++++++++-----------------------
 gtk/gtktextview.h   |    4 ++--
 4 files changed, 60 insertions(+), 49 deletions(-)
---
diff --git a/gtk/gtktextlayout.c b/gtk/gtktextlayout.c
index 185c1a0..6adcdc5 100644
--- a/gtk/gtktextlayout.c
+++ b/gtk/gtktextlayout.c
@@ -362,7 +362,7 @@ gtk_text_layout_default_style_changed (GtkTextLayout *layout)
 }
 
 void
-gtk_text_layout_set_default_style (GtkTextLayout *layout,
+gtk_text_layout_set_default_style (GtkTextLayout     *layout,
                                    GtkTextAttributes *values)
 {
   g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
@@ -2736,32 +2736,37 @@ gtk_text_layout_get_line_at_y (GtkTextLayout *layout,
   gtk_text_layout_get_iter_at_line (layout, target_iter, line, 0);
 }
 
-void
+gboolean
 gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
                                    GtkTextIter   *target_iter,
-                                   gint           x, 
-                                  gint           y)
+                                   gint           x,
+                                   gint           y)
 {
   gint trailing;
+  gboolean inside;
+
+  inside = gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
 
-  gtk_text_layout_get_iter_at_position (layout, target_iter, &trailing, x, y);
+  gtk_text_iter_forward_chars (target_iter, trailing);
 
-  gtk_text_iter_forward_chars (target_iter, trailing);  
+  return inside;
 }
 
-void gtk_text_layout_get_iter_at_position (GtkTextLayout     *layout,
-                                          GtkTextIter       *target_iter,
-                                          gint              *trailing,
-                                          gint               x,
-                                          gint               y)
+gboolean
+gtk_text_layout_get_iter_at_position (GtkTextLayout *layout,
+                                      GtkTextIter   *target_iter,
+                                      gint          *trailing,
+                                      gint           x,
+                                      gint           y)
 {
   GtkTextLine *line;
   gint byte_index;
   gint line_top;
   GtkTextLineDisplay *display;
+  gboolean inside;
 
-  g_return_if_fail (GTK_IS_TEXT_LAYOUT (layout));
-  g_return_if_fail (target_iter != NULL);
+  g_return_val_if_fail (GTK_IS_TEXT_LAYOUT (layout), FALSE);
+  g_return_val_if_fail (target_iter != NULL, FALSE);
 
   get_line_at_y (layout, y, &line, &line_top);
 
@@ -2778,6 +2783,8 @@ void gtk_text_layout_get_iter_at_position (GtkTextLayout     *layout,
       byte_index = _gtk_text_line_byte_count (line);
       if (trailing)
         *trailing = 0;
+
+      inside = FALSE;
     }
   else
     {
@@ -2785,13 +2792,15 @@ void gtk_text_layout_get_iter_at_position (GtkTextLayout     *layout,
         * the right thing even if we are outside the layout in the
         * x-direction.
         */
-      pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
-                                &byte_index, trailing);
+      inside = pango_layout_xy_to_index (display->layout, x * PANGO_SCALE, y * PANGO_SCALE,
+                                         &byte_index, trailing);
     }
 
   line_display_index_to_iter (layout, display, target_iter, byte_index, 0);
 
   gtk_text_layout_free_line_display (layout, display);
+
+  return inside;
 }
 
 
diff --git a/gtk/gtktextlayout.h b/gtk/gtktextlayout.h
index 2565b64..ba5318c 100644
--- a/gtk/gtktextlayout.h
+++ b/gtk/gtktextlayout.h
@@ -345,16 +345,16 @@ void gtk_text_layout_get_line_at_y     (GtkTextLayout     *layout,
                                         gint               y,
                                         gint              *line_top);
 GDK_AVAILABLE_IN_ALL
-void gtk_text_layout_get_iter_at_pixel (GtkTextLayout     *layout,
-                                        GtkTextIter       *iter,
-                                        gint               x,
-                                        gint               y);
-GDK_AVAILABLE_IN_ALL
-void gtk_text_layout_get_iter_at_position (GtkTextLayout     *layout,
-                                          GtkTextIter       *iter,
-                                          gint              *trailing,
-                                          gint               x,
-                                          gint               y);
+gboolean gtk_text_layout_get_iter_at_pixel (GtkTextLayout     *layout,
+                                            GtkTextIter       *iter,
+                                            gint               x,
+                                            gint               y);
+GDK_AVAILABLE_IN_ALL
+gboolean gtk_text_layout_get_iter_at_position (GtkTextLayout     *layout,
+                                               GtkTextIter       *iter,
+                                               gint              *trailing,
+                                               gint               x,
+                                               gint               y);
 GDK_AVAILABLE_IN_ALL
 void gtk_text_layout_invalidate        (GtkTextLayout     *layout,
                                         const GtkTextIter *start,
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index c2d651b..8703928 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -2091,20 +2091,21 @@ gtk_text_view_get_cursor_locations (GtkTextView       *text_view,
  * currently-displayed portion.  If you have coordinates from an
  * event, you have to convert those to buffer coordinates with
  * gtk_text_view_window_to_buffer_coords().
- **/
-void
+ *
+ * Returns: %TRUE if the position is over text
+ */
+gboolean
 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
                                     GtkTextIter *iter,
                                     gint         x,
                                     gint         y)
 {
-  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
-  g_return_if_fail (iter != NULL);
+  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
+  g_return_val_if_fail (iter != NULL, FALSE);
 
   gtk_text_view_ensure_layout (text_view);
-  
-  gtk_text_layout_get_iter_at_pixel (text_view->priv->layout,
-                                     iter, x, y);
+
+  return gtk_text_layout_get_iter_at_pixel (text_view->priv->layout, iter, x, y);
 }
 
 /**
@@ -2113,38 +2114,39 @@ gtk_text_view_get_iter_at_location (GtkTextView *text_view,
  * @iter: (out): a #GtkTextIter
  * @trailing: (out) (allow-none): if non-%NULL, location to store an integer indicating where
  *    in the grapheme the user clicked. It will either be
- *    zero, or the number of characters in the grapheme. 
+ *    zero, or the number of characters in the grapheme.
  *    0 represents the trailing edge of the grapheme.
  * @x: x position, in buffer coordinates
  * @y: y position, in buffer coordinates
  *
- * Retrieves the iterator pointing to the character at buffer 
- * coordinates @x and @y. Buffer coordinates are coordinates for 
- * the entire buffer, not just the currently-displayed portion.  
- * If you have coordinates from an event, you have to convert 
- * those to buffer coordinates with 
+ * Retrieves the iterator pointing to the character at buffer
+ * coordinates @x and @y. Buffer coordinates are coordinates for
+ * the entire buffer, not just the currently-displayed portion.
+ * If you have coordinates from an event, you have to convert
+ * those to buffer coordinates with
  * gtk_text_view_window_to_buffer_coords().
  *
  * Note that this is different from gtk_text_view_get_iter_at_location(),
  * which returns cursor locations, i.e. positions between
  * characters.
  *
+ * Returns: %TRUE if the position is over text
+ *
  * Since: 2.6
  **/
-void
+gboolean
 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
-                                   GtkTextIter *iter,
-                                   gint        *trailing,
-                                   gint         x,
-                                   gint         y)
+                                    GtkTextIter *iter,
+                                    gint        *trailing,
+                                    gint         x,
+                                    gint         y)
 {
-  g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
-  g_return_if_fail (iter != NULL);
+  g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
+  g_return_val_if_fail (iter != NULL, FALSE);
 
   gtk_text_view_ensure_layout (text_view);
-  
-  gtk_text_layout_get_iter_at_position (text_view->priv->layout,
-                                       iter, trailing, x, y);
+
+  return gtk_text_layout_get_iter_at_position (text_view->priv->layout, iter, trailing, x, y);
 }
 
 /**
diff --git a/gtk/gtktextview.h b/gtk/gtktextview.h
index 432e7b7..0cf15c6 100644
--- a/gtk/gtktextview.h
+++ b/gtk/gtktextview.h
@@ -259,12 +259,12 @@ void           gtk_text_view_get_iter_location     (GtkTextView   *text_view,
                                                     const GtkTextIter *iter,
                                                     GdkRectangle  *location);
 GDK_AVAILABLE_IN_ALL
-void           gtk_text_view_get_iter_at_location  (GtkTextView   *text_view,
+gboolean       gtk_text_view_get_iter_at_location  (GtkTextView   *text_view,
                                                     GtkTextIter   *iter,
                                                     gint           x,
                                                     gint           y);
 GDK_AVAILABLE_IN_ALL
-void           gtk_text_view_get_iter_at_position  (GtkTextView   *text_view,
+gboolean       gtk_text_view_get_iter_at_position  (GtkTextView   *text_view,
                                                     GtkTextIter   *iter,
                                                    gint          *trailing,
                                                     gint           x,


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