home · contact · privacy
Use a Win struct starty attribute too, instead of endlessly calling getbegy on curses...
[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 (win_top->starty != 1)
105       win_top = win_top->prev;                                   // else, default to placing window in new top
106     start.x = win_top->startx + win_top->width + 1;              // column to the right of the last one
107     uint16_t winprev_maxy = win->prev->starty + getmaxy(win->prev->curses);
108     if (win->width <= win->prev->width && win->height < win_meta->height - winprev_maxy) {
109       start.x = win->prev->startx;                         // 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 (win_up->starty != win_upup->starty)
119             break;
120           win_upup = win_upup->prev; }
121         winprev_maxy = win_upup->starty + getmaxy(win_upup->curses);
122         widthdiff = (win_upup->startx + win_upup->width) - (win_up->startx + win_up->width);
123         if (win->height < win_meta->height - winprev_maxy && win->width < widthdiff) {
124           start.x = win_up->startx + 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   win->starty = startyx.y;
137   refit_pad(win_meta);
138   win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
139   if (0 != win->next)
140     update_windows (win_meta, win->next); }
141
142 static void destroy_window (struct Win * win) {
143 // Delete window.
144   delwin(win->curses);
145   win->curses = 0; }
146
147 static void draw_window_borders (struct Win * win, char active) {
148 // Draw borders of window win, including title. Decorate in a special way if window is marked as active.
149   uint16_t y, x;
150   for (y = win->starty; y <= win->starty + win->height; y++) {
151     mvwaddch(wgetparent(win->curses), y, win->startx - 1, '|');
152     mvwaddch(wgetparent(win->curses), y, win->startx + win->width, '|'); }
153   for (x = win->startx; x <= win->startx + win->width; x++) {
154     mvwaddch(wgetparent(win->curses), win->starty - 1, x, '-');
155     mvwaddch(wgetparent(win->curses), win->starty + win->height, x, '-'); }
156   char min_title_length_visible = 3;        // 1 char minimal, plus 2 chars for decoration left/right of title
157   if (win->width >= min_title_length_visible) {
158     uint16_t title_offset = 0;
159     if (win->width > strlen(win->title) + 2)
160       title_offset = (win->width - (strlen(win->title) + 2)) / 2;                     // + 2 is for decoration
161     uint16_t length_visible = strnlen(win->title, win->width - 2);
162     char title[length_visible + 3];
163     char decoration = ' ';
164     if (1 == active)
165       decoration = '$';
166     memcpy(title + 1, win->title, length_visible);
167     title[0] = title[length_visible + 1] = decoration;
168     title[length_visible + 2] = '\0';
169     mvwaddstr(wgetparent(win->curses), win->starty - 1, win->startx + title_offset, title); } }
170
171 static void draw_windows_borders (struct Win * win, struct Win * win_active, struct Corners * corners, uint16_t ccount) {
172 // Craw draw_window_borders() for all windows in chain from win on. Save current window's border corners.
173   char active = 0;
174   if (win == win_active)
175     active = 1;
176   draw_window_borders(win, active);
177   corners[ccount].tl.y = win->starty - 1;
178   corners[ccount].tl.x = win->startx - 1;
179   corners[ccount].tr.y = win->starty - 1;
180   corners[ccount].tr.x = win->startx + win->width;
181   corners[ccount].bl.y = win->starty + win->height;
182   corners[ccount].bl.x = win->startx - 1;
183   corners[ccount].br.y = win->starty + win->height;
184   corners[ccount].br.x = win->startx + win->width;
185   if (0 != win->next) {
186     draw_windows_borders (win->next, win_active, corners, ccount + 1); } }
187
188 static void draw_windows (struct Win * win) {
189 // Draw contents of all windows in window chain from win on.
190   win->draw(win);
191   if (0 != win->next) {
192     draw_windows (win->next); } }
193
194 static void draw_vertical_scroll_hint (struct WinMeta * win_meta, uint16_t x, uint32_t more_cols, char dir) {
195 // Draw scroll hint line in win at col x of pad display, announce more_cols more columns in direction dir.
196   uint16_t y, offset;
197   char phrase[] = "more columns";
198   char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10); // 10 = max chars for uint32_t string
199   sprintf(scrolldesc, " %d %s ", more_cols, phrase);
200   offset = 1;
201   if (win_meta->height > (strlen(scrolldesc) + 1))
202     offset = (win_meta->height - strlen(scrolldesc)) / 2;
203   for (y = 0; y < win_meta->height; y++)
204     if (y >= offset && y < strlen(scrolldesc) + offset)
205       mvwaddch(win_meta->pad, y, x, scrolldesc[y - offset] | A_REVERSE);
206     else
207       mvwaddch(win_meta->pad, y, x, dir | A_REVERSE);
208   free(scrolldesc); }
209
210 extern void draw_all_windows (struct WinMeta * win_meta) {
211 // Draw pad with all windows and their borders, plus scrolling hints.
212   erase();
213   wnoutrefresh(win_meta->screen);
214   werase(win_meta->pad);
215   if (win_meta->chain_start) {
216     uint16_t n_wins = 1;
217     struct Win * win_p = win_meta->chain_start;
218     while (0 != win_p->next) {
219       win_p = win_p->next;
220       n_wins++; }
221     struct Corners * all_corners = malloc(sizeof(struct Corners) * n_wins);
222     draw_windows (win_meta->chain_start);
223     draw_windows_borders (win_meta->chain_start, win_meta->active, all_corners, 0);
224     uint16_t i;
225     for (i = 0; i < n_wins; i++) {
226       mvwaddch(win_meta->pad, all_corners[i].tl.y, all_corners[i].tl.x, '+');
227       mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
228       mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
229       mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
230     free(all_corners);
231     uint16_t y;
232     if (win_meta->pad_offset > 0)
233       draw_vertical_scroll_hint(win_meta, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
234     if (win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
235       for (y = 0; y < win_meta->height; y++)
236         draw_vertical_scroll_hint(win_meta, win_meta->pad_offset + win_meta->width - 1,
237                                   getmaxx(win_meta->pad) - (win_meta->pad_offset + win_meta->width), '>');
238     pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1); }
239   doupdate(); }
240
241 extern void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
242 // Grow or shrink currently active window. Correct its geometry and that of its followers.
243   if (0 != win_meta->active && width > 0 && height > 0 && height < win_meta->height) {
244     win_meta->active->height = height;
245     win_meta->active->width  = width;
246     update_windows(win_meta, win_meta->chain_start); } }
247
248 extern void cycle_active_window (struct WinMeta * win_meta, char dir) {
249 // Cycle active window selection forwards (dir = 'n') or backwards.
250   if (0 != win_meta->active) {
251     if ('n' == dir) {
252       if (win_meta->active->next != 0)
253         win_meta->active = win_meta->active->next;
254       else
255         win_meta->active = win_meta->chain_start; }
256     else {
257       if (win_meta->active->prev != 0)
258         win_meta->active = win_meta->active->prev;
259       else
260         win_meta->active = win_meta->chain_end; } } }
261
262 extern void shift_active_window (struct WinMeta * win_meta, char dir) {
263 // Move active window forward/backward in window chain. If jumping beyond start/end, move to other chain end.
264   if (0 != win_meta->active && win_meta->chain_start != win_meta->chain_end && (dir == 'f' || dir == 'b')) {
265     struct Win * win_shift = win_meta->active, * win_p, * win_p_next;
266     char wrap = 0;
267     if ((dir == 'f' && win_shift == win_meta->chain_end)
268         || (dir == 'b' && win_shift == win_meta->chain_start))
269       wrap = 1;
270     uint16_t i, i_max;
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; }