- win->start_x = 0; // if window is first in chain, place it on top-left corner
- win->start_y = 0;
- if (0 != win->prev) {
- 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
-
-void update_windows (struct WinMeta * win_meta, struct Win * win) {
-// Update geometry of win and its next of kin. Before, destroy window, if visible. After, (re-)build it.
- if (0 != win->curses_win)
- destroy_window (win);
- place_window(win_meta, win);
- if (win->start_y + win->height < win_meta->height) // dependent on window position,
- win->border_down = 1; // append space for borders to be drawn
- else
- win->border_down = 0;
- if (win->start_x > 0)
- win->border_left = 1;
- else
- win->border_left = 0;
- win->curses_win = newwin(win->height + win->border_down, win->width + win->border_left, win->start_y, win->start_x - win->border_left);
- if (0 != win->next)
- update_windows (win_meta, win->next); }
-
-void destroy_window (struct Win * win) {
-// Undraw and delete window.
- undraw_window (win->curses_win);
- delwin(win->curses_win);
- win->curses_win = 0; }
-
-void draw_windows (struct WinMeta * win_meta, struct Win * win) {
-// Draw all windows from the current one on.
- draw_window(win_meta, win);
- if (0 != win->next)
- draw_windows (win_meta, win->next); }
-
-void draw_all_windows (struct WinMeta * win_meta) {
-// Draw all windows from the chain start on.
- draw_windows (win_meta, win_meta->chain_start); }
-
-void draw_window(struct WinMeta * win_meta, struct Win * win) {
-// Draw win's content, including border and title (the latter dependent on space available for it).
- char ls = '|';
- char rs = '|';
- char ts = '-';
- char bs = '-';
- char tl = '-';
- char tr = '+';
- char bl = '|';
- char br = '|';
- if (1 == win->border_down) {
- bl = '+';
- br = '+'; }
- if (1 == win->border_left)
- tl = '+';
- wborder(win->curses_win, ls, rs, ts, bs, tl, tr, bl, br);
- char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
- if (win->width > min_title_length_visible) {
- int title_length = strlen(win->title);
- int title_offset = (((win->width) - (title_length + 2)) / 2) + win->border_left; // + 2 is for decoration
- if (title_offset < win->border_left)
- title_offset = win->border_left;
- int length_visible = strnlen(win->title, win->width - min_title_length_visible);
+ w->start.x = 0; // if window is first in chain, place it on top-left corner
+ w->start.y = 1;
+ if (0 != w->prev) {
+ struct Win * w_top = w->prev;
+ while (w_top->start.y != 1)
+ w_top = w_top->prev; // else, default to placing window in new top
+ w->start.x = w_top->start.x + w_top->frame.size.x + 1; // column to the right of the last one
+ uint16_t w_prev_maxy = w->prev->start.y + getmaxy(w->prev->frame.curses_win);
+ if (w->frame.size.x <= w->prev->frame.size.x && w->frame.size.y < wmeta->pad.size.y - w_prev_maxy) {
+ w->start.x = w->prev->start.x; // place window below previous window if it fits
+ w->start.y = w_prev_maxy + 1; } // vertically and is not wider than its predecessor
+ else {
+ struct Win * w_up = w->prev;
+ struct Win * w_upup = w_up;
+ uint16_t widthdiff;
+ while (w_up != w_top) {
+ w_upup = w_up->prev;
+ while (1) {
+ if (w_up->start.y != w_upup->start.y)
+ break;
+ w_upup = w_upup->prev; }
+ w_prev_maxy = w_upup->start.y + getmaxy(w_upup->frame.curses_win);
+ widthdiff = (w_upup->start.x + w_upup->frame.size.x) - (w_up->start.x + w_up->frame.size.x);
+ if (w->frame.size.y < wmeta->pad.size.y - w_prev_maxy && w->frame.size.x < widthdiff) {
+ w->start.x = w_up->start.x + w_up->frame.size.x + 1 ; // else try to open new sub column under
+ w->start.y = w_prev_maxy + 1; // last window below which enough space remains
+ break; }
+ w_up = w_upup; } } } }
+
+static void update_wins (struct WinMeta * wmeta, struct Win * w) {
+// Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
+ if (0 != w->frame.curses_win)
+ destroy_win (w);
+ place_win(wmeta, w);
+ refit_pad(wmeta);
+ w->frame.curses_win=subpad(wmeta->pad.curses_win, w->frame.size.y, w->frame.size.x, w->start.y, w->start.x);
+ if (0 != w->next)
+ update_wins (wmeta, w->next); }
+
+static void destroy_win (struct Win * w) {
+// Delete window.
+ delwin(w->frame.curses_win);
+ w->frame.curses_win = 0; }
+
+static void draw_win_borders (struct Win * w, char active) {
+// Draw borders of window win, including title. Decorate in a special way if window is marked as active.
+ uint16_t y, x;
+ for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++) {
+ mvwaddch(wgetparent(w->frame.curses_win), y, w->start.x - 1, '|');
+ mvwaddch(wgetparent(w->frame.curses_win), y, w->start.x + w->frame.size.x, '|'); }
+ for (x = w->start.x; x <= w->start.x + w->frame.size.x; x++) {
+ mvwaddch(wgetparent(w->frame.curses_win), w->start.y - 1, x, '-');
+ mvwaddch(wgetparent(w->frame.curses_win), w->start.y + w->frame.size.y, x, '-'); }
+ char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
+ if (w->frame.size.x >= min_title_length_visible) {
+ uint16_t title_offset = 0;
+ if (w->frame.size.x > strlen(w->title) + 2)
+ title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2; // + 2 is for decoration
+ uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);