4 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t, UINT16_MAX */
5 #include <ncurses.h> /* for typedefs WINDOW, chtype, wresize(), getmaxx(), */
6 /* getmaxy(), delwin(), mvwaddch(), mvwaddstr(), */
7 /* newpad(), wnoutrefres(), erase(), werase(), */
8 /* pnoutrefresh(), doupdate(), getmaxyx() */
9 #include <stdlib.h> /* for malloc(), free() */
10 #include <string.h> /* for strlen(), strnlen(), memcpy() */
11 #include "yx_uint16.h" /* for struct yx_uint16 */
12 #include "misc.h" /* for center_offset() */
16 /* Fit virtual screen's width to minimum width demanded by current windows'
19 static uint8_t refit_pad(struct WinMeta * wmeta);
23 /* Update geometry (sizes, positions) of window "w" and its successors in the
24 * window chain. For the positioning algorithm place_win() is used.
26 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w);
27 static void place_win(struct WinMeta * wmeta, struct Win * w);
31 /* Draw scroll hint (a line stating that there are "dist" more elements of
32 * "unit" further into the direction symbolized by the "dir" char) into virtual
33 * screen pad, onto an appropriate edge of "frame": the left or right edge if
34 * "dir" is "<" or ">", or the upper or lower edge if it is "^" or "v". "start"
35 * should be either the start coordinate of "frame" if it describes a window or
36 * .y=, .x=wm->pad_offset if it describes the virtual screen. winscroll_hint()
37 * and padscroll_hint() are wrappers to simplify the use of scroll_hint().
39 static void scroll_hint(struct WinMeta * wm, struct Frame * frame, char dir,
40 uint16_t dist, char * unit, struct yx_uint16 start);
41 static void winscroll_hint(struct WinMeta * wm, struct Win * w, char dir,
43 static void padscroll_hint(struct WinMeta * wm, char dir, uint16_t dist);
47 /* Draw contents of all windows in window chain from window "w" onwards. */
48 static uint8_t draw_wins(struct WinMeta * wm, struct Win * w);
52 /* draw_win_borderlines() draws the vertical and horizontal borders of window
53 * "w" sans corners into the virtual screen "pad", and draws the top border
54 * line as the windows' title bar (highlighted if the window is described
55 * active by "active" being == 1).
57 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
58 * windows from "w" on. "w_active" is a pointer to the one window that
59 * draw_win_borderlines() is supposed to handle as the active window.
61 * Finally, draw_wins_bordercorners() draws into "pad" the borders of window "w"
62 * and all its successors.
64 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
65 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
67 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
71 /* Shift active window forwards / backwards in window chain. */
72 static void shift_win_forward(struct WinMeta * wmeta);
73 static void shift_win_backward(struct WinMeta * wmeta);
77 static uint8_t refit_pad(struct WinMeta * wmeta)
79 /* Determine rightmost window column. */
80 uint32_t lastwincol = 0;
81 struct Win * w_p = wmeta->chain_start;
84 if ((uint32_t) w_p->start.x + (uint32_t) w_p->frame.size.x
87 lastwincol = (uint32_t) w_p->start.x
88 + (uint32_t) w_p->frame.size.x - 1;
93 /* Only resize the pad if the rightmost window column has changed. */
94 if (getmaxx(wmeta->padframe.curses_win) + 1 != lastwincol)
96 if (lastwincol + 2 > UINT16_MAX)
100 return (ERR == wresize(wmeta->padframe.curses_win,
101 getmaxy(wmeta->padframe.curses_win),
109 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w)
112 uint8_t test_refit = refit_pad(wmeta);
119 return update_wins(wmeta, w->next);
126 static void place_win(struct WinMeta * wmeta, struct Win * w)
128 /* First window goes into the upper-left corner. */
130 w->start.y = 1; /* Leave space for title bar. */
134 /* Non-first window fallbacks to: fit rightwards of rightmost border. */
135 struct Win * w_top = w->prev;
136 while (w_top->start.y != 1)
140 w->start.x = w_top->start.x + w_top->frame.size.x + 1;
142 /* Fit window below its predecessor if that one directly thrones over
143 * empty space wide and high enough.
145 uint16_t w_prev_maxy = w->prev->start.y + w->prev->frame.size.y;
146 if ( w->frame.size.x <= w->prev->frame.size.x
147 && w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy)
149 w->start.x = w->prev->start.x;
150 w->start.y = w_prev_maxy + 1;
153 /* Failing that, try to open a new sub column below the nearest
154 * predecessor window that thrones over enough empty space.
158 struct Win * w_up = w->prev;
159 struct Win * w_upup = w_up;
161 while (w_up != w_top)
166 if (w_up->start.y != w_upup->start.y)
170 w_upup = w_upup->prev;
172 w_prev_maxy = w_upup->start.y + w_upup->frame.size.y;
173 widthdiff = (w_upup->start.x + w_upup->frame.size.x)
174 - (w_up->start.x + w_up->frame.size.x);
175 if ( w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy
176 && w->frame.size.x < widthdiff)
178 w->start.x = w_up->start.x + w_up->frame.size.x + 1 ;
179 w->start.y = w_prev_maxy + 1;
190 static void scroll_hint(struct WinMeta * wm, struct Frame * frame, char dir,
191 uint16_t dist, char * unit, struct yx_uint16 start)
193 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
194 char * more = "more";
195 uint16_t dsc_space = frame->size.x;
196 if ('<' == dir || '>' == dir)
198 dsc_space = frame->size.y;
199 } /* vv-- 10 = max strlen for uint16_t */
200 char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
201 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
203 /* Decide on offset of the description text inside the scroll hint line. */
204 uint16_t dsc_offset = 1;
205 if (dsc_space > strlen(scrolldsc) + 1)
207 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
210 /* Draw scroll hint line as dir symbols bracketing description text. */
211 uint16_t draw_offset = 0;
214 draw_offset = frame->size.x - 1;
218 draw_offset = frame->size.y - 1;
221 for (; q < dsc_space; q++)
223 chtype symbol = dir | A_REVERSE;
224 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
226 symbol = scrolldsc[q - dsc_offset] | A_REVERSE;
228 if ('<' == dir || '>' == dir)
230 mvwaddch(wm->padframe.curses_win,
231 start.y + q, start.x + draw_offset, symbol);
235 mvwaddch(wm->padframe.curses_win,
236 start.y + draw_offset, start.x + q, symbol);
242 static void padscroll_hint(struct WinMeta * wm, char dir, uint16_t dist)
244 struct yx_uint16 start;
246 start.x = wm->pad_offset;
247 scroll_hint(wm, &wm->padframe, dir, dist, "columns", start);
252 static void winscroll_hint(struct WinMeta * wm, struct Win * w, char dir,
255 char * unit = "lines";
256 if ('<' == dir || '>' == dir)
260 struct yx_uint16 start = w->start;
261 scroll_hint(wm, &w->frame, dir, dist, unit, start);
266 static uint8_t draw_wins(struct WinMeta * wm, struct Win * w)
268 if (ERR == wresize(w->frame.curses_win, 1, 1))
273 uint16_t y, x, size_y, size_x;
274 getmaxyx(w->frame.curses_win, size_y, size_x);
275 uint16_t offset_y = center_offset(w->center.y, size_y, w->frame.size.y);
276 uint16_t offset_x = center_offset(w->center.x, size_x, w->frame.size.x);
277 for (y = offset_y; y < w->frame.size.y + offset_y && y < size_y; y++)
279 for (x = offset_x; x < w->frame.size.x + offset_x && x < size_x; x++)
281 chtype ch = mvwinch(w->frame.curses_win, y, x);
282 mvwaddch(wm->padframe.curses_win, w->start.y + (y - offset_y),
283 w->start.x + (x - offset_x), ch);
288 winscroll_hint(wm, w, '^', offset_y + 1);
290 if (size_y > offset_y + w->frame.size.y)
292 winscroll_hint(wm, w, 'v', size_y - ((offset_y + w->frame.size.y) - 1));
296 winscroll_hint(wm, w, '<', offset_x + 1);
298 if (size_x > offset_x + w->frame.size.x)
300 winscroll_hint(wm, w, '>', size_x - ((offset_x + w->frame.size.x) - 1));
304 return draw_wins(wm, w->next);
311 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
313 /* Draw vertical and horizontal border lines. */
315 for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++)
317 mvwaddch(pad, y, w->start.x - 1, '|');
318 mvwaddch(pad, y, w->start.x + w->frame.size.x, '|');
320 for (x = w->start.x; x <= w->start.x + w->frame.size.x; x++)
322 mvwaddch(pad, w->start.y - 1, x, '-');
323 mvwaddch(pad, w->start.y + w->frame.size.y, x, '-');
326 /* Draw as much as possible of the title into center of top border line. */
327 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
328 if (w->frame.size.x >= min_title_length_visible)
330 uint16_t title_offset = 0;
331 if (w->frame.size.x > strlen(w->title) + 2)
333 title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2;
334 } /* +2 is for padding/decoration */
335 uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);
336 char title[length_visible + 3];
337 char decoration = ' ';
342 memcpy(title + 1, w->title, length_visible);
343 title[0] = title[length_visible + 1] = decoration;
344 title[length_visible + 2] = '\0';
345 mvwaddstr(pad, w->start.y - 1, w->start.x + title_offset, title);
351 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
359 draw_win_borderlines(w, active, pad);
362 draw_wins_borderlines(w->next, w_active, pad);
368 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
370 mvwaddch(pad, w->start.y - 1, w->start.x - 1, '+');
371 mvwaddch(pad, w->start.y - 1, w->start.x + w->frame.size.x, '+');
372 mvwaddch(pad, w->start.y + w->frame.size.y, w->start.x - 1, '+');
374 w->start.y + w->frame.size.y, w->start.x + w->frame.size.x, '+');
377 draw_wins_bordercorners(w->next, pad);
383 static void shift_win_forward(struct WinMeta * wmeta)
385 if (wmeta->active == wmeta->chain_end)
387 wmeta->chain_end = wmeta->active->prev;
388 wmeta->chain_end->next = 0;
389 wmeta->active->next = wmeta->chain_start;
390 wmeta->active->next->prev = wmeta->active;
391 wmeta->chain_start = wmeta->active;
392 wmeta->chain_start->prev = 0;
396 struct Win * old_prev = wmeta->active->prev;
397 struct Win * old_next = wmeta->active->next;
398 if (wmeta->chain_end == wmeta->active->next)
400 wmeta->chain_end = wmeta->active;
401 wmeta->active->next = 0;
405 wmeta->active->next = old_next->next;
406 wmeta->active->next->prev = wmeta->active;
408 if (wmeta->chain_start == wmeta->active)
410 wmeta->chain_start = old_next;
414 old_prev->next = old_next;
416 old_next->prev = old_prev;
417 old_next->next = wmeta->active;
418 wmeta->active->prev = old_next;
424 static void shift_win_backward(struct WinMeta * wmeta)
426 if (wmeta->active == wmeta->chain_start)
428 wmeta->chain_start = wmeta->active->next;
429 wmeta->chain_start->prev = 0;
430 wmeta->active->prev = wmeta->chain_end;
431 wmeta->active->prev->next = wmeta->active;
432 wmeta->chain_end = wmeta->active;
433 wmeta->chain_end->next = 0;
437 struct Win * old_prev = wmeta->active->prev;
438 struct Win * old_next = wmeta->active->next;
439 if (wmeta->chain_start == wmeta->active->prev)
441 wmeta->chain_start = wmeta->active;
442 wmeta->active->prev = 0;
446 wmeta->active->prev = old_prev->prev;
447 wmeta->active->prev->next = wmeta->active;
449 if (wmeta->chain_end == wmeta->active)
451 wmeta->chain_end = old_prev;
455 old_next->prev = old_prev;
457 old_prev->next = old_next;
458 old_prev->prev = wmeta->active;
459 wmeta->active->next = old_prev;
465 extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta ** wmp)
467 struct WinMeta * wmeta = malloc(sizeof(struct WinMeta));
468 wmeta->screen = screen;
469 uint32_t maxy_test = getmaxy(screen);
470 uint32_t maxx_test = getmaxx(screen);
471 if (maxy_test > UINT16_MAX || maxx_test > UINT16_MAX)
475 wmeta->padframe.size.y = maxy_test;
476 wmeta->padframe.size.x = maxx_test;
477 wmeta->chain_start = 0;
478 wmeta->chain_end = 0;
479 wmeta->pad_offset = 0;
480 WINDOW * pad_test = newpad(wmeta->padframe.size.y, 1);
481 if (NULL == pad_test)
485 wmeta->padframe.curses_win = pad_test;
493 extern uint8_t init_win(struct WinMeta * wmeta, struct Win ** wp, char * title,
494 int16_t height, int16_t width,
495 void * data, void * func)
497 struct Win * w = malloc(sizeof(struct Win));
504 w->frame.curses_win = newpad(1, 1);
505 if (NULL == w->frame.curses_win)
509 w->title = malloc(strlen(title) + 1);
510 if (NULL == w->title)
514 sprintf(w->title, "%s", title);
521 w->frame.size.x = width;
525 w->frame.size.x = wmeta->padframe.size.x + width;
527 if (0 < height && height <= wmeta->padframe.size.y - 1)
529 w->frame.size.y = height;
531 else if (0 >= height && wmeta->padframe.size.y + (height - 1) > 0)
533 w->frame.size.y = wmeta->padframe.size.y + (height - 1);
541 extern void free_winmeta(struct WinMeta * wmeta)
543 delwin(wmeta->padframe.curses_win);
549 extern void free_win(struct Win * win)
551 delwin(win->frame.curses_win);
558 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
560 if (0 != wmeta->chain_start)
562 w->prev = wmeta->chain_end;
563 wmeta->chain_end->next = w;
568 wmeta->chain_start = w;
570 wmeta->chain_end = w;
571 return update_wins(wmeta, w);
576 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
578 if (wmeta->chain_start != w)
580 w->prev->next = w->next;
584 wmeta->chain_start = w->next;
586 char pad_refitted = 0;
587 if (wmeta->chain_end != w)
589 w->next->prev = w->prev;
590 if (wmeta->active == w)
592 wmeta->active = w->next;
594 uint8_t test = update_wins(wmeta, w->next); /* Positioning of */
595 if (0 != test) /* successor windows may */
596 { /* be affected / need */
597 return test; /* correction. Note that */
598 } /* update_wins() already */
599 pad_refitted = 1; /* refits the pad, voiding*/
600 } /* later need for that. */
603 wmeta->chain_end = w->prev;
604 if (wmeta->active == w)
606 wmeta->active = w->prev;
613 if (0 == pad_refitted)
615 return refit_pad(wmeta);
622 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
625 && (new_offset < wmeta->pad_offset
626 || new_offset + wmeta->padframe.size.x
627 < getmaxx(wmeta->padframe.curses_win)))
629 wmeta->pad_offset = new_offset;
635 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
637 if (0 != wmeta->active
638 && size.x > 0 && size.y > 0
639 && size.y < wmeta->padframe.size.y)
641 wmeta->active->frame.size = size;
642 return update_wins(wmeta, wmeta->active); /* Positioning of following */
643 } /* windows may be affected. */
649 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
651 if (0 != wmeta->active)
655 if (wmeta->active->next != 0)
657 wmeta->active = wmeta->active->next;
661 wmeta->active = wmeta->chain_start;
666 if (wmeta->active->prev != 0)
668 wmeta->active = wmeta->active->prev;
672 wmeta->active = wmeta->chain_end;
680 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
682 if ( 0 == wmeta->active /* No shifting with < 2 */
683 || wmeta->chain_start == wmeta->chain_end) /* windows visible. */
689 shift_win_forward(wmeta);
693 shift_win_backward(wmeta);
695 return update_wins(wmeta, wmeta->chain_start);
700 extern uint8_t draw_all_wins(struct WinMeta * wm)
702 /* Empty everything before filling it a-new. */
704 wnoutrefresh(wm->screen);
705 werase(wm->padframe.curses_win);
709 /* Draw windows' borders first, then windows. */
710 draw_wins_borderlines(wm->chain_start, wm->active,
711 wm->padframe.curses_win);
712 draw_wins_bordercorners(wm->chain_start, wm->padframe.curses_win);
714 if (1 == draw_wins(wm, wm->chain_start))
719 /* Draw virtual screen scroll hints. */
720 if (wm->pad_offset > 0)
722 padscroll_hint(wm, '<', wm->pad_offset + 1);
724 uint16_t size_x = getmaxx(wm->padframe.curses_win);
725 uint16_t right_edge = wm->pad_offset + wm->padframe.size.x;
726 if (right_edge < size_x - 1)
728 padscroll_hint(wm, '>', size_x - right_edge);
731 /* Write pad segment to be shown on physical screen to screen buffer. */
732 pnoutrefresh(wm->padframe.curses_win, 0, wm->pad_offset, 0, 0,
733 wm->padframe.size.y, wm->padframe.size.x - 1);
736 /* Only at the end write accumulated changes to the physical screen. */