17 static void refit_pad (struct WinMeta *);
18 static struct yx place_window (struct WinMeta *, struct Win *);
19 static void update_windows (struct WinMeta *, struct Win *);
20 static void destroy_window (struct Win *);
21 static void draw_windows_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
22 static void draw_window_borders (struct Win *, char);
23 static void draw_windows (struct Win *);
24 static void draw_vertical_scroll_hint (struct WinMeta *, uint16_t, uint32_t, char);
26 extern struct WinMeta init_win_meta (WINDOW * screen) {
27 // Create and populate WinMeta struct with sane default values.
28 struct WinMeta win_meta;
29 win_meta.screen = screen;
30 win_meta.height = getmaxy(screen);
31 win_meta.width = getmaxx(screen);
32 win_meta.chain_start = 0;
33 win_meta.chain_end = 0;
34 win_meta.pad_offset = 0;
35 win_meta.pad = newpad(win_meta.height, 1);
39 extern struct Win init_window (struct WinMeta * win_meta, char * title, void * data, void * func) {
40 // Create and populate Win struct with sane default values.
47 win.height = win_meta->height - 1;
52 extern void append_window (struct WinMeta * win_meta, struct Win * win) {
53 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
54 if (0 != win_meta->chain_start) {
55 win->prev = win_meta->chain_end;
56 win_meta->chain_end->next = win; }
58 win_meta->active = win;
59 win_meta->chain_start = win; }
60 win_meta->chain_end = win;
61 update_windows(win_meta, win); }
63 static void refit_pad (struct WinMeta * win_meta) {
64 // Fit pad width to minimum width demanded by current windows' geometries.
65 uint16_t lastwincol = 0;
66 struct Win * win_p = win_meta->chain_start;
68 if (win_p->startx + win_p->width > lastwincol + 1)
69 lastwincol = win_p->startx + win_p->width - 1;
70 win_p = win_p->next; }
71 if (getmaxx(win_meta->pad) != lastwincol)
72 wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); }
74 extern void suspend_window (struct WinMeta * win_meta, struct Win * win) {
75 // Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
77 if (win_meta->chain_start != win) // Give win's position in the chain to element next to it in the chain.
78 win->prev->next = win->next;
80 win_meta->chain_start = win->next;
81 char pad_refitted = 0;
82 if (win_meta->chain_end != win) { // Let chain element next to win know its new predecessor.
83 win->next->prev = win->prev;
84 if (win_meta->active == win) // If win was active, shift active window pointer to
85 win_meta->active = win->next; // the next chain element, if that is a window ...
86 update_windows(win_meta, win->next);
89 win_meta->chain_end = win->prev;
90 if (win_meta->active == win) // ... or else to the previous element.
91 win_meta->active = win->prev; }
94 if (0 == pad_refitted) // Refit pad if necessary.
95 refit_pad(win_meta); }
97 static struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
98 // Based on position and sizes of previous window, find fitting place for current window.
100 start.x = 0; // if window is first in chain, place it on top-left corner
102 if (0 != win->prev) {
103 struct Win * win_top = win->prev;
104 while (getbegy(win_top->curses) != 1)
105 win_top = win_top->prev; // else, default to placing window in new top
106 start.x = win_top->startx + win_top->width + 1; // column to the right of the last one
107 uint16_t winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
108 if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
109 start.x = win->prev->startx; // place window below previous window if it fits
110 start.y = winprev_maxy + 1; } // vertically and is not wider than its predecessor
112 struct Win * win_up = win->prev;
113 struct Win * win_upup = win_up;
115 while (win_up != win_top) {
116 win_upup = win_up->prev;
118 if (getbegy(win_up->curses) != getbegy(win_upup->curses))
120 win_upup = win_upup->prev; }
121 winprev_maxy = getbegy(win_upup->curses) + getmaxy(win_upup->curses);
122 widthdiff = (win_upup->startx + win_upup->width) - (win_up->startx + win_up->width);
123 if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
124 start.x = win_up->startx + win_up->width + 1; // else try to open new sub column under last
125 start.y = winprev_maxy + 1; // window below which enough space remains
127 win_up = win_upup; } } }
130 static void update_windows (struct WinMeta * win_meta, struct Win * win) {
131 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
132 if (0 != win->curses)
133 destroy_window (win);
134 struct yx startyx = place_window(win_meta, win);
135 win->startx = startyx.x;
137 win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
139 update_windows (win_meta, win->next); }
141 static void destroy_window (struct Win * win) {
146 static void draw_window_borders (struct Win * win, char active) {
147 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
149 for (y = getbegy(win->curses); y <= getbegy(win->curses) + win->height; y++) {
150 mvwaddch(wgetparent(win->curses), y, win->startx - 1, '|');
151 mvwaddch(wgetparent(win->curses), y, win->startx + win->width, '|'); }
152 for (x = win->startx; x <= win->startx + win->width; x++) {
153 mvwaddch(wgetparent(win->curses), getbegy(win->curses) - 1, x, '-');
154 mvwaddch(wgetparent(win->curses), getbegy(win->curses) + win->height, x, '-'); }
155 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
156 if (win->width >= min_title_length_visible) {
157 uint16_t title_offset = 0;
158 if (win->width > strlen(win->title) + 2)
159 title_offset = (win->width - (strlen(win->title) + 2)) / 2; // + 2 is for decoration
160 uint16_t length_visible = strnlen(win->title, win->width - 2);
161 char title[length_visible + 3];
162 char decoration = ' ';
165 memcpy(title + 1, win->title, length_visible);
166 title[0] = title[length_visible + 1] = decoration;
167 title[length_visible + 2] = '\0';
168 mvwaddstr(wgetparent(win->curses), getbegy(win->curses) - 1, win->startx + title_offset, title); } }
170 static void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, uint16_t ccount) {
171 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
173 if (win == win_active)
175 draw_window_borders(win, active);
176 corners[ccount].tl.y = getbegy(win->curses) - 1;
177 corners[ccount].tl.x = win->startx - 1;
178 corners[ccount].tr.y = getbegy(win->curses) - 1;
179 corners[ccount].tr.x = win->startx + win->width;
180 corners[ccount].bl.y = getbegy(win->curses) + win->height;
181 corners[ccount].bl.x = win->startx - 1;
182 corners[ccount].br.y = getbegy(win->curses) + win->height;
183 corners[ccount].br.x = win->startx + win->width;
184 if (0 != win->next) {
185 draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
187 static void draw_windows (struct Win * win) {
188 // Draw contents of all windows in window chain from win on.
190 if (0 != win->next) {
191 draw_windows (win->next); } }
193 static void draw_vertical_scroll_hint (struct WinMeta * win_meta, uint16_t x, uint32_t more_cols, char dir) {
194 // Draw scroll hint line in win at col x of pad display, announce more_cols more columns in direction dir.
196 char phrase[] = "more columns";
197 char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10); // 10 = max chars for uint32_t string
198 sprintf(scrolldesc, " %d %s ", more_cols, phrase);
200 if (win_meta->height > (strlen(scrolldesc) + 1))
201 offset = (win_meta->height - strlen(scrolldesc)) / 2;
202 for (y = 0; y < win_meta->height; y++)
203 if (y >= offset && y < strlen(scrolldesc) + offset)
204 mvwaddch(win_meta->pad, y, x, scrolldesc[y - offset] | A_REVERSE);
206 mvwaddch(win_meta->pad, y, x, dir | A_REVERSE);
209 extern void draw_all_windows (struct WinMeta * win_meta) {
210 // Draw pad with all windows and their borders, plus scrolling hints.
212 wnoutrefresh(win_meta->screen);
213 werase(win_meta->pad);
214 if (win_meta->chain_start) {
216 struct Win * win_p = win_meta->chain_start;
217 while (0 != win_p->next) {
220 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
221 draw_windows (win_meta->chain_start);
222 draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
224 for (i = 0; i < n_wins; i++) {
225 mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
226 mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
227 mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
228 mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
231 if (win_meta->pad_offset > 0)
232 draw_vertical_scroll_hint(win_meta, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
233 if (win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
234 for (y = 0; y < win_meta->height; y++)
235 draw_vertical_scroll_hint(win_meta, win_meta->pad_offset + win_meta->width - 1,
236 getmaxx(win_meta->pad) - (win_meta->pad_offset + win_meta->width), '>');
237 pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1); }
240 extern void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
241 // Grow or shrink currently active window. Correct its geometry and that of its followers.
242 if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->height) {
243 win_meta->active->height = height;
244 win_meta->active->width = width;
245 update_windows(win_meta, win_meta->chain_start); } }
247 extern void cycle_active_window (struct WinMeta * win_meta, char dir) {
248 // Cycle active window selection forwards (dir = 'n') or backwards.
249 if (0 != win_meta->active) {
251 if (win_meta->active->next != 0)
252 win_meta->active = win_meta->active->next;
254 win_meta->active = win_meta->chain_start; }
256 if (win_meta->active->prev != 0)
257 win_meta->active = win_meta->active->prev;
259 win_meta->active = win_meta->chain_end; } } }
261 extern void shift_active_window (struct WinMeta * win_meta, char dir) {
262 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
263 if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
264 struct Win * win_shift = win_meta->active, * win_p, * win_p_next;
266 if ((dir == 'f' && win_shift == win_meta->chain_end)
267 || (dir == 'b' && win_shift == win_meta->chain_start))
270 for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
272 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
273 for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
274 win_p_next = win_p->next;
275 suspend_window(win_meta, win_p);
277 win_p = win_p_next; }
280 append_window(win_meta, win_shift);
281 for (i = 0; i < i_max - 1; i++)
282 append_window(win_meta, wins[i]); }
284 for (i = 1; i < i_max; i++)
285 append_window(win_meta, wins[i]);
286 append_window(win_meta, win_shift); }
288 for (i = 0; i < i_max; i++)
289 if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
290 append_window(win_meta, wins[i+1]);
291 append_window(win_meta, wins[i]);
294 append_window(win_meta, wins[i]);
296 win_meta->active = win_shift; } }
298 extern void reset_pad_offset(struct WinMeta * win_meta, uint16_t new_offset) {
299 // Apply new_offset to windows pad, if it proves to be sane.
300 if (new_offset >= 0 && new_offset + win_meta->width < getmaxx(win_meta->pad))
301 win_meta->pad_offset = new_offset; }