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