]> Pileus Git - ~andy/gtk/blob - tests/testmultidisplay.c
Translation updated by Ivar Smolin.
[~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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include <gtk/gtk.h>
23 #include <gtk/gtkstock.h>
24 #include <gdk/gdk.h>
25
26 gchar *screen2_name = NULL;
27
28 typedef struct
29 {
30   GtkEntry *e1;
31   GtkEntry *e2;
32 }
33 MyDoubleGtkEntry;
34
35 void
36 get_screen_response (GtkDialog *dialog,
37                      gint       response_id,
38                      GtkEntry  *entry)
39 {
40   if (response_id == GTK_RESPONSE_DELETE_EVENT)
41     return;
42   if (screen2_name)
43     g_free (screen2_name);
44   screen2_name = g_strdup (gtk_entry_get_text (entry));
45 }
46
47 void
48 entry_dialog_response (GtkDialog        *dialog,
49                        gint              response_id,
50                        MyDoubleGtkEntry *de)
51 {
52   if (response_id == GTK_RESPONSE_APPLY)
53     gtk_entry_set_text (de->e2, gtk_entry_get_text (de->e1));
54   else
55     gtk_main_quit ();
56 }
57
58 void
59 make_selection_dialog (GdkScreen * screen,
60                        GtkWidget * entry,
61                        GtkWidget * other_entry)
62 {
63   GtkWidget *window, *vbox;
64   MyDoubleGtkEntry *double_entry = g_new (MyDoubleGtkEntry, 1);
65   double_entry->e1 = GTK_ENTRY (entry);
66   double_entry->e2 = GTK_ENTRY (other_entry);
67
68   if (!screen)
69     screen = gdk_screen_get_default ();
70
71   window = gtk_widget_new (GTK_TYPE_DIALOG,
72                            "screen", screen,
73                            "user_data", NULL,
74                            "type", GTK_WINDOW_TOPLEVEL,
75                            "title", "MultiDisplay Cut & Paste",
76                            "border_width", 10, NULL);
77   g_signal_connect (window, "destroy",
78                     G_CALLBACK (gtk_main_quit), NULL);
79
80
81   vbox = gtk_widget_new (GTK_TYPE_VBOX,
82                          "border_width", 5,
83                          NULL);
84   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, FALSE, FALSE, 0);
85
86   gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
87   gtk_widget_grab_focus (entry);
88
89   gtk_dialog_add_buttons (GTK_DIALOG (window),
90                           GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
91                           GTK_STOCK_QUIT, GTK_RESPONSE_DELETE_EVENT,
92                           NULL);
93   gtk_dialog_set_default_response (GTK_DIALOG (window), GTK_RESPONSE_APPLY);
94
95   g_signal_connect (window, "response",
96                     G_CALLBACK (entry_dialog_response), double_entry);
97
98   gtk_widget_show_all (window);
99 }
100
101 int
102 main (int argc, char *argv[])
103 {
104   GtkWidget *dialog, *display_entry, *dialog_label;
105   GtkWidget *entry, *entry2;
106   GdkDisplay *dpy2;
107   GdkScreen *scr2 = NULL;       /* Quiet GCC */
108   gboolean correct_second_display = FALSE;
109
110   gtk_init (&argc, &argv);
111
112   if (argc == 2)
113     screen2_name = g_strdup (argv[1]);
114   
115   /* Get the second display information */
116
117   dialog = gtk_dialog_new_with_buttons ("Second Display Selection",
118                                         NULL,
119                                         GTK_DIALOG_MODAL,
120                                         GTK_STOCK_OK,
121                                         GTK_RESPONSE_OK, NULL);
122
123   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
124   display_entry = gtk_entry_new ();
125   gtk_entry_set_activates_default (GTK_ENTRY (display_entry), TRUE);
126   dialog_label =
127     gtk_label_new ("Please enter the name of\nthe second display\n");
128
129   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), dialog_label);
130   gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
131                      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 (dialog)->child);
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 = gtk_widget_new (GTK_TYPE_ENTRY,
168                           "activates_default", TRUE,
169                           "visible", TRUE,
170                           NULL);
171   entry2 = gtk_widget_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 }