Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to write a script that detects when the user moves a Gtk2 window across the desktop.

Specifically, I want to know when the user releases the mouse button, at the end of the drag operation, so that I can check the window's new position.

This C tutorial has the right idea, but I can't work out how to do the same thing in Perl:

http://zetcode.com/tutorials/gtktutorial/gtkevents/

The Gtk2 Perl tutorial covers drag-and-drop operations, but I don't want to drop the Gtk2 window into another widget:

http://gtk2-perl.sourceforge.net/doc/gtk2-perl-study-guide/c5651.html

How can I amend the script below?

#!/usr/bin/perl package dragwin; use strict; use diagnostics; use warnings; use Gtk2 '-init'; # Want to detect when the user drags a window across the desktop # Specifically, when the user releases their mouse button, meaning the # drag operation has finished # Create the window my $window = Gtk2::Window->new(); $window->set_title('Drag me!'); $window->set_position('center'); $window->set_default_size(300, 300); $window->set_border_width(5); $window->signal_connect (destroy => sub { Gtk2->main_quit; }); # We want to detect the end of a drag, but this doesn't work $window->signal_connect('event' => sub { my ($widget, $event) = @_; print "Window event type: " . $event->type . "\n"; }); # Open the window $window->show_all(); Gtk2->main();

Replies are listed 'Best First'.
Re: Gtk2 window dragging
by FreeBeerReekingMonk (Deacon) on May 18, 2015 at 21:11 UTC
    #!/usr/bin/perl package dragwin; use strict; use diagnostics; use warnings; use Gtk2 '-init'; # Want to detect when the user drags a window across the desktop # Specifically, when the user releases their mouse button, meaning the # drag operation has finished my ($mouse_x, $mouse_y, $mask); my ($window_x, $window_y); # Create the window my $window = Gtk2::Window->new(); $window->set_title('Drag me!'); $window->set_position('center'); $window->set_default_size(300, 300); $window->set_border_width(5); $window->signal_connect (destroy => sub { Gtk2->main_quit; }); # We want to detect the end of a drag, but this doesn't work $window->signal_connect('event' => sub { my ($widget, $event) = @_; print "Window event type: " . $event->type . "\n"; exit 0 if $event->type eq "delete"; # Get mouse position (undef, $mouse_x, $mouse_y, $mask) = Gtk2::Gdk::Screen->get_default->get_root_window->get_pointer; # Get Windows (top-left) position ($window_x, $window_y) = $window->get_position; # print current positions print "POS($mouse_x,$mouse_y) - ($window_x, $window_y)\n"; }); # Open the window $window->show_all(); Gtk2->main();

    See a perlmonk post here: node_id=980493 and the GTK Window webpage

      Thanks! But, this only tells me how to check the window's current position. It doesn't tell me that a drag has started, or that a drag has stopped.

      I want to take some action at the end of the drag operation, for example displaying a message, or even moving the window back to its original position.

      I could check the window's position after every Gtk2 event, of course. But this only tells me that the window is moving. It doesn't tell me when the window has stopped moving.

        Hmm.. I am afraid you will need to do it that old fashioned way. Detect a focus change event, then check with your recorded X,Y, if the same, it is a window move start and on the next focus change event, that is the window move end...
        I tried signal_connect to all these other events: http://gtk2-perl.sourceforge.net/doc/gtk2-perl-tut/sec-Events.html They do not give anything for window...