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