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 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, 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);
64 mask = XCB_CW_OVERRIDE_REDIRECT;
67 /* Open a new window, the resizebar. Grab the pointer and move the window around
68 as the user moves the pointer. */
69 xcb_window_t grabwin = create_window(conn, output->rect, XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
72 if (orientation == HORIZ) {
73 helprect.x = event->root_x;
74 helprect.y = output->rect.y;
76 helprect.height = output->rect.height;
77 new_position = event->root_x;
79 helprect.x = output->rect.x;
80 helprect.y = event->root_y;
81 helprect.width = output->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 == HORIZ ?
94 XCURSOR_CURSOR_RESIZE_HORIZONTAL :
95 XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
97 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
101 struct callback_params params = { orientation, output, helpwin, &new_position };
103 drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, ¶ms);
105 xcb_destroy_window(conn, helpwin);
106 xcb_destroy_window(conn, grabwin);
110 if (orientation == HORIZ)
111 pixels = (new_position - event->root_x);
112 else pixels = (new_position - event->root_y);
114 DLOG("Done, pixels = %d\n", pixels);
116 // if we got thus far, the containers must have
117 // percentages associated with them
118 assert(first->percent > 0.0);
119 assert(second->percent > 0.0);
121 // calculate the new percentage for the first container
122 double new_percent, difference;
123 double percent = first->percent;
124 DLOG("percent = %f\n", percent);
125 int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
126 DLOG("original = %d\n", original);
127 new_percent = (original + pixels) * (percent / original);
128 difference = percent - new_percent;
129 DLOG("difference = %f\n", difference);
130 DLOG("new percent = %f\n", new_percent);
131 first->percent = new_percent;
133 // calculate the new percentage for the second container
134 double s_percent = second->percent;
135 second->percent = s_percent + difference;
136 DLOG("second->percent = %f\n", second->percent);
138 // now we must make sure that the sum of the percentages remain 1.0
139 con_fix_percent(first->parent);