]> Pileus Git - ~andy/gtk/blob - tests/testmultidisplay.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testmultidisplay.c
1 /* testmultidisplay.c
2  * Copyright (C) 2001 Sun Microsystems Inc.
3  * Author: Erwann Chenede <erwann.chenede@sun.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20 #include <gtk/gtk.h>
21
22 gchar *screen2_name = NULL;
23
24 typedef struct
25 {
26   GtkEntry *e1;
27   GtkEntry *e2;
28 }
29 MyDoubleGtkEntry;
30
31 void
32 get_screen_response (GtkDialog *dialog,
33                      gint       response_id,
34                      GtkEntry  *entry)
35 {
36   if (response_id == GTK_RESPONSE_DELETE_EVENT)
37     return;
38   g_free (screen2_name);
39   screen2_name = g_strdup (gtk_entry_get_text (entry));
40 }
41
42 void
43 entry_dialog_response (GtkDialog        *dialog,
44                        gint              response_id,
45                        MyDoubleGtkEntry *de)
46 {
47   if (response_id == GTK_RESPONSE_APPLY)
48     gtk_entry_set_text (de->e2, gtk_entry_get_text (de->e1));
49   else
50     gtk_main_quit ();
51 }
52
53 void
54 make_selection_dialog (GdkScreen * screen,
55                        GtkWidget * entry,
56                        GtkWidget * other_entry)
57 {
58   GtkWidget *window, *vbox;
59   GtkWidget *content_area;
60   MyDoubleGtkEntry *double_entry = g_new (MyDoubleGtkEntry, 1);
61   double_entry->e1 = GTK_ENTRY (entry);
62   double_entry->e2 = GTK_ENTRY (other_entry);
63
64   if (!screen)
65     screen = gdk_screen_get_default ();
66
67   window = g_object_new (GTK_TYPE_DIALOG,
68                            "screen", screen,
69                            "user_data", NULL,
70                            "type", GTK_WINDOW_TOPLEVEL,
71                            "title", "MultiDisplay Cut & Paste",
72                            "border_width", 10, NULL);
73   g_signal_connect (window, "destroy",
74                     G_CALLBACK (gtk_main_quit), NULL);
75
76   content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
77
78   vbox = g_object_new (GTK_TYPE_BOX,
79                          "border_width", 5,
80                          "orientation", GTK_ORIENTATION_VERTICAL,
81                          NULL);
82   gtk_box_pack_start (GTK_BOX (content_area), vbox, FALSE, FALSE, 0);
83
84   gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
85   gtk_widget_grab_focus (entry);
86
87   gtk_dialog_add_buttons (GTK_DIALOG (window),
88                           GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
89                           GTK_STOCK_QUIT, GTK_RESPONSE_DELETE_EVENT,
90                           NULL);
91   gtk_dialog_set_default_response (GTK_DIALOG (window), GTK_RESPONSE_APPLY);
92
93   g_signal_connect (window, "response",
94                     G_CALLBACK (entry_dialog_response), double_entry);
95
96   gtk_widget_show_all (window);
97 }
98
99 int
100 main (int argc, char *argv[])
101 {
102   GtkWidget *dialog, *display_entry, *dialog_label;
103   GtkWidget *entry, *entry2;
104   GtkWidget *content_area;
105   GdkDisplay *dpy2;
106   GdkScreen *scr2 = NULL;       /* Quiet GCC */
107   gboolean correct_second_display = FALSE;
108
109   gtk_init (&argc, &argv);
110
111   if (argc == 2)
112     screen2_name = g_strdup (argv[1]);
113   
114   /* Get the second display information */
115
116   dialog = gtk_dialog_new_with_buttons ("Second Display Selection",
117                                         NULL,
118                                         GTK_DIALOG_MODAL,
119                                         GTK_STOCK_OK,
120                                         GTK_RESPONSE_OK, NULL);
121
122   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
123   display_entry = gtk_entry_new ();
124   gtk_entry_set_activates_default (GTK_ENTRY (display_entry), TRUE);
125   dialog_label =
126     gtk_label_new ("Please enter the name of\nthe second display\n");
127
128   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
129
130   gtk_container_add (GTK_CONTAINER (content_area), dialog_label);
131   gtk_container_add (GTK_CONTAINER (content_area), display_entry);
132   g_signal_connect (dialog, "response",
133                     G_CALLBACK (get_screen_response), display_entry);
134
135   gtk_widget_grab_focus (display_entry);
136   gtk_widget_show_all (gtk_bin_get_child (GTK_BIN (dialog)));
137   
138   while (!correct_second_display)
139     {
140       if (screen2_name)
141         {
142           if (!g_ascii_strcasecmp (screen2_name, ""))
143             g_printerr ("No display name, reverting to default display\n");
144           
145           dpy2 = gdk_display_open (screen2_name);
146           if (dpy2)
147             {
148               scr2 = gdk_display_get_default_screen (dpy2);
149               correct_second_display = TRUE;
150             }
151           else
152             {
153               char *error_msg =
154                 g_strdup_printf  ("Can't open display :\n\t%s\nplease try another one\n",
155                                   screen2_name);
156               gtk_label_set_text (GTK_LABEL (dialog_label), error_msg);
157               g_free (error_msg);
158             }
159        }
160
161       if (!correct_second_display)
162         gtk_dialog_run (GTK_DIALOG (dialog));
163     }
164   
165   gtk_widget_destroy (dialog);
166
167   entry = g_object_new (GTK_TYPE_ENTRY,
168                           "activates_default", TRUE,
169                           "visible", TRUE,
170                           NULL);
171   entry2 = g_object_new (GTK_TYPE_ENTRY,
172                            "activates_default", TRUE,
173                            "visible", TRUE,
174                            NULL);
175
176   /* for default display */
177   make_selection_dialog (NULL, entry2, entry);
178   /* for selected display */
179   make_selection_dialog (scr2, entry, entry2);
180   gtk_main ();
181
182   return 0;
183 }