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 ** wmp)
367 struct WinMeta * wmeta = malloc(sizeof(struct WinMeta));
368 wmeta->_screen = screen;
369 uint32_t maxy_test = getmaxy(screen);
370 uint32_t maxx_test = getmaxx(screen);
371 if (maxy_test > UINT16_MAX || maxx_test > UINT16_MAX)
375 wmeta->padframe.size.y = maxy_test;
376 wmeta->padframe.size.x = maxx_test;
377 wmeta->_chain_start = 0;
378 wmeta->_chain_end = 0;
379 wmeta->pad_offset = 0;
380 WINDOW * pad_test = newpad(wmeta->padframe.size.y, 1);
381 if (NULL == pad_test)
385 wmeta->padframe.curses_win = pad_test;
393 extern uint8_t init_win(struct WinMeta * wmeta, struct Win ** wp, char * title,
394 int16_t height, int16_t width,
395 void * data, void * func)
397 struct Win * w = malloc(sizeof(struct Win));
404 w->frame.curses_win = 0;
405 w->_title = malloc(strlen(title) + 1);
406 if (NULL == w->_title)
410 sprintf(w->_title, "%s", title);
415 w->frame.size.x = width;
419 w->frame.size.x = wmeta->padframe.size.x + width;
421 if (0 < height && height <= wmeta->padframe.size.y - 1)
423 w->frame.size.y = height;
425 else if (0 >= height && wmeta->padframe.size.y + (height - 1) > 0)
427 w->frame.size.y = wmeta->padframe.size.y + (height - 1);
435 extern void free_winmeta(struct WinMeta * wmeta)
437 delwin(wmeta->padframe.curses_win);
443 extern void free_win(struct Win * win)
445 if (0 != win->frame.curses_win)
447 delwin(win->frame.curses_win);
455 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
457 if (0 != wmeta->_chain_start)
459 w->_prev = wmeta->_chain_end;
460 wmeta->_chain_end->_next = w;
465 wmeta->_chain_start = w;
467 wmeta->_chain_end = w;
468 return update_wins(wmeta, w);
473 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
477 if (wmeta->_chain_start != w)
479 w->_prev->_next = w->_next;
483 wmeta->_chain_start = w->_next;
485 char pad_refitted = 0;
486 if (wmeta->_chain_end != w)
488 w->_next->_prev = w->_prev;
489 if (wmeta->active == w)
491 wmeta->active = w->_next;
493 uint8_t test = update_wins(wmeta, w->_next);/* Positioning of */
494 if (0 != test) /* successor windows may */
495 { /* be affected / need */
496 return test; /* correction. Note that */
497 } /* update_wins() already */
498 pad_refitted = 1; /* refits the pad, voiding*/
499 } /* later need for that. */
502 wmeta->_chain_end = w->_prev;
503 if (wmeta->active == w)
505 wmeta->active = w->_prev;
512 if (0 == pad_refitted)
514 return refit_pad(wmeta);
521 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
524 && (new_offset < wmeta->pad_offset
525 || new_offset + wmeta->padframe.size.x
526 < getmaxx(wmeta->padframe.curses_win)))
528 wmeta->pad_offset = new_offset;
534 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
536 if (0 != wmeta->active
537 && size.x > 0 && size.y > 0
538 && size.y < wmeta->padframe.size.y)
540 wmeta->active->frame.size = size;
541 return update_wins(wmeta, wmeta->active); /* Positioning of following */
542 } /* windows may be affected. */
548 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
550 if (0 != wmeta->active)
554 if (wmeta->active->_next != 0)
556 wmeta->active = wmeta->active->_next;
560 wmeta->active = wmeta->_chain_start;
565 if (wmeta->active->_prev != 0)
567 wmeta->active = wmeta->active->_prev;
571 wmeta->active = wmeta->_chain_end;
579 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
581 if ( 0 == wmeta->active /* No shifting with < 2 */
582 || wmeta->_chain_start == wmeta->_chain_end) /* windows visible. */
588 shift_win_forward(wmeta);
592 shift_win_backward(wmeta);
594 return update_wins(wmeta, wmeta->_chain_start);
599 extern uint8_t draw_all_wins(struct WinMeta * wmeta)
601 /* Empty everything before filling it a-new. */
603 wnoutrefresh(wmeta->_screen);
604 werase(wmeta->padframe.curses_win);
605 if (wmeta->_chain_start)
608 /* Draw windows' contents first, then their borders. */
609 draw_wins(wmeta->_chain_start);
610 draw_wins_borderlines(wmeta->_chain_start, wmeta->active,
611 wmeta->padframe.curses_win);
612 draw_wins_bordercorners(wmeta->_chain_start,wmeta->padframe.curses_win);
614 /* Draw virtual screen scroll hints. */
615 if (wmeta->pad_offset > 0)
617 if (draw_scroll_hint(&wmeta->padframe,
618 wmeta->pad_offset, wmeta->pad_offset + 1, '<'))
623 if (wmeta->pad_offset + wmeta->padframe.size.x
624 < getmaxx(wmeta->padframe.curses_win) - 1)
626 if (draw_scroll_hint(&wmeta->padframe,
627 wmeta->pad_offset + wmeta->padframe.size.x - 1,
628 getmaxx(wmeta->padframe.curses_win)
629 - (wmeta->pad_offset + wmeta->padframe.size.x),
636 /* Write virtual screen segment to be shown on physical screen into */
637 /* ncurses screen buffer. */
638 pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
639 wmeta->padframe.size.y, wmeta->padframe.size.x-1);
642 /* Only at the end write accumulated changes to the physical screen. */
649 extern uint8_t draw_scroll_hint(struct Frame * frame, uint16_t pos,
650 uint32_t dist, char dir)
652 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
653 char * more = "more";
654 char * unit_cols = "columns";
655 char * unit_rows = "lines";
656 uint16_t dsc_space = frame->size.x;
657 char * unit = unit_rows;
658 if ('<' == dir || '>' == dir)
660 dsc_space = frame->size.y;
663 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
664 + 10); /* 10 = uint32 max strlen */
665 if (NULL == scrolldsc)
669 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
671 /* Decide on offset of the description text inside the scroll hint line. */
673 if (dsc_space > strlen(scrolldsc) + 1)
675 offset = (dsc_space - strlen(scrolldsc)) / 2;
678 /* Draw scroll hint line as dir symbols bracketing description text. */
680 for (q = 0; q < dsc_space; q++)
682 if (q >= offset && q < strlen(scrolldsc) + offset)
684 symbol = scrolldsc[q - offset] | A_REVERSE;
688 symbol = dir | A_REVERSE;
690 if ('<' == dir || '>' == dir)
692 mvwaddch(frame->curses_win, q, pos, symbol);
696 mvwaddch(frame->curses_win, pos, q, symbol);