4 #include <stdint.h> /* for uint8_t, uint16_t, uint32_t, UINT16_MAX */
5 #include <ncurses.h> /* for typedefs WINDOW, chtype, wresize(), getmaxx(), */
6 /* getmaxy(), delwin(), mvwaddch(), mvwaddstr(), */
7 /* newpad(), wnoutrefres(), erase(), werase(), */
8 /* pnoutrefresh(), doupdate(), getmaxyx() */
9 #include <stdlib.h> /* for malloc(), free() */
10 #include <string.h> /* for strlen(), strnlen(), memcpy() */
11 #include "yx_uint16.h" /* for struct yx_uint16 */
12 #include "misc.h" /* for center_offset() */
16 /* Fit virtual screen's width to minimum width demanded by current windows'
19 static uint8_t refit_pad(struct WinMeta * wmeta);
23 /* Update geometry (sizes, positions) of window "w" and its successors in the
24 * window chain. For the positioning algorithm place_win() is used.
26 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w);
27 static void place_win(struct WinMeta * wmeta, struct Win * w);
31 /* Draw scroll hint (a line saying that there are "dist" more elements of
32 * "unit" further into the direction symbolized by the "dir" char) into virtual
33 * screen pad, onto an appropriate edge of either a window or the screen; the
34 * left or right edge if "dir" is "<" or ">", or the upper or lower edge if it
35 * is "^" or "v". "start" should be either the start coordinate of a window's
36 * frame or .y=0, .x=wm->pad_offset if it describes the virtual screen pad.
37 * winscroll_hint() and padscroll_hint() are wrappers to simplify these uses.
39 static void scroll_hint(struct WinMeta * wm, struct yx_uint16 fsize, char dir,
40 uint16_t dist, char * unit, struct yx_uint16 start);
41 static void winscroll_hint(struct WinMeta * wm, struct Win * w, char dir,
43 static void padscroll_hint(struct WinMeta * wm, char dir, uint16_t dist);
47 /* Draw contents of all windows in window chain from window "w" onwards. */
48 static uint8_t draw_wins(struct WinMeta * wm, struct Win * w);
52 /* draw_win_borderlines() draws the vertical and horizontal borders of window
53 * "w" sans corners into the virtual screen "pad", and draws the top border
54 * line as the windows' title bar (highlighted if the window is described
55 * active by "active" being == 1).
57 * draw_wins_borderlines() calls draw_win_borderlines() recursively on all
58 * windows from "w" on. "w_active" is a pointer to the one window that
59 * draw_win_borderlines() is supposed to handle as the active window.
61 * Finally, draw_wins_bordercorners() draws into "pad" the borders of window "w"
62 * and all its successors.
64 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad);
65 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
67 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
71 /* Shift active window forwards / backwards in window chain. */
72 static void shift_win_forward(struct WinMeta * wmeta);
73 static void shift_win_backward(struct WinMeta * wmeta);
77 static uint8_t refit_pad(struct WinMeta * wmeta)
79 /* Determine rightmost window column. */
80 uint32_t lastwcol = 0;
81 struct Win * wp = wmeta->chain_start;
84 if ((uint32_t) wp->start.x + (uint32_t) wp->framesize.x > lastwcol + 1)
86 lastwcol = (uint32_t) wp->start.x + (uint32_t) wp->framesize.x - 1;
91 /* Only resize the pad if the rightmost window column has changed. */
92 if (getmaxx(wmeta->pad) + 1 != lastwcol)
94 if (lastwcol + 2 > UINT16_MAX)
98 return (ERR == wresize(wmeta->pad, getmaxy(wmeta->pad), lastwcol + 2));
105 static uint8_t update_wins(struct WinMeta * wmeta, struct Win * w)
108 uint8_t test_refit = refit_pad(wmeta);
115 return update_wins(wmeta, w->next);
122 static void place_win(struct WinMeta * wmeta, struct Win * w)
124 /* First window goes into the upper-left corner. */
126 w->start.y = 1; /* Leave space for title bar. */
130 /* Non-first window fallbacks to: fit rightwards of rightmost border. */
131 struct Win * w_top = w->prev;
132 while (w_top->start.y != 1)
136 w->start.x = w_top->start.x + w_top->framesize.x + 1;
138 /* Fit window below its predecessor if that one directly thrones over
139 * empty space wide and high enough.
141 uint16_t w_prev_maxy = w->prev->start.y + w->prev->framesize.y;
142 if ( w->framesize.x <= w->prev->framesize.x
143 && w->framesize.y < wmeta->padsize.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 + w_upup->framesize.y;
169 widthdiff = (w_upup->start.x + w_upup->framesize.x)
170 - (w_up->start.x + w_up->framesize.x);
171 if ( w->framesize.y < wmeta->padsize.y - w_prev_maxy
172 && w->framesize.x < widthdiff)
174 w->start.x = w_up->start.x + w_up->framesize.x + 1 ;
175 w->start.y = w_prev_maxy + 1;
186 static void scroll_hint(struct WinMeta * wm, struct yx_uint16 fsize, char dir,
187 uint16_t dist, char * unit, struct yx_uint16 start)
189 /* Decide on alignment (vertical/horizontal?), thereby hint text space. */
190 char * more = "more";
191 uint16_t dsc_space = fsize.x;
192 if ('<' == dir || '>' == dir)
195 } /* vv-- 10 = max strlen for uint16_t */
196 char scrolldsc[1 + strlen(more) + 1 + 10 + 1 + strlen(unit) + 1 + 1];
197 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
199 /* Decide on offset of the description text inside the scroll hint line. */
200 uint16_t dsc_offset = 1;
201 if (dsc_space > strlen(scrolldsc) + 1)
203 dsc_offset = (dsc_space - strlen(scrolldsc)) / 2;
206 /* Draw scroll hint line as dir symbols bracketing description text. */
207 uint16_t draw_offset = 0;
210 draw_offset = fsize.x - 1;
214 draw_offset = fsize.y - 1;
217 for (; q < dsc_space; q++)
219 chtype symbol = dir | A_REVERSE;
220 if (q >= dsc_offset && q < strlen(scrolldsc) + dsc_offset)
222 symbol = scrolldsc[q - dsc_offset] | A_REVERSE;
224 if ('<' == dir || '>' == dir)
226 mvwaddch(wm->pad, start.y + q, start.x + draw_offset, symbol);
230 mvwaddch(wm->pad, start.y + draw_offset, start.x + q, symbol);
236 static void padscroll_hint(struct WinMeta * wm, char dir, uint16_t dist)
238 struct yx_uint16 start;
240 start.x = wm->pad_offset;
241 scroll_hint(wm, wm->padsize, dir, dist, "columns", start);
246 static void winscroll_hint(struct WinMeta * wm, struct Win * w, char dir,
249 char * unit = "lines";
250 if ('<' == dir || '>' == dir)
254 struct yx_uint16 start = w->start;
255 scroll_hint(wm, w->framesize, dir, dist, unit, start);
260 static uint8_t draw_wins(struct WinMeta * wm, struct Win * w)
263 uint16_t y, x, size_y, size_x;
264 size_y = w->winmapsize.y;
265 size_x = w->winmapsize.x;
266 uint16_t offset_y = center_offset(w->center.y, size_y, w->framesize.y);
267 uint16_t offset_x = center_offset(w->center.x, size_x, w->framesize.x);
268 for (y = offset_y; y < w->framesize.y + offset_y && y < size_y; y++)
270 for (x = offset_x; x < w->framesize.x + offset_x && x < size_x; x++)
272 chtype ch = w->winmap[(y * w->winmapsize.x) + x];
273 mvwaddch(wm->pad, w->start.y + (y - offset_y),
274 w->start.x + (x - offset_x), ch);
283 winscroll_hint(wm, w, '^', offset_y + 1);
285 if (size_y > offset_y + w->framesize.y)
287 winscroll_hint(wm, w, 'v', size_y - ((offset_y + w->framesize.y) - 1));
291 winscroll_hint(wm, w, '<', offset_x + 1);
293 if (size_x > offset_x + w->framesize.x)
295 winscroll_hint(wm, w, '>', size_x - ((offset_x + w->framesize.x) - 1));
299 return draw_wins(wm, w->next);
306 static void draw_win_borderlines(struct Win * w, char active, WINDOW * pad)
308 /* Draw vertical and horizontal border lines. */
310 for (y = w->start.y; y <= w->start.y + w->framesize.y; y++)
312 mvwaddch(pad, y, w->start.x - 1, '|');
313 mvwaddch(pad, y, w->start.x + w->framesize.x, '|');
315 for (x = w->start.x; x <= w->start.x + w->framesize.x; x++)
317 mvwaddch(pad, w->start.y - 1, x, '-');
318 mvwaddch(pad, w->start.y + w->framesize.y, x, '-');
321 /* Draw as much as possible of the title into center of top border line. */
322 char min_title_length_visible = 3; /* min. 1 char + 2 padding/decoration */
323 if (w->framesize.x >= min_title_length_visible)
325 uint16_t title_offset = 0;
326 if (w->framesize.x > strlen(w->title) + 2)
328 title_offset = (w->framesize.x - (strlen(w->title) + 2)) / 2;
329 } /* +2 is for padding/decoration */
330 uint16_t length_visible = strnlen(w->title, w->framesize.x - 2);
331 char title[length_visible + 3];
332 char decoration = ' ';
337 memcpy(title + 1, w->title, length_visible);
338 title[0] = title[length_visible + 1] = decoration;
339 title[length_visible + 2] = '\0';
340 mvwaddstr(pad, w->start.y - 1, w->start.x + title_offset, title);
346 static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
354 draw_win_borderlines(w, active, pad);
357 draw_wins_borderlines(w->next, w_active, pad);
363 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad)
365 mvwaddch(pad, w->start.y - 1, w->start.x - 1, '+');
366 mvwaddch(pad, w->start.y - 1, w->start.x + w->framesize.x, '+');
367 mvwaddch(pad, w->start.y + w->framesize.y, w->start.x - 1, '+');
368 mvwaddch(pad, w->start.y + w->framesize.y, w->start.x + w->framesize.x,'+');
371 draw_wins_bordercorners(w->next, pad);
377 static void shift_win_forward(struct WinMeta * wmeta)
379 if (wmeta->active == wmeta->chain_end)
381 wmeta->chain_end = wmeta->active->prev;
382 wmeta->chain_end->next = 0;
383 wmeta->active->next = wmeta->chain_start;
384 wmeta->active->next->prev = wmeta->active;
385 wmeta->chain_start = wmeta->active;
386 wmeta->chain_start->prev = 0;
390 struct Win * old_prev = wmeta->active->prev;
391 struct Win * old_next = wmeta->active->next;
392 if (wmeta->chain_end == wmeta->active->next)
394 wmeta->chain_end = wmeta->active;
395 wmeta->active->next = 0;
399 wmeta->active->next = old_next->next;
400 wmeta->active->next->prev = wmeta->active;
402 if (wmeta->chain_start == wmeta->active)
404 wmeta->chain_start = old_next;
408 old_prev->next = old_next;
410 old_next->prev = old_prev;
411 old_next->next = wmeta->active;
412 wmeta->active->prev = old_next;
418 static void shift_win_backward(struct WinMeta * wmeta)
420 if (wmeta->active == wmeta->chain_start)
422 wmeta->chain_start = wmeta->active->next;
423 wmeta->chain_start->prev = 0;
424 wmeta->active->prev = wmeta->chain_end;
425 wmeta->active->prev->next = wmeta->active;
426 wmeta->chain_end = wmeta->active;
427 wmeta->chain_end->next = 0;
431 struct Win * old_prev = wmeta->active->prev;
432 struct Win * old_next = wmeta->active->next;
433 if (wmeta->chain_start == wmeta->active->prev)
435 wmeta->chain_start = wmeta->active;
436 wmeta->active->prev = 0;
440 wmeta->active->prev = old_prev->prev;
441 wmeta->active->prev->next = wmeta->active;
443 if (wmeta->chain_end == wmeta->active)
445 wmeta->chain_end = old_prev;
449 old_next->prev = old_prev;
451 old_prev->next = old_next;
452 old_prev->prev = wmeta->active;
453 wmeta->active->next = old_prev;
459 extern uint8_t init_win_meta(WINDOW * screen, struct WinMeta ** wmp)
461 struct WinMeta * wmeta = malloc(sizeof(struct WinMeta));
462 wmeta->screen = screen;
463 uint32_t maxy_test = getmaxy(screen);
464 uint32_t maxx_test = getmaxx(screen);
465 if (maxy_test > UINT16_MAX || maxx_test > UINT16_MAX)
469 wmeta->padsize.y = maxy_test;
470 wmeta->padsize.x = maxx_test;
471 wmeta->chain_start = 0;
472 wmeta->chain_end = 0;
473 wmeta->pad_offset = 0;
474 WINDOW * pad_test = newpad(wmeta->padsize.y, 1);
475 if (NULL == pad_test)
479 wmeta->pad = pad_test;
487 extern uint8_t init_win(struct WinMeta * wmeta, struct Win ** wp, char * title,
488 int16_t height, int16_t width,
489 void * data, void * func)
491 struct Win * w = malloc(sizeof(struct Win));
501 w->title = malloc(strlen(title) + 1);
502 if (NULL == w->title)
506 sprintf(w->title, "%s", title);
513 w->framesize.x = width;
517 w->framesize.x = wmeta->padsize.x + width;
519 if (0 < height && height <= wmeta->padsize.y - 1)
521 w->framesize.y = height;
523 else if (0 >= height && wmeta->padsize.y + (height - 1) > 0)
525 w->framesize.y = wmeta->padsize.y + (height - 1);
533 extern void free_winmeta(struct WinMeta * wmeta)
541 extern void free_win(struct Win * win)
549 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
551 if (0 != wmeta->chain_start)
553 w->prev = wmeta->chain_end;
554 wmeta->chain_end->next = w;
559 wmeta->chain_start = w;
561 wmeta->chain_end = w;
562 return update_wins(wmeta, w);
567 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
569 if (wmeta->chain_start != w)
571 w->prev->next = w->next;
575 wmeta->chain_start = w->next;
577 char pad_refitted = 0;
578 if (wmeta->chain_end != w)
580 w->next->prev = w->prev;
581 if (wmeta->active == w)
583 wmeta->active = w->next;
585 uint8_t test = update_wins(wmeta, w->next); /* Positioning of */
586 if (0 != test) /* successor windows may */
587 { /* be affected / need */
588 return test; /* correction. Note that */
589 } /* update_wins() already */
590 pad_refitted = 1; /* refits the pad, voiding*/
591 } /* later need for that. */
594 wmeta->chain_end = w->prev;
595 if (wmeta->active == w)
597 wmeta->active = w->prev;
604 if (0 == pad_refitted)
606 return refit_pad(wmeta);
613 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
616 && (new_offset < wmeta->pad_offset
617 || new_offset + wmeta->padsize.x < getmaxx(wmeta->pad)))
619 wmeta->pad_offset = new_offset;
625 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
627 if (0 != wmeta->active
628 && size.x > 0 && size.y > 0 && size.y < wmeta->padsize.y)
630 wmeta->active->framesize = size;
631 return update_wins(wmeta, wmeta->active); /* Positioning of following */
632 } /* windows may be affected. */
638 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
640 if (0 != wmeta->active)
644 if (wmeta->active->next != 0)
646 wmeta->active = wmeta->active->next;
650 wmeta->active = wmeta->chain_start;
655 if (wmeta->active->prev != 0)
657 wmeta->active = wmeta->active->prev;
661 wmeta->active = wmeta->chain_end;
669 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
671 if ( 0 == wmeta->active /* No shifting with < 2 */
672 || wmeta->chain_start == wmeta->chain_end) /* windows visible. */
678 shift_win_forward(wmeta);
682 shift_win_backward(wmeta);
684 return update_wins(wmeta, wmeta->chain_start);
689 extern uint8_t draw_all_wins(struct WinMeta * wm)
691 /* Empty everything before filling it a-new. */
693 wnoutrefresh(wm->screen);
698 /* Draw windows' borders first, then windows. */
699 draw_wins_borderlines(wm->chain_start, wm->active, wm->pad);
700 draw_wins_bordercorners(wm->chain_start, wm->pad);
701 if (1 == draw_wins(wm, wm->chain_start))
706 /* Draw virtual screen scroll hints. */
707 if (wm->pad_offset > 0)
709 padscroll_hint(wm, '<', wm->pad_offset + 1);
711 uint16_t size_x = getmaxx(wm->pad);
712 uint16_t right_edge = wm->pad_offset + wm->padsize.x;
713 if (right_edge < size_x - 1)
715 padscroll_hint(wm, '>', size_x - right_edge);
718 /* Write pad segment to be shown on physical screen to screen buffer. */
719 pnoutrefresh(wm->pad, 0, wm->pad_offset, 0, 0,
720 wm->padsize.y, wm->padsize.x - 1);
723 /* Only at the end write accumulated changes to the physical screen. */