3
#include "update-notifier.h"
4
#include "trayappletui.h"
6
void tray_applet_ui_ensure(TrayApplet *ta)
8
g_debug("tray_applet_ui_ensure: %s", ta->name);
9
#ifdef HAVE_APP_INDICATOR
12
ta->indicator = app_indicator_new (ta->name, ta->name,
13
APP_INDICATOR_CATEGORY_SYSTEM_SERVICES);
17
ta->tray_icon = gtk_status_icon_new_from_icon_name (ta->name);
18
gtk_status_icon_set_visible (ta->tray_icon, FALSE);
22
void tray_applet_ui_set_icon(TrayApplet *ta, const char *icon_name)
24
#ifdef HAVE_APP_INDICATOR
25
return app_indicator_set_icon(ta->indicator, icon_name);
27
return gtk_status_icon_set_from_icon_name(ta->tray_icon, icon_name);
32
gboolean tray_applet_ui_get_visible(TrayApplet *ta)
34
#ifdef HAVE_APP_INDICATOR
37
return app_indicator_get_status(ta->indicator) != APP_INDICATOR_STATUS_PASSIVE;
41
return gtk_status_icon_get_visible(ta->tray_icon);
45
void tray_applet_ui_set_visible(TrayApplet *ta, gboolean visible)
47
#ifdef HAVE_APP_INDICATOR
49
app_indicator_set_status(ta->indicator, APP_INDICATOR_STATUS_ACTIVE);
51
app_indicator_set_status(ta->indicator, APP_INDICATOR_STATUS_PASSIVE);
53
gtk_status_icon_set_visible(ta->tray_icon, visible);
57
// tooltips are only support for GtkStatusIcon, so with the indicator
58
// we need to simulate this with a menu
59
void tray_applet_ui_set_tooltip_text(TrayApplet *ta, const char *text)
61
#ifdef HAVE_APP_INDICATOR
62
// there are no tooltips so we show them in the menu as inactive entries
64
ta->menu = gtk_menu_new();
66
// clear out any previous fake tooltip
67
GList *l = gtk_container_get_children(GTK_CONTAINER(ta->menu));
68
if (l && l->data && g_object_get_data(G_OBJECT(l->data), "fake-tooltip")) {
70
gtk_container_remove(GTK_CONTAINER(ta->menu), l->data);
73
gtk_container_remove(GTK_CONTAINER(ta->menu), g_list_next(l)->data);
77
// when we simluate the tooltip we may get really long lines, those
78
// need to be split up. in order to do that in a unicode friendly
79
// way we ask pango to do the line wrap for us
80
GtkWidget *label = gtk_label_new(text);
81
g_object_ref_sink(label);
82
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
83
PangoLayout *layout = gtk_label_get_layout(GTK_LABEL(label));
84
// use 300px max for a line by default
85
static const gint MAX_LINE_SIZE_IN_PIXEL = 300;
86
pango_layout_set_width(layout,
87
pango_units_from_double(MAX_LINE_SIZE_IN_PIXEL));
89
// now go over the lines that pango identified and manually add \n
90
GSList *lines = pango_layout_get_lines_readonly(layout);
91
GString *new_text = g_string_new("");
92
while (lines != NULL) {
93
new_text = g_string_append_len(new_text,
94
text+((PangoLayoutLine*)lines->data)->start_index,
95
((PangoLayoutLine*)lines->data)->length);
96
lines = g_slist_next(lines);
97
// avoid final newline
99
new_text = g_string_append(new_text, "\n");
103
GtkWidget *menu_item = gtk_menu_item_new_with_label(new_text->str);
104
gtk_menu_shell_insert(GTK_MENU_SHELL(ta->menu), menu_item, 0);
105
g_object_set_data(G_OBJECT(menu_item), "fake-tooltip", "1");
106
gtk_widget_set_sensitive(menu_item, FALSE);
107
gtk_widget_show(menu_item);
108
menu_item = gtk_separator_menu_item_new();
109
gtk_menu_shell_insert(GTK_MENU_SHELL(ta->menu), menu_item, 1);
110
gtk_widget_show(menu_item);
114
g_string_free(new_text, TRUE);
115
g_object_unref(label);
117
gtk_status_icon_set_tooltip_text (ta->tray_icon, text);
121
#ifndef HAVE_APP_INDICATOR
122
// internal helper for the tray_applet_ui_set_menu call
124
popup_menu_for_gtk_status_icon_cb (GtkStatusIcon *status_icon,
129
gtk_menu_set_screen (GTK_MENU (ta->menu),
130
gtk_status_icon_get_screen(ta->tray_icon));
131
gtk_menu_popup (GTK_MENU (ta->menu), NULL, NULL,
132
gtk_status_icon_position_menu, ta->tray_icon,
133
button, activate_time);
138
// add a menu to the applet, on the indicators this is left-click,
139
// on the GtkStatusBar its right click
140
void tray_applet_ui_set_menu(TrayApplet *ta, GtkWidget *menu)
143
#ifdef HAVE_APP_INDICATOR
144
app_indicator_set_menu(ta->indicator, GTK_MENU(ta->menu));
146
g_signal_connect (G_OBJECT(ta->tray_icon),
148
G_CALLBACK (popup_menu_for_gtk_status_icon_cb),
156
void tray_applet_ui_set_single_action(TrayApplet *ta,
161
#ifdef HAVE_APP_INDICATOR
162
GtkWidget *menu = gtk_menu_new();
163
GtkWidget *menu_item = gtk_menu_item_new_with_label(label);
164
g_signal_connect (G_OBJECT(menu_item),
166
G_CALLBACK(callback),
168
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
169
gtk_widget_show_all(menu);
170
app_indicator_set_menu(ta->indicator, GTK_MENU(menu));
172
tray_applet_ui_set_tooltip_text (ta, label);
173
g_signal_connect (G_OBJECT(ta->tray_icon),
180
void tray_applet_ui_set_data(TrayApplet *ta, const char *key, void *data)
182
#ifdef HAVE_APP_INDICATOR
183
g_object_set_data(G_OBJECT(ta->indicator), key, data);
185
g_object_set_data(G_OBJECT(ta->tray_icon), key, data);
189
void* tray_applet_ui_get_data(TrayApplet *ta, const char *key)
191
#ifdef HAVE_APP_INDICATOR
192
return g_object_get_data(G_OBJECT(ta->indicator), key);
194
return g_object_get_data(G_OBJECT(ta->tray_icon), key);
198
void tray_applet_ui_destroy(TrayApplet *ta)
200
g_debug("tray_applet_ui_destroy: %s", ta->name);
201
#ifdef HAVE_APP_INDICATOR
202
g_clear_object(&ta->indicator);
204
g_clear_object(&ta->tray_icon);