4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 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.
29 #include "workspace.h"
33 * This is an ugly data structure which we need because there is no standard
34 * way of having nested functions (only available as a gcc extension at the
35 * moment, clang doesn’t support it) or blocks (only available as a clang
36 * extension and only on Mac OS X systems at the moment).
39 struct callback_params {
40 resize_orientation_t orientation;
43 uint32_t *new_position;
46 DRAGGING_CB(resize_callback) {
47 struct callback_params *params = extra;
48 Output *screen = params->screen;
49 DLOG("new x = %d, y = %d\n", new_x, new_y);
50 if (params->orientation == O_VERTICAL) {
51 /* Check if the new coordinates are within screen boundaries */
52 if (new_x > (screen->rect.x + screen->rect.width - 25) ||
53 new_x < (screen->rect.x + 25))
56 *(params->new_position) = new_x;
57 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
59 if (new_y > (screen->rect.y + screen->rect.height - 25) ||
60 new_y < (screen->rect.y + 25))
63 *(params->new_position) = new_y;
64 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
71 * Renders the resize window between the first/second container and resizes
72 * the table column/row.
75 int resize_graphical_handler(xcb_connection_t *conn, Workspace *ws, int first, int second,
76 resize_orientation_t orientation, xcb_button_press_event_t *event) {
77 uint32_t new_position;
78 Output *screen = get_output_containing(event->root_x, event->root_y);
80 ELOG("BUG: No screen found at this position (%d, %d)\n", event->root_x, event->root_y);
84 /* We cannot use the X root window's width_in_pixels or height_in_pixels
85 * attributes here since they are not updated when you configure new
86 * screens during runtime. Instead, we just use the most right and most
87 * bottom Xinerama screen and use their position + width/height to get
88 * the area of pixels currently in use */
89 Output *most_right = get_output_most(D_RIGHT, screen),
90 *most_bottom = get_output_most(D_DOWN, screen);
92 DLOG("event->event_x = %d, event->root_x = %d\n", event->event_x, event->root_x);
94 DLOG("Screen dimensions: (%d, %d) %d x %d\n", screen->rect.x, screen->rect.y, screen->rect.width, screen->rect.height);
99 mask = XCB_CW_OVERRIDE_REDIRECT;
102 /* Open a new window, the resizebar. Grab the pointer and move the window around
103 as the user moves the pointer. */
106 most_right->rect.x + most_right->rect.width,
107 most_bottom->rect.x + most_bottom->rect.height};
108 xcb_window_t grabwin = create_window(conn, grabrect, XCB_WINDOW_CLASS_INPUT_ONLY, -1, true, mask, values);
111 if (orientation == O_VERTICAL) {
112 helprect.x = event->root_x;
113 helprect.y = screen->rect.y;
115 helprect.height = screen->rect.height;
116 new_position = event->root_x;
118 helprect.x = screen->rect.x;
119 helprect.y = event->root_y;
120 helprect.width = screen->rect.width;
122 new_position = event->root_y;
125 mask = XCB_CW_BACK_PIXEL;
126 values[0] = config.client.focused.border;
128 mask |= XCB_CW_OVERRIDE_REDIRECT;
131 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
132 (orientation == O_VERTICAL ?
133 XCB_CURSOR_SB_H_DOUBLE_ARROW :
134 XCB_CURSOR_SB_V_DOUBLE_ARROW), true, mask, values);
136 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
140 struct callback_params params = { orientation, screen, helpwin, &new_position };
142 drag_pointer(conn, NULL, event, grabwin, BORDER_TOP, resize_callback, ¶ms);
144 xcb_destroy_window(conn, helpwin);
145 xcb_destroy_window(conn, grabwin);
149 if (orientation == O_VERTICAL)
150 pixels = (new_position - event->root_x);
151 else pixels = (new_position - event->root_y);
152 resize_container(conn, ws, first, second, orientation, pixels);
158 * Adjusts the container size factors according to the resizing parameters.
159 * This is an abstraction used by resize_container.
161 static void adjust_container_factors(float *factors, int ws_size, int unoccupied_size,
162 int num_items, int first, int second, int pixels) {
163 /* Find the current sizes */
164 int sizes[num_items];
165 for (int i = 0; i < num_items; ++i)
166 sizes[i] = factors[i] == 0 ? ws_size / num_items : unoccupied_size * factors[i];
169 sizes[first] += pixels;
170 sizes[second] -= pixels;
172 /* Calculate the new unoccupied size */
173 if (factors[first] == 0) unoccupied_size += ws_size / num_items;
174 if (factors[second] == 0) unoccupied_size += ws_size / num_items;
176 /* Calculate the new factors */
177 for (int i = 0; i < num_items; ++i) {
178 if (factors[i] != 0 || i == first || i == second)
179 factors[i] = (float)sizes[i] / unoccupied_size;
184 * Resizes a column/row by the given amount of pixels. Called by
185 * resize_graphical_handler (the user clicked) or parse_resize_command (the
186 * user issued the command)
189 void resize_container(xcb_connection_t *conn, Workspace *ws, int first, int second,
190 resize_orientation_t orientation, int pixels) {
191 if (orientation == O_VERTICAL) {
192 adjust_container_factors(ws->width_factor, ws->rect.width,
193 get_unoccupied_x(ws), ws->cols, first, second, pixels);
196 adjust_container_factors(ws->height_factor, workspace_height(ws),
197 get_unoccupied_y(ws), ws->rows, first, second, pixels);