X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fui_appindicator.c;h=317e238ed1ea09b0113c72c98401fc24959f12e3;hb=8ddc118263514e38cc4b7bed661464d2f318e0b4;hp=6a095077f47c0400ba901fe7d419852c477fd28a;hpb=cde76fd1723bbdad164adac96d105111b831bf50;p=psensor.git diff --git a/src/ui_appindicator.c b/src/ui_appindicator.c index 6a09507..317e238 100644 --- a/src/ui_appindicator.c +++ b/src/ui_appindicator.c @@ -27,27 +27,27 @@ #include "psensor.h" #include "ui.h" #include "ui_appindicator.h" +#include "ui_sensorpref.h" #include "ui_pref.h" -static void cb_appindicator_show(gpointer data, - guint cb_action, - GtkWidget *item) +static GtkMenuItem **sensor_menu_items; +static GtkWidget *main_window; +static int appindicator_supported = 1; + +static void cb_menu_show(GtkMenuItem *mi, gpointer data) { struct ui_psensor *ui = (struct ui_psensor *)data; gtk_window_present(GTK_WINDOW(ui->main_window)); } -static void cb_appindicator_quit(gpointer data, - guint cb_action, - GtkWidget *item) +static void cb_menu_quit(GtkMenuItem *mi, gpointer data) { - ui_psensor_exit(data); + ui_psensor_quit(data); } -static void cb_appindicator_preferences(gpointer data, - guint cb_action, - GtkWidget *item) +static void +cb_menu_preferences(GtkMenuItem *mi, gpointer data) { #ifdef HAVE_APPINDICATOR_029 gdk_threads_enter(); @@ -60,29 +60,143 @@ static void cb_appindicator_preferences(gpointer data, #endif } -static GtkItemFactoryEntry menu_items[] = { - {"/Show", - NULL, cb_appindicator_show, 0, ""}, - {"/Preferences", - NULL, cb_appindicator_preferences, 0, ""}, - {"/sep1", - NULL, NULL, 0, ""}, - {"/Quit", - "", cb_appindicator_quit, 0, "", GTK_STOCK_QUIT}, +static void +cb_sensor_preferences(GtkMenuItem *mi, gpointer data) +{ + struct ui_psensor *ui = data; + +#ifdef HAVE_APPINDICATOR_029 + gdk_threads_enter(); +#endif + + if (ui->sensors && *ui->sensors) + ui_sensorpref_dialog_run(*ui->sensors, ui); + +#ifdef HAVE_APPINDICATOR_029 + gdk_threads_leave(); +#endif +} + +static void cb_about(GtkMenuItem *mi, gpointer data) +{ + ui_show_about_dialog(); +} + +static const char *menu_desc = +"" +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +""; + +static GtkActionEntry entries[] = { + { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */ + + { "ShowAction", NULL, /* name, stock id */ + "_Show", NULL, /* label, accelerator */ + "Show", /* tooltip */ + G_CALLBACK(cb_menu_show) }, + + { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */ + "_Preferences", NULL, /* label, accelerator */ + "Preferences", /* tooltip */ + G_CALLBACK(cb_menu_preferences) }, + + { "SensorPreferencesAction", GTK_STOCK_PREFERENCES, + "S_ensor Preferences", + NULL, + "SensorPreferences", + G_CALLBACK(cb_sensor_preferences) }, + + { "AboutAction", NULL, + "_About", + NULL, + "About", + G_CALLBACK(cb_about) }, + + { "QuitAction", + GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) } }; +static guint n_entries = G_N_ELEMENTS(entries); -static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]); -GtkWidget *ui_appindicator_get_menu(struct ui_psensor *ui) +static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s) { - GtkItemFactory *item_factory; + gchar *str; + + str = g_strdup_printf("%s: %2.f %s", + s->name, + psensor_get_current_value(s), + psensor_type_to_unit_str(s->type)); + + gtk_menu_item_set_label(item, str); - item_factory = gtk_item_factory_new(GTK_TYPE_MENU, "
", NULL); + g_free(str); +} + +static void update_sensor_menu_items(struct psensor **sensors) +{ + int n = psensor_list_size(sensors); + int i; - gtk_item_factory_create_items(item_factory, - nmenu_items, menu_items, ui); - return gtk_item_factory_get_widget(item_factory, "
"); + for (i = 0; i < n; i++) + update_sensor_menu_item(sensor_menu_items[i], + sensors[i]); } +static GtkWidget *get_menu(struct ui_psensor *ui) +{ + GtkActionGroup *action_group; + GtkUIManager *menu_manager; + GError *error; + GtkMenu *menu; + int i; + int n = psensor_list_size(ui->sensors); + struct psensor **sensors = ui->sensors; + + + action_group = gtk_action_group_new("PsensorActions"); + gtk_action_group_set_translation_domain(action_group, PACKAGE); + menu_manager = gtk_ui_manager_new(); + + gtk_action_group_add_actions(action_group, entries, n_entries, ui); + gtk_ui_manager_insert_action_group(menu_manager, action_group, 0); + + error = NULL; + gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error); + + if (error) + g_error(_("building menus failed: %s"), error->message); + + menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu")); + + sensor_menu_items = malloc(sizeof(GtkWidget *)*n); + for (i = 0; i < n; i++) { + struct psensor *s = sensors[i]; + + sensor_menu_items[i] + = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name)); + + gtk_menu_shell_insert(GTK_MENU_SHELL(menu), + GTK_WIDGET(sensor_menu_items[i]), + i+2); + + update_sensor_menu_item(sensor_menu_items[i], + s); + } + + + return GTK_WIDGET(menu); +} + + void ui_appindicator_update(struct ui_psensor *ui) { struct psensor **sensor_cur = ui->sensors; @@ -112,20 +226,55 @@ void ui_appindicator_update(struct ui_psensor *ui) if (attention && status == APP_INDICATOR_STATUS_ACTIVE) app_indicator_set_status (ui->indicator, APP_INDICATOR_STATUS_ATTENTION); + + update_sensor_menu_items(ui->sensors); } +static GtkStatusIcon *unity_fallback(AppIndicator *indicator) +{ + log_puts(LOG_DEBUG, "ui_appindicator#unity_fallback"); + + gtk_widget_show_all(main_window); + + appindicator_supported = 0; + + return NULL; +} + +static void unity_unfallback(AppIndicator *indicator, + GtkStatusIcon *status_icon) +{ + log_puts(LOG_DEBUG, "ui_appindicator#unity_unfallback"); + + appindicator_supported = 1; +} + + void ui_appindicator_init(struct ui_psensor *ui) { GtkWidget *indicatormenu; + main_window = ui->main_window; + ui->indicator = app_indicator_new("psensor", - "psensor", + "psensor_normal", APP_INDICATOR_CATEGORY_APPLICATION_STATUS); + APP_INDICATOR_GET_CLASS(ui->indicator)->fallback = unity_fallback; + APP_INDICATOR_GET_CLASS(ui->indicator)->unfallback = unity_unfallback; + app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE); app_indicator_set_attention_icon(ui->indicator, "psensor_hot"); - indicatormenu = ui_appindicator_get_menu(ui); + indicatormenu = get_menu(ui); + + gtk_widget_show_all(indicatormenu); + app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu)); } + +int is_appindicator_supported() +{ + return appindicator_supported; +}