From 8b6f92d08720f1fcbcb4a061db4c21212aaa185f Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Mon, 11 Apr 2011 18:38:22 +0200 Subject: [PATCH] API: Add gtk_gradient_to_string() --- docs/reference/gtk/gtk3-sections.txt | 1 + gtk/gtk.symbols | 1 + gtk/gtkgradient.c | 100 +++++++++++++++++++++++++++ gtk/gtkgradient.h | 2 + 4 files changed, 104 insertions(+) diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt index 1fed765efe..5333b01210 100644 --- a/docs/reference/gtk/gtk3-sections.txt +++ b/docs/reference/gtk/gtk3-sections.txt @@ -5747,6 +5747,7 @@ gtk_gradient_add_color_stop gtk_gradient_ref gtk_gradient_unref gtk_gradient_resolve +gtk_gradient_to_string GTK_TYPE_GRADIENT diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols index 643bc3507c..49d0f2cce7 100644 --- a/gtk/gtk.symbols +++ b/gtk/gtk.symbols @@ -712,6 +712,7 @@ gtk_gradient_new_linear gtk_gradient_new_radial gtk_gradient_ref gtk_gradient_resolve +gtk_gradient_to_string gtk_gradient_unref gtk_dialog_add_action_widget gtk_dialog_add_button diff --git a/gtk/gtkgradient.c b/gtk/gtkgradient.c index a19b8fe583..3815860566 100644 --- a/gtk/gtkgradient.c +++ b/gtk/gtkgradient.c @@ -280,3 +280,103 @@ gtk_gradient_resolve (GtkGradient *gradient, *resolved_gradient = pattern; return TRUE; } + +static void +append_number (GString *str, + double d, + const char *zero, + const char *half, + const char *one) +{ + if (zero && d == 0.0) + g_string_append (str, zero); + else if (half && d == 0.5) + g_string_append (str, half); + else if (one && d == 1.0) + g_string_append (str, one); + else + { + char buf[G_ASCII_DTOSTR_BUF_SIZE]; + + g_ascii_dtostr (buf, sizeof (buf), d); + g_string_append (str, buf); + } +} + +/** + * gtk_gradient_to_string: + * @gradient: the gradient to print + * + * Creates a string representation for @gradient that is suitable + * for using in GTK CSS files. + * + * Returns: A string representation for @gradient + **/ +char * +gtk_gradient_to_string (GtkGradient *gradient) +{ + GString *str; + guint i; + + g_return_val_if_fail (gradient != NULL, NULL); + + str = g_string_new ("-gtk-gradient ("); + + if (gradient->radius0 == 0 && gradient->radius1 == 0) + { + g_string_append (str, "linear, "); + append_number (str, gradient->x0, "left", "center", "right"); + g_string_append_c (str, ' '); + append_number (str, gradient->y0, "top", "center", "bottom"); + g_string_append (str, ", "); + append_number (str, gradient->x1, "left", "center", "right"); + g_string_append_c (str, ' '); + append_number (str, gradient->y1, "top", "center", "bottom"); + } + else + { + g_string_append (str, "radial, "); + append_number (str, gradient->x0, "left", "center", "right"); + g_string_append_c (str, ' '); + append_number (str, gradient->y0, "top", "center", "bottom"); + g_string_append (str, ", "); + append_number (str, gradient->radius0, NULL, NULL, NULL); + g_string_append (str, ", "); + append_number (str, gradient->x1, "left", "center", "right"); + g_string_append_c (str, ' '); + append_number (str, gradient->y1, "top", "center", "bottom"); + g_string_append (str, ", "); + append_number (str, gradient->radius1, NULL, NULL, NULL); + } + + for (i = 0; i < gradient->stops->len; i++) + { + ColorStop *stop; + char *s; + + stop = &g_array_index (gradient->stops, ColorStop, i); + + g_string_append (str, ", "); + + if (stop->offset == 0.0) + g_string_append (str, "from ("); + else if (stop->offset == 1.0) + g_string_append (str, "to ("); + else + { + g_string_append (str, "color-stop ("); + append_number (str, stop->offset, NULL, NULL, NULL); + g_string_append (str, ", "); + } + + s = gtk_symbolic_color_to_string (stop->color); + g_string_append (str, s); + g_free (s); + + g_string_append (str, ")"); + } + + g_string_append (str, ")"); + + return g_string_free (str, FALSE); +} diff --git a/gtk/gtkgradient.h b/gtk/gtkgradient.h index f097c40fd0..7d54030235 100644 --- a/gtk/gtkgradient.h +++ b/gtk/gtkgradient.h @@ -56,6 +56,8 @@ gboolean gtk_gradient_resolve (GtkGradient *gradient, GtkStyleProperties *props, cairo_pattern_t **resolved_gradient); +char * gtk_gradient_to_string (GtkGradient *gradient); + G_END_DECLS #endif /* __GTK_GRADIENT_H__ */