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 /* If the space used for customly resized columns has changed we need to adapt the
169 * other customly resized columns, if any */
170 if (new_unoccupied_x != old_unoccupied_x)
171 for (int col = 0; col < ws->cols; col++) {
172 if (ws->width_factor[col] == 0)
175 LOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
176 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
177 LOG("to %f\n", ws->width_factor[col]);
180 LOG("old_unoccupied_x = %d\n", old_unoccupied_x);
182 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
183 /* Convert 0 (for default width_factor) to actual numbers */
184 if (ws->width_factor[first] == 0)
185 ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
187 LOG("middle = %f\n", ws->width_factor[first]);
188 int old_width = ws->width_factor[first] * old_unoccupied_x;
189 LOG("first->width = %d, pixels = %d\n", pixels);
190 ws->width_factor[first] *= (float)(old_width + pixels) / old_width;
191 LOG("-> %f\n", ws->width_factor[first]);
194 LOG("Updating second (before = %f)\n", ws->width_factor[second]);
195 if (ws->width_factor[second] == 0)
196 ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
197 LOG("middle = %f\n", ws->width_factor[second]);
198 old_width = ws->width_factor[second] * old_unoccupied_x;
199 LOG("second->width = %d, pixels = %d\n", pixels);
200 ws->width_factor[second] *= (float)(old_width - pixels) / old_width;
201 LOG("-> %f\n", ws->width_factor[second]);
203 LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
207 int default_height = ws->rect.height / ws->rows;
208 int old_unoccupied_y = get_unoccupied_y(ws);
210 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
211 * doing the resize */
212 int new_unoccupied_y = old_unoccupied_y;
214 if (old_unoccupied_y == 0)
215 old_unoccupied_y = ws->rect.height;
217 if (ws->height_factor[first] == 0)
218 new_unoccupied_y += default_height;
220 if (ws->height_factor[second] == 0)
221 new_unoccupied_y += default_height;
224 LOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
226 /* If the space used for customly resized columns has changed we need to adapt the
227 * other customly resized columns, if any */
228 if (new_unoccupied_y != old_unoccupied_y)
229 for (int row = 0; row < ws->rows; row++) {
230 if (ws->height_factor[row] == 0)
233 LOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
234 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
235 LOG("to %f\n", ws->height_factor[row]);
238 LOG("old_unoccupied_y = %d\n", old_unoccupied_y);
240 LOG("Updating first (before = %f)\n", ws->height_factor[first]);
241 /* Convert 0 (for default width_factor) to actual numbers */
242 if (ws->height_factor[first] == 0)
243 ws->height_factor[first] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
245 LOG("middle = %f\n", ws->height_factor[first]);
246 int old_height = ws->height_factor[first] * old_unoccupied_y;
247 LOG("first->width = %d, pixels = %d\n", pixels);
248 ws->height_factor[first] *= (float)(old_height + pixels) / old_height;
249 LOG("-> %f\n", ws->height_factor[first]);
252 LOG("Updating second (before = %f)\n", ws->height_factor[second]);
253 if (ws->height_factor[second] == 0)
254 ws->height_factor[second] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
255 LOG("middle = %f\n", ws->height_factor[second]);
256 old_height = ws->height_factor[second] * old_unoccupied_y;
257 LOG("second->width = %d, pixels = %d\n", pixels);
258 ws->height_factor[second] *= (float)(old_height - pixels) / old_height;
259 LOG("-> %f\n", ws->height_factor[second]);
261 LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));