Wednesday, May 16, 2007

gtk: when the window stopped moving?

It's very common to use the following gtk function to start to move the window when the user pressed the left button:


The control of the window is passed to the window manager, which will move the window. However, you may want to find out when the window stopped moving in order to update something, like a background image. There is no specific signal for that and trying button-release-event is something that does not work either (* - see below).

A friend of mine suggestion a good solution for that. First, you connect to in-focus-event. This event will be emitted right after you stop moving the window. But, it is emitted whenever you click on the window, even when not dragging. Then, what you can do is the following:

gboolean on_focus_in_event (GtkWidget* pWidget, GdkEvent* event)
if (moving) {
moving = FALSE;
// move stopped
// do whatever you need
return FALSE;

gboolean on_button_press (GtkWidget* widget,
GdkEventButton* button, GdkWindowEdge edge)
if (button->type == GDK_BUTTON_PRESS &&
button->button == 1)
moving = TRUE;
GTK_WINDOW (gtk_widget_get_toplevel (pWidget)),
button->button, button->x_root, button->y_root,


gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK);

g_signal_connect (G_OBJECT (window), "button-press-event",
G_CALLBACK (on_button_press), NULL);
g_signal_connect (G_OBJECT (window), "focus-in-event",
G_CALLBACK (on_focus_in_event), NULL);

(*) The button-press-event works when you click but does not move the window. So, you can connect to the button-press-event to detect this case and undo anything you may have done when the user first clicked.
Post a Comment