diff options
author | Ted Gould <ted@gould.cx> | 2010-03-10 22:06:56 -0600 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2010-03-10 22:06:56 -0600 |
commit | fc87fda00e8e42d5a212a199a6f9e8d350388905 (patch) | |
tree | 8787d836c971b9a08afe3976256da89b7b45a783 | |
parent | 8e12685bab175ee6d494d7545c5c4187385e41eb (diff) | |
parent | 5867fa760a390d48f30e8794f6292601c61cefad (diff) | |
download | libayatana-indicator-fc87fda00e8e42d5a212a199a6f9e8d350388905.tar.gz libayatana-indicator-fc87fda00e8e42d5a212a199a6f9e8d350388905.tar.bz2 libayatana-indicator-fc87fda00e8e42d5a212a199a6f9e8d350388905.zip |
Add a small helper for loading images.
-rw-r--r-- | .bzrignore | 1 | ||||
-rwxr-xr-x | helper/test-loader-build | 3 | ||||
-rw-r--r-- | helper/test-loader.c | 18 | ||||
-rw-r--r-- | libindicator/Makefile.am | 2 | ||||
-rw-r--r-- | libindicator/indicator-image-helper.c | 143 | ||||
-rw-r--r-- | libindicator/indicator-image-helper.h | 31 |
6 files changed, 198 insertions, 0 deletions
@@ -151,3 +151,4 @@ libindicator/libindicator_la-indicator-desktop-shortcuts.lo tests/test-desktop-shortcuts tests/test-desktop-shortcuts-tester tests/test-desktop-shortcuts-touch-test +libindicator/libindicator_la-indicator-image-helper.lo diff --git a/helper/test-loader-build b/helper/test-loader-build new file mode 100755 index 0000000..fa1e006 --- /dev/null +++ b/helper/test-loader-build @@ -0,0 +1,3 @@ +#!/bin/bash + +gcc `pkg-config --cflags --libs gtk+-2.0` -I../libindicator/ test-loader.c ../libindicator/indicator-image-helper.c -o test-loader diff --git a/helper/test-loader.c b/helper/test-loader.c new file mode 100644 index 0000000..6a06248 --- /dev/null +++ b/helper/test-loader.c @@ -0,0 +1,18 @@ + +#include <gtk/gtk.h> +#include "indicator-image-helper.h" + +int +main (int argv, char * argc[]) +{ + gtk_init(&argv, &argc); + + GtkImage * image = indicator_image_helper(argc[1]); + + GdkPixbuf * pixbuf = gtk_image_get_pixbuf(image); + + g_debug("Pixbuf width: %d", gdk_pixbuf_get_width(pixbuf)); + g_debug("Pixbuf height: %d", gdk_pixbuf_get_height(pixbuf)); + + return; +} diff --git a/libindicator/Makefile.am b/libindicator/Makefile.am index 467e09a..890160c 100644 --- a/libindicator/Makefile.am +++ b/libindicator/Makefile.am @@ -11,6 +11,7 @@ libindicatorincludedir=$(includedir)/libindicator-0.3/libindicator indicator_headers = \ indicator.h \ indicator-desktop-shortcuts.h \ + indicator-image-helper.h \ indicator-object.h \ indicator-service.h \ indicator-service-manager.h @@ -26,6 +27,7 @@ libindicator_la_SOURCES = \ dbus-shared.h \ indicator-object.c \ indicator-desktop-shortcuts.c \ + indicator-image-helper.c \ indicator-object-marshal.h \ indicator-object-marshal.c \ indicator-service.c \ diff --git a/libindicator/indicator-image-helper.c b/libindicator/indicator-image-helper.c new file mode 100644 index 0000000..cbbad3d --- /dev/null +++ b/libindicator/indicator-image-helper.c @@ -0,0 +1,143 @@ +/* +A little helper to make a themed image with fallbacks that +is only constrained in the vertical dimention. + +Copyright 2010 Canonical Ltd. + +Authors: + Ted Gould <ted@canonical.com> + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +version 3.0 as published by the Free Software Foundation. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License version 3.0 for more details. + +You should have received a copy of the GNU General Public +License along with this library. If not, see +<http://www.gnu.org/licenses/>. +*/ + +#include <math.h> +#include "indicator-image-helper.h" + +const gchar * INDICATOR_NAMES_DATA = "indicator-names-data"; + +static void +refresh_image (GtkImage * image) +{ + g_return_if_fail(GTK_IS_IMAGE(image)); + + GIcon * icon_names = (GIcon *)g_object_get_data(G_OBJECT(image), INDICATOR_NAMES_DATA); + g_return_if_fail(icon_names != NULL); + + /* Get the default theme */ + GtkIconTheme * default_theme = gtk_icon_theme_get_default(); + g_return_if_fail(default_theme != NULL); + + gint icon_size = 22; + + GtkStyle * style = gtk_widget_get_style(GTK_WIDGET(image)); + GValue styleprop = {0}; + gtk_style_get_style_property(style, GTK_TYPE_IMAGE, "x-ayatana-indicator-dynamic", &styleprop); + + if (G_VALUE_HOLDS_BOOLEAN(&styleprop) && g_value_get_boolean(&styleprop)) { + PangoContext * context = gtk_widget_get_pango_context(GTK_WIDGET(image)); + PangoFontMetrics * metrics = pango_context_get_metrics(context, style->font_desc, pango_context_get_language(context)); + icon_size = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics)) + PANGO_PIXELS(pango_font_metrics_get_descent(metrics)); + g_debug("Looking for icon size %d", icon_size); + pango_font_metrics_unref(metrics); + } + + /* Look through the themes for that icon */ + GtkIconInfo * icon_info = gtk_icon_theme_lookup_by_gicon(default_theme, icon_names, icon_size, 0); + if (icon_info == NULL) { + g_warning("Unable to find icon in theme."); + return; + } + + /* Grab the filename */ + const gchar * icon_filename = gtk_icon_info_get_filename(icon_info); + g_return_if_fail(icon_filename != NULL); /* An error because we shouldn't get info without a filename */ + + /* Build a pixbuf */ + GError * error = NULL; + GdkPixbuf * pixbuf = gdk_pixbuf_new_from_file(icon_filename, &error); + gtk_icon_info_free(icon_info); + + if (pixbuf == NULL) { + g_error("Unable to load icon from file '%s' because: %s", icon_filename, error == NULL ? "I don't know" : error->message); + return; + } + + /* Scale icon if all we get is something too big. */ + if (gdk_pixbuf_get_height(pixbuf) > icon_size) { + gfloat scale = (gfloat)icon_size / (gfloat)gdk_pixbuf_get_height(pixbuf); + gint width = round(gdk_pixbuf_get_width(pixbuf) * scale); + + GdkPixbuf * scaled = gdk_pixbuf_scale_simple(pixbuf, width, icon_size, GDK_INTERP_BILINEAR); + g_object_unref(G_OBJECT(pixbuf)); + pixbuf = scaled; + } + + /* Put the pixbuf on the image */ + gtk_image_set_from_pixbuf(image, pixbuf); + g_object_unref(G_OBJECT(pixbuf)); + + return; +} + +/* Handles the theme changed signal to refresh the icon to make + sure that it changes appropriately */ +static void +theme_changed_cb (GtkIconTheme * theme, gpointer user_data) +{ + GtkImage * image = GTK_IMAGE(user_data); + refresh_image(image); + return; +} + +/* Removes the signal on the theme that was calling update on this + image. */ +static void +image_destroyed_cb (GtkImage * image, gpointer user_data) +{ + g_signal_handlers_disconnect_by_func(gtk_icon_theme_get_default(), theme_changed_cb, image); + return; +} + +GtkImage * +indicator_image_helper (const gchar * name) +{ + g_return_val_if_fail(name != NULL, NULL); + g_return_val_if_fail(name[0] != '\0', NULL); + + /* Build us a GIcon */ + GIcon * icon_names = g_themed_icon_new_with_default_fallbacks(name); + g_return_val_if_fail(icon_names != NULL, NULL); + + /* Build us an image */ + GtkImage * image = GTK_IMAGE(gtk_image_new()); + + if (image == NULL) { + g_error("Unable to create image from pixbuf on icon name '%s'", name); + g_object_unref(icon_names); + return NULL; + } + + /* Attach our names to the image */ + g_object_set_data_full(G_OBJECT(image), INDICATOR_NAMES_DATA, icon_names, g_object_unref); + + /* Put the pixbuf in */ + refresh_image(image); + + /* Connect to all changes */ + g_signal_connect(G_OBJECT(gtk_icon_theme_get_default()), "changed", G_CALLBACK(theme_changed_cb), image); + g_signal_connect(G_OBJECT(image), "destroy", G_CALLBACK(image_destroyed_cb), NULL); + + /* Return our built image */ + return image; +} diff --git a/libindicator/indicator-image-helper.h b/libindicator/indicator-image-helper.h new file mode 100644 index 0000000..fed02dc --- /dev/null +++ b/libindicator/indicator-image-helper.h @@ -0,0 +1,31 @@ +/* +A little helper to make a themed image with fallbacks that +is only constrained in the vertical dimention. + +Copyright 2010 Canonical Ltd. + +Authors: + Ted Gould <ted@canonical.com> + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +version 3.0 as published by the Free Software Foundation. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License version 3.0 for more details. + +You should have received a copy of the GNU General Public +License along with this library. If not, see +<http://www.gnu.org/licenses/>. +*/ + +#ifndef __INDICATOR_IMAGE_HELPER_H__ +#define __INDICATOR_IMAGE_HELPER_H__ + +#include <gtk/gtk.h> + +GtkImage * indicator_image_helper (const gchar * name); + +#endif /* __INDICATOR_IMAGE_HELPER_H__ */ |