6 struct WinMeta init_win_meta (WINDOW * screen) {
7 // Create and populate WinMeta struct with sane default values.
8 struct WinMeta win_meta;
9 win_meta.screen = screen;
10 win_meta.height = getmaxy(screen);
11 win_meta.width = getmaxx(screen);
12 win_meta.chain_start = 0;
13 win_meta.chain_end = 0;
14 win_meta.pad_offset = 0;
15 win_meta.pad = newpad(win_meta.height, 1);
18 void scroll_pad (struct WinMeta * win_meta, char dir) {
19 // Scroll pad left (if possible) or right.
21 win_meta->pad_offset++;
22 else if ('-' == dir && win_meta->pad_offset > 0)
23 win_meta->pad_offset--; }
25 struct Win init_window (struct WinMeta * win_meta, char * title) {
26 // Create and populate Win struct with sane default values.
33 win.height = win_meta->height - 1;
36 void append_window (struct WinMeta * win_meta, struct Win * win) {
37 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
38 if (0 != win_meta->chain_start) {
39 win->prev = win_meta->chain_end;
40 win_meta->chain_end->next = win; }
42 win_meta->active = win;
43 win_meta->chain_start = win; }
44 win_meta->chain_end = win;
45 update_windows(win_meta, win); }
47 void suspend_window (struct WinMeta * win_meta, struct Win * win) {
48 // Destroy win, suspend from window chain. Update geometry of following rows, as well as activity selection.
50 if (win_meta->chain_start != win) // Give win's position in the chain to element next to it in the chain.
51 win->prev->next = win->next;
53 win_meta->chain_start = win->next;
54 if (win_meta->chain_end != win) { // Let chain element next to win know its new predecessor.
55 win->next->prev = win->prev;
56 if (win_meta->active == win) // If win was active, shift active window pointer to
57 win_meta->active = win->next; // the next chain element, if that is a window ...
58 update_windows(win_meta, win->next); }
60 win_meta->chain_end = win->prev;
61 if (win_meta->active == win) // ... or else to the previous element.
62 win_meta->active = win->prev; }
66 struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
67 // Based on position and sizes of previous window, find fitting place for current window.
69 start.x = 0; // if window is first in chain, place it on top-left corner
72 struct Win * win_top = win->prev;
73 while (getbegy(win_top->curses) != 1)
74 win_top = win_top->prev; // else, default to placing window in new top
75 start.x = getbegx(win_top->curses) + win_top->width + 1; // column to the right of the last one
76 int winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
77 if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
78 start.x = getbegx(win->prev->curses); // place window below previous window if it fits
79 start.y = winprev_maxy + 1; } // vertically and is not wider than its predecessor
81 struct Win * win_up = win->prev;
82 struct Win * win_upup = win_up;
84 while (win_up != win_top) {
85 win_upup = win_up->prev;
87 if (getbegy(win_up->curses) != getbegy(win_upup->curses))
89 win_upup = win_upup->prev; }
90 winprev_maxy = getbegy(win_upup->curses) + getmaxy(win_upup->curses);
91 widthdiff = (getbegx(win_upup->curses) + win_upup->width) - (getbegx(win_up->curses) + win_up->width);
92 if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
93 start.x = getbegx(win_up->curses) + win_up->width + 1; // else try to open new sub column under last
94 start.y = winprev_maxy + 1; // window below which enough space remains
96 win_up = win_upup; } } }
99 void update_windows (struct WinMeta * win_meta, struct Win * win) {
100 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
101 if (0 != win->curses)
102 destroy_window (win);
103 struct yx startyx = place_window(win_meta, win);
105 struct Win * win_p = win_meta->chain_start;
107 if (win_p != win && getbegx(win_p->curses) + win_p->width > lastwincol + 1)
108 lastwincol = getbegx(win_p->curses) + win_p->width - 1;
109 else if (win_p == win && startyx.x + win->width > lastwincol + 1)
110 lastwincol = startyx.x + win->width - 1;
111 win_p = win_p->next; }
112 if (getmaxx(win_meta->pad) != lastwincol) {
113 wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); }
114 win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
116 update_windows (win_meta, win->next); }
118 void destroy_window (struct Win * win) {
123 void draw_window_borders (struct Win * win, char active) {
124 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
126 for (y = getbegy(win->curses); y <= getbegy(win->curses) + win->height; y++) {
127 mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) - 1, '|');
128 mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) + win->width, '|'); }
129 for (x = getbegx(win->curses); x <= getbegx(win->curses) + win->width; x++) {
130 mvwaddch(wgetparent(win->curses), getbegy(win->curses) - 1, x, '-');
131 mvwaddch(wgetparent(win->curses), getbegy(win->curses) + win->height, x, '-'); }
132 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
133 if (win->width >= min_title_length_visible) {
134 int title_offset = 0;
135 if (win->width > strlen(win->title) + 2)
136 title_offset = (win->width - (strlen(win->title) + 2)) / 2; // + 2 is for decoration
137 int length_visible = strnlen(win->title, win->width - 2);
138 char title[length_visible + 3];
139 char decoration = ' ';
142 memcpy(title + 1, win->title, length_visible);
143 title[0] = title[length_visible + 1] = decoration;
144 title[length_visible + 2] = '\0';
145 mvwaddstr (wgetparent(win->curses), getbegy(win->curses)-1, getbegx(win->curses)+title_offset, title); } }
147 void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, int ccount) {
148 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
150 if (win == win_active)
152 draw_window_borders(win, active);
153 corners[ccount].tl.y = getbegy(win->curses) - 1;
154 corners[ccount].tl.x = getbegx(win->curses) - 1;
155 corners[ccount].tr.y = getbegy(win->curses) - 1;
156 corners[ccount].tr.x = getbegx(win->curses) + win->width;
157 corners[ccount].bl.y = getbegy(win->curses) + win->height;
158 corners[ccount].bl.x = getbegx(win->curses) - 1;
159 corners[ccount].br.y = getbegy(win->curses) + win->height;
160 corners[ccount].br.x = getbegx(win->curses) + win->width;
161 if (0 != win->next) {
162 draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
164 void draw_windows (struct Win * win) {
165 // Draw contents of all windows in window chain from win on.
167 if (0 != win->next) {
168 draw_windows (win->next); } }
170 void draw_all_windows (struct WinMeta * win_meta) {
171 // Draw all windows and their borders.
173 wnoutrefresh(win_meta->screen);
174 werase(win_meta->pad);
175 if (win_meta->chain_start) {
177 struct Win * win_p = win_meta->chain_start;
178 while (0 != win_p->next) {
181 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
182 draw_windows (win_meta->chain_start);
183 draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
185 for (i = 0; i < n_wins; i++) {
186 mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
187 mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
188 mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
189 mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
190 pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1);
194 void resize_window (struct WinMeta * win_meta, char change) {
195 // Grow or shrink currently active window. Correct its geometry and that of its followers.
196 if (change == '-' && win_meta->active->height > 1)
197 win_meta->active->height--;
198 else if (change == '+' && win_meta->active->height < win_meta->height - 1)
199 win_meta->active->height++;
200 else if (change == '_' && win_meta->active->width > 1)
201 win_meta->active->width--;
202 else if (change == '*')
203 win_meta->active->width++;
204 update_windows(win_meta, win_meta->chain_start); }
206 void cycle_active_window (struct WinMeta * win_meta, char dir) {
207 // Cycle active window selection forwards (dir = 'n') or backwards.
208 if (0 != win_meta->active) {
210 if (win_meta->active->next != 0)
211 win_meta->active = win_meta->active->next;
213 win_meta->active = win_meta->chain_start; }
215 if (win_meta->active->prev != 0)
216 win_meta->active = win_meta->active->prev;
218 win_meta->active = win_meta->chain_end; } } }
220 void shift_window (struct WinMeta * win_meta, char dir) {
221 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
222 if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
224 struct Win * win_shift = win_meta->active;
226 if ((dir == 'f' && win_shift == win_meta->chain_end)
227 || (dir == 'b' && win_shift == win_meta->chain_start))
229 struct Win * win_p, * win_p_next;
230 for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
232 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
233 for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
234 win_p_next = win_p->next;
235 suspend_window(win_meta, win_p);
237 win_p = win_p_next; }
240 append_window(win_meta, win_shift);
241 for (i = 0; i < i_max - 1; i++)
242 append_window(win_meta, wins[i]); }
244 for (i = 1; i < i_max; i++)
245 append_window(win_meta, wins[i]);
246 append_window(win_meta, win_shift); }
248 for (i = 0; i < i_max; i++)
249 if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
250 append_window(win_meta, wins[i+1]);
251 append_window(win_meta, wins[i]);
254 append_window(win_meta, wins[i]);
256 win_meta->active = win_shift; } }