summaryrefslogtreecommitdiff
path: root/test-gtk-move-3.c
blob: d98573981d78ae4202260f91aa1aba9c28f9df63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* 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 <glib/gprintf.h>
#include <gtk/gtk.h>

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;
}