GtkCellRendererText: Add [foreground|background]-rgba properties

These properties use GdkRGBA to render the cell renderer's content.
Note that Pango attributes are used to render the foreground color,
so the alpha value is currently ignored.
This commit is contained in:
Carlos Garnacho 2010-10-22 18:33:07 +02:00
parent 5fbf20c2d8
commit d1140d0b1e

View File

@ -93,6 +93,8 @@ enum {
PROP_FOREGROUND, PROP_FOREGROUND,
PROP_BACKGROUND_GDK, PROP_BACKGROUND_GDK,
PROP_FOREGROUND_GDK, PROP_FOREGROUND_GDK,
PROP_BACKGROUND_RGBA,
PROP_FOREGROUND_RGBA,
PROP_FONT, PROP_FONT,
PROP_FONT_DESC, PROP_FONT_DESC,
PROP_FAMILY, PROP_FAMILY,
@ -140,8 +142,8 @@ struct _GtkCellRendererTextPrivate
PangoAlignment align; PangoAlignment align;
PangoAttrList *extra_attrs; PangoAttrList *extra_attrs;
PangoColor foreground; GdkRGBA foreground;
PangoColor background; GdkRGBA background;
PangoEllipsizeMode ellipsize; PangoEllipsizeMode ellipsize;
PangoFontDescription *font; PangoFontDescription *font;
PangoLanguage *language; PangoLanguage *language;
@ -274,6 +276,20 @@ gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
GDK_TYPE_COLOR, GDK_TYPE_COLOR,
GTK_PARAM_READWRITE)); GTK_PARAM_READWRITE));
/**
* GtkCellRendererText:background-rgba:
*
* Background color as a #GdkRGBA
*
* Since: 3.0
*/
g_object_class_install_property (object_class,
PROP_BACKGROUND_RGBA,
g_param_spec_boxed ("background-rgba",
P_("Background color as RGBA"),
P_("Background color as a GdkRGBA"),
GDK_TYPE_RGBA,
GTK_PARAM_READWRITE));
g_object_class_install_property (object_class, g_object_class_install_property (object_class,
PROP_FOREGROUND, PROP_FOREGROUND,
g_param_spec_string ("foreground", g_param_spec_string ("foreground",
@ -290,6 +306,21 @@ gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
GDK_TYPE_COLOR, GDK_TYPE_COLOR,
GTK_PARAM_READWRITE)); GTK_PARAM_READWRITE));
/**
* GtkCellRendererText:foreground-rgba:
*
* Foreground color as a #GdkRGBA
*
* Since: 3.0
*/
g_object_class_install_property (object_class,
PROP_FOREGROUND_RGBA,
g_param_spec_boxed ("foreground-rgba",
P_("Foreground color as RGBA"),
P_("Foreground color as a GdkRGBA"),
GDK_TYPE_RGBA,
GTK_PARAM_READWRITE));
g_object_class_install_property (object_class, g_object_class_install_property (object_class,
PROP_EDITABLE, PROP_EDITABLE,
@ -717,11 +748,11 @@ gtk_cell_renderer_text_get_property (GObject *object,
case PROP_BACKGROUND_GDK: case PROP_BACKGROUND_GDK:
{ {
GdkColor color; GdkColor color;
color.red = priv->background.red; color.red = (guint16) (priv->background.red * 65535);
color.green = priv->background.green; color.green = (guint16) (priv->background.green * 65535);
color.blue = priv->background.blue; color.blue = (guint16) (priv->background.blue * 65535);
g_value_set_boxed (value, &color); g_value_set_boxed (value, &color);
} }
break; break;
@ -729,15 +760,23 @@ gtk_cell_renderer_text_get_property (GObject *object,
case PROP_FOREGROUND_GDK: case PROP_FOREGROUND_GDK:
{ {
GdkColor color; GdkColor color;
color.red = priv->foreground.red; color.red = (guint16) (priv->foreground.red * 65535);
color.green = priv->foreground.green; color.green = (guint16) (priv->foreground.green * 65535);
color.blue = priv->foreground.blue; color.blue = (guint16) (priv->foreground.blue * 65535);
g_value_set_boxed (value, &color); g_value_set_boxed (value, &color);
} }
break; break;
case PROP_BACKGROUND_RGBA:
g_value_set_boxed (value, &priv->background);
break;
case PROP_FOREGROUND_RGBA:
g_value_set_boxed (value, &priv->foreground);
break;
case PROP_FONT: case PROP_FONT:
g_value_take_string (value, pango_font_description_to_string (priv->font)); g_value_take_string (value, pango_font_description_to_string (priv->font));
break; break;
@ -887,21 +926,19 @@ gtk_cell_renderer_text_get_property (GObject *object,
static void static void
set_bg_color (GtkCellRendererText *celltext, set_bg_color (GtkCellRendererText *celltext,
GdkColor *color) GdkRGBA *rgba)
{ {
GtkCellRendererTextPrivate *priv = celltext->priv; GtkCellRendererTextPrivate *priv = celltext->priv;
if (color) if (rgba)
{ {
if (!priv->background_set) if (!priv->background_set)
{ {
priv->background_set = TRUE; priv->background_set = TRUE;
g_object_notify (G_OBJECT (celltext), "background-set"); g_object_notify (G_OBJECT (celltext), "background-set");
} }
priv->background.red = color->red; priv->background = *rgba;
priv->background.green = color->green;
priv->background.blue = color->blue;
} }
else else
{ {
@ -916,21 +953,19 @@ set_bg_color (GtkCellRendererText *celltext,
static void static void
set_fg_color (GtkCellRendererText *celltext, set_fg_color (GtkCellRendererText *celltext,
GdkColor *color) GdkRGBA *rgba)
{ {
GtkCellRendererTextPrivate *priv = celltext->priv; GtkCellRendererTextPrivate *priv = celltext->priv;
if (color) if (rgba)
{ {
if (!priv->foreground_set) if (!priv->foreground_set)
{ {
priv->foreground_set = TRUE; priv->foreground_set = TRUE;
g_object_notify (G_OBJECT (celltext), "foreground-set"); g_object_notify (G_OBJECT (celltext), "foreground-set");
} }
priv->foreground.red = color->red; priv->foreground = *rgba;
priv->foreground.green = color->green;
priv->foreground.blue = color->blue;
} }
else else
{ {
@ -1140,12 +1175,12 @@ gtk_cell_renderer_text_set_property (GObject *object,
case PROP_BACKGROUND: case PROP_BACKGROUND:
{ {
GdkColor color; GdkRGBA rgba;
if (!g_value_get_string (value)) if (!g_value_get_string (value))
set_bg_color (celltext, NULL); /* reset to background_set to FALSE */ set_bg_color (celltext, NULL); /* reset to background_set to FALSE */
else if (gdk_color_parse (g_value_get_string (value), &color)) else if (gdk_rgba_parse (g_value_get_string (value), &rgba))
set_bg_color (celltext, &color); set_bg_color (celltext, &rgba);
else else
g_warning ("Don't know color `%s'", g_value_get_string (value)); g_warning ("Don't know color `%s'", g_value_get_string (value));
@ -1155,12 +1190,12 @@ gtk_cell_renderer_text_set_property (GObject *object,
case PROP_FOREGROUND: case PROP_FOREGROUND:
{ {
GdkColor color; GdkRGBA rgba;
if (!g_value_get_string (value)) if (!g_value_get_string (value))
set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */ set_fg_color (celltext, NULL); /* reset to foreground_set to FALSE */
else if (gdk_color_parse (g_value_get_string (value), &color)) else if (gdk_rgba_parse (g_value_get_string (value), &rgba))
set_fg_color (celltext, &color); set_fg_color (celltext, &rgba);
else else
g_warning ("Don't know color `%s'", g_value_get_string (value)); g_warning ("Don't know color `%s'", g_value_get_string (value));
@ -1169,11 +1204,39 @@ gtk_cell_renderer_text_set_property (GObject *object,
break; break;
case PROP_BACKGROUND_GDK: case PROP_BACKGROUND_GDK:
{
GdkColor *color;
GdkRGBA rgba;
color = g_value_get_boxed (value);
rgba.red = color->red / 65535.;
rgba.green = color->green / 65535.;
rgba.blue = color->blue / 65535.;
set_bg_color (celltext, &rgba);
}
break;
case PROP_FOREGROUND_GDK:
{
GdkColor *color;
GdkRGBA rgba;
color = g_value_get_boxed (value);
rgba.red = color->red / 65535.;
rgba.green = color->green / 65535.;
rgba.blue = color->blue / 65535.;
set_fg_color (celltext, &rgba);
}
break;
case PROP_BACKGROUND_RGBA:
/* This notifies the GObject itself. */ /* This notifies the GObject itself. */
set_bg_color (celltext, g_value_get_boxed (value)); set_bg_color (celltext, g_value_get_boxed (value));
break; break;
case PROP_FOREGROUND_GDK: case PROP_FOREGROUND_RGBA:
/* This notifies the GObject itself. */ /* This notifies the GObject itself. */
set_fg_color (celltext, g_value_get_boxed (value)); set_fg_color (celltext, g_value_get_boxed (value));
break; break;
@ -1465,8 +1528,10 @@ get_layout (GtkCellRendererText *celltext,
{ {
PangoColor color; PangoColor color;
color = priv->foreground; color.red = (guint16) (priv->foreground.red * 65535);
color.green = (guint16) (priv->foreground.green * 65535);
color.blue = (guint16) (priv->foreground.blue * 65535);
add_attr (attr_list, add_attr (attr_list,
pango_attr_foreground_new (color.red, color.green, color.blue)); pango_attr_foreground_new (color.red, color.green, color.blue));
} }
@ -1738,10 +1803,7 @@ gtk_cell_renderer_text_render (GtkCellRenderer *cell,
(flags & GTK_CELL_RENDERER_SELECTED) == 0) (flags & GTK_CELL_RENDERER_SELECTED) == 0)
{ {
gdk_cairo_rectangle (cr, background_area); gdk_cairo_rectangle (cr, background_area);
cairo_set_source_rgb (cr, gdk_cairo_set_source_rgba (cr, &priv->background);
priv->background.red / 65535.,
priv->background.green / 65535.,
priv->background.blue / 65535.);
cairo_fill (cr); cairo_fill (cr);
} }