From 1ef250f44a93504bf154afca32cd2d4cc0908780 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Thu, 21 Feb 2019 03:26:03 +0100 Subject: [PATCH] snapshot: Add functions to append shadows --- docs/reference/gtk/gtk4-sections.txt | 2 + gtk/gtkcssshadowvalue.c | 26 ++------ gtk/gtksnapshot.c | 88 ++++++++++++++++++++++++++++ gtk/gtksnapshot.h | 16 +++++ 4 files changed, 110 insertions(+), 22 deletions(-) diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt index bb39c8f935..c82f4b8c43 100644 --- a/docs/reference/gtk/gtk4-sections.txt +++ b/docs/reference/gtk/gtk4-sections.txt @@ -4412,6 +4412,8 @@ gtk_snapshot_append_layout gtk_snapshot_append_linear_gradient gtk_snapshot_append_repeating_linear_gradient gtk_snapshot_append_border +gtk_snapshot_append_inset_shadow +gtk_snapshot_append_outset_shadow gtk_snapshot_render_background gtk_snapshot_render_frame gtk_snapshot_render_focus diff --git a/gtk/gtkcssshadowvalue.c b/gtk/gtkcssshadowvalue.c index b04c1f58c3..7e6c41c17d 100644 --- a/gtk/gtkcssshadowvalue.c +++ b/gtk/gtkcssshadowvalue.c @@ -349,28 +349,19 @@ gtk_css_shadow_value_snapshot_outset (const GtkCssValue *shadow, GtkSnapshot *snapshot, const GskRoundedRect *border_box) { - GskRoundedRect outline; - GskRenderNode *node; - int off_x, off_y; - g_return_if_fail (shadow->class == >K_CSS_VALUE_SHADOW); /* We don't need to draw invisible shadows */ if (gdk_rgba_is_clear (_gtk_css_rgba_value_get_rgba (shadow->color))) return; - gtk_snapshot_get_offset (snapshot, &off_x, &off_y); - gsk_rounded_rect_init_copy (&outline, border_box); - gsk_rounded_rect_offset (&outline, off_x, off_y); - - node = gsk_outset_shadow_node_new (&outline, + gtk_snapshot_append_outset_shadow (snapshot, + border_box, _gtk_css_rgba_value_get_rgba (shadow->color), _gtk_css_number_value_get (shadow->hoffset, 0), _gtk_css_number_value_get (shadow->voffset, 0), _gtk_css_number_value_get (shadow->spread, 0), _gtk_css_number_value_get (shadow->radius, 0)); - gtk_snapshot_append_node_internal (snapshot, node); - gsk_render_node_unref (node); } void @@ -378,28 +369,19 @@ gtk_css_shadow_value_snapshot_inset (const GtkCssValue *shadow, GtkSnapshot *snapshot, const GskRoundedRect*padding_box) { - GskRoundedRect outline; - GskRenderNode *node; - int off_x, off_y; - g_return_if_fail (shadow->class == >K_CSS_VALUE_SHADOW); /* We don't need to draw invisible shadows */ if (gdk_rgba_is_clear (_gtk_css_rgba_value_get_rgba (shadow->color))) return; - gtk_snapshot_get_offset (snapshot, &off_x, &off_y); - gsk_rounded_rect_init_copy (&outline, padding_box); - gsk_rounded_rect_offset (&outline, off_x, off_y); - - node = gsk_inset_shadow_node_new (&outline, + gtk_snapshot_append_inset_shadow (snapshot, + padding_box, _gtk_css_rgba_value_get_rgba (shadow->color), _gtk_css_number_value_get (shadow->hoffset, 0), _gtk_css_number_value_get (shadow->voffset, 0), _gtk_css_number_value_get (shadow->spread, 0), _gtk_css_number_value_get (shadow->radius, 0)); - gtk_snapshot_append_node_internal (snapshot, node); - gsk_render_node_unref (node); } gboolean diff --git a/gtk/gtksnapshot.c b/gtk/gtksnapshot.c index eb666dc5ad..0caa0ef078 100644 --- a/gtk/gtksnapshot.c +++ b/gtk/gtksnapshot.c @@ -1636,3 +1636,91 @@ gtk_snapshot_append_border (GtkSnapshot *snapshot, gtk_snapshot_append_node_internal (snapshot, node); gsk_render_node_unref (node); } + +/** + * gtk_snapshot_append_inset_shadow: + * @snapshot: a #GtkSnapshot + * @outline: outline of the region surrounded by shadow + * @color: color of the shadow + * @dx: horizontal offset of shadow + * @dy: vertical offset of shadow + * @spread: how far the shadow spreads towards the inside + * @blur_radius: how much blur to apply to the shadow + * + * Appends an inset shadow into the box given by @outline. + */ +void +gtk_snapshot_append_inset_shadow (GtkSnapshot *snapshot, + const GskRoundedRect *outline, + const GdkRGBA *color, + float dx, + float dy, + float spread, + float blur_radius) +{ + GskRenderNode *node; + GskRoundedRect real_outline; + float scale_x, scale_y, x, y; + + g_return_if_fail (snapshot != NULL); + g_return_if_fail (outline != NULL); + g_return_if_fail (color != NULL); + + gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &x, &y); + gtk_rounded_rect_scale_affine (&real_outline, outline, scale_x, scale_y, x, y); + + node = gsk_inset_shadow_node_new (&real_outline, + color, + scale_x * dx + x, + scale_y * dy + y, + spread, + blur_radius); + + gtk_snapshot_append_node_internal (snapshot, node); + gsk_render_node_unref (node); +} + +/** + * gtk_snapshot_append_outset_shadow: + * @snapshot: a #GtkSnapshot + * @outline: outline of the region surrounded by shadow + * @color: color of the shadow + * @dx: horizontal offset of shadow + * @dy: vertical offset of shadow + * @spread: how far the shadow spreads towards the outside + * @blur_radius: how much blur to apply to the shadow + * + * Appends an outset shadow node around the box given by @outline. + */ +void +gtk_snapshot_append_outset_shadow (GtkSnapshot *snapshot, + const GskRoundedRect *outline, + const GdkRGBA *color, + float dx, + float dy, + float spread, + float blur_radius) +{ + GskRenderNode *node; + GskRoundedRect real_outline; + float scale_x, scale_y, x, y; + + g_return_if_fail (snapshot != NULL); + g_return_if_fail (outline != NULL); + g_return_if_fail (color != NULL); + + gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &x, &y); + gtk_rounded_rect_scale_affine (&real_outline, outline, scale_x, scale_y, x, y); + + node = gsk_outset_shadow_node_new (&real_outline, + color, + scale_x * dx + x, + scale_y * dy + y, + spread, + blur_radius); + + + gtk_snapshot_append_node_internal (snapshot, node); + gsk_render_node_unref (node); +} + diff --git a/gtk/gtksnapshot.h b/gtk/gtksnapshot.h index 1f61d7a214..05cf3f5867 100644 --- a/gtk/gtksnapshot.h +++ b/gtk/gtksnapshot.h @@ -146,6 +146,22 @@ void gtk_snapshot_append_border (GtkSnapshot const GskRoundedRect *outline, const float border_width[4], const GdkRGBA border_color[4]); +GDK_AVAILABLE_IN_ALL +void gtk_snapshot_append_inset_shadow (GtkSnapshot *snapshot, + const GskRoundedRect *outline, + const GdkRGBA *color, + float dx, + float dy, + float spread, + float blur_radius); +GDK_AVAILABLE_IN_ALL +void gtk_snapshot_append_outset_shadow (GtkSnapshot *snapshot, + const GskRoundedRect *outline, + const GdkRGBA *color, + float dx, + float dy, + float spread, + float blur_radius); /* next function implemented in gskpango.c */ GDK_AVAILABLE_IN_ALL void gtk_snapshot_append_layout (GtkSnapshot *snapshot,