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 /* Fit virtual screen's width to minimum width demanded by current windows'
15 static void refit_pad(struct WinMeta * wmeta);
19 /* Update geometry (sizes, positions) of window "w" and its successors in the
20 * window chain. For the positioning algorithm place_win() is used.
22 static void update_wins(struct WinMeta * wmeta, struct Win * w);
23 static void place_win(struct WinMeta * wmeta, struct Win * w);
27 /* Destroy window "w"'s ncurses window (and set w.Frame.curses_win to 0). */
28 static void destroy_win(struct Win * w);
32 /* Draw contents of all windows in window chain from window "w" onwards. */
33 static void draw_wins(struct Win * w);
37 /* draw_win_borderlines() draws the vertical and horizontal borders of window
38 * "w" sans corners into the virtual screen "pad", and draws the top border
39 * line as the windows' title bar (highlighted if the window is described
40 * active by "active" being set). draw_wins_borderlines().
42 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
43 * windows from "w" on. "w_active" is a pointer to the one window that
44 * draw_win_borderlines() is supposed to handle as the active window.
46 * Finally, draw_wins_bordercorners draws into "pad" the borders of window "w"
47 * and all its successors.
49 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
50 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
52 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
56 static void refit_pad(struct WinMeta * wmeta)
58 /* Determine rightmost window column. */
59 uint16_t lastwincol = 0;
60 struct Win * w_p = wmeta->chain_start;
63 if (w_p->start.x + w_p->frame.size.x > lastwincol + 1)
65 lastwincol = w_p->start.x + w_p->frame.size.x - 1;
70 /* Only resize the pad if the rightmost window column has changed. */
71 if (getmaxx(wmeta->padframe.curses_win) != lastwincol)
73 wresize(wmeta->padframe.curses_win,
74 getmaxy(wmeta->padframe.curses_win), lastwincol + 2);
80 static void update_wins (struct WinMeta * wmeta, struct Win * w)
82 if (0 != w->frame.curses_win)
88 w->frame.curses_win = subpad(wmeta->padframe.curses_win,
89 w->frame.size.y, w->frame.size.x,
90 w->start.y, w->start.x);
93 update_wins (wmeta, w->next);
99 static void place_win (struct WinMeta * wmeta, struct Win * w)
101 /* First window goes into the upper-left corner. */
103 w->start.y = 1; /* Leave space for title bar. */
107 /* Non-first window fallbacks to: fit rightwards of rightmost border. */
108 struct Win * w_top = w->prev;
109 while (w_top->start.y != 1)
113 w->start.x = w_top->start.x + w_top->frame.size.x + 1;
115 /* Fit window below its predecessor if that one directly thrones over
116 * empty space wide and high enough.
118 uint16_t w_prev_maxy = w->prev->start.y
119 + getmaxy(w->prev->frame.curses_win);
120 if ( w->frame.size.x <= w->prev->frame.size.x
121 && w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy)
123 w->start.x = w->prev->start.x;
124 w->start.y = w_prev_maxy + 1;
127 /* Failing that, try to open a new sub column below the nearest
128 * predecessor window that thrones over enough empty space.
132 struct Win * w_up = w->prev;
133 struct Win * w_upup = w_up;
135 while (w_up != w_top)
140 if (w_up->start.y != w_upup->start.y)
144 w_upup = w_upup->prev;
146 w_prev_maxy = w_upup->start.y
147 + getmaxy(w_upup->frame.curses_win);
148 widthdiff = (w_upup->start.x + w_upup->frame.size.x)
149 - (w_up->start.x + w_up->frame.size.x);
150 if ( w->frame.size.y < wmeta->padframe.size.y - w_prev_maxy
151 && w->frame.size.x < widthdiff)
153 w->start.x = w_up->start.x + w_up->frame.size.x + 1 ;
154 w->start.y = w_prev_maxy + 1;
165 static void destroy_win (struct Win * w)
167 delwin(w->frame.curses_win);
168 w->frame.curses_win = 0;
173 static void draw_wins (struct Win * w)
184 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
186 /* Draw vertical and horizontal border lines. */
188 for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++)
190 mvwaddch(pad, y, w->start.x - 1, '|');
191 mvwaddch(pad, y, w->start.x + w->frame.size.x, '|');
193 for (x = w->start.x; x <= w->start.x + w->frame.size.x; x++)
195 mvwaddch(pad, w->start.y - 1, x, '-');
196 mvwaddch(pad, w->start.y + w->frame.size.y, x, '-');
199 /* Draw as much as possible of the title into center of top border line. */
200 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
201 if (w->frame.size.x >= min_title_length_visible)
203 uint16_t title_offset = 0;
204 if (w->frame.size.x > strlen(w->title) + 2)
206 title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2;
207 } /* +2 is for padding/decoration */
208 uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);
209 char title[length_visible + 3];
210 char decoration = ' ';
215 memcpy(title + 1, w->title, length_visible);
216 title[0] = title[length_visible + 1] = decoration;
217 title[length_visible + 2] = '\0';
218 mvwaddstr(pad, w->start.y - 1, w->start.x + title_offset, title);
224 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
232 draw_win_borderlines(w, active, pad);
235 draw_wins_borderlines (w->next, w_active, pad);
241 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
243 mvwaddch(pad, w->start.y - 1, w->start.x - 1, '+');
244 mvwaddch(pad, w->start.y - 1, w->start.x + w->frame.size.x, '+');
245 mvwaddch(pad, w->start.y + w->frame.size.y, w->start.x - 1, '+');
247 w->start.y + w->frame.size.y, w->start.x + w->frame.size.x, '+');
250 draw_wins_bordercorners(w->next, pad);
256 extern struct WinMeta init_win_meta(WINDOW * screen)
258 struct WinMeta wmeta;
259 wmeta.screen = screen;
260 wmeta.padframe.size.y = getmaxy(screen);
261 wmeta.padframe.size.x = getmaxx(screen);
262 wmeta.chain_start = 0;
264 wmeta.pad_offset = 0;
265 wmeta.padframe.curses_win = newpad(wmeta.padframe.size.y, 1);
272 extern struct Win init_win(struct WinMeta * wmeta, char * title,
273 void * data, void * func)
278 w.frame.curses_win = 0;
281 w.frame.size.y = wmeta->padframe.size.y - 1;
289 extern void append_win(struct WinMeta * wmeta, struct Win * w)
291 if (0 != wmeta->chain_start)
293 w->prev = wmeta->chain_end;
294 wmeta->chain_end->next = w;
299 wmeta->chain_start = w;
301 wmeta->chain_end = w;
302 update_wins(wmeta, w);
307 extern void suspend_win(struct WinMeta * wmeta, struct Win * w)
311 if (wmeta->chain_start != w)
313 w->prev->next = w->next;
317 wmeta->chain_start = w->next;
319 char pad_refitted = 0;
320 if (wmeta->chain_end != w)
322 w->next->prev = w->prev;
323 if (wmeta->active == w)
325 wmeta->active = w->next;
327 update_wins(wmeta, w->next); /* Positioning of successor windows may */
328 pad_refitted = 1; /* be affected / need correction. Note */
329 } /* that update_wins() already refits the */
330 else /* pad, voiding later need for that. */
332 wmeta->chain_end = w->prev;
333 if (wmeta->active == w)
335 wmeta->active = w->prev;
342 if (0 == pad_refitted)
350 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
353 && (new_offset < wmeta->pad_offset
354 || new_offset + wmeta->padframe.size.x
355 < getmaxx(wmeta->padframe.curses_win)))
357 wmeta->pad_offset = new_offset;
363 extern void resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
365 if (0 != wmeta->active
366 && size.x > 0 && size.y > 0
367 && size.y < wmeta->padframe.size.y)
369 wmeta->active->frame.size = size;
370 update_wins(wmeta, wmeta->chain_start); /* Positioning of successor */
371 } /* windows may be affected. */
376 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
378 if (0 != wmeta->active)
382 if (wmeta->active->next != 0)
384 wmeta->active = wmeta->active->next;
388 wmeta->active = wmeta->chain_start;
393 if (wmeta->active->prev != 0)
395 wmeta->active = wmeta->active->prev;
399 wmeta->active = wmeta->chain_end;
407 extern void shift_active_win(struct WinMeta * wmeta, char dir)
409 if (0 != wmeta->active /* No shifting with less */
410 && wmeta->chain_start != wmeta->chain_end /* than one window visible. */
411 && (dir == 'f' || dir == 'b'))
413 struct Win * w_shift = wmeta->active, * w_p, * w_p_next;
415 /* Check if shifting will lead to wrapping. */
417 if ( (dir == 'f' && w_shift == wmeta->chain_end)
418 || (dir == 'b' && w_shift == wmeta->chain_start))
423 /* Suspend all visible windows, remember their order in wins[]. */
425 for (w_p = wmeta->chain_start, i_max = 1;
426 w_p != wmeta->chain_end;
431 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
432 for (i = 0, w_p = wmeta->chain_start;
436 w_p_next = w_p->next;
437 suspend_win(wmeta, w_p);
442 /* Re-append all previously visible windows in the new order. */
447 append_win(wmeta, w_shift);
448 for (i = 0; i < i_max - 1; i++)
450 append_win(wmeta, wins[i]);
455 for (i = 1; i < i_max; i++)
457 append_win(wmeta, wins[i]);
459 append_win(wmeta, w_shift);
464 for (i = 0; i < i_max; i++)
466 if ( (dir == 'f' && w_shift == wins[i])
467 || (dir == 'b' && w_shift == wins[i+1]))
469 append_win(wmeta, wins[i+1]);
470 append_win(wmeta, wins[i]);
475 append_win(wmeta, wins[i]);
481 wmeta->active = w_shift; /* Otherwise lastly appended win is active. */
487 extern void draw_all_wins(struct WinMeta * wmeta)
489 /* Empty everything before filling it a-new. */
491 wnoutrefresh(wmeta->screen);
492 werase(wmeta->padframe.curses_win);
493 if (wmeta->chain_start)
496 /* Draw windows' contents first, then their borders. */
497 draw_wins(wmeta->chain_start);
498 draw_wins_borderlines(wmeta->chain_start, wmeta->active,
499 wmeta->padframe.curses_win);
500 draw_wins_bordercorners(wmeta->chain_start, wmeta->padframe.curses_win);
502 /* Draw virtual screen scroll hints. */
503 if (wmeta->pad_offset > 0)
505 draw_scroll_hint(&wmeta->padframe,
506 wmeta->pad_offset, wmeta->pad_offset + 1, '<');
508 if (wmeta->pad_offset + wmeta->padframe.size.x
509 < getmaxx(wmeta->padframe.curses_win) - 1)
511 draw_scroll_hint(&wmeta->padframe,
512 wmeta->pad_offset + wmeta->padframe.size.x - 1,
513 getmaxx(wmeta->padframe.curses_win)
514 - (wmeta->pad_offset + wmeta->padframe.size.x),
518 /* Write virtual screen segment to be shown on physical screen into */
519 /* ncurses screen buffer. */
520 pnoutrefresh(wmeta->padframe.curses_win, 0, wmeta->pad_offset, 0, 0,
521 wmeta->padframe.size.y, wmeta->padframe.size.x-1);
524 /* Only at the end write accumulated changes to the physical screen. */
530 extern void draw_scroll_hint(struct Frame * frame, uint16_t pos, uint32_t dist,
533 /* Decide on alignment (vertical/horizontal?), thereby scroll hint text. */
534 char * more = "more";
535 char * unit_cols = "columns";
536 char * unit_rows = "lines";
537 uint16_t dsc_space = frame->size.x;
538 char * unit = unit_rows;
539 if ('<' == dir || '>' == dir)
541 dsc_space = frame->size.y;
544 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit)
545 + 10); /* 10 = uint32 max strlen */
546 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
548 /* Decide on offset of the description text inside the scroll hint line. */
550 if (dsc_space > strlen(scrolldsc) + 1)
552 offset = (dsc_space - strlen(scrolldsc)) / 2;
555 /* Draw scroll hint line as dir symbols bracketing description text. */
557 for (q = 0; q < dsc_space; q++)
559 if (q >= offset && q < strlen(scrolldsc) + offset)
561 symbol = scrolldsc[q - offset] | A_REVERSE;
565 symbol = dir | A_REVERSE;
567 if ('<' == dir || '>' == dir)
569 mvwaddch(frame->curses_win, q, pos, symbol);
573 mvwaddch(frame->curses_win, pos, q, symbol);