4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * This file contains the functions for resizing table columns/rows because
11 * it’s actually lots of work, compared to the other handlers.
18 #include <xcb/xcb_event.h>
32 * Renders the resize window between the first/second container and resizes
33 * the table column/row.
36 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
37 resize_orientation_t orientation, xcb_button_press_event_t *event) {
39 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
41 LOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
45 /* We cannot use the X root window's width_in_pixels or height_in_pixels
46 * attributes here since they are not updated when you configure new
47 * screens during runtime. Instead, we just use the most right and most
48 * bottom Xinerama screen and use their position + width/height to get
49 * the area of pixels currently in use */
50 i3Screen *most_right = get_screen_most(D_RIGHT, screen),
51 *most_bottom = get_screen_most(D_DOWN, screen);
53 LOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
55 LOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
60 mask = XCB_CW_OVERRIDE_REDIRECT;
63 /* Open a new window, the resizebar. Grab the pointer and move the window around
64 as the user moves the pointer. */
67 most_right->rect.x + most_right->rect.width,
68 most_bottom->rect.x + most_bottom->rect.height};
69 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
72 if (orientation == O_VERTICAL) {
73 helprect.x = event->root_x;
74 helprect.y = screen->rect.y;
76 helprect.height = screen->rect.height;
77 new_position = event->root_x;
79 helprect.x = screen->rect.x;
80 helprect.y = event->root_y;
81 helprect.width = screen->rect.width;
83 new_position = event->root_y;
86 mask = XCB_CW_BACK_PIXEL;
87 values[0] = config.client.focused.border;
89 mask |= XCB_CW_OVERRIDE_REDIRECT;
92 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
93 (orientation == O_VERTICAL ?
94 XCB_CURSOR_SB_V_DOUBLE_ARROW :
95 XCB_CURSOR_SB_H_DOUBLE_ARROW), true, mask, values);
97 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
101 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
102 LOG("new x = %d, y = %d\n", new_x, new_y);
103 if (orientation == O_VERTICAL) {
104 /* Check if the new coordinates are within screen boundaries */
105 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
106 new_x < (screen->rect.x + 25))
109 values[0] = new_position = new_x;
110 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
112 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
113 new_y < (screen->rect.y + 25))
116 values[0] = new_position = new_y;
117 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
123 drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback);
125 xcb_destroy_window(conn, helpwin);
126 xcb_destroy_window(conn, grabwin);
130 if (orientation == O_VERTICAL)
131 pixels = (new_position - event->root_x);
132 else pixels = (new_position - event->root_y);
133 resize_container(conn, ws, first, second, orientation, pixels);
139 * Resizes a column/row by the given amount of pixels. Called by
140 * resize_graphical_handler (the user clicked) or parse_resize_command (the
141 * user issued the command)
144 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
145 resize_orientation_t orientation, int pixels) {
147 /* TODO: refactor this, both blocks are very identical */
148 if (orientation == O_VERTICAL) {
149 int default_width = ws->rect.width / ws->cols;
150 int old_unoccupied_x = get_unoccupied_x(ws);
152 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
153 * doing the resize */
154 int new_unoccupied_x = old_unoccupied_x;
156 if (old_unoccupied_x == 0)
157 old_unoccupied_x = ws->rect.width;
159 if (ws->width_factor[first] == 0)
160 new_unoccupied_x += default_width;
162 if (ws->width_factor[second] == 0)
163 new_unoccupied_x += default_width;
166 LOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
168 int cols_without_wf = 0;
169 int old_width, old_second_width;
170 for (int col = 0; col < ws->cols; col++)
171 if (ws->width_factor[col] == 0)
174 LOG("old_unoccupied_x = %d\n", old_unoccupied_x);
176 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
177 /* Convert 0 (for default width_factor) to actual numbers */
178 if (ws->width_factor[first] == 0)
179 old_width = (old_unoccupied_x / max(cols_without_wf, 1));
180 else old_width = ws->width_factor[first] * old_unoccupied_x;
182 LOG("second (before = %f)\n", ws->width_factor[second]);
183 if (ws->width_factor[second] == 0)
184 old_second_width = (old_unoccupied_x / max(cols_without_wf, 1));
185 else old_second_width = ws->width_factor[second] * old_unoccupied_x;
187 LOG("middle = %f\n", ws->width_factor[first]);
189 /* If the space used for customly resized columns has changed we need to adapt the
190 * other customly resized columns, if any */
191 if (new_unoccupied_x != old_unoccupied_x)
192 for (int col = 0; col < ws->cols; col++) {
193 if (ws->width_factor[col] == 0)
196 LOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
197 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
198 LOG("to %f\n", ws->width_factor[col]);
201 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
202 /* Convert 0 (for default width_factor) to actual numbers */
203 if (ws->width_factor[first] == 0)
204 ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
206 LOG("first->width = %d, pixels = %d\n", old_width, pixels);
207 ws->width_factor[first] *= (float)(old_width + pixels) / old_width;
208 LOG("-> %f\n", ws->width_factor[first]);
211 LOG("Updating second (before = %f)\n", ws->width_factor[second]);
212 if (ws->width_factor[second] == 0)
213 ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
215 LOG("middle = %f\n", ws->width_factor[second]);
216 LOG("second->width = %d, pixels = %d\n", old_second_width, pixels);
217 ws->width_factor[second] *= (float)(old_second_width - pixels) / old_second_width;
218 LOG("-> %f\n", ws->width_factor[second]);
220 LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
224 int default_height = ws->rect.height / ws->rows;
225 int old_unoccupied_y = get_unoccupied_y(ws);
227 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
228 * doing the resize */
229 int new_unoccupied_y = old_unoccupied_y;
231 if (old_unoccupied_y == 0)
232 old_unoccupied_y = ws->rect.height;
234 if (ws->height_factor[first] == 0)
235 new_unoccupied_y += default_height;
237 if (ws->height_factor[second] == 0)
238 new_unoccupied_y += default_height;
241 LOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
243 /* If the space used for customly resized columns has changed we need to adapt the
244 * other customly resized columns, if any */
245 if (new_unoccupied_y != old_unoccupied_y)
246 for (int row = 0; row < ws->rows; row++) {
247 if (ws->height_factor[row] == 0)
250 LOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
251 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
252 LOG("to %f\n", ws->height_factor[row]);
255 LOG("old_unoccupied_y = %d\n", old_unoccupied_y);
257 LOG("Updating first (before = %f)\n", ws->height_factor[first]);
258 /* Convert 0 (for default width_factor) to actual numbers */
259 if (ws->height_factor[first] == 0)
260 ws->height_factor[first] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
262 LOG("middle = %f\n", ws->height_factor[first]);
263 int old_height = ws->height_factor[first] * old_unoccupied_y;
264 LOG("first->width = %d, pixels = %d\n", pixels);
265 ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
266 LOG("-> %f\n", ws->height_factor[first]);
269 LOG("Updating second (before = %f)\n", ws->height_factor[second]);
270 if (ws->height_factor[second] == 0)
271 ws->height_factor[second] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
272 LOG("middle = %f\n", ws->height_factor[second]);
273 old_height = ws->height_factor[second] * old_unoccupied_y;
274 LOG("second->width = %d, pixels = %d\n", pixels);
275 ws->height_factor[second] *= (float)(old_height - pixels) / old_height;
276 LOG("-> %f\n", ws->height_factor[second]);
278 LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));