tooltip: Fix positioning
authorMatthias Clasen <mclasen@redhat.com>
Sun, 17 May 2020 05:22:02 +0000 (01:22 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 17 May 2020 05:22:02 +0000 (01:22 -0400)
There are a few more places where we were forgetting
to apply the surface->native transform. With these
changes, tooltips are positioned correctly when
the toplevel has padding applied.

Fixes: #1619
gtk/gtktooltip.c

index a6962641af15e93034591001e47c9b504621634b..2e65ee1ca5e48430398a21d075e79ec79fba7305 100644 (file)
@@ -35,7 +35,7 @@
 #include "gtkwindowprivate.h"
 #include "gtkwidgetprivate.h"
 #include "gtkaccessible.h"
-#include "gtknative.h"
+#include "gtknativeprivate.h"
 
 /**
  * SECTION:gtktooltip
@@ -426,6 +426,7 @@ _gtk_widget_find_at_coords (GdkSurface *surface,
 {
   GtkWidget *event_widget;
   GtkWidget *picked_widget;
+  int native_x, native_y;
 
   g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
 
@@ -434,6 +435,10 @@ _gtk_widget_find_at_coords (GdkSurface *surface,
   if (!event_widget)
     return NULL;
 
+  gtk_native_get_surface_transform (GTK_NATIVE (event_widget), &native_x, &native_y);
+  surface_x -= native_x;
+  surface_y -= native_y;
+
   picked_widget = gtk_widget_pick (event_widget, surface_x, surface_y, GTK_PICK_INSENSITIVE);
 
   if (picked_widget != NULL)
@@ -583,17 +588,22 @@ gtk_tooltip_position (GtkTooltip *tooltip,
   int rect_anchor_dx = 0;
   int cursor_size;
   int anchor_rect_padding;
+  int native_x, native_y;
 
   gtk_widget_realize (GTK_WIDGET (tooltip->window));
 
   tooltip->tooltip_widget = new_tooltip_widget;
 
   toplevel = GTK_WIDGET (gtk_widget_get_native (new_tooltip_widget));
+  gtk_native_get_surface_transform (GTK_NATIVE (toplevel), &native_x, &native_y);
+
   if (gtk_widget_compute_bounds (new_tooltip_widget, toplevel, &anchor_bounds))
     {
       anchor_rect = (GdkRectangle) {
-        floorf (anchor_bounds.origin.x), floorf (anchor_bounds.origin.y),
-        ceilf (anchor_bounds.size.width), ceilf (anchor_bounds.size.height)
+        floorf (anchor_bounds.origin.x + native_x),
+        floorf (anchor_bounds.origin.y + native_y),
+        ceilf (anchor_bounds.size.width),
+        ceilf (anchor_bounds.size.height)
       };
     }
   else
@@ -896,16 +906,26 @@ _gtk_tooltip_handle_event (GtkWidget *target,
 {
   GdkEventType event_type;
   GdkSurface *surface;
-  gdouble dx, dy;
+  double x, y;
+  int native_x, native_y;
+  int tx, ty;
+  GtkWidget *native;
 
   if (!tooltips_enabled (event))
     return;
 
   event_type = gdk_event_get_event_type (event);
   surface = gdk_event_get_surface (event);
-  gdk_event_get_position (event, &dx, &dy);
+  gdk_event_get_position (event, &x, &y);
+  native = GTK_WIDGET (gtk_widget_get_native (target));
+
+  gtk_native_get_surface_transform (GTK_NATIVE (native), &native_x, &native_y);
+  gtk_widget_translate_coordinates (native, target,
+                                    x - native_x,
+                                    y - native_y,
+                                    &tx, &ty);
 
-  gtk_tooltip_handle_event_internal (event_type, surface, target, dx, dy);
+  gtk_tooltip_handle_event_internal (event_type, surface, target, tx, ty);
 }
 
 /* dx/dy must be in @target_widget's coordinates */
@@ -913,8 +933,8 @@ static void
 gtk_tooltip_handle_event_internal (GdkEventType   event_type,
                                    GdkSurface    *surface,
                                    GtkWidget     *target_widget,
-                                   gdouble       dx,
-                                   gdouble       dy)
+                                   gdouble        dx,
+                                   gdouble        dy)
 {
   int x = dx, y = dy;
   GdkDisplay *display;