4 #include <stdint.h> /* for uint16_t, uint32_t */
5 #include <ncurses.h> /* for LOTS of stuff */
6 #include <stdlib.h> /* for malloc(), free() */
7 #include <string.h> /* for strlen(), memcpy() */
8 #include "yx_uint16.h" /* for yx_uint16 coordinates */
12 /* Stores a window's border corners. This is a helper to draw_all_wins() (and
13 * filled by its helper draw_wins_borderlines()) which draws the horizontal and
14 * vertical lines of all windows' borders first and the corner characters of
15 * all windows only afterwards (so that corners are not overwritten by lines).
16 * This delay of corner drawing necessitates temporarily storing their
17 * coordinates (harvested during the previous border drawing activities) in a
18 * series of such Corners structs to be released at the end.
20 * TODO: Maybe replace this complicated method by dropping the harvesting of
21 * corners from draw_wins_borderlines() and instead collecting them in a second
22 * border drawing cycle that repeats some cycles but works in a much more
23 * straightforward way.
35 /* Fit virtual screen's width to minimum width demanded by current windows'
38 static void refit_pad(struct WinMeta * wmeta);
42 /* Update geometry (sizes, positions) of window "w" and its successors in the
43 * window chain. For the positioning algorithm place_win() is used.
45 static void update_wins(struct WinMeta * wmeta, struct Win * w);
46 static void place_win(struct WinMeta * wmeta, struct Win * w);
50 /* Destroy window "w"'s ncurses window (and set w.Frame.curses_win to 0). */
51 static void destroy_win(struct Win * w);
55 /* Draw contents of all windows in window chain from window "w" onwards. */
56 static void draw_wins(struct Win * w);
60 /* draw_win_borderlines() draws the vertical and horizontal borders of window
61 * "w" sans corners, and draws the top border line as the windows' title bar
62 * (highlighted if the window is described active by "active" being set).
63 * draw_wins_borderlines().
65 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
66 * windows from "w" on. It also fills "corners" with coordinates of each
67 * window's corners, iterating over its Corners structs via the "i" index
68 * incremented by 1 over each handled window. "w_active" is a pointer to the
69 * one window that draw_win_borderlines() is supposed to handle as the active
72 static void draw_win_borderlines(struct Win * w, char active);
73 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
74 struct Corners * corners, uint16_t i);
78 static void refit_pad(struct WinMeta * wmeta)
80 /* Determine rightmost window column. */
81 uint16_t lastwincol = 0;
82 struct Win * w_p = wmeta->chain_start;
85 if (w_p->start.x + w_p->frame.size.x > lastwincol + 1)
87 lastwincol = w_p->start.x + w_p->frame.size.x - 1;
92 /* Only resize the pad if the rightmost window column has changed. */
93 if (getmaxx(wmeta->pad.curses_win) != lastwincol)
95 wresize(wmeta->pad.curses_win,
96 getmaxy(wmeta->pad.curses_win), lastwincol + 2);
102 static void update_wins (struct WinMeta * wmeta, struct Win * w)
104 if (0 != w->frame.curses_win)
110 w->frame.curses_win = subpad(wmeta->pad.curses_win,
111 w->frame.size.y, w->frame.size.x,
112 w->start.y, w->start.x);
115 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->pad.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->pad.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)
208 /* Draw vertical and horizontal border lines. */
210 for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++)
212 mvwaddch(wgetparent(w->frame.curses_win), y, w->start.x - 1, '|');
213 mvwaddch(wgetparent(w->frame.curses_win),
214 y, w->start.x + w->frame.size.x, '|');
216 for (x = w->start.x; x <= w->start.x + w->frame.size.x; x++)
218 mvwaddch(wgetparent(w->frame.curses_win), w->start.y - 1, x, '-');
219 mvwaddch(wgetparent(w->frame.curses_win),
220 w->start.y + w->frame.size.y, x, '-');
223 /* Draw as much as possible of the title into center of top border line. */
224 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
225 if (w->frame.size.x >= min_title_length_visible)
227 uint16_t title_offset = 0;
228 if (w->frame.size.x > strlen(w->title) + 2)
230 title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2;
231 } /* +2 is for padding/decoration */
232 uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);
233 char title[length_visible + 3];
234 char decoration = ' ';
239 memcpy(title + 1, w->title, length_visible);
240 title[0] = title[length_visible + 1] = decoration;
241 title[length_visible + 2] = '\0';
242 mvwaddstr(wgetparent(w->frame.curses_win),
243 w->start.y - 1, w->start.x + title_offset, title);
249 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
250 struct Corners * corners, uint16_t i)
257 draw_win_borderlines(w, active);
258 corners[i].tl.y = w->start.y - 1;
259 corners[i].tl.x = w->start.x - 1;
260 corners[i].tr.y = w->start.y - 1;
261 corners[i].tr.x = w->start.x + w->frame.size.x;
262 corners[i].bl.y = w->start.y + w->frame.size.y;
263 corners[i].bl.x = w->start.x - 1;
264 corners[i].br.y = w->start.y + w->frame.size.y;
265 corners[i].br.x = w->start.x + w->frame.size.x;
268 draw_wins_borderlines (w->next, w_active, corners, i + 1);
274 extern struct WinMeta init_win_meta(WINDOW * screen)
276 struct WinMeta wmeta;
277 wmeta.screen = screen;
278 wmeta.pad.size.y = getmaxy(screen);
279 wmeta.pad.size.x = getmaxx(screen);
280 wmeta.chain_start = 0;
282 wmeta.pad_offset = 0;
283 wmeta.pad.curses_win = newpad(wmeta.pad.size.y, 1);
290 extern struct Win init_win(struct WinMeta * wmeta, char * title,
291 void * data, void * func)
296 w.frame.curses_win = 0;
299 w.frame.size.y = wmeta->pad.size.y - 1;
307 extern void append_win(struct WinMeta * wmeta, struct Win * w)
309 if (0 != wmeta->chain_start)
311 w->prev = wmeta->chain_end;
312 wmeta->chain_end->next = w;
317 wmeta->chain_start = w;
319 wmeta->chain_end = w;
320 update_wins(wmeta, w);
325 extern void suspend_win(struct WinMeta * wmeta, struct Win * w)
329 if (wmeta->chain_start != w)
331 w->prev->next = w->next;
335 wmeta->chain_start = w->next;
337 char pad_refitted = 0;
338 if (wmeta->chain_end != w)
340 w->next->prev = w->prev;
341 if (wmeta->active == w)
343 wmeta->active = w->next;
345 update_wins(wmeta, w->next); /* Positioning of successor windows may */
346 pad_refitted = 1; /* be affected / need correction. Note */
347 } /* that update_wins() already refits the */
348 else /* pad, voiding later need for that. */
350 wmeta->chain_end = w->prev;
351 if (wmeta->active == w)
353 wmeta->active = w->prev;
360 if (0 == pad_refitted)
368 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
371 && (new_offset < wmeta->pad_offset
372 || new_offset + wmeta->pad.size.x < getmaxx(wmeta->pad.curses_win)))
374 wmeta->pad_offset = new_offset;
380 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
382 if (0 != wmeta->active
383 && size.x > 0 && size.y > 0
384 && size.y < wmeta->pad.size.y)
386 wmeta->active->frame.size = size;
387 update_wins(wmeta, wmeta->chain_start); /* Positioning of successor */
388 } /* windows may be affected. */
393 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
395 if (0 != wmeta->active)
399 if (wmeta->active->next != 0)
401 wmeta->active = wmeta->active->next;
405 wmeta->active = wmeta->chain_start;
410 if (wmeta->active->prev != 0)
412 wmeta->active = wmeta->active->prev;
416 wmeta->active = wmeta->chain_end;
424 extern void shift_active_win(struct WinMeta * wmeta, char dir)
426 if (0 != wmeta->active /* No shifting with less */
427 && wmeta->chain_start != wmeta->chain_end /* than one window visible. */
428 && (dir == 'f' || dir == 'b'))
430 struct Win * w_shift = wmeta->active, * w_p, * w_p_next;
432 /* Check if shifting will lead to wrapping. */
434 if ( (dir == 'f' && w_shift == wmeta->chain_end)
435 || (dir == 'b' && w_shift == wmeta->chain_start))
440 /* Suspend all visible windows, remember their order in wins[]. */
442 for (w_p = wmeta->chain_start, i_max = 1;
443 w_p != wmeta->chain_end;
448 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
449 for (i = 0, w_p = wmeta->chain_start;
453 w_p_next = w_p->next;
454 suspend_win(wmeta, w_p);
459 /* Re-append all previously visible windows in the new order. */
464 append_win(wmeta, w_shift);
465 for (i = 0; i < i_max - 1; i++)
467 append_win(wmeta, wins[i]);
472 for (i = 1; i < i_max; i++)
474 append_win(wmeta, wins[i]);
476 append_win(wmeta, w_shift);
481 for (i = 0; i < i_max; i++)
483 if ( (dir == 'f' && w_shift == wins[i])
484 || (dir == 'b' && w_shift == wins[i+1]))
486 append_win(wmeta, wins[i+1]);
487 append_win(wmeta, wins[i]);
492 append_win(wmeta, wins[i]);
498 wmeta->active = w_shift; /* Otherwise lastly appended win is active. */
504 extern void draw_all_wins(struct WinMeta * wmeta)
506 /* Empty everything before filling it a-new. */
508 wnoutrefresh(wmeta->screen);
509 werase(wmeta->pad.curses_win);
510 if (wmeta->chain_start)
513 /* Only draw the windows' *contents* first. */
514 draw_wins (wmeta->chain_start);
516 /* Draw windows' borders. Lines first, then line crossings / corners. */
517 uint16_t n_wins = 1, i;
518 struct Win * win_p = wmeta->chain_start;
519 while (0 != win_p->next)
524 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
525 draw_wins_borderlines(wmeta->chain_start, wmeta->active, all_corners, 0);
526 for (i = 0; i < n_wins; i++)
528 mvwaddch(wmeta->pad.curses_win,
529 all_corners[i].tl.y, all_corners[i].tl.x, '+');
530 mvwaddch(wmeta->pad.curses_win,
531 all_corners[i].tr.y, all_corners[i].tr.x, '+');
532 mvwaddch(wmeta->pad.curses_win,
533 all_corners[i].bl.y, all_corners[i].bl.x, '+');
534 mvwaddch(wmeta->pad.curses_win,
535 all_corners[i].br.y, all_corners[i].br.x, '+');
539 /* Draw virtual screen scroll hints. */
540 if (wmeta->pad_offset > 0)
542 draw_scroll_hint(&wmeta->pad,
543 wmeta->pad_offset, wmeta->pad_offset + 1, '<');
545 if (wmeta->pad_offset + wmeta->pad.size.x
546 < getmaxx(wmeta->pad.curses_win) - 1)
548 draw_scroll_hint(&wmeta->pad,
549 wmeta->pad_offset + wmeta->pad.size.x - 1,
550 getmaxx(wmeta->pad.curses_win)
551 - (wmeta->pad_offset + wmeta->pad.size.x), '>');
554 /* Write virtual screen segment to be shown on physical screen into */
555 /* ncurses screen buffer. */
556 pnoutrefresh(wmeta->pad.curses_win, 0, wmeta->pad_offset, 0, 0,
557 wmeta->pad.size.y, wmeta->pad.size.x-1);
560 /* Only at the end write accumulated changes to the physical screen. */
566 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
569 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
570 char * more = "more";
571 char * unit_cols = "columns";
572 char * unit_rows = "lines";
573 uint16_t dsc_space = frame->size.x;
574 char * unit = unit_rows;
575 if ('<' == dir || '>' == dir)
577 dsc_space = frame->size.y;
580 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
581 + 10); /* 10 = uint32 max strlen */
582 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
584 /* Decide on offset of the description text inside the scroll hint line. */
586 if (dsc_space > strlen(scrolldsc) + 1)
588 offset = (dsc_space - strlen(scrolldsc)) / 2;
591 /* Draw scroll hint line as dir symbols bracketing description text. */
593 for (q = 0; q < dsc_space; q++)
595 if (q >= offset && q < strlen(scrolldsc) + offset)
597 symbol = scrolldsc[q - offset] | A_REVERSE;
601 symbol = dir | A_REVERSE;
603 if ('<' == dir || '>' == dir)
605 mvwaddch(frame->curses_win, q, pos, symbol);
609 mvwaddch(frame->curses_win, pos, q, symbol);