[gnome-panel/gtk3] panel, libpanel-applet: Cleanup the background code a bit



commit cef832d3729250af58477833408efd6ebddc1171
Author: Vincent Untz <vuntz gnome org>
Date:   Tue Feb 1 00:41:44 2011 +0100

    panel, libpanel-applet: Cleanup the background code a bit
    
    Explicitly check for CAIRO_STATUS_SUCCESS return value after calls to
    cairo_pattern_get_surface() and cairo_surface_status().
    
    Make the utility functions to set background on a widget a bit more
    useful, and document some tricks I learnt by playing with things:
    calling gtk_style_context_add_provider() again instead of just once is
    the right thing to do, else it doesn't work well.
    
    Drop trivial functions, and variables that track a state that we don't
    care about.

 gnome-panel/panel-background.c |   71 ++++++++++++++++++----------------
 gnome-panel/panel-background.h |    1 -
 libpanel-applet/panel-applet.c |   82 +++++++++++++++++++++++++---------------
 3 files changed, 89 insertions(+), 65 deletions(-)
---
diff --git a/gnome-panel/panel-background.c b/gnome-panel/panel-background.c
index 9d73f7f..228721c 100644
--- a/gnome-panel/panel-background.c
+++ b/gnome-panel/panel-background.c
@@ -40,14 +40,6 @@ static gboolean panel_background_composite (PanelBackground *background);
 static void load_background_file (PanelBackground *background);
 
 
-static void
-set_pixbuf_background (PanelBackground *background)
-{
-	g_assert (background->composited_pattern != NULL);
-
-        gdk_window_set_background_pattern (background->window, background->composited_pattern);
-}
-
 static gboolean
 panel_background_prepare (PanelBackground *background)
 {
@@ -71,13 +63,16 @@ panel_background_prepare (PanelBackground *background)
 	case PANEL_BACK_COLOR:
 		if (background->has_alpha &&
 		    background->composited_pattern)
-			set_pixbuf_background (background);
+			gdk_window_set_background_pattern (background->window,
+							   background->composited_pattern);
 		else
 			gdk_window_set_background_rgba (background->window,
                                                         &background->color);
 		break;
 	case PANEL_BACK_IMAGE:
-		set_pixbuf_background (background);
+		g_assert (background->composited_pattern != NULL);
+		gdk_window_set_background_pattern (background->window,
+						   background->composited_pattern);
 		break;
 	default:
 		g_assert_not_reached ();
@@ -96,8 +91,6 @@ panel_background_prepare (PanelBackground *background)
 	if (GTK_IS_WIDGET (widget))
 	  gtk_widget_queue_draw (widget);
 
-	background->prepared = TRUE;
-
 	background->notify_changed (background, background->user_data);
 
 	return TRUE;
@@ -106,7 +99,6 @@ panel_background_prepare (PanelBackground *background)
 static void
 free_composited_resources (PanelBackground *background)
 {
-        background->prepared = FALSE;
 	background->composited = FALSE;
 
 	if (background->composited_pattern)
@@ -708,7 +700,6 @@ panel_background_realized (PanelBackground *background,
 void
 panel_background_unrealized (PanelBackground *background)
 {
-        background->prepared = FALSE;
 	if (background->window)
 		g_object_unref (background->window);
 	background->window = NULL;
@@ -827,7 +818,6 @@ panel_background_init (PanelBackground              *background,
 
 	background->transformed = FALSE;
 	background->composited  = FALSE;
-	background->prepared    = FALSE;
 }
 
 void
@@ -876,11 +866,10 @@ panel_background_make_string (PanelBackground *background,
 		if (!background->composited_pattern)
 			return NULL;
 
-                if (cairo_pattern_get_surface (background->composited_pattern, &surface))
+                if (cairo_pattern_get_surface (background->composited_pattern, &surface) != CAIRO_STATUS_SUCCESS)
                         return NULL;
 
-                if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_XLIB)
-                        return NULL;
+                g_assert (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB);
 
                 variant = g_variant_new ("(uii)", (guint32)cairo_xlib_surface_get_drawable (surface),
                                          x, y);
@@ -939,7 +928,7 @@ panel_background_get_pattern_for_widget (PanelBackground *background,
         if (!background->composited_pattern)
                 return NULL;
 
-        if (cairo_pattern_get_surface (background->composited_pattern, &bg_surface))
+        if (cairo_pattern_get_surface (background->composited_pattern, &bg_surface) != CAIRO_STATUS_SUCCESS)
                 return NULL;
 
 	gtk_widget_get_allocation (widget, &allocation);
@@ -964,22 +953,37 @@ panel_background_get_pattern_for_widget (PanelBackground *background,
 }
 
 static GtkStyleProperties *
-get_widget_style_properties (GtkWidget *widget)
+_panel_background_get_widget_style_properties (GtkWidget *widget,
+                                               gboolean   create_if_needed)
 {
         GtkStyleProperties *properties;
 
-        properties = g_object_get_data (G_OBJECT (widget), "panel-object-style-props");
-        if (!properties) {
+        properties = g_object_get_data (G_OBJECT (widget),
+                                        "panel-object-style-props");
+
+        if (!properties && create_if_needed) {
                 properties = gtk_style_properties_new ();
-                g_object_set_data_full (G_OBJECT (widget), "panel-object-style-props",
-                                        properties, (GDestroyNotify)g_object_unref);
+                g_object_set_data_full (G_OBJECT (widget),
+                                        "panel-object-style-props",
+                                        properties,
+                                        (GDestroyNotify) g_object_unref);
         }
+
         return properties;
 }
 
 static void
-reset_widget_style_properties (GtkWidget *widget)
+_panel_background_reset_widget_style_properties (GtkWidget *widget)
 {
+        GtkStyleProperties *properties;
+
+        properties = _panel_background_get_widget_style_properties (widget, FALSE);
+
+        if (properties)
+                gtk_style_context_remove_provider (gtk_widget_get_style_context (widget),
+                                                   GTK_STYLE_PROVIDER (properties));
+
+
         g_object_set_data (G_OBJECT (widget), "panel-object-style-props", NULL);
 }
 
@@ -991,16 +995,13 @@ panel_background_change_background_on_widget (PanelBackground *background,
 
         gtk_widget_reset_style (widget);
 
-        properties = get_widget_style_properties (widget);
-
 	switch (panel_background_get_type (background)) {
 	case PANEL_BACK_NONE:
-                gtk_style_context_remove_provider (gtk_widget_get_style_context (widget),
-                                                   GTK_STYLE_PROVIDER (properties));
-                reset_widget_style_properties (widget);
+                _panel_background_reset_widget_style_properties (widget);
                 return;
 	case PANEL_BACK_COLOR:
                 if (!background->has_alpha) {
+                        properties = _panel_background_get_widget_style_properties (widget, TRUE);
                         gtk_style_properties_set (properties, GTK_STATE_FLAG_NORMAL,
                                                   "background-color", &background->color,
                                                   "background-image", NULL,
@@ -1011,16 +1012,18 @@ panel_background_change_background_on_widget (PanelBackground *background,
 	case PANEL_BACK_IMAGE: {
                 cairo_pattern_t *pattern;
 
+                properties = _panel_background_get_widget_style_properties (widget, TRUE);
                 pattern = panel_background_get_pattern_for_widget (background, widget);
                 if (pattern) {
                         gtk_style_properties_set (properties, GTK_STATE_FLAG_NORMAL,
+                                                  /* background-color can't be
+                                                   * NULL, but is ignored
+                                                   * anyway */
                                                   "background-image", pattern,
                                                   NULL);
                         cairo_pattern_destroy (pattern);
                 } else {
-                        gtk_style_context_remove_provider (gtk_widget_get_style_context (widget),
-                                                           GTK_STYLE_PROVIDER (properties));
-                        reset_widget_style_properties (widget);
+                        _panel_background_reset_widget_style_properties (widget);
                         return;
                 }
         }
@@ -1030,6 +1033,8 @@ panel_background_change_background_on_widget (PanelBackground *background,
 		break;
 	}
 
+	/* Note: this actually replaces the old properties, since it's the same
+	 * pointer */
         gtk_style_context_add_provider (gtk_widget_get_style_context (widget),
                                         GTK_STYLE_PROVIDER (properties),
                                         GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
diff --git a/gnome-panel/panel-background.h b/gnome-panel/panel-background.h
index a949729..e9b86df 100644
--- a/gnome-panel/panel-background.h
+++ b/gnome-panel/panel-background.h
@@ -71,7 +71,6 @@ struct _PanelBackground {
 	guint                   loaded : 1;
 	guint                   transformed : 1;
 	guint                   composited : 1;
-	guint                   prepared : 1;
 };
 
 void  panel_background_init              (PanelBackground     *background,
diff --git a/libpanel-applet/panel-applet.c b/libpanel-applet/panel-applet.c
index 683c581..2829a34 100644
--- a/libpanel-applet/panel-applet.c
+++ b/libpanel-applet/panel-applet.c
@@ -1134,6 +1134,20 @@ panel_applet_popup_menu (GtkWidget *widget)
 	return TRUE;
 }
 
+static GtkSizeRequestMode
+panel_applet_get_request_mode (GtkWidget *widget)
+{
+        PanelApplet *applet = PANEL_APPLET (widget);
+        PanelAppletOrient orientation;
+
+        orientation = panel_applet_get_orient (applet);
+        if (orientation == PANEL_APPLET_ORIENT_UP ||
+            orientation == PANEL_APPLET_ORIENT_DOWN)
+                return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
+
+        return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
+}
+
 static void
 panel_applet_get_preferred_width (GtkWidget *widget,
                                   int       *minimum_width,
@@ -1158,20 +1172,6 @@ panel_applet_get_preferred_width (GtkWidget *widget,
         *natural_width += 2 * focus_width;
 }
 
-static GtkSizeRequestMode
-panel_applet_get_request_mode (GtkWidget *widget)
-{
-        PanelApplet *applet = PANEL_APPLET (widget);
-        PanelAppletOrient orientation;
-
-        orientation = panel_applet_get_orient (applet);
-        if (orientation == PANEL_APPLET_ORIENT_UP ||
-            orientation == PANEL_APPLET_ORIENT_DOWN)
-                return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
-
-        return GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
-}
-
 static void
 panel_applet_get_preferred_height (GtkWidget *widget,
                                   int       *minimum_height,
@@ -1366,10 +1366,10 @@ panel_applet_create_foreign_surface_for_display (GdkDisplay     *display,
 }
 
 static cairo_pattern_t *
-panel_applet_get_pixmap (PanelApplet    *applet,
-                         GdkNativeWindow xid,
-                         int             x,
-			 int             y)
+panel_applet_get_pattern_from_pixmap (PanelApplet    *applet,
+                                      GdkNativeWindow xid,
+                                      int             x,
+                                      int             y)
 {
 	GdkWindow       *window;
 	int              width;
@@ -1392,7 +1392,11 @@ panel_applet_get_pixmap (PanelApplet    *applet,
                                                                       gdk_window_get_visual (window),
                                                                       xid);
         gdk_error_trap_pop_ignored ();
-        if (!background || cairo_surface_status (background)) {
+
+        /* background can be NULL if the user changes the background very fast.
+         * We'll get the next update, so it's not a big deal. */
+        if (!background ||
+            cairo_surface_status (background) != CAIRO_STATUS_SUCCESS) {
                 if (background)
                         cairo_surface_destroy (background);
                 return NULL;
@@ -1468,7 +1472,7 @@ panel_applet_get_background (PanelApplet *applet)
                 int x, y;
 
                 g_variant_get (variant, "(uii)", &xid, &x, &y);
-                pattern = panel_applet_get_pixmap (applet, xid, x, y);
+                pattern = panel_applet_get_pattern_from_pixmap (applet, xid, x, y);
                 if (!pattern)
                         g_warning ("Failed to get pixmap %d, %d, %d", xid, x, y);
         }
@@ -1501,22 +1505,36 @@ panel_applet_set_background_string (PanelApplet *applet,
 }
 
 static GtkStyleProperties *
-get_widget_style_properties (GtkWidget *widget)
+_panel_applet_get_widget_style_properties (GtkWidget *widget,
+                                           gboolean   create_if_needed)
 {
         GtkStyleProperties *properties;
 
-        properties = g_object_get_data (G_OBJECT (widget), "panel-applet-style-props");
-        if (!properties) {
+        properties = g_object_get_data (G_OBJECT (widget),
+                                        "panel-applet-style-props");
+
+        if (!properties && create_if_needed) {
                 properties = gtk_style_properties_new ();
-                g_object_set_data_full (G_OBJECT (widget), "panel-applet-style-props",
-                                        properties, (GDestroyNotify)g_object_unref);
+                g_object_set_data_full (G_OBJECT (widget),
+                                        "panel-applet-style-props",
+                                        properties,
+                                        (GDestroyNotify) g_object_unref);
         }
+
         return properties;
 }
 
 static void
-reset_widget_style_properties (GtkWidget *widget)
+_panel_applet_reset_widget_style_properties (GtkWidget *widget)
 {
+        GtkStyleProperties *properties;
+
+        properties = _panel_applet_get_widget_style_properties (widget, FALSE);
+
+        if (properties)
+                gtk_style_context_remove_provider (gtk_widget_get_style_context (widget),
+                                                   GTK_STYLE_PROVIDER (properties));
+
         g_object_set_data (G_OBJECT (widget), "panel-applet-style-props", NULL);
 }
 
@@ -1528,15 +1546,13 @@ panel_applet_update_background_for_widget (GtkWidget       *widget,
 
         gtk_widget_reset_style (widget);
 
-        properties = get_widget_style_properties (widget);
-
         if (!pattern) {
-                gtk_style_context_remove_provider (gtk_widget_get_style_context (widget),
-                                                   GTK_STYLE_PROVIDER (properties));
-                reset_widget_style_properties (widget);
+                _panel_applet_reset_widget_style_properties (widget);
                 return;
         }
 
+        properties = _panel_applet_get_widget_style_properties (widget, TRUE);
+
         switch (cairo_pattern_get_type (pattern)) {
         case CAIRO_PATTERN_TYPE_SOLID: {
                 GdkRGBA color;
@@ -1550,6 +1566,8 @@ panel_applet_update_background_for_widget (GtkWidget       *widget,
                 break;
         case CAIRO_PATTERN_TYPE_SURFACE:
                 gtk_style_properties_set (properties, GTK_STATE_FLAG_NORMAL,
+					  /* background-color can't be NULL,
+					   * but is ignored anyway */
                                           "background-image", pattern,
                                           NULL);
                 break;
@@ -1557,6 +1575,8 @@ panel_applet_update_background_for_widget (GtkWidget       *widget,
                 break;
         }
 
+	/* Note: this actually replaces the old properties, since it's the same
+	 * pointer */
         gtk_style_context_add_provider (gtk_widget_get_style_context (widget),
                                         GTK_STYLE_PROVIDER (properties),
                                         GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]