17 static struct yx place_window (struct WinMeta *, struct Win *);
18 static void update_windows (struct WinMeta *, struct Win *);
19 static void destroy_window (struct Win *);
20 static void draw_windows_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
21 static void draw_window_borders (struct Win *, char);
22 static void draw_windows (struct Win *);
23 static void draw_vertical_scroll_hint (struct WinMeta *, uint16_t, uint32_t, char);
25 extern struct WinMeta init_win_meta (WINDOW * screen) {
26 // Create and populate WinMeta struct with sane default values.
27 struct WinMeta win_meta;
28 win_meta.screen = screen;
29 win_meta.height = getmaxy(screen);
30 win_meta.width = getmaxx(screen);
31 win_meta.chain_start = 0;
32 win_meta.chain_end = 0;
33 win_meta.pad_offset = 0;
34 win_meta.pad = newpad(win_meta.height, 1);
38 extern struct Win init_window (struct WinMeta * win_meta, char * title, void * data, void * func) {
39 // Create and populate Win struct with sane default values.
46 win.height = win_meta->height - 1;
51 extern void append_window (struct WinMeta * win_meta, struct Win * win) {
52 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
53 if (0 != win_meta->chain_start) {
54 win->prev = win_meta->chain_end;
55 win_meta->chain_end->next = win; }
57 win_meta->active = win;
58 win_meta->chain_start = win; }
59 win_meta->chain_end = win;
60 update_windows(win_meta, win); }
62 extern void suspend_window (struct WinMeta * win_meta, struct Win * win) {
63 // Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
65 if (win_meta->chain_start != win) // Give win's position in the chain to element next to it in the chain.
66 win->prev->next = win->next;
68 win_meta->chain_start = win->next;
69 char pad_refitted = 0;
70 if (win_meta->chain_end != win) { // Let chain element next to win know its new predecessor.
71 win->next->prev = win->prev;
72 if (win_meta->active == win) // If win was active, shift active window pointer to
73 win_meta->active = win->next; // the next chain element, if that is a window ...
74 update_windows(win_meta, win->next);
77 win_meta->chain_end = win->prev;
78 if (win_meta->active == win) // ... or else to the previous element.
79 win_meta->active = win->prev; }
82 if (0 == pad_refitted) { // Refit pad if necessary.
83 uint16_t lastwincol = 0;
84 struct Win * win_p = win_meta->chain_start;
86 if (getbegx(win_p->curses) + win_p->width > lastwincol + 1)
87 lastwincol = getbegx(win_p->curses) + win_p->width - 1;
88 win_p = win_p->next; }
89 if (getmaxx(win_meta->pad) != lastwincol)
90 wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); } }
92 static struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
93 // Based on position and sizes of previous window, find fitting place for current window.
95 start.x = 0; // if window is first in chain, place it on top-left corner
98 struct Win * win_top = win->prev;
99 while (getbegy(win_top->curses) != 1)
100 win_top = win_top->prev; // else, default to placing window in new top
101 start.x = getbegx(win_top->curses) + win_top->width + 1; // column to the right of the last one
102 uint16_t winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
103 if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
104 start.x = getbegx(win->prev->curses); // place window below previous window if it fits
105 start.y = winprev_maxy + 1; } // vertically and is not wider than its predecessor
107 struct Win * win_up = win->prev;
108 struct Win * win_upup = win_up;
110 while (win_up != win_top) {
111 win_upup = win_up->prev;
113 if (getbegy(win_up->curses) != getbegy(win_upup->curses))
115 win_upup = win_upup->prev; }
116 winprev_maxy = getbegy(win_upup->curses) + getmaxy(win_upup->curses);
117 widthdiff = (getbegx(win_upup->curses) + win_upup->width) - (getbegx(win_up->curses) + win_up->width);
118 if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
119 start.x = getbegx(win_up->curses) + win_up->width + 1; // else try to open new sub column under last
120 start.y = winprev_maxy + 1; // window below which enough space remains
122 win_up = win_upup; } } }
125 static void update_windows (struct WinMeta * win_meta, struct Win * win) {
126 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
127 if (0 != win->curses)
128 destroy_window (win);
129 struct yx startyx = place_window(win_meta, win);
130 uint16_t lastwincol = 0;
131 struct Win * win_p = win_meta->chain_start;
133 if (win_p != win && getbegx(win_p->curses) + win_p->width > lastwincol + 1)
134 lastwincol = getbegx(win_p->curses) + win_p->width - 1;
135 else if (win_p == win && startyx.x + win->width > lastwincol + 1)
136 lastwincol = startyx.x + win->width - 1;
137 win_p = win_p->next; }
138 if (getmaxx(win_meta->pad) != lastwincol)
139 wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2);
140 win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
142 update_windows (win_meta, win->next); }
144 static void destroy_window (struct Win * win) {
149 static void draw_window_borders (struct Win * win, char active) {
150 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
152 for (y = getbegy(win->curses); y <= getbegy(win->curses) + win->height; y++) {
153 mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) - 1, '|');
154 mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) + win->width, '|'); }
155 for (x = getbegx(win->curses); x <= getbegx(win->curses) + win->width; x++) {
156 mvwaddch(wgetparent(win->curses), getbegy(win->curses) - 1, x, '-');
157 mvwaddch(wgetparent(win->curses), getbegy(win->curses) + win->height, x, '-'); }
158 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
159 if (win->width >= min_title_length_visible) {
160 uint16_t title_offset = 0;
161 if (win->width > strlen(win->title) + 2)
162 title_offset = (win->width - (strlen(win->title) + 2)) / 2; // + 2 is for decoration
163 uint16_t length_visible = strnlen(win->title, win->width - 2);
164 char title[length_visible + 3];
165 char decoration = ' ';
168 memcpy(title + 1, win->title, length_visible);
169 title[0] = title[length_visible + 1] = decoration;
170 title[length_visible + 2] = '\0';
171 mvwaddstr (wgetparent(win->curses), getbegy(win->curses)-1, getbegx(win->curses)+title_offset, title); } }
173 static void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, uint16_t ccount) {
174 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
176 if (win == win_active)
178 draw_window_borders(win, active);
179 corners[ccount].tl.y = getbegy(win->curses) - 1;
180 corners[ccount].tl.x = getbegx(win->curses) - 1;
181 corners[ccount].tr.y = getbegy(win->curses) - 1;
182 corners[ccount].tr.x = getbegx(win->curses) + win->width;
183 corners[ccount].bl.y = getbegy(win->curses) + win->height;
184 corners[ccount].bl.x = getbegx(win->curses) - 1;
185 corners[ccount].br.y = getbegy(win->curses) + win->height;
186 corners[ccount].br.x = getbegx(win->curses) + win->width;
187 if (0 != win->next) {
188 draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
190 static void draw_windows (struct Win * win) {
191 // Draw contents of all windows in window chain from win on.
193 if (0 != win->next) {
194 draw_windows (win->next); } }
196 static void draw_vertical_scroll_hint (struct WinMeta * win_meta, uint16_t x, uint32_t more_cols, char dir) {
197 // Draw scroll hint line in win at col x of pad display, announce more_cols more columns in direction dir.
199 char phrase[] = "more columns";
200 char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10); // 10 = max chars for uint32_t string
201 sprintf(scrolldesc, " %d %s ", more_cols, phrase);
203 if (win_meta->height > (strlen(scrolldesc) + 1))
204 offset = (win_meta->height - strlen(scrolldesc)) / 2;
205 for (y = 0; y < win_meta->height; y++)
206 if (y >= offset && y < strlen(scrolldesc) + offset)
207 mvwaddch(win_meta->pad, y, x, scrolldesc[y - offset] | A_REVERSE);
209 mvwaddch(win_meta->pad, y, x, dir | A_REVERSE);
212 extern void draw_all_windows (struct WinMeta * win_meta) {
213 // Draw pad with all windows and their borders, plus scrolling hints.
215 wnoutrefresh(win_meta->screen);
216 werase(win_meta->pad);
217 if (win_meta->chain_start) {
219 struct Win * win_p = win_meta->chain_start;
220 while (0 != win_p->next) {
223 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
224 draw_windows (win_meta->chain_start);
225 draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
227 for (i = 0; i < n_wins; i++) {
228 mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
229 mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
230 mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
231 mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
234 if (win_meta->pad_offset > 0)
235 draw_vertical_scroll_hint(win_meta, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
236 if (win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
237 for (y = 0; y < win_meta->height; y++)
238 draw_vertical_scroll_hint(win_meta, win_meta->pad_offset + win_meta->width - 1,
239 getmaxx(win_meta->pad) - (win_meta->pad_offset + win_meta->width), '>');
240 pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1); }
243 extern void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
244 // Grow or shrink currently active window. Correct its geometry and that of its followers.
245 if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->height) {
246 win_meta->active->height = height;
247 win_meta->active->width = width;
248 update_windows(win_meta, win_meta->chain_start); } }
250 extern void cycle_active_window (struct WinMeta * win_meta, char dir) {
251 // Cycle active window selection forwards (dir = 'n') or backwards.
252 if (0 != win_meta->active) {
254 if (win_meta->active->next != 0)
255 win_meta->active = win_meta->active->next;
257 win_meta->active = win_meta->chain_start; }
259 if (win_meta->active->prev != 0)
260 win_meta->active = win_meta->active->prev;
262 win_meta->active = win_meta->chain_end; } } }
264 extern void shift_active_window (struct WinMeta * win_meta, char dir) {
265 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
266 if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
267 struct Win * win_shift = win_meta->active;
269 if ((dir == 'f' && win_shift == win_meta->chain_end)
270 || (dir == 'b' && win_shift == win_meta->chain_start))
273 struct Win * win_p, * win_p_next;
274 for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
276 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
277 for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
278 win_p_next = win_p->next;
279 suspend_window(win_meta, win_p);
281 win_p = win_p_next; }
284 append_window(win_meta, win_shift);
285 for (i = 0; i < i_max - 1; i++)
286 append_window(win_meta, wins[i]); }
288 for (i = 1; i < i_max; i++)
289 append_window(win_meta, wins[i]);
290 append_window(win_meta, win_shift); }
292 for (i = 0; i < i_max; i++)
293 if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
294 append_window(win_meta, wins[i+1]);
295 append_window(win_meta, wins[i]);
298 append_window(win_meta, wins[i]);
300 win_meta->active = win_shift; } }
302 extern void reset_pad_offset(struct WinMeta * win_meta, uint16_t new_offset) {
303 // Apply new_offset to windows pad, if it proves to be sane.
304 if (new_offset >= 0 && new_offset + win_meta->width < getmaxx(win_meta->pad))
305 win_meta->pad_offset = new_offset; }