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;
423 w->frame.size.x = wmeta->padframe.size.x;
425 if (0 < height && height <= wmeta->padframe.size.y - 1)
427 w->frame.size.y = height;
429 else if (0 > height && wmeta->padframe.size.y + (height - 1) > 0)
431 w->frame.size.y = wmeta->padframe.size.y + (height - 1);
435 w->frame.size.y = wmeta->padframe.size.y - 1;
443 extern void free_winmeta(struct WinMeta * wmeta)
445 delwin(wmeta->padframe.curses_win);
451 extern void free_win(struct Win * win)
453 if (0 != win->frame.curses_win)
455 delwin(win->frame.curses_win);
463 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
465 if (0 != wmeta->_chain_start)
467 w->_prev = wmeta->_chain_end;
468 wmeta->_chain_end->_next = w;
473 wmeta->_chain_start = w;
475 wmeta->_chain_end = w;
476 return update_wins(wmeta, w);
481 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
485 if (wmeta->_chain_start != w)
487 w->_prev->_next = w->_next;
491 wmeta->_chain_start = w->_next;
493 char pad_refitted = 0;
494 if (wmeta->_chain_end != w)
496 w->_next->_prev = w->_prev;
497 if (wmeta->active == w)
499 wmeta->active = w->_next;
501 uint8_t test = update_wins(wmeta, w->_next);/* Positioning of */
502 if (0 != test) /* successor windows may */
503 { /* be affected / need */
504 return test; /* correction. Note that */
505 } /* update_wins() already */
506 pad_refitted = 1; /* refits the pad, voiding*/
507 } /* later need for that. */
510 wmeta->_chain_end = w->_prev;
511 if (wmeta->active == w)
513 wmeta->active = w->_prev;
520 if (0 == pad_refitted)
522 return refit_pad(wmeta);
529 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
532 && (new_offset < wmeta->pad_offset
533 || new_offset + wmeta->padframe.size.x
534 < getmaxx(wmeta->padframe.curses_win)))
536 wmeta->pad_offset = new_offset;
542 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
544 if (0 != wmeta->active
545 && size.x > 0 && size.y > 0
546 && size.y < wmeta->padframe.size.y)
548 wmeta->active->frame.size = size;
549 return update_wins(wmeta, wmeta->active); /* Positioning of following */
550 } /* windows may be affected. */
556 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
558 if (0 != wmeta->active)
562 if (wmeta->active->_next != 0)
564 wmeta->active = wmeta->active->_next;
568 wmeta->active = wmeta->_chain_start;
573 if (wmeta->active->_prev != 0)
575 wmeta->active = wmeta->active->_prev;
579 wmeta->active = wmeta->_chain_end;
587 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
589 if ( 0 == wmeta->active /* No shifting with < 2 */
590 || wmeta->_chain_start == wmeta->_chain_end) /* windows visible. */
596 shift_win_forward(wmeta);
600 shift_win_backward(wmeta);
602 return update_wins(wmeta, wmeta->_chain_start);
607 extern uint8_t draw_all_wins(struct WinMeta * wmeta)
609 /* Empty everything before filling it a-new. */
611 wnoutrefresh(wmeta->_screen);
612 werase(wmeta->padframe.curses_win);
613 if (wmeta->_chain_start)
616 /* Draw windows' contents first, then their borders. */
617 draw_wins(wmeta->_chain_start);
618 draw_wins_borderlines(wmeta->_chain_start, wmeta->active,
619 wmeta->padframe.curses_win);
620 draw_wins_bordercorners(wmeta->_chain_start,wmeta->padframe.curses_win);
622 /* Draw virtual screen scroll hints. */
623 if (wmeta->pad_offset > 0)
625 if (draw_scroll_hint(&wmeta->padframe,
626 wmeta->pad_offset, wmeta->pad_offset + 1, '<'))
631 if (wmeta->pad_offset + wmeta->padframe.size.x
632 < getmaxx(wmeta->padframe.curses_win) - 1)
634 if (draw_scroll_hint(&wmeta->padframe,
635 wmeta->pad_offset + wmeta->padframe.size.x - 1,
636 getmaxx(wmeta->padframe.curses_win)
637 - (wmeta->pad_offset + wmeta->padframe.size.x),
644 /* Write virtual screen segment to be shown on physical screen into */
645 /* ncurses screen buffer. */
646 pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
647 wmeta->padframe.size.y, wmeta->padframe.size.x-1);
650 /* Only at the end write accumulated changes to the physical screen. */
657 extern uint8_t draw_scroll_hint(struct Frame * frame, uint16_t pos,
658 uint32_t dist, char dir)
660 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
661 char * more = "more";
662 char * unit_cols = "columns";
663 char * unit_rows = "lines";
664 uint16_t dsc_space = frame->size.x;
665 char * unit = unit_rows;
666 if ('<' == dir || '>' == dir)
668 dsc_space = frame->size.y;
671 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
672 + 10); /* 10 = uint32 max strlen */
673 if (NULL == scrolldsc)
677 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
679 /* Decide on offset of the description text inside the scroll hint line. */
681 if (dsc_space > strlen(scrolldsc) + 1)
683 offset = (dsc_space - strlen(scrolldsc)) / 2;
686 /* Draw scroll hint line as dir symbols bracketing description text. */
688 for (q = 0; q < dsc_space; q++)
690 if (q >= offset && q < strlen(scrolldsc) + offset)
692 symbol = scrolldsc[q - offset] | A_REVERSE;
696 symbol = dir | A_REVERSE;
698 if ('<' == dir || '>' == dir)
700 mvwaddch(frame->curses_win, q, pos, symbol);
704 mvwaddch(frame->curses_win, pos, q, symbol);