/* Testcase for: Dialogs that save their positions when closing keep drifting downwards. To compile: gcc -Wall -o test-gtk-move-3 test-gtk-move-3.c `pkg-config --cflags --libs gtk+-2.0 glib-2.0` To use: X -multiwindow & export DISPLAY=:0 ./test-gtk-move-3 Press 'Show', then close the dialog, then press 'Show' again. The dialog should reopen at the same place. In fact, it will reopen a little lower each time. Repeated execution will cause it to drift downwards. */ #include #include GtkWidget *main_window; GtkWidget *tool_window; /* Hold the last dialog position */ gint tool_x = 100, tool_y = 100; static gboolean delete_tool_window( GtkWidget *widget, GdkEvent *event, gpointer data ) { /* At closing time, save the dialog position */ gtk_window_get_position (GTK_WINDOW (tool_window), &tool_x, &tool_y); g_printf("My position was: %d,%d\n",tool_x,tool_y); return FALSE; } static void destroy_tool_window( GtkWidget *widget, gpointer data ) { tool_window = NULL; } static void create_tool_window() { tool_window = gtk_window_new (GTK_WINDOW_TOPLEVEL); g_signal_connect (G_OBJECT (tool_window), "delete-event", G_CALLBACK (delete_tool_window), NULL); g_signal_connect (G_OBJECT (tool_window), "destroy", G_CALLBACK (destroy_tool_window), NULL); gtk_container_set_border_width (GTK_CONTAINER (tool_window), 10); } static void show_tool_window( GtkWidget *widget, gpointer data ) { /* If the dialog is closed, create it. */ if (!tool_window) create_tool_window(); else { gtk_window_get_position (GTK_WINDOW (tool_window), &tool_x, &tool_y); g_printf("My position is: %d,%d\n",tool_x,tool_y); } /* Move it to the saved position */ /* We move it before it is shown, to avoid flicker */ gtk_window_move (GTK_WINDOW (tool_window), tool_x, tool_y); /* Show the window */ gtk_widget_show (tool_window); } static void destroy( GtkWidget *widget, gpointer data ) { gtk_main_quit (); } int main( int argc, char *argv[] ) { GtkWidget *button; gtk_init (&argc, &argv); main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_decorated (GTK_WINDOW(main_window), TRUE); g_signal_connect (G_OBJECT (main_window), "destroy", G_CALLBACK (destroy), NULL); gtk_container_set_border_width (GTK_CONTAINER (main_window), 10); button = gtk_button_new_with_label ("Show"); g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (show_tool_window), NULL); gtk_container_add (GTK_CONTAINER (main_window), button); gtk_widget_show (button); gtk_widget_show (main_window); gtk_main (); return 0; }