Enabling multithreaded compilation.
[debian/lxpanel.git] / src / plugins / netstat / statusicon.c
1 /**
2 * Copyright (c) 2008 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <string.h>
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <glib/gi18n.h>
23 #include "netstat.h"
24 #include "statusicon.h"
25
26 struct statusicon *create_statusicon(GtkWidget *box, const char *filename,
27 const char *tooltips, const char* icon_name)
28 {
29 struct statusicon *newicon;
30 newicon = malloc(sizeof(struct statusicon));
31
32 /* main */
33 newicon->main = gtk_event_box_new();
34
35 GTK_WIDGET_SET_FLAGS(newicon->main, GTK_NO_WINDOW);
36 gtk_widget_add_events(newicon->main, GDK_BUTTON_PRESS_MASK);
37 gtk_widget_set_size_request(newicon->main, 24, 24);
38 gtk_box_pack_start(GTK_BOX(box), newicon->main, TRUE, TRUE, 0);
39
40 /* icon */
41
42 /*icon theme*/
43 GtkIconTheme* icon_theme = gtk_icon_theme_get_default();
44
45 /*if we've got a theme*/
46 if(gtk_icon_theme_has_icon(icon_theme, icon_name)) {
47 GdkPixbuf* temp_pixbuf = gtk_icon_theme_load_icon(icon_theme, icon_name, 24, 0, NULL);
48 newicon->icon = gtk_image_new_from_pixbuf(temp_pixbuf);
49 }
50
51 /* oh well...*/
52 else {
53 newicon->icon = gtk_image_new_from_file(filename);
54 }
55 gtk_container_add(GTK_CONTAINER(newicon->main), newicon->icon);
56 gtk_widget_show_all(newicon->main);
57
58 /* tooltip */
59 newicon->tooltips = gtk_tooltips_new();
60 #if GLIB_CHECK_VERSION( 2, 10, 0 )
61 g_object_ref_sink(newicon->tooltips);
62 #else
63 g_object_ref(newicon->tooltips);
64 gtk_object_sink(newicon->tooltips);
65 #endif
66 gtk_tooltips_set_tip(newicon->tooltips, newicon->main, tooltips, NULL);
67
68 return newicon;
69 }
70
71 void statusicon_destroy(struct statusicon *icon)
72 {
73 if (icon==NULL)
74 return;
75
76 g_object_unref(icon->tooltips);
77 gtk_widget_destroy(GTK_WIDGET(icon->icon));
78 gtk_widget_destroy(GTK_WIDGET(icon->main));
79 g_free(icon);
80 }
81
82
83 void set_statusicon_image_from_file(struct statusicon *widget, const char *filename)
84 {
85 gtk_image_set_from_file(GTK_IMAGE(widget->icon), filename);
86 }
87
88 void set_statusicon_tooltips(struct statusicon *widget, const char *tooltips)
89 {
90 gtk_tooltips_set_tip(widget->tooltips, widget->main, tooltips, NULL);
91 }
92
93 void set_statusicon_visible(struct statusicon *widget, gboolean b)
94 {
95 if (b)
96 gtk_widget_show(widget->main);
97 else
98 gtk_widget_hide(widget->main);
99 }