add a bit more cairo (background)
authorRafał Mużyło <galtgendo@gmail.com>
Sun, 27 May 2012 11:21:25 +0000 (13:21 +0200)
committerHenry Gebhardt <hsggebhardt@googlemail.com>
Wed, 1 Aug 2012 11:54:13 +0000 (13:54 +0200)
This simplifies panel background drawing a bit and fixes a subtle bug:
try setting alpha to 255 - it gets saved as 254 without this fix.

src/bg.c
src/bg.h
src/configurator.c
src/panel.c

index 1b7ed31..0d1d41f 100644 (file)
--- a/src/bg.c
+++ b/src/bg.c
@@ -210,33 +210,16 @@ fb_bg_get_xroot_pix_for_win(FbBg *bg, GtkWidget *widget)
 }
 
 void
-fb_bg_composite(GdkDrawable *base, GdkGC *gc, guint32 tintcolor, gint alpha)
+fb_bg_composite(GdkDrawable *base, GdkColor *tintcolor, gint alpha)
 {
-    GdkPixbuf *ret, *ret2;
-    int w, h;
-    static GdkColormap *cmap = NULL;
+    cairo_t *cr;
 
     ENTER;
-    gdk_drawable_get_size (base, &w, &h);
-    if (!cmap) {
-        cmap = gdk_colormap_get_system ();
-    }
-    DBG("here\n");
-    ret = gdk_pixbuf_get_from_drawable (NULL, base, cmap, 0, 0, 0, 0, w, h);
-    if (!ret)
-        RET();
-    DBG("here w=%d h=%d\n", w, h);
-    ret2 = gdk_pixbuf_composite_color_simple(ret, w, h,
-          GDK_INTERP_HYPER, 255-alpha, MIN(w, h), tintcolor, tintcolor);
-    DBG("here\n");
-    if (!ret2) {
-        g_object_unref(ret);
-        RET();
-    }
-    //gdk_pixbuf_render_to_drawable (ret2, base, gc, 0, 0, 0, 0, w, h, GDK_RGB_DITHER_NONE, 0, 0);
-    gdk_draw_pixbuf (base, gc, ret2, 0, 0, 0, 0, w, h, GDK_RGB_DITHER_NONE, 0, 0);
-    g_object_unref(ret);
-    g_object_unref(ret2);
+    cr = gdk_cairo_create(base);
+    gdk_cairo_set_source_color(cr, tintcolor);
+    cairo_paint_with_alpha(cr, (double) alpha/255);
+    cairo_destroy(cr);
+    fb_bg_changed(fb_bg_get_for_display());
     RET();
 }
 
@@ -282,6 +265,7 @@ fb_bg_get_pix_from_file(GtkWidget *widget, const char *filename)
 {
     ENTER;
     GdkPixbuf *pixbuf;
+    cairo_t *cr;
     GdkPixmap *pixmap;
 
     pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
@@ -289,12 +273,10 @@ fb_bg_get_pix_from_file(GtkWidget *widget, const char *filename)
         RET(widget->style->bg_pixmap[0]);
     }
     pixmap = gdk_pixmap_new(widget->window, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), -1);
-    gdk_pixbuf_render_to_drawable(pixbuf,pixmap,
-            widget->style->fg_gc[GTK_STATE_NORMAL],
-            0,0,0,0,
-            gdk_pixbuf_get_width(pixbuf),
-            gdk_pixbuf_get_height(pixbuf),
-            GDK_RGB_DITHER_NORMAL,0,0);
+    cr = gdk_cairo_create(pixmap);
+    gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
+    cairo_paint(cr);
+    cairo_destroy(cr);
 
     g_object_unref( pixbuf );
     RET(pixmap);
index a13358b..f1fd3fc 100644 (file)
--- a/src/bg.h
+++ b/src/bg.h
@@ -56,7 +56,7 @@ typedef struct _FbBg      FbBg;
 
 GType fb_bg_get_type       (void);
 #define fb_bg_new() (FbBg *)g_object_new(FB_TYPE_BG, NULL)
-void fb_bg_composite(GdkDrawable *base, GdkGC *gc, guint32 tintcolor, gint alpha);
+void fb_bg_composite(GdkDrawable *base, GdkColor *tintcolor, gint alpha);
 GdkPixmap *fb_bg_get_xroot_pix_for_win(FbBg *bg, GtkWidget *widget);
 void fb_bg_notify_changed_bg(FbBg *bg);
 FbBg *fb_bg_get_for_display(void);
index aef880a..28da644 100644 (file)
@@ -66,6 +66,10 @@ extern GSList* all_panels;
 extern gchar *cprofile;
 extern int config;
 
+/* GtkColotButton expects a number between 0 and 65535, but p->alpha has range
+ * 0 to 255, and (2^(2n) - 1) / (2^n - 1) = 2^n + 1 = 257, with n = 8. */
+static guint16 const alpha_scale_factor = 257;
+
 void panel_global_config_save( Panel* p, FILE *fp);
 void panel_plugin_config_save( Panel* p, FILE *fp);
 
@@ -320,7 +324,7 @@ on_tint_color_set( GtkColorButton* clr,  Panel* p )
 {
     gtk_color_button_get_color( clr, &p->gtintcolor );
     p->tintcolor = gcolor2rgb24(&p->gtintcolor);
-    p->alpha = gtk_color_button_get_alpha( clr ) / 256;
+    p->alpha = gtk_color_button_get_alpha( clr ) / alpha_scale_factor;
     panel_update_background( p );
 }
 
@@ -968,7 +972,7 @@ void panel_configure( Panel* p, int sel_page )
     /* transparancy */
     tint_clr = w = (GtkWidget*)gtk_builder_get_object( builder, "tint_clr" );
     gtk_color_button_set_color((GtkColorButton*)w, &p->gtintcolor);
-    gtk_color_button_set_alpha((GtkColorButton*)w, 256 * p->alpha);
+    gtk_color_button_set_alpha((GtkColorButton*)w, alpha_scale_factor * p->alpha);
     if ( ! p->transparent )
         gtk_widget_set_sensitive( w, FALSE );
     g_signal_connect( w, "color-set", G_CALLBACK( on_tint_color_set ), p );
index 14f203e..c71ad35 100644 (file)
@@ -421,7 +421,7 @@ void panel_determine_background_pixmap(Panel * p, GtkWidget * widget, GdkWindow
         }
         pixmap = fb_bg_get_xroot_pix_for_win(p->bg, widget);
         if ((pixmap != NULL) && (pixmap != GDK_NO_BG) && (p->alpha != 0))
-            fb_bg_composite(pixmap, widget->style->black_gc, p->tintcolor, p->alpha);
+            fb_bg_composite(pixmap, &p->gtintcolor, p->alpha);
     }
 
     if (pixmap != NULL)