- 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.
- if (win_meta->chain_start)
- 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 < win_meta->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 < win_meta->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_windows (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_window (w);
+ place_window(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_windows (wmeta, w->next); }
+
+static void destroy_window (struct Win * win) {
+// Delete window.
+ delwin(win->frame.curses_win);
+ win->frame.curses_win = 0; }
+
+static void draw_win_borders (struct Win * win, 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 = win->start.y; y <= win->start.y + win->frame.size.y; y++) {
+ mvwaddch(wgetparent(win->frame.curses_win), y, win->start.x - 1, '|');
+ mvwaddch(wgetparent(win->frame.curses_win), y, win->start.x + win->frame.size.x, '|'); }
+ for (x = win->start.x; x <= win->start.x + win->frame.size.x; x++) {
+ mvwaddch(wgetparent(win->frame.curses_win), win->start.y - 1, x, '-');
+ mvwaddch(wgetparent(win->frame.curses_win), win->start.y + win->frame.size.y, x, '-'); }
+ char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
+ if (win->frame.size.x >= min_title_length_visible) {
+ uint16_t title_offset = 0;
+ if (win->frame.size.x > strlen(win->title) + 2)
+ title_offset = (win->frame.size.x - (strlen(win->title) + 2)) / 2; // + 2 is for decoration
+ uint16_t length_visible = strnlen(win->title, win->frame.size.x - 2);