[Xfce4-commits] [xfce/xfdesktop] 07/34: backdrop: Migrate to cairo for drawing

noreply at xfce.org noreply at xfce.org
Sun Apr 16 07:06:41 CEST 2017


This is an automated email from the git hooks/post-receive script.

eric pushed a commit to branch master
in repository xfce/xfdesktop.

commit caaaddf07bc3235462572cb408ce3bb607f5b880
Author: Eric Koegel <eric.koegel at gmail.com>
Date:   Mon Feb 27 11:15:58 2017 +0300

    backdrop: Migrate to cairo for drawing
    
    And switch to using GdkRGBA for colors. We will need to migrate
    from the old color1/color2 settings at some point.
---
 settings/main.c      |  53 ++++++++-------
 src/xfce-backdrop.c  | 177 ++++++++++++++++++++++++++-------------------------
 src/xfce-backdrop.h  |   8 +--
 src/xfce-workspace.c |  20 ++++--
 4 files changed, 135 insertions(+), 123 deletions(-)

diff --git a/settings/main.c b/settings/main.c
index 46593af..113f183 100644
--- a/settings/main.c
+++ b/settings/main.c
@@ -1336,72 +1336,76 @@ xfdesktop_settings_background_tab_change_bindings(AppearancePanel *panel,
     g_free(buf);
 
     /* color 1 button */
-    buf = xfdesktop_settings_generate_per_workspace_binding_string(panel, "color1");
+    /* Fixme: we will need to migrate from color1 to rgba1 (GdkColor to GdkRGBA) */
+    buf = xfdesktop_settings_generate_per_workspace_binding_string(panel, "rgba1");
     if(remove_binding) {
         xfconf_g_property_unbind(panel->color1_btn_id);
     } else {
         /* If the first color doesn't exist, try to load the old one */
         if(!xfconf_channel_has_property(channel, buf)) {
             GValue value = { 0, };
-            old_property = xfdesktop_settings_generate_old_binding_string(panel, "color1");
+            old_property = xfdesktop_settings_generate_old_binding_string(panel, "rgba1");
 
             xfconf_channel_get_property(channel, old_property, &value);
 
             if(G_VALUE_HOLDS_BOXED(&value)) {
-                gtk_color_button_set_color(GTK_COLOR_BUTTON(panel->color1_btn),
+                gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(panel->color1_btn),
                                            g_value_get_boxed(&value));
                 g_value_unset(&value);
             } else {
                 /* revert to showing our default color */
-                GdkColor color1;
-                color1.red = 0x1515;
-                color1.green = 0x2222;
-                color1.blue = 0x3333;
-                gtk_color_button_set_color(GTK_COLOR_BUTTON(panel->color1_btn), &color1);
+                GdkRGBA color1;
+                color1.red = 0.5f;
+                color1.green = 0.5f;
+                color1.blue = 0.5f;
+                color1.alpha = 1.0f;
+                gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(panel->color1_btn), &color1);
             }
 
             g_free(old_property);
         }
 
         /* Now bind to the new value */
-        panel->color1_btn_id = xfconf_g_property_bind_gdkcolor(channel, buf,
-                                                               G_OBJECT(panel->color1_btn),
-                                                               "color");
+        panel->color1_btn_id = xfconf_g_property_bind_gdkrgba(channel, buf,
+                                                              G_OBJECT(panel->color1_btn),
+                                                              "rgba");
     }
     g_free(buf);
 
     /* color 2 button */
-    buf = xfdesktop_settings_generate_per_workspace_binding_string(panel, "color2");
+    /* Fixme: we will need to migrate from color1 to rgba1 (GdkColor to GdkRGBA) */
+    buf = xfdesktop_settings_generate_per_workspace_binding_string(panel, "rgba2");
     if(remove_binding) {
         xfconf_g_property_unbind(panel->color2_btn_id);
     } else {
         /* If the 2nd color doesn't exist, try to load the old one */
         if(!xfconf_channel_has_property(channel, buf)) {
             GValue value = { 0, };
-            old_property = xfdesktop_settings_generate_old_binding_string(panel, "color2");
+            old_property = xfdesktop_settings_generate_old_binding_string(panel, "rgba2");
 
             xfconf_channel_get_property(channel, old_property, &value);
 
             if(G_VALUE_HOLDS_BOXED(&value)) {
-                gtk_color_button_set_color(GTK_COLOR_BUTTON(panel->color2_btn),
+                gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(panel->color2_btn),
                                            g_value_get_boxed(&value));
                 g_value_unset(&value);
             } else {
                 /* revert to showing our default color */
-                GdkColor color2;
-                color2.red = 0x1515;
-                color2.green = 0x2222;
-                color2.blue = 0x3333;
-                gtk_color_button_set_color(GTK_COLOR_BUTTON(panel->color2_btn), &color2);
+                GdkRGBA color2;
+                color2.red = 0.5f;
+                color2.green = 0.5f;
+                color2.blue = 0.5f;
+                color2.alpha = 1.0f;
+                gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(panel->color2_btn), &color2);
             }
 
             g_free(old_property);
         }
 
         /* Now bind to the new value */
-        panel->color2_btn_id = xfconf_g_property_bind_gdkcolor(channel, buf,
-                                                               G_OBJECT(panel->color2_btn),
-                                                               "color");
+        panel->color2_btn_id = xfconf_g_property_bind_gdkrgba(channel, buf,
+                                                              G_OBJECT(panel->color2_btn),
+                                                              "rgba");
     }
     g_free(buf);
 
@@ -1718,7 +1722,6 @@ xfdesktop_settings_dialog_setup_tabs(GtkBuilder *main_gxml,
     GdkScreen *screen;
     WnckScreen *wnck_screen;
     XfconfChannel *channel = panel->channel;
-    GdkColor color;
     const gchar *path;
     GFile *file;
     gchar *uri_path;
@@ -1831,12 +1834,8 @@ xfdesktop_settings_dialog_setup_tabs(GtkBuilder *main_gxml,
                                                        "infobar_header"));
 
     panel->infobar_label = gtk_label_new("This is some text");
-    gdk_color_parse ("black", &color);
-    gtk_widget_modify_fg (panel->infobar_label, GTK_STATE_NORMAL, &color);
-    gtk_widget_set_no_show_all(panel->infobar, TRUE);
     gtk_widget_show(panel->infobar_label);
     gtk_widget_show(panel->infobar);
-    gtk_widget_set_visible(panel->infobar, FALSE);
 
     /* Add the panel's infobar label to the infobar, with this setup
      * it's easy to update the text for the infobar. */
diff --git a/src/xfce-backdrop.c b/src/xfce-backdrop.c
index d451a9d..956994f 100644
--- a/src/xfce-backdrop.c
+++ b/src/xfce-backdrop.c
@@ -115,8 +115,8 @@ struct _XfceBackdropPriv
     XfceBackdropImageData *image_data;
 
     XfceBackdropColorStyle color_style;
-    GdkColor color1;
-    GdkColor color2;
+    GdkRGBA color1;
+    GdkRGBA color2;
 
     XfceBackdropImageStyle image_style;
     gchar *image_path;
@@ -173,84 +173,77 @@ static guint backdrop_signals[LAST_SIGNAL] = { 0, };
 /* helper functions */
 
 static GdkPixbuf *
-create_solid(GdkColor *color,
+create_solid(GdkRGBA *color,
              gint width,
-             gint height,
-             gboolean has_alpha,
-             gint alpha)
+             gint height)
 {
+    GdkWindow *root;
     GdkPixbuf *pix;
-    guint32 rgba;
-    
-    pix = gdk_pixbuf_new(GDK_COLORSPACE_RGB, has_alpha, 8, width, height);
-    
-    rgba = ((((color->red & 0xff00) << 8) | ((color->green & 0xff00))
-            | ((color->blue & 0xff00) >> 8)) << 8) | alpha;
-    
-    gdk_pixbuf_fill(pix, rgba);
+    cairo_surface_t *surface;
+    cairo_t *cr;
+
+    root = gdk_screen_get_root_window(gdk_screen_get_default ());
+    surface = gdk_window_create_similar_surface(root, CAIRO_CONTENT_COLOR_ALPHA, width, height);
+    cr = cairo_create(surface);
+
+    cairo_set_source_rgba(cr, color->red, color->green, color->blue, color->alpha);
+
+    cairo_rectangle(cr, 0, 0, width, height);
+    cairo_fill(cr);
+
+    cairo_surface_flush(surface);
+
+    pix = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height);
+
+    cairo_destroy(cr);
+    cairo_surface_destroy(surface);
     
     return pix;
 }
 
 static GdkPixbuf *
-create_gradient(GdkColor *color1, GdkColor *color2, gint width, gint height,
+create_gradient(GdkRGBA *color1, GdkRGBA *color2, gint width, gint height,
         XfceBackdropColorStyle style)
 {
+    GdkWindow *root;
     GdkPixbuf *pix;
-    gint i, j;
-    GdkPixdata pixdata;
-    guint8 rgb[3];
-    GError *err = NULL;
-    
+    cairo_surface_t *surface;
+    cairo_pattern_t *pat;
+    cairo_t *cr;
+
     g_return_val_if_fail(color1 != NULL && color2 != NULL, NULL);
     g_return_val_if_fail(width > 0 && height > 0, NULL);
-    g_return_val_if_fail(style == XFCE_BACKDROP_COLOR_HORIZ_GRADIENT
-            || style == XFCE_BACKDROP_COLOR_VERT_GRADIENT, NULL);
-    
-    pixdata.magic = GDK_PIXBUF_MAGIC_NUMBER;
-    pixdata.length = GDK_PIXDATA_HEADER_LENGTH + (width * height * 3);
-    pixdata.pixdata_type = GDK_PIXDATA_COLOR_TYPE_RGB
-            | GDK_PIXDATA_SAMPLE_WIDTH_8 | GDK_PIXDATA_ENCODING_RAW;
-    pixdata.rowstride = width * 3;
-    pixdata.width = width;
-    pixdata.height = height;
-    pixdata.pixel_data = g_malloc(width * height * 3);
-
-    if(style == XFCE_BACKDROP_COLOR_HORIZ_GRADIENT) {
-        for(i = 0; i < width; i++) {
-            rgb[0] = (color1->red + (i * (color2->red - color1->red) / width)) >> 8;
-            rgb[1] = (color1->green + (i * (color2->green - color1->green) / width)) >> 8;
-            rgb[2] = (color1->blue + (i * (color2->blue - color1->blue) / width)) >> 8;
-            memcpy(pixdata.pixel_data+(i*3), rgb, 3);
-        }
-        
-        for(i = 1; i < height; i++) {
-            memcpy(pixdata.pixel_data+(i*pixdata.rowstride),
-                    pixdata.pixel_data, pixdata.rowstride);
-        }
-    } else if(XFCE_BACKDROP_COLOR_TRANSPARENT == style)
-        memset(pixdata.pixel_data, 0x0, width * height * 3);
-    else {
-        for(i = 0; i < height; i++) {
-            rgb[0] = (color1->red + (i * (color2->red - color1->red) / height)) >> 8;
-            rgb[1] = (color1->green + (i * (color2->green - color1->green) / height)) >> 8;
-            rgb[2] = (color1->blue + (i * (color2->blue - color1->blue) / height)) >> 8;
-            for(j = 0; j < width; j++)
-                memcpy(pixdata.pixel_data+(i*pixdata.rowstride)+(j*3), rgb, 3);
-        }
-    }
+    g_return_val_if_fail(style == XFCE_BACKDROP_COLOR_HORIZ_GRADIENT || style == XFCE_BACKDROP_COLOR_VERT_GRADIENT, NULL);
 
-G_GNUC_BEGIN_IGNORE_DEPRECATIONS
-    pix = gdk_pixbuf_from_pixdata(&pixdata, TRUE, &err);
-G_GNUC_END_IGNORE_DEPRECATIONS
-    if(!pix) {
-        g_warning("%s: Unable to create color gradient: %s\n", PACKAGE,
-                err->message);
-        g_error_free(err);
+    root = gdk_screen_get_root_window(gdk_screen_get_default ());
+    surface = gdk_window_create_similar_surface(root, CAIRO_CONTENT_COLOR_ALPHA, width, height);
+    cr = cairo_create(surface);
+
+    if(style == XFCE_BACKDROP_COLOR_VERT_GRADIENT) {
+        pat = cairo_pattern_create_linear (0.0, 0.0,  0.0, height);
+    } else if(style == XFCE_BACKDROP_COLOR_HORIZ_GRADIENT) {
+        pat = cairo_pattern_create_linear (0.0, 0.0,  width, 0.0);
+    } else {
+        cairo_destroy(cr);
+        cairo_surface_destroy(surface);
+        return NULL;
     }
-    
-    g_free(pixdata.pixel_data);
-    
+
+    cairo_pattern_add_color_stop_rgba (pat, 1, color2->red, color2->green, color2->blue, color2->alpha);
+    cairo_pattern_add_color_stop_rgba (pat, 0, color1->red, color1->green, color1->blue, color1->alpha);
+
+    cairo_rectangle(cr, 0, 0, width, height);
+    cairo_set_source(cr, pat);
+    cairo_fill(cr);
+
+    cairo_surface_flush(surface);
+
+    pix = gdk_pixbuf_get_from_surface(surface, 0, 0, width, height);
+
+    cairo_destroy(cr);
+    cairo_pattern_destroy(pat);
+    cairo_surface_destroy(surface);
+
     return pix;
 }
 
@@ -741,14 +734,14 @@ xfce_backdrop_class_init(XfceBackdropClass *klass)
                                     g_param_spec_boxed("first-color",
                                                        "first color",
                                                        "first color",
-                                                       GDK_TYPE_COLOR,
+                                                       GDK_TYPE_RGBA,
                                                        XFDESKTOP_PARAM_FLAGS));
 
     g_object_class_install_property(gobject_class, PROP_COLOR2,
                                     g_param_spec_boxed("second-color",
                                                        "second color",
                                                        "second color",
-                                                       GDK_TYPE_COLOR,
+                                                       GDK_TYPE_RGBA,
                                                        XFDESKTOP_PARAM_FLAGS));
 
     /* Defaults to an invalid image style so that
@@ -817,12 +810,14 @@ xfce_backdrop_init(XfceBackdrop *backdrop)
     backdrop->priv->cycle_timer_id = 0;
 
     /* color defaults */
-    backdrop->priv->color1.red = 0x1515;
-    backdrop->priv->color1.green = 0x2222;
-    backdrop->priv->color1.blue = 0x3333;
-    backdrop->priv->color2.red = 0x1515;
-    backdrop->priv->color2.green = 0x2222;
-    backdrop->priv->color2.blue = 0x3333;
+    backdrop->priv->color1.red = 0.08235f;
+    backdrop->priv->color1.green = 0.13333f;
+    backdrop->priv->color1.blue = 0.2f;
+    backdrop->priv->color1.alpha = 1.0f;
+    backdrop->priv->color2.red = 0.08235f;
+    backdrop->priv->color2.green = 0.13333f;
+    backdrop->priv->color2.blue = 0.2f;
+    backdrop->priv->color2.alpha = 1.0f;
 }
 
 static void
@@ -861,7 +856,7 @@ xfce_backdrop_set_property(GObject *object,
                            GParamSpec *pspec)
 {
     XfceBackdrop *backdrop = XFCE_BACKDROP(object);
-    GdkColor *color;
+    GdkRGBA *color;
 
     switch(property_id) {
         case PROP_COLOR_STYLE:
@@ -1081,7 +1076,7 @@ xfce_backdrop_get_color_style(XfceBackdrop *backdrop)
 /**
  * xfce_backdrop_set_first_color:
  * @backdrop: An #XfceBackdrop.
- * @color: A #GdkColor.
+ * @color: A #GdkRGBA.
  *
  * Sets the "first" color for the #XfceBackdrop.  This is the color used if
  * the color style is set to XFCE_BACKDROP_COLOR_SOLID.  It is used as the
@@ -1091,35 +1086,38 @@ xfce_backdrop_get_color_style(XfceBackdrop *backdrop)
  **/
 void
 xfce_backdrop_set_first_color(XfceBackdrop *backdrop,
-                              const GdkColor *color)
+                              const GdkRGBA *color)
 {
     g_return_if_fail(XFCE_IS_BACKDROP(backdrop) && color != NULL);
     
     if(color->red != backdrop->priv->color1.red
             || color->green != backdrop->priv->color1.green
-            || color->blue != backdrop->priv->color1.blue)
+            || color->blue != backdrop->priv->color1.blue
+            || color->alpha != backdrop->priv->color1.alpha)
     {
         xfce_backdrop_clear_cached_image(backdrop);
         backdrop->priv->color1.red = color->red;
         backdrop->priv->color1.green = color->green;
         backdrop->priv->color1.blue = color->blue;
+        backdrop->priv->color1.alpha = color->alpha;
+
         g_signal_emit(G_OBJECT(backdrop), backdrop_signals[BACKDROP_CHANGED], 0);
     }
 }
 
 void
 xfce_backdrop_get_first_color(XfceBackdrop *backdrop,
-                              GdkColor *color)
+                              GdkRGBA *color)
 {
     g_return_if_fail(XFCE_IS_BACKDROP(backdrop) && color);
     
-    memcpy(color, &backdrop->priv->color1, sizeof(GdkColor));
+    memcpy(color, &backdrop->priv->color1, sizeof(GdkRGBA));
 }
 
 /**
  * xfce_backdrop_set_second_color:
  * @backdrop: An #XfceBackdrop.
- * @color: A #GdkColor.
+ * @color: A #GdkRGBA.
  *
  * Sets the "second" color for the #XfceBackdrop.  This is the color used as the
  * right-side color or bottom color if the color style is set to
@@ -1128,18 +1126,21 @@ xfce_backdrop_get_first_color(XfceBackdrop *backdrop,
  **/
 void
 xfce_backdrop_set_second_color(XfceBackdrop *backdrop,
-                               const GdkColor *color)
+                               const GdkRGBA *color)
 {
     g_return_if_fail(XFCE_IS_BACKDROP(backdrop) && color != NULL);
     
     if(color->red != backdrop->priv->color2.red
             || color->green != backdrop->priv->color2.green
-            || color->blue != backdrop->priv->color2.blue)
+            || color->blue != backdrop->priv->color2.blue
+            || color->alpha != backdrop->priv->color2.alpha)
     {
         xfce_backdrop_clear_cached_image(backdrop);
         backdrop->priv->color2.red = color->red;
         backdrop->priv->color2.green = color->green;
         backdrop->priv->color2.blue = color->blue;
+        backdrop->priv->color2.alpha = color->alpha;
+
         if(backdrop->priv->color_style != XFCE_BACKDROP_COLOR_SOLID)
             g_signal_emit(G_OBJECT(backdrop), backdrop_signals[BACKDROP_CHANGED], 0);
     }
@@ -1147,11 +1148,11 @@ xfce_backdrop_set_second_color(XfceBackdrop *backdrop,
 
 void
 xfce_backdrop_get_second_color(XfceBackdrop *backdrop,
-                               GdkColor *color)
+                               GdkRGBA *color)
 {
     g_return_if_fail(XFCE_IS_BACKDROP(backdrop) && color);
     
-    memcpy(color, &backdrop->priv->color2, sizeof(GdkColor));
+    memcpy(color, &backdrop->priv->color2, sizeof(GdkRGBA));
 }
 
 /**
@@ -1660,15 +1661,15 @@ xfce_backdrop_generate_canvas(XfceBackdrop *backdrop)
     }
 
     if(backdrop->priv->color_style == XFCE_BACKDROP_COLOR_SOLID)
-        final_image = create_solid(&backdrop->priv->color1, w, h, FALSE, 0xff);
+        final_image = create_solid(&backdrop->priv->color1, w, h);
     else if(backdrop->priv->color_style == XFCE_BACKDROP_COLOR_TRANSPARENT) {
-        GdkColor c = { 0, 0xffff, 0xffff, 0xffff };
-        final_image = create_solid(&c, w, h, TRUE, 0x00);
+        GdkRGBA c = { 1.0f, 1.0f, 1.0f, 1.0f };
+        final_image = create_solid(&c, w, h);
     } else {
         final_image = create_gradient(&backdrop->priv->color1,
                 &backdrop->priv->color2, w, h, backdrop->priv->color_style);
         if(!final_image)
-            final_image = create_solid(&backdrop->priv->color1, w, h, FALSE, 0xff);
+            final_image = create_solid(&backdrop->priv->color1, w, h);
     }
 
     return final_image;
diff --git a/src/xfce-backdrop.h b/src/xfce-backdrop.h
index 4879a34..b49cb35 100644
--- a/src/xfce-backdrop.h
+++ b/src/xfce-backdrop.h
@@ -107,14 +107,14 @@ XfceBackdropColorStyle xfce_backdrop_get_color_style
                                          (XfceBackdrop *backdrop);
 
 void xfce_backdrop_set_first_color       (XfceBackdrop *backdrop,
-                                          const GdkColor *color);
+                                          const GdkRGBA *color);
 void xfce_backdrop_get_first_color       (XfceBackdrop *backdrop,
-                                          GdkColor *color);
+                                          GdkRGBA *color);
 
 void xfce_backdrop_set_second_color      (XfceBackdrop *backdrop,
-                                          const GdkColor *color);
+                                          const GdkRGBA *color);
 void xfce_backdrop_get_second_color      (XfceBackdrop *backdrop,
-                                          GdkColor *color);
+                                          GdkRGBA *color);
 
 void xfce_backdrop_set_image_style       (XfceBackdrop *backdrop,
                                           XfceBackdropImageStyle style);
diff --git a/src/xfce-workspace.c b/src/xfce-workspace.c
index a293d6d..3c4518f 100644
--- a/src/xfce-workspace.c
+++ b/src/xfce-workspace.c
@@ -455,6 +455,12 @@ xfce_workspace_migrate_backdrop_first_color(XfceWorkspace *workspace,
 
     TRACE("entering");
 
+    /* TODO: migrate from color1 (and older) to the new rgba1 */
+    if(TRUE) {
+        TRACE("warning: we aren't migrating from GdkColor to GdkRGBA yet");
+        return;
+    }
+
     /* Use the old property format */
     g_snprintf(buf, sizeof(buf), "%smonitor%d/",
                workspace->priv->property_prefix, monitor);
@@ -481,6 +487,12 @@ xfce_workspace_migrate_backdrop_second_color(XfceWorkspace *workspace,
 
     TRACE("entering");
 
+    /* TODO: migrate from color1 (and older) to the new rgba1 */
+    if(TRUE) {
+        TRACE("warning: we aren't migrating from GdkColor to GdkRGBA yet");
+        return;
+    }
+
     /* Use the old property format */
     g_snprintf(buf, sizeof(buf), "%smonitor%d/",
                workspace->priv->property_prefix, monitor);
@@ -624,19 +636,19 @@ xfce_workspace_connect_backdrop_settings(XfceWorkspace *workspace,
                            G_OBJECT(backdrop), "color-style");
 
     buf[pp_len] = 0;
-    g_strlcat(buf, "color1", sizeof(buf));
+    g_strlcat(buf, "rgba1", sizeof(buf));
     if(!xfconf_channel_has_property(channel, buf)) {
         xfce_workspace_migrate_backdrop_first_color(workspace, backdrop, monitor);
     }
-    workspace->priv->first_color_id[monitor] = xfconf_g_property_bind_gdkcolor(channel, buf,
+    workspace->priv->first_color_id[monitor] = xfconf_g_property_bind_gdkrgba(channel, buf,
                                                             G_OBJECT(backdrop), "first-color");
 
     buf[pp_len] = 0;
-    g_strlcat(buf, "color2", sizeof(buf));
+    g_strlcat(buf, "rgba2", sizeof(buf));
     if(!xfconf_channel_has_property(channel, buf)) {
         xfce_workspace_migrate_backdrop_second_color(workspace, backdrop, monitor);
     }
-    workspace->priv->second_color_id[monitor] = xfconf_g_property_bind_gdkcolor(channel, buf,
+    workspace->priv->second_color_id[monitor] = xfconf_g_property_bind_gdkrgba(channel, buf,
                                                             G_OBJECT(backdrop), "second-color");
 
     buf[pp_len] = 0;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list