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.width = screen->_maxx + 1;
11 win_meta.chain_start = 0;
12 win_meta.chain_end = 0;
15 struct Win init_window (struct WinMeta * win_meta, char * title) {
16 // Create and populate Win struct with sane default values.
23 win.height = win_meta->height - 1;
26 void append_window (struct WinMeta * win_meta, struct Win * win) {
27 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
28 if (0 != win_meta->chain_start) {
29 win->prev = win_meta->chain_end;
30 win_meta->chain_end->next = win; }
32 win_meta->active = win;
33 win_meta->chain_start = win; }
34 win_meta->chain_end = win;
35 update_windows(win_meta, win); }
37 void suspend_window (struct WinMeta * win_meta, struct Win * win) {
38 // Destroy win, suspend from window chain. Update geometry of following rows, as well as activity selection.
40 if (win_meta->chain_start != win) // Give win's position in the chain to element next to it in the chain.
41 win->prev->next = win->next;
43 win_meta->chain_start = win->next;
44 if (win_meta->chain_end != win) { // Let chain element next to win know its new predecessor.
45 win->next->prev = win->prev;
46 if (win_meta->active == win) // If win was active, shift active window pointer to ...
47 win_meta->active = win->next; // ... the next chain element, if that is a window ...
48 update_windows(win_meta, win->next); }
50 win_meta->chain_end = win->prev;
51 if (win_meta->active == win) // ... or else to the previous element.
52 win_meta->active = win->prev; }
56 struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
57 // Based on position and sizes of previous window, find fitting place for current window.
59 start.x = 0; // if window is first in chain, place it on top-left corner
62 if (win->prev->height == win_meta->height - 1) // if prev window fills entire column,
63 start.x = win->prev->curses_win->_begx + win->prev->width + 1; // place win in new column next to it
65 struct Win * first_ceiling = win->prev; // first_ceiling determines column with;
66 while (first_ceiling->curses_win->_begy != 1) // default: place window in new column
67 first_ceiling = first_ceiling->prev; // next to it
68 start.x = first_ceiling->curses_win->_begx + first_ceiling->width + 1;
69 if (first_ceiling->width >= win->width) { // only place wins in prev column that fit into its width
70 struct Win * win_p = first_ceiling;
71 struct Win * lastrow_startwin = win_p;
72 while (win_p != win) {
73 if (win_p->curses_win->_begx == first_ceiling->curses_win->_begx)
74 lastrow_startwin = win_p; // try to fit window at the end of the last row of windows
75 win_p = win_p ->next; } // inside column; if failure, try opening a new row below
76 int lastcol_start = win->prev->curses_win->_begx + win->prev->width + 1;
77 if (win->width <= first_ceiling->curses_win->_begx + first_ceiling->width - lastcol_start
78 && win->height <= lastrow_startwin->height) {
79 start.x = lastcol_start;
80 start.y = lastrow_startwin->curses_win->_begy; }
81 else if (win->height < win_meta->height - (lastrow_startwin->curses_win->_begy + lastrow_startwin->height)
82 && win->width <= first_ceiling->width) {
83 start.x = first_ceiling->curses_win->_begx;
84 start.y = lastrow_startwin->curses_win->_begy + lastrow_startwin->height + 1; } } } }
87 void update_windows (struct WinMeta * win_meta, struct Win * win) {
88 // Update geometry of win and its next of kin. Before, destroy window, if visible. After, (re-)build it.
89 if (0 != win->curses_win)
91 struct yx startyx = place_window(win_meta, win);
92 win->curses_win = newwin(win->height, win->width, startyx.y, startyx.x);
94 update_windows (win_meta, win->next); }
96 void destroy_window (struct Win * win) {
97 // Undraw and delete window.
98 delwin(win->curses_win);
99 win->curses_win = 0; }
101 void draw_window_borders (struct Win * win, char active) {
102 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
104 for (y = win->curses_win->_begy; y <= win->curses_win->_begy + win->height; y++) {
105 mvaddch(y, win->curses_win->_begx - 1, '|');
106 mvaddch(y, win->curses_win->_begx + win->width, '|'); }
107 for (x = win->curses_win->_begx; x <= win->curses_win->_begx + win->width; x++) {
108 mvaddch(win->curses_win->_begy - 1, x, '-');
109 mvaddch(win->curses_win->_begy + win->height, x, '-'); }
110 char min_title_length_visible = 3; // 1 char minimal, plus 2 chars for decoration left/right of title
111 if (win->width > min_title_length_visible) {
112 int title_length = strlen(win->title);
113 int title_offset = (((win->width) - (title_length + 2)) / 2); // + 2 is for decoration
114 int length_visible = strnlen(win->title, win->width - min_title_length_visible);
115 char title[length_visible + 3];
116 char decoration = ' ';
119 memcpy(title + 1, win->title, length_visible);
120 title[0] = title[length_visible + 1] = decoration;
121 title[length_visible + 2] = '\0';
122 mvaddstr(win->curses_win->_begy - 1, win->curses_win->_begx + title_offset, title); }
125 void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, int ccount) {
126 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
128 if (win == win_active)
130 draw_window_borders(win, active);
131 corners[ccount].tl.y = win->curses_win->_begy - 1;
132 corners[ccount].tl.x = win->curses_win->_begx - 1;
133 corners[ccount].tr.y = win->curses_win->_begy - 1;
134 corners[ccount].tr.x = win->curses_win->_begx + win->width;
135 corners[ccount].bl.y = win->curses_win->_begy + win->height;
136 corners[ccount].bl.x = win->curses_win->_begx - 1;
137 corners[ccount].br.y = win->curses_win->_begy + win->height;
138 corners[ccount].br.x = win->curses_win->_begx + win->width;
139 if (0 != win->next) {
140 draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
142 void draw_windows (struct Win * win) {
143 // Draw contents of all windows in window chain from win on.
145 if (0 != win->next) {
146 draw_windows (win->next); } }
148 void draw_all_windows (struct WinMeta * win_meta) {
149 // Draw all windows and their borders.
151 for (y = 0; y < win_meta->height; y++)
152 for (x = 0; x < win_meta->width; x++)
154 if (win_meta->chain_start) {
156 struct Win * win_p = win_meta->chain_start;
157 while (0 != win_p->next) {
160 struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
161 draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
162 draw_windows (win_meta->chain_start);
163 for (y = 0; y < n_wins; y++) {
164 mvaddch(all_corners[y].tl.y, all_corners[y].tl.x, 'o');
165 mvaddch(all_corners[y].tr.y, all_corners[y].tr.x, 'o');
166 mvaddch(all_corners[y].bl.y, all_corners[y].bl.x, 'o');
167 mvaddch(all_corners[y].br.y, all_corners[y].br.x, 'o'); }
168 free(all_corners); } }
170 void draw_window(struct Win * win) {
171 // Draw window content if visible.
172 if (win->height > 1 && win->width > 1) ;
174 wrefresh(win->curses_win); }
176 void resize_window (struct WinMeta * win_meta, char change) {
177 // Grow or shrink currently active window. Correct its geometry and that of its followers.
178 if (change == '-' && win_meta->active->height > 1)
179 win_meta->active->height--;
180 else if (change == '+' && win_meta->active->height < win_meta->height - 1)
181 win_meta->active->height++;
182 else if (change == '_' && win_meta->active->width > 1)
183 win_meta->active->width--;
184 else if (change == '*')
185 win_meta->active->width++;
186 update_windows(win_meta, win_meta->chain_start); }
188 void cycle_active_window (struct WinMeta * win_meta, char dir) {
189 // Cycle active window selection forwards (dir = 'n') or backwards.
191 if (win_meta->active->next != 0)
192 win_meta->active = win_meta->active->next;
194 win_meta->active = win_meta->chain_start; }
196 if (win_meta->active->prev != 0)
197 win_meta->active = win_meta->active->prev;
199 win_meta->active = win_meta->chain_end; } }
201 void shift_window (struct WinMeta * win_meta, char dir) {
202 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
203 if (win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
205 struct Win * win_shift = win_meta->active;
207 if ((dir == 'f' && win_shift == win_meta->chain_end)
208 || (dir == 'b' && win_shift == win_meta->chain_start))
210 struct Win * win_p, * win_p_next;
211 for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++, win_p = win_p->next);
212 struct Win ** wins = malloc(i_max * sizeof(struct Win *));
213 for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
214 win_p_next = win_p->next;
215 suspend_window(win_meta, win_p);
217 win_p = win_p_next; }
220 append_window(win_meta, win_shift);
221 for (i = 0; i < i_max - 1; i++)
222 append_window(win_meta, wins[i]); }
224 for (i = 1; i < i_max; i++)
225 append_window(win_meta, wins[i]);
226 append_window(win_meta, win_shift); }
228 for (i = 0; i < i_max; i++)
229 if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
230 append_window(win_meta, wins[i+1]);
231 append_window(win_meta, wins[i]);
234 append_window(win_meta, wins[i]);
236 win_meta->active = win_shift; } }