2 * vim:ts=4:sw=4:expandtab
6 #include "cmdparse.tab.h"
8 typedef enum { BEFORE, AFTER } position_t;
11 * This function detaches 'con' from its parent and inserts it either before or
15 static void insert_con_into(Con *con, Con *target, position_t position) {
16 Con *parent = target->parent;
17 /* We need to preserve the old con->parent. While it might still be used to
18 * insert the entry before/after it, we call the on_remove_child callback
19 * afterwards which might then close the con if it is empty. */
20 Con *old_parent = con->parent;
23 con_fix_percent(con->parent);
27 if (position == BEFORE) {
28 TAILQ_INSERT_BEFORE(target, con, nodes);
29 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
30 } else if (position == AFTER) {
31 TAILQ_INSERT_AFTER(&(parent->nodes_head), target, con, nodes);
32 TAILQ_INSERT_HEAD(&(parent->focus_head), con, focused);
35 /* Pretend the con was just opened with regards to size percent values.
36 * Since the con is moved to a completely different con, the old value
37 * does not make sense anyways. */
39 con_fix_percent(parent);
41 CALL(old_parent, on_remove_child);
45 * This function detaches 'con' from its parent and inserts it at the given
49 static void attach_to_workspace(Con *con, Con *ws) {
51 con_fix_percent(con->parent);
53 CALL(con->parent, on_remove_child);
57 TAILQ_INSERT_TAIL(&(ws->nodes_head), con, nodes);
58 TAILQ_INSERT_TAIL(&(ws->focus_head), con, focused);
60 /* Pretend the con was just opened with regards to size percent values.
61 * Since the con is moved to a completely different con, the old value
62 * does not make sense anyways. */
68 * Moves the current container in the given direction (TOK_LEFT, TOK_RIGHT,
69 * TOK_UP, TOK_DOWN from cmdparse.l)
72 void tree_move(int direction) {
73 DLOG("Moving in direction %d\n", direction);
74 /* 1: get the first parent with the same orientation */
77 if (con->type == CT_WORKSPACE) {
78 DLOG("Not moving workspace\n");
82 if (con->parent->type == CT_WORKSPACE && con_num_children(con->parent) == 1) {
83 DLOG("This is the only con on this workspace, not doing anything\n");
87 orientation_t o = (direction == TOK_LEFT || direction == TOK_RIGHT ? HORIZ : VERT);
89 Con *same_orientation = con_parent_with_orientation(con, o);
90 /* There is no parent container with the same orientation */
91 if (!same_orientation) {
92 if (con_is_floating(con)) {
93 /* this is a floating con, we just disable floating */
94 floating_disable(con, true);
97 if (con_inside_floating(con)) {
98 /* 'con' should be moved out of a floating container */
99 DLOG("Inside floating, moving to workspace\n");
100 attach_to_workspace(con, con_get_workspace(con));
103 DLOG("Force-changing orientation\n");
104 ws_force_orientation(con_get_workspace(con), o);
105 same_orientation = con_parent_with_orientation(con, o);
108 /* easy case: the move is within this container */
109 if (same_orientation == con->parent) {
110 DLOG("We are in the same container\n");
112 /* TODO: TAILQ_SWAP? */
113 if (direction == TOK_LEFT || direction == TOK_UP) {
114 if (!(swap = TAILQ_PREV(con, nodes_head, nodes)))
117 if (!con_is_leaf(swap)) {
118 insert_con_into(con, con_descend_focused(swap), AFTER);
122 /* the container right of the current one is a normal one. */
124 TAILQ_INSERT_BEFORE(swap, con, nodes);
125 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
127 if (!(swap = TAILQ_NEXT(con, nodes)))
130 if (!con_is_leaf(swap)) {
131 insert_con_into(con, con_descend_focused(swap), AFTER);
136 TAILQ_INSERT_AFTER(&(swap->parent->nodes_head), swap, con, nodes);
137 TAILQ_INSERT_HEAD(&(swap->parent->focus_head), con, focused);
143 /* this time, we have to move to another container */
144 /* This is the container *above* 'con' which is inside 'same_orientation' */
146 while (above->parent != same_orientation)
147 above = above->parent;
149 DLOG("above = %p\n", above);
152 if (direction == TOK_UP || direction == TOK_LEFT) {
154 next = TAILQ_PREV(above, nodes_head, nodes);
155 } else if (direction == TOK_DOWN || direction == TOK_RIGHT) {
157 next = TAILQ_NEXT(above, nodes);
160 /* special case: there is a split container in the direction we are moving
161 * to, so descend and append */
162 if (next && !con_is_leaf(next))
163 insert_con_into(con, con_descend_focused(next), AFTER);
165 insert_con_into(con, above, position);
168 /* We need to call con_focus() to fix the focus stack "above" the container
169 * we just inserted the focused container into (otherwise, the parent
170 * container(s) would still point to the old container(s)). */