]> git.sur5r.net Git - i3/i3/blob - src/render.c
Implement support for size hints (including test case)
[i3/i3] / src / render.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6
7 /*
8  * "Renders" the given container (and its children), meaning that all rects are
9  * updated correctly. Note that this function does not call any xcb_*
10  * functions, so the changes are completely done in memory only (and
11  * side-effect free). As soon as you call x_push_changes(), the changes will be
12  * updated in X11.
13  *
14  */
15 void render_con(Con *con) {
16     printf("currently rendering node %p / %s / layout %d\n",
17             con, con->name, con->layout);
18     int children = 0;
19     Con *child;
20     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
21         children++;
22     printf("children: %d, orientation = %d\n", children, con->orientation);
23
24     /* Copy container rect, subtract container border */
25     /* This is the actually usable space inside this container for clients */
26     Rect rect = con->rect;
27     rect.x += 2;
28     rect.y += 2;
29     rect.width -= 2 * 2;
30     rect.height -= 2 * 2;
31
32     int x = rect.x;
33     int y = rect.y;
34
35     int i = 0;
36
37     printf("mapped = true\n");
38     con->mapped = true;
39
40     /* if this container contains a window, set the coordinates */
41     if (con->window) {
42         /* depending on the border style, the rect of the child window
43          * needs to be smaller */
44         Rect *inset = &(con->window_rect);
45         *inset = (Rect){0, 0, con->rect.width, con->rect.height};
46         /* TODO: different border styles */
47         inset->x += 2;
48         inset->width -= 2 * 2;
49         inset->height -= 2;
50
51         /* Obey the aspect ratio, if any */
52         if (con->proportional_height != 0 &&
53             con->proportional_width != 0) {
54             DLOG("proportional height = %d, width = %d\n", con->proportional_height, con->proportional_width);
55             double new_height = inset->height + 1;
56             int new_width = inset->width;
57
58             while (new_height > inset->height) {
59                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
60
61                 if (new_height > inset->height)
62                     new_width--;
63             }
64             /* Center the window */
65             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
66             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
67
68             inset->height = new_height;
69             inset->width = new_width;
70             DLOG("new_height = %f, new_width = %d\n", new_height, new_width);
71         }
72
73         if (con->height_increment > 1) {
74             int old_height = inset->height;
75             inset->height -= (inset->height - con->base_height) % con->height_increment;
76             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
77                 old_height - inset->height, con->height_increment, con->base_height);
78         }
79
80         if (con->width_increment > 1) {
81             int old_width = inset->width;
82             inset->width -= (inset->width - con->base_width) % con->width_increment;
83             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
84                 old_width - inset->width, con->width_increment, con->base_width);
85         }
86
87         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
88     }
89
90     /* Check for fullscreen nodes */
91     Con *fullscreen = con_get_fullscreen_con(con);
92     if (fullscreen) {
93         LOG("got fs node: %p\n", fullscreen);
94         fullscreen->rect = rect;
95         x_raise_con(fullscreen);
96         render_con(fullscreen);
97         return;
98     }
99
100     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
101
102         /* default layout */
103         if (con->layout == L_DEFAULT) {
104             double percentage = 1.0 / children;
105             if (child->percent > 0.0)
106                 percentage = child->percent;
107             printf("child %p / %s requests percentage %f\n",
108                     child, child->name, percentage);
109
110             if (con->orientation == HORIZ) {
111                 child->rect.x = x;
112                 child->rect.y = y;
113                 child->rect.width = percentage * rect.width;
114                 child->rect.height = rect.height;
115                 x += child->rect.width;
116             } else {
117                 child->rect.x = x;
118                 child->rect.y = y;
119                 child->rect.width = rect.width;
120                 child->rect.height = percentage * rect.height;
121                 y += child->rect.height;
122             }
123
124             /* first we have the decoration, if this is a leaf node */
125             if (con_is_leaf(child)) {
126                 printf("that child is a leaf node, subtracting deco\n");
127                 /* TODO: make a function for relative coords? */
128                 child->deco_rect.x = child->rect.x - con->rect.x;
129                 child->deco_rect.y = child->rect.y - con->rect.y;
130
131                 child->rect.y += 17;
132                 child->rect.height -= 17;
133
134                 child->deco_rect.width = child->rect.width;
135                 child->deco_rect.height = 17;
136             }
137         }
138
139         if (con->layout == L_STACKED) {
140             printf("stacked con\n");
141             child->rect.x = x;
142             child->rect.y = y;
143             child->rect.width = rect.width;
144             child->rect.height = rect.height;
145
146             child->rect.y += (17 * children);
147             child->rect.height -= (17 * children);
148
149             child->deco_rect.x = x - con->rect.x;
150             child->deco_rect.y = y - con->rect.y + (i * 17);
151             child->deco_rect.width = child->rect.width;
152             child->deco_rect.height = 17;
153         }
154
155         printf("child at (%d, %d) with (%d x %d)\n",
156                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
157         printf("x now %d, y now %d\n", x, y);
158         x_raise_con(child);
159         render_con(child);
160         i++;
161     }
162
163     /* in a stacking container, we ensure the focused client is raised */
164     if (con->layout == L_STACKED) {
165         Con *foc = TAILQ_FIRST(&(con->focus_head));
166         if (foc != TAILQ_END(&(con->focus_head)))
167             x_raise_con(foc);
168     }
169
170     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
171         LOG("render floating:\n");
172         LOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
173         x_raise_con(child);
174         render_con(child);
175     }
176
177     printf("-- level up\n");
178 }