2 #define I3__FILE__ "resize.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
9 * resize.c: Interactive resizing.
14 extern xcb_connection_t *conn;
17 * This is an ugly data structure which we need because there is no standard
18 * way of having nested functions (only available as a gcc extension at the
19 * moment, clang doesn’t support it) or blocks (only available as a clang
20 * extension and only on Mac OS X systems at the moment).
23 struct callback_params {
24 orientation_t orientation;
27 uint32_t *new_position;
30 DRAGGING_CB(resize_callback) {
31 const struct callback_params *params = extra;
32 Con *output = params->output;
33 DLOG("new x = %d, y = %d\n", new_x, new_y);
34 if (params->orientation == HORIZ) {
35 /* Check if the new coordinates are within screen boundaries */
36 if (new_x > (output->rect.x + output->rect.width - 25) ||
37 new_x < (output->rect.x + 25))
40 *(params->new_position) = new_x;
41 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
43 if (new_y > (output->rect.y + output->rect.height - 25) ||
44 new_y < (output->rect.y + 25))
47 *(params->new_position) = new_y;
48 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
54 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, const xcb_button_press_event_t *event) {
55 DLOG("resize handler\n");
57 uint32_t new_position;
59 /* TODO: previously, we were getting a rect containing all screens. why? */
60 Con *output = con_get_output(first);
61 DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
63 x_mask_event_mask(~XCB_EVENT_MASK_ENTER_WINDOW);
69 mask = XCB_CW_OVERRIDE_REDIRECT;
72 /* Open a new window, the resizebar. Grab the pointer and move the window around
73 as the user moves the pointer. */
74 xcb_window_t grabwin = create_window(conn, output->rect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
75 XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
78 if (orientation == HORIZ) {
79 helprect.x = event->root_x;
80 helprect.y = output->rect.y;
82 helprect.height = output->rect.height;
83 new_position = event->root_x;
85 helprect.x = output->rect.x;
86 helprect.y = event->root_y;
87 helprect.width = output->rect.width;
89 new_position = event->root_y;
92 mask = XCB_CW_BACK_PIXEL;
93 values[0] = config.client.focused.border;
95 mask |= XCB_CW_OVERRIDE_REDIRECT;
98 xcb_window_t helpwin = create_window(conn, helprect, XCB_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
99 XCB_WINDOW_CLASS_INPUT_OUTPUT, (orientation == HORIZ ?
100 XCURSOR_CURSOR_RESIZE_HORIZONTAL :
101 XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
103 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
107 const struct callback_params params = { orientation, output, helpwin, &new_position };
109 drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, ¶ms);
111 xcb_destroy_window(conn, helpwin);
112 xcb_destroy_window(conn, grabwin);
116 if (orientation == HORIZ)
117 pixels = (new_position - event->root_x);
118 else pixels = (new_position - event->root_y);
120 DLOG("Done, pixels = %d\n", pixels);
122 // if we got thus far, the containers must have
123 // percentages associated with them
124 assert(first->percent > 0.0);
125 assert(second->percent > 0.0);
127 // calculate the new percentage for the first container
128 double new_percent, difference;
129 double percent = first->percent;
130 DLOG("percent = %f\n", percent);
131 int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
132 DLOG("original = %d\n", original);
133 new_percent = (original + pixels) * (percent / original);
134 difference = percent - new_percent;
135 DLOG("difference = %f\n", difference);
136 DLOG("new percent = %f\n", new_percent);
137 first->percent = new_percent;
139 // calculate the new percentage for the second container
140 double s_percent = second->percent;
141 second->percent = s_percent + difference;
142 DLOG("second->percent = %f\n", second->percent);
144 // now we must make sure that the sum of the percentages remain 1.0
145 con_fix_percent(first->parent);