- win->start_x = win->prev->start_x + win->prev->width; // next best default: open new window column with it
- if (win->prev->height < win_meta->height) { // ... unless the previous window does not fill a whole column
- struct Win * last_ceiling;
- last_ceiling = win->prev;
- while (last_ceiling->start_y != 0 // determine last window serving as a
- && (last_ceiling->prev->start_y == last_ceiling->start_y // ceiling to other windows or filling
- || last_ceiling->prev->width > last_ceiling->width)) // the whole last column's width
- last_ceiling = last_ceiling->prev;
- if (win->prev == last_ceiling) {
- if (win->width <= win->prev->width
- && win->prev->start_y + win->prev->height + win->height <= win_meta->height) {
- win->start_x = win->prev->start_x; // if prev window is last ceiling, try to
- win->start_y = win->prev->start_y + win->prev->height; } } // fit window below it; else: use default
- else {
- int remaining_width = last_ceiling->width; // calculate free width remaining in last row of last
- struct Win * win_p = last_ceiling->next; // window column
- while (win != win_p) {
- remaining_width = remaining_width - win_p->width;
- win_p = win_p->next; }
- if (win->width <= remaining_width && win->height <= win->prev->height) { // if enough space left in
- win->start_y = win->prev->start_y; // last column, place window
- win->start_x = win->prev->start_x + win->prev->width; } // here
- else if (win->width <= last_ceiling->width
- && win->height + win->prev->height + win->prev->start_y <= win_meta->height ) {
- win->start_y = last_ceiling->next->start_y + last_ceiling->next->height; // else, try to put it
- win->start_x = last_ceiling->start_x; } // below
- else // else, put it next to max
- win->start_x = last_ceiling->width + last_ceiling->start_x; } } } } // width of the last last column
+ struct Win * win_top = win->prev;
+ while (getbegy(win_top->curses) != 1)
+ win_top = win_top->prev; // else, default to placing window in new top
+ start.x = getbegx(win_top->curses) + win_top->width + 1; // column to the right of the last one
+ uint16_t winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
+ if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
+ start.x = getbegx(win->prev->curses); // place window below previous window if it fits
+ start.y = winprev_maxy + 1; } // vertically and is not wider than its predecessor
+ else {
+ struct Win * win_up = win->prev;
+ struct Win * win_upup = win_up;
+ uint16_t widthdiff;
+ while (win_up != win_top) {
+ win_upup = win_up->prev;
+ while (1) {
+ if (getbegy(win_up->curses) != getbegy(win_upup->curses))
+ break;
+ win_upup = win_upup->prev; }
+ winprev_maxy = getbegy(win_upup->curses) + getmaxy(win_upup->curses);
+ widthdiff = (getbegx(win_upup->curses) + win_upup->width) - (getbegx(win_up->curses) + win_up->width);
+ if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
+ start.x = getbegx(win_up->curses) + win_up->width + 1; // else try to open new sub column under last
+ start.y = winprev_maxy + 1; // window below which enough space remains
+ break; }
+ win_up = win_upup; } } }
+ return start; }