]> Pileus Git - ~andy/gtk/blob - tests/testsocket_common.c
Add Copyright/License information.
[~andy/gtk] / tests / testsocket_common.c
1 /* testsocket_common.c
2  * Copyright (C) 2001 Red Hat, Inc
3  * Author: Owen Taylor
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 #undef GTK_DISABLE_DEPRECATED
22
23 #include <config.h>
24 #include "x11/gdkx.h"
25 #include <gtk/gtk.h>
26
27 enum
28 {
29   ACTION_FILE_NEW,
30   ACTION_FILE_OPEN,
31   ACTION_OK,
32   ACTION_HELP_ABOUT
33 };
34
35 static void
36 print_hello (GtkWidget *w,
37              guint      action)
38 {
39   switch (action)
40     {
41     case ACTION_FILE_NEW:
42       g_message ("File New activated");
43       break;
44     case ACTION_FILE_OPEN:
45       g_message ("File Open activated");
46       break;
47     case ACTION_OK:
48       g_message ("OK activated");
49       break;
50     case ACTION_HELP_ABOUT:
51       g_message ("Help About activated ");
52       break;
53     default:
54       g_assert_not_reached ();
55       break;
56     }
57 }
58
59 static GtkItemFactoryEntry menu_items[] = {
60   { "/_File",         NULL,         NULL,           0, "<Branch>" },
61   { "/File/_New",     "<control>N", print_hello,    ACTION_FILE_NEW, "<Item>" },
62   { "/File/_Open",    "<control>O", print_hello,    ACTION_FILE_OPEN, "<Item>" },
63   { "/File/sep1",     NULL,         NULL,           0, "<Separator>" },
64   { "/File/Quit",     "<control>Q", gtk_main_quit,  0, "<Item>" },
65   { "/O_K",            "<control>K",print_hello,    ACTION_OK, "<Item>" },
66   { "/_Help",         NULL,         NULL,           0, "<LastBranch>" },
67   { "/_Help/About",   NULL,         print_hello,    ACTION_HELP_ABOUT, "<Item>" },
68 };
69
70 static void
71 remove_buttons (GtkWidget *widget, GtkWidget *other_button)
72 {
73   gtk_widget_destroy (other_button);
74   gtk_widget_destroy (widget);
75 }
76
77 static gboolean
78 blink_cb (gpointer data)
79 {
80   GtkWidget *widget = data;
81
82   gtk_widget_show (widget);
83   g_object_set_data (G_OBJECT (widget), "blink", NULL);
84
85   return FALSE;
86 }
87
88 static void
89 blink (GtkWidget *widget,
90        GtkWidget *window)
91 {
92   guint blink_timeout = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (window), "blink"));
93   
94   if (!blink_timeout)
95     {
96       blink_timeout = g_timeout_add (1000, blink_cb, window);
97       gtk_widget_hide (window);
98
99       g_object_set_data (G_OBJECT (window), "blink", GUINT_TO_POINTER (blink_timeout));
100     }
101 }
102
103 static void
104 local_destroy (GtkWidget *window)
105 {
106   guint blink_timeout = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (window), "blink"));
107   if (blink_timeout)
108     g_source_remove (blink_timeout);
109 }
110
111 static void
112 remote_destroy (GtkWidget *window)
113 {
114   local_destroy (window);
115   gtk_main_quit ();
116 }
117
118 static void
119 add_buttons (GtkWidget *widget, GtkWidget *box)
120 {
121   GtkWidget *add_button;
122   GtkWidget *remove_button;
123
124   add_button = gtk_button_new_with_mnemonic ("_Add");
125   gtk_box_pack_start (GTK_BOX (box), add_button, TRUE, TRUE, 0);
126   gtk_widget_show (add_button);
127
128   g_signal_connect (add_button, "clicked",
129                     G_CALLBACK (add_buttons),
130                     box);
131
132   remove_button = gtk_button_new_with_mnemonic ("_Remove");
133   gtk_box_pack_start (GTK_BOX (box), remove_button, TRUE, TRUE, 0);
134   gtk_widget_show (remove_button);
135
136   g_signal_connect (remove_button, "clicked",
137                     G_CALLBACK (remove_buttons),
138                     add_button);
139 }
140
141 static GtkWidget *
142 create_combo (void)
143 {
144   GList *cbitems;
145   GtkCombo *combo;
146
147   cbitems = NULL;
148   cbitems = g_list_append (cbitems, "item0");
149   cbitems = g_list_append (cbitems, "item1 item1");
150   cbitems = g_list_append (cbitems, "item2 item2 item2");
151   cbitems = g_list_append (cbitems, "item3 item3 item3 item3");
152   cbitems = g_list_append (cbitems, "item4 item4 item4 item4 item4");
153   cbitems = g_list_append (cbitems, "item5 item5 item5 item5 item5 item5");
154   cbitems = g_list_append (cbitems, "item6 item6 item6 item6 item6");
155   cbitems = g_list_append (cbitems, "item7 item7 item7 item7");
156   cbitems = g_list_append (cbitems, "item8 item8 item8");
157   cbitems = g_list_append (cbitems, "item9 item9");
158
159   combo = GTK_COMBO (gtk_combo_new ());
160   gtk_combo_set_popdown_strings (combo, cbitems);
161   gtk_entry_set_text (GTK_ENTRY (combo->entry), "hello world");
162   gtk_editable_select_region (GTK_EDITABLE (combo->entry), 0, -1);
163
164   return GTK_WIDGET (combo);
165 }
166
167 static GtkWidget *
168 create_menubar (GtkWindow *window)
169 {
170   GtkItemFactory *item_factory;
171   GtkAccelGroup *accel_group=NULL;
172   GtkWidget *menubar;
173   
174   accel_group = gtk_accel_group_new ();
175   item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>",
176                                        accel_group);
177   gtk_item_factory_create_items (item_factory,
178                                  G_N_ELEMENTS (menu_items),
179                                  menu_items, NULL);
180   
181   gtk_window_add_accel_group (window, accel_group);
182   menubar = gtk_item_factory_get_widget (item_factory, "<main>");
183
184   return menubar;
185 }
186
187 static GtkWidget *
188 create_combo_box (void)
189 {
190   GtkComboBox *combo_box = GTK_COMBO_BOX (gtk_combo_box_new_text ());
191
192   gtk_combo_box_append_text (combo_box, "This");
193   gtk_combo_box_append_text (combo_box, "Is");
194   gtk_combo_box_append_text (combo_box, "A");
195   gtk_combo_box_append_text (combo_box, "ComboBox");
196   
197   return GTK_WIDGET (combo_box);
198 }
199
200 static GtkWidget *
201 create_content (GtkWindow *window, gboolean local)
202 {
203   GtkWidget *vbox;
204   GtkWidget *button;
205   GtkWidget *frame;
206
207   frame = gtk_frame_new (local? "Local" : "Remote");
208   gtk_container_set_border_width (GTK_CONTAINER (frame), 3);
209   vbox = gtk_vbox_new (TRUE, 0);
210   gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
211
212   gtk_container_add (GTK_CONTAINER (frame), vbox);
213   
214   /* Combo */
215   gtk_box_pack_start (GTK_BOX (vbox), create_combo(), TRUE, TRUE, 0);
216
217   /* Entry */
218   gtk_box_pack_start (GTK_BOX (vbox), gtk_entry_new(), TRUE, TRUE, 0);
219
220   /* Close Button */
221   button = gtk_button_new_with_mnemonic ("_Close");
222   gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
223   g_signal_connect_swapped (button, "clicked",
224                             G_CALLBACK (gtk_widget_destroy), window);
225
226   /* Blink Button */
227   button = gtk_button_new_with_mnemonic ("_Blink");
228   gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
229   g_signal_connect (button, "clicked",
230                     G_CALLBACK (blink),
231                     window);
232
233   /* Menubar */
234   gtk_box_pack_start (GTK_BOX (vbox), create_menubar (GTK_WINDOW (window)),
235                       TRUE, TRUE, 0);
236
237   /* Combo Box */
238   gtk_box_pack_start (GTK_BOX (vbox), create_combo_box (), TRUE, TRUE, 0);
239   
240   add_buttons (NULL, vbox);
241
242   return frame;
243 }
244
245 guint32
246 create_child_plug (guint32  xid,
247                    gboolean local)
248 {
249   GtkWidget *window;
250   GtkWidget *content;
251
252   window = gtk_plug_new (xid);
253
254   g_signal_connect (window, "destroy",
255                     local ? G_CALLBACK (local_destroy)
256                           : G_CALLBACK (remote_destroy),
257                     NULL);
258   gtk_container_set_border_width (GTK_CONTAINER (window), 0);
259
260   content = create_content (GTK_WINDOW (window), local);
261   
262   gtk_container_add (GTK_CONTAINER (window), content);
263
264   gtk_widget_show_all (window);
265
266   if (GTK_WIDGET_REALIZED (window))
267     return GDK_WINDOW_XID (window->window);
268   else
269     return 0;
270 }