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