Browse Source

Add function to load source at a size.

Like xfce_panel_pixbuf_from_source, but with a
dest width and height.
tags/xfce4-panel-4.10.0
Nick Schermer 9 years ago
parent
commit
8c89c08518
4 changed files with 59 additions and 24 deletions
  1. +1
    -0
      docs/references/libxfce4panel-sections.txt
  2. +1
    -0
      libxfce4panel/libxfce4panel.symbols
  3. +46
    -18
      libxfce4panel/xfce-panel-convenience.c
  4. +11
    -6
      libxfce4panel/xfce-panel-convenience.h

+ 1
- 0
docs/references/libxfce4panel-sections.txt View File

@@ -57,6 +57,7 @@ xfce_panel_create_button
xfce_panel_create_toggle_button
xfce_panel_get_channel_name
xfce_panel_pixbuf_from_source
xfce_panel_pixbuf_from_source_at_size
xfce_allow_panel_customization
xfce_create_panel_button
xfce_create_panel_toggle_button


+ 1
- 0
libxfce4panel/libxfce4panel.symbols View File

@@ -78,6 +78,7 @@ 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
xfce_panel_pixbuf_from_source_at_size
#endif
#endif



+ 46
- 18
libxfce4panel/xfce-panel-convenience.c View File

@@ -123,10 +123,11 @@ xfce_panel_get_channel_name (void)


/**
* xfce_panel_pixbuf_from_source:
* xfce_panel_pixbuf_from_source_at_size:
* @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 that should be loaded
* @dest_width: the maximum returned width of the GdkPixbuf
* @dest_height: the maximum returned height of the GdkPixbuf
*
* Try to load a pixbuf from a source string. The source could be
* an abolute path, an icon name or a filename that points to a
@@ -135,20 +136,22 @@ xfce_panel_get_channel_name (void)
* This function is particularly usefull for loading names from
* the Icon key of desktop files.
*
* The pixbuf is never bigger than @size. If it is when loaded from the
* disk, the pixbuf is scaled preserving the aspect ratio.
* The pixbuf is never bigger than @dest_width and @dest_height.
* If it is when loaded from the disk, the pixbuf is scaled
* preserving the aspect ratio.
*
* Returns: a GdkPixbuf or %NULL if nothing was found. The value should
* be released with g_object_unref when no longer used.
*
* See also: XfcePanelImage
*
* Since: 4.8
* Since: 4.10
**/
GdkPixbuf *
xfce_panel_pixbuf_from_source (const gchar *source,
GtkIconTheme *icon_theme,
gint size)
xfce_panel_pixbuf_from_source_at_size (const gchar *source,
GtkIconTheme *icon_theme,
gint dest_width,
gint dest_height)
{
GdkPixbuf *pixbuf = NULL;
gchar *p;
@@ -156,17 +159,18 @@ xfce_panel_pixbuf_from_source (const gchar *source,
gchar *filename;
gint src_w, src_h;
gdouble wratio, hratio;
gint dest_w, dest_h;
GdkPixbuf *dest;
GError *error = NULL;
gint size = MIN (dest_width, dest_height);

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);
g_return_val_if_fail (dest_width > 0, NULL);
g_return_val_if_fail (dest_height > 0, NULL);

if (G_UNLIKELY (g_path_is_absolute (source)))
{
pixbuf = gdk_pixbuf_new_from_file_at_scale (source, size, size, TRUE, &error);
pixbuf = gdk_pixbuf_new_from_file_at_scale (source, dest_width, dest_height, TRUE, &error);
if (G_UNLIKELY (pixbuf == NULL))
{
g_message ("Failed to load image \"%s\": %s",
@@ -226,21 +230,20 @@ xfce_panel_pixbuf_from_source (const gchar *source,
src_w = gdk_pixbuf_get_width (pixbuf);
src_h = gdk_pixbuf_get_height (pixbuf);

if (src_w > size || src_h > size)
if (src_w > dest_width || src_h > dest_height)
{
/* 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);
dest_width = rint (src_w / hratio);
else
dest_h = rint (src_h / wratio);
dest_height = rint (src_h / wratio);

dest = gdk_pixbuf_scale_simple (pixbuf, MAX (dest_w, 1),
MAX (dest_h, 1),
dest = gdk_pixbuf_scale_simple (pixbuf,
MAX (dest_width, 1),
MAX (dest_height, 1),
GDK_INTERP_BILINEAR);

g_object_unref (G_OBJECT (pixbuf));
@@ -253,5 +256,30 @@ xfce_panel_pixbuf_from_source (const gchar *source,



/**
* 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 that should be loaded
*
* See xfce_panel_pixbuf_from_source_at_size
*
* Returns: a GdkPixbuf or %NULL if nothing was found. The value should
* be released with g_object_unref when no longer used.
*
* See also: XfcePanelImage
*
* Since: 4.8
**/
GdkPixbuf *
xfce_panel_pixbuf_from_source (const gchar *source,
GtkIconTheme *icon_theme,
gint size)
{
return xfce_panel_pixbuf_from_source_at_size (source, icon_theme, size, size);
}



#define __XFCE_PANEL_CONVENIENCE_C__
#include <libxfce4panel/libxfce4panel-aliasdef.c>

+ 11
- 6
libxfce4panel/xfce-panel-convenience.h View File

@@ -29,15 +29,20 @@

G_BEGIN_DECLS

GtkWidget *xfce_panel_create_button (void) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
GtkWidget *xfce_panel_create_button (void) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;

GtkWidget *xfce_panel_create_toggle_button (void) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
GtkWidget *xfce_panel_create_toggle_button (void) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;

const gchar *xfce_panel_get_channel_name (void);
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;
GdkPixbuf *xfce_panel_pixbuf_from_source_at_size (const gchar *source,
GtkIconTheme *icon_theme,
gint dest_width,
gint dest_height) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;

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



Loading…
Cancel
Save