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; /* min. 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;
230 } /* +2 is for padding/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);
248 static void draw_wins_borders(struct Win * w, struct Win * w_active,
249 struct Corners * corners, uint16_t i)
256 draw_win_borders(w, active);
257 corners[i].tl.y = w->start.y - 1;
258 corners[i].tl.x = w->start.x - 1;
259 corners[i].tr.y = w->start.y - 1;
260 corners[i].tr.x = w->start.x + w->frame.size.x;
261 corners[i].bl.y = w->start.y + w->frame.size.y;
262 corners[i].bl.x = w->start.x - 1;
263 corners[i].br.y = w->start.y + w->frame.size.y;
264 corners[i].br.x = w->start.x + w->frame.size.x;
267 draw_wins_borders (w->next, w_active, corners, i + 1);
273 extern struct WinMeta init_win_meta(WINDOW * screen)
275 struct WinMeta wmeta;
276 wmeta.screen = screen;
277 wmeta.pad.size.y = getmaxy(screen);
278 wmeta.pad.size.x = getmaxx(screen);
279 wmeta.chain_start = 0;
281 wmeta.pad_offset = 0;
282 wmeta.pad.curses_win = newpad(wmeta.pad.size.y, 1);
289 extern struct Win init_win(struct WinMeta * wmeta, char * title,
290 void * data, void * func)
295 w.frame.curses_win = 0;
298 w.frame.size.y = wmeta->pad.size.y - 1;
306 extern void append_win(struct WinMeta * wmeta, struct Win * w)
308 if (0 != wmeta->chain_start)
310 w->prev = wmeta->chain_end;
311 wmeta->chain_end->next = w;
316 wmeta->chain_start = w;
318 wmeta->chain_end = w;
319 update_wins(wmeta, w);
324 extern void suspend_win(struct WinMeta * wmeta, struct Win * w)
328 if (wmeta->chain_start != w)
330 w->prev->next = w->next;
334 wmeta->chain_start = w->next;
336 char pad_refitted = 0;
337 if (wmeta->chain_end != w)
339 w->next->prev = w->prev;
340 if (wmeta->active == w)
342 wmeta->active = w->next;
344 update_wins(wmeta, w->next); /* Positioning of successor windows may */
345 pad_refitted = 1; /* be affected / need correction. Note */
346 } /* that update_wins() already refits the */
347 else /* pad, voiding later need for that. */
349 wmeta->chain_end = w->prev;
350 if (wmeta->active == w)
352 wmeta->active = w->prev;
359 if (0 == pad_refitted)
367 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
370 && (new_offset < wmeta->pad_offset
371 || new_offset + wmeta->pad.size.x < getmaxx(wmeta->pad.curses_win)))
373 wmeta->pad_offset = new_offset;
379 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
381 if (0 != wmeta->active
382 && size.x > 0 && size.y > 0
383 && size.y < wmeta->pad.size.y)
385 wmeta->active->frame.size = size;
386 update_wins(wmeta, wmeta->chain_start); /* Positioning of successor */
387 } /* windows may be affected. */
392 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
394 if (0 != wmeta->active)
398 if (wmeta->active->next != 0)
400 wmeta->active = wmeta->active->next;
404 wmeta->active = wmeta->chain_start;
409 if (wmeta->active->prev != 0)
411 wmeta->active = wmeta->active->prev;
415 wmeta->active = wmeta->chain_end;
423 extern void shift_active_win(struct WinMeta * wmeta, char dir)
425 if (0 != wmeta->active /* No shifting with less */
426 && wmeta->chain_start != wmeta->chain_end /* than one window visible. */
427 && (dir == 'f' || dir == 'b'))
429 struct Win * w_shift = wmeta->active, * w_p, * w_p_next;
431 /* Check if shifting will lead to wrapping. */
433 if ( (dir == 'f' && w_shift == wmeta->chain_end)
434 || (dir == 'b' && w_shift == wmeta->chain_start))
439 /* Suspend all visible windows. */
441 for (i_max = 1, w_p = wmeta->chain_start;
442 w_p != wmeta->chain_end;
447 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
448 for (i = 0, w_p = wmeta->chain_start; i < i_max; i++)
450 w_p_next = w_p->next;
451 suspend_win(wmeta, w_p);
456 /* Re-append all previously visible windows in the new order. */
461 append_win(wmeta, w_shift);
462 for (i = 0; i < i_max - 1; i++)
464 append_win(wmeta, wins[i]);
469 for (i = 1; i < i_max; i++)
471 append_win(wmeta, wins[i]);
473 append_win(wmeta, w_shift);
478 for (i = 0; i < i_max; i++)
480 if ( (dir == 'f' && w_shift == wins[i])
481 || (dir == 'b' && w_shift == wins[i+1]))
483 append_win(wmeta, wins[i+1]);
484 append_win(wmeta, wins[i]);
489 append_win(wmeta, wins[i]);
495 wmeta->active = w_shift; /* TODO: Is this necessary? If so, why? */
501 extern void draw_all_wins(struct WinMeta * wmeta)
503 /* Empty everything before filling it a-new. */
505 wnoutrefresh(wmeta->screen);
506 werase(wmeta->pad.curses_win);
507 if (wmeta->chain_start)
510 /* Only draw the windows' *contents* first. */
511 draw_wins (wmeta->chain_start);
513 /* Draw windows' borders. Lines first, then line crossings / corners. */
514 uint16_t n_wins = 1, i;
515 struct Win * win_p = wmeta->chain_start;
516 while (0 != win_p->next)
521 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
522 draw_wins_borders (wmeta->chain_start, wmeta->active, all_corners, 0);
523 for (i = 0; i < n_wins; i++)
525 mvwaddch(wmeta->pad.curses_win,
526 all_corners[i].tl.y, all_corners[i].tl.x, '+');
527 mvwaddch(wmeta->pad.curses_win,
528 all_corners[i].tr.y, all_corners[i].tr.x, '+');
529 mvwaddch(wmeta->pad.curses_win,
530 all_corners[i].bl.y, all_corners[i].bl.x, '+');
531 mvwaddch(wmeta->pad.curses_win,
532 all_corners[i].br.y, all_corners[i].br.x, '+');
536 /* Draw virtual screen scroll hints. */
537 if (wmeta->pad_offset > 0)
539 draw_scroll_hint(&wmeta->pad,
540 wmeta->pad_offset, wmeta->pad_offset + 1, '<');
542 if (wmeta->pad_offset + wmeta->pad.size.x
543 < getmaxx(wmeta->pad.curses_win) - 1)
545 draw_scroll_hint(&wmeta->pad,
546 wmeta->pad_offset + wmeta->pad.size.x - 1,
547 getmaxx(wmeta->pad.curses_win)
548 - (wmeta->pad_offset + wmeta->pad.size.x), '>');
551 /* Write virtual screen segment to be shown on physical screen into */
552 /* ncurses screen buffer. */
553 pnoutrefresh(wmeta->pad.curses_win, 0, wmeta->pad_offset, 0, 0,
554 wmeta->pad.size.y, wmeta->pad.size.x-1);
557 /* Only at the end write accumulated changes to the physical screen. */
563 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
566 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
567 char * more = "more";
568 char * unit_cols = "columns";
569 char * unit_rows = "lines";
570 uint16_t dsc_space = frame->size.x;
571 char * unit = unit_rows;
572 if ('<' == dir || '>' == dir)
574 dsc_space = frame->size.y;
577 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
578 + 10); /* 10 = uint32 max strlen */
579 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
581 /* Decide on offset of the description text inside the scroll hint line. */
583 if (dsc_space > strlen(scrolldsc) + 1)
585 offset = (dsc_space - strlen(scrolldsc)) / 2;
588 /* Draw scroll hint line as dir symbols bracketing description text. */
590 for (q = 0; q < dsc_space; q++)
592 if (q >= offset && q < strlen(scrolldsc) + offset)
594 symbol = scrolldsc[q - offset] | A_REVERSE;
598 symbol = dir | A_REVERSE;
600 if ('<' == dir || '>' == dir)
602 mvwaddch(frame->curses_win, q, pos, symbol);
606 mvwaddch(frame->curses_win, pos, q, symbol);