11 struct yx_uint16 br; };
13 static void refit_pad (struct WinMeta *);
14 static void place_window (struct WinMeta *, struct Win *);
15 static void update_windows (struct WinMeta *, struct Win *);
16 static void destroy_window (struct Win *);
17 static void draw_windows_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
18 static void draw_window_borders (struct Win *, char);
19 static void draw_windows (struct Win *);
21 extern struct WinMeta init_win_meta (WINDOW * screen) {
22 // Create and populate WinMeta struct with sane default values.
23 struct WinMeta win_meta;
24 win_meta.screen = screen;
25 win_meta.pad.size.y = getmaxy(screen);
26 win_meta.pad.size.x = getmaxx(screen);
27 win_meta.chain_start = 0;
28 win_meta.chain_end = 0;
29 win_meta.pad_offset = 0;
30 win_meta.pad.curses_win = newpad(win_meta.pad.size.y, 1);
34 extern struct Win init_window (struct WinMeta * win_meta, char * title, void * data, void * func) {
35 // Create and populate Win struct with sane default values.
39 win.frame.curses_win = 0;
41 win.frame.size.x = 20;
42 win.frame.size.y = win_meta->pad.size.y - 1;
47 extern void append_window (struct WinMeta * win_meta, struct Win * win) {
48 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
49 if (0 != win_meta->chain_start) {
50 win->prev = win_meta->chain_end;
51 win_meta->chain_end->next = win; }
53 win_meta->active = win;
54 win_meta->chain_start = win; }
55 win_meta->chain_end = win;
56 update_windows(win_meta, win); }
58 static void refit_pad (struct WinMeta * win_meta) {
59 // Fit pad width to minimum width demanded by current windows' geometries.
60 uint16_t lastwincol = 0;
61 struct Win * win_p = win_meta->chain_start;
63 if (win_p->start.x + win_p->frame.size.x > lastwincol + 1)
64 lastwincol = win_p->start.x + win_p->frame.size.x - 1;
65 win_p = win_p->next; }
66 if (getmaxx(win_meta->pad.curses_win) != lastwincol)
67 wresize(win_meta->pad.curses_win, getmaxy(win_meta->pad.curses_win), lastwincol + 2); }
69 extern void suspend_window (struct WinMeta * win_meta, struct Win * win) {
70 // Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
72 if (win_meta->chain_start != win) // Give win's position in the chain to element next to it in the chain.
73 win->prev->next = win->next;
75 win_meta->chain_start = win->next;
76 char pad_refitted = 0;
77 if (win_meta->chain_end != win) { // Let chain element next to win know its new predecessor.
78 win->next->prev = win->prev;
79 if (win_meta->active == win) // If win was active, shift active window pointer to
80 win_meta->active = win->next; // the next chain element, if that is a window ...
81 update_windows(win_meta, win->next);
84 win_meta->chain_end = win->prev;
85 if (win_meta->active == win) // ... or else to the previous element.
86 win_meta->active = win->prev; }
89 if (0 == pad_refitted) // Refit pad if necessary.
90 refit_pad(win_meta); }
92 static void place_window (struct WinMeta * win_meta, struct Win * win) {
93 // Based on position and sizes of previous window, find fitting place for current window.
94 win->start.x = 0; // if window is first in chain, place it on top-left corner
97 struct Win * win_top = win->prev;
98 while (win_top->start.y != 1)
99 win_top = win_top->prev; // else, default to placing window in new top
100 win->start.x = win_top->start.x + win_top->frame.size.x + 1; // column to the right of the last one
101 uint16_t winprev_maxy = win->prev->start.y + getmaxy(win->prev->frame.curses_win);
102 if ( win->frame.size.x <= win->prev->frame.size.x
103 && win->frame.size.y < win_meta->pad.size.y - winprev_maxy) {
104 win->start.x = win->prev->start.x; // place window below previous window if it fits
105 win->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 (win_up->start.y != win_upup->start.y)
115 win_upup = win_upup->prev; }
116 winprev_maxy = win_upup->start.y + getmaxy(win_upup->frame.curses_win);
117 widthdiff = (win_upup->start.x + win_upup->frame.size.x) - (win_up->start.x + win_up->frame.size.x);
118 if (win->frame.size.y < win_meta->pad.size.y - winprev_maxy && win->frame.size.x < widthdiff) {
119 win->start.x = win_up->start.x + win_up->frame.size.x + 1 ; // else try to open new sub column under
120 win->start.y = winprev_maxy + 1; // last window below which enough space remains
122 win_up = win_upup; } } } }
124 static void update_windows (struct WinMeta * win_meta, struct Win * win) {
125 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
126 if (0 != win->frame.curses_win)
127 destroy_window (win);
128 place_window(win_meta, win);
130 win->frame.curses_win = subpad(win_meta->pad.curses_win, win->frame.size.y, win->frame.size.x, win->start.y,
133 update_windows (win_meta, win->next); }
135 static void destroy_window (struct Win * win) {
137 delwin(win->frame.curses_win);
138 win->frame.curses_win = 0; }
140 static void draw_window_borders (struct Win * win, char active) {
141 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
143 for (y = win->start.y; y <= win->start.y + win->frame.size.y; y++) {
144 mvwaddch(wgetparent(win->frame.curses_win), y, win->start.x - 1, '|');
145 mvwaddch(wgetparent(win->frame.curses_win), y, win->start.x + win->frame.size.x, '|'); }
146 for (x = win->start.x; x <= win->start.x + win->frame.size.x; x++) {
147 mvwaddch(wgetparent(win->frame.curses_win), win->start.y - 1, x, '-');
148 mvwaddch(wgetparent(win->frame.curses_win), win->start.y + win->frame.size.y, x, '-'); }
149 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
150 if (win->frame.size.x >= min_title_length_visible) {
151 uint16_t title_offset = 0;
152 if (win->frame.size.x > strlen(win->title) + 2)
153 title_offset = (win->frame.size.x - (strlen(win->title) + 2)) / 2; // + 2 is for decoration
154 uint16_t length_visible = strnlen(win->title, win->frame.size.x - 2);
155 char title[length_visible + 3];
156 char decoration = ' ';
159 memcpy(title + 1, win->title, length_visible);
160 title[0] = title[length_visible + 1] = decoration;
161 title[length_visible + 2] = '\0';
162 mvwaddstr(wgetparent(win->frame.curses_win), win->start.y - 1, win->start.x + title_offset, title); } }
164 static void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners,
166 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
168 if (win == win_active)
170 draw_window_borders(win, active);
171 corners[ccount].tl.y = win->start.y - 1;
172 corners[ccount].tl.x = win->start.x - 1;
173 corners[ccount].tr.y = win->start.y - 1;
174 corners[ccount].tr.x = win->start.x + win->frame.size.x;
175 corners[ccount].bl.y = win->start.y + win->frame.size.y;
176 corners[ccount].bl.x = win->start.x - 1;
177 corners[ccount].br.y = win->start.y + win->frame.size.y;
178 corners[ccount].br.x = win->start.x + win->frame.size.x;
179 if (0 != win->next) {
180 draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
182 static void draw_windows (struct Win * win) {
183 // Draw contents of all windows in window chain from win on.
185 if (0 != win->next) {
186 draw_windows (win->next); } }
188 extern void draw_scroll_hint (struct Frame * frame, uint16_t pos, uint32_t dist, char dir) {
189 // Draw scroll hint into frame at pos (row or col dependend on dir), mark distance of dist cells into dir.
190 char more[] = "more";
191 char unit_cols[] = "columns";
192 char unit_rows[] = "lines";
193 uint16_t dsc_space = frame->size.x;
194 char * unit = unit_rows;
195 if ('<' == dir || '>' == dir) {
196 dsc_space = frame->size.y;
198 char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit) + 10); // 10 = uint32 max strlen
199 sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
201 if (dsc_space > strlen(scrolldsc) + 1)
202 offset = (dsc_space - strlen(scrolldsc)) / 2;
204 for (q = 0; q < dsc_space; q++) {
205 if (q >= offset && q < strlen(scrolldsc) + offset)
206 symbol = scrolldsc[q - offset] | A_REVERSE;
208 symbol = dir | A_REVERSE;
209 if ('<' == dir || '>' == dir)
210 mvwaddch(frame->curses_win, q, pos, symbol);
212 mvwaddch(frame->curses_win, pos, q, symbol); }
215 extern void draw_all_windows (struct WinMeta * win_meta) {
216 // Draw pad with all windows and their borders, plus scrolling hints.
218 wnoutrefresh(win_meta->screen);
219 werase(win_meta->pad.curses_win);
220 if (win_meta->chain_start) {
222 struct Win * win_p = win_meta->chain_start;
223 while (0 != win_p->next) {
226 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
227 draw_windows (win_meta->chain_start);
228 draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
230 for (i = 0; i < n_wins; i++) {
231 mvwaddch(win_meta->pad.curses_win, all_corners[i].tl.y, all_corners[i].tl.x, '+');
232 mvwaddch(win_meta->pad.curses_win, all_corners[i].tr.y, all_corners[i].tr.x, '+');
233 mvwaddch(win_meta->pad.curses_win, all_corners[i].bl.y, all_corners[i].bl.x, '+');
234 mvwaddch(win_meta->pad.curses_win, all_corners[i].br.y, all_corners[i].br.x, '+'); }
237 if (win_meta->pad_offset > 0)
238 draw_scroll_hint(&win_meta->pad, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
239 if (win_meta->pad_offset + win_meta->pad.size.x < getmaxx(win_meta->pad.curses_win) - 1)
240 for (y = 0; y < win_meta->pad.size.y; y++)
241 draw_scroll_hint(&win_meta->pad, win_meta->pad_offset + win_meta->pad.size.x - 1,
242 getmaxx(win_meta->pad.curses_win) - (win_meta->pad_offset + win_meta->pad.size.x),
244 pnoutrefresh(win_meta->pad.curses_win, 0, win_meta->pad_offset, 0, 0, win_meta->pad.size.y,
245 win_meta->pad.size.x - 1); }
248 extern void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
249 // Grow or shrink currently active window. Correct its geometry and that of its followers.
250 if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->pad.size.y) {
251 win_meta->active->frame.size.y = height;
252 win_meta->active->frame.size.x = width;
253 update_windows(win_meta, win_meta->chain_start); } }
255 extern void cycle_active_window (struct WinMeta * win_meta, char dir) {
256 // Cycle active window selection forwards (dir = 'n') or backwards.
257 if (0 != win_meta->active) {
259 if (win_meta->active->next != 0)
260 win_meta->active = win_meta->active->next;
262 win_meta->active = win_meta->chain_start; }
264 if (win_meta->active->prev != 0)
265 win_meta->active = win_meta->active->prev;
267 win_meta->active = win_meta->chain_end; } } }
269 extern void shift_active_window (struct WinMeta * win_meta, char dir) {
270 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
271 if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
272 struct Win * win_shift = win_meta->active, * win_p, * win_p_next;
274 if ((dir == 'f' && win_shift == win_meta->chain_end)
275 || (dir == 'b' && win_shift == win_meta->chain_start))
278 for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
280 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
281 for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
282 win_p_next = win_p->next;
283 suspend_window(win_meta, win_p);
285 win_p = win_p_next; }
288 append_window(win_meta, win_shift);
289 for (i = 0; i < i_max - 1; i++)
290 append_window(win_meta, wins[i]); }
292 for (i = 1; i < i_max; i++)
293 append_window(win_meta, wins[i]);
294 append_window(win_meta, win_shift); }
296 for (i = 0; i < i_max; i++)
297 if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
298 append_window(win_meta, wins[i+1]);
299 append_window(win_meta, wins[i]);
302 append_window(win_meta, wins[i]);
304 win_meta->active = win_shift; } }
306 extern void reset_pad_offset(struct WinMeta * win_meta, uint16_t new_offset) {
307 // Apply new_offset to windows pad, if it proves to be sane.
308 if (new_offset >= 0 && new_offset + win_meta->pad.size.x < getmaxx(win_meta->pad.curses_win))
309 win_meta->pad_offset = new_offset; }