From a9809e5d30fbf4d5e3b555b5489d75102d5d30d0 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Sun, 18 Dec 2016 17:10:45 +0100 Subject: [PATCH] gsk: Add gsk_rounded_rect_shrink() ... and replace _gtk_rounded_box_grow() and _gtk_rounded_box_shrink() with it. --- docs/reference/gsk/gsk4-sections.txt | 1 + gsk/gskroundedrect.c | 72 ++++++++++++++++++++++++++++ gsk/gskroundedrect.h | 6 +++ gtk/gtkcssshadowvalue.c | 8 ++-- gtk/gtkrender.c | 2 +- gtk/gtkrenderbackground.c | 4 +- gtk/gtkrenderborder.c | 10 ++-- gtk/gtkroundedbox.c | 62 ------------------------ gtk/gtkroundedboxprivate.h | 11 ----- 9 files changed, 91 insertions(+), 85 deletions(-) diff --git a/docs/reference/gsk/gsk4-sections.txt b/docs/reference/gsk/gsk4-sections.txt index f5cc1518aa..3b47dab517 100644 --- a/docs/reference/gsk/gsk4-sections.txt +++ b/docs/reference/gsk/gsk4-sections.txt @@ -72,5 +72,6 @@ gsk_rounded_rect_init_copy gsk_rounded_rect_init_from_rect gsk_rounded_rect_normalize gsk_rounded_rect_offset +gsk_rounded_rect_shrink gsk_rounded_rect_is_rectilinear diff --git a/gsk/gskroundedrect.c b/gsk/gskroundedrect.c index 0e4173ad30..f1ecc2b4ec 100644 --- a/gsk/gskroundedrect.c +++ b/gsk/gskroundedrect.c @@ -205,6 +205,78 @@ gsk_rounded_rect_offset (GskRoundedRect *self, return self; } +static void +border_radius_shrink (graphene_size_t *corner, + double width, + double height) +{ + if (corner->width > 0) + corner->width -= width; + if (corner->height > 0) + corner->height -= height; + + if (corner->width <= 0 || corner->height <= 0) + { + corner->width = 0; + corner->height = 0; + } +} + +/** + * gsk_rounded_rect_shrink: + * @self: The @GskRoundedRect to shrink or grow + * @top: How far to move the top side downwards + * @right: How far to move the right side to the left + * @bottom: How far to move the bottom side upwards + * @left: How far to move the left side to the right + * + * Shrinks (or grows) the given rectangle by moving the 4 sides + * according to the offsets given. The corner radii will be changed + * in a way that tries to keep the center of the corner circle intact. + * This emulates CSS behavior. + * + * This function also works for growing rectangles if you pass + * negative values for the @top, @right, @bottom or @left. + * + * Returns: @self + **/ +GskRoundedRect * +gsk_rounded_rect_shrink (GskRoundedRect *self, + float top, + float right, + float bottom, + float left) +{ + if (self->bounds.size.width - left - right < 0) + { + self->bounds.origin.x += left * self->bounds.size.width / (left + right); + self->bounds.size.width = 0; + } + else + { + self->bounds.origin.x += left; + self->bounds.size.width -= left + right; + } + + if (self->bounds.size.height - bottom - top < 0) + { + self->bounds.origin.y += top * self->bounds.size.height / (top + bottom); + self->bounds.size.height = 0; + } + else + { + self->bounds.origin.y += top; + self->bounds.size.height -= top + bottom; + } + + border_radius_shrink (&self->corner[GSK_CORNER_TOP_LEFT], left, top); + border_radius_shrink (&self->corner[GSK_CORNER_TOP_RIGHT], right, top); + border_radius_shrink (&self->corner[GSK_CORNER_BOTTOM_RIGHT], right, bottom); + border_radius_shrink (&self->corner[GSK_CORNER_BOTTOM_LEFT], left, bottom); + + return self; +} + /** * gsk_rounded_rect_is_rectilinear: * @self: the #GskRoundedRect to check diff --git a/gsk/gskroundedrect.h b/gsk/gskroundedrect.h index 96551edd43..9a3d33eb01 100644 --- a/gsk/gskroundedrect.h +++ b/gsk/gskroundedrect.h @@ -87,6 +87,12 @@ GDK_AVAILABLE_IN_3_90 GskRoundedRect * gsk_rounded_rect_offset (GskRoundedRect *self, float dx, float dy); +GDK_AVAILABLE_IN_3_90 +GskRoundedRect * gsk_rounded_rect_shrink (GskRoundedRect *self, + float top, + float right, + float bottom, + float left); GDK_AVAILABLE_IN_3_90 gboolean gsk_rounded_rect_is_rectilinear (GskRoundedRect *self); diff --git a/gtk/gtkcssshadowvalue.c b/gtk/gtkcssshadowvalue.c index 24e852b06e..6edd51aa22 100644 --- a/gtk/gtkcssshadowvalue.c +++ b/gtk/gtkcssshadowvalue.c @@ -925,7 +925,7 @@ _gtk_css_shadow_value_paint_box (const GtkCssValue *shadow, gsk_rounded_rect_path (padding_box, cr); outside = spread + clip_radius + MAX (fabs (x), fabs (y)); clip_box = *padding_box; - _gtk_rounded_box_grow (&clip_box, outside, outside, outside, outside); + gsk_rounded_rect_shrink (&clip_box, -outside, -outside, -outside, -outside); _gtk_rounded_box_clip_path (&clip_box, cr); cairo_clip (cr); @@ -935,12 +935,12 @@ _gtk_css_shadow_value_paint_box (const GtkCssValue *shadow, gsk_rounded_rect_offset (&box, x, y); if (shadow->inset) - _gtk_rounded_box_shrink (&box, spread, spread, spread, spread); + gsk_rounded_rect_shrink (&box, spread, spread, spread, spread); else /* Outset */ - _gtk_rounded_box_grow (&box, spread, spread, spread, spread); + gsk_rounded_rect_shrink (&box, -spread, -spread, -spread, -spread); clip_box = *padding_box; - _gtk_rounded_box_shrink (&clip_box, -clip_radius, -clip_radius, -clip_radius, -clip_radius); + gsk_rounded_rect_shrink (&clip_box, -clip_radius, -clip_radius, -clip_radius, -clip_radius); if (!needs_blur (shadow)) draw_shadow (shadow, cr, &box, &clip_box, GTK_BLUR_NONE); diff --git a/gtk/gtkrender.c b/gtk/gtkrender.c index e23d712406..a0c086bb97 100644 --- a/gtk/gtkrender.c +++ b/gtk/gtkrender.c @@ -986,7 +986,7 @@ gtk_render_content_path (GtkStyleContext *context, _gtk_rounded_box_init_rect (&box, x, y, width, height); _gtk_rounded_box_apply_border_radius_for_style (&box, gtk_style_context_lookup_style (context), 0); - _gtk_rounded_box_shrink (&box, + gsk_rounded_rect_shrink (&box, _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_TOP_WIDTH), 100) + _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_PADDING_TOP), 100), _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH), 100) diff --git a/gtk/gtkrenderbackground.c b/gtk/gtkrenderbackground.c index 10f54c6878..4beafb203d 100644 --- a/gtk/gtkrenderbackground.c +++ b/gtk/gtkrenderbackground.c @@ -555,12 +555,12 @@ gtk_theming_background_init (GtkThemingBackground *bg, _gtk_rounded_box_apply_border_radius_for_style (&bg->boxes[GTK_CSS_AREA_BORDER_BOX], bg->style, junction); bg->boxes[GTK_CSS_AREA_PADDING_BOX] = bg->boxes[GTK_CSS_AREA_BORDER_BOX]; - _gtk_rounded_box_shrink (&bg->boxes[GTK_CSS_AREA_PADDING_BOX], + gsk_rounded_rect_shrink (&bg->boxes[GTK_CSS_AREA_PADDING_BOX], border.top, border.right, border.bottom, border.left); bg->boxes[GTK_CSS_AREA_CONTENT_BOX] = bg->boxes[GTK_CSS_AREA_PADDING_BOX]; - _gtk_rounded_box_shrink (&bg->boxes[GTK_CSS_AREA_CONTENT_BOX], + gsk_rounded_rect_shrink (&bg->boxes[GTK_CSS_AREA_CONTENT_BOX], padding.top, padding.right, padding.bottom, padding.left); } diff --git a/gtk/gtkrenderborder.c b/gtk/gtkrenderborder.c index 7c418616a5..9696c6cc69 100644 --- a/gtk/gtkrenderborder.c +++ b/gtk/gtkrenderborder.c @@ -362,7 +362,7 @@ render_frame_fill (cairo_t *cr, guint i, j; padding_box = *border_box; - _gtk_rounded_box_shrink (&padding_box, + gsk_rounded_rect_shrink (&padding_box, border_width[GTK_CSS_TOP], border_width[GTK_CSS_RIGHT], border_width[GTK_CSS_BOTTOM], @@ -486,7 +486,7 @@ render_frame_stroke (cairo_t *cr, border_width[0] != border_width[3] ; stroke_box = *border_box; - _gtk_rounded_box_shrink (&stroke_box, + gsk_rounded_rect_shrink (&stroke_box, border_width[GTK_CSS_TOP] / 2.0, border_width[GTK_CSS_RIGHT] / 2.0, border_width[GTK_CSS_BOTTOM] / 2.0, @@ -513,7 +513,7 @@ render_frame_stroke (cairo_t *cr, GskRoundedRect padding_box; padding_box = *border_box; - _gtk_rounded_box_shrink (&padding_box, + gsk_rounded_rect_shrink (&padding_box, border_width[GTK_CSS_TOP], border_width[GTK_CSS_RIGHT], border_width[GTK_CSS_BOTTOM], @@ -640,7 +640,7 @@ render_border (cairo_t *cr, render_frame_fill (cr, border_box, other_border, colors, dont_draw); other_box = *border_box; - _gtk_rounded_box_shrink (&other_box, + gsk_rounded_rect_shrink (&other_box, 2 * other_border[GTK_CSS_TOP], 2 * other_border[GTK_CSS_RIGHT], 2 * other_border[GTK_CSS_BOTTOM], @@ -674,7 +674,7 @@ render_border (cairo_t *cr, render_frame_fill (cr, border_box, other_border, colors, dont_draw); other_box = *border_box; - _gtk_rounded_box_shrink (&other_box, + gsk_rounded_rect_shrink (&other_box, other_border[GTK_CSS_TOP], other_border[GTK_CSS_RIGHT], other_border[GTK_CSS_BOTTOM], diff --git a/gtk/gtkroundedbox.c b/gtk/gtkroundedbox.c index 73b9746489..5dafd63d25 100644 --- a/gtk/gtkroundedbox.c +++ b/gtk/gtkroundedbox.c @@ -151,68 +151,6 @@ _gtk_rounded_box_apply_outline_radius_for_style (GskRoundedRect *box, _gtk_rounded_box_apply_border_radius (box, corner, junction); } -static void -gtk_css_border_radius_grow (graphene_size_t *corner, - double width, - double height) -{ - if (corner->width) - corner->width += width; - if (corner->height) - corner->height += height; - - if (corner->width <= 0 || corner->height <= 0) - { - corner->width = 0; - corner->height = 0; - } -} - -void -_gtk_rounded_box_grow (GskRoundedRect *box, - double top, - double right, - double bottom, - double left) -{ - if (box->bounds.size.width + left + right < 0) - { - box->bounds.origin.x -= left * box->bounds.size.width / (left + right); - box->bounds.size.width = 0; - } - else - { - box->bounds.origin.x -= left; - box->bounds.size.width += left + right; - } - - if (box->bounds.size.height + bottom + top < 0) - { - box->bounds.origin.y -= top * box->bounds.size.height / (top + bottom); - box->bounds.size.height = 0; - } - else - { - box->bounds.origin.y -= top; - box->bounds.size.height += top + bottom; - } - - gtk_css_border_radius_grow (&box->corner[GSK_CORNER_TOP_LEFT], left, top); - gtk_css_border_radius_grow (&box->corner[GSK_CORNER_TOP_RIGHT], right, top); - gtk_css_border_radius_grow (&box->corner[GSK_CORNER_BOTTOM_RIGHT], right, bottom); - gtk_css_border_radius_grow (&box->corner[GSK_CORNER_BOTTOM_LEFT], left, bottom); -} - -void -_gtk_rounded_box_shrink (GskRoundedRect *box, - double top, - double right, - double bottom, - double left) -{ - _gtk_rounded_box_grow (box, -top, -right, -bottom, -left); -} - typedef struct { double angle1; double angle2; diff --git a/gtk/gtkroundedboxprivate.h b/gtk/gtkroundedboxprivate.h index 957bb3e4ca..474c56cc79 100644 --- a/gtk/gtkroundedboxprivate.h +++ b/gtk/gtkroundedboxprivate.h @@ -42,17 +42,6 @@ void _gtk_rounded_box_apply_outline_radius_for_style (GskRoundedRect GtkCssStyle *style, GtkJunctionSides junction); -void _gtk_rounded_box_grow (GskRoundedRect *box, - double top, - double right, - double bottom, - double left); -void _gtk_rounded_box_shrink (GskRoundedRect *box, - double top, - double right, - double bottom, - double left); - double _gtk_rounded_box_guess_length (const GskRoundedRect *box, GtkCssSide side);