]> Pileus Git - ~andy/gtk/blob - tests/testmultidisplay.c
Updated Traditional Chinese translation (Hong Kong and Taiwan)
[~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
24 gchar *screen2_name = NULL;
25
26 typedef struct
27 {
28   GtkEntry *e1;
29   GtkEntry *e2;
30 }
31 MyDoubleGtkEntry;
32
33 void
34 get_screen_response (GtkDialog *dialog,
35                      gint       response_id,
36                      GtkEntry  *entry)
37 {
38   if (response_id == GTK_RESPONSE_DELETE_EVENT)
39     return;
40   g_free (screen2_name);
41   screen2_name = g_strdup (gtk_entry_get_text (entry));
42 }
43
44 void
45 entry_dialog_response (GtkDialog        *dialog,
46                        gint              response_id,
47                        MyDoubleGtkEntry *de)
48 {
49   if (response_id == GTK_RESPONSE_APPLY)
50     gtk_entry_set_text (de->e2, gtk_entry_get_text (de->e1));
51   else
52     gtk_main_quit ();
53 }
54
55 void
56 make_selection_dialog (GdkScreen * screen,
57                        GtkWidget * entry,
58                        GtkWidget * other_entry)
59 {
60   GtkWidget *window, *vbox;
61   GtkWidget *content_area;
62   MyDoubleGtkEntry *double_entry = g_new (MyDoubleGtkEntry, 1);
63   double_entry->e1 = GTK_ENTRY (entry);
64   double_entry->e2 = GTK_ENTRY (other_entry);
65
66   if (!screen)
67     screen = gdk_screen_get_default ();
68
69   window = g_object_new (GTK_TYPE_DIALOG,
70                            "screen", screen,
71                            "user_data", NULL,
72                            "type", GTK_WINDOW_TOPLEVEL,
73                            "title", "MultiDisplay Cut & Paste",
74                            "border_width", 10, NULL);
75   g_signal_connect (window, "destroy",
76                     G_CALLBACK (gtk_main_quit), NULL);
77
78   content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
79
80   vbox = g_object_new (GTK_TYPE_VBOX,
81                          "border_width", 5,
82                          NULL);
83   gtk_box_pack_start (GTK_BOX (content_area), vbox, FALSE, FALSE, 0);
84
85   gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
86   gtk_widget_grab_focus (entry);
87
88   gtk_dialog_add_buttons (GTK_DIALOG (window),
89                           GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
90                           GTK_STOCK_QUIT, GTK_RESPONSE_DELETE_EVENT,
91                           NULL);
92   gtk_dialog_set_default_response (GTK_DIALOG (window), GTK_RESPONSE_APPLY);
93
94   g_signal_connect (window, "response",
95                     G_CALLBACK (entry_dialog_response), double_entry);
96
97   gtk_widget_show_all (window);
98 }
99
100 int
101 main (int argc, char *argv[])
102 {
103   GtkWidget *dialog, *display_entry, *dialog_label;
104   GtkWidget *entry, *entry2;
105   GtkWidget *content_area;
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   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
130
131   gtk_container_add (GTK_CONTAINER (content_area), dialog_label);
132   gtk_container_add (GTK_CONTAINER (content_area), display_entry);
133   g_signal_connect (dialog, "response",
134                     G_CALLBACK (get_screen_response), display_entry);
135
136   gtk_widget_grab_focus (display_entry);
137   gtk_widget_show_all (gtk_bin_get_child (GTK_BIN (dialog)));
138   
139   while (!correct_second_display)
140     {
141       if (screen2_name)
142         {
143           if (!g_ascii_strcasecmp (screen2_name, ""))
144             g_printerr ("No display name, reverting to default display\n");
145           
146           dpy2 = gdk_display_open (screen2_name);
147           if (dpy2)
148             {
149               scr2 = gdk_display_get_default_screen (dpy2);
150               correct_second_display = TRUE;
151             }
152           else
153             {
154               char *error_msg =
155                 g_strdup_printf  ("Can't open display :\n\t%s\nplease try another one\n",
156                                   screen2_name);
157               gtk_label_set_text (GTK_LABEL (dialog_label), error_msg);
158               g_free (error_msg);
159             }
160        }
161
162       if (!correct_second_display)
163         gtk_dialog_run (GTK_DIALOG (dialog));
164     }
165   
166   gtk_widget_destroy (dialog);
167
168   entry = g_object_new (GTK_TYPE_ENTRY,
169                           "activates_default", TRUE,
170                           "visible", TRUE,
171                           NULL);
172   entry2 = g_object_new (GTK_TYPE_ENTRY,
173                            "activates_default", TRUE,
174                            "visible", TRUE,
175                            NULL);
176
177   /* for default display */
178   make_selection_dialog (NULL, entry2, entry);
179   /* for selected display */
180   make_selection_dialog (scr2, entry, entry2);
181   gtk_main ();
182
183   return 0;
184 }