Gtk4: Get the mouse position relative to widget

Finally I found the way to get the mouse position relative to widget, just share the Vala code for public, enjoy the code…

private void listbox_name_button_clicked(Gtk.Button sender) 
    var device_pointer= this.get_display().get_default_seat().get_pointer();
    GLib.return_if_fail(null != device_pointer);

    Gdk.ModifierType mask = 0;
    double x = 0, y = 0, x_new = 0, y_new = 0;
    GLib.return_if_fail(this.root.get_surface().get_device_position(device_pointer, out x, out y, out mask));
    GLib.return_if_fail(this.root.translate_coordinates(sender.parent, x, y, out x_new, out y_new));

    Gdk.Rectangle rect = { (int)x_new, (int)y_new, 0, 0, };
1 Like

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