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 xcb_screen_t *root_screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
40 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
42 LOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
46 LOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
48 LOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
53 mask = XCB_CW_OVERRIDE_REDIRECT;
56 /* Open a new window, the resizebar. Grab the pointer and move the window around
57 as the user moves the pointer. */
58 Rect grabrect = {0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
59 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
62 if (orientation == O_VERTICAL) {
63 helprect.x = event->root_x;
64 helprect.y = screen->rect.y;
66 helprect.height = screen->rect.height;
67 new_position = event->root_x;
70 helprect.y = event->root_y;
71 helprect.width = root_screen->width_in_pixels;
73 new_position = event->root_y;
76 mask = XCB_CW_BACK_PIXEL;
77 values[0] = config.client.focused.border;
79 mask |= XCB_CW_OVERRIDE_REDIRECT;
82 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
83 (orientation == O_VERTICAL ?
84 XCB_CURSOR_SB_V_DOUBLE_ARROW :
85 XCB_CURSOR_SB_H_DOUBLE_ARROW), true, mask, values);
87 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
91 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
92 LOG("new x = %d, y = %d\n", new_x, new_y);
93 if (orientation == O_VERTICAL) {
94 /* Check if the new coordinates are within screen boundaries */
95 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
96 new_x < (screen->rect.x + 25))
99 values[0] = new_position = new_x;
100 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_X, values);
102 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
103 new_y < (screen->rect.y + 25))
106 values[0] = new_position = new_y;
107 xcb_configure_window(conn, helpwin, XCB_CONFIG_WINDOW_Y, values);
113 drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback);
115 xcb_destroy_window(conn, helpwin);
116 xcb_destroy_window(conn, grabwin);
119 /* TODO: refactor this, both blocks are very identical */
120 if (orientation == O_VERTICAL) {
121 LOG("Resize was from X = %d to X = %d\n", event->root_x, new_position);
122 if (event->root_x == new_position) {
123 LOG("Nothing changed, not updating anything\n");
127 int default_width = ws->rect.width / ws->cols;
128 int old_unoccupied_x = get_unoccupied_x(ws);
130 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
131 * doing the resize */
132 int new_unoccupied_x = old_unoccupied_x;
134 if (old_unoccupied_x == 0)
135 old_unoccupied_x = ws->rect.width;
137 if (ws->width_factor[first] == 0)
138 new_unoccupied_x += default_width;
140 if (ws->width_factor[second] == 0)
141 new_unoccupied_x += default_width;
144 LOG("old = %d, new = %d\n", old_unoccupied_x, new_unoccupied_x);
146 /* If the space used for customly resized columns has changed we need to adapt the
147 * other customly resized columns, if any */
148 if (new_unoccupied_x != old_unoccupied_x)
149 for (int col = 0; col < ws->cols; col++) {
150 if (ws->width_factor[col] == 0)
153 LOG("Updating other column (%d) (current width_factor = %f)\n", col, ws->width_factor[col]);
154 ws->width_factor[col] = (ws->width_factor[col] * old_unoccupied_x) / new_unoccupied_x;
155 LOG("to %f\n", ws->width_factor[col]);
158 LOG("old_unoccupied_x = %d\n", old_unoccupied_x);
160 LOG("Updating first (before = %f)\n", ws->width_factor[first]);
161 /* Convert 0 (for default width_factor) to actual numbers */
162 if (ws->width_factor[first] == 0)
163 ws->width_factor[first] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
165 LOG("middle = %f\n", ws->width_factor[first]);
166 int old_width = ws->width_factor[first] * old_unoccupied_x;
167 LOG("first->width = %d, new_position = %d, event->root_x = %d\n", old_width, new_position, event->root_x);
168 ws->width_factor[first] *= (float)(old_width + (new_position - event->root_x)) / old_width;
169 LOG("-> %f\n", ws->width_factor[first]);
172 LOG("Updating second (before = %f)\n", ws->width_factor[second]);
173 if (ws->width_factor[second] == 0)
174 ws->width_factor[second] = ((float)ws->rect.width / ws->cols) / new_unoccupied_x;
175 LOG("middle = %f\n", ws->width_factor[second]);
176 old_width = ws->width_factor[second] * old_unoccupied_x;
177 LOG("second->width = %d, new_position = %d, event->root_x = %d\n", old_width, new_position, event->root_x);
178 ws->width_factor[second] *= (float)(old_width - (new_position - event->root_x)) / old_width;
179 LOG("-> %f\n", ws->width_factor[second]);
181 LOG("new unoccupied_x = %d\n", get_unoccupied_x(ws));
185 LOG("Resize was from X = %d to X = %d\n", event->root_y, new_position);
186 if (event->root_y == new_position) {
187 LOG("Nothing changed, not updating anything\n");
191 int default_height = ws->rect.height / ws->rows;
192 int old_unoccupied_y = get_unoccupied_y(ws);
194 /* We pre-calculate the unoccupied space to see if we need to adapt sizes before
195 * doing the resize */
196 int new_unoccupied_y = old_unoccupied_y;
198 if (old_unoccupied_y == 0)
199 old_unoccupied_y = ws->rect.height;
201 if (ws->height_factor[first] == 0)
202 new_unoccupied_y += default_height;
204 if (ws->height_factor[second] == 0)
205 new_unoccupied_y += default_height;
208 LOG("old = %d, new = %d\n", old_unoccupied_y, new_unoccupied_y);
210 /* If the space used for customly resized columns has changed we need to adapt the
211 * other customly resized columns, if any */
212 if (new_unoccupied_y != old_unoccupied_y)
213 for (int row = 0; row < ws->rows; row++) {
214 if (ws->height_factor[row] == 0)
217 LOG("Updating other column (%d) (current width_factor = %f)\n", row, ws->height_factor[row]);
218 ws->height_factor[row] = (ws->height_factor[row] * old_unoccupied_y) / new_unoccupied_y;
219 LOG("to %f\n", ws->height_factor[row]);
222 LOG("old_unoccupied_y = %d\n", old_unoccupied_y);
224 LOG("Updating first (before = %f)\n", ws->height_factor[first]);
225 /* Convert 0 (for default width_factor) to actual numbers */
226 if (ws->height_factor[first] == 0)
227 ws->height_factor[first] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
229 LOG("middle = %f\n", ws->height_factor[first]);
230 int old_height = ws->height_factor[first] * old_unoccupied_y;
231 LOG("first->width = %d, new_position = %d, event->root_x = %d\n", old_height, new_position, event->root_y);
232 ws->height_factor[first] *= (float)(old_height + (new_position - event->root_y)) / old_height;
233 LOG("-> %f\n", ws->height_factor[first]);
236 LOG("Updating second (before = %f)\n", ws->height_factor[second]);
237 if (ws->height_factor[second] == 0)
238 ws->height_factor[second] = ((float)ws->rect.height / ws->rows) / new_unoccupied_y;
239 LOG("middle = %f\n", ws->height_factor[second]);
240 old_height = ws->height_factor[second] * old_unoccupied_y;
241 LOG("second->width = %d, new_position = %d, event->root_x = %d\n", old_height, new_position, event->root_y);
242 ws->height_factor[second] *= (float)(old_height - (new_position - event->root_y)) / old_height;
243 LOG("-> %f\n", ws->height_factor[second]);
245 LOG("new unoccupied_y = %d\n", get_unoccupied_y(ws));