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