4 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t */
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'
16 * geometries. Returns 0 on success, 1 on (pad memory allocation) error.
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 * update_wins() returns 0 on success, 1 on (pad/window memory alloc.) error.
27 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w);
28 static void place_win(struct WinMeta * wmeta, struct Win * w);
32 /* Destroy window "w"'s ncurses window (and set w.Frame.curses_win to 0). */
33 static void destroy_win(struct Win * w);
37 /* Draw contents of all windows in window chain from window "w" onwards. */
38 static void draw_wins(struct Win * w);
42 /* draw_win_borderlines() draws the vertical and horizontal borders of window
43 * "w" sans corners into the virtual screen "pad", and draws the top border
44 * line as the windows' title bar (highlighted if the window is described
45 * active by "active" being set). draw_wins_borderlines().
47 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
48 * windows from "w" on. "w_active" is a pointer to the one window that
49 * draw_win_borderlines() is supposed to handle as the active window.
51 * Finally, draw_wins_bordercorners draws into "pad" the borders of window "w"
52 * and all its successors.
54 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
55 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
57 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
61 /* Shift active window forwards / backwards in window chain. */
62 static void shift_win_forward(struct WinMeta * wmeta);
63 static void shift_win_backward(struct WinMeta * wmeta);
67 static uint8_t refit_pad(struct WinMeta * wmeta)
69 /* Determine rightmost window column. */
70 uint16_t lastwincol = 0;
71 struct Win * w_p = wmeta->_chain_start;
74 if (w_p->_start.x + w_p->frame.size.x > lastwincol + 1)
76 lastwincol = w_p->_start.x + 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 return (ERR == wresize(wmeta->padframe.curses_win,
85 getmaxy(wmeta->padframe.curses_win),
93 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w)
95 if (0 != w->frame.curses_win)
100 if (0 != refit_pad(wmeta))
104 WINDOW * test = subpad(wmeta->padframe.curses_win,
105 w->frame.size.y, w->frame.size.x,
106 w->_start.y, w->_start.x);
111 w->frame.curses_win = test;
114 return update_wins(wmeta, w->_next);
121 static void place_win(struct WinMeta * wmeta, struct Win * w)
123 /* First window goes into the upper-left corner. */
125 w->_start.y = 1; /* Leave space for title bar. */
129 /* Non-first window fallbacks to: fit rightwards of rightmost border. */
130 struct Win * w_top = w->_prev;
131 while (w_top->_start.y != 1)
133 w_top = w_top->_prev;
135 w->_start.x = w_top->_start.x + w_top->frame.size.x + 1;
137 /* Fit window below its predecessor if that one directly thrones over
138 * empty space wide and high enough.
140 uint16_t w_prev_maxy = w->_prev->_start.y
141 + getmaxy(w->_prev->frame.curses_win);
142 if ( w->frame.size.x <= w->_prev->frame.size.x
143 && w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy)
145 w->_start.x = w->_prev->_start.x;
146 w->_start.y = w_prev_maxy + 1;
149 /* Failing that, try to open a new sub column below the nearest
150 * predecessor window that thrones over enough empty space.
154 struct Win * w_up = w->_prev;
155 struct Win * w_upup = w_up;
157 while (w_up != w_top)
159 w_upup = w_up->_prev;
162 if (w_up->_start.y != w_upup->_start.y)
166 w_upup = w_upup->_prev;
168 w_prev_maxy = w_upup->_start.y
169 + getmaxy(w_upup->frame.curses_win);
170 widthdiff = (w_upup->_start.x + w_upup->frame.size.x)
171 - (w_up->_start.x + w_up->frame.size.x);
172 if ( w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy
173 && w->frame.size.x < widthdiff)
175 w->_start.x = w_up->_start.x + w_up->frame.size.x + 1 ;
176 w->_start.y = w_prev_maxy + 1;
187 static void destroy_win(struct Win * w)
189 delwin(w->frame.curses_win);
190 w->frame.curses_win = 0;
195 static void draw_wins(struct Win * w)
206 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
208 /* Draw vertical and horizontal border lines. */
210 for (y = w->_start.y; y <= w->_start.y + w->frame.size.y; y++)
212 mvwaddch(pad, y, w->_start.x - 1, '|');
213 mvwaddch(pad, y, w->_start.x + w->frame.size.x, '|');
215 for (x = w->_start.x; x <= w->_start.x + w->frame.size.x; x++)
217 mvwaddch(pad, w->_start.y - 1, x, '-');
218 mvwaddch(pad, w->_start.y + w->frame.size.y, x, '-');
221 /* Draw as much as possible of the title into center of top border line. */
222 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
223 if (w->frame.size.x >= min_title_length_visible)
225 uint16_t title_offset = 0;
226 if (w->frame.size.x > strlen(w->_title) + 2)
228 title_offset = (w->frame.size.x - (strlen(w->_title) + 2)) / 2;
229 } /* +2 is for padding/decoration */
230 uint16_t length_visible = strnlen(w->_title, w->frame.size.x - 2);
231 char title[length_visible + 3];
232 char decoration = ' ';
237 memcpy(title + 1, w->_title, length_visible);
238 title[0] = title[length_visible + 1] = decoration;
239 title[length_visible + 2] = '\0';
240 mvwaddstr(pad, w->_start.y - 1, w->_start.x + title_offset, title);
246 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
254 draw_win_borderlines(w, active, pad);
257 draw_wins_borderlines(w->_next, w_active, pad);
263 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
265 mvwaddch(pad, w->_start.y - 1, w->_start.x - 1, '+');
266 mvwaddch(pad, w->_start.y - 1, w->_start.x + w->frame.size.x, '+');
267 mvwaddch(pad, w->_start.y + w->frame.size.y, w->_start.x - 1, '+');
269 w->_start.y + w->frame.size.y, w->_start.x + w->frame.size.x, '+');
272 draw_wins_bordercorners(w->_next, pad);
278 static void shift_win_forward(struct WinMeta * wmeta)
280 if (wmeta->active == wmeta->_chain_end)
282 wmeta->_chain_end = wmeta->active->_prev;
283 wmeta->_chain_end->_next = 0;
284 wmeta->active->_next = wmeta->_chain_start;
285 wmeta->active->_next->_prev = wmeta->active;
286 wmeta->_chain_start = wmeta->active;
287 wmeta->_chain_start->_prev = 0;
291 struct Win * old_prev = wmeta->active->_prev;
292 struct Win * old_next = wmeta->active->_next;
293 if (wmeta->_chain_end == wmeta->active->_next)
295 wmeta->_chain_end = wmeta->active;
296 wmeta->active->_next = 0;
300 wmeta->active->_next = old_next->_next;
301 wmeta->active->_next->_prev = wmeta->active;
303 if (wmeta->_chain_start == wmeta->active)
305 wmeta->_chain_start = old_next;
309 old_prev->_next = old_next;
311 old_next->_prev = old_prev;
312 old_next->_next = wmeta->active;
313 wmeta->active->_prev = old_next;
319 static void shift_win_backward(struct WinMeta * wmeta)
321 if (wmeta->active == wmeta->_chain_start)
323 wmeta->_chain_start = wmeta->active->_next;
324 wmeta->_chain_start->_prev = 0;
325 wmeta->active->_prev = wmeta->_chain_end;
326 wmeta->active->_prev->_next = wmeta->active;
327 wmeta->_chain_end = wmeta->active;
328 wmeta->_chain_end->_next = 0;
332 struct Win * old_prev = wmeta->active->_prev;
333 struct Win * old_next = wmeta->active->_next;
334 if (wmeta->_chain_start == wmeta->active->_prev)
336 wmeta->_chain_start = wmeta->active;
337 wmeta->active->_prev = 0;
341 wmeta->active->_prev = old_prev->_prev;
342 wmeta->active->_prev->_next = wmeta->active;
344 if (wmeta->_chain_end == wmeta->active)
346 wmeta->_chain_end = old_prev;
350 old_next->_prev = old_prev;
352 old_prev->_next = old_next;
353 old_prev->_prev = wmeta->active;
354 wmeta->active->_next = old_prev;
360 extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta)
362 wmeta->_screen = screen;
363 wmeta->padframe.size.y = getmaxy(screen);
364 wmeta->padframe.size.x = getmaxx(screen);
365 wmeta->_chain_start = 0;
366 wmeta->_chain_end = 0;
367 wmeta->pad_offset = 0;
369 test = newpad(wmeta->padframe.size.y, 1);
374 wmeta->padframe.curses_win = test;
381 extern struct Win init_win(struct WinMeta * wmeta, char * title,
382 uint16_t height, uint16_t width,
383 void * data, void * func)
388 w.frame.curses_win = 0;
394 w.frame.size.x = width;
400 if (height > 0 && height <= wmeta->padframe.size.y - 1)
402 w.frame.size.y = height;
406 w.frame.size.y = wmeta->padframe.size.y - 1;
413 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
415 if (0 != wmeta->_chain_start)
417 w->_prev = wmeta->_chain_end;
418 wmeta->_chain_end->_next = w;
423 wmeta->_chain_start = w;
425 wmeta->_chain_end = w;
426 return update_wins(wmeta, w);
431 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
435 if (wmeta->_chain_start != w)
437 w->_prev->_next = w->_next;
441 wmeta->_chain_start = w->_next;
443 char pad_refitted = 0;
444 if (wmeta->_chain_end != w)
446 w->_next->_prev = w->_prev;
447 if (wmeta->active == w)
449 wmeta->active = w->_next;
451 if (0 != update_wins(wmeta, w->_next)) /* Positioning of successor */
452 { /* windows may be affected / */
453 return 1; /* need correction. Note that */
454 } /* update_wins() already */
455 pad_refitted = 1; /* refits the pad, voiding */
456 } /* later need for that. */
459 wmeta->_chain_end = w->_prev;
460 if (wmeta->active == w)
462 wmeta->active = w->_prev;
469 if (0 == pad_refitted)
471 return refit_pad(wmeta);
478 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
481 && (new_offset < wmeta->pad_offset
482 || new_offset + wmeta->padframe.size.x
483 < getmaxx(wmeta->padframe.curses_win)))
485 wmeta->pad_offset = new_offset;
491 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
493 if (0 != wmeta->active
494 && size.x > 0 && size.y > 0
495 && size.y < wmeta->padframe.size.y)
497 wmeta->active->frame.size = size;
498 return update_wins(wmeta, wmeta->_chain_start); /* Following windows' */
499 } /* positioning may be */
500 return 0; /* affected. */
501 } /* TODO: Why start at */
502 /* chain_start then? */
505 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
507 if (0 != wmeta->active)
511 if (wmeta->active->_next != 0)
513 wmeta->active = wmeta->active->_next;
517 wmeta->active = wmeta->_chain_start;
522 if (wmeta->active->_prev != 0)
524 wmeta->active = wmeta->active->_prev;
528 wmeta->active = wmeta->_chain_end;
536 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
538 if ( 0 == wmeta->active /* No shifting with <2 */
539 || wmeta->_chain_start == wmeta->_chain_end /* windows visible or */
540 || (dir != 'f' && dir != 'b')) /* wrong direction char. */
546 shift_win_forward(wmeta);
550 shift_win_backward(wmeta);
552 return update_wins(wmeta, wmeta->_chain_start);
557 extern uint8_t draw_all_wins(struct WinMeta * wmeta)
559 /* Empty everything before filling it a-new. */
561 wnoutrefresh(wmeta->_screen);
562 werase(wmeta->padframe.curses_win);
563 if (wmeta->_chain_start)
566 /* Draw windows' contents first, then their borders. */
567 draw_wins(wmeta->_chain_start);
568 draw_wins_borderlines(wmeta->_chain_start, wmeta->active,
569 wmeta->padframe.curses_win);
570 draw_wins_bordercorners(wmeta->_chain_start,wmeta->padframe.curses_win);
572 /* Draw virtual screen scroll hints. */
573 if (wmeta->pad_offset > 0)
575 if (draw_scroll_hint(&wmeta->padframe,
576 wmeta->pad_offset, wmeta->pad_offset + 1, '<'))
581 if (wmeta->pad_offset + wmeta->padframe.size.x
582 < getmaxx(wmeta->padframe.curses_win) - 1)
584 if (draw_scroll_hint(&wmeta->padframe,
585 wmeta->pad_offset + wmeta->padframe.size.x - 1,
586 getmaxx(wmeta->padframe.curses_win)
587 - (wmeta->pad_offset + wmeta->padframe.size.x),
594 /* Write virtual screen segment to be shown on physical screen into */
595 /* ncurses screen buffer. */
596 pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
597 wmeta->padframe.size.y, wmeta->padframe.size.x-1);
600 /* Only at the end write accumulated changes to the physical screen. */
607 extern uint8_t draw_scroll_hint(struct Frame * frame, uint16_t pos,
608 uint32_t dist, char dir)
610 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
611 char * more = "more";
612 char * unit_cols = "columns";
613 char * unit_rows = "lines";
614 uint16_t dsc_space = frame->size.x;
615 char * unit = unit_rows;
616 if ('<' == dir || '>' == dir)
618 dsc_space = frame->size.y;
621 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
622 + 10); /* 10 = uint32 max strlen */
623 if (NULL == scrolldsc)
627 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
629 /* Decide on offset of the description text inside the scroll hint line. */
631 if (dsc_space > strlen(scrolldsc) + 1)
633 offset = (dsc_space - strlen(scrolldsc)) / 2;
636 /* Draw scroll hint line as dir symbols bracketing description text. */
638 for (q = 0; q < dsc_space; q++)
640 if (q >= offset && q < strlen(scrolldsc) + offset)
642 symbol = scrolldsc[q - offset] | A_REVERSE;
646 symbol = dir | A_REVERSE;
648 if ('<' == dir || '>' == dir)
650 mvwaddch(frame->curses_win, q, pos, symbol);
654 mvwaddch(frame->curses_win, pos, q, symbol);