Support named icons in cells by adding a icon-name property. (#165777,

2005-05-18  Matthias Clasen  <mclasen@redhat.com>

	* gtk/gtkcellrendererpixbuf.c: Support named icons in
	cells by adding a icon-name property. (#165777, Anders
	Carlsson)
This commit is contained in:
Matthias Clasen 2005-05-18 06:13:42 +00:00 committed by Matthias Clasen
parent 67f557226f
commit 9fe4bffd4f
6 changed files with 135 additions and 13 deletions

View File

@ -1,10 +1,15 @@
2005-05-18 Matthias Clasen <mclasen@redhat.com> 2005-05-18 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcellrendererpixbuf.c: Support named icons in
cells by adding a icon-name property. (#165777, Anders
Carlsson)
* gtk/gtk.symbols: * gtk/gtk.symbols:
* gtk/gtktoolbutton.h: * gtk/gtktoolbutton.h:
* gtk/gtktoolbutton.c (gtk_tool_button_set_icon_name) * gtk/gtktoolbutton.c (gtk_tool_button_set_icon_name)
(gtk_tool_button_get_icon_name): Support named icons in (gtk_tool_button_get_icon_name): Support named icons in
tool buttons by adding a icon-name property. tool buttons by adding a icon-name property. (#116577,
Christian Neumair)
* gtk/gtk.symbols: * gtk/gtk.symbols:
* gtk/gtkdnd.h: * gtk/gtkdnd.h:

View File

@ -1,10 +1,15 @@
2005-05-18 Matthias Clasen <mclasen@redhat.com> 2005-05-18 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcellrendererpixbuf.c: Support named icons in
cells by adding a icon-name property. (#165777, Anders
Carlsson)
* gtk/gtk.symbols: * gtk/gtk.symbols:
* gtk/gtktoolbutton.h: * gtk/gtktoolbutton.h:
* gtk/gtktoolbutton.c (gtk_tool_button_set_icon_name) * gtk/gtktoolbutton.c (gtk_tool_button_set_icon_name)
(gtk_tool_button_get_icon_name): Support named icons in (gtk_tool_button_get_icon_name): Support named icons in
tool buttons by adding a icon-name property. tool buttons by adding a icon-name property. (#116577,
Christian Neumair)
* gtk/gtk.symbols: * gtk/gtk.symbols:
* gtk/gtkdnd.h: * gtk/gtkdnd.h:

View File

@ -1,10 +1,15 @@
2005-05-18 Matthias Clasen <mclasen@redhat.com> 2005-05-18 Matthias Clasen <mclasen@redhat.com>
* gtk/gtkcellrendererpixbuf.c: Support named icons in
cells by adding a icon-name property. (#165777, Anders
Carlsson)
* gtk/gtk.symbols: * gtk/gtk.symbols:
* gtk/gtktoolbutton.h: * gtk/gtktoolbutton.h:
* gtk/gtktoolbutton.c (gtk_tool_button_set_icon_name) * gtk/gtktoolbutton.c (gtk_tool_button_set_icon_name)
(gtk_tool_button_get_icon_name): Support named icons in (gtk_tool_button_get_icon_name): Support named icons in
tool buttons by adding a icon-name property. tool buttons by adding a icon-name property. (#116577,
Christian Neumair)
* gtk/gtk.symbols: * gtk/gtk.symbols:
* gtk/gtkdnd.h: * gtk/gtkdnd.h:

View File

@ -1,5 +1,8 @@
2005-05-18 Matthias Clasen <mclasen@redhat.com> 2005-05-18 Matthias Clasen <mclasen@redhat.com>
* gtk/migrating-checklist.sgml: Add a section about named
icons.
* gtk/gtk-sections.txt: Add new functions. * gtk/gtk-sections.txt: Add new functions.
2005-05-16 Matthias Clasen <mclasen@redhat.com> 2005-05-16 Matthias Clasen <mclasen@redhat.com>

View File

@ -282,6 +282,26 @@ my_widget_key_press_event_handler (GtkWidget *widget, GdkEventKey *event)
} }
</programlisting> </programlisting>
</section> </section>
<section id="checklist-named-icons">
<title>Use named icons</title>
<formalpara>
<title>Why</title>
<para>
Named icons automatically adapt to theme changes, giving your
application a much more integrated appearance.
</para>
</formalpara>
<para>
Since GTK+ 2.6, named icons can be used for window icons (see
gtk_window_set_icon_name()) and images (see gtk_image_set_icon_name()).
In GTK+ 2.8, you can also use named icons for drag-and-drop (see
gtk_drag_source_set_icon_name()) and in treeview cells (see the
<link linkend="GtkCellRendererPixbuf--icon-name">icon_name</link>
property of #GtkCellRendererPixbuf).
</para>
</chapter> </chapter>
<!-- <!--

View File

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "gtkcellrendererpixbuf.h" #include "gtkcellrendererpixbuf.h"
#include "gtkiconfactory.h" #include "gtkiconfactory.h"
#include "gtkicontheme.h"
#include "gtkintl.h" #include "gtkintl.h"
#include "gtkprivate.h" #include "gtkprivate.h"
#include "gtkalias.h" #include "gtkalias.h"
@ -38,6 +39,8 @@ static void gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *cla
static void gtk_cell_renderer_pixbuf_finalize (GObject *object); static void gtk_cell_renderer_pixbuf_finalize (GObject *object);
static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf, static void gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
GtkWidget *widget); GtkWidget *widget);
static void gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
GtkWidget *widget);
static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell, static void gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
GtkWidget *widget, GtkWidget *widget,
GdkRectangle *rectangle, GdkRectangle *rectangle,
@ -62,7 +65,8 @@ enum {
PROP_STOCK_ID, PROP_STOCK_ID,
PROP_STOCK_SIZE, PROP_STOCK_SIZE,
PROP_STOCK_DETAIL, PROP_STOCK_DETAIL,
PROP_FOLLOW_STATE PROP_FOLLOW_STATE,
PROP_ICON_NAME
}; };
static gpointer parent_class; static gpointer parent_class;
@ -77,6 +81,8 @@ struct _GtkCellRendererPixbufPrivate
GtkIconSize stock_size; GtkIconSize stock_size;
gchar *stock_detail; gchar *stock_detail;
gboolean follow_state; gboolean follow_state;
gchar *icon_name;
}; };
@ -183,6 +189,24 @@ gtk_cell_renderer_pixbuf_class_init (GtkCellRendererPixbufClass *class)
NULL, NULL,
GTK_PARAM_READWRITE)); GTK_PARAM_READWRITE));
/**
* GtkCellRendererPixbuf:icon-name:
*
* The name of the themed icon to display.
* This property only has an effect if not overridden by "stock_id"
* or "pixbuf" properties.
*
* Since: 2.8
*/
g_object_class_install_property (object_class,
PROP_ICON_NAME,
g_param_spec_string ("icon-name",
P_("Icon Name"),
P_("The name of the icon from the icon theme"),
NULL,
GTK_PARAM_READWRITE));
/** /**
* GtkCellRendererPixbuf:follow-state: * GtkCellRendererPixbuf:follow-state:
* *
@ -211,15 +235,13 @@ gtk_cell_renderer_pixbuf_finalize (GObject *object)
GtkCellRendererPixbufPrivate *priv; GtkCellRendererPixbufPrivate *priv;
priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object); priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (object);
if (cellpixbuf->pixbuf) if (cellpixbuf->pixbuf)
g_object_unref (cellpixbuf->pixbuf); g_object_unref (cellpixbuf->pixbuf);
if (priv->stock_id) g_free (priv->stock_id);
g_free (priv->stock_id); g_free (priv->stock_detail);
g_free (priv->icon_name);
if (priv->stock_detail)
g_free (priv->stock_detail);
(* G_OBJECT_CLASS (parent_class)->finalize) (object); (* G_OBJECT_CLASS (parent_class)->finalize) (object);
} }
@ -261,6 +283,9 @@ gtk_cell_renderer_pixbuf_get_property (GObject *object,
case PROP_FOLLOW_STATE: case PROP_FOLLOW_STATE:
g_value_set_boolean (value, priv->follow_state); g_value_set_boolean (value, priv->follow_state);
break; break;
case PROP_ICON_NAME:
g_value_set_string (value, priv->icon_name);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break; break;
@ -315,6 +340,8 @@ gtk_cell_renderer_pixbuf_set_property (GObject *object,
cellpixbuf->pixbuf = NULL; cellpixbuf->pixbuf = NULL;
} }
g_free (priv->stock_id); g_free (priv->stock_id);
g_free (priv->icon_name);
priv->icon_name = NULL;
} }
priv->stock_id = g_strdup (g_value_get_string (value)); priv->stock_id = g_strdup (g_value_get_string (value));
break; break;
@ -326,6 +353,19 @@ gtk_cell_renderer_pixbuf_set_property (GObject *object,
g_free (priv->stock_detail); g_free (priv->stock_detail);
priv->stock_detail = g_strdup (g_value_get_string (value)); priv->stock_detail = g_strdup (g_value_get_string (value));
break; break;
case PROP_ICON_NAME:
if (priv->icon_name)
{
if (cellpixbuf->pixbuf)
{
g_object_unref (cellpixbuf->pixbuf);
cellpixbuf->pixbuf = NULL;
}
g_free (priv->stock_id);
g_free (priv->icon_name);
}
priv->icon_name = g_strdup (g_value_get_string (value));
break;
case PROP_FOLLOW_STATE: case PROP_FOLLOW_STATE:
priv->follow_state = g_value_get_boolean (value); priv->follow_state = g_value_get_boolean (value);
break; break;
@ -377,6 +417,45 @@ gtk_cell_renderer_pixbuf_create_stock_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
priv->stock_detail); priv->stock_detail);
} }
static void
gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (GtkCellRendererPixbuf *cellpixbuf,
GtkWidget *widget)
{
GtkCellRendererPixbufPrivate *priv;
GdkScreen *screen;
GtkIconTheme *icon_theme;
GtkSettings *settings;
gint width, height;
GError *error = NULL;
priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cellpixbuf);
if (cellpixbuf->pixbuf)
g_object_unref (cellpixbuf->pixbuf);
screen = gtk_widget_get_screen (GTK_WIDGET (widget));
icon_theme = gtk_icon_theme_get_for_screen (screen);
settings = gtk_settings_get_for_screen (screen);
if (!gtk_icon_size_lookup_for_settings (settings,
priv->stock_size,
&width, &height))
{
g_warning ("Invalid icon size %d\n", priv->stock_size);
width = height = 24;
}
cellpixbuf->pixbuf =
gtk_icon_theme_load_icon (icon_theme,
priv->icon_name,
MIN (width, height), 0, &error);
if (!cellpixbuf->pixbuf)
{
g_warning ("could not load image: %s\n", error->message);
g_error_free (error);
}
}
static GdkPixbuf * static GdkPixbuf *
create_colorized_pixbuf (GdkPixbuf *src, create_colorized_pixbuf (GdkPixbuf *src,
GdkColor *new_color) GdkColor *new_color)
@ -442,9 +521,14 @@ gtk_cell_renderer_pixbuf_get_size (GtkCellRenderer *cell,
priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell); priv = GTK_CELL_RENDERER_PIXBUF_GET_PRIVATE (cell);
if (!cellpixbuf->pixbuf && priv->stock_id) if (!cellpixbuf->pixbuf)
gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget); {
if (priv->stock_id)
gtk_cell_renderer_pixbuf_create_stock_pixbuf (cellpixbuf, widget);
else if (priv->icon_name)
gtk_cell_renderer_pixbuf_create_named_icon_pixbuf (cellpixbuf, widget);
}
if (cellpixbuf->pixbuf) if (cellpixbuf->pixbuf)
{ {
pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf); pixbuf_width = gdk_pixbuf_get_width (cellpixbuf->pixbuf);