summaryrefslogtreecommitdiff
path: root/gtk/inspector/layoutoverlay.c
blob: f1afa20acb8e55d6e642b25eb6ef3b19d86c6fcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

#include "config.h"
#include "layoutoverlay.h"
#include "gtkwidgetprivate.h"
#include "gtkcssstyleprivate.h"
#include "gtkcssnodeprivate.h"
#include "gtkcssnumbervalueprivate.h"

struct _GtkLayoutOverlay
{
  GtkInspectorOverlay parent_instance;
};

struct _GtkLayoutOverlayClass
{
  GtkInspectorOverlayClass parent_class;
};

G_DEFINE_TYPE (GtkLayoutOverlay, gtk_layout_overlay, GTK_TYPE_INSPECTOR_OVERLAY)


static gint
get_number (GtkCssValue *value)
{
  double d = _gtk_css_number_value_get (value, 100);

  if (d < 1)
    return ceil (d);
  else
    return floor (d);
}

static void
get_box_margin (GtkCssStyle *style,
                GtkBorder   *margin)
{
  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);
}

static void
get_box_border (GtkCssStyle *style,
                GtkBorder   *border)
{
  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);
}

static void
get_box_padding (GtkCssStyle *style,
                 GtkBorder   *border)
{
  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);
}

static void
recurse_child_widgets (GtkWidget   *widget,
                       GtkSnapshot *snapshot)
{
  gboolean needs_clip;
  int width = gtk_widget_get_width (widget);
  int height = gtk_widget_get_height (widget);
  GtkCssStyle *style;
  GtkWidget *child;
  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;

  if (!gtk_widget_get_mapped (widget))
    return;

  G_STATIC_ASSERT (G_N_ELEMENTS (boxes) == G_N_ELEMENTS (colors));

  style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
  get_box_padding (style, &boxes[0]);
  get_box_border (style, &boxes[1]);
  get_box_margin (style, &boxes[2]);

  /* TODO: Eh, left = start? RTL? */
  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.
   */
  gtk_snapshot_save (snapshot);
  gtk_snapshot_push_debug (snapshot, "Widget layout debugging");

  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));
    }

  gtk_snapshot_pop (snapshot);


  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)));

  /* Recurse into child widgets */
  for (child = gtk_widget_get_first_child (widget);
       child != NULL;
       child = gtk_widget_get_next_sibling (child))
    {
      gtk_snapshot_save (snapshot);
      gtk_snapshot_transform (snapshot, child->priv->transform);

      recurse_child_widgets (child, snapshot);

      gtk_snapshot_restore (snapshot);
    }

  if (needs_clip)
    gtk_snapshot_pop (snapshot);

  gtk_snapshot_restore (snapshot);
}

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);
}