7 struct WinMeta init_win_meta (WINDOW * screen) {
8 // Create and populate WinMeta struct with sane default values.
9 struct WinMeta win_meta;
10 win_meta.screen = screen;
11 win_meta.height = getmaxy(screen);
12 win_meta.width = getmaxx(screen);
13 win_meta.chain_start = 0;
14 win_meta.chain_end = 0;
15 win_meta.pad_offset = 0;
16 win_meta.pad = newpad(win_meta.height, 1);
20 void scroll_pad (struct WinMeta * win_meta, char dir) {
21 // Scroll pad left or right (if possible).
22 if ('+' == dir && win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
23 win_meta->pad_offset++;
24 else if ('-' == dir && win_meta->pad_offset > 0)
25 win_meta->pad_offset--; }
27 struct Win init_window (struct WinMeta * win_meta, char * title, void * data, void * func) {
28 // Create and populate Win struct with sane default values.
35 win.height = win_meta->height - 1;
40 void append_window (struct WinMeta * win_meta, struct Win * win) {
41 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
42 if (0 != win_meta->chain_start) {
43 win->prev = win_meta->chain_end;
44 win_meta->chain_end->next = win; }
46 win_meta->active = win;
47 win_meta->chain_start = win; }
48 win_meta->chain_end = win;
49 update_windows(win_meta, win); }
51 void suspend_window (struct WinMeta * win_meta, struct Win * win) {
52 // Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
54 if (win_meta->chain_start != win) // Give win's position in the chain to element next to it in the chain.
55 win->prev->next = win->next;
57 win_meta->chain_start = win->next;
58 char pad_refitted = 0;
59 if (win_meta->chain_end != win) { // Let chain element next to win know its new predecessor.
60 win->next->prev = win->prev;
61 if (win_meta->active == win) // If win was active, shift active window pointer to
62 win_meta->active = win->next; // the next chain element, if that is a window ...
63 update_windows(win_meta, win->next);
66 win_meta->chain_end = win->prev;
67 if (win_meta->active == win) // ... or else to the previous element.
68 win_meta->active = win->prev; }
71 if (0 == pad_refitted) { // Refit pad if necessary.
72 uint16_t lastwincol = 0;
73 struct Win * win_p = win_meta->chain_start;
75 if (getbegx(win_p->curses) + win_p->width > lastwincol + 1)
76 lastwincol = getbegx(win_p->curses) + win_p->width - 1;
77 win_p = win_p->next; }
78 if (getmaxx(win_meta->pad) != lastwincol)
79 wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); } }
81 struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
82 // Based on position and sizes of previous window, find fitting place for current window.
84 start.x = 0; // if window is first in chain, place it on top-left corner
87 struct Win * win_top = win->prev;
88 while (getbegy(win_top->curses) != 1)
89 win_top = win_top->prev; // else, default to placing window in new top
90 start.x = getbegx(win_top->curses) + win_top->width + 1; // column to the right of the last one
91 uint16_t winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
92 if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
93 start.x = getbegx(win->prev->curses); // place window below previous window if it fits
94 start.y = winprev_maxy + 1; } // vertically and is not wider than its predecessor
96 struct Win * win_up = win->prev;
97 struct Win * win_upup = win_up;
99 while (win_up != win_top) {
100 win_upup = win_up->prev;
102 if (getbegy(win_up->curses) != getbegy(win_upup->curses))
104 win_upup = win_upup->prev; }
105 winprev_maxy = getbegy(win_upup->curses) + getmaxy(win_upup->curses);
106 widthdiff = (getbegx(win_upup->curses) + win_upup->width) - (getbegx(win_up->curses) + win_up->width);
107 if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
108 start.x = getbegx(win_up->curses) + win_up->width + 1; // else try to open new sub column under last
109 start.y = winprev_maxy + 1; // window below which enough space remains
111 win_up = win_upup; } } }
114 void update_windows (struct WinMeta * win_meta, struct Win * win) {
115 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
116 if (0 != win->curses)
117 destroy_window (win);
118 struct yx startyx = place_window(win_meta, win);
119 uint16_t lastwincol = 0;
120 struct Win * win_p = win_meta->chain_start;
122 if (win_p != win && getbegx(win_p->curses) + win_p->width > lastwincol + 1)
123 lastwincol = getbegx(win_p->curses) + win_p->width - 1;
124 else if (win_p == win && startyx.x + win->width > lastwincol + 1)
125 lastwincol = startyx.x + win->width - 1;
126 win_p = win_p->next; }
127 if (getmaxx(win_meta->pad) != lastwincol)
128 wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2);
129 win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
131 update_windows (win_meta, win->next); }
133 void destroy_window (struct Win * win) {
138 void draw_window_borders (struct Win * win, char active) {
139 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
141 for (y = getbegy(win->curses); y <= getbegy(win->curses) + win->height; y++) {
142 mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) - 1, '|');
143 mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) + win->width, '|'); }
144 for (x = getbegx(win->curses); x <= getbegx(win->curses) + win->width; x++) {
145 mvwaddch(wgetparent(win->curses), getbegy(win->curses) - 1, x, '-');
146 mvwaddch(wgetparent(win->curses), getbegy(win->curses) + win->height, x, '-'); }
147 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
148 if (win->width >= min_title_length_visible) {
149 uint16_t title_offset = 0;
150 if (win->width > strlen(win->title) + 2)
151 title_offset = (win->width - (strlen(win->title) + 2)) / 2; // + 2 is for decoration
152 uint16_t length_visible = strnlen(win->title, win->width - 2);
153 char title[length_visible + 3];
154 char decoration = ' ';
157 memcpy(title + 1, win->title, length_visible);
158 title[0] = title[length_visible + 1] = decoration;
159 title[length_visible + 2] = '\0';
160 mvwaddstr (wgetparent(win->curses), getbegy(win->curses)-1, getbegx(win->curses)+title_offset, title); } }
162 void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, uint16_t ccount) {
163 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
165 if (win == win_active)
167 draw_window_borders(win, active);
168 corners[ccount].tl.y = getbegy(win->curses) - 1;
169 corners[ccount].tl.x = getbegx(win->curses) - 1;
170 corners[ccount].tr.y = getbegy(win->curses) - 1;
171 corners[ccount].tr.x = getbegx(win->curses) + win->width;
172 corners[ccount].bl.y = getbegy(win->curses) + win->height;
173 corners[ccount].bl.x = getbegx(win->curses) - 1;
174 corners[ccount].br.y = getbegy(win->curses) + win->height;
175 corners[ccount].br.x = getbegx(win->curses) + win->width;
176 if (0 != win->next) {
177 draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
179 void draw_windows (struct Win * win) {
180 // Draw contents of all windows in window chain from win on.
182 if (0 != win->next) {
183 draw_windows (win->next); } }
185 void draw_vertical_scroll_hint (struct WinMeta * win_meta, uint16_t x, uint32_t more_cols, char dir) {
186 // Draw scroll hint line in win at col x of pad display, announce more_cols more columns in direction dir.
188 char phrase[] = "more columns";
189 char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10); // 10 = max chars for uint32_t string
190 sprintf(scrolldesc, " %d %s ", more_cols, phrase);
192 if (win_meta->height > (strlen(scrolldesc) + 1))
193 offset = (win_meta->height - strlen(scrolldesc)) / 2;
194 for (y = 0; y < win_meta->height; y++)
195 if (y >= offset && y < strlen(scrolldesc) + offset)
196 mvwaddch(win_meta->pad, y, x, scrolldesc[y - offset] | A_REVERSE);
198 mvwaddch(win_meta->pad, y, x, dir | A_REVERSE);
201 void draw_all_windows (struct WinMeta * win_meta) {
202 // Draw pad with all windows and their borders, plus scrolling hints.
204 wnoutrefresh(win_meta->screen);
205 werase(win_meta->pad);
206 if (win_meta->chain_start) {
208 struct Win * win_p = win_meta->chain_start;
209 while (0 != win_p->next) {
212 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
213 draw_windows (win_meta->chain_start);
214 draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
216 for (i = 0; i < n_wins; i++) {
217 mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
218 mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
219 mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
220 mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
223 if (win_meta->pad_offset > 0)
224 draw_vertical_scroll_hint(win_meta, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
225 if (win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
226 for (y = 0; y < win_meta->height; y++)
227 draw_vertical_scroll_hint(win_meta, win_meta->pad_offset + win_meta->width - 1,
228 getmaxx(win_meta->pad) - (win_meta->pad_offset + win_meta->width), '>');
229 pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1); }
232 void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
233 // Grow or shrink currently active window. Correct its geometry and that of its followers.
234 if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->height) {
235 win_meta->active->height = height;
236 win_meta->active->width = width;
237 update_windows(win_meta, win_meta->chain_start); } }
239 void cycle_active_window (struct WinMeta * win_meta, char dir) {
240 // Cycle active window selection forwards (dir = 'n') or backwards.
241 if (0 != win_meta->active) {
243 if (win_meta->active->next != 0)
244 win_meta->active = win_meta->active->next;
246 win_meta->active = win_meta->chain_start; }
248 if (win_meta->active->prev != 0)
249 win_meta->active = win_meta->active->prev;
251 win_meta->active = win_meta->chain_end; } } }
253 void shift_active_window (struct WinMeta * win_meta, char dir) {
254 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
255 if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
256 struct Win * win_shift = win_meta->active;
258 if ((dir == 'f' && win_shift == win_meta->chain_end)
259 || (dir == 'b' && win_shift == win_meta->chain_start))
262 struct Win * win_p, * win_p_next;
263 for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
265 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
266 for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
267 win_p_next = win_p->next;
268 suspend_window(win_meta, win_p);
270 win_p = win_p_next; }
273 append_window(win_meta, win_shift);
274 for (i = 0; i < i_max - 1; i++)
275 append_window(win_meta, wins[i]); }
277 for (i = 1; i < i_max; i++)
278 append_window(win_meta, wins[i]);
279 append_window(win_meta, win_shift); }
281 for (i = 0; i < i_max; i++)
282 if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
283 append_window(win_meta, wins[i+1]);
284 append_window(win_meta, wins[i]);
287 append_window(win_meta, wins[i]);
289 win_meta->active = win_shift; } }