11 struct yx_uint16 br; };
13 static void refit_pad (struct WinMeta *);
14 static void place_win (struct WinMeta *, struct Win *);
15 static void update_wins (struct WinMeta *, struct Win *);
16 static void destroy_win (struct Win *);
17 static void draw_wins_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
18 static void draw_win_borders (struct Win *, char);
19 static void draw_wins (struct Win *);
21 extern struct WinMeta init_win_meta (WINDOW * screen) {
22 // Create and populate WinMeta struct with sane default values.
24 wmeta.screen = screen;
25 wmeta.pad.size.y = getmaxy(screen);
26 wmeta.pad.size.x = getmaxx(screen);
27 wmeta.chain_start = 0;
30 wmeta.pad.curses_win = newpad(wmeta.pad.size.y, 1);
34 extern struct Win init_win (struct WinMeta * wmeta, char * title, void * data, void * func) {
35 // Create and populate Win struct with sane default values.
39 w.frame.curses_win = 0;
42 w.frame.size.y = wmeta->pad.size.y - 1;
47 extern void append_win (struct WinMeta * wmeta, struct Win * w) {
48 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
49 if (0 != wmeta->chain_start) {
50 w->prev = wmeta->chain_end;
51 wmeta->chain_end->next = w; }
54 wmeta->chain_start = w; }
56 update_wins(wmeta, w); }
58 static void refit_pad (struct WinMeta * wmeta) {
59 // Fit pad width to minimum width demanded by current windows' geometries.
60 uint16_t lastwincol = 0;
61 struct Win * w_p = wmeta->chain_start;
63 if (w_p->start.x + w_p->frame.size.x > lastwincol + 1)
64 lastwincol = w_p->start.x + w_p->frame.size.x - 1;
66 if (getmaxx(wmeta->pad.curses_win) != lastwincol)
67 wresize(wmeta->pad.curses_win, getmaxy(wmeta->pad.curses_win), lastwincol + 2); }
69 extern void suspend_win (struct WinMeta * wmeta, struct Win * w) {
70 // Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
72 if (wmeta->chain_start != w) // Give win's position in the chain to element next to it in the chain.
73 w->prev->next = w->next;
75 wmeta->chain_start = w->next;
76 char pad_refitted = 0;
77 if (wmeta->chain_end != w) { // Let chain element next to win know its new predecessor.
78 w->next->prev = w->prev;
79 if (wmeta->active == w) // If win was active, shift active window pointer to
80 wmeta->active = w->next; // the next chain element, if that is a window ...
81 update_wins(wmeta, w->next);
84 wmeta->chain_end = w->prev;
85 if (wmeta->active == w) // ... or else to the previous element.
86 wmeta->active = w->prev; }
89 if (0 == pad_refitted) // Refit pad if necessary.
92 static void place_win (struct WinMeta * wmeta, struct Win * w) {
93 // Based on position and sizes of previous window, find fitting place for current window.
94 w->start.x = 0; // if window is first in chain, place it on top-left corner
97 struct Win * w_top = w->prev;
98 while (w_top->start.y != 1)
99 w_top = w_top->prev; // else, default to placing window in new top
100 w->start.x = w_top->start.x + w_top->frame.size.x + 1; // column to the right of the last one
101 uint16_t w_prev_maxy = w->prev->start.y + getmaxy(w->prev->frame.curses_win);
102 if (w->frame.size.x <= w->prev->frame.size.x && w->frame.size.y < wmeta->pad.size.y - w_prev_maxy) {
103 w->start.x = w->prev->start.x; // place window below previous window if it fits
104 w->start.y = w_prev_maxy + 1; } // vertically and is not wider than its predecessor
106 struct Win * w_up = w->prev;
107 struct Win * w_upup = w_up;
109 while (w_up != w_top) {
112 if (w_up->start.y != w_upup->start.y)
114 w_upup = w_upup->prev; }
115 w_prev_maxy = w_upup->start.y + getmaxy(w_upup->frame.curses_win);
116 widthdiff = (w_upup->start.x + w_upup->frame.size.x) - (w_up->start.x + w_up->frame.size.x);
117 if (w->frame.size.y < wmeta->pad.size.y - w_prev_maxy && w->frame.size.x < widthdiff) {
118 w->start.x = w_up->start.x + w_up->frame.size.x + 1 ; // else try to open new sub column under
119 w->start.y = w_prev_maxy + 1; // last window below which enough space remains
121 w_up = w_upup; } } } }
123 static void update_wins (struct WinMeta * wmeta, struct Win * w) {
124 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
125 if (0 != w->frame.curses_win)
129 w->frame.curses_win=subpad(wmeta->pad.curses_win, w->frame.size.y, w->frame.size.x, w->start.y, w->start.x);
131 update_wins (wmeta, w->next); }
133 static void destroy_win (struct Win * w) {
135 delwin(w->frame.curses_win);
136 w->frame.curses_win = 0; }
138 static void draw_win_borders (struct Win * w, char active) {
139 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
141 for (y = w->start.y; y <= w->start.y + w->frame.size.y; y++) {
142 mvwaddch(wgetparent(w->frame.curses_win), y, w->start.x - 1, '|');
143 mvwaddch(wgetparent(w->frame.curses_win), y, w->start.x + w->frame.size.x, '|'); }
144 for (x = w->start.x; x <= w->start.x + w->frame.size.x; x++) {
145 mvwaddch(wgetparent(w->frame.curses_win), w->start.y - 1, x, '-');
146 mvwaddch(wgetparent(w->frame.curses_win), w->start.y + w->frame.size.y, x, '-'); }
147 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
148 if (w->frame.size.x >= min_title_length_visible) {
149 uint16_t title_offset = 0;
150 if (w->frame.size.x > strlen(w->title) + 2)
151 title_offset = (w->frame.size.x - (strlen(w->title) + 2)) / 2; // + 2 is for decoration
152 uint16_t length_visible = strnlen(w->title, w->frame.size.x - 2);
153 char title[length_visible + 3];
154 char decoration = ' ';
157 memcpy(title + 1, w->title, length_visible);
158 title[0] = title[length_visible + 1] = decoration;
159 title[length_visible + 2] = '\0';
160 mvwaddstr(wgetparent(w->frame.curses_win), w->start.y - 1, w->start.x + title_offset, title); } }
162 static void draw_wins_borders (struct Win * w, struct Win * w_active, struct Corners * corners, uint16_t i) {
163 // Call draw_win_borders() for all windows in chain from win on. Save current window's border corners.
167 draw_win_borders(w, active);
168 corners[i].tl.y = w->start.y - 1;
169 corners[i].tl.x = w->start.x - 1;
170 corners[i].tr.y = w->start.y - 1;
171 corners[i].tr.x = w->start.x + w->frame.size.x;
172 corners[i].bl.y = w->start.y + w->frame.size.y;
173 corners[i].bl.x = w->start.x - 1;
174 corners[i].br.y = w->start.y + w->frame.size.y;
175 corners[i].br.x = w->start.x + w->frame.size.x;
177 draw_wins_borders (w->next, w_active, corners, i + 1); } }
179 static void draw_wins (struct Win * w) {
180 // Draw contents of all windows in window chain from win on.
183 draw_wins (w->next); } }
185 extern void draw_scroll_hint (struct Frame * frame, uint16_t pos, uint32_t dist, char dir) {
186 // Draw scroll hint into frame at pos (row or col dependend on dir), mark distance of dist cells into dir.
187 char more[] = "more";
188 char unit_cols[] = "columns";
189 char unit_rows[] = "lines";
190 uint16_t dsc_space = frame->size.x;
191 char * unit = unit_rows;
192 if ('<' == dir || '>' == dir) {
193 dsc_space = frame->size.y;
195 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit) + 10); // 10 = uint32 max strlen
196 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
198 if (dsc_space > strlen(scrolldsc) + 1)
199 offset = (dsc_space - strlen(scrolldsc)) / 2;
201 for (q = 0; q < dsc_space; q++) {
202 if (q >= offset && q < strlen(scrolldsc) + offset)
203 symbol = scrolldsc[q - offset] | A_REVERSE;
205 symbol = dir | A_REVERSE;
206 if ('<' == dir || '>' == dir)
207 mvwaddch(frame->curses_win, q, pos, symbol);
209 mvwaddch(frame->curses_win, pos, q, symbol); }
212 extern void draw_all_wins (struct WinMeta * wmeta) {
213 // Draw pad with all windows and their borders, plus scrolling hints.
215 wnoutrefresh(wmeta->screen);
216 werase(wmeta->pad.curses_win);
217 if (wmeta->chain_start) {
218 uint16_t n_wins = 1, i;
219 struct Win * win_p = wmeta->chain_start;
220 while (0 != win_p->next) {
223 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
224 draw_wins (wmeta->chain_start);
225 draw_wins_borders (wmeta->chain_start, wmeta->active, all_corners, 0);
226 for (i = 0; i < n_wins; i++) {
227 mvwaddch(wmeta->pad.curses_win, all_corners[i].tl.y, all_corners[i].tl.x, '+');
228 mvwaddch(wmeta->pad.curses_win, all_corners[i].tr.y, all_corners[i].tr.x, '+');
229 mvwaddch(wmeta->pad.curses_win, all_corners[i].bl.y, all_corners[i].bl.x, '+');
230 mvwaddch(wmeta->pad.curses_win, all_corners[i].br.y, all_corners[i].br.x, '+'); }
232 if (wmeta->pad_offset > 0)
233 draw_scroll_hint(&wmeta->pad, wmeta->pad_offset, wmeta->pad_offset + 1, '<');
234 if (wmeta->pad_offset + wmeta->pad.size.x < getmaxx(wmeta->pad.curses_win) - 1)
235 draw_scroll_hint(&wmeta->pad, wmeta->pad_offset + wmeta->pad.size.x - 1,
236 getmaxx(wmeta->pad.curses_win) - (wmeta->pad_offset + wmeta->pad.size.x), '>');
237 pnoutrefresh(wmeta->pad.curses_win, 0, wmeta->pad_offset, 0, 0, wmeta->pad.size.y, wmeta->pad.size.x-1); }
240 extern void resize_active_win (struct WinMeta * wmeta, uint16_t height, uint16_t width) {
241 // Grow or shrink currently active window. Correct its geometry and that of its followers.
242 if (0 != wmeta->active && width > 0 && height > 0 && height < wmeta->pad.size.y) {
243 wmeta->active->frame.size.y = height;
244 wmeta->active->frame.size.x = width;
245 update_wins(wmeta, wmeta->chain_start); } }
247 extern void cycle_active_win (struct WinMeta * wmeta, char dir) {
248 // Cycle active window selection forwards (dir = 'n') or backwards.
249 if (0 != wmeta->active) {
251 if (wmeta->active->next != 0)
252 wmeta->active = wmeta->active->next;
254 wmeta->active = wmeta->chain_start; }
256 if (wmeta->active->prev != 0)
257 wmeta->active = wmeta->active->prev;
259 wmeta->active = wmeta->chain_end; } } }
261 extern void shift_active_win (struct WinMeta * wmeta, char dir) {
262 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
263 if (0 != wmeta->active && wmeta->chain_start != wmeta->chain_end && (dir == 'f' || dir == 'b')) {
264 struct Win * w_shift = wmeta->active, * w_p, * w_p_next;
266 if ((dir == 'f' && w_shift == wmeta->chain_end) || (dir == 'b' && w_shift == wmeta->chain_start))
269 for (i_max = 1, w_p = wmeta->chain_start; w_p != wmeta->chain_end; i_max++)
271 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
272 for (i = 0, w_p = wmeta->chain_start; i < i_max; i++) {
273 w_p_next = w_p->next;
274 suspend_win(wmeta, w_p);
279 append_win(wmeta, w_shift);
280 for (i = 0; i < i_max - 1; i++)
281 append_win(wmeta, wins[i]); }
283 for (i = 1; i < i_max; i++)
284 append_win(wmeta, wins[i]);
285 append_win(wmeta, w_shift); }
287 for (i = 0; i < i_max; i++)
288 if ((dir == 'f' && w_shift == wins[i]) || (dir == 'b' && w_shift == wins[i+1])) {
289 append_win(wmeta, wins[i+1]);
290 append_win(wmeta, wins[i]);
293 append_win(wmeta, wins[i]);
295 wmeta->active = w_shift; } }
297 extern void reset_pad_offset(struct WinMeta * wmeta, uint16_t new_offset) {
298 // Apply new_offset to windows pad, if it proves to be sane.
299 if (new_offset >= 0 && new_offset + wmeta->pad.size.x < getmaxx(wmeta->pad.curses_win))
300 wmeta->pad_offset = new_offset; }