From 7edfe59b1c61d5a6ff6c293949394c07040e3d71 Mon Sep 17 00:00:00 2001 From: Eric Koegel Date: Sun, 22 Dec 2013 12:22:19 +0300 Subject: [PATCH] Fix transparency issues with GTK3 plugins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch uses some GTK3 CSS magic written by Simon Steinbeiß to make the GtkPlug button in the panel transparent. This patch also removes the wrapper_plug_draw code and instead updates the panel's background when the respective set function is called for gtk3 plugins. --- libxfce4panel/xfce-panel-plugin.c | 5 +- wrapper/wrapper-plug.c | 175 ++++++++++++++++++++++---------------- 2 files changed, 102 insertions(+), 78 deletions(-) diff --git a/libxfce4panel/xfce-panel-plugin.c b/libxfce4panel/xfce-panel-plugin.c index df417fe..9fb43d1 100644 --- a/libxfce4panel/xfce-panel-plugin.c +++ b/libxfce4panel/xfce-panel-plugin.c @@ -715,10 +715,7 @@ xfce_panel_plugin_init (XfcePanelPlugin *plugin) #endif /* hide the event box window to make the plugin transparent */ - // FIXME - // Temporarily disabled to workaround plugin transparency issues. - // It breaks background transparency and color support. - //gtk_event_box_set_visible_window (GTK_EVENT_BOX (plugin), FALSE); + gtk_event_box_set_visible_window (GTK_EVENT_BOX (plugin), FALSE); } diff --git a/wrapper/wrapper-plug.c b/wrapper/wrapper-plug.c index 6dc4432..8e13a2e 100644 --- a/wrapper/wrapper-plug.c +++ b/wrapper/wrapper-plug.c @@ -94,6 +94,8 @@ wrapper_plug_init (WrapperPlug *plug) GdkVisual *visual = NULL; GdkScreen *screen; GtkStyleContext *context; + GtkCssProvider *provider = gtk_css_provider_new(); + gchar *css_string; #else GdkColormap *colormap = NULL; GdkScreen *screen; @@ -135,6 +137,17 @@ wrapper_plug_init (WrapperPlug *plug) context = gtk_widget_get_style_context (GTK_WIDGET (plug)); gtk_style_context_add_class (context, "panel"); gtk_style_context_add_class (context, "xfce4-panel"); + + /* We need to set the plugin button to transparent and let everything else + * be in the theme or panel's color */ + css_string = g_strdup_printf (".xfce4-panel .button { background-color: transparent; }"); + gtk_css_provider_load_from_data (provider, css_string, -1, NULL); + gtk_style_context_add_provider (context, + GTK_STYLE_PROVIDER (provider), + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + + g_free (css_string); + g_object_unref (provider); #endif } @@ -155,80 +168,6 @@ static gboolean wrapper_plug_draw (GtkWidget *widget, cairo_t *cr) { - WrapperPlug *plug = WRAPPER_PLUG (widget); - GtkStyleContext *style; - const GdkColor *color; - GdkRGBA rgba; - gdouble alpha; - GdkPixbuf *pixbuf; - GError *error = NULL; - - if (gtk_widget_is_drawable (widget)) - { - if (G_UNLIKELY (plug->background_image != NULL)) - { - cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); - - if (G_LIKELY (plug->background_image_cache != NULL)) - { - cairo_set_source (cr, plug->background_image_cache); - cairo_paint (cr); - } - else - { - /* load the image in a pixbuf */ - pixbuf = gdk_pixbuf_new_from_file (plug->background_image, &error); - - if (G_LIKELY (pixbuf != NULL)) - { - gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0); - g_object_unref (G_OBJECT (pixbuf)); - - plug->background_image_cache = cairo_get_source (cr); - cairo_pattern_reference (plug->background_image_cache); - cairo_pattern_set_extend (plug->background_image_cache, CAIRO_EXTEND_REPEAT); - cairo_paint (cr); - } - else - { - /* print error message */ - g_warning ("Background image disabled, \"%s\" could not be loaded: %s", - plug->background_image, error != NULL ? error->message : "No error"); - g_error_free (error); - - /* disable background image */ - wrapper_plug_background_reset (plug); - } - } - } - else - { - alpha = gtk_widget_is_composited (GTK_WIDGET (plug)) ? plug->background_alpha : 1.00; - - if (alpha < 1.00 || plug->background_color != NULL) - { - cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); - - /* get the background gdk color */ - if (plug->background_color != NULL) - { - color = plug->background_color; - cairo_set_source_rgba (cr, PANEL_GDKCOLOR_TO_DOUBLE (color), alpha); - } - else - { - style = gtk_widget_get_style_context (widget); - gtk_style_context_get_background_color (style, GTK_STATE_FLAG_NORMAL, &rgba); - rgba.alpha = alpha; - gdk_cairo_set_source_rgba (cr, &rgba); - } - - /* draw the background color */ - cairo_fill (cr); - } - } - } - return GTK_WIDGET_CLASS (wrapper_plug_parent_class)->draw (widget, cr); } @@ -356,6 +295,88 @@ wrapper_plug_new (GdkNativeWindow socket_id) +static void +wrapper_plug_update_color (WrapperPlug *plug) +{ +#if GTK_CHECK_VERSION (3, 0, 0) + GdkRGBA rgba; + GtkStyleContext *context; + + panel_return_if_fail (WRAPPER_IS_PLUG (plug)); + + if (plug->background_color == NULL) + { + /* Get the theme's color and update the alpha */ + context = gtk_widget_get_style_context (GTK_WIDGET (plug)); + gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &rgba); + rgba.alpha = plug->background_alpha; + } + else + { + /* convert GdkColor to GdkRGBA */ + rgba.red = (double)plug->background_color->red / 255.0f; + rgba.green = (double)plug->background_color->green / 255.0f; + rgba.blue = (double)plug->background_color->blue / 255.0f; + rgba.alpha = plug->background_alpha; + } + + /* set the panel color/transparency */ + gdk_window_set_background_rgba (gtk_widget_get_window (GTK_WIDGET (plug)), &rgba); + +#endif +} + + + +static void +wrapper_plug_update_image (WrapperPlug *plug) +{ +#if GTK_CHECK_VERSION (3, 0, 0) + cairo_t *cr; + GdkPixbuf *pixbuf; + GError *error = NULL; + + panel_return_if_fail (WRAPPER_IS_PLUG (plug)); + panel_return_if_fail (plug->background_image_cache == NULL); + + if (G_UNLIKELY (plug->background_image != NULL)) + { + cr = gdk_cairo_create (gtk_widget_get_window (GTK_WIDGET (plug))); + cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); + + /* load the image in a pixbuf */ + pixbuf = gdk_pixbuf_new_from_file (plug->background_image, &error); + + if (G_LIKELY (pixbuf != NULL)) + { + gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0); + g_object_unref (G_OBJECT (pixbuf)); + + plug->background_image_cache = cairo_get_source (cr); + cairo_pattern_reference (plug->background_image_cache); + cairo_pattern_set_extend (plug->background_image_cache, CAIRO_EXTEND_REPEAT); + } + else + { + /* print error message */ + g_warning ("Background image disabled, \"%s\" could not be loaded: %s", + plug->background_image, error != NULL ? error->message : "No error"); + g_error_free (error); + + /* disable background image */ + wrapper_plug_background_reset (plug); + } + + cairo_destroy (cr); + } + + if (plug->background_image_cache) + gdk_window_set_background_pattern (gtk_widget_get_window (GTK_WIDGET (plug)), plug->background_image_cache); +#endif +} + + + void wrapper_plug_set_background_alpha (WrapperPlug *plug, gdouble alpha) @@ -366,6 +387,8 @@ wrapper_plug_set_background_alpha (WrapperPlug *plug, /* set the alpha */ plug->background_alpha = CLAMP (alpha, 0.00, 1.00); + wrapper_plug_update_color (plug); + /* redraw */ if (gtk_widget_is_composited (GTK_WIDGET (plug))) gtk_widget_queue_draw (GTK_WIDGET (plug)); @@ -388,6 +411,8 @@ wrapper_plug_set_background_color (WrapperPlug *plug, && gdk_color_parse (color_string, &color)) plug->background_color = gdk_color_copy (&color); + wrapper_plug_update_color (plug); + gtk_widget_queue_draw (GTK_WIDGET (plug)); } @@ -403,5 +428,7 @@ wrapper_plug_set_background_image (WrapperPlug *plug, plug->background_image = g_strdup (image); + wrapper_plug_update_image (plug); + gtk_widget_queue_draw (GTK_WIDGET (plug)); } -- 1.8.3.2