Is gdk_event_get_position () of GDK4 a direct replacement for gdk_event_get_coords() of GDK3?

I am using GtkEventControllerLegacy for a simple CAD app with GtkDrawingArea.

From the API docs or Google query it is not really clear. I did a test late yesterday evening, and gdk-event-get-position() seems to have an offset, I do not get (0,0) for top left corner of my drawing area. But maybe there is a bug in my own app? For me an offset would make some sense, as the GDK allocation may be larger than the actual visible content of the GtkDrawingArea, and maybe different name Coord vs Position should indicate exactly that? But then, how may I remove the offset.

Maybe it is really not easy?

A closely related question: When I am using the GtkEventControllerLegacy from

may I use then at the same time other controllers like GtkGestureDrag or GtkEventControllerMotion? The later may give me the desired absolute coordinates, but I would assume that using the legacy controller at the same time will not work, as the legacy controller works also with motion events.

At least the issue seems to be not related to the Nim bindings or my tiny CAD program.

I did a test with drawing.c GTK4 example program. The default drag gestures seems to have correct coordinates. But when I use instead

gboolean
legcb(GtkEventControllerLegacy *controller,
               GdkEvent                 *event,
               gpointer                  user_data)
{
  double x, y;
  gdk_event_get_position (event, &x, &y);
  printf("%f  %f\n", x, y);
  return TRUE;
}

....

   GtkEventController *leg;
  leg =gtk_event_controller_legacy_new ();
  gtk_widget_add_controller (drawing_area, GTK_EVENT_CONTROLLER (leg));
  g_signal_connect (leg, "event", G_CALLBACK (legcb), drawing_area);

we have an y offset of approx 70 and an x offset of approx 30, so coordinates seems to be related to the parent window.

[EDIT]

Still unsolved. And I am not sure if I don’t get to manage the right coordinate transformation working or if it a GTK4 bug.

[EDIT2]

After reading GTK4 API docs for a few hours and using grep a lot on GTK4 sources I just came to

gboolean
legcb(GtkEventControllerLegacy *controller,
               GdkEvent                 *event,
               GtkWidget                  *widget)
{
  double x, y;
  double nx, ny;
  gdk_event_get_position (event, &x, &y);
  GtkNative *native;
  native = gtk_widget_get_native (widget);
  gtk_native_get_surface_transform (native, &nx, &ny);  
  GtkWidget *toplevel = GTK_WIDGET (gtk_widget_get_root (widget));
  gtk_widget_translate_coordinates (toplevel, widget, x - nx, y - ny, &x, &y);
  printf("%f  %f\n", x, y);
  return TRUE;
}

That transformation gives me indeed values close to zero for top left edge of my drawing area.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.