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(), supbad(), delwin(), mvwaddch(), */
7 /* mvwaddstr(), newpad(), wnoutrefres(), erase(), */
8 /* werase(), pnoutrefresh(), doupdate() */
9 #include <stdlib.h> /* for malloc(), free() */
10 #include <string.h> /* for strlen(), strnlen(), memcpy() */
11 #include "yx_uint16.h" /* for yx_uint16 coordinates */
15 /* Fit virtual screen's width to minimum width demanded by current windows'
18 static uint8_t refit_pad(struct WinMeta * wmeta);
22 /* Update geometry (sizes, positions) of window "w" and its successors in the
23 * window chain. For the positioning algorithm place_win() is used.
25 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w);
26 static void place_win(struct WinMeta * wmeta, struct Win * w);
30 /* Destroy window "w"'s ncurses WINDOW (and set w.Frame.curses_win to 0). */
31 static void destroy_win(struct Win * w);
35 /* Draw contents of all windows in window chain from window "w" onwards. */
36 static void draw_wins(struct Win * w);
40 /* draw_win_borderlines() draws the vertical and horizontal borders of window
41 * "w" sans corners into the virtual screen "pad", and draws the top border
42 * line as the windows' title bar (highlighted if the window is described
43 * active by "active" being == 1).
45 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
46 * windows from "w" on. "w_active" is a pointer to the one window that
47 * draw_win_borderlines() is supposed to handle as the active window.
49 * Finally, draw_wins_bordercorners() draws into "pad" the borders of window "w"
50 * and all its successors.
52 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
53 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
55 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
59 /* Shift active window forwards / backwards in window chain. */
60 static void shift_win_forward(struct WinMeta * wmeta);
61 static void shift_win_backward(struct WinMeta * wmeta);
65 static uint8_t refit_pad(struct WinMeta * wmeta)
67 /* Determine rightmost window column. */
68 uint32_t lastwincol = 0;
69 struct Win * w_p = wmeta->_chain_start;
72 if ((uint32_t) w_p->_start.x + (uint32_t) w_p->frame.size.x
75 lastwincol = (uint32_t) w_p->_start.x
76 + (uint32_t) w_p->frame.size.x - 1;
81 /* Only resize the pad if the rightmost window column has changed. */
82 if (getmaxx(wmeta->padframe.curses_win) != lastwincol)
84 if (lastwincol + 2 > UINT16_MAX)
88 return (ERR == wresize(wmeta->padframe.curses_win,
89 getmaxy(wmeta->padframe.curses_win),
97 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w)
99 if (0 != w->frame.curses_win)
104 uint8_t test_refit = refit_pad(wmeta);
109 WINDOW * subpad_test = subpad(wmeta->padframe.curses_win,
110 w->frame.size.y, w->frame.size.x,
111 w->_start.y, w->_start.x);
112 if (NULL == subpad_test)
116 w->frame.curses_win = subpad_test;
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)
138 w_top = w_top->_prev;
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
146 + getmaxy(w->_prev->frame.curses_win);
147 if ( w->frame.size.x <= w->_prev->frame.size.x
148 && w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy)
150 w->_start.x = w->_prev->_start.x;
151 w->_start.y = w_prev_maxy + 1;
154 /* Failing that, try to open a new sub column below the nearest
155 * predecessor window that thrones over enough empty space.
159 struct Win * w_up = w->_prev;
160 struct Win * w_upup = w_up;
162 while (w_up != w_top)
164 w_upup = w_up->_prev;
167 if (w_up->_start.y != w_upup->_start.y)
171 w_upup = w_upup->_prev;
173 w_prev_maxy = w_upup->_start.y
174 + getmaxy(w_upup->frame.curses_win);
175 widthdiff = (w_upup->_start.x + w_upup->frame.size.x)
176 - (w_up->_start.x + w_up->frame.size.x);
177 if ( w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy
178 && w->frame.size.x < widthdiff)
180 w->_start.x = w_up->_start.x + w_up->frame.size.x + 1 ;
181 w->_start.y = w_prev_maxy + 1;
192 static void destroy_win(struct Win * w)
194 delwin(w->frame.curses_win);
195 w->frame.curses_win = 0;
200 static void draw_wins(struct Win * w)
211 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
213 /* Draw vertical and horizontal border lines. */
215 for (y = w->_start.y; y <= w->_start.y + w->frame.size.y; y++)
217 mvwaddch(pad, y, w->_start.x - 1, '|');
218 mvwaddch(pad, y, w->_start.x + w->frame.size.x, '|');
220 for (x = w->_start.x; x <= w->_start.x + w->frame.size.x; x++)
222 mvwaddch(pad, w->_start.y - 1, x, '-');
223 mvwaddch(pad, w->_start.y + w->frame.size.y, x, '-');
226 /* Draw as much as possible of the title into center of top border line. */
227 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
228 if (w->frame.size.x >= min_title_length_visible)
230 uint16_t title_offset = 0;
231 if (w->frame.size.x > strlen(w->_title) + 2)
233 title_offset = (w->frame.size.x - (strlen(w->_title) + 2)) / 2;
234 } /* +2 is for padding/decoration */
235 uint16_t length_visible = strnlen(w->_title, w->frame.size.x - 2);
236 char title[length_visible + 3];
237 char decoration = ' ';
242 memcpy(title + 1, w->_title, length_visible);
243 title[0] = title[length_visible + 1] = decoration;
244 title[length_visible + 2] = '\0';
245 mvwaddstr(pad, w->_start.y - 1, w->_start.x + title_offset, title);
251 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
259 draw_win_borderlines(w, active, pad);
262 draw_wins_borderlines(w->_next, w_active, pad);
268 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
270 mvwaddch(pad, w->_start.y - 1, w->_start.x - 1, '+');
271 mvwaddch(pad, w->_start.y - 1, w->_start.x + w->frame.size.x, '+');
272 mvwaddch(pad, w->_start.y + w->frame.size.y, w->_start.x - 1, '+');
274 w->_start.y + w->frame.size.y, w->_start.x + w->frame.size.x, '+');
277 draw_wins_bordercorners(w->_next, pad);
283 static void shift_win_forward(struct WinMeta * wmeta)
285 if (wmeta->active == wmeta->_chain_end)
287 wmeta->_chain_end = wmeta->active->_prev;
288 wmeta->_chain_end->_next = 0;
289 wmeta->active->_next = wmeta->_chain_start;
290 wmeta->active->_next->_prev = wmeta->active;
291 wmeta->_chain_start = wmeta->active;
292 wmeta->_chain_start->_prev = 0;
296 struct Win * old_prev = wmeta->active->_prev;
297 struct Win * old_next = wmeta->active->_next;
298 if (wmeta->_chain_end == wmeta->active->_next)
300 wmeta->_chain_end = wmeta->active;
301 wmeta->active->_next = 0;
305 wmeta->active->_next = old_next->_next;
306 wmeta->active->_next->_prev = wmeta->active;
308 if (wmeta->_chain_start == wmeta->active)
310 wmeta->_chain_start = old_next;
314 old_prev->_next = old_next;
316 old_next->_prev = old_prev;
317 old_next->_next = wmeta->active;
318 wmeta->active->_prev = old_next;
324 static void shift_win_backward(struct WinMeta * wmeta)
326 if (wmeta->active == wmeta->_chain_start)
328 wmeta->_chain_start = wmeta->active->_next;
329 wmeta->_chain_start->_prev = 0;
330 wmeta->active->_prev = wmeta->_chain_end;
331 wmeta->active->_prev->_next = wmeta->active;
332 wmeta->_chain_end = wmeta->active;
333 wmeta->_chain_end->_next = 0;
337 struct Win * old_prev = wmeta->active->_prev;
338 struct Win * old_next = wmeta->active->_next;
339 if (wmeta->_chain_start == wmeta->active->_prev)
341 wmeta->_chain_start = wmeta->active;
342 wmeta->active->_prev = 0;
346 wmeta->active->_prev = old_prev->_prev;
347 wmeta->active->_prev->_next = wmeta->active;
349 if (wmeta->_chain_end == wmeta->active)
351 wmeta->_chain_end = old_prev;
355 old_next->_prev = old_prev;
357 old_prev->_next = old_next;
358 old_prev->_prev = wmeta->active;
359 wmeta->active->_next = old_prev;
365 extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta)
367 wmeta->_screen = screen;
368 uint32_t maxy_test = getmaxy(screen);
369 uint32_t maxx_test = getmaxx(screen);
370 if (maxy_test > UINT16_MAX || maxx_test > UINT16_MAX)
374 wmeta->padframe.size.y = maxy_test;
375 wmeta->padframe.size.x = maxx_test;
376 wmeta->_chain_start = 0;
377 wmeta->_chain_end = 0;
378 wmeta->pad_offset = 0;
379 WINDOW * pad_test = newpad(wmeta->padframe.size.y, 1);
380 if (NULL == pad_test)
384 wmeta->padframe.curses_win = pad_test;
391 extern struct Win init_win(struct WinMeta * wmeta, char * title,
392 int16_t height, int16_t width,
393 void * data, void * func)
398 w.frame.curses_win = 0;
404 w.frame.size.x = width;
408 w.frame.size.x = wmeta->padframe.size.x + width;
412 w.frame.size.x = wmeta->padframe.size.x;
414 if (0 < height && height <= wmeta->padframe.size.y - 1)
416 w.frame.size.y = height;
418 else if (0 > height && wmeta->padframe.size.y + (height - 1) > 0)
420 w.frame.size.y = wmeta->padframe.size.y + (height - 1);
424 w.frame.size.y = wmeta->padframe.size.y - 1;
431 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
433 if (0 != wmeta->_chain_start)
435 w->_prev = wmeta->_chain_end;
436 wmeta->_chain_end->_next = w;
441 wmeta->_chain_start = w;
443 wmeta->_chain_end = w;
444 return update_wins(wmeta, w);
449 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
453 if (wmeta->_chain_start != w)
455 w->_prev->_next = w->_next;
459 wmeta->_chain_start = w->_next;
461 char pad_refitted = 0;
462 if (wmeta->_chain_end != w)
464 w->_next->_prev = w->_prev;
465 if (wmeta->active == w)
467 wmeta->active = w->_next;
469 uint8_t test = update_wins(wmeta, w->_next);/* Positioning of */
470 if (0 != test) /* successor windows may */
471 { /* be affected / need */
472 return test; /* correction. Note that */
473 } /* update_wins() already */
474 pad_refitted = 1; /* refits the pad, voiding*/
475 } /* later need for that. */
478 wmeta->_chain_end = w->_prev;
479 if (wmeta->active == w)
481 wmeta->active = w->_prev;
488 if (0 == pad_refitted)
490 return refit_pad(wmeta);
497 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
500 && (new_offset < wmeta->pad_offset
501 || new_offset + wmeta->padframe.size.x
502 < getmaxx(wmeta->padframe.curses_win)))
504 wmeta->pad_offset = new_offset;
510 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
512 if (0 != wmeta->active
513 && size.x > 0 && size.y > 0
514 && size.y < wmeta->padframe.size.y)
516 wmeta->active->frame.size = size;
517 return update_wins(wmeta, wmeta->active); /* Positioning of following */
518 } /* windows may be affected. */
524 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
526 if (0 != wmeta->active)
530 if (wmeta->active->_next != 0)
532 wmeta->active = wmeta->active->_next;
536 wmeta->active = wmeta->_chain_start;
541 if (wmeta->active->_prev != 0)
543 wmeta->active = wmeta->active->_prev;
547 wmeta->active = wmeta->_chain_end;
555 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
557 if ( 0 == wmeta->active /* No shifting with < 2 */
558 || wmeta->_chain_start == wmeta->_chain_end) /* windows visible. */
564 shift_win_forward(wmeta);
568 shift_win_backward(wmeta);
570 return update_wins(wmeta, wmeta->_chain_start);
575 extern uint8_t draw_all_wins(struct WinMeta * wmeta)
577 /* Empty everything before filling it a-new. */
579 wnoutrefresh(wmeta->_screen);
580 werase(wmeta->padframe.curses_win);
581 if (wmeta->_chain_start)
584 /* Draw windows' contents first, then their borders. */
585 draw_wins(wmeta->_chain_start);
586 draw_wins_borderlines(wmeta->_chain_start, wmeta->active,
587 wmeta->padframe.curses_win);
588 draw_wins_bordercorners(wmeta->_chain_start,wmeta->padframe.curses_win);
590 /* Draw virtual screen scroll hints. */
591 if (wmeta->pad_offset > 0)
593 if (draw_scroll_hint(&wmeta->padframe,
594 wmeta->pad_offset, wmeta->pad_offset + 1, '<'))
599 if (wmeta->pad_offset + wmeta->padframe.size.x
600 < getmaxx(wmeta->padframe.curses_win) - 1)
602 if (draw_scroll_hint(&wmeta->padframe,
603 wmeta->pad_offset + wmeta->padframe.size.x - 1,
604 getmaxx(wmeta->padframe.curses_win)
605 - (wmeta->pad_offset + wmeta->padframe.size.x),
612 /* Write virtual screen segment to be shown on physical screen into */
613 /* ncurses screen buffer. */
614 pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
615 wmeta->padframe.size.y, wmeta->padframe.size.x-1);
618 /* Only at the end write accumulated changes to the physical screen. */
625 extern uint8_t draw_scroll_hint(struct Frame * frame, uint16_t pos,
626 uint32_t dist, char dir)
628 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
629 char * more = "more";
630 char * unit_cols = "columns";
631 char * unit_rows = "lines";
632 uint16_t dsc_space = frame->size.x;
633 char * unit = unit_rows;
634 if ('<' == dir || '>' == dir)
636 dsc_space = frame->size.y;
639 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
640 + 10); /* 10 = uint32 max strlen */
641 if (NULL == scrolldsc)
645 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
647 /* Decide on offset of the description text inside the scroll hint line. */
649 if (dsc_space > strlen(scrolldsc) + 1)
651 offset = (dsc_space - strlen(scrolldsc)) / 2;
654 /* Draw scroll hint line as dir symbols bracketing description text. */
656 for (q = 0; q < dsc_space; q++)
658 if (q >= offset && q < strlen(scrolldsc) + offset)
660 symbol = scrolldsc[q - offset] | A_REVERSE;
664 symbol = dir | A_REVERSE;
666 if ('<' == dir || '>' == dir)
668 mvwaddch(frame->curses_win, q, pos, symbol);
672 mvwaddch(frame->curses_win, pos, q, symbol);