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