]> Pileus Git - ~andy/gtk/blob - tests/teststatusicon.c
Make it possible to track the embeddedness of statusicon (#387215, Martyn
[~andy/gtk] / tests / teststatusicon.c
1 /* gtkstatusicon-x11.c:
2  *
3  * Copyright (C) 2003 Sun Microsystems, Inc.
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  * Authors:
21  *      Mark McLoughlin <mark@skynet.ie>
22  */
23
24 #include <gtk/gtk.h>
25
26 typedef enum
27 {
28   TEST_STATUS_INFO,
29   TEST_STATUS_QUESTION
30 } TestStatus;
31
32 static TestStatus status = TEST_STATUS_INFO;
33 static gint timeout = 0;
34 static GSList *icons = NULL;
35
36 static void
37 size_changed_cb (GtkStatusIcon *icon,
38                  int size)
39 {
40   g_print ("status icon %p size-changed size = %d\n", icon, size);
41 }
42
43 static void
44 embedded_changed_cb (GtkStatusIcon *icon)
45 {
46   g_print ("status icon %p embedded changed to %d\n", icon,
47            gtk_status_icon_is_embedded (icon));
48 }
49
50 static void
51 orientation_changed_cb (GtkStatusIcon *icon)
52 {
53   GtkOrientation orientation;
54
55   g_object_get (icon, "orientation", &orientation, NULL);
56   g_print ("status icon %p orientation changed to %d\n", icon, orientation);
57 }
58
59 static void
60 update_icons (void)
61 {
62   GSList *l;
63   gchar *icon_name;
64   gchar *tooltip;
65
66   if (status == TEST_STATUS_INFO)
67     {
68       icon_name = GTK_STOCK_DIALOG_INFO;
69       tooltip = "Some Infromation ...";
70     }
71   else
72     {
73       icon_name = GTK_STOCK_DIALOG_QUESTION;
74       tooltip = "Some Question ...";
75     }
76
77   for (l = icons; l; l = l->next)
78     {
79       GtkStatusIcon *status_icon = l->data;
80
81       gtk_status_icon_set_from_icon_name (status_icon, icon_name);
82       gtk_status_icon_set_tooltip (status_icon, tooltip);
83     }
84 }
85
86 static gboolean
87 timeout_handler (gpointer data)
88 {
89   if (status == TEST_STATUS_INFO)
90     status = TEST_STATUS_QUESTION;
91   else
92     status = TEST_STATUS_INFO;
93
94   update_icons ();
95
96   return TRUE;
97 }
98
99 static void
100 blink_toggle_toggled (GtkToggleButton *toggle)
101 {
102   GSList *l;
103
104   for (l = icons; l; l = l->next)
105     gtk_status_icon_set_blinking (GTK_STATUS_ICON (l->data), 
106                                   gtk_toggle_button_get_active (toggle));
107 }
108
109 static void
110 visible_toggle_toggled (GtkToggleButton *toggle)
111 {
112   GSList *l;
113
114   for (l = icons; l; l = l->next)
115     gtk_status_icon_set_visible (GTK_STATUS_ICON (l->data),
116                                  gtk_toggle_button_get_active (toggle));
117 }
118
119 static void
120 timeout_toggle_toggled (GtkToggleButton *toggle)
121 {
122   if (timeout)
123     {
124       g_source_remove (timeout);
125       timeout = 0;
126     }
127   else
128     {
129       timeout = g_timeout_add (2000, timeout_handler, NULL);
130     }
131 }
132
133 static void
134 icon_activated (GtkStatusIcon *icon)
135 {
136   GtkWidget *dialog;
137   GtkWidget *toggle;
138
139   dialog = g_object_get_data (G_OBJECT (icon), "test-status-icon-dialog");
140   if (dialog == NULL)
141     {
142       dialog = gtk_message_dialog_new (NULL, 0,
143                                        GTK_MESSAGE_QUESTION,
144                                        GTK_BUTTONS_CLOSE,
145                                        "You wanna test the status icon ?");
146
147       gtk_window_set_screen (GTK_WINDOW (dialog), gtk_status_icon_get_screen (icon));
148       gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
149
150       g_object_set_data_full (G_OBJECT (icon), "test-status-icon-dialog",
151                               dialog, (GDestroyNotify) gtk_widget_destroy);
152
153       g_signal_connect (dialog, "response", 
154                         G_CALLBACK (gtk_widget_hide), NULL);
155       g_signal_connect (dialog, "delete_event", 
156                         G_CALLBACK (gtk_widget_hide_on_delete), NULL);
157
158       toggle = gtk_toggle_button_new_with_mnemonic ("_Show the icon");
159       gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), toggle, TRUE, TRUE, 6);
160       gtk_widget_show (toggle);
161
162       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
163                                     gtk_status_icon_get_visible (icon));
164       g_signal_connect (toggle, "toggled", 
165                         G_CALLBACK (visible_toggle_toggled), NULL);
166
167       toggle = gtk_toggle_button_new_with_mnemonic ("_Blink the icon");
168       gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), toggle, TRUE, TRUE, 6);
169       gtk_widget_show (toggle);
170
171       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
172                                     gtk_status_icon_get_blinking (icon));
173       g_signal_connect (toggle, "toggled", 
174                         G_CALLBACK (blink_toggle_toggled), NULL);
175
176       toggle = gtk_toggle_button_new_with_mnemonic ("_Change images");
177       gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), toggle, TRUE, TRUE, 6);
178       gtk_widget_show (toggle);
179
180       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
181                                     timeout != 0);
182       g_signal_connect (toggle, "toggled", 
183                         G_CALLBACK (timeout_toggle_toggled), NULL);
184     }
185
186   gtk_window_present (GTK_WINDOW (dialog));
187 }
188
189 static void
190 check_activated (GtkCheckMenuItem *item)
191 {
192   GSList *l;
193   GdkScreen *screen;
194
195   screen = NULL;
196
197   for (l = icons; l; l = l->next)
198     {
199       GtkStatusIcon *icon = l->data;
200       GdkScreen *orig_screen;
201
202       orig_screen = gtk_status_icon_get_screen (icon);
203
204       if (screen != NULL)
205         gtk_status_icon_set_screen (icon, screen);
206
207       screen = orig_screen;
208
209       gtk_status_icon_set_blinking (icon,
210                                     gtk_check_menu_item_get_active (item));
211     }
212
213   g_assert (screen != NULL);
214
215   gtk_status_icon_set_screen (GTK_STATUS_ICON (icons->data), screen);
216 }
217
218 static void
219 do_quit (GtkMenuItem *item)
220 {
221   GSList *l;
222
223   for (l = icons; l; l = l->next)
224     {
225       GtkStatusIcon *icon = l->data;
226
227       gtk_status_icon_set_visible (icon, FALSE);
228       g_object_unref (icon);
229     }
230
231   g_slist_free (icons);
232   icons = NULL;
233
234   gtk_main_quit ();
235 }
236
237 static void 
238 popup_menu (GtkStatusIcon *icon,
239             guint          button,
240             guint32        activate_time)
241 {
242   GtkWidget *menu, *menuitem;
243
244   menu = gtk_menu_new ();
245
246   gtk_menu_set_screen (GTK_MENU (menu),
247                        gtk_status_icon_get_screen (icon));
248
249   menuitem = gtk_check_menu_item_new_with_label ("Blink");
250   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), 
251                                   gtk_status_icon_get_blinking (icon));
252   g_signal_connect (menuitem, "activate", G_CALLBACK (check_activated), NULL);
253
254   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
255
256   gtk_widget_show (menuitem);
257
258   menuitem = gtk_menu_item_new_with_label ("Quit");
259   g_signal_connect (menuitem, "activate", G_CALLBACK (do_quit), NULL);
260
261   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
262
263   gtk_widget_show (menuitem);
264
265   gtk_menu_popup (GTK_MENU (menu), 
266                   NULL, NULL, NULL, NULL, 
267                   button, activate_time);
268 }
269
270 int
271 main (int argc, char **argv)
272 {
273   GdkDisplay *display;
274   guint n_screens, i;
275
276   gtk_init (&argc, &argv);
277
278   display = gdk_display_get_default ();
279
280   n_screens = gdk_display_get_n_screens (display);
281
282   for (i = 0; i < n_screens; i++)
283     {
284       GtkStatusIcon *icon;
285
286       icon = gtk_status_icon_new ();
287       gtk_status_icon_set_screen (icon, gdk_display_get_screen (display, i));
288       update_icons ();
289
290       g_signal_connect (icon, "size-changed", G_CALLBACK (size_changed_cb), NULL);
291       g_signal_connect (icon, "notify::embedded", G_CALLBACK (embedded_changed_cb), NULL);
292       g_signal_connect (icon, "notify::orientation", G_CALLBACK (orientation_changed_cb), NULL);
293       g_print ("icon size %d\n", gtk_status_icon_get_size (icon));
294       gtk_status_icon_set_blinking (GTK_STATUS_ICON (icon), FALSE);
295
296       g_signal_connect (icon, "activate",
297                         G_CALLBACK (icon_activated), NULL);
298
299       g_signal_connect (icon, "popup-menu",
300                         G_CALLBACK (popup_menu), NULL);
301
302       icons = g_slist_append (icons, icon);
303  
304       timeout = g_timeout_add (2000, timeout_handler, icon);
305     }
306
307   gtk_main ();
308
309   return 0;
310 }