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