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, void * func)
490 struct Win * w = malloc(sizeof(struct Win));
500 w->title = malloc(strlen(title) + 1);
501 if (NULL == w->title)
505 sprintf(w->title, "%s", title);
511 w->framesize.x = width;
515 w->framesize.x = wmeta->padsize.x + width;
517 if (0 < height && height <= wmeta->padsize.y - 1)
519 w->framesize.y = height;
521 else if (0 >= height && wmeta->padsize.y + (height - 1) > 0)
523 w->framesize.y = wmeta->padsize.y + (height - 1);
531 extern void free_winmeta(struct WinMeta * wmeta)
539 extern void free_win(struct Win * win)
547 extern uint8_t append_win(struct WinMeta * wmeta, struct Win * w)
549 if (0 != wmeta->chain_start)
551 w->prev = wmeta->chain_end;
552 wmeta->chain_end->next = w;
557 wmeta->chain_start = w;
559 wmeta->chain_end = w;
560 return update_wins(wmeta, w);
565 extern uint8_t suspend_win(struct WinMeta * wmeta, struct Win * w)
567 if (wmeta->chain_start != w)
569 w->prev->next = w->next;
573 wmeta->chain_start = w->next;
575 char pad_refitted = 0;
576 if (wmeta->chain_end != w)
578 w->next->prev = w->prev;
579 if (wmeta->active == w)
581 wmeta->active = w->next;
583 uint8_t test = update_wins(wmeta, w->next); /* Positioning of */
584 if (0 != test) /* successor windows may */
585 { /* be affected / need */
586 return test; /* correction. Note that */
587 } /* update_wins() already */
588 pad_refitted = 1; /* refits the pad, voiding*/
589 } /* later need for that. */
592 wmeta->chain_end = w->prev;
593 if (wmeta->active == w)
595 wmeta->active = w->prev;
602 if (0 == pad_refitted)
604 return refit_pad(wmeta);
611 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset)
614 && (new_offset < wmeta->pad_offset
615 || new_offset + wmeta->padsize.x < getmaxx(wmeta->pad)))
617 wmeta->pad_offset = new_offset;
623 extern uint8_t resize_active_win(struct WinMeta * wmeta, struct yx_uint16 size)
625 if (0 != wmeta->active
626 && size.x > 0 && size.y > 0 && size.y < wmeta->padsize.y)
628 wmeta->active->framesize = size;
629 return update_wins(wmeta, wmeta->active); /* Positioning of following */
630 } /* windows may be affected. */
636 extern void cycle_active_win(struct WinMeta * wmeta, char dir)
638 if (0 != wmeta->active)
642 if (wmeta->active->next != 0)
644 wmeta->active = wmeta->active->next;
648 wmeta->active = wmeta->chain_start;
653 if (wmeta->active->prev != 0)
655 wmeta->active = wmeta->active->prev;
659 wmeta->active = wmeta->chain_end;
667 extern uint8_t shift_active_win(struct WinMeta * wmeta, char dir)
669 if ( 0 == wmeta->active /* No shifting with < 2 */
670 || wmeta->chain_start == wmeta->chain_end) /* windows visible. */
676 shift_win_forward(wmeta);
680 shift_win_backward(wmeta);
682 return update_wins(wmeta, wmeta->chain_start);
687 extern uint8_t draw_all_wins(struct WinMeta * wm)
689 /* Empty everything before filling it a-new. */
691 wnoutrefresh(wm->screen);
696 /* Draw windows' borders first, then windows. */
697 draw_wins_borderlines(wm->chain_start, wm->active, wm->pad);
698 draw_wins_bordercorners(wm->chain_start, wm->pad);
699 if (1 == draw_wins(wm, wm->chain_start))
704 /* Draw virtual screen scroll hints. */
705 if (wm->pad_offset > 0)
707 padscroll_hint(wm, '<', wm->pad_offset + 1);
709 uint16_t size_x = getmaxx(wm->pad);
710 uint16_t right_edge = wm->pad_offset + wm->padsize.x;
711 if (right_edge < size_x - 1)
713 padscroll_hint(wm, '>', size_x - right_edge);
716 /* Write pad segment to be shown on physical screen to screen buffer. */
717 pnoutrefresh(wm->pad, 0, wm->pad_offset, 0, 0,
718 wm->padsize.y, wm->padsize.x - 1);
721 /* Only at the end write accumulated changes to the physical screen. */