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