]> Pileus Git - ~andy/gtk/blob - tests/teststatusicon.c
Position the dialog with GTK_WIN_POS_CENTER. (do_quit): New function, hide
[~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
35 static void
36 update_icon (GtkStatusIcon *status_icon)
37 {
38   gchar *icon_name;
39   gchar *tooltip;
40
41   if (status == TEST_STATUS_INFO)
42     {
43       icon_name = GTK_STOCK_DIALOG_INFO;
44       tooltip = "Some Infromation ...";
45     }
46   else
47     {
48       icon_name = GTK_STOCK_DIALOG_QUESTION;
49       tooltip = "Some Question ...";
50     }
51
52   gtk_status_icon_set_from_icon_name (status_icon, icon_name);
53   gtk_status_icon_set_tooltip (status_icon, tooltip);
54 }
55
56 static gboolean
57 timeout_handler (gpointer data)
58 {
59   GtkStatusIcon *icon = GTK_STATUS_ICON (data);
60
61   if (status == TEST_STATUS_INFO)
62     status = TEST_STATUS_QUESTION;
63   else
64     status = TEST_STATUS_INFO;
65
66   update_icon (icon);
67
68   return TRUE;
69 }
70
71 static void
72 blink_toggle_toggled (GtkToggleButton *toggle,
73                       GtkStatusIcon   *icon)
74 {
75   gtk_status_icon_set_blinking (icon, 
76                                 gtk_toggle_button_get_active (toggle));
77 }
78
79 static void
80 visible_toggle_toggled (GtkToggleButton *toggle,
81                         GtkStatusIcon   *icon)
82 {
83   gtk_status_icon_set_visible (icon, 
84                                gtk_toggle_button_get_active (toggle));
85 }
86
87 static void
88 timeout_toggle_toggled (GtkToggleButton *toggle,
89                         GtkStatusIcon   *icon)
90 {
91   if (timeout)
92     {
93       g_source_remove (timeout);
94       timeout = 0;
95     }
96   else
97     {
98       timeout = g_timeout_add (2000, timeout_handler, icon);
99     }
100 }
101
102 static void
103 icon_activated (GtkStatusIcon *icon)
104 {
105   GtkWidget *dialog;
106   GtkWidget *toggle;
107
108   dialog = g_object_get_data (G_OBJECT (icon), "test-status-icon-dialog");
109   if (dialog == NULL)
110     {
111       dialog = gtk_message_dialog_new (NULL, 0,
112                                        GTK_MESSAGE_QUESTION,
113                                        GTK_BUTTONS_CLOSE,
114                                        "You wanna test the status icon ?");
115
116       gtk_window_set_position (dialog, GTK_WIN_POS_CENTER);
117
118       g_object_set_data_full (G_OBJECT (icon), "test-status-icon-dialog",
119                               dialog, (GDestroyNotify) gtk_widget_destroy);
120
121       g_signal_connect (dialog, "response", 
122                         G_CALLBACK (gtk_widget_hide), NULL);
123       g_signal_connect (dialog, "delete_event", 
124                         G_CALLBACK (gtk_widget_hide_on_delete), NULL);
125
126       toggle = gtk_toggle_button_new_with_mnemonic ("_Show the icon");
127       gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), toggle, TRUE, TRUE, 6);
128       gtk_widget_show (toggle);
129
130       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
131                                     gtk_status_icon_get_visible (icon));
132       g_signal_connect (toggle, "toggled", 
133                         G_CALLBACK (visible_toggle_toggled), icon);
134
135       toggle = gtk_toggle_button_new_with_mnemonic ("_Blink 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_blinking (icon));
141       g_signal_connect (toggle, "toggled", 
142                         G_CALLBACK (blink_toggle_toggled), icon);
143
144       toggle = gtk_toggle_button_new_with_mnemonic ("_Change images");
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                                     timeout != 0);
150       g_signal_connect (toggle, "toggled", 
151                         G_CALLBACK (timeout_toggle_toggled), icon);
152     }
153
154   gtk_window_present (GTK_WINDOW (dialog));
155 }
156
157 static void
158 check_activated (GtkCheckMenuItem *item,
159                  GtkStatusIcon    *icon)
160 {
161   gtk_status_icon_set_blinking (icon, 
162                                 gtk_check_menu_item_get_active (item));
163 }
164
165 static void
166 do_quit (GtkMenuItem   *item,
167          GtkStatusIcon *icon)
168 {
169   gtk_status_icon_set_visible (icon, FALSE);
170   g_object_unref (icon);
171   gtk_main_quit ();
172 }
173
174 static void 
175 popup_menu (GtkStatusIcon *icon,
176             guint          button,
177             guint32        activate_time)
178 {
179   GtkWidget *menu, *menuitem;
180
181   menu = gtk_menu_new ();
182
183   menuitem = gtk_check_menu_item_new_with_label ("Blink");
184   gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), 
185                                   gtk_status_icon_get_blinking (icon));
186   g_signal_connect (menuitem, "activate", G_CALLBACK (check_activated), icon);
187
188   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
189
190   gtk_widget_show (menuitem);
191
192   menuitem = gtk_menu_item_new_with_label ("Quit");
193   g_signal_connect (menuitem, "activate", G_CALLBACK (do_quit), icon);
194
195   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
196
197   gtk_widget_show (menuitem);
198
199   gtk_menu_popup (GTK_MENU (menu), 
200                   NULL, NULL, NULL, NULL, 
201                   button, activate_time);
202 }
203
204 int
205 main (int argc, char **argv)
206 {
207   GtkStatusIcon *icon;
208
209   gtk_init (&argc, &argv);
210
211   icon = gtk_status_icon_new ();
212   update_icon (icon);
213
214   gtk_status_icon_set_blinking (GTK_STATUS_ICON (icon), TRUE);
215
216   g_signal_connect (icon, "activate",
217                     G_CALLBACK (icon_activated), NULL);
218
219   g_signal_connect (icon, "popup-menu",
220                     G_CALLBACK (popup_menu), NULL);
221  
222   timeout = g_timeout_add (2000, timeout_handler, icon);
223
224   gtk_main ();
225
226   return 0;
227 }