1 #include <gtk/gtk.h>
2 
3 enum {
4   DESTROY_PARENT,
5   DETACH,
6   ATTACH_1,
7   ATTACH_2
8 };
9 
10 GtkWidget *window1, *window2;
11 
12 static void
dialog_response(GtkDialog * dialog,int response,gpointer user_data)13 dialog_response (GtkDialog *dialog, int response, gpointer user_data)
14 {
15   if (response == DESTROY_PARENT)
16     {
17       GtkWidget *window = GTK_WIDGET (gtk_window_get_transient_for (GTK_WINDOW (dialog)));
18 
19       if (window == window1)
20 	{
21 	  gtk_dialog_set_response_sensitive (dialog, ATTACH_1, FALSE);
22 	  window1 = NULL;
23 	}
24       else
25 	{
26 	  gtk_dialog_set_response_sensitive (dialog, ATTACH_2, FALSE);
27 	  window2 = NULL;
28 	}
29 
30       gtk_dialog_set_response_sensitive (dialog, DESTROY_PARENT, FALSE);
31       gtk_dialog_set_response_sensitive (dialog, DETACH, FALSE);
32       gtk_widget_destroy (window);
33     }
34   else if (response == DETACH)
35     {
36       gtk_window_set_transient_for (GTK_WINDOW (dialog), NULL);
37       gtk_dialog_set_response_sensitive (dialog, DESTROY_PARENT, FALSE);
38       gtk_dialog_set_response_sensitive (dialog, DETACH, FALSE);
39       gtk_dialog_set_response_sensitive (dialog, ATTACH_1, window1 != NULL);
40       gtk_dialog_set_response_sensitive (dialog, ATTACH_2, window2 != NULL);
41     }
42   else if (response == ATTACH_1)
43     {
44       gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window1));
45       gtk_dialog_set_response_sensitive (dialog, DESTROY_PARENT, TRUE);
46       gtk_dialog_set_response_sensitive (dialog, DETACH, TRUE);
47       gtk_dialog_set_response_sensitive (dialog, ATTACH_1, FALSE);
48       gtk_dialog_set_response_sensitive (dialog, ATTACH_2, window2 != NULL);
49     }
50   else if (response == ATTACH_2)
51     {
52       gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window2));
53       gtk_dialog_set_response_sensitive (dialog, DESTROY_PARENT, TRUE);
54       gtk_dialog_set_response_sensitive (dialog, DETACH, TRUE);
55       gtk_dialog_set_response_sensitive (dialog, ATTACH_1, window1 != NULL);
56       gtk_dialog_set_response_sensitive (dialog, ATTACH_2, FALSE);
57     }
58   else if (response == GTK_RESPONSE_CLOSE)
59     gtk_main_quit ();
60 }
61 
62 int
main(int argc,char ** argv)63 main (int argc, char **argv)
64 {
65   GtkWidget *dialog;
66 
67   gtk_init (&argc, &argv);
68 
69   window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
70   gtk_window_set_title (GTK_WINDOW (window1), "Parent 1");
71   gtk_widget_show (window1);
72 
73   window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
74   gtk_window_set_title (GTK_WINDOW (window2), "Parent 2");
75   gtk_widget_show (window2);
76 
77   dialog = gtk_dialog_new_with_buttons ("Child",
78 					NULL,
79 					GTK_DIALOG_MODAL,
80 					"Destroy Parent",
81 					DESTROY_PARENT,
82 					"Detach",
83 					DETACH,
84 					"Attach to 1",
85 					ATTACH_1,
86 					"Attach to 2",
87 					ATTACH_2,
88 					"Quit",
89 					GTK_RESPONSE_CLOSE,
90 					NULL);
91   gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), DESTROY_PARENT, FALSE);
92   gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), DETACH, FALSE);
93 
94   g_signal_connect (dialog, "response", G_CALLBACK (dialog_response), NULL);
95   gtk_widget_show (dialog);
96 
97   gtk_main ();
98 
99   return 0;
100 }
101