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)
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)
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. */
454 } /* Note that update_wins() */
455 pad_refitted = 1; /* already refits the pad, */
456 } /* voiding 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); /* Succeeding 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 void shift_active_win(struct WinMeta * wmeta, char dir)
538 if ( 0 == wmeta->active /* No shifting with less */
539 || wmeta->chain_start == wmeta->chain_end /* than two windows visible */
540 || (dir != 'f' && dir != 'b')) /* or wrong direction char. */
546 shift_win_forward(wmeta);
550 shift_win_backward(wmeta);
552 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);