2018-07-08 09:26:12 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "layoutoverlay.h"
|
|
|
|
#include "gtkwidgetprivate.h"
|
|
|
|
#include "gtkcssstyleprivate.h"
|
|
|
|
#include "gtkcssnodeprivate.h"
|
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2020-06-07 12:48:14 +00:00
|
|
|
#include "gtknative.h"
|
2018-07-08 09:26:12 +00:00
|
|
|
|
|
|
|
struct _GtkLayoutOverlay
|
|
|
|
{
|
|
|
|
GtkInspectorOverlay parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkLayoutOverlayClass
|
|
|
|
{
|
|
|
|
GtkInspectorOverlayClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkLayoutOverlay, gtk_layout_overlay, GTK_TYPE_INSPECTOR_OVERLAY)
|
|
|
|
|
|
|
|
|
|
|
|
static gint
|
2020-01-28 07:38:25 +00:00
|
|
|
get_number (GtkCssValue *value)
|
2018-07-08 09:26:12 +00:00
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
double d = _gtk_css_number_value_get (value, 100);
|
2018-07-08 09:26:12 +00:00
|
|
|
|
|
|
|
if (d < 1)
|
|
|
|
return ceil (d);
|
|
|
|
else
|
|
|
|
return floor (d);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_box_margin (GtkCssStyle *style,
|
|
|
|
GtkBorder *margin)
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
margin->top = get_number (style->size->margin_top);
|
|
|
|
margin->left = get_number (style->size->margin_left);
|
|
|
|
margin->bottom = get_number (style->size->margin_bottom);
|
|
|
|
margin->right = get_number (style->size->margin_right);
|
2018-07-08 09:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_box_border (GtkCssStyle *style,
|
|
|
|
GtkBorder *border)
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
border->top = get_number (style->border->border_top_width);
|
|
|
|
border->left = get_number (style->border->border_left_width);
|
|
|
|
border->bottom = get_number (style->border->border_bottom_width);
|
|
|
|
border->right = get_number (style->border->border_right_width);
|
2018-07-08 09:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_box_padding (GtkCssStyle *style,
|
|
|
|
GtkBorder *border)
|
|
|
|
{
|
2020-01-28 07:38:25 +00:00
|
|
|
border->top = get_number (style->size->padding_top);
|
|
|
|
border->left = get_number (style->size->padding_left);
|
|
|
|
border->bottom = get_number (style->size->padding_bottom);
|
|
|
|
border->right = get_number (style->size->padding_right);
|
2018-07-08 09:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recurse_child_widgets (GtkWidget *widget,
|
|
|
|
GtkSnapshot *snapshot)
|
|
|
|
{
|
2019-08-26 17:34:52 +00:00
|
|
|
gboolean needs_clip;
|
|
|
|
int width = gtk_widget_get_width (widget);
|
|
|
|
int height = gtk_widget_get_height (widget);
|
2018-07-08 09:26:12 +00:00
|
|
|
GtkCssStyle *style;
|
|
|
|
GtkWidget *child;
|
2019-08-26 17:34:52 +00:00
|
|
|
GtkBorder boxes[4];
|
|
|
|
const GdkRGBA colors[4] = {
|
|
|
|
{0.7, 0.0, 0.7, 0.6}, /* Padding */
|
|
|
|
{0.0, 0.0, 0.0, 0.0}, /* Border */
|
|
|
|
{0.7, 0.7, 0.0, 0.6}, /* CSS Margin */
|
|
|
|
{0.7, 0.0, 0.0, 0.6}, /* Widget Margin */
|
|
|
|
};
|
|
|
|
int i;
|
2018-07-08 09:26:12 +00:00
|
|
|
|
|
|
|
if (!gtk_widget_get_mapped (widget))
|
|
|
|
return;
|
|
|
|
|
2019-08-26 17:34:52 +00:00
|
|
|
G_STATIC_ASSERT (G_N_ELEMENTS (boxes) == G_N_ELEMENTS (colors));
|
|
|
|
|
2018-07-08 09:26:12 +00:00
|
|
|
style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
|
2019-08-26 17:34:52 +00:00
|
|
|
get_box_padding (style, &boxes[0]);
|
|
|
|
get_box_border (style, &boxes[1]);
|
|
|
|
get_box_margin (style, &boxes[2]);
|
2018-07-08 09:26:12 +00:00
|
|
|
|
|
|
|
/* TODO: Eh, left = start? RTL? */
|
2019-08-26 17:34:52 +00:00
|
|
|
boxes[3].left = gtk_widget_get_margin_start (widget);
|
|
|
|
boxes[3].top = gtk_widget_get_margin_top (widget);
|
|
|
|
boxes[3].right = gtk_widget_get_margin_end (widget);
|
|
|
|
boxes[3].bottom = gtk_widget_get_margin_bottom (widget);
|
|
|
|
|
|
|
|
/* width/height are the content size and we're going to grow that
|
|
|
|
* as we're drawing the boxes, as well as offset the origin.
|
|
|
|
* Right now we're at the widget's own origin.
|
|
|
|
*/
|
2019-02-21 04:34:12 +00:00
|
|
|
gtk_snapshot_save (snapshot);
|
2018-07-08 09:26:12 +00:00
|
|
|
gtk_snapshot_push_debug (snapshot, "Widget layout debugging");
|
|
|
|
|
2019-08-26 17:34:52 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS (boxes); i ++)
|
|
|
|
{
|
|
|
|
const GdkRGBA *color = &colors[i];
|
|
|
|
const GtkBorder *box = &boxes[i];
|
|
|
|
|
|
|
|
gtk_snapshot_append_color (snapshot, color,
|
|
|
|
&GRAPHENE_RECT_INIT ( 0, - box->top, width, box->top));
|
|
|
|
gtk_snapshot_append_color (snapshot, color,
|
|
|
|
&GRAPHENE_RECT_INIT (width, 0, box->right, height));
|
|
|
|
gtk_snapshot_append_color (snapshot, color,
|
|
|
|
&GRAPHENE_RECT_INIT (0, height, width, box->bottom));
|
|
|
|
gtk_snapshot_append_color (snapshot, color,
|
|
|
|
&GRAPHENE_RECT_INIT (- box->left, 0, box->left, height));
|
|
|
|
|
|
|
|
/* Grow box + offset */
|
|
|
|
width += box->left + box->right;
|
|
|
|
height += box->top + box->bottom;
|
|
|
|
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (- box->left, - box->top));
|
|
|
|
}
|
2018-07-08 09:26:12 +00:00
|
|
|
|
|
|
|
gtk_snapshot_pop (snapshot);
|
|
|
|
|
|
|
|
|
2019-08-26 17:34:52 +00:00
|
|
|
needs_clip = gtk_widget_get_overflow (widget) == GTK_OVERFLOW_HIDDEN &&
|
|
|
|
gtk_widget_get_first_child (widget) != NULL;
|
|
|
|
|
|
|
|
if (needs_clip)
|
|
|
|
gtk_snapshot_push_clip (snapshot,
|
|
|
|
&GRAPHENE_RECT_INIT (0, 0, gtk_widget_get_width (widget), gtk_widget_get_height (widget)));
|
|
|
|
|
2018-07-08 09:26:12 +00:00
|
|
|
/* Recurse into child widgets */
|
|
|
|
for (child = gtk_widget_get_first_child (widget);
|
|
|
|
child != NULL;
|
|
|
|
child = gtk_widget_get_next_sibling (child))
|
|
|
|
{
|
2019-08-26 17:34:52 +00:00
|
|
|
gtk_snapshot_save (snapshot);
|
|
|
|
gtk_snapshot_transform (snapshot, child->priv->transform);
|
2018-07-08 09:26:12 +00:00
|
|
|
|
|
|
|
recurse_child_widgets (child, snapshot);
|
2019-08-26 17:34:52 +00:00
|
|
|
|
|
|
|
gtk_snapshot_restore (snapshot);
|
2018-07-08 09:26:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 17:34:52 +00:00
|
|
|
if (needs_clip)
|
|
|
|
gtk_snapshot_pop (snapshot);
|
|
|
|
|
2019-02-21 04:34:12 +00:00
|
|
|
gtk_snapshot_restore (snapshot);
|
2018-07-08 09:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_layout_overlay_snapshot (GtkInspectorOverlay *overlay,
|
|
|
|
GtkSnapshot *snapshot,
|
|
|
|
GskRenderNode *node,
|
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
recurse_child_widgets (widget, snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_layout_overlay_init (GtkLayoutOverlay *self)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_layout_overlay_class_init (GtkLayoutOverlayClass *klass)
|
|
|
|
{
|
|
|
|
GtkInspectorOverlayClass *overlay_class = (GtkInspectorOverlayClass *)klass;
|
|
|
|
|
|
|
|
overlay_class->snapshot = gtk_layout_overlay_snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkInspectorOverlay *
|
|
|
|
gtk_layout_overlay_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_LAYOUT_OVERLAY, NULL);
|
|
|
|
}
|