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.height = screen->_maxy + 1;
10 win_meta.chain_start = 0;
11 win_meta.chain_end = 0;
14 struct Win init_window (struct WinMeta * win_meta, char * title) {
15 // Create and populate Win struct with sane default values.
22 win.height = win_meta->height - 1;
25 void append_window (struct WinMeta * win_meta, struct Win * win) {
26 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
27 if (0 != win_meta->chain_start) {
28 win->prev = win_meta->chain_end;
29 win_meta->chain_end->next = win; }
31 win_meta->active = win;
32 win_meta->chain_start = win; }
33 win_meta->chain_end = win;
34 update_windows(win_meta, win); }
36 void suspend_window (struct WinMeta * win_meta, struct Win * win) {
37 // Destroy win, suspend from window chain. Update geometry of following rows, as well as activity selection.
39 if (win_meta->chain_start != win) // Give win's position in the chain to element next to it in the chain.
40 win->prev->next = win->next;
42 win_meta->chain_start = win->next;
43 if (win_meta->chain_end != win) { // Let chain element next to win know its new predecessor.
44 win->next->prev = win->prev;
45 if (win_meta->active == win) // If win was active, shift active window pointer to ...
46 win_meta->active = win->next; // ... the next chain element, if that is a window ...
47 update_windows(win_meta, win->next); }
49 win_meta->chain_end = win->prev;
50 if (win_meta->active == win) // ... or else to the previous element.
51 win_meta->active = win->prev; }
55 struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
56 // Based on position and sizes of previous window, find fitting place for current window.
58 start.x = 0; // if window is first in chain, place it on top-left corner
61 if (win->prev->height == win_meta->height - 1) // if prev window fills entire column,
62 start.x = win->prev->curses_win->_begx + win->prev->width + 1; // place win in new column next to it
64 struct Win * first_ceiling = win->prev; // first_ceiling determines column with;
65 while (first_ceiling->curses_win->_begy != 1) // default: place window in new column
66 first_ceiling = first_ceiling->prev; // next to it
67 start.x = first_ceiling->curses_win->_begx + first_ceiling->width + 1;
68 if (first_ceiling->width >= win->width) { // only place wins in prev column that fit into its width
69 struct Win * win_p = first_ceiling;
70 struct Win * lastrow_startwin = win_p;
71 while (win_p != win) {
72 if (win_p->curses_win->_begx == first_ceiling->curses_win->_begx)
73 lastrow_startwin = win_p; // try to fit window at the end of the last row of windows
74 win_p = win_p ->next; } // inside column; if failure, try opening a new row below
75 int lastcol_start = win->prev->curses_win->_begx + win->prev->width + 1;
76 if (win->width <= first_ceiling->curses_win->_begx + first_ceiling->width - lastcol_start
77 && win->height <= lastrow_startwin->height) {
78 start.x = lastcol_start;
79 start.y = lastrow_startwin->curses_win->_begy; }
80 else if (win->height < win_meta->height - (lastrow_startwin->curses_win->_begy + lastrow_startwin->height)
81 && win->width <= first_ceiling->width) {
82 start.x = first_ceiling->curses_win->_begx;
83 start.y = lastrow_startwin->curses_win->_begy + lastrow_startwin->height + 1; } } } }
86 void update_windows (struct WinMeta * win_meta, struct Win * win) {
87 // Update geometry of win and its next of kin. Before, destroy window, if visible. After, (re-)build it.
88 if (0 != win->curses_win)
90 struct yx startyx = place_window(win_meta, win);
91 win->curses_win = newwin(win->height, win->width, startyx.y, startyx.x);
93 update_windows (win_meta, win->next); }
95 void destroy_window (struct Win * win) {
97 delwin(win->curses_win);
98 win->curses_win = 0; }
100 void draw_window_borders (struct Win * win, char active) {
101 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
103 for (y = win->curses_win->_begy; y <= win->curses_win->_begy + win->height; y++) {
104 mvaddch(y, win->curses_win->_begx - 1, '|');
105 mvaddch(y, win->curses_win->_begx + win->width, '|'); }
106 for (x = win->curses_win->_begx; x <= win->curses_win->_begx + win->width; x++) {
107 mvaddch(win->curses_win->_begy - 1, x, '-');
108 mvaddch(win->curses_win->_begy + win->height, x, '-'); }
109 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
110 if (win->width > min_title_length_visible) {
111 int title_length = strlen(win->title);
112 int title_offset = (((win->width) - (title_length + 2)) / 2); // + 2 is for decoration
113 int length_visible = strnlen(win->title, win->width - min_title_length_visible);
114 char title[length_visible + 3];
115 char decoration = ' ';
118 memcpy(title + 1, win->title, length_visible);
119 title[0] = title[length_visible + 1] = decoration;
120 title[length_visible + 2] = '\0';
121 mvaddstr(win->curses_win->_begy - 1, win->curses_win->_begx + title_offset, title); }
124 void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, int ccount) {
125 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
127 if (win == win_active)
129 draw_window_borders(win, active);
130 corners[ccount].tl.y = win->curses_win->_begy - 1;
131 corners[ccount].tl.x = win->curses_win->_begx - 1;
132 corners[ccount].tr.y = win->curses_win->_begy - 1;
133 corners[ccount].tr.x = win->curses_win->_begx + win->width;
134 corners[ccount].bl.y = win->curses_win->_begy + win->height;
135 corners[ccount].bl.x = win->curses_win->_begx - 1;
136 corners[ccount].br.y = win->curses_win->_begy + win->height;
137 corners[ccount].br.x = win->curses_win->_begx + win->width;
138 if (0 != win->next) {
139 draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
141 void draw_windows (struct Win * win) {
142 // Draw contents of all windows in window chain from win on.
144 if (0 != win->next) {
145 draw_windows (win->next); } }
147 void draw_all_windows (struct WinMeta * win_meta) {
148 // Draw all windows and their borders.
150 if (win_meta->chain_start) {
152 struct Win * win_p = win_meta->chain_start;
153 while (0 != win_p->next) {
156 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
157 draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
158 draw_windows (win_meta->chain_start);
160 for (i = 0; i < n_wins; i++) {
161 mvaddch(all_corners[i].tl.y, all_corners[i].tl.x, '+');
162 mvaddch(all_corners[i].tr.y, all_corners[i].tr.x, '+');
163 mvaddch(all_corners[i].bl.y, all_corners[i].bl.x, '+');
164 mvaddch(all_corners[i].br.y, all_corners[i].br.x, '+'); }
165 free(all_corners); } }
167 void draw_window(struct Win * win) {
168 // Draw window content if visible.
169 if (win->height > 1 && win->width > 1) ;
171 wrefresh(win->curses_win); }
173 void resize_window (struct WinMeta * win_meta, char change) {
174 // Grow or shrink currently active window. Correct its geometry and that of its followers.
175 if (change == '-' && win_meta->active->height > 1)
176 win_meta->active->height--;
177 else if (change == '+' && win_meta->active->height < win_meta->height - 1)
178 win_meta->active->height++;
179 else if (change == '_' && win_meta->active->width > 1)
180 win_meta->active->width--;
181 else if (change == '*')
182 win_meta->active->width++;
183 update_windows(win_meta, win_meta->chain_start); }
185 void cycle_active_window (struct WinMeta * win_meta, char dir) {
186 // Cycle active window selection forwards (dir = 'n') or backwards.
188 if (win_meta->active->next != 0)
189 win_meta->active = win_meta->active->next;
191 win_meta->active = win_meta->chain_start; }
193 if (win_meta->active->prev != 0)
194 win_meta->active = win_meta->active->prev;
196 win_meta->active = win_meta->chain_end; } }
198 void shift_window (struct WinMeta * win_meta, char dir) {
199 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
200 if (win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
202 struct Win * win_shift = win_meta->active;
204 if ((dir == 'f' && win_shift == win_meta->chain_end)
205 || (dir == 'b' && win_shift == win_meta->chain_start))
207 struct Win * win_p, * win_p_next;
208 for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++, win_p = win_p->next);
209 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
210 for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
211 win_p_next = win_p->next;
212 suspend_window(win_meta, win_p);
214 win_p = win_p_next; }
217 append_window(win_meta, win_shift);
218 for (i = 0; i < i_max - 1; i++)
219 append_window(win_meta, wins[i]); }
221 for (i = 1; i < i_max; i++)
222 append_window(win_meta, wins[i]);
223 append_window(win_meta, win_shift); }
225 for (i = 0; i < i_max; i++)
226 if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
227 append_window(win_meta, wins[i+1]);
228 append_window(win_meta, wins[i]);
231 append_window(win_meta, wins[i]);
233 win_meta->active = win_shift; } }