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, see <http://www.gnu.org/licenses/>.
19 #include "gtkgradient.h"
20 #include "gtkstylecontextprivate.h"
21 #include "gtkstyleproperties.h"
26 * @Short_description: Gradients
29 * GtkGradient is a boxed type that represents a gradient.
30 * It is the result of parsing a
31 * <link linkend="gtkcssprovider-gradients">gradient expression</link>.
32 * To obtain the gradient represented by a GtkGradient, it has to
33 * be resolved with gtk_gradient_resolve(), which replaces all
34 * symbolic color references by the colors they refer to (in a given
35 * context) and constructs a #cairo_pattern_t value.
37 * It is not normally necessary to deal directly with #GtkGradients,
38 * since they are mostly used behind the scenes by #GtkStyleContext and
42 G_DEFINE_BOXED_TYPE (GtkGradient, gtk_gradient,
43 gtk_gradient_ref, gtk_gradient_unref)
45 typedef struct ColorStop ColorStop;
50 GtkSymbolicColor *color;
68 * gtk_gradient_new_linear:
69 * @x0: X coordinate of the starting point
70 * @y0: Y coordinate of the starting point
71 * @x1: X coordinate of the end point
72 * @y1: Y coordinate of the end point
74 * Creates a new linear gradient along the line defined by (x0, y0) and (x1, y1). Before using the gradient
75 * a number of stop colors must be added through gtk_gradient_add_color_stop().
77 * Returns: A newly created #GtkGradient
82 gtk_gradient_new_linear (gdouble x0,
87 GtkGradient *gradient;
89 gradient = g_slice_new (GtkGradient);
90 gradient->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
96 gradient->radius0 = 0;
97 gradient->radius1 = 0;
99 gradient->ref_count = 1;
105 * gtk_gradient_new_radial:
106 * @x0: X coordinate of the start circle
107 * @y0: Y coordinate of the start circle
108 * @radius0: radius of the start circle
109 * @x1: X coordinate of the end circle
110 * @y1: Y coordinate of the end circle
111 * @radius1: radius of the end circle
113 * Creates a new radial gradient along the two circles defined by (x0, y0, radius0) and
114 * (x1, y1, radius1). Before using the gradient a number of stop colors must be added
115 * through gtk_gradient_add_color_stop().
117 * Returns: A newly created #GtkGradient
122 gtk_gradient_new_radial (gdouble x0,
129 GtkGradient *gradient;
131 gradient = g_slice_new (GtkGradient);
132 gradient->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
138 gradient->radius0 = radius0;
139 gradient->radius1 = radius1;
141 gradient->ref_count = 1;
147 * gtk_gradient_add_color_stop:
148 * @gradient: a #GtkGradient
149 * @offset: offset for the color stop
150 * @color: color to use
152 * Adds a stop color to @gradient.
157 gtk_gradient_add_color_stop (GtkGradient *gradient,
159 GtkSymbolicColor *color)
163 g_return_if_fail (gradient != NULL);
165 stop.offset = offset;
166 stop.color = gtk_symbolic_color_ref (color);
168 g_array_append_val (gradient->stops, stop);
173 * @gradient: a #GtkGradient
175 * Increases the reference count of @gradient.
177 * Returns: The same @gradient
182 gtk_gradient_ref (GtkGradient *gradient)
184 g_return_val_if_fail (gradient != NULL, NULL);
186 gradient->ref_count++;
192 * gtk_gradient_unref:
193 * @gradient: a #GtkGradient
195 * Decreases the reference count of @gradient, freeing its memory
196 * if the reference count reaches 0.
201 gtk_gradient_unref (GtkGradient *gradient)
203 g_return_if_fail (gradient != NULL);
205 gradient->ref_count--;
207 if (gradient->ref_count == 0)
211 for (i = 0; i < gradient->stops->len; i++)
215 stop = &g_array_index (gradient->stops, ColorStop, i);
216 gtk_symbolic_color_unref (stop->color);
219 g_array_free (gradient->stops, TRUE);
220 g_slice_free (GtkGradient, gradient);
225 * gtk_gradient_resolve:
226 * @gradient: a #GtkGradient
227 * @props: #GtkStyleProperties to use when resolving named colors
228 * @resolved_gradient: (out): return location for the resolved pattern
230 * If @gradient is resolvable, @resolved_gradient will be filled in
231 * with the resolved gradient as a cairo_pattern_t, and %TRUE will
232 * be returned. Generally, if @gradient can't be resolved, it is
233 * due to it being defined on top of a named color that doesn't
236 * Returns: %TRUE if the gradient has been resolved
241 gtk_gradient_resolve (GtkGradient *gradient,
242 GtkStyleProperties *props,
243 cairo_pattern_t **resolved_gradient)
245 cairo_pattern_t *pattern;
248 g_return_val_if_fail (gradient != NULL, FALSE);
249 g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
250 g_return_val_if_fail (resolved_gradient != NULL, FALSE);
252 if (gradient->radius0 == 0 && gradient->radius1 == 0)
253 pattern = cairo_pattern_create_linear (gradient->x0, gradient->y0,
254 gradient->x1, gradient->y1);
256 pattern = cairo_pattern_create_radial (gradient->x0, gradient->y0,
258 gradient->x1, gradient->y1,
261 for (i = 0; i < gradient->stops->len; i++)
266 stop = &g_array_index (gradient->stops, ColorStop, i);
268 if (!gtk_symbolic_color_resolve (stop->color, props, &color))
270 cairo_pattern_destroy (pattern);
274 cairo_pattern_add_color_stop_rgba (pattern, stop->offset,
275 color.red, color.green,
276 color.blue, color.alpha);
279 *resolved_gradient = pattern;
284 _gtk_gradient_resolve_full (GtkGradient *gradient,
285 GtkStyleContext *context,
286 GtkCssDependencies *dependencies)
288 cairo_pattern_t *pattern;
291 g_return_val_if_fail (gradient != NULL, NULL);
292 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
293 g_return_val_if_fail (*dependencies == 0, NULL);
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++)
308 GtkCssDependencies stop_deps;
310 stop = &g_array_index (gradient->stops, ColorStop, i);
312 /* if color resolving fails, assume transparency */
313 if (!_gtk_style_context_resolve_color (context, stop->color, &rgba, &stop_deps))
314 rgba.red = rgba.green = rgba.blue = rgba.alpha = 0.0;
316 *dependencies = _gtk_css_dependencies_union (*dependencies, stop_deps);
317 cairo_pattern_add_color_stop_rgba (pattern, stop->offset,
318 rgba.red, rgba.green,
319 rgba.blue, rgba.alpha);
326 gtk_gradient_resolve_for_context (GtkGradient *gradient,
327 GtkStyleContext *context)
329 GtkCssDependencies ignored = 0;
331 g_return_val_if_fail (gradient != NULL, NULL);
332 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
334 return _gtk_gradient_resolve_full (gradient, context, &ignored);
338 append_number (GString *str,
344 if (zero && d == 0.0)
345 g_string_append (str, zero);
346 else if (half && d == 0.5)
347 g_string_append (str, half);
348 else if (one && d == 1.0)
349 g_string_append (str, one);
352 char buf[G_ASCII_DTOSTR_BUF_SIZE];
354 g_ascii_dtostr (buf, sizeof (buf), d);
355 g_string_append (str, buf);
360 * gtk_gradient_to_string:
361 * @gradient: the gradient to print
363 * Creates a string representation for @gradient that is suitable
364 * for using in GTK CSS files.
366 * Returns: A string representation for @gradient
369 gtk_gradient_to_string (GtkGradient *gradient)
374 g_return_val_if_fail (gradient != NULL, NULL);
376 str = g_string_new ("-gtk-gradient (");
378 if (gradient->radius0 == 0 && gradient->radius1 == 0)
380 g_string_append (str, "linear, ");
381 append_number (str, gradient->x0, "left", "center", "right");
382 g_string_append_c (str, ' ');
383 append_number (str, gradient->y0, "top", "center", "bottom");
384 g_string_append (str, ", ");
385 append_number (str, gradient->x1, "left", "center", "right");
386 g_string_append_c (str, ' ');
387 append_number (str, gradient->y1, "top", "center", "bottom");
391 g_string_append (str, "radial, ");
392 append_number (str, gradient->x0, "left", "center", "right");
393 g_string_append_c (str, ' ');
394 append_number (str, gradient->y0, "top", "center", "bottom");
395 g_string_append (str, ", ");
396 append_number (str, gradient->radius0, NULL, NULL, NULL);
397 g_string_append (str, ", ");
398 append_number (str, gradient->x1, "left", "center", "right");
399 g_string_append_c (str, ' ');
400 append_number (str, gradient->y1, "top", "center", "bottom");
401 g_string_append (str, ", ");
402 append_number (str, gradient->radius1, NULL, NULL, NULL);
405 for (i = 0; i < gradient->stops->len; i++)
410 stop = &g_array_index (gradient->stops, ColorStop, i);
412 g_string_append (str, ", ");
414 if (stop->offset == 0.0)
415 g_string_append (str, "from (");
416 else if (stop->offset == 1.0)
417 g_string_append (str, "to (");
420 g_string_append (str, "color-stop (");
421 append_number (str, stop->offset, NULL, NULL, NULL);
422 g_string_append (str, ", ");
425 s = gtk_symbolic_color_to_string (stop->color);
426 g_string_append (str, s);
429 g_string_append (str, ")");
432 g_string_append (str, ")");
434 return g_string_free (str, FALSE);