From 551ac2c8b83a58972a47f97cccade84c5b04adb6 Mon Sep 17 00:00:00 2001 From: Landry Breuil Date: Fri, 6 Jan 2017 23:04:11 +0100 Subject: [PATCH] Rework label css handling Move it to XnlpMonitorLabel object. --- panel-plugin/monitor-label.c | 34 +++++++++++++++++++++++++++++ panel-plugin/monitor-label.h | 1 + panel-plugin/netload.c | 42 ++++-------------------------------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/panel-plugin/monitor-label.c b/panel-plugin/monitor-label.c index bcdda15..032bc7f 100644 --- a/panel-plugin/monitor-label.c +++ b/panel-plugin/monitor-label.c @@ -14,6 +14,7 @@ #endif #include +#include #include "monitor-label.h" @@ -28,6 +29,7 @@ struct _XnlpMonitorLabel { GtkLabel parent; /**/ + GtkCssProvider *css_provider; gint count_width; gint count_height; gint width; @@ -58,6 +60,11 @@ xnlp_monitor_label_init (XnlpMonitorLabel *label) label->count_height = 0; label->width = 0; label->height = 0; + label->css_provider = gtk_css_provider_new (); + gtk_style_context_add_provider ( + GTK_STYLE_CONTEXT (gtk_widget_get_style_context (GTK_WIDGET (label))), + GTK_STYLE_PROVIDER (label->css_provider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); g_signal_connect (label, "notify::label", G_CALLBACK (cb_label_changed), NULL); } @@ -125,6 +132,33 @@ xnlp_monitor_label_new (const gchar *str) return GTK_WIDGET (label); } +void +xnlp_monitor_label_set_color (XnlpMonitorLabel *label, GdkRGBA* color) +{ + gchar * css; + if (color != NULL) + { +#if GTK_CHECK_VERSION (3, 20, 0) + css = g_strdup_printf("label { color: %s; }", +#else + css = g_strdup_printf(".label { color: %s; }", +#endif + gdk_rgba_to_string(color)); + } + else + { +#if GTK_CHECK_VERSION (3, 20, 0) + css = g_strdup_printf("label { color: inherit; }"); +#else + css = g_strdup_printf(".label { color: inherit; }"); +#endif + } + DBG("setting label css: %s", gtk_css_provider_to_string (label->css_provider)); + gtk_css_provider_load_from_data (label->css_provider, css, strlen(css), NULL); + g_free(css); + +} + void xnlp_monitor_label_reinit_size_request (XnlpMonitorLabel *label) { diff --git a/panel-plugin/monitor-label.h b/panel-plugin/monitor-label.h index 4f8e267..bf495fd 100644 --- a/panel-plugin/monitor-label.h +++ b/panel-plugin/monitor-label.h @@ -31,6 +31,7 @@ typedef struct _XnlpMonitorLabel XnlpMonitorLabel; GType xnlp_monitor_label_get_type (void); GtkWidget * xnlp_monitor_label_new (const gchar *str); void xnlp_monitor_label_reinit_size_request (XnlpMonitorLabel *label); +void xnlp_monitor_label_set_color (XnlpMonitorLabel *label, GdkRGBA* color); #endif /* !MONITOR_LABEL_H */ diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c index fdc17ef..9424b98 100644 --- a/panel-plugin/netload.c +++ b/panel-plugin/netload.c @@ -536,40 +536,6 @@ static t_global_monitor * monitor_new(XfcePanelPlugin *plugin) return global; } -/* ---------------------------------------------------------------------------------------------- */ -static void set_label_csscolor(GtkWidget* label, GdkRGBA* color) -{ - GtkCssProvider *css_provider; - gchar * css; - if (color != NULL) - { -#if GTK_CHECK_VERSION (3, 20, 0) - css = g_strdup_printf("label { color: %s; }", -#else - css = g_strdup_printf(".label { color: %s; }", -#endif - gdk_rgba_to_string(color)); - } - else - { -#if GTK_CHECK_VERSION (3, 20, 0) - css = g_strdup_printf("label { color: inherit; }"); -#else - css = g_strdup_printf(".label { color: inherit; }"); -#endif - } - css_provider = gtk_css_provider_new (); - gtk_css_provider_load_from_data (css_provider, css, strlen(css), NULL); - g_free(css); - - DBG("setting label css: %s", gtk_css_provider_to_string (css_provider)); - gtk_style_context_add_provider ( - GTK_STYLE_CONTEXT (gtk_widget_get_style_context (GTK_WIDGET (label))), - GTK_STYLE_PROVIDER (css_provider), - GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - -} - /* ---------------------------------------------------------------------------------------------- */ static void set_progressbar_csscolor(GtkWidget* pbar, GdkRGBA* color) { @@ -617,9 +583,9 @@ static void setup_monitor(t_global_monitor *global, gboolean supress_warnings) if (global->monitor->options.colorize_values) { #if GTK_CHECK_VERSION (3, 16, 0) - set_label_csscolor(global->monitor->rcv_label, + xnlp_monitor_label_set_color(XNLP_MONITOR_LABEL(global->monitor->rcv_label), &global->monitor->options.color[IN]); - set_label_csscolor(global->monitor->sent_label, + xnlp_monitor_label_set_color(XNLP_MONITOR_LABEL(global->monitor->sent_label), &global->monitor->options.color[OUT]); #else gtk_widget_override_color(global->monitor->rcv_label, GTK_STATE_NORMAL, @@ -632,8 +598,8 @@ static void setup_monitor(t_global_monitor *global, gboolean supress_warnings) { DBG("resetting label colors"); #if GTK_CHECK_VERSION (3, 16, 0) - set_label_csscolor(global->monitor->rcv_label, NULL); - set_label_csscolor(global->monitor->sent_label, NULL); + xnlp_monitor_label_set_color(XNLP_MONITOR_LABEL(global->monitor->rcv_label), NULL); + xnlp_monitor_label_set_color(XNLP_MONITOR_LABEL(global->monitor->sent_label), NULL); #else gtk_widget_override_color(global->monitor->rcv_label, GTK_STATE_NORMAL, NULL); gtk_widget_override_color(global->monitor->sent_label, GTK_STATE_NORMAL, NULL);