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