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 static uint8_t refit_pad(struct WinMeta * wmeta)
63 /* Determine rightmost window column. */
64 uint16_t lastwincol = 0;
65 struct Win * w_p = wmeta->chain_start;
68 if (w_p->start.x + w_p->frame.size.x > lastwincol + 1)
70 lastwincol = w_p->start.x + w_p->frame.size.x - 1;
75 /* Only resize the pad if the rightmost window column has changed. */
76 if (getmaxx(wmeta->padframe.curses_win) != lastwincol)
78 return (ERR == wresize(wmeta->padframe.curses_win,
79 getmaxy(wmeta->padframe.curses_win),
87 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w)
89 if (0 != w->frame.curses_win)
94 if (0 != refit_pad(wmeta))
98 WINDOW * test = subpad(wmeta->padframe.curses_win,
99 w->frame.size.y, w->frame.size.x,
100 w->start.y, w->start.x);
105 w->frame.curses_win = test;
108 return update_wins(wmeta, w->next);
115 static void place_win(struct WinMeta * wmeta, struct Win * w)
117 /* First window goes into the upper-left corner. */
119 w->start.y = 1; /* Leave space for title bar. */
123 /* Non-first window fallbacks to: fit rightwards of rightmost border. */
124 struct Win * w_top = w->prev;
125 while (w_top->start.y != 1)
129 w->start.x = w_top->start.x + w_top->frame.size.x + 1;
131 /* Fit window below its predecessor if that one directly thrones over
132 * empty space wide and high enough.
134 uint16_t w_prev_maxy = w->prev->start.y
135 + getmaxy(w->prev->frame.curses_win);
136 if ( w->frame.size.x <= w->prev->frame.size.x
137 && w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy)
139 w->start.x = w->prev->start.x;
140 w->start.y = w_prev_maxy + 1;
143 /* Failing that, try to open a new sub column below the nearest
144 * predecessor window that thrones over enough empty space.
148 struct Win * w_up = w->prev;
149 struct Win * w_upup = w_up;
151 while (w_up != w_top)
156 if (w_up->start.y != w_upup->start.y)
160 w_upup = w_upup->prev;
162 w_prev_maxy = w_upup->start.y
163 + getmaxy(w_upup->frame.curses_win);
164 widthdiff = (w_upup->start.x + w_upup->frame.size.x)
165 - (w_up->start.x + w_up->frame.size.x);
166 if ( w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy
167 && w->frame.size.x < widthdiff)
169 w->start.x = w_up->start.x + w_up->frame.size.x + 1 ;
170 w->start.y = w_prev_maxy + 1;
181 static void destroy_win(struct Win * w)
183 delwin(w->frame.curses_win);
184 w->frame.curses_win = 0;
189 static void draw_wins(struct Win * w)
200 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
202 /* Draw vertical and horizontal border lines. */
204 for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++)
206 mvwaddch(pad, y, w->start.x - 1, '|');
207 mvwaddch(pad, y, w->start.x + w->frame.size.x, '|');
209 for (x = w->start.x; x <= w->start.x + w->frame.size.x; x++)
211 mvwaddch(pad, w->start.y - 1, x, '-');
212 mvwaddch(pad, w->start.y + w->frame.size.y, x, '-');
215 /* Draw as much as possible of the title into center of top border line. */
216 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
217 if (w->frame.size.x >= min_title_length_visible)
219 uint16_t title_offset = 0;
220 if (w->frame.size.x > strlen(w->title) + 2)
222 title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2;
223 } /* +2 is for padding/decoration */
224 uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);
225 char title[length_visible + 3];
226 char decoration = ' ';
231 memcpy(title + 1, w->title, length_visible);
232 title[0] = title[length_visible + 1] = decoration;
233 title[length_visible + 2] = '\0';
234 mvwaddstr(pad, w->start.y - 1, w->start.x + title_offset, title);
240 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
248 draw_win_borderlines(w, active, pad);
251 draw_wins_borderlines(w->next, w_active, pad);
257 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
259 mvwaddch(pad, w->start.y - 1, w->start.x - 1, '+');
260 mvwaddch(pad, w->start.y - 1, w->start.x + w->frame.size.x, '+');
261 mvwaddch(pad, w->start.y + w->frame.size.y, w->start.x - 1, '+');
263 w->start.y + w->frame.size.y, w->start.x + w->frame.size.x, '+');
266 draw_wins_bordercorners(w->next, pad);
272 extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta * wmeta)
274 wmeta->screen = screen;
275 wmeta->padframe.size.y = getmaxy(screen);
276 wmeta->padframe.size.x = getmaxx(screen);
277 wmeta->chain_start = 0;
278 wmeta->chain_end = 0;
279 wmeta->pad_offset = 0;
281 test = newpad(wmeta->padframe.size.y, 1);
286 wmeta->padframe.curses_win = test;
293 extern struct Win init_win(struct WinMeta * wmeta, char * title,
294 uint16_t height, uint16_t width,
295 void * data, void * func)
300 w.frame.curses_win = 0;
306 w.frame.size.x = width;
312 if (height > 0 && height <= wmeta->padframe.size.y - 1)
314 w.frame.size.y = height;
318 w.frame.size.y = wmeta->padframe.size.y - 1;
325 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
327 if (0 != wmeta->chain_start)
329 w->prev = wmeta->chain_end;
330 wmeta->chain_end->next = w;
335 wmeta->chain_start = w;
337 wmeta->chain_end = w;
338 return update_wins(wmeta, w);
343 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
347 if (wmeta->chain_start != w)
349 w->prev->next = w->next;
353 wmeta->chain_start = w->next;
355 char pad_refitted = 0;
356 if (wmeta->chain_end != w)
358 w->next->prev = w->prev;
359 if (wmeta->active == w)
361 wmeta->active = w->next;
363 if (0 != update_wins(wmeta, w->next)) /* Positioning of successor */
364 { /* windows may be affected / */
365 return 1; /* need correction. */
366 } /* Note that update_wins() */
367 pad_refitted = 1; /* already refits the pad, */
368 } /* voiding later need for that. */
371 wmeta->chain_end = w->prev;
372 if (wmeta->active == w)
374 wmeta->active = w->prev;
381 if (0 == pad_refitted)
383 return refit_pad(wmeta);
390 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
393 && (new_offset < wmeta->pad_offset
394 || new_offset + wmeta->padframe.size.x
395 < getmaxx(wmeta->padframe.curses_win)))
397 wmeta->pad_offset = new_offset;
403 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
405 if (0 != wmeta->active
406 && size.x > 0 && size.y > 0
407 && size.y < wmeta->padframe.size.y)
409 wmeta->active->frame.size = size;
410 return update_wins(wmeta, wmeta->chain_start); /* Succeeding windows' */
411 } /* positioning may be */
412 return 0; /* affected. */
413 } /* TODO: Why start at */
414 /* chain_start then? */
417 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
419 if (0 != wmeta->active)
423 if (wmeta->active->next != 0)
425 wmeta->active = wmeta->active->next;
429 wmeta->active = wmeta->chain_start;
434 if (wmeta->active->prev != 0)
436 wmeta->active = wmeta->active->prev;
440 wmeta->active = wmeta->chain_end;
448 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
450 if (0 != wmeta->active /* No shifting with less */
451 && wmeta->chain_start != wmeta->chain_end /* than one window visible. */
452 && (dir == 'f' || dir == 'b'))
454 struct Win * w_shift = wmeta->active, * w_p, * w_p_next;
456 /* Check if shifting will lead to wrapping. */
458 if ( (dir == 'f' && w_shift == wmeta->chain_end)
459 || (dir == 'b' && w_shift == wmeta->chain_start))
464 /* Suspend all visible windows, remember their order in wins[]. */
466 for (w_p = wmeta->chain_start, i_max = 1;
467 w_p != wmeta->chain_end;
472 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
477 for (i = 0, w_p = wmeta->chain_start;
481 w_p_next = w_p->next;
482 suspend_win(wmeta, w_p);
487 /* Re-append all previously visible windows in the new order. */
492 if (0 != append_win(wmeta, w_shift))
496 for (i = 0; i < i_max - 1; i++)
498 if (0 != append_win(wmeta, wins[i]))
506 for (i = 1; i < i_max; i++)
508 if (0 != append_win(wmeta, wins[i]))
513 if (0 != append_win(wmeta, w_shift))
521 for (i = 0; i < i_max; i++)
523 if ( (dir == 'f' && w_shift == wins[i])
524 || (dir == 'b' && w_shift == wins[i+1]))
526 if ( 0 != append_win(wmeta, wins[i+1])
527 || 0 != append_win(wmeta, wins[i]))
535 if (0 != append_win(wmeta, wins[i]))
544 wmeta->active = w_shift; /* Otherwise lastly appended win is active. */
551 extern void draw_all_wins(struct WinMeta * wmeta)
553 /* Empty everything before filling it a-new. */
555 wnoutrefresh(wmeta->screen);
556 werase(wmeta->padframe.curses_win);
557 if (wmeta->chain_start)
560 /* Draw windows' contents first, then their borders. */
561 draw_wins(wmeta->chain_start);
562 draw_wins_borderlines(wmeta->chain_start, wmeta->active,
563 wmeta->padframe.curses_win);
564 draw_wins_bordercorners(wmeta->chain_start, wmeta->padframe.curses_win);
566 /* Draw virtual screen scroll hints. */
567 if (wmeta->pad_offset > 0)
569 draw_scroll_hint(&wmeta->padframe,
570 wmeta->pad_offset, wmeta->pad_offset + 1, '<');
572 if (wmeta->pad_offset + wmeta->padframe.size.x
573 < getmaxx(wmeta->padframe.curses_win) - 1)
575 draw_scroll_hint(&wmeta->padframe,
576 wmeta->pad_offset + wmeta->padframe.size.x - 1,
577 getmaxx(wmeta->padframe.curses_win)
578 - (wmeta->pad_offset + wmeta->padframe.size.x),
582 /* Write virtual screen segment to be shown on physical screen into */
583 /* ncurses screen buffer. */
584 pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
585 wmeta->padframe.size.y, wmeta->padframe.size.x-1);
588 /* Only at the end write accumulated changes to the physical screen. */
594 extern uint8_t draw_scroll_hint(struct Frame * frame, uint16_t pos,
595 uint32_t dist, char dir)
597 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
598 char * more = "more";
599 char * unit_cols = "columns";
600 char * unit_rows = "lines";
601 uint16_t dsc_space = frame->size.x;
602 char * unit = unit_rows;
603 if ('<' == dir || '>' == dir)
605 dsc_space = frame->size.y;
608 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
609 + 10); /* 10 = uint32 max strlen */
610 if (NULL == scrolldsc)
614 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
616 /* Decide on offset of the description text inside the scroll hint line. */
618 if (dsc_space > strlen(scrolldsc) + 1)
620 offset = (dsc_space - strlen(scrolldsc)) / 2;
623 /* Draw scroll hint line as dir symbols bracketing description text. */
625 for (q = 0; q < dsc_space; q++)
627 if (q >= offset && q < strlen(scrolldsc) + offset)
629 symbol = scrolldsc[q - offset] | A_REVERSE;
633 symbol = dir | A_REVERSE;
635 if ('<' == dir || '>' == dir)
637 mvwaddch(frame->curses_win, q, pos, symbol);
641 mvwaddch(frame->curses_win, pos, q, symbol);