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_borders()) 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_borders() 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_borders() Draws the vertical and horizontal borders of window "w"
61 * 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_borders().
65 * draw_wins_borders() calls draw_win_borders() recursively on all windows from
66 * "w" on. It also fills "corners" with coordinates of each window's corners,
67 * iterating over its Corners structs via the "i" index incremented by 1 over
68 * each handled window. "w_active" is a pointer to the one window that
69 * draw_win_borders() is supposed to handle as the active window.
71 static void draw_win_borders(struct Win * w, char active);
72 static void draw_wins_borders(struct Win * w, struct Win * w_active,
73 struct Corners * corners, uint16_t i);
77 static void refit_pad(struct WinMeta * wmeta)
79 /* Determine rightmost window column. */
80 uint16_t lastwincol = 0;
81 struct Win * w_p = wmeta->chain_start;
84 if (w_p->start.x + w_p->frame.size.x > lastwincol + 1)
86 lastwincol = w_p->start.x + w_p->frame.size.x - 1;
91 /* Only resize the pad if the rightmost window column has changed. */
92 if (getmaxx(wmeta->pad.curses_win) != lastwincol)
94 wresize(wmeta->pad.curses_win,
95 getmaxy(wmeta->pad.curses_win), lastwincol + 2);
101 static void update_wins (struct WinMeta * wmeta, struct Win * w)
103 if (0 != w->frame.curses_win)
109 w->frame.curses_win = subpad(wmeta->pad.curses_win,
110 w->frame.size.y, w->frame.size.x,
111 w->start.y, w->start.x);
114 update_wins (wmeta, w->next);
120 static void place_win (struct WinMeta * wmeta, struct Win * w)
122 /* First window goes into the upper-left corner. */
124 w->start.y = 1; /* Leave space for title bar. */
128 /* Non-first window fallbacks to: fit rightwards of rightmost border. */
129 struct Win * w_top = w->prev;
130 while (w_top->start.y != 1)
134 w->start.x = w_top->start.x + w_top->frame.size.x + 1;
136 /* Fit window below its predecessor if that one directly thrones over
137 * empty space wide and high enough.
139 uint16_t w_prev_maxy = w->prev->start.y
140 + getmaxy(w->prev->frame.curses_win);
141 if ( w->frame.size.x <= w->prev->frame.size.x
142 && w->frame.size.y < wmeta->pad.size.y - w_prev_maxy)
144 w->start.x = w->prev->start.x;
145 w->start.y = w_prev_maxy + 1;
148 /* Failing that, try to open a new sub column below the nearest
149 * predecessor window that thrones over enough empty space.
153 struct Win * w_up = w->prev;
154 struct Win * w_upup = w_up;
156 while (w_up != w_top)
161 if (w_up->start.y != w_upup->start.y)
165 w_upup = w_upup->prev;
167 w_prev_maxy = w_upup->start.y
168 + getmaxy(w_upup->frame.curses_win);
169 widthdiff = (w_upup->start.x + w_upup->frame.size.x)
170 - (w_up->start.x + w_up->frame.size.x);
171 if ( w->frame.size.y < wmeta->pad.size.y - w_prev_maxy
172 && w->frame.size.x < widthdiff)
174 w->start.x = w_up->start.x + w_up->frame.size.x + 1 ;
175 w->start.y = w_prev_maxy + 1;
186 static void destroy_win (struct Win * w)
188 delwin(w->frame.curses_win);
189 w->frame.curses_win = 0;
194 static void draw_wins (struct Win * w)
205 static void draw_win_borders(struct Win * w, char active)
207 /* Draw vertical and horizontal border lines. */
209 for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++)
211 mvwaddch(wgetparent(w->frame.curses_win), y, w->start.x - 1, '|');
212 mvwaddch(wgetparent(w->frame.curses_win),
213 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(wgetparent(w->frame.curses_win), w->start.y - 1, x, '-');
218 mvwaddch(wgetparent(w->frame.curses_win),
219 w->start.y + w->frame.size.y, x, '-');
222 /* Draw as much as possible of the title into center of top border line. */
223 char min_title_length_visible = 3; /* minimum 1 char + 2 padding/decoration */
224 if (w->frame.size.x >= min_title_length_visible)
226 uint16_t title_offset = 0;
227 if (w->frame.size.x > strlen(w->title) + 2)
229 title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2; // + 2 is for decoration
231 uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);
232 char title[length_visible + 3];
233 char decoration = ' ';
238 memcpy(title + 1, w->title, length_visible);
239 title[0] = title[length_visible + 1] = decoration;
240 title[length_visible + 2] = '\0';
241 mvwaddstr(wgetparent(w->frame.curses_win),
242 w->start.y - 1, w->start.x + title_offset, title);
246 static void draw_wins_borders(struct Win * w, struct Win * w_active,
247 struct Corners * corners, uint16_t i)
254 draw_win_borders(w, active);
255 corners[i].tl.y = w->start.y - 1;
256 corners[i].tl.x = w->start.x - 1;
257 corners[i].tr.y = w->start.y - 1;
258 corners[i].tr.x = w->start.x + w->frame.size.x;
259 corners[i].bl.y = w->start.y + w->frame.size.y;
260 corners[i].bl.x = w->start.x - 1;
261 corners[i].br.y = w->start.y + w->frame.size.y;
262 corners[i].br.x = w->start.x + w->frame.size.x;
265 draw_wins_borders (w->next, w_active, corners, i + 1);
271 extern struct WinMeta init_win_meta(WINDOW * screen)
273 struct WinMeta wmeta;
274 wmeta.screen = screen;
275 wmeta.pad.size.y = getmaxy(screen);
276 wmeta.pad.size.x = getmaxx(screen);
277 wmeta.chain_start = 0;
279 wmeta.pad_offset = 0;
280 wmeta.pad.curses_win = newpad(wmeta.pad.size.y, 1);
287 extern struct Win init_win(struct WinMeta * wmeta, char * title,
288 void * data, void * func)
293 w.frame.curses_win = 0;
296 w.frame.size.y = wmeta->pad.size.y - 1;
304 extern void append_win(struct WinMeta * wmeta, struct Win * w)
306 if (0 != wmeta->chain_start)
308 w->prev = wmeta->chain_end;
309 wmeta->chain_end->next = w;
314 wmeta->chain_start = w;
316 wmeta->chain_end = w;
317 update_wins(wmeta, w);
322 extern void suspend_win(struct WinMeta * wmeta, struct Win * w)
326 if (wmeta->chain_start != w)
328 w->prev->next = w->next;
332 wmeta->chain_start = w->next;
334 char pad_refitted = 0;
335 if (wmeta->chain_end != w)
337 w->next->prev = w->prev;
338 if (wmeta->active == w)
340 wmeta->active = w->next;
342 update_wins(wmeta, w->next); /* Positioning of successor windows may */
343 pad_refitted = 1; /* be affected / need correction. Note */
344 } /* that update_wins() already refits the */
345 else /* pad, voiding later need for that. */
347 wmeta->chain_end = w->prev;
348 if (wmeta->active == w)
350 wmeta->active = w->prev;
357 if (0 == pad_refitted)
365 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
368 && (new_offset < wmeta->pad_offset
369 || new_offset + wmeta->pad.size.x < getmaxx(wmeta->pad.curses_win)))
371 wmeta->pad_offset = new_offset;
377 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
379 if (0 != wmeta->active
380 && size.x > 0 && size.y > 0
381 && size.y < wmeta->pad.size.y)
383 wmeta->active->frame.size = size;
384 update_wins(wmeta, wmeta->chain_start); /* Positioning of successor */
385 } /* windows may be affected. */
390 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
392 if (0 != wmeta->active)
396 if (wmeta->active->next != 0)
398 wmeta->active = wmeta->active->next;
402 wmeta->active = wmeta->chain_start;
407 if (wmeta->active->prev != 0)
409 wmeta->active = wmeta->active->prev;
413 wmeta->active = wmeta->chain_end;
421 extern void shift_active_win(struct WinMeta * wmeta, char dir)
423 if (0 != wmeta->active /* No shifting with less */
424 && wmeta->chain_start != wmeta->chain_end /* than one window visible. */
425 && (dir == 'f' || dir == 'b'))
427 struct Win * w_shift = wmeta->active, * w_p, * w_p_next;
429 /* Check if shifting will lead to wrapping. */
431 if ( (dir == 'f' && w_shift == wmeta->chain_end)
432 || (dir == 'b' && w_shift == wmeta->chain_start))
437 /* Suspend all visible windows. */
439 for (i_max = 1, w_p = wmeta->chain_start;
440 w_p != wmeta->chain_end;
445 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
446 for (i = 0, w_p = wmeta->chain_start; i < i_max; i++)
448 w_p_next = w_p->next;
449 suspend_win(wmeta, w_p);
454 /* Re-append all previously visible windows in the new order. */
459 append_win(wmeta, w_shift);
460 for (i = 0; i < i_max - 1; i++)
462 append_win(wmeta, wins[i]);
467 for (i = 1; i < i_max; i++)
469 append_win(wmeta, wins[i]);
471 append_win(wmeta, w_shift);
476 for (i = 0; i < i_max; i++)
478 if ( (dir == 'f' && w_shift == wins[i])
479 || (dir == 'b' && w_shift == wins[i+1]))
481 append_win(wmeta, wins[i+1]);
482 append_win(wmeta, wins[i]);
487 append_win(wmeta, wins[i]);
493 wmeta->active = w_shift; /* TODO: Is this necessary? If so, why? */
499 extern void draw_all_wins(struct WinMeta * wmeta)
501 /* Empty everything before filling it a-new. */
503 wnoutrefresh(wmeta->screen);
504 werase(wmeta->pad.curses_win);
505 if (wmeta->chain_start)
508 /* Only draw the windows' *contents* first. */
509 draw_wins (wmeta->chain_start);
511 /* Draw windows' borders. Lines first, then line crossings / corners. */
512 uint16_t n_wins = 1, i;
513 struct Win * win_p = wmeta->chain_start;
514 while (0 != win_p->next)
519 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
520 draw_wins_borders (wmeta->chain_start, wmeta->active, all_corners, 0);
521 for (i = 0; i < n_wins; i++)
523 mvwaddch(wmeta->pad.curses_win,
524 all_corners[i].tl.y, all_corners[i].tl.x, '+');
525 mvwaddch(wmeta->pad.curses_win,
526 all_corners[i].tr.y, all_corners[i].tr.x, '+');
527 mvwaddch(wmeta->pad.curses_win,
528 all_corners[i].bl.y, all_corners[i].bl.x, '+');
529 mvwaddch(wmeta->pad.curses_win,
530 all_corners[i].br.y, all_corners[i].br.x, '+');
534 /* Draw virtual screen scroll hints. */
535 if (wmeta->pad_offset > 0)
537 draw_scroll_hint(&wmeta->pad,
538 wmeta->pad_offset, wmeta->pad_offset + 1, '<');
540 if (wmeta->pad_offset + wmeta->pad.size.x
541 < getmaxx(wmeta->pad.curses_win) - 1)
543 draw_scroll_hint(&wmeta->pad,
544 wmeta->pad_offset + wmeta->pad.size.x - 1,
545 getmaxx(wmeta->pad.curses_win)
546 - (wmeta->pad_offset + wmeta->pad.size.x), '>');
549 /* Write virtual screen segment to be shown on physical screen into */
550 /* ncurses screen buffer. */
551 pnoutrefresh(wmeta->pad.curses_win, 0, wmeta->pad_offset, 0, 0,
552 wmeta->pad.size.y, wmeta->pad.size.x-1);
555 /* Only at the end write accumulated changes to the physical screen. */
561 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
564 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
565 char * more = "more";
566 char * unit_cols = "columns";
567 char * unit_rows = "lines";
568 uint16_t dsc_space = frame->size.x;
569 char * unit = unit_rows;
570 if ('<' == dir || '>' == dir)
572 dsc_space = frame->size.y;
575 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
576 + 10); /* 10 = uint32 max strlen */
577 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
579 /* Decide on offset of the description text inside the scroll hint line. */
581 if (dsc_space > strlen(scrolldsc) + 1)
583 offset = (dsc_space - strlen(scrolldsc)) / 2;
586 /* Draw scroll hint line as dir symbols bracketing description text. */
588 for (q = 0; q < dsc_space; q++)
590 if (q >= offset && q < strlen(scrolldsc) + offset)
592 symbol = scrolldsc[q - offset] | A_REVERSE;
596 symbol = dir | A_REVERSE;
598 if ('<' == dir || '>' == dir)
600 mvwaddch(frame->curses_win, q, pos, symbol);
604 mvwaddch(frame->curses_win, pos, q, symbol);