1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkgradient.h"
22 #include "gtkstylecontextprivate.h"
23 #include "gtkstyleproperties.h"
28 * @Short_description: Gradients
31 * GtkGradient is a boxed type that represents a gradient.
32 * It is the result of parsing a
33 * <link linkend="gtkcssprovider-gradients">gradient expression</link>.
34 * To obtain the gradient represented by a GtkGradient, it has to
35 * be resolved with gtk_gradient_resolve(), which replaces all
36 * symbolic color references by the colors they refer to (in a given
37 * context) and constructs a #cairo_pattern_t value.
39 * It is not normally necessary to deal directly with #GtkGradients,
40 * since they are mostly used behind the scenes by #GtkStyleContext and
44 G_DEFINE_BOXED_TYPE (GtkGradient, gtk_gradient,
45 gtk_gradient_ref, gtk_gradient_unref)
47 typedef struct ColorStop ColorStop;
52 GtkSymbolicColor *color;
70 * gtk_gradient_new_linear:
71 * @x0: X coordinate of the starting point
72 * @y0: Y coordinate of the starting point
73 * @x1: X coordinate of the end point
74 * @y1: Y coordinate of the end point
76 * Creates a new linear gradient along the line defined by (x0, y0) and (x1, y1). Before using the gradient
77 * a number of stop colors must be added through gtk_gradient_add_color_stop().
79 * Returns: A newly created #GtkGradient
84 gtk_gradient_new_linear (gdouble x0,
89 GtkGradient *gradient;
91 gradient = g_slice_new (GtkGradient);
92 gradient->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
98 gradient->radius0 = 0;
99 gradient->radius1 = 0;
101 gradient->ref_count = 1;
107 * gtk_gradient_new_radial:
108 * @x0: X coordinate of the start circle
109 * @y0: Y coordinate of the start circle
110 * @radius0: radius of the start circle
111 * @x1: X coordinate of the end circle
112 * @y1: Y coordinate of the end circle
113 * @radius1: radius of the end circle
115 * Creates a new radial gradient along the two circles defined by (x0, y0, radius0) and
116 * (x1, y1, radius1). Before using the gradient a number of stop colors must be added
117 * through gtk_gradient_add_color_stop().
119 * Returns: A newly created #GtkGradient
124 gtk_gradient_new_radial (gdouble x0,
131 GtkGradient *gradient;
133 gradient = g_slice_new (GtkGradient);
134 gradient->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
140 gradient->radius0 = radius0;
141 gradient->radius1 = radius1;
143 gradient->ref_count = 1;
149 * gtk_gradient_add_color_stop:
150 * @gradient: a #GtkGradient
151 * @offset: offset for the color stop
152 * @color: color to use
154 * Adds a stop color to @gradient.
159 gtk_gradient_add_color_stop (GtkGradient *gradient,
161 GtkSymbolicColor *color)
165 g_return_if_fail (gradient != NULL);
167 stop.offset = offset;
168 stop.color = gtk_symbolic_color_ref (color);
170 g_array_append_val (gradient->stops, stop);
175 * @gradient: a #GtkGradient
177 * Increases the reference count of @gradient.
179 * Returns: The same @gradient
184 gtk_gradient_ref (GtkGradient *gradient)
186 g_return_val_if_fail (gradient != NULL, NULL);
188 gradient->ref_count++;
194 * gtk_gradient_unref:
195 * @gradient: a #GtkGradient
197 * Decreases the reference count of @gradient, freeing its memory
198 * if the reference count reaches 0.
203 gtk_gradient_unref (GtkGradient *gradient)
205 g_return_if_fail (gradient != NULL);
207 gradient->ref_count--;
209 if (gradient->ref_count == 0)
213 for (i = 0; i < gradient->stops->len; i++)
217 stop = &g_array_index (gradient->stops, ColorStop, i);
218 gtk_symbolic_color_unref (stop->color);
221 g_array_free (gradient->stops, TRUE);
222 g_slice_free (GtkGradient, gradient);
227 * gtk_gradient_resolve:
228 * @gradient: a #GtkGradient
229 * @props: #GtkStyleProperties to use when resolving named colors
230 * @resolved_gradient: (out): return location for the resolved pattern
232 * If @gradient is resolvable, @resolved_gradient will be filled in
233 * with the resolved gradient as a cairo_pattern_t, and %TRUE will
234 * be returned. Generally, if @gradient can't be resolved, it is
235 * due to it being defined on top of a named color that doesn't
238 * Returns: %TRUE if the gradient has been resolved
243 gtk_gradient_resolve (GtkGradient *gradient,
244 GtkStyleProperties *props,
245 cairo_pattern_t **resolved_gradient)
247 cairo_pattern_t *pattern;
250 g_return_val_if_fail (gradient != NULL, FALSE);
251 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
252 g_return_val_if_fail (resolved_gradient != NULL, FALSE);
254 if (gradient->radius0 == 0 && gradient->radius1 == 0)
255 pattern = cairo_pattern_create_linear (gradient->x0, gradient->y0,
256 gradient->x1, gradient->y1);
258 pattern = cairo_pattern_create_radial (gradient->x0, gradient->y0,
260 gradient->x1, gradient->y1,
263 for (i = 0; i < gradient->stops->len; i++)
268 stop = &g_array_index (gradient->stops, ColorStop, i);
270 if (!gtk_symbolic_color_resolve (stop->color, props, &color))
272 cairo_pattern_destroy (pattern);
276 cairo_pattern_add_color_stop_rgba (pattern, stop->offset,
277 color.red, color.green,
278 color.blue, color.alpha);
281 *resolved_gradient = pattern;
286 gtk_gradient_resolve_for_context (GtkGradient *gradient,
287 GtkStyleContext *context)
289 cairo_pattern_t *pattern;
292 g_return_val_if_fail (gradient != NULL, FALSE);
293 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
295 if (gradient->radius0 == 0 && gradient->radius1 == 0)
296 pattern = cairo_pattern_create_linear (gradient->x0, gradient->y0,
297 gradient->x1, gradient->y1);
299 pattern = cairo_pattern_create_radial (gradient->x0, gradient->y0,
301 gradient->x1, gradient->y1,
304 for (i = 0; i < gradient->stops->len; i++)
309 stop = &g_array_index (gradient->stops, ColorStop, i);
311 /* if color resolving fails, assume transparency */
312 if (!_gtk_style_context_resolve_color (context, stop->color, &rgba))
313 rgba.red = rgba.green = rgba.blue = rgba.alpha = 0.0;
315 cairo_pattern_add_color_stop_rgba (pattern, stop->offset,
316 rgba.red, rgba.green,
317 rgba.blue, rgba.alpha);
324 append_number (GString *str,
330 if (zero && d == 0.0)
331 g_string_append (str, zero);
332 else if (half && d == 0.5)
333 g_string_append (str, half);
334 else if (one && d == 1.0)
335 g_string_append (str, one);
338 char buf[G_ASCII_DTOSTR_BUF_SIZE];
340 g_ascii_dtostr (buf, sizeof (buf), d);
341 g_string_append (str, buf);
346 * gtk_gradient_to_string:
347 * @gradient: the gradient to print
349 * Creates a string representation for @gradient that is suitable
350 * for using in GTK CSS files.
352 * Returns: A string representation for @gradient
355 gtk_gradient_to_string (GtkGradient *gradient)
360 g_return_val_if_fail (gradient != NULL, NULL);
362 str = g_string_new ("-gtk-gradient (");
364 if (gradient->radius0 == 0 && gradient->radius1 == 0)
366 g_string_append (str, "linear, ");
367 append_number (str, gradient->x0, "left", "center", "right");
368 g_string_append_c (str, ' ');
369 append_number (str, gradient->y0, "top", "center", "bottom");
370 g_string_append (str, ", ");
371 append_number (str, gradient->x1, "left", "center", "right");
372 g_string_append_c (str, ' ');
373 append_number (str, gradient->y1, "top", "center", "bottom");
377 g_string_append (str, "radial, ");
378 append_number (str, gradient->x0, "left", "center", "right");
379 g_string_append_c (str, ' ');
380 append_number (str, gradient->y0, "top", "center", "bottom");
381 g_string_append (str, ", ");
382 append_number (str, gradient->radius0, NULL, NULL, NULL);
383 g_string_append (str, ", ");
384 append_number (str, gradient->x1, "left", "center", "right");
385 g_string_append_c (str, ' ');
386 append_number (str, gradient->y1, "top", "center", "bottom");
387 g_string_append (str, ", ");
388 append_number (str, gradient->radius1, NULL, NULL, NULL);
391 for (i = 0; i < gradient->stops->len; i++)
396 stop = &g_array_index (gradient->stops, ColorStop, i);
398 g_string_append (str, ", ");
400 if (stop->offset == 0.0)
401 g_string_append (str, "from (");
402 else if (stop->offset == 1.0)
403 g_string_append (str, "to (");
406 g_string_append (str, "color-stop (");
407 append_number (str, stop->offset, NULL, NULL, NULL);
408 g_string_append (str, ", ");
411 s = gtk_symbolic_color_to_string (stop->color);
412 g_string_append (str, s);
415 g_string_append (str, ")");
418 g_string_append (str, ")");
420 return g_string_free (str, FALSE);