2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * resize.c: Interactive resizing.
12 extern xcb_connection_t *conn;
15 * This is an ugly data structure which we need because there is no standard
16 * way of having nested functions (only available as a gcc extension at the
17 * moment, clang doesn’t support it) or blocks (only available as a clang
18 * extension and only on Mac OS X systems at the moment).
21 struct callback_params {
22 orientation_t orientation;
25 uint32_t *new_position;
28 DRAGGING_CB(resize_callback) {
29 const struct callback_params *params = extra;
30 Con *output = params->output;
31 DLOG("new x = %d, y = %d\n", new_x, new_y);
32 if (params->orientation == HORIZ) {
33 /* Check if the new coordinates are within screen boundaries */
34 if (new_x > (output->rect.x + output->rect.width - 25) ||
35 new_x < (output->rect.x + 25))
38 *(params->new_position) = new_x;
39 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
41 if (new_y > (output->rect.y + output->rect.height - 25) ||
42 new_y < (output->rect.y + 25))
45 *(params->new_position) = new_y;
46 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
52 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
53 DLOG("resize handler\n");
55 uint32_t new_position;
57 /* TODO: previously, we were getting a rect containing all screens. why? */
58 Con *output = con_get_output(first);
59 DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
61 x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
67 mask = XCB_CW_OVERRIDE_REDIRECT;
70 /* Open a new window, the resizebar. Grab the pointer and move the window around
71 as the user moves the pointer. */
72 xcb_window_t grabwin = create_window(conn, output->rect, XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
75 if (orientation == HORIZ) {
76 helprect.x = event->root_x;
77 helprect.y = output->rect.y;
79 helprect.height = output->rect.height;
80 new_position = event->root_x;
82 helprect.x = output->rect.x;
83 helprect.y = event->root_y;
84 helprect.width = output->rect.width;
86 new_position = event->root_y;
89 mask = XCB_CW_BACK_PIXEL;
90 values[0] = config.client.focused.border;
92 mask |= XCB_CW_OVERRIDE_REDIRECT;
95 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
96 (orientation == HORIZ ?
97 XCURSOR_CURSOR_RESIZE_HORIZONTAL :
98 XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
100 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
104 const struct callback_params params = { orientation, output, helpwin, &new_position };
106 drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, ¶ms);
108 xcb_destroy_window(conn, helpwin);
109 xcb_destroy_window(conn, grabwin);
113 if (orientation == HORIZ)
114 pixels = (new_position - event->root_x);
115 else pixels = (new_position - event->root_y);
117 DLOG("Done, pixels = %d\n", pixels);
119 // if we got thus far, the containers must have
120 // percentages associated with them
121 assert(first->percent > 0.0);
122 assert(second->percent > 0.0);
124 // calculate the new percentage for the first container
125 double new_percent, difference;
126 double percent = first->percent;
127 DLOG("percent = %f\n", percent);
128 int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
129 DLOG("original = %d\n", original);
130 new_percent = (original + pixels) * (percent / original);
131 difference = percent - new_percent;
132 DLOG("difference = %f\n", difference);
133 DLOG("new percent = %f\n", new_percent);
134 first->percent = new_percent;
136 // calculate the new percentage for the second container
137 double s_percent = second->percent;
138 second->percent = s_percent + difference;
139 DLOG("second->percent = %f\n", second->percent);
141 // now we must make sure that the sum of the percentages remain 1.0
142 con_fix_percent(first->parent);