[Xfce4-commits] <xfce4-panel:master> Add xfce_panel_pixbuf_from_source().

Nick Schermer noreply at xfce.org
Tue Dec 28 13:10:01 CET 2010


Updating branch refs/heads/master
         to 72c7bf9eefa179509b8c62d70db8a9b16a7d5fa4 (commit)
       from 61c91e8365add52992e3015ff88e9beebdd7f64a (commit)

commit 72c7bf9eefa179509b8c62d70db8a9b16a7d5fa4
Author: Nick Schermer <nick at xfce.org>
Date:   Tue Dec 28 12:53:39 2010 +0100

    Add xfce_panel_pixbuf_from_source().
    
    Convenience function to load pixbuf from icon names
    the the ones used in desktop files.
    Code is the same as that from XfcePanelImage.

 docs/references/libxfce4panel-sections.txt |    1 +
 libxfce4panel/libxfce4panel.symbols        |    1 +
 libxfce4panel/xfce-panel-convenience.c     |  123 ++++++++++++++++++++++++++++
 libxfce4panel/xfce-panel-convenience.h     |    4 +
 4 files changed, 129 insertions(+), 0 deletions(-)

diff --git a/docs/references/libxfce4panel-sections.txt b/docs/references/libxfce4panel-sections.txt
index 843e2b4..e681dc8 100644
--- a/docs/references/libxfce4panel-sections.txt
+++ b/docs/references/libxfce4panel-sections.txt
@@ -56,6 +56,7 @@ xfce_hvbox_get_type
 xfce_panel_create_button
 xfce_panel_create_toggle_button
 xfce_panel_get_channel_name
+xfce_panel_pixbuf_from_source
 xfce_allow_panel_customization
 xfce_create_panel_button
 xfce_create_panel_toggle_button
diff --git a/libxfce4panel/libxfce4panel.symbols b/libxfce4panel/libxfce4panel.symbols
index 5c856ce..4821b79 100644
--- a/libxfce4panel/libxfce4panel.symbols
+++ b/libxfce4panel/libxfce4panel.symbols
@@ -76,6 +76,7 @@ xfce_hvbox_get_orientation
 xfce_panel_create_button G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT
 xfce_panel_create_toggle_button G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT
 xfce_panel_get_channel_name
+xfce_panel_pixbuf_from_source
 #endif
 #endif
 
diff --git a/libxfce4panel/xfce-panel-convenience.c b/libxfce4panel/xfce-panel-convenience.c
index a6e30d8..ed4e681 100644
--- a/libxfce4panel/xfce-panel-convenience.c
+++ b/libxfce4panel/xfce-panel-convenience.c
@@ -21,6 +21,10 @@
 #include <config.h>
 #endif
 
+#ifdef HAVE_MATH_H
+#include <math.h>
+#endif
+
 #include <libxfce4util/libxfce4util.h>
 #include <gtk/gtk.h>
 
@@ -118,5 +122,124 @@ xfce_panel_get_channel_name (void)
 
 
 
+/**
+ * xfce_panel_pixbuf_from_source:
+ * @source: string that contains the location of an icon
+ * @icon_theme: icon theme or %NULL to use the default icon theme
+ * @size: size the icon should be loaded
+ *
+ * Try to load a pixbug from a source string. The source could be
+ * an abolute path, an icon name or a filename that point to a
+ * file in the pixmaps directory.
+ *
+ * This function is particularly usefull for loading names from
+ * the Icon key of desktop files.
+ *
+ * The pixbufs is never bigger then @size. If it is when loaded from the
+ * disk, the pixbuf is scales preserving the aspect ratio.
+ *
+ * Returns: a GdkPixbuf or %NULL is nothing was found. The value should
+ *          be released with g_object_unref is no long used.
+ *
+ * See also: XfcePanelImage
+ *
+ * Since: 4.8
+ **/
+GdkPixbuf *
+xfce_panel_pixbuf_from_source (const gchar  *source,
+                               GtkIconTheme *icon_theme,
+                               gint          size)
+{
+  GdkPixbuf *pixbuf = NULL;
+  gchar     *p;
+  gchar     *name;
+  gchar     *filename;
+  gint       src_w, src_h;
+  gdouble    wratio, hratio;
+  gint       dest_w, dest_h;
+  GdkPixbuf *dest;
+  GError    *error = NULL;
+
+  g_return_val_if_fail (source != NULL, NULL);
+  g_return_val_if_fail (icon_theme == NULL || GTK_IS_ICON_THEME (icon_theme), NULL);
+  g_return_val_if_fail (size > 0, NULL);
+
+  if (G_UNLIKELY (g_path_is_absolute (source)))
+    {
+      pixbuf = gdk_pixbuf_new_from_file (source, &error);
+      if (G_UNLIKELY (pixbuf == NULL))
+        {
+          g_message ("Failed to load image \"%s\": %s",
+                     source, error->message);
+          g_error_free (error);
+        }
+    }
+  else
+    {
+      if (G_UNLIKELY (icon_theme == NULL))
+        icon_theme = gtk_icon_theme_get_default ();
+
+      /* try to load from the icon theme */
+      pixbuf = gtk_icon_theme_load_icon (icon_theme, source, size, 0, NULL);
+      if (G_UNLIKELY (pixbuf == NULL))
+        {
+          /* try to lookup names like application.png in the theme */
+          p = strrchr (source, '.');
+          if (p != NULL)
+            {
+              name = g_strndup (source, p - source);
+              pixbuf = gtk_icon_theme_load_icon (icon_theme, name, size, 0, NULL);
+              g_free (name);
+            }
+
+          /* maybe they point to a file in the pixbufs folder */
+          if (G_UNLIKELY (pixbuf == NULL))
+            {
+              filename = g_build_filename ("pixmaps", source, NULL);
+              name = xfce_resource_lookup (XFCE_RESOURCE_DATA, filename);
+              g_free (filename);
+
+              if (name != NULL)
+                {
+                  pixbuf = gdk_pixbuf_new_from_file (name, NULL);
+                  g_free (name);
+                }
+            }
+        }
+    }
+
+  /* scale the pixbug if required */
+  if (G_LIKELY (pixbuf != NULL))
+    {
+      src_w = gdk_pixbuf_get_width (pixbuf);
+      src_h = gdk_pixbuf_get_height (pixbuf);
+
+      if (src_w > size || src_h > size)
+        {
+          /* calculate the new dimensions */
+          wratio = (gdouble) src_w / (gdouble) size;
+          hratio = (gdouble) src_h / (gdouble) size;
+
+          dest_w = dest_h = size;
+
+          if (hratio > wratio)
+            dest_w  = rint (src_w / hratio);
+          else
+            dest_h = rint (src_h / wratio);
+
+          dest = gdk_pixbuf_scale_simple (pixbuf, MAX (dest_w, 1),
+                                          MAX (dest_h, 1),
+                                          GDK_INTERP_BILINEAR);
+
+          g_object_unref (G_OBJECT (pixbuf));
+          pixbuf = dest;
+        }
+    }
+
+  return pixbuf;
+}
+
+
+
 #define __XFCE_PANEL_CONVENIENCE_C__
 #include <libxfce4panel/libxfce4panel-aliasdef.c>
diff --git a/libxfce4panel/xfce-panel-convenience.h b/libxfce4panel/xfce-panel-convenience.h
index 4f616f0..d0a0021 100644
--- a/libxfce4panel/xfce-panel-convenience.h
+++ b/libxfce4panel/xfce-panel-convenience.h
@@ -35,6 +35,10 @@ GtkWidget   *xfce_panel_create_toggle_button  (void) G_GNUC_MALLOC G_GNUC_WARN_U
 
 const gchar *xfce_panel_get_channel_name      (void);
 
+GdkPixbuf   *xfce_panel_pixbuf_from_source    (const gchar  *source,
+                                               GtkIconTheme *icon_theme,
+                                               gint          size) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
+
 G_END_DECLS
 
 #endif /* !__XFCE_PANEL_CONVENIENCE_H__ */



More information about the Xfce4-commits mailing list