2 * vim:ts=4:sw=4:expandtab
6 extern xcb_connection_t *conn;
9 * This is an ugly data structure which we need because there is no standard
10 * way of having nested functions (only available as a gcc extension at the
11 * moment, clang doesn’t support it) or blocks (only available as a clang
12 * extension and only on Mac OS X systems at the moment).
15 struct callback_params {
16 orientation_t orientation;
19 uint32_t *new_position;
22 DRAGGING_CB(resize_callback) {
23 struct callback_params *params = extra;
24 Con *output = params->output;
25 DLOG("new x = %d, y = %d\n", new_x, new_y);
26 if (params->orientation == HORIZ) {
27 /* Check if the new coordinates are within screen boundaries */
28 if (new_x > (output->rect.x + output->rect.width - 25) ||
29 new_x < (output->rect.x + 25))
32 *(params->new_position) = new_x;
33 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_X, params->new_position);
35 if (new_y > (output->rect.y + output->rect.height - 25) ||
36 new_y < (output->rect.y + 25))
39 *(params->new_position) = new_y;
40 xcb_configure_window(conn, params->helpwin, XCB_CONFIG_WINDOW_Y, params->new_position);
46 int resize_graphical_handler(Con *first, Con *second, orientation_t orientation, xcb_button_press_event_t *event) {
47 DLOG("resize handler\n");
49 uint32_t new_position;
51 /* TODO: previously, we were getting a rect containing all screens. why? */
52 Con *output = con_get_output(first);
53 DLOG("x = %d, width = %d\n", output->rect.x, output->rect.width);
58 mask = XCB_CW_OVERRIDE_REDIRECT;
61 /* Open a new window, the resizebar. Grab the pointer and move the window around
62 as the user moves the pointer. */
63 xcb_window_t grabwin = create_window(conn, output->rect, XCB_WINDOW_CLASS_INPUT_ONLY, XCURSOR_CURSOR_POINTER, true, mask, values);
66 if (orientation == HORIZ) {
67 helprect.x = event->root_x;
68 helprect.y = output->rect.y;
70 helprect.height = output->rect.height;
71 new_position = event->root_x;
73 helprect.x = output->rect.x;
74 helprect.y = event->root_y;
75 helprect.width = output->rect.width;
77 new_position = event->root_y;
80 mask = XCB_CW_BACK_PIXEL;
81 values[0] = config.client.focused.border;
83 mask |= XCB_CW_OVERRIDE_REDIRECT;
86 xcb_window_t helpwin = create_window(conn, helprect, XCB_WINDOW_CLASS_INPUT_OUTPUT,
87 (orientation == HORIZ ?
88 XCURSOR_CURSOR_RESIZE_HORIZONTAL :
89 XCURSOR_CURSOR_RESIZE_VERTICAL), true, mask, values);
91 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, helpwin);
95 struct callback_params params = { orientation, output, helpwin, &new_position };
97 drag_pointer(NULL, event, grabwin, BORDER_TOP, resize_callback, ¶ms);
99 xcb_destroy_window(conn, helpwin);
100 xcb_destroy_window(conn, grabwin);
104 if (orientation == HORIZ)
105 pixels = (new_position - event->root_x);
106 else pixels = (new_position - event->root_y);
108 DLOG("Done, pixels = %d\n", pixels);
110 // if we got thus far, the containers must have
111 // percentages associated with them
112 assert(first->percent > 0.0);
113 assert(second->percent > 0.0);
115 // calculate the new percentage for the first container
116 double new_percent, difference;
117 double percent = first->percent;
118 DLOG("percent = %f\n", percent);
119 int original = (orientation == HORIZ ? first->rect.width : first->rect.height);
120 DLOG("original = %d\n", original);
121 new_percent = (original + pixels) * (percent / original);
122 difference = percent - new_percent;
123 DLOG("difference = %f\n", difference);
124 DLOG("new percent = %f\n", new_percent);
125 first->percent = new_percent;
127 // calculate the new percentage for the second container
128 double s_percent = second->percent;
129 second->percent = s_percent + difference;
130 DLOG("second->percent = %f\n", second->percent);
132 // now we must make sure that the sum of the percentages remain 1.0
133 con_fix_percent(first->parent);