gradient: Allow resolving with contexts

This commit is contained in:
Benjamin Otte 2012-01-02 15:44:27 +01:00
parent 4576653006
commit 29382c1305
2 changed files with 44 additions and 0 deletions

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "gtkgradient.h"
#include "gtkstylecontextprivate.h"
#include "gtkstyleproperties.h"
#include "gtkintl.h"
@ -281,6 +282,44 @@ gtk_gradient_resolve (GtkGradient *gradient,
return TRUE;
}
cairo_pattern_t *
gtk_gradient_resolve_for_context (GtkGradient *gradient,
GtkStyleContext *context)
{
cairo_pattern_t *pattern;
guint i;
g_return_val_if_fail (gradient != NULL, FALSE);
g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
if (gradient->radius0 == 0 && gradient->radius1 == 0)
pattern = cairo_pattern_create_linear (gradient->x0, gradient->y0,
gradient->x1, gradient->y1);
else
pattern = cairo_pattern_create_radial (gradient->x0, gradient->y0,
gradient->radius0,
gradient->x1, gradient->y1,
gradient->radius1);
for (i = 0; i < gradient->stops->len; i++)
{
ColorStop *stop;
GdkRGBA rgba;
stop = &g_array_index (gradient->stops, ColorStop, i);
/* if color resolving fails, assume transparency */
if (!_gtk_style_context_resolve_color (context, stop->color, &rgba))
rgba.red = rgba.green = rgba.blue = rgba.alpha = 0.0;
cairo_pattern_add_color_stop_rgba (pattern, stop->offset,
rgba.red, rgba.green,
rgba.blue, rgba.alpha);
}
return pattern;
}
static void
append_number (GString *str,
double d,

View File

@ -25,6 +25,7 @@
#define __GTK_GRADIENT_H__
#include <gdk/gdk.h>
#include <gtk/gtkstylecontext.h>
#include <gtk/gtkstyleproperties.h>
#include <gtk/gtksymboliccolor.h>
@ -55,6 +56,10 @@ void gtk_gradient_unref (GtkGradient *gradient);
gboolean gtk_gradient_resolve (GtkGradient *gradient,
GtkStyleProperties *props,
cairo_pattern_t **resolved_gradient);
cairo_pattern_t *
gtk_gradient_resolve_for_context
(GtkGradient *gradient,
GtkStyleContext *context);
char * gtk_gradient_to_string (GtkGradient *gradient);