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