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, remember their order in wins[]. */
441 for (w_p = wmeta->chain_start, i_max = 1;
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;
452 w_p_next = w_p->next;
453 suspend_win(wmeta, w_p);
458 /* Re-append all previously visible windows in the new order. */
463 append_win(wmeta, w_shift);
464 for (i = 0; i < i_max - 1; i++)
466 append_win(wmeta, wins[i]);
471 for (i = 1; i < i_max; i++)
473 append_win(wmeta, wins[i]);
475 append_win(wmeta, w_shift);
480 for (i = 0; i < i_max; i++)
482 if ( (dir == 'f' && w_shift == wins[i])
483 || (dir == 'b' && w_shift == wins[i+1]))
485 append_win(wmeta, wins[i+1]);
486 append_win(wmeta, wins[i]);
491 append_win(wmeta, wins[i]);
497 wmeta->active = w_shift; /* Otherwise lastly appended win is active. */
503 extern void draw_all_wins(struct WinMeta * wmeta)
505 /* Empty everything before filling it a-new. */
507 wnoutrefresh(wmeta->screen);
508 werase(wmeta->pad.curses_win);
509 if (wmeta->chain_start)
512 /* Only draw the windows' *contents* first. */
513 draw_wins (wmeta->chain_start);
515 /* Draw windows' borders. Lines first, then line crossings / corners. */
516 uint16_t n_wins = 1, i;
517 struct Win * win_p = wmeta->chain_start;
518 while (0 != win_p->next)
523 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
524 draw_wins_borders(wmeta->chain_start, wmeta->active, all_corners, 0);
525 for (i = 0; i < n_wins; i++)
527 mvwaddch(wmeta->pad.curses_win,
528 all_corners[i].tl.y, all_corners[i].tl.x, '+');
529 mvwaddch(wmeta->pad.curses_win,
530 all_corners[i].tr.y, all_corners[i].tr.x, '+');
531 mvwaddch(wmeta->pad.curses_win,
532 all_corners[i].bl.y, all_corners[i].bl.x, '+');
533 mvwaddch(wmeta->pad.curses_win,
534 all_corners[i].br.y, all_corners[i].br.x, '+');
538 /* Draw virtual screen scroll hints. */
539 if (wmeta->pad_offset > 0)
541 draw_scroll_hint(&wmeta->pad,
542 wmeta->pad_offset, wmeta->pad_offset + 1, '<');
544 if (wmeta->pad_offset + wmeta->pad.size.x
545 < getmaxx(wmeta->pad.curses_win) - 1)
547 draw_scroll_hint(&wmeta->pad,
548 wmeta->pad_offset + wmeta->pad.size.x - 1,
549 getmaxx(wmeta->pad.curses_win)
550 - (wmeta->pad_offset + wmeta->pad.size.x), '>');
553 /* Write virtual screen segment to be shown on physical screen into */
554 /* ncurses screen buffer. */
555 pnoutrefresh(wmeta->pad.curses_win, 0, wmeta->pad_offset, 0, 0,
556 wmeta->pad.size.y, wmeta->pad.size.x-1);
559 /* Only at the end write accumulated changes to the physical screen. */
565 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
568 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
569 char * more = "more";
570 char * unit_cols = "columns";
571 char * unit_rows = "lines";
572 uint16_t dsc_space = frame->size.x;
573 char * unit = unit_rows;
574 if ('<' == dir || '>' == dir)
576 dsc_space = frame->size.y;
579 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
580 + 10); /* 10 = uint32 max strlen */
581 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
583 /* Decide on offset of the description text inside the scroll hint line. */
585 if (dsc_space > strlen(scrolldsc) + 1)
587 offset = (dsc_space - strlen(scrolldsc)) / 2;
590 /* Draw scroll hint line as dir symbols bracketing description text. */
592 for (q = 0; q < dsc_space; q++)
594 if (q >= offset && q < strlen(scrolldsc) + offset)
596 symbol = scrolldsc[q - offset] | A_REVERSE;
600 symbol = dir | A_REVERSE;
602 if ('<' == dir || '>' == dir)
604 mvwaddch(frame->curses_win, q, pos, symbol);
608 mvwaddch(frame->curses_win, pos, q, symbol);