]> Pileus Git - ~andy/gtk/blob - tests/teststatusicon.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
17  *
18  * Authors:
19  *      Mark McLoughlin <mark@skynet.ie>
20  */
21
22 #include <gtk/gtk.h>
23 #include <stdlib.h>
24
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 = "dialog-information";
77       tooltip = "Some Information ...";
78     }
79   else
80     {
81       icon_name = "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_text (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 visible_toggle_toggled (GtkToggleButton *toggle)
109 {
110   GSList *l;
111
112   for (l = icons; l; l = l->next)
113     gtk_status_icon_set_visible (GTK_STATUS_ICON (l->data),
114                                  gtk_toggle_button_get_active (toggle));
115 }
116
117 static void
118 timeout_toggle_toggled (GtkToggleButton *toggle)
119 {
120   if (timeout)
121     {
122       g_source_remove (timeout);
123       timeout = 0;
124     }
125   else
126     {
127       timeout = gdk_threads_add_timeout (2000, timeout_handler, NULL);
128     }
129 }
130
131 static void
132 icon_activated (GtkStatusIcon *icon)
133 {
134   GtkWidget *content_area;
135   GtkWidget *dialog;
136   GtkWidget *toggle;
137
138   dialog = g_object_get_data (G_OBJECT (icon), "test-status-icon-dialog");
139   if (dialog == NULL)
140     {
141       dialog = gtk_message_dialog_new (NULL, 0,
142                                        GTK_MESSAGE_QUESTION,
143                                        GTK_BUTTONS_CLOSE,
144                                        "You wanna test the status icon ?");
145
146       gtk_window_set_screen (GTK_WINDOW (dialog), gtk_status_icon_get_screen (icon));
147       gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
148
149       g_object_set_data_full (G_OBJECT (icon), "test-status-icon-dialog",
150                               dialog, (GDestroyNotify) gtk_widget_destroy);
151
152       g_signal_connect (dialog, "response", 
153                         G_CALLBACK (gtk_widget_hide), NULL);
154       g_signal_connect (dialog, "delete_event", 
155                         G_CALLBACK (gtk_widget_hide_on_delete), NULL);
156
157       content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
158
159       toggle = gtk_toggle_button_new_with_mnemonic ("_Show the icon");
160       gtk_box_pack_end (GTK_BOX (content_area), toggle, TRUE, TRUE, 6);
161       gtk_widget_show (toggle);
162
163       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
164                                     gtk_status_icon_get_visible (icon));
165       g_signal_connect (toggle, "toggled", 
166                         G_CALLBACK (visible_toggle_toggled), NULL);
167
168       toggle = gtk_toggle_button_new_with_mnemonic ("_Change images");
169       gtk_box_pack_end (GTK_BOX (content_area), toggle, TRUE, TRUE, 6);
170       gtk_widget_show (toggle);
171
172       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
173                                     timeout != 0);
174       g_signal_connect (toggle, "toggled", 
175                         G_CALLBACK (timeout_toggle_toggled), NULL);
176     }
177
178   gtk_window_present (GTK_WINDOW (dialog));
179 }
180
181 static void
182 do_properties (GtkMenuItem   *item,
183                GtkStatusIcon *icon)
184 {
185         static GtkWidget *editor = NULL;
186
187         if (editor == NULL) {
188                 editor = create_prop_editor (G_OBJECT (icon), GTK_TYPE_STATUS_ICON);
189                 g_signal_connect (editor, "destroy", G_CALLBACK (gtk_widget_destroyed), &editor);
190         }
191
192         gtk_window_present (GTK_WINDOW (editor));
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 do_exit (GtkMenuItem *item)
216 {
217   exit (0);
218 }
219
220 static void 
221 popup_menu (GtkStatusIcon *icon,
222             guint          button,
223             guint32        activate_time)
224 {
225   GtkWidget *menu, *menuitem;
226
227   menu = gtk_menu_new ();
228
229   gtk_menu_set_screen (GTK_MENU (menu),
230                        gtk_status_icon_get_screen (icon));
231
232   menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_PROPERTIES, NULL);
233   g_signal_connect (menuitem, "activate", G_CALLBACK (do_properties), icon);
234
235   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
236
237   gtk_widget_show (menuitem);
238
239   menuitem = gtk_menu_item_new_with_label ("Quit");
240   g_signal_connect (menuitem, "activate", G_CALLBACK (do_quit), NULL);
241
242   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
243
244   gtk_widget_show (menuitem);
245
246   menuitem = gtk_menu_item_new_with_label ("Exit abruptly");
247   g_signal_connect (menuitem, "activate", G_CALLBACK (do_exit), NULL);
248
249   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
250
251   gtk_widget_show (menuitem);
252
253   gtk_menu_popup (GTK_MENU (menu), 
254                   NULL, NULL,
255                   gtk_status_icon_position_menu, icon,
256                   button, activate_time);
257 }
258
259 int
260 main (int argc, char **argv)
261 {
262   GdkDisplay *display;
263   guint n_screens, i;
264
265   gtk_init (&argc, &argv);
266
267   display = gdk_display_get_default ();
268
269   n_screens = gdk_display_get_n_screens (display);
270
271   for (i = 0; i < n_screens; i++)
272     {
273       GtkStatusIcon *icon;
274
275       icon = gtk_status_icon_new ();
276       gtk_status_icon_set_screen (icon, gdk_display_get_screen (display, i));
277       update_icons ();
278
279       g_signal_connect (icon, "size-changed", G_CALLBACK (size_changed_cb), NULL);
280       g_signal_connect (icon, "notify::embedded", G_CALLBACK (embedded_changed_cb), NULL);
281       g_signal_connect (icon, "notify::orientation", G_CALLBACK (orientation_changed_cb), NULL);
282       g_signal_connect (icon, "notify::screen", G_CALLBACK (screen_changed_cb), NULL);
283       g_print ("icon size %d\n", gtk_status_icon_get_size (icon));
284
285       g_signal_connect (icon, "activate",
286                         G_CALLBACK (icon_activated), NULL);
287
288       g_signal_connect (icon, "popup-menu",
289                         G_CALLBACK (popup_menu), NULL);
290
291       icons = g_slist_append (icons, icon);
292  
293       update_icons ();
294
295       timeout = gdk_threads_add_timeout (2000, timeout_handler, icon);
296     }
297
298   gtk_main ();
299
300   return 0;
301 }