]> git.sur5r.net Git - i3/i3/blob - src/render.c
implement different border styles
[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 = 0;
23     Con *child;
24     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
25         children++;
26     printf("children: %d, orientation = %d\n", children, con->orientation);
27
28     /* Copy container rect, subtract container border */
29     /* This is the actually usable space inside this container for clients */
30     Rect rect = con->rect;
31
32     /* Display a border if this is a leaf node. For container nodes, we don’t
33      * draw borders (except when in debug mode) */
34     if (show_debug_borders) {
35         rect.x += 2;
36         rect.y += 2;
37         rect.width -= 2 * 2;
38         rect.height -= 2 * 2;
39     }
40
41     int x = rect.x;
42     int y = rect.y;
43
44     int i = 0;
45
46     printf("mapped = true\n");
47     con->mapped = true;
48
49     /* if this container contains a window, set the coordinates */
50     if (con->window) {
51         /* depending on the border style, the rect of the child window
52          * needs to be smaller */
53         Rect *inset = &(con->window_rect);
54
55         if (con->border_style == BS_NORMAL)
56             *inset = (Rect){2, 0, con->rect.width - (2 * 2), con->rect.height - 2};
57         else if (con->border_style == BS_1PIXEL)
58             *inset = (Rect){1, 1, con->rect.width - 2, con->rect.height - 1};
59         else *inset = (Rect){0, 0, con->rect.width, con->rect.height};
60
61         /* Obey the aspect ratio, if any */
62         if (con->proportional_height != 0 &&
63             con->proportional_width != 0) {
64             DLOG("proportional height = %d, width = %d\n", con->proportional_height, con->proportional_width);
65             double new_height = inset->height + 1;
66             int new_width = inset->width;
67
68             while (new_height > inset->height) {
69                 new_height = ((double)con->proportional_height / con->proportional_width) * new_width;
70
71                 if (new_height > inset->height)
72                     new_width--;
73             }
74             /* Center the window */
75             inset->y += ceil(inset->height / 2) - floor(new_height / 2);
76             inset->x += ceil(inset->width / 2) - floor(new_width / 2);
77
78             inset->height = new_height;
79             inset->width = new_width;
80             DLOG("new_height = %f, new_width = %d\n", new_height, new_width);
81         }
82
83         if (con->height_increment > 1) {
84             int old_height = inset->height;
85             inset->height -= (inset->height - con->base_height) % con->height_increment;
86             DLOG("Lost %d pixel due to client's height_increment (%d px, base_height = %d)\n",
87                 old_height - inset->height, con->height_increment, con->base_height);
88         }
89
90         if (con->width_increment > 1) {
91             int old_width = inset->width;
92             inset->width -= (inset->width - con->base_width) % con->width_increment;
93             DLOG("Lost %d pixel due to client's width_increment (%d px, base_width = %d)\n",
94                 old_width - inset->width, con->width_increment, con->base_width);
95         }
96
97         DLOG("child will be at %dx%d with size %dx%d\n", inset->x, inset->y, inset->width, inset->height);
98     }
99
100     /* Check for fullscreen nodes */
101     Con *fullscreen = con_get_fullscreen_con(con);
102     if (fullscreen) {
103         LOG("got fs node: %p\n", fullscreen);
104         fullscreen->rect = rect;
105         x_raise_con(fullscreen);
106         render_con(fullscreen);
107         return;
108     }
109
110     TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
111
112         /* default layout */
113         if (con->layout == L_DEFAULT) {
114             double percentage = 1.0 / children;
115             if (child->percent > 0.0)
116                 percentage = child->percent;
117             printf("child %p / %s requests percentage %f\n",
118                     child, child->name, percentage);
119
120             if (con->orientation == HORIZ) {
121                 child->rect.x = x;
122                 child->rect.y = y;
123                 child->rect.width = percentage * rect.width;
124                 child->rect.height = rect.height;
125                 x += child->rect.width;
126             } else {
127                 child->rect.x = x;
128                 child->rect.y = y;
129                 child->rect.width = rect.width;
130                 child->rect.height = percentage * rect.height;
131                 y += child->rect.height;
132             }
133
134             /* first we have the decoration, if this is a leaf node */
135             if (con_is_leaf(child) && child->border_style == BS_NORMAL) {
136                 printf("that child is a leaf node, subtracting deco\n");
137                 /* TODO: make a function for relative coords? */
138                 child->deco_rect.x = child->rect.x - con->rect.x;
139                 child->deco_rect.y = child->rect.y - con->rect.y;
140
141                 child->rect.y += 17;
142                 child->rect.height -= 17;
143
144                 child->deco_rect.width = child->rect.width;
145                 child->deco_rect.height = 17;
146             }
147         }
148
149         if (con->layout == L_STACKED) {
150             printf("stacked con\n");
151             child->rect.x = x;
152             child->rect.y = y;
153             child->rect.width = rect.width;
154             child->rect.height = rect.height;
155
156             child->rect.y += (17 * children);
157             child->rect.height -= (17 * children);
158
159             child->deco_rect.x = x - con->rect.x;
160             child->deco_rect.y = y - con->rect.y + (i * 17);
161             child->deco_rect.width = child->rect.width;
162             child->deco_rect.height = 17;
163         }
164
165         printf("child at (%d, %d) with (%d x %d)\n",
166                 child->rect.x, child->rect.y, child->rect.width, child->rect.height);
167         printf("x now %d, y now %d\n", x, y);
168         x_raise_con(child);
169         render_con(child);
170         i++;
171     }
172
173     /* in a stacking container, we ensure the focused client is raised */
174     if (con->layout == L_STACKED) {
175         Con *foc = TAILQ_FIRST(&(con->focus_head));
176         if (foc != TAILQ_END(&(con->focus_head)))
177             x_raise_con(foc);
178     }
179
180     TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
181         LOG("render floating:\n");
182         LOG("floating child at (%d,%d) with %d x %d\n", child->rect.x, child->rect.y, child->rect.width, child->rect.height);
183         x_raise_con(child);
184         render_con(child);
185     }
186
187     printf("-- level up\n");
188 }