home · contact · privacy
Minor intendation correction.
[plomrogue] / windows.c
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <ncurses.h>
4 #include <string.h>
5 #include "windows.h"
6
7 struct yx {
8   uint16_t y;
9   uint16_t x; };
10
11 struct Corners {
12   struct yx tl;
13   struct yx tr;
14   struct yx bl;
15   struct yx br; };
16
17 static struct yx place_window (struct WinMeta *, struct Win *);
18 static void update_windows (struct WinMeta *, struct Win *);
19 static void destroy_window (struct Win *);
20 static void draw_windows_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
21 static void draw_window_borders (struct Win *, char);
22 static void draw_windows (struct Win *);
23 static void draw_vertical_scroll_hint (struct WinMeta *, uint16_t, uint32_t, char);
24
25 extern struct WinMeta init_win_meta (WINDOW * screen) {
26 // Create and populate WinMeta struct with sane default values.
27   struct WinMeta win_meta;
28   win_meta.screen = screen;
29   win_meta.height = getmaxy(screen);
30   win_meta.width = getmaxx(screen);
31   win_meta.chain_start = 0;
32   win_meta.chain_end = 0;
33   win_meta.pad_offset = 0;
34   win_meta.pad = newpad(win_meta.height, 1);
35   win_meta.active = 0;
36   return win_meta; }
37
38 extern struct Win init_window (struct WinMeta * win_meta, char * title, void * data, void * func) {
39 // Create and populate Win struct with sane default values.
40   struct Win win;
41   win.prev = 0;
42   win.next = 0;
43   win.curses = 0;
44   win.title = title;
45   win.width = 20;
46   win.height = win_meta->height - 1;
47   win.data = data;
48   win.draw = func;
49   return win; }
50
51 extern void append_window (struct WinMeta * win_meta, struct Win * win) {
52 // Append win to window chain. Set active, if first window. Update geometry of windows from new window on.
53   if (0 != win_meta->chain_start) {
54     win->prev = win_meta->chain_end;
55     win_meta->chain_end->next = win; }
56   else {
57     win_meta->active = win;
58     win_meta->chain_start = win; }
59   win_meta->chain_end = win;
60   update_windows(win_meta, win); }
61
62 extern void suspend_window (struct WinMeta * win_meta, struct Win * win) {
63 // Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
64   destroy_window(win);
65   if (win_meta->chain_start != win)    // Give win's position in the chain to element next to it in the chain.
66     win->prev->next = win->next;
67   else
68     win_meta->chain_start = win->next;
69   char pad_refitted = 0;
70   if (win_meta->chain_end != win) {                 // Let chain element next to win know its new predecessor.
71     win->next->prev = win->prev;
72     if (win_meta->active == win)                          // If win was active, shift active window pointer to
73       win_meta->active = win->next;                       // the next chain element, if that is a window ...
74     update_windows(win_meta, win->next);
75     pad_refitted = 1; }
76   else {
77     win_meta->chain_end = win->prev;
78     if (win_meta->active == win)                                       // ... or else to the previous element.
79       win_meta->active = win->prev; }
80   win->prev = 0;
81   win->next = 0;
82   if (0 == pad_refitted) {                                                          // Refit pad if necessary.
83     uint16_t lastwincol = 0;
84     struct Win * win_p = win_meta->chain_start;
85     while (win_p != 0) {
86       if (getbegx(win_p->curses) + win_p->width > lastwincol + 1)
87         lastwincol = getbegx(win_p->curses) + win_p->width - 1;
88       win_p = win_p->next; }
89     if (getmaxx(win_meta->pad) != lastwincol)
90       wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); } }
91
92 static struct yx 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   struct yx start;
95   start.x = 0;                                     // if window is first in chain, place it on top-left corner
96   start.y = 1;
97   if (0 != win->prev) {
98     struct Win * win_top = win->prev;
99     while (getbegy(win_top->curses) != 1)
100       win_top = win_top->prev;                                   // else, default to placing window in new top
101     start.x = getbegx(win_top->curses) + win_top->width + 1;     // column to the right of the last one
102     uint16_t winprev_maxy = getbegy(win->prev->curses) + getmaxy(win->prev->curses);
103     if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
104       start.x = getbegx(win->prev->curses);                // place window below previous window if it fits
105       start.y = winprev_maxy + 1; }                        // vertically and is not wider than its predecessor
106     else {
107       struct Win * win_up = win->prev;
108       struct Win * win_upup = win_up;
109       uint16_t widthdiff;
110       while (win_up != win_top) {
111         win_upup = win_up->prev;
112         while (1) {
113           if (getbegy(win_up->curses) != getbegy(win_upup->curses))
114             break;
115           win_upup = win_upup->prev; }
116         winprev_maxy = getbegy(win_upup->curses) + getmaxy(win_upup->curses);
117         widthdiff = (getbegx(win_upup->curses) + win_upup->width) - (getbegx(win_up->curses) + win_up->width);
118         if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
119           start.x = getbegx(win_up->curses) + win_up->width + 1; // else try to open new sub column under last
120           start.y = winprev_maxy + 1;                            // window below which enough space remains
121           break; }
122         win_up = win_upup; } } }
123   return start; }
124
125 static void update_windows (struct WinMeta * win_meta, struct Win * win) {
126 // Update geometry of win and its next of kin. Destroy (if visible), (re-)build window. If need, resize pad.
127   if (0 != win->curses)
128     destroy_window (win);
129   struct yx startyx = place_window(win_meta, win);
130   uint16_t lastwincol = 0;
131   struct Win * win_p = win_meta->chain_start;
132   while (win_p != 0) {
133     if (win_p != win && getbegx(win_p->curses) + win_p->width > lastwincol + 1)
134       lastwincol = getbegx(win_p->curses) + win_p->width - 1;
135     else if (win_p == win && startyx.x + win->width > lastwincol + 1)
136       lastwincol = startyx.x + win->width - 1;
137     win_p = win_p->next; }
138   if (getmaxx(win_meta->pad) != lastwincol)
139     wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2);
140   win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
141   if (0 != win->next)
142     update_windows (win_meta, win->next); }
143
144 static void destroy_window (struct Win * win) {
145 // Delete window.
146   delwin(win->curses);
147   win->curses = 0; }
148
149 static void draw_window_borders (struct Win * win, char active) {
150 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
151   uint16_t y, x;
152   for (y = getbegy(win->curses); y <= getbegy(win->curses) + win->height; y++) {
153     mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) - 1, '|');
154     mvwaddch(wgetparent(win->curses), y, getbegx(win->curses) + win->width, '|'); }
155   for (x = getbegx(win->curses); x <= getbegx(win->curses) + win->width; x++) {
156     mvwaddch(wgetparent(win->curses), getbegy(win->curses) - 1, x, '-');
157     mvwaddch(wgetparent(win->curses), getbegy(win->curses) + win->height, x, '-'); }
158   char min_title_length_visible = 3;        // 1 char minimal, plus 2 chars for decoration left/right of title
159   if (win->width >= min_title_length_visible) {
160     uint16_t title_offset = 0;
161     if (win->width > strlen(win->title) + 2)
162       title_offset = (win->width - (strlen(win->title) + 2)) / 2;                     // + 2 is for decoration
163     uint16_t length_visible = strnlen(win->title, win->width - 2);
164     char title[length_visible + 3];
165     char decoration = ' ';
166     if (1 == active)
167       decoration = '$';
168     memcpy(title + 1, win->title, length_visible);
169     title[0] = title[length_visible + 1] = decoration;
170     title[length_visible + 2] = '\0';
171     mvwaddstr (wgetparent(win->curses), getbegy(win->curses)-1, getbegx(win->curses)+title_offset, title); } }
172
173 static void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, uint16_t ccount) {
174 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
175   char active = 0;
176   if (win == win_active)
177     active = 1;
178   draw_window_borders(win, active);
179   corners[ccount].tl.y = getbegy(win->curses) - 1;
180   corners[ccount].tl.x = getbegx(win->curses) - 1;
181   corners[ccount].tr.y = getbegy(win->curses) - 1;
182   corners[ccount].tr.x = getbegx(win->curses) + win->width;
183   corners[ccount].bl.y = getbegy(win->curses) + win->height;
184   corners[ccount].bl.x = getbegx(win->curses) - 1;
185   corners[ccount].br.y = getbegy(win->curses) + win->height;
186   corners[ccount].br.x = getbegx(win->curses) + win->width;
187   if (0 != win->next) {
188     draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
189
190 static void draw_windows (struct Win * win) {
191 // Draw contents of all windows in window chain from win on.
192   win->draw(win);
193   if (0 != win->next) {
194     draw_windows (win->next); } }
195
196 static void draw_vertical_scroll_hint (struct WinMeta * win_meta, uint16_t x, uint32_t more_cols, char dir) {
197 // Draw scroll hint line in win at col x of pad display, announce more_cols more columns in direction dir.
198   uint16_t y, offset;
199   char phrase[] = "more columns";
200   char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10); // 10 = max chars for uint32_t string
201   sprintf(scrolldesc, " %d %s ", more_cols, phrase);
202   offset = 1;
203   if (win_meta->height > (strlen(scrolldesc) + 1))
204     offset = (win_meta->height - strlen(scrolldesc)) / 2;
205   for (y = 0; y < win_meta->height; y++)
206     if (y >= offset && y < strlen(scrolldesc) + offset)
207       mvwaddch(win_meta->pad, y, x, scrolldesc[y - offset] | A_REVERSE);
208     else
209       mvwaddch(win_meta->pad, y, x, dir | A_REVERSE);
210   free(scrolldesc); }
211
212 extern void draw_all_windows (struct WinMeta * win_meta) {
213 // Draw pad with all windows and their borders, plus scrolling hints.
214   erase();
215   wnoutrefresh(win_meta->screen);
216   werase(win_meta->pad);
217   if (win_meta->chain_start) {
218     uint16_t n_wins = 1;
219     struct Win * win_p = win_meta->chain_start;
220     while (0 != win_p->next) {
221       win_p = win_p->next;
222       n_wins++; }
223     struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
224     draw_windows (win_meta->chain_start);
225     draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
226     uint16_t i;
227     for (i = 0; i < n_wins; i++) {
228       mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
229       mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
230       mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
231       mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
232     free(all_corners);
233     uint16_t y;
234     if (win_meta->pad_offset > 0)
235       draw_vertical_scroll_hint(win_meta, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
236     if (win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
237       for (y = 0; y < win_meta->height; y++)
238         draw_vertical_scroll_hint(win_meta, win_meta->pad_offset + win_meta->width - 1,
239                                   getmaxx(win_meta->pad) - (win_meta->pad_offset + win_meta->width), '>');
240     pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1); }
241   doupdate(); }
242
243 extern void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
244 // Grow or shrink currently active window. Correct its geometry and that of its followers.
245   if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->height) {
246     win_meta->active->height = height;
247     win_meta->active->width  = width;
248     update_windows(win_meta, win_meta->chain_start); } }
249
250 extern void cycle_active_window (struct WinMeta * win_meta, char dir) {
251 // Cycle active window selection forwards (dir = 'n') or backwards.
252   if (0 != win_meta->active) {
253     if ('n' == dir) {
254       if (win_meta->active->next != 0)
255         win_meta->active = win_meta->active->next;
256       else
257         win_meta->active = win_meta->chain_start; }
258     else {
259       if (win_meta->active->prev != 0)
260         win_meta->active = win_meta->active->prev;
261       else
262         win_meta->active = win_meta->chain_end; } } }
263
264 extern void shift_active_window (struct WinMeta * win_meta, char dir) {
265 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
266   if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
267     struct Win * win_shift = win_meta->active;
268     char wrap = 0;
269     if ((dir == 'f' && win_shift == win_meta->chain_end)
270         || (dir == 'b' && win_shift == win_meta->chain_start))
271       wrap = 1;
272     uint16_t i, i_max;
273     struct Win * win_p, * win_p_next;
274     for (i_max = 1, win_p = win_meta->chain_start; win_p != win_meta->chain_end; i_max++)
275       win_p = win_p->next;
276     struct Win ** wins = malloc(i_max * sizeof(struct Win *));
277     for (i = 0, win_p = win_meta->chain_start; i < i_max; i++) {
278       win_p_next = win_p->next;
279       suspend_window(win_meta, win_p);
280       wins[i] = win_p;
281       win_p = win_p_next; }
282     if (wrap)
283       if (dir == 'f') {
284         append_window(win_meta, win_shift);
285         for (i = 0; i < i_max - 1; i++)
286           append_window(win_meta, wins[i]); }
287       else {
288         for (i = 1; i < i_max; i++)
289           append_window(win_meta, wins[i]);
290         append_window(win_meta, win_shift); }
291     else
292       for (i = 0; i < i_max; i++)
293         if ((dir == 'f' && win_shift == wins[i]) || (dir == 'b' && win_shift == wins[i+1])) {
294           append_window(win_meta, wins[i+1]);
295           append_window(win_meta, wins[i]);
296           i++; }
297         else
298           append_window(win_meta, wins[i]);
299     free(wins);
300     win_meta->active = win_shift; } }
301
302 extern void reset_pad_offset(struct WinMeta * win_meta, uint16_t new_offset) {
303 // Apply new_offset to windows pad, if it proves to be sane.
304   if (new_offset >= 0 && new_offset + win_meta->width < getmaxx(win_meta->pad))
305     win_meta->pad_offset = new_offset; }